传递 MTLTexture 时崩溃
Crash with passing about a MTLTexture
这是我的场景:
在 MTKView
(以及其中的单个命令缓冲区)中,我有一个 MTLTexture
是 MTLComputeEncoder
操作的结果。
我在 MTKView
上的 draw
方法被称为 dispatch_get_global_queue
的高优先级实例。在那个 MTKView
中 drawRect
的尾端,我 "scoop" 那个纹理并将它设置为另一个 MTKView
上的 属性。然后,我分派一个低优先级线程来调用该视图的 draw
方法,将原始纹理的一个区域复制到第二个 MTKView
,以及最终的 presentDrawable
调用——全部在不同的命令缓冲区。同时,原始的 MTKView
结束了 draw
方法,它自己调用了 presentDrawable
.
我应该提到这两个 MTKViews
都配置了 isPaused=YES
和 enableSetNeedsDisplay=NO
。
这一切工作得相当好,但我偶尔会因“[CAMetalLayerDrawable texture] should not be called after presenting the drawable
”警告或某些无法识别的线程中的偶尔崩溃而崩溃:
QuartzCore`-[CAMetalDrawable present]:
0x7fff881cf471 <+0>: pushq %rbp
0x7fff881cf472 <+1>: movq %rsp, %rbp
0x7fff881cf475 <+4>: movq %rdi, %rax
0x7fff881cf478 <+7>: movq 0x15f25ce1(%rip), %rcx ; CAMetalDrawable._priv
0x7fff881cf47f <+14>: movq (%rax,%rcx), %rcx
0x7fff881cf483 <+18>: movq 0x20(%rcx), %rdi <---- crashes here
0x7fff881cf487 <+22>: xorps %xmm0, %xmm0
0x7fff881cf48a <+25>: movq %rax, %rsi
0x7fff881cf48d <+28>: popq %rbp
0x7fff881cf48e <+29>: jmp 0x7fff881cf493 ; layer_private_present(_CAMetalLayerPrivate*, CAMetalDrawable*, double, unsigned int)
如果我remove/comment去掉辅助"scoop"重绘原来的贴图,就不会崩溃了。
有没有更容易接受的方法来完成这个?可能使用 MTLBlitCommandEncoder
显式复制所提供纹理的内容?
更新#1
让代码更清晰一点是一个明智的想法,可能:
这是主要MTKView
的drawRect
方法,由dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
驱动:
- (void)drawRect:(NSRect)dirtyRect {
dispatch_semaphore_wait(inflightSemaphore, DISPATCH_TIME_FOREVER);
[super drawRect:dirtyRect];
…
id<MTLTexture> theTexture = …
[renderedQueue addObject:theTexture];
// populate secondary views
for (BoardMonitorMTKView *v in self.downstreamOutputs)
[v enqueueTexture:(hasValidContent) ? [renderedQueue firstObject] : nil];
__block dispatch_semaphore_t fSemaphore = inflightSemaphore;
__weak __block NSMutableArray *rQueue = renderedQueue;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
if ([rQueue count]==2)
[rQueue removeObjectAtIndex:0];
dispatch_semaphore_signal(fSemaphore);
}];
[commandBuffer presentDrawable:self.currentDrawable];
[commandBuffer commit];
}
在辅助视图中:
-(void)enqueueTexture:(id<MTLTexture>)inputTexture {
dispatch_semaphore_wait(inflightSemaphore2, DISPATCH_TIME_FOREVER);
if (inputTexture) {
[textureQueue addObject:inputTexture];
if ([textureQueue count]>kTextureQueueCapacity)
[textureQueue removeObject:[textureQueue firstObject]];
}
dispatch_semaphore_signal(inflightSemaphore2);
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(aQueue, ^{
[self draw];
});
}
- (void)drawRect:(NSRect)dirtyRect {
dispatch_semaphore_wait(inflightSemaphore2, DISPATCH_TIME_FOREVER);
[super drawRect:dirtyRect];
…
id<MTLTexture>inputTexture = [textureQueue firstObject];
MTLRenderPassDescriptor *renderPassDescriptor = self.currentRenderPassDescriptor;
id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
MTLViewport viewPort = {0.0, 0.0, (double)self.drawableSize.width, (double)self.drawableSize.height, -1.0, 1.0};
[encoder setViewport:viewPort];
[encoder setRenderPipelineState:metalVertexPipelineState];
// draw main content
NSUInteger vSize = _vertexInfo.metalVertexCount*sizeof(AAPLVertex);
id<MTLBuffer> mBuff = [self.device newBufferWithBytes:_vertexInfo.metalVertices
length:vSize
options:MTLResourceStorageModeShared];
[encoder setVertexBuffer:mBuff offset:0 atIndex:0];
[encoder setVertexBytes:&_viewportSize length:sizeof(_viewportSize) atIndex:1];
[encoder setFragmentTexture:inputTexture atIndex:0];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:_vertexInfo.metalVertexCount];
[encoder endEncoding];
__block dispatch_semaphore_t fSemaphore = inflightSemaphore2;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
[textureQueue removeObject:[textureQueue firstObject]];
dispatch_semaphore_signal(fSemaphore);
}];
[commandBuffer presentDrawable:self.currentDrawable];
[commandBuffer commit];
}
我不确定您是否知道创建自己的纹理并对其进行渲染是可以接受的并且很常见。您不必渲染视图或图层的可绘制对象的纹理。
创建您自己的要渲染到的纹理,然后,仅在当前步骤中,从您的纹理渲染到可绘制对象的纹理。
请注意,根据您的具体操作,您可能需要一个由三个左右的纹理组成的池,您可以在其中旋转。您需要关心的问题是 Metal 是否仍在从纹理中读取,因此在读取完成之前不要写入它。
这是我的场景:
在 MTKView
(以及其中的单个命令缓冲区)中,我有一个 MTLTexture
是 MTLComputeEncoder
操作的结果。
我在 MTKView
上的 draw
方法被称为 dispatch_get_global_queue
的高优先级实例。在那个 MTKView
中 drawRect
的尾端,我 "scoop" 那个纹理并将它设置为另一个 MTKView
上的 属性。然后,我分派一个低优先级线程来调用该视图的 draw
方法,将原始纹理的一个区域复制到第二个 MTKView
,以及最终的 presentDrawable
调用——全部在不同的命令缓冲区。同时,原始的 MTKView
结束了 draw
方法,它自己调用了 presentDrawable
.
我应该提到这两个 MTKViews
都配置了 isPaused=YES
和 enableSetNeedsDisplay=NO
。
这一切工作得相当好,但我偶尔会因“[CAMetalLayerDrawable texture] should not be called after presenting the drawable
”警告或某些无法识别的线程中的偶尔崩溃而崩溃:
QuartzCore`-[CAMetalDrawable present]:
0x7fff881cf471 <+0>: pushq %rbp
0x7fff881cf472 <+1>: movq %rsp, %rbp
0x7fff881cf475 <+4>: movq %rdi, %rax
0x7fff881cf478 <+7>: movq 0x15f25ce1(%rip), %rcx ; CAMetalDrawable._priv
0x7fff881cf47f <+14>: movq (%rax,%rcx), %rcx
0x7fff881cf483 <+18>: movq 0x20(%rcx), %rdi <---- crashes here
0x7fff881cf487 <+22>: xorps %xmm0, %xmm0
0x7fff881cf48a <+25>: movq %rax, %rsi
0x7fff881cf48d <+28>: popq %rbp
0x7fff881cf48e <+29>: jmp 0x7fff881cf493 ; layer_private_present(_CAMetalLayerPrivate*, CAMetalDrawable*, double, unsigned int)
如果我remove/comment去掉辅助"scoop"重绘原来的贴图,就不会崩溃了。
有没有更容易接受的方法来完成这个?可能使用 MTLBlitCommandEncoder
显式复制所提供纹理的内容?
更新#1
让代码更清晰一点是一个明智的想法,可能:
这是主要MTKView
的drawRect
方法,由dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
驱动:
- (void)drawRect:(NSRect)dirtyRect {
dispatch_semaphore_wait(inflightSemaphore, DISPATCH_TIME_FOREVER);
[super drawRect:dirtyRect];
…
id<MTLTexture> theTexture = …
[renderedQueue addObject:theTexture];
// populate secondary views
for (BoardMonitorMTKView *v in self.downstreamOutputs)
[v enqueueTexture:(hasValidContent) ? [renderedQueue firstObject] : nil];
__block dispatch_semaphore_t fSemaphore = inflightSemaphore;
__weak __block NSMutableArray *rQueue = renderedQueue;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
if ([rQueue count]==2)
[rQueue removeObjectAtIndex:0];
dispatch_semaphore_signal(fSemaphore);
}];
[commandBuffer presentDrawable:self.currentDrawable];
[commandBuffer commit];
}
在辅助视图中:
-(void)enqueueTexture:(id<MTLTexture>)inputTexture {
dispatch_semaphore_wait(inflightSemaphore2, DISPATCH_TIME_FOREVER);
if (inputTexture) {
[textureQueue addObject:inputTexture];
if ([textureQueue count]>kTextureQueueCapacity)
[textureQueue removeObject:[textureQueue firstObject]];
}
dispatch_semaphore_signal(inflightSemaphore2);
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(aQueue, ^{
[self draw];
});
}
- (void)drawRect:(NSRect)dirtyRect {
dispatch_semaphore_wait(inflightSemaphore2, DISPATCH_TIME_FOREVER);
[super drawRect:dirtyRect];
…
id<MTLTexture>inputTexture = [textureQueue firstObject];
MTLRenderPassDescriptor *renderPassDescriptor = self.currentRenderPassDescriptor;
id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
MTLViewport viewPort = {0.0, 0.0, (double)self.drawableSize.width, (double)self.drawableSize.height, -1.0, 1.0};
[encoder setViewport:viewPort];
[encoder setRenderPipelineState:metalVertexPipelineState];
// draw main content
NSUInteger vSize = _vertexInfo.metalVertexCount*sizeof(AAPLVertex);
id<MTLBuffer> mBuff = [self.device newBufferWithBytes:_vertexInfo.metalVertices
length:vSize
options:MTLResourceStorageModeShared];
[encoder setVertexBuffer:mBuff offset:0 atIndex:0];
[encoder setVertexBytes:&_viewportSize length:sizeof(_viewportSize) atIndex:1];
[encoder setFragmentTexture:inputTexture atIndex:0];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:_vertexInfo.metalVertexCount];
[encoder endEncoding];
__block dispatch_semaphore_t fSemaphore = inflightSemaphore2;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
[textureQueue removeObject:[textureQueue firstObject]];
dispatch_semaphore_signal(fSemaphore);
}];
[commandBuffer presentDrawable:self.currentDrawable];
[commandBuffer commit];
}
我不确定您是否知道创建自己的纹理并对其进行渲染是可以接受的并且很常见。您不必渲染视图或图层的可绘制对象的纹理。
创建您自己的要渲染到的纹理,然后,仅在当前步骤中,从您的纹理渲染到可绘制对象的纹理。
请注意,根据您的具体操作,您可能需要一个由三个左右的纹理组成的池,您可以在其中旋转。您需要关心的问题是 Metal 是否仍在从纹理中读取,因此在读取完成之前不要写入它。