Search code examples
iosarraysswiftmacosmetal

How do I properly pass a texture2d_array<float> to the fragment shader?


Here's my fragment shader.

constexpr sampler ColorSampler;
fragment half4 T(VertexOut Vertex [[stage_in]], texture2d_array<float> Texture [[texture(0)]]) {
    return half4(Vertex.Color, 1)*half4(Texture.sample(ColorSampler, Vertex.TexCoord.xy/Vertex.TexCoord.z, 0));
}

This compiles. However, how do I pass a texture2d_array<float> from the Swift side that this shader will accept? I have a variable called Textures of type [MTLTexture?], but how do I pass that as an array to texture buffer 0? This doesn't work.

CommandEncoder.setFragmentTextures(Textures, range: 0..<Textures.count)

It doesn't work because it's putting each texture in the texture buffer of its index. How do I properly pass a texture array to the fragment function?

How do I do it?

And is what I'm trying a good idea even?

And how am I expected to research if almost no in-depth content about this exists?

enter image description here


Solution

  • setFragmentTextures does indeed bind each texture individually. If you want to use texture2d_array in your shader you'd have to create a MTLTexture with MTLTextureType type2DArray.