swiftshaderscenekitmetaldepth-buffer

Maximum value of metal depth2d<float>/Depth32Float when using SceneKit


I'm using an SCNTechnique with SceneKit to implement my own multipass rendering. I have 1 DRAW_SCENE pass that outputs to separate colour and depth targets, and then a second DRAW_QUAD pass that uses those targets and outputs to COLOR.

It's all working great, my only question is what the range of values for the depth target are? I'd like to normalise them between 0 and 1, but unsure what the maximum SceneKit produces is when it arrives in my fragment shader as a depth2d sampler. The largest value I've seen in the debugger when inspecting frames is 0.129, I'm sure that's not the max, but no idea what is.

fragment half4 depth_of_field_fragment(out_vertex_t vert [[stage_in]],
                                       texture2d<float, access::sample> colorSampler [[texture(0)]],
                                       depth2d<float, access::sample> depthSampler [[texture(1)]],
                                       texture2d<float, access::sample> pixelatedSampler [[texture(2)]])
{
    float depth = depthSampler.sample(nearestSampler, vert.uv); //range 0 to ??? max I've seen is 0.129
    float normalisedDepth = depth * ???; // range 0 to 1.0
    if (normalisedDepth > 0.8) {
        return half4(colorSampler.sample(linearSampler, vert.uv));
    } else {
        return half4(pixelatedSampler.sample(nearestSampler, vert.uv));
    }
};

The individual passes, 1 DRAW_SCENE pass that outposts to colour and depth targets, and then a DRAW_QUAD pass that uses those targets and outputs to COLOR

Sequence, consisting of a colour and depth pass, and then a pass that uses both of those


Solution

  • SceneKit depth values are in the [0,1] range.

    Depending on the usesReverseZ property the near plane has a depth value of 1 (resp. 0) and the far plane a depth value of 0 (resp. 1). You can have a look at Depth Precision Visualized for more information on that.

    The related SCNCamera APIs are zNear, zFar and automaticallyAdjustsZRange.