金属着色器混合片段接收到错误的颜色值



我在Metal:中有两次通过

1-节点路径

2-混合通道

使用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是在node_vertex计算的节点中心的数据。为什么并不重要。我有自己的理由。

并且从XCODE调试器,我可以确认"float4颜色">返回[0.416-0.345、0.0,0.0]。这意味着它将正确返回half4颜色,Y([1](值为-0.345。

到目前为止一切都很好!

问题是当它进入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;
…
}

在调试器中(选择节点内的像素(float3center_map为[0.416,0.0,0.0,0.0]。

0.0用于第二个组件,而它应该是-0.345

为什么?这太疯狂了,因为node_fragment的调试器显示Y([1](的值-0.345

理论上,它应该是之前通过调试器确认的确切颜色值[0.416,-0.345,0.0,0.0]。

当我执行center_map.x时,它返回正确的值0.416,这意味着它从node_fragment中获取x颜色值。

但是,当我执行center_map.y时,它是0.0,这不是node_fragment中调试器中显示的值。

有什么线索吗?

第一次使用的像素格式是什么?大多数像素格式都是无符号,包括SceneKit默认为的MTLPixelFormatBGRA8Unorm_sRGB

如果您只使用两种颜色组件,MTLPixelFormatRG32Float是一个很好的候选者。

最新更新