金属着色器未按预期工作
Metal shader not working as expected
如果我 运行 Metal/Swift 中的以下顶点着色器,我会在屏幕上得到一个漂亮的矩形:
vertex Vertex vertexShader(uint k [[ vertex_id ]],
device float2* position [[buffer(1)]]){
Vertex output;
float2 pos = position[k];
output.position = float4(pos,0,1);
return output;
};
//position [0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.5]
//indexList [0, 1, 2, 2, 1, 3]
现在,如果我 运行 以下内容,我会得到一个空白屏幕:
vertex Vertex vertexShader(uint k [[ vertex_id ]],
device float3* position [[buffer(1)]]){
Vertex output;
float3 pos = position[k];
output.position = float4(pos,1);
return output;
};
//position [0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.0]
//indexList [0, 1, 2, 2, 1, 3]
在我看来,这些应该会产生相同的结果。我错过了什么?
您在应用程序代码中究竟如何填充与索引 1 关联的缓冲区?
我怀疑您只是提供了一组浮点数。好吧,float3
没有打包。它的布局与 3 float
s 不同。有衬垫。它的大小其实和float4
或者4float
s一样。
可能,最简单的解决方法是将 position
声明为指向 packed_float3
的指针。
如果我 运行 Metal/Swift 中的以下顶点着色器,我会在屏幕上得到一个漂亮的矩形:
vertex Vertex vertexShader(uint k [[ vertex_id ]],
device float2* position [[buffer(1)]]){
Vertex output;
float2 pos = position[k];
output.position = float4(pos,0,1);
return output;
};
//position [0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.5]
//indexList [0, 1, 2, 2, 1, 3]
现在,如果我 运行 以下内容,我会得到一个空白屏幕:
vertex Vertex vertexShader(uint k [[ vertex_id ]],
device float3* position [[buffer(1)]]){
Vertex output;
float3 pos = position[k];
output.position = float4(pos,1);
return output;
};
//position [0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.0]
//indexList [0, 1, 2, 2, 1, 3]
在我看来,这些应该会产生相同的结果。我错过了什么?
您在应用程序代码中究竟如何填充与索引 1 关联的缓冲区?
我怀疑您只是提供了一组浮点数。好吧,float3
没有打包。它的布局与 3 float
s 不同。有衬垫。它的大小其实和float4
或者4float
s一样。
可能,最简单的解决方法是将 position
声明为指向 packed_float3
的指针。