将可变数量的MTLTexture作为数组传递给片段着色器的正确语法是什么?
"How to use texture2d_array array in metal?" 提到使用:
array<texture2d<half>, 5>
但是,这需要指定数组的大小。在里面 Metal Shading Language Specification.pdf array_ref 但我不清楚如何使用它,或者它是否被允许作为共享片段的参数类型,给定以下语句:
array_ref
“数组\u ref类型不能作为参数传递给graphics 和内核函数。”
我目前正在做的只是将参数声明为:
fragment float4 fragmentShader(RasterizerData in [[ stage_in ]], sampler s [[ sampler(0) ]], const array<texture2d<half>, 128> textures [[ texture(0) ]]) { const half4 c = textures[in.textureIndex].sample(s, in.coords); }
做错了吗?
我的用例是绘制一个二维平面,该平面被细分为一堆瓷砖。根据预先计算的纹理索引,从阵列中的指定纹理中对每个平铺的内容进行采样。纹理是使用 setFragmentTexture:atIndex
setFragmentTexture:atIndex
应该考虑数组纹理而不是纹理数组。即,类型为的纹理 MTLTextureType2DArray arrayLength 属性指定阵列纹理包含的二维切片数。
MTLTextureType2DArray
arrayLength
要填充纹理,可以使用以下方法指定要写入的切片: -replaceRegion:... 或 -copyFrom{Buffer,Texture}:...toTexture:...
-replaceRegion:...
-copyFrom{Buffer,Texture}:...toTexture:...
在着色器中,可以使用 array 参数
array