PDA

View Full Version : GLSL initialized uniform block members



saucyio
09-18-2013, 04:08 PM
Prerequisite - My terminology may be slightly off or bad, please excuse and correct me where ever necessary. Please also excuse that this is my first post. I have often lurked these forums and haven't made an account until recently.

Proposal
I propose that uniforms inside uniform blocks may be initialized by other block members. These initialized uniform block members don't take up user accesses memory (you can't issue a command to change them directly.)

Example


uniform MatricesUniformBlock {
mat4 ModelMatrix;
mat4 ViewMatrix;
mat4 ProjectionMatrix;

mat4 ModelViewMatrix = ViewMatrix * ModelMatrix;
};


In the above code ModelViewMatrix's value changes every time ModelMatrix or ViewMatrix is updated. Certain vendor implementations may already kind of do this implicitly when just initializing in the main function[1], but being explicit about the initialization would be nice.

What do you think? I am aware that you usually just initialize something like ModelViewMatrix on the cpu and pass it to opengl, but this is a different approach which I believe is much more shader flexible.

[1] - I actually have no idea if they do or if it's a trivial optimization.

kRogue
09-18-2013, 04:20 PM
Prerequisite - My terminology may be slightly off or bad, please excuse and correct me where ever necessary. Please also excuse that this is my first post. I have often lurked these forums and haven't made an account until recently.

Proposal
I propose that uniforms inside uniform blocks may be initialized by other block members. These initialized uniform block members don't take up user accesses memory (you can't issue a command to change them directly.)

Example


uniform MatricesUniformBlock {
mat4 ModelMatrix;
mat4 ViewMatrix;
mat4 ProjectionMatrix;

mat4 ModelViewMatrix = ViewMatrix * ModelMatrix;
};


In the above code ModelViewMatrix's value changes every time ModelMatrix or ViewMatrix is updated. Certain vendor implementations may already kind of do this implicitly when just initializing in the main function[1], but being explicit about the initialization would be nice.

What do you think?

[1] - I actually have no idea if they do or if it's a trivial optimization.

I don't think this will work because uniform blocks are backed by a buffer object. In order for this to work, the buffer object would then need to be written to each time the program is bound. That is not a good thing since the buffer object backing a uniform block is supposed to be read only. Moreover, one of the points behind UBO's is that the same buffer object can be used by multiple programs which use the same block type.


However, moving it out of uniform blocks to:



uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;

uniform mat4 ModelViewMatrix = ViewMatrix * ModelMatrix;


makes the suggestion doable; this is so because GL maintains the values of the uniforms as part of GLProgram state, thus changing the value of ViewMatrix would then trigger a change to ModelViewMatrix.

Now for the funny part: it is quite likely that for non-block uniforms, if the compiler detects a variable value which is assigned only once as an expression coming solely from non-block uniforms, it is quite likely a GL implementation would then make a "hidden uniform" holding that value. This is my suspicion.