Writing to depth buffer

Hi-

Just curious.. I'm trying to write a custom depth out in the fragment shader and I can't seem to figure out how to do this. I looked into writing to the depth texture, but the access::write is not allowed for a depth2d<> var. Is there any way to do this without doing a pre-pass?


Seems odd that the same hardware that runs OpenGLES3 w/depth buffer write can't do it in the metal side of the house..


Thanks!

Replies

As far as I know, you have to pass in an additional texture2d<> for this, say, for linear world depth. I believe the depth2d<> has specific semantics due to the common case that is done with hardware assist.

  • Depth buffer as written by vertex shader (left) and with fragment shader (right). Note fragment shader changes depth buffer allowing subsequently drawn crosshairs to show a depth test intersection with the brain. If Metal can not write to the depth buffer, I need to restrict myself to OpenGL and WebGL2, both of which support this feature.

    vertexShader_fragrmentShader

Add a Comment

Hello, Is it still impossible for the Metal fragment shader to write to the depth buffer for the current version of Metal on the M1 CPUs? The image below shows the use case for my MRIcroGL software (with these images showing the correct behavior of OpenGL on my M1). MRIcroGL is a volume raycaster. The vertex shader simply determines the location of the front faces of the cube (left panel). The fragment shader subsequently samples each pixel from the front face of the cube to the back face, accumulating color/opacity from all the voxels it traverses. In OpenGL, I can simply write 'gl_FragDepth' for a ray when it first hits a non-transparent voxel. This has two benefits: first the depth test fro the subsequent crosshairs correctly shows the ntersection with the brain, not the cube. Second, we can read the depth buffer allowing depth picking, where clicking on the brain moves the crosshair to that location (allowing the user to work out the location of features seen on the rendering for 2D orthogonal slices).

In every other respect, my software works identically when compiled for OpenGL and Metal. However, the inability to write to a depth buffer seems like a deal breaker for this usage scenario.

At least on my M1, the Metal fragment shader can write to the depth buffer using this method