MSL-如何在Metal shader中指定统一的数组参数?

问题描述 投票:1回答:1

我正在尝试将统一的数组传递到Metal着色器中,例如:

fragment vec4 fragment_func(constant float4& colors[3] [[buffer(0)]], ...) {...}

我遇到错误:

"NSLocalizedDescription" : "Compilation failed: \n\nprogram_source:2:1917: error: 'colors' declared as array of references of type 'const constant float4 &'\nprogram_source:2:1923: 
error: 'buffer' attribute cannot be applied to types\nprogram_source:2:1961:

我知道'buffer'属性只能应用于指针和引用。在这种情况下,在MSL中传递统一数组的正确方法是什么?

编辑:MSL specs声明缓冲区属性支持“缓冲区类型数组”。我在语法上一定做错了吗?

ios graphics metal metalkit
1个回答
0
投票

C ++中不允许使用引用数组,MSL也不将其作为扩展支持。

float4是规范中使用的“缓冲区类型”,但float4&不是。而是使用指向数组中包含的类型的指针:

fragment vec4 fragment_func(constant float4 *colors [[buffer(0)]], ...) {...}

[如果需要,可以将数组的大小作为另一个缓冲区参数传递,或者可以仅确保着色器函数读取的元素不超过缓冲区中的元素。

然后访问元素就像普通的取消引用一样简单:

float4 color0 = *colors;   // or, more likely:
float4 color2 = colors[2];
© www.soinside.com 2019 - 2024. All rights reserved.