我正试图将我的简单应用程序从D3D12移植到Metal,我被困在呈现一个简单的三角形上。
该窗口不显示任何原语;只有透明的颜色是可见的。
在调试我的应用程序时,我注意到所有的原语都是剪裁的(性能>基本元素>基本元素: 100%)。我不明白为什么会这样,因为这是我的坐标:
struct vertex { float x, y, z; };
static const struct vertex vertices[] =
{
{ 0.0f, 0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ -0.5f, -0.5f, 0.0f }
};到目前为止,我已经尝试过:
{0, 0, 800, 600, 0, 1}。{0, 0, 800, 600}。MTLCullModeNone。MTLTriangleFillModeFill。我错过了什么金属陷阱吗?(即设置像D3D12那样的示例掩码?)
我的管道供参考:
MTLRenderPipelineDescriptor* descriptor = [MTLRenderPipelineDescriptor new];
descriptor.vertexFunction = desc->vertex->handle;
descriptor.fragmentFunction = desc->pixel->handle;
descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
vs.attributes[0].format = MTLVertexFormatFloat3;
vs.attributes[0].offset = 0;
vs.attributes[0].bufferIndex = 0;
vs.layouts[0].stride = sizeof(float) * 3;
vs.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vs.layouts[0].stepRate = 1;注意,我没有使用MTKView。
以下是我使用的着色器:
struct Out
{
float4 pos [[position]];
};
vertex Out vertexShader(
const device float3* vertices [[buffer(0)]],
unsigned int idx [[vertex_id]]
) {
Out out;
out.pos = float4(vertices[idx], 1.0);
return out;
}
fragment float4 fragmentShader(Out in [[stage_in]])
{
// TODO: Write fragment shader
return float4(1, 0, 0, 1);
}编辑:我做了以下更改,我的三角形最终被呈现出来:
struct vertex { float x, y, z, w; };
static const struct vertex vertices[] =
{
{ 0.0f, 0.5f, 0.0f, 1.f },
{ 0.5f, -0.5f, 0.0f, 1.f},
{ -0.5f, -0.5f, 0.0f, 1.f }
};
// Vertex Descriptor
MTLVertexDescriptor* vs = descriptor.vertexDescriptor;
vs.attributes[0].format = MTLVertexFormatFloat4;
vs.attributes[0].offset = 0;
vs.attributes[0].bufferIndex = 0;
vs.layouts[0].stride = sizeof(float) * 4;
vs.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vs.layouts[0].stepRate = 1;着色器:
struct Out
{
float4 pos [[position]];
};
vertex Out vertexShader(
const device float4* vertices [[buffer(0)]],
unsigned int idx [[vertex_id]]
) {
Out out;
out.pos = vertices[idx];
return out;
}
fragment float4 fragmentShader(Out in [[stage_in]])
{
// TODO: Write fragment shader
return float4(1, 0, 0, 1);
}我不明白为什么我的应用程序失败,当使用3D向量而不是四维向量。有什么想法吗?
发布于 2021-05-26 17:17:52
金属阴影语言中的float3具有与float4相同的大小/对齐方式,即16个字节。我认为您的原始代码会在顶点着色器中使用packed_float3 (12字节)。
https://computergraphics.stackexchange.com/questions/10913
复制相似问题