Search code examples
objective-cmetal

Changing the blend mode in Metal


Does changing the blend function in Metal needs setting a whole new MTLRenderPipelineState?

I assume YES, because the MTLRenderPipelineState is immutable, so I cannot change its descriptor and, for example descriptor's sourceRGBBlendFactor property. But I wanted to confirm, as this sounds a little inefficient to generate large objects to change a single parameter.

Edit: I am thinking about a case, where I am drawing one vertex buffer with series of meshes and multiple call to -drawPrimitives:. Each mesh can use a different blend mode but all use the same vertex and fragment shader. In OpenGL I could switch glBlendFunc() between the draw calls. In Metal I need to set a whole separate MTLRenderPipelineState with several state values.


Solution

  • Some objects in Metal are designed to be transient and extremely lightweight, while others are more expensive and can last for a long time, perhaps for the lifetime of the app.

    Command buffer and command encoder objects are transient and designed for a single use. They are very inexpensive to allocate and deallocate, so their creation methods return autoreleased objects.

    MTLRenderPipelineState is not transient.

    Does changing the blend function in Metal needs setting a whole new MTLRenderPipelineState?

    Yes, you must create a whole new MTLRenderPipelineState object for each blending configuration.