iosshadermetalfragment-shadermetalkit

Render into 2 offscreen textures at once in Metal fragment shader


Metal fragment shader render into its render target by explicitly returning float4 value. Does it mean that fragment shader can only render in one texture at once? I'd like to render into 2 textures at once, both at the same coordinates, but for now I have to resort to texture2d.write function for one of it, which doesn't seem too natural for me. Is there a way to make both textures to be render targets at once?


Solution

  • You can return a struct from the fragment shader instead, where each variable has the type you want to output (like float4) and an attribute like [[color(i)]] where i is the index of the color attachment from MTLRenderPassDescriptpr