View Full Version : GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT + glBindBufferBase ?

09-21-2016, 08:13 AM
hi everyone,

i have read this tutorial: http://alfonse.bitbucket.org/oldtut/Illumination/Tutorial%2012.html
plus the wiki page for the interface blocks: https://www.opengl.org/wiki/Interface_Block_(GLSL)#Memory_layout

and still i'm asking myself if i have to use GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT anyhow to make my code working on other platforms
(it works on mine without bothering about that)

the fragment shader looks like that:

struct Material
vec3 Kd;
vec3 Ks;

#define MAX_MATERIALS 100

layout (std140, binding = 1) uniform Materials
Material materials[MAX_MATERIALS];

the c++ code looks like that:

struct Material
vec3 Kd;
float PADDING1;
vec3 Ks;
float PADDING2;

std::vector<Material> materials;
// fill that array with some materials ...

// allocate buffer memory
glGenBuffers(1, &m_materialbuffer);
glBindBuffer(GL_UNIFORM_BUFFER, m_materialbuffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(Material) * materials.size(), materials.data(), GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

// bind materials to location 1:
glBindBufferBase(GL_UNIFORM_BUFFER, 1, m_materialbuffer);

so i'm allocating just enough space so put the vector<Material> into the buffer, that works fine

do i only have to bother about GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT when using glbindBufferRange(...) ?
(which by-the-way is 256 on my system)

Alfonse Reinheart
09-21-2016, 08:30 AM
You are creating an array of structs in GLSL. The tutorial in question is not. The tutorial's shader doesn't take an array of structs; it takes a single struct. The tutorial stores multiple copies of those structs in the same buffer. So for each object, it has to adjust the buffer binding for that UBO.

The offset alignment matters when you want a UBO to start at any place other than the start of the buffer. OpenGL guarantees that the GPU memory alignment of a buffer object will be appropriate for whatever things you want to use it for. But alignment within a buffer is up to you, so you have to respect what the hardware requires.

09-21-2016, 08:45 AM
OK thanx for clarifying.

i didnt read your tutorial completely from the beginning, so i've mixed up something :D