I want to do something like Chapter 19, using a height map to generate maps. However, I have significantly more “terrain types”, so need to pass more then 3 texture types. Is there a way to pass an array of textures or something? Or must I pass each one as a parameters (and limited to the number of buffer indices, 31)) ?
You can use argument buffers with a resource heap, which is described in Chapter 25. This is rather and needs a lot of concentration. I would suggest that as well as following Chapter 25, you watch all the Apple Metal videos on Argument Buffers and also Resource Heaps.
There is a good amount of Apple sample codes too. You could start with Encoding Argument Buffers on the GPU | Apple Developer Documentation where the app loads 66 textures into a heap.
You can also use texture2d_array
instead of argument buffers, which might be easier to start with.
Chapter 31 has a project which renders rocks, with one of three random textures. These textures are held in a texture2d_array
.
In the screenshot, the current rock is being rendered with index 1 in the array.
Take a look at Nature.swift, TextureController.loadTextureArray(textureNames:)
and Nature.metal.