gpt4 book ai didi

ios - 在 iOS 上的 Metal 中未触发 addPresentedHandler

转载 作者:行者123 更新时间:2023-12-01 19:34:05 24 4
gpt4 key购买 nike

我一直在尝试设置 addPresentedHandler有一段时间了,但没有运气。

我有一个 addCompletedHandler处理包含每帧要执行的渲染工作的命令缓冲区。我已验证 addCompletedHandler通过查看在代码块中设置断点来工作。这些是在运行应用程序时触发的。对于addPresentedHandler但是,不会触发代码块中的断点(我还通过修改代码块中的变量并将 printf 插入 block 中进行了验证)。

我的渲染循环目前非常简单,看起来像这样(希望我已经提供了足够的):

注意:我使用的是 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 block 是......我错过了一些明显的东西吗?

谢谢!

最佳答案

我没有看到您的代码有任何问题,我已经检查过它是否有效。

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];
}

关于ios - 在 iOS 上的 Metal 中未触发 addPresentedHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60774782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com