Summary
I’ve been searching around for a while and haven’t seen this question asked anywhere: is it possible to add an extra layer to an existing GL_TEXTURE_2D_ARRAY
?
My game has multiple maps which consist of different kinds of terrain (brick, sand, rock, concrete, metal, etc), each of which have their own texture. These textures are stored in a texture array and are updated/reused when a new map loads.
The good
When loading a new map that has the same kind of terrain, I can re-use the texture array I’ve already created, rather than re-upload all the textures again. If the new map has a terrain type that the old map didn’t have, I overwrite that ‘stale’ layer with the new texture.
The bad
When loading a new map that has more terrain types, I recreate the texture array with the correct depth
value and re-upload each texture in the array.
I’d like to be able to add N extra layers, upload the new textures to these new layers and then generate mipmaps again
The code
var ID = Gl.GenTexture();
var depth = 20;
Gl.BindTexture(TextureTarget.Texture2dArray, ID);
Gl.TexImage3D(TextureTarget.Texture2dArray, 0, InternalFormat.Rgba8, TEXTURE_SIZE, TEXTURE_SIZE, depth, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
// Allocate memory ahead of time so textures can be sent to the GPU seamlessly
if (GLFeatures.TexStorage2DAvailable)
{
var levels = (int)Math.Floor(Math.Log(TEXTURE_SIZE, 2)) + 1;
Gl.TexStorage3D(TextureTarget.Texture2dArray, levels, InternalFormat.Rgba8, TEXTURE_SIZE, TEXTURE_SIZE, depth);
}
// Load textures from disk on another thread
// When a texture is ready, send it to the GPU via Gl.TexSubImage3D
...
// When all textures have been uploaded, generate mipmaps
Gl.GenerateMipmap(TextureTarget.Texture2dArray);