I want to pass the texture array to the fragment shader. But there is a problem

var textures: [MTLTexture?]
Sent it to the fragment shader with renderEncoder.setFragmentTextures(textures, range: 0…<textures.count).

Inside the fragment shader, I want to select as many textures as textures.count.
How do I do it? (However, textures.count is dynamic. [It may be 1 or 5]).


Texture creation options are:
let textureDesciptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: format, width: width, height: height, mipmapped: false)
let texture = Renderer.device.makeTexture(descriptor: textureDesciptor)

texture?.replace(region: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0, withBytes: data, bytesPerRow: width)

Thank you.

You can’t use dynamic arrays in shaders.

If you need a dynamic number, you can send the actual number in something like Params.

The dynamic number of lights in an array is an example.

However, you can use a texture array. This stackoverflow answer may help you: ios - Texture array in Metal - Stack Overflow

2 Likes

Thank you
I found another way to solve it. :smiley:

1 Like