简体   繁体   中英

Does Metal support anything like glDepthRange()?

I'm writing some metal code that draws a skybox. I'd like for the depth output by the vertex shader to always be 1, but of course, I'd also like the vertices to be drawn in their correct positions.

In OpenGL, you could use glDepthRange(1,1) to have the depth always be written out as 1.0 in this scenario. I don't see anything similar in Metal. Does such a thing exist? If not, is there another way to always output 1.0 as the depth from the vertex shader?

What I'm trying to accomplish is drawing the scenery first and then drawing the skybox to avoid overdraw. If I just set the z component of the outgoing vertex to 1.0, then the geometry doesn't draw correctly, obviously. What are my options here?

Looks like you can specify the fragment shader output (return value) format roughly so:

struct MyFragmentOutput {
    // color attachment 0
    float4 color_att [[color(0)]];
    // depth attachment
    float depth_att [[depth(depth_argument)]]
}

as seen in the section "Fragment Function Output Attributes" on page 88 of the Metal Shading Language Specification ( https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf ). Looks like any is a working value for depth_argument (see here for more: In metal how to clear the depth buffer or the stencil buffer? )

Then you would set you fragment shader to use that format

fragment MyFragmentOutput interestingShaderFragment
// instead of: fragment float4 interestingShaderFragment

and finally just write to the depth buffer in your fragment shader:

MyFragmentOutput out;
out.color_att = float(rgb_color_here, 1.0);
out.depth_att = 1.0;
return out;

Tested and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM