我已经尝试设置addPresentedHandler
一段时间了,但是没有运气。
我有一个addCompletedHandler
在命令缓冲区中工作,该命令缓冲区包含每帧要执行的渲染工作。通过查看代码块中的设置断点,我已经验证了addCompletedHandler
是否正常工作。这些在运行应用程序时触发。但是,对于addPresentedHandler
,不会触发代码块内的断点(我还通过修改代码块中的变量并将printf
s插入到该块中来进行验证。)>
我的渲染循环目前非常简单,看起来像这样(希望我已经提供了足够的内容:]]
注意:我正在使用C ++和Objective-C ++的强大组合。因此,下面显示的代码不是实际代码的直接表示。
dispatch_semaphore_wait(m_inFlightFramesSemaphore, DISPATCH_TIME_FOREVER); m_drawables[m_currentFrameIndex] = [m_swapChain nextDrawable]; MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new]; renderPassDescriptor.colorAttachments[0].texture = m_drawables[m_currentFrameIndex].texture; renderPassDescriptor.colorAttachments[0].level = 0; renderPassDescriptor.colorAttachments[0].slice = 0; renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 1.0); id<MTLCommandBuffer> commandBuffer = [m_commandQueue commandBuffer]; id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor: renderPassDescriptor]; [renderEncoder setRenderPipelineState: m_pipelineState]; [renderEncoder setTriangleFillMode: MTLTriangleFillModeFill]; [renderEncoder setFrontFacingWinding: MTLWindingCounterClockwise]; [renderEncoder setCullMode: MTLCullModeBack]; /* set viewport and scissor (omitted here) */ [renderEncoder useResource: m_vertexBuffer usage: MTLResourceUsageRead stages: MTLRenderStageVertex]; [renderEncoder setVertexBuffer: m_vertexBuffer offset: 0 atIndex: 0]; [renderEncoder useResource: m_indexBuffer usage: MTLResourceUsageRead stages: MTLRenderStageVertex]; [renderEncoder drawIndexedPrimitives: MTLPrimitiveTypeTriangle indexCount: 6 indexType: MTLIndexTypeUInt32 indexBuffer: m_indexBuffer indexBufferOffset: 0 instanceCount: 1]; [renderEncoder endEncoding]; PresentDrawable(commandBuffer, m_drawables[m_currentFrameIndex]); CommitFinalCommandBufferInFrame(commandBuffer);
两个最终函数调用的实现如下:
void PresentDrawable(id<MTLCommandBuffer> commandBuffer, id<CAMetalDrawable> drawable) { [drawable addPresentedHandler:^(id<MTLDrawable> dr) { m_framesPresented++; if (m_framesPresented == m_maxFrames) { dispatch_semaphore_signal(m_lastFramePresentedSemaphore); } }]; [commandBuffer presentDrawable:drawable]; } void CommitFinalCommandBufferInFrame(id<MTLCommandBuffer> commandBuffer) { [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> cb) { dispatch_semaphore_signal(m_inFlightFramesSemaphore); }]; [commandBuffer commit]; m_framesSubmitted++; m_currentFrameIndex = (m_currentFrameIndex + 1) % m_maximumDrawableCount; }
我不明白为什么
addPresentedHandler
代码块没有被触发,而addCompletedHandler
代码块却...我缺少明显的东西吗?谢谢!
我已经尝试设置addPresentedHandler一段时间了,但是没有运气。我有一个addCompletedHandler在包含要执行的渲染工作的命令缓冲区上工作...
我看不到您的代码有任何问题,我已经检查了它是否起作用。
void PresentDrawable(id<MTLCommandBuffer> commandBuffer, id<CAMetalDrawable> drawable)
{
[drawable addPresentedHandler:^(id<MTLDrawable> dr) {
printf("Handler \n");
m_framesPresented++;
if (m_framesPresented == m_maxFrames)
{
dispatch_semaphore_signal(m_lastFramePresentedSemaphore);
}
}];
[commandBuffer presentDrawable:drawable];
}
在我的问题中,我没有提到我在iPhone 11 Pro Max(v13.3)模拟器上运行。在尝试使用实际的iPhone 11(v13.3.1)时,我的问题中实现的addPresentHandler
成功触发。我试图确定模拟器的功能限制是否在任何地方都有记载,但已空了。