Fragment函数似乎编写正确,但Metal报错

3

TL;DR: Metal似乎无法检测到我的顶点着色器返回的内容

我在MSL中编写了这两个函数:

vertex float4 base_image_rect(constant float4 *pos [[buffer(0)]],
                                           uint vid [[vertex_id]]) {
    return pos[vid];
}

fragment float4 fragment_image_display(float4 vPos [[stage_in]],
                              texture2d<float, access::sample> imageToRender [[texture(0)]],
                              sampler imageSampler [[sampler(0)]]) {
    return imageToRender.sample(imageSampler, float2(vPos.x, vPos.y));
}

当我尝试使用这些代码创建我的渲染管道状态时:
// Make image display render pipeline state
let imageDisplayStateDescriptor = MTLRenderPipelineDescriptor()
imageDisplayStateDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
imageDisplayStateDescriptor.vertexFunction = library.makeFunction(name: "base_image_rect")
imageDisplayStateDescriptor.fragmentFunction = library.makeFunction(name: "fragment_image_display")

displayImagePipelineState = try! device.makeRenderPipelineState(descriptor: imageDisplayStateDescriptor)

在创建管线状态时出现错误:

致命错误:'try!'表达式意外地引发了一个错误:错误领域=编译器错误代码=1“链接失败:片段输入vPos在顶点着色器输出中未被找到”[...]

我已经检查了代码,但仍然不知道哪里出错了。

有什么想法吗?谢谢!

1个回答

6
尝试将stage_in替换为position。我认为stage_in主要用于带有特定属性限定符注释或按名称匹配的每个字段都是struct。显然,当它与非结构类型一起使用时,它尝试通过名称匹配。例如,如果您的顶点函数输出一个结构体,其中一个字段是vPos,那么就会找到它。

位置标签不是用来获取相对于矩形的位置,而是用来获取在屏幕上的位置吗? - Pop Flamingo
谢谢您的回答,我明天会检查一下。 - Pop Flamingo
它是“窗口相对的”,但实际上是相对于视口,该视口指定了颜色或深度附件的一部分。对于像您这样只返回float4的顶点着色器,返回值就是position。由于这是您希望作为片段函数输入的内容,因此您也希望将该输入注释为position - Ken Thomases

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接