rendermetalfragment-shadermetalkitstencil-buffer

Is it possible to fill in Stencil Buffer programmatically via fragment shader in Metal?


Do stencil buffer can only be filled in by some hard-coded rules during geometry drawing (like depth buffer), or is it possible to fill it in completely programmatically with the help of full-screen fragment shader, which can for example draw black-and-white shape based on some 2D SDF?

Also is it possible to use stencil buffer separate of depth buffer in this case and don't use depth buffer at all?


Solution

  • You can output [[stencil]] decorated value from the fragment function, which is gonna be the stencil reference value according to Metal Shading Language specification table 5.7, and then set up MTLDepthStencilState in a way that stencil always succeeds. And set up the operation to be MTLStencilOperationReplace, which means

    Replace the stencil value with the stencil reference value, [...]

    which should make the stencil being effectively output by fragment function.