[Solved] Wasted space a big deal in GLSL Shaders?

I’m writing a 3D object batcher, 256 objects per batch, so I’ll have 256 modelview, projection, normal matrices, etc. The arrays won’t always be full, should I be concerned about the wasted space?

Edit: not really solved, just can’t pass enough uniforms for it to matter.

Texture Buffer Objects to the rescue :point:

Texture buffer objects work really well for this.

Yeah after Riven posted I looked into it and it’s exactly what I want. Thanks guys!

Edit: I just realized I posted this on the wrong board, sorry about that

Just one note:
You might want to also have a look at Uniform Buffer Object.
Those could be more up to the task when it comes to feeding structured data to your shader, since they allow for aggregate data types, such as vec4 and mat4 directly in them and you therefore do not need a custom texture lookup function to build matrices from texel samples.
You also do not need a backing texture and sampler, just a Buffer Object.

UBOs however need a defined static (max.) size in the shader.
So if you know you are going to have at max 256 objects, you can use the following:


#define MAX_OBJECTS 256
struct Object {
  mat4 modelviewMatrix;
  mat4 projectionMatrix;
  mat4 normalMatrix;
}
layout (std140) uniform Objects {
  Object objects[MAX_OBJECTS];
}

The layout with std140 in memory (in the Buffer Object) on your Java side in this particular case is column-major, tight packing without any padding.
And to retrieve the modelview matrix (for example), you can just use:

objects[theIndex].modelviewMatrix

MAX_OBJECTS is also just an upper bound on the maximum number of objects that can be contained in the UBO. You can always glBufferData just a subset of the data to the Buffer Object and access that subset in the shader.