[Mesa-dev] [PATCH 08/13] glsl: move update_uniform_buffer_variables() to lower UBO

Timothy Arceri timothy.arceri at collabora.com
Wed Jul 27 05:20:49 UTC 2016


This make more sense here as its lowering that uses the results of
this function.

This allows us to call lower_ubo_reference() before assigning uniform
locations which is useful for calling backend specific optimisations
on the IR before assigning uniform and varying locations.

While we are at it we also call the other lowering passes before
assigning locations.
---
 src/compiler/glsl/link_uniforms.cpp       | 71 ------------------------------
 src/compiler/glsl/linker.cpp              | 38 ++++++++--------
 src/compiler/glsl/lower_ubo_reference.cpp | 72 +++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+), 90 deletions(-)

diff --git a/src/compiler/glsl/link_uniforms.cpp b/src/compiler/glsl/link_uniforms.cpp
index 793f12c..bb8905b 100644
--- a/src/compiler/glsl/link_uniforms.cpp
+++ b/src/compiler/glsl/link_uniforms.cpp
@@ -882,75 +882,6 @@ public:
 };
 
 /**
- * Walks the IR and update the references to uniform blocks in the
- * ir_variables to point at linked shader's list (previously, they
- * would point at the uniform block list in one of the pre-linked
- * shaders).
- */
-static void
-link_update_uniform_buffer_variables(struct gl_linked_shader *shader)
-{
-   foreach_in_list(ir_instruction, node, shader->ir) {
-      ir_variable *const var = node->as_variable();
-
-      if ((var == NULL) || !var->is_in_buffer_block())
-         continue;
-
-      assert(var->data.mode == ir_var_uniform ||
-             var->data.mode == ir_var_shader_storage);
-
-      if (var->is_interface_instance()) {
-         var->data.location = 0;
-         continue;
-      }
-
-      bool found = false;
-      char sentinel = '\0';
-
-      if (var->type->is_record()) {
-         sentinel = '.';
-      } else if (var->type->is_array() && (var->type->fields.array->is_array()
-                 || var->type->without_array()->is_record())) {
-         sentinel = '[';
-      }
-
-      unsigned num_blocks = var->data.mode == ir_var_uniform ?
-         shader->NumUniformBlocks : shader->NumShaderStorageBlocks;
-      struct gl_uniform_block **blks = var->data.mode == ir_var_uniform ?
-         shader->UniformBlocks : shader->ShaderStorageBlocks;
-
-      const unsigned l = strlen(var->name);
-      for (unsigned i = 0; i < num_blocks; i++) {
-         for (unsigned j = 0; j < blks[i]->NumUniforms; j++) {
-            if (sentinel) {
-               const char *begin = blks[i]->Uniforms[j].Name;
-               const char *end = strchr(begin, sentinel);
-
-               if (end == NULL)
-                  continue;
-
-               if ((ptrdiff_t) l != (end - begin))
-                  continue;
-
-               if (strncmp(var->name, begin, l) == 0) {
-                  found = true;
-                  var->data.location = j;
-                  break;
-               }
-            } else if (!strcmp(var->name, blks[i]->Uniforms[j].Name)) {
-               found = true;
-               var->data.location = j;
-               break;
-            }
-         }
-         if (found)
-            break;
-      }
-      assert(found);
-   }
-}
-
-/**
  * Combine the hidden uniform hash map with the uniform hash map so that the
  * hidden uniforms will be given indicies at the end of the uniform storage
  * array.
@@ -1261,8 +1192,6 @@ link_assign_uniform_locations(struct gl_shader_program *prog,
       memset(sh->SamplerUnits, 0, sizeof(sh->SamplerUnits));
       memset(sh->ImageUnits, 0, sizeof(sh->ImageUnits));
 
-      link_update_uniform_buffer_variables(sh);
-
       /* Reset various per-shader target counts.
        */
       uniform_size.start_shader();
diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp
index ba61d39..a2b1ce2 100644
--- a/src/compiler/glsl/linker.cpp
+++ b/src/compiler/glsl/linker.cpp
@@ -4559,6 +4559,25 @@ link_varyings_and_uniforms(unsigned first, unsigned last,
          return false;
    }
 
+   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+      struct gl_linked_shader *sh = prog->_LinkedShaders[i];
+      if (sh == NULL)
+         continue;
+
+      const struct gl_shader_compiler_options *options =
+         &ctx->Const.ShaderCompilerOptions[i];
+
+      if (options->LowerBufferInterfaceBlocks)
+         lower_ubo_reference(prog->_LinkedShaders[i],
+                             options->ClampBlockIndicesToArrayBounds);
+
+      if (options->LowerShaderSharedVariables)
+         lower_shared_reference(sh, &prog->Comp.SharedSize);
+
+      lower_vector_derefs(sh);
+      do_vec_index_to_swizzle(sh->ir);
+   }
+
    /* If there is no fragment shader we need to set transform feedback.
     *
     * For SSO we also need to assign output locations.  We assign them here
@@ -4671,25 +4690,6 @@ link_varyings_and_uniforms(unsigned first, unsigned last,
    if (!prog->LinkStatus)
       return false;
 
-   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      if (prog->_LinkedShaders[i] == NULL)
-         continue;
-
-      const struct gl_shader_compiler_options *options =
-         &ctx->Const.ShaderCompilerOptions[i];
-
-      if (options->LowerBufferInterfaceBlocks)
-         lower_ubo_reference(prog->_LinkedShaders[i],
-                             options->ClampBlockIndicesToArrayBounds);
-
-      if (options->LowerShaderSharedVariables)
-         lower_shared_reference(prog->_LinkedShaders[i],
-                                &prog->Comp.SharedSize);
-
-      lower_vector_derefs(prog->_LinkedShaders[i]);
-      do_vec_index_to_swizzle(prog->_LinkedShaders[i]->ir);
-   }
-
    return true;
 }
 
diff --git a/src/compiler/glsl/lower_ubo_reference.cpp b/src/compiler/glsl/lower_ubo_reference.cpp
index 083f43f..12dc048 100644
--- a/src/compiler/glsl/lower_ubo_reference.cpp
+++ b/src/compiler/glsl/lower_ubo_reference.cpp
@@ -1089,9 +1089,81 @@ lower_ubo_reference_visitor::visit_enter(ir_call *ir)
 
 } /* unnamed namespace */
 
+/**
+ * Walks the IR and update the references to uniform blocks in the
+ * ir_variables to point at linked shader's list (previously, they
+ * would point at the uniform block list in one of the pre-linked
+ * shaders).
+ */
+static void
+update_uniform_buffer_variables(struct gl_linked_shader *shader)
+{
+   foreach_in_list(ir_instruction, node, shader->ir) {
+      ir_variable *const var = node->as_variable();
+
+      if ((var == NULL) || !var->is_in_buffer_block())
+         continue;
+
+      assert(var->data.mode == ir_var_uniform ||
+             var->data.mode == ir_var_shader_storage);
+
+      if (var->is_interface_instance()) {
+         var->data.location = 0;
+         continue;
+      }
+
+      bool found = false;
+      char sentinel = '\0';
+
+      if (var->type->is_record()) {
+         sentinel = '.';
+      } else if (var->type->is_array() && (var->type->fields.array->is_array()
+                 || var->type->without_array()->is_record())) {
+         sentinel = '[';
+      }
+
+      unsigned num_blocks = var->data.mode == ir_var_uniform ?
+         shader->NumUniformBlocks : shader->NumShaderStorageBlocks;
+      struct gl_uniform_block **blks = var->data.mode == ir_var_uniform ?
+         shader->UniformBlocks : shader->ShaderStorageBlocks;
+
+      const unsigned l = strlen(var->name);
+      for (unsigned i = 0; i < num_blocks; i++) {
+         for (unsigned j = 0; j < blks[i]->NumUniforms; j++) {
+            if (sentinel) {
+               const char *begin = blks[i]->Uniforms[j].Name;
+               const char *end = strchr(begin, sentinel);
+
+               if (end == NULL)
+                  continue;
+
+               if ((ptrdiff_t) l != (end - begin))
+                  continue;
+
+               if (strncmp(var->name, begin, l) == 0) {
+                  found = true;
+                  var->data.location = j;
+                  break;
+               }
+            } else if (!strcmp(var->name, blks[i]->Uniforms[j].Name)) {
+               found = true;
+               var->data.location = j;
+               break;
+            }
+         }
+         if (found)
+            break;
+      }
+      assert(found);
+   }
+}
+
+
 void
 lower_ubo_reference(struct gl_linked_shader *shader, bool clamp_block_indices)
 {
+   update_uniform_buffer_variables(shader);
+
    lower_ubo_reference_visitor v(shader, clamp_block_indices);
 
    /* Loop over the instructions lowering references, because we take
-- 
2.7.4



More information about the mesa-dev mailing list