6

I'm learning Metal, and there's a conceptual question that I'm trying to wrap my head around: at what level, exactly, should my code handle successive drawing operations that require different pipeline states? As I understand it (from answers like this: https://stackoverflow.com/a/43827775/2752221), I can use a single MTLRenderCommandEncoder and change its pipeline state, the vertex buffer it's using, etc., between calls to drawPrimitives:, and the encoder state that was current at the time of each call to drawPrimitives: will be preserved. So that's great. But it also seems like the design of Metal is such that one can make multiple MTLRenderCommandEncoder instances, and use them to sequentially throw batches of commands into a MTLCommandBuffer. Given that the former works – using one MTLRenderCommandEncoder and changing its state – why would one do the latter? Under what circumstances is it correct to do the former, and under what circumstances is it necessary to do the latter? What is an example of a situation where the latter would be necessary/appropriate?

If it matters, I'm working on a macOS app, using Objective-C. Thanks.

2 Answers 2

10

Ignoring multithreaded encoding cases, which are somewhat advanced, the main reason you'd want to create multiple render command encoders during a frame is because you need to change which textures you're rendering to.

You'll notice that you need to provide a render pass descriptor when creating a render command encoder. For this reason, we often say that the sequence of commands belonging to a particular encoder constitute a render pass. The attachments of that descriptor refer to the textures that will be written to by the commands encoded by the encoder.

Many different techniques, including shadow mapping and postprocessing effects like bloom require multiple passes to produce. Since you can't change attachments in the midst of a pass, creating a new encoder is the only way to encode multiple passes in a frame.

Relatedly, you should ordinarily use one command buffer per frame. You can, however, sometimes reduce frame time by splitting your passes across multiple command buffers, but this is highly dependent on the shape of your workload and should only be done in tandem with profiling, as it's not always an optimization.

4
  • Very interesting. It is becoming clear to my how much I have yet to learn. Is there a book you would particularly recommend to me? Although I don't need it for my current work, I'd like to understand what things like "shadow mapping" and "bloom" are. I want a broader picture of how GPU rendering works and what can be done with it. Ideally, with specific reference to Metal, but perhaps that book does not yet exist...?
    – bhaller
    Commented Feb 2, 2020 at 23:15
  • 1
    There are a few Metal books on the market, but the one that's most up-to-date (and happens to include a discussion of shadow mapping) is this one
    – warrenm
    Commented Feb 2, 2020 at 23:19
  • 1
    And don't forget this one. ;) Commented Feb 3, 2020 at 1:10
  • @warrenm: Could it be that separate render command encoders might be needed to synchronize buffer writes / reads? I am having an issue with this and posted a question here: Using multiple render pipelines in a single MTLRenderCommandEncoder: How to Synchronize MTLBuffer?
    – Wizard
    Commented Feb 5, 2020 at 0:08
7

In addition to Warren's answer, another way to look at the question is by examining the API. A number of Metal objects are created from descriptors. The properties of the descriptor at the time an object is created from it govern that object for its lifetime. Those are aspects of the object that can't be changed after creation.

By contrast, the object will have various setter methods to modify other properties over its lifetime.

For a render command encoder, the properties that are fixed for its lifetime are those specified by the MTLRenderPassDescriptor used to create it. If you want to render with different values for any of those properties, the only way to do so is to create a new encoder from a different descriptor. On the other hand, if you can do everything you need/want to do by using the encoder's setter methods, then you don't need a new encoder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.