Metal Shader mix fragment receiving wrong color value

Viewed 270

I have 2 passes in Metal:

1 - node_pass

2 - mix_pass

debugged it with xCode:

fragment half4 node_fragment (NodeColorInOut vert [[stage_in]])
{
    float4 color =  float4((vert.center.x + 1.0) * 0.5 , (vert.center.y + 1.0) * -0.5, 0.0, 0.0);
    return half4(color);
}

vert.center is a data from node's center calculated at node_vertex. It doesn't matter why. I have my reasons.

And from the XCODE debugger i can confirm "float4 color" returns [0.416,-0.345,0.0,0.0]. Which means it is returning the half4 color correctly with the Y ([1]) value -0.345.

So far so good!

The problem is when it goes to the mix_pass.

- mix_vertex:

vertex MixColorInOut mix_vertex (VertexInput in [[stage_in]],
                                        constant SCNSceneBuffer & scn_frame [[buffer (0)]])
{
    MixColorInOut out;
    out.position = in.position;
    out.uv = float2 ((in.position.x + 1.0) * 0.5, (in.position.y + 1.0) * -0.5);
    return out;
}

- mix_fragment:

fragment half4 mix_fragment (MixColorInOut vert [[stage_in]],
                            texture2d <float, access :: sample> colorScene [[texture (0)]],
                            depth2d <float, access :: sample> depthScene [[texture (1)]],
                            texture2d <float, access :: sample> colorNode [[texture (2)]],
                            depth2d <float, access :: sample> depthNode [[texture (3)]])
{
…
// this part runs only when it's a pixel inside the node
float3 center_map = colorNode.sample(s, vert.uv).rgb;
…
}

In the debugger (selecting a pixels inside the node) float3 center_map is [0.416,0.0,0.0,0.0].

0.0 for the second component when it should be -0.345.

Why is that? It's crazy because the debugger at node_fragment shows the value -0.345 for Y ([1]).

In theory, it should be the exact color value [0.416,-0.345,0.0,0.0] confirmed with debugger previously.

When i do center_map.x it returns the correct value 0.416, which means it is getting the x color value from node_fragment.

But when i do center_map.y it is 0.0, which is NOT the value shown in the debugger from node_fragment.

Any clues what's wrong here?

1 Answers

What pixel format is being used for the first pass? Most pixel formats are unsigned, including MTLPixelFormatBGRA8Unorm_sRGB which SceneKit defaults to.

If you're only working with two color components MTLPixelFormatRG32Float is a good candidate.

Related