Add extra layers to an existing GL_TEXTURE_2D_ARRAY


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


But since you have a known, fixed number of possible different kinds of textures, you should just create an array texture with all of the possible layers. You can fill in the data for the ones you actually use, and leave the rest unused until you need them later.

1 Like

Thank you, would that impact the texture cache or are OpenGL drivers smart enough to ignore layers that aren’t being used?

consider using bindless texture extension

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.