UniformBuffer addUniform

when i use UniformBuffer .addUniform,i find when i addUniform(‘a1’,1),addUniform(‘a2’,2),addUniform(‘a3’,1) ,and i console the uniform buffer the data will get five data , you can see this Babylon.js Playground
i think the four data are just aligned


LearnOpenGL - Advanced GLSL

Yeah, it’s just for alignment purposes, you can check how setUniform is implemented here :slight_smile: Babylon.js/uniformBuffer.ts at master · BabylonJS/Babylon.js (github.com)

where can i find rules about it,it’s puzzling to me https://www.babylonjs-playground.com/#MFGG10#2

We follow the std140 rules. What exactly you don’t understand? :slight_smile:

pg2
Can you help me explain why they are different

The sizes of the uniforms? This depends on the type of data you’re going to pass to the buffer.


Oh sorry, I hadn’t noticed the different orderings!

Basically, when filling out the uniform buffer, the addUniform function I linked earlier is going to make each member follow the std140 alignment rules ( OpenGL 4.5 (Core Profile) - June 29, 2017 (khronos.org))

In the 1, 2, 1, case, what happens is:

  1. Member with size 1 is added. The uniform buffer pointer is now at offset 1, which is a multiple of 1, so no padding is added.
  2. Member with size 2 is added. The uniform buffer pointer is now at 3, which is not a multiple of 2, so we pad to the nearest multiple, which is 4. Resulting in one 0 being added.
  3. Member with size 1 is added. The uniform buffer pointer is now at 5, which is a multiple of 1, so again, no need of padding.

In the 1, 1, 2 case, we have:

  1. Member with size 1 is added. The uniform buffer pointer is now at offset 1, which is a multiple of 1, so no padding is added.
  2. Member with size 1 is added. The uniform buffer pointer is now at offset 2, which is a multiple of 1, so no padding is added.
  3. Member with size 2 is added. The uniform buffer pointer is now at offset 4, which is a multiple of 2, so no padding is added.

That’s why one case has extra padding while the other doesn’t :slight_smile:

2 Likes

thank you very much,i understand

2 Likes