title: Efficient way to manage matrices within a graphic application using Texture Buffer Object(s) (OpenGL)
body: <p>I'm developping a little 3D Engine using OpenGL and GLSL. I currently use Texture Buffer Objects (TBOs) to store all my matrices (Proj, View, Model and Shadow Matrices). But I did some researches on what is the best way to handle matrices (I mean the most efficient way) within a graphic engine, without any success. The goal is to store a maximum of matrices into a minimum number of TBO and occur a minimum of state changes and a minimum of exchanges between the GPU and client code (glBufferSubData).</p>
  
  <p>I propose 2 different methods (with their advantages and disadvantages):</p>
  
  <p>Here's a scene example:</p>
  
  <p>1 Camera (1 ProjMatrix, 1 ViewMatrix)
  5 boxes (5 ModelMatrix)</p>
  
  <p>Here's an example of a simple vertex shader I use:</p>
  
  <pre><code>#version 400
  
  /*
  ** Vertex attributes.
  */
  layout (location = 0) in vec4 VertexPosition;
  layout (location = 1) in vec2 VertexTexture;
  
  /*
  ** Uniform matrix buffer.
  */
  uniform samplerBuffer matrixBuffer;
  
  /*
  ** Matrix buffer offset.
  */
  uniform int MatrixBufferOffset;
  
  /*
  ** Output variables.
  */
  out vec2 TexCoords;
  
  /*
  ** Returns matrix4x4 from texture cache.
  */
  mat4 Get_Matrix(int offset)
  {
      return (mat4(texelFetch(
          matrixBuffer, offset), texelFetch(
              matrixBuffer, offset + 1), texelFetch(matrixBuffer, offset + 2),
                  texelFetch(matrixBuffer, offset + 3)));
  }
  
  /*
  ** Vertex shader entry point.
  */
  void main(void)
  {
      TexCoords = VertexTexture;
      {
          mat4 ModelViewProjMatrix = Get_Matrix(
              MatrixBufferOffset);
          gl_Position = ModelViewProjMatrix  * VertexPosition;
      }
  }
  </code></pre>
  
  <p>1) The method I currently use: in my vertex shader I use to use ModelViewProjMatrix (needed for rasterization(gl_Position)), ModelViewMatrix (for lighting calculations) and ModelMatrix. So to avoid useless calculation within the vertex shader I've decided to store the ModelViewProjMatrix, the ModelViewMatrix and the ModelMatrix for each mesh node inlined in the TBO as follow:</p>
  
  <p>TBO = {[ModelViewProj_Box1][ModelView_Box1][Model_Box1]|[ModelViewProj_Box2]...}</p>
  
  <p>Advantages: I don't need to compute the product Proj * View * Model (ModelViewProj for example) for each vertex shader (the matrices are pre-calculated).</p>
  
  <p>Disadvantages: if I move the camera I need to update all the ModelViewProj and ModelView matrices. So, a lot of informations to update.</p>
  
  <p>2) I thought about an other way, I think more efficient: store once the projection matrix, once the view matrix and finally each box scene node model matrix once again this way:</p>
  
  <p>TBO = {[ProjMatrix][ViewMatrix][ModelMatrix_Box1][ModelMatrix_Box2]...}</p>
  
  <p>So my vertex shader will look like this:</p>
  
  <pre><code>#version 400
  
  /*
  ** Vertex attributes.
  */
  layout (location = 0) in vec4 VertexPosition;
  layout (location = 1) in vec2 VertexTexture;
  
  /*
  ** Uniform matrix buffer.
  */
  uniform samplerBuffer matrixBuffer;
  
  /*
  ** Matrix buffer offset.
  */
  uniform int MatrixBufferOffset;
  
  /*
  ** Output variables.
  */
  out vec2 TexCoords;
  
  /*
  ** Returns matrix4x4 from texture cache.
  */
  mat4 Get_Matrix(int offset)
  {
      return (mat4(texelFetch(
          matrixBuffer, offset), texelFetch(
              matrixBuffer, offset + 1), texelFetch(matrixBuffer, offset + 2),
                  texelFetch(matrixBuffer, offset + 3)));
  }
  
  /*
  ** Vertex shader entry point.
  */
  void main(void)
  {
      TexCoords = VertexTexture;
      {
          mat4 ProjMatrix = Get_Matrix(MatrixBufferOffset);
          mat4 ViewMatrix = Get_Matrix(MatrixBufferOffset + 4);
          mat4 ModelMatrix = Get_Matrix(MatrixBufferOffset + 8);
  
          gl_Position = ProjMatrix * ViewMatrix * ModelMatrix * VertexPosition;
      }
  }
  </code></pre>
  
  <p>Advantages: The TBO contains the exact number of matrices used. The update is highly targeted (if I move the camera I only updates the view matrix, if I resize the window I only updates the projection matrix and finally if a object is moving only its model matrix will be updated).</p>
  
  <p>Disadvantages: I need to compute fo each vertex within the vertex shader the ModelViewProjMatrix. Plus, if the scene is composed of a huge number of object with each of them owning a different model matrix, I probably need to create a new TBO. Consequently, I will loose the proj/view matrix information because I won't be connect to the right TBO, which bring us to my third method.</p>
  
  <p>3) Store the Projection and View matrix in a TBO and all the other model matrices within another or others TBO(s) as follow:</p>
  
  <p>TBO_0 = {[ProjMatrix][ViewMatrix]}
  TBO_1 = {[ModelMatrix_Box1][ModelMatrix_Box2]...}</p>
  
  <p>What do you think of my 3 methods ? Which one is the best for you?</p>
  
  <p>Thanks a lot in advance for your help!</p>