[Mesa-dev] [PATCH 0/6] Eliminating unused built-in varyings

Marek Olšák maraeo at gmail.com
Thu Jun 13 05:25:41 PDT 2013


Hi everyone,

this series adds a new GLSL compiler optimization pass which eliminates unused and set-but-unused built-in varyings and adds a few improvements to the GLSL linker in the process.

Before I show you how it works, I wanna say that there are patches which are related to and will most probably conflict with the geometry shader work, but they are necessary because the linkage of varyings is largely suboptimal.

Also, the GL_EXT_separate_shader_objects extension must be disabled for this optimization to be enabled. The reason is a program object with both a VS and FS can be bound partially, e.g. by glUseShaderProgramEXT(GL_VERTEX_SHADER, prog), so the extension makes every program object be just a set of "separate shaders". The extension is not important anyway.

Now, to illustrate how the optimization works, consider these 2 shader IR dumps:


Vertex shader (8 varyings):
...
(declare (shader_out ) vec4 gl_FrontColor)
(declare (shader_out ) vec4 gl_FrontSecondaryColor)
(declare (shader_out ) (array vec4 6) gl_TexCoord)
(function main
  (signature void
    (parameters
    )
    (
      ...
      (assign  (xyzw) (var_ref gl_FrontColor)  (var_ref gl_Color) ) 
      (assign  (xyzw) (var_ref gl_FrontSecondaryColor)  (var_ref gl_SecondaryColor) ) 
      (assign  (xyzw) (array_ref (var_ref gl_TexCoord) (constant int (1)) )  (var_ref gl_MultiTexCoord1) ) 
      (assign  (xyzw) (array_ref (var_ref gl_TexCoord) (constant int (4)) )  (var_ref gl_MultiTexCoord4) ) 
      (assign  (xyzw) (array_ref (var_ref gl_TexCoord) (constant int (5)) )  (var_ref gl_MultiTexCoord5) ) 
    ))
)

Fragment shader (6 varyings):
...
(declare (shader_in ) vec4 gl_SecondaryColor)
(declare (shader_in ) (array vec4 5) gl_TexCoord)
(function main
  (signature void
    (parameters
    )
    (
      (declare () vec4 r)
      (assign  (xyzw) (var_ref r)  ... (var_ref gl_SecondaryColor) ) ) 
      (assign  (xyzw) (var_ref r)  ... (array_ref (var_ref gl_TexCoord) (constant int (1)) ) ) ) ) 
      (assign  (xyzw) (var_ref r)  ... (array_ref (var_ref gl_TexCoord) (constant int (2)) ) ) ) ) 
      (assign  (xyzw) (var_ref r)  ... (array_ref (var_ref gl_TexCoord) (constant int (3)) ) ) ) ) 
      (declare (temporary ) vec4 assignment_tmp)
      (assign  (xyzw) (var_ref assignment_tmp)  ... (array_ref (var_ref gl_TexCoord) (constant int (4)) ) ) ) ) 
      ...
    ))
)


Note that only gl_TexCoord[1], gl_TexCoord[4], and gl_SecondaryColor are used by both shaders. The optimization replaces all occurences of varyings which are unused by the other stage by temporary variables. It also breaks down the gl_TexCoord array into separate vec4 variables if needed. Here's the result:


Vertex shader (3 varyings instead of 8):
...
(declare (shader_out ) vec4 gl_out_TexCoord1)
(declare (shader_out ) vec4 gl_out_TexCoord4)
(declare (temporary ) vec4 gl_out_TexCoord5_dummy)
(declare (temporary ) vec4 gl_out_FrontColor0_dummy)
(declare (shader_out ) vec4 gl_FrontSecondaryColor)
(function main
  (signature void
    (parameters
    )
    (
      ...
      (assign  (xyzw) (var_ref gl_out_FrontColor0_dummy)  (var_ref gl_Color) ) 
      (assign  (xyzw) (var_ref gl_FrontSecondaryColor)  (var_ref gl_SecondaryColor) ) 
      (assign  (xyzw) (var_ref gl_out_TexCoord1)  (var_ref gl_MultiTexCoord1) ) 
      (assign  (xyzw) (var_ref gl_out_TexCoord4)  (var_ref gl_MultiTexCoord4) ) 
      (assign  (xyzw) (var_ref gl_out_TexCoord5_dummy)  (var_ref gl_MultiTexCoord5) ) 
    ))
)

Fragment shader (3 varyings instead of 6):
...
(declare (shader_in ) vec4 gl_in_TexCoord1)
(declare (temporary ) vec4 gl_in_TexCoord2_dummy)
(declare (temporary ) vec4 gl_in_TexCoord3_dummy)
(declare (shader_in ) vec4 gl_in_TexCoord4)
(declare (shader_in ) vec4 gl_SecondaryColor)
(function main
  (signature void
    (parameters
    )
    (
      (declare () vec4 r)
      (assign  (xyzw) (var_ref r)  ... (var_ref gl_SecondaryColor) ) ) 
      (assign  (xyzw) (var_ref r)  ... (var_ref gl_in_TexCoord1) ) ) ) 
      (assign  (xyzw) (var_ref r)  ... (var_ref gl_in_TexCoord2_dummy) ) ) ) 
      (assign  (xyzw) (var_ref r)  ... (var_ref gl_in_TexCoord3_dummy) ) ) ) 
      (declare (temporary ) vec4 assignment_tmp)
      (assign  (xyzw) (var_ref assignment_tmp)  ... (var_ref gl_in_TexCoord4) ) ) ) 
      ...
    ))
)

The locations of varyings remain the same. That's all. Please review.

Marek


More information about the mesa-dev mailing list