Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: GLSL initialized uniform block members

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    3

    GLSL initialized uniform block members

    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
    Code :
    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.
    Last edited by saucyio; 09-18-2013 at 03:18 PM.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2009
    Posts
    591
    Quote Originally Posted by saucyio View Post
    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
    Code :
    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:

    Code :
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •