Swift 金属着色器未按预期工作
如果我在Metal/Swift中运行以下顶点着色器,我会在屏幕上看到一个漂亮的矩形:Swift 金属着色器未按预期工作,swift,graphics,metal,vertex-shader,Swift,Graphics,Metal,Vertex Shader,如果我在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);
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
没有打包。其布局与3float
s不同。有填充物。它的大小实际上与float4
或4float
s相同
可能,最简单的修复方法是将
位置
声明为指向packed\u float3
的指针,但在着色器方面经验不足,请理解!区别在于float2
与float3
,以及如何声明float4
。我想到了几件事。(1) 为什么资产是@1、@2、@3?我想是因为像素。你是在处理像素密度吗?(2)使用coreImage,第四维通常是“alpha”。您的float4(位置1)
是否默认为alpha或0?只是想帮忙。祝你好运