2017-01-09 53 views
2

TL; DR:金属似乎并没有检测一下我的顶点着色器返回片段功能似乎正确地写入,但金属抱怨

我已经写在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) 

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

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=CompilerError Code=1 "Link failed: fragment input vPos was not found in vertex shader outputs" [...]

我检查并重新检查了代码,并且无法理解错误。

任何想法?谢谢!

回答

3

尝试用position代替stage_in。我认为stage_in主要与struct s一起使用,其中每个字段或者使用特定的属性限定符进行注释,或者使用名称进行匹配。显然,当它与非结构类型一起使用时,它试图按名称匹配。例如,如果你的顶点函数要输出一个字段为vPos的结构体,那就可以找到它。

+0

不是位置标签用于获取屏幕上的位置而不是相对于矩形的位置? –

+0

感谢您的回答,我会在明天检查它 –

+0

它与“窗口相对”相关,但这确实意味着与视口相关,该视口指定颜色或深度附件的一部分。对于像你的顶点着色器,它只返回一个'float4',返回值就是'position'。因为这就是你想要作为你的片段函数的输入,所以你也想把这个输入注释为'position'。 –