How to use fence for indirect draws?

Hi, I met a problem about fences. My render steps are like the following:

[ComputeEncoder]

  1. Generate indirect buffer B.
  2. updateFence F.

[RenderEncoder]

  1. waitForFence F. // which stage?
  2. drawPrimitives with indirect buffer B.

But to call waitForFence I need to pass beforeStages, the only choices for this param are MTLRenderStageVertex orMTLRenderStageFragment. There is no stage for indirect draw like vulkan's VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT.

I tried to use MTLRenderStageVertex but it didn't work. What is the correct way to wait for a fence in this scenario?

Thanks.

  • Also notice that the indirect buffer B is created from a MTLHeap, so the automatic resource tracking is disabled, I have to sync manually using MTLFences.

Add a Comment

Replies

Hi atyuwen, you seem to be approaching this in the correct way. Metal doesn't have an explicit stage for indirect-command-buffer consumption like Vulkan does. That said, waiting before MTLRenderStageVertex will indeed wait before ICB consumption.

As long as your encoders are committed to the same MTLCommandQueue, and they are in the correct, sequential order, then what you are doing should work. I would double-check if your usage of the fence F is correct.

  • Oh, I forgot the vertex buffer. The actual render commands were:

    [ComputeEncoder]

    Generate a vertex buffer VB and an indirect buffer IB.updateFence F.

    [RenderEncoder]

    waitForFence F. // which stage?drawPrimitives with VB and IB.

    Does waiting before MTLRenderStageVertex also wait before vertex buffer consumption? 

Add a Comment