1
votes

Let's say I have a depth/stencil texture that has format MTLPixelFormatDepth32Float_Stencil8 that I use as an attachment in a render pass and then I want to sample stencil values from that texture in a latter render pass.

Metal Shading Language Specification doesn't mention anything at all about this, only that depth textures need to be declared as depth2d<T, access a = access::sample> and for depth2d both T sample(sampler s, float2 coord, int2 offset = int2(0)) const and T read(uint2 coord, uint lod = 0) const return T where T will most probably be float.

So, my question is, how do I read or sample stencil values from this texture?

1

1 Answers

2
votes

Create a stencil texture view of the depth/stencil texture with format MTLPixelFormatX32_Stencil8. Then, pass that texture view in to the shader where you want to read it. It operates as a R8Uint texture.

Note that not all devices support stencil texture views. They're supported for feature sets equal to or better than iOS_GPUFamily1_v3, iOS_GPUFamily2_v3, iOS_GPUFamily3_v2, tvOS_GPUFamily1_v2, or OSX_GPUFamily1_v2.

If it's not supported, you need to use a blit encoder to copy the depth/stencil texture to a buffer and then either use that buffer directly or copy it to a texture of a suitable format.