【问题标题】:Fragment function seems properly written but Metal complains片段功能似乎正确编写,但金属抱怨
【发布时间】:2017-05-24 08:02:11
【问题描述】:

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)

管道状态创建时出错:

致命错误:“试试!”表达式意外引发错误:错误 Domain=CompilerError Code=1 "链接失败:片段输入 vPos 不是 在顶点着色器输出中找到" [...]

我检查并重新检查了代码,但无法理解问题所在。

有什么想法吗?谢谢!

【问题讨论】:

    标签: metal metalanguage


    【解决方案1】:

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

    【讨论】:

    • 位置标签不是用来获取屏幕上的位置,还是相对于矩形的?
    • 谢谢你的回答,我明天去看看?
    • 它是“窗口相关的”,但这实际上意味着相对于视口,它指定了颜色或深度附件的一部分。对于像你这样的顶点着色器,它只返回一个float4,返回值是position。由于这是您想要作为片段函数的输入,因此您也希望将该输入注释为 position
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2014-05-21
    相关资源
    最近更新 更多