[Mesa-dev] [PATCH 11/26] glsl: Make ir_variable::num_state_slots and ir_variable::state_slots private

Ian Romanick idr at freedesktop.org
Mon Jul 14 15:48:43 PDT 2014


From: Ian Romanick <ian.d.romanick at intel.com>

Also move num_state_slots inside ir_variable_data for better packing.

The payoff for this will come in a few more patches.

No change Valgrind massif results for a trimmed apitrace of dota2.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/glsl/builtin_variables.cpp                 |  5 +--
 src/glsl/ir.h                                  | 61 +++++++++++++++++++-------
 src/glsl/ir_clone.cpp                          | 13 ++----
 src/glsl/ir_validate.cpp                       |  2 +-
 src/glsl/linker.cpp                            |  7 +--
 src/mesa/drivers/dri/i965/brw_fs.cpp           |  6 +--
 src/mesa/drivers/dri/i965/brw_shader.cpp       |  6 +--
 src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp |  6 +--
 src/mesa/program/ir_to_mesa.cpp                | 14 +++---
 src/mesa/state_tracker/st_glsl_to_tgsi.cpp     | 14 +++---
 10 files changed, 78 insertions(+), 56 deletions(-)

diff --git a/src/glsl/builtin_variables.cpp b/src/glsl/builtin_variables.cpp
index 6fc3319..9e31fb8 100644
--- a/src/glsl/builtin_variables.cpp
+++ b/src/glsl/builtin_variables.cpp
@@ -478,12 +478,9 @@ builtin_variable_generator::add_uniform(const glsl_type *type,
       &_mesa_builtin_uniform_desc[i];
 
    const unsigned array_count = type->is_array() ? type->length : 1;
-   uni->num_state_slots = array_count * statevar->num_elements;
 
    ir_state_slot *slots =
-      ralloc_array(uni, ir_state_slot, uni->num_state_slots);
-
-   uni->state_slots = slots;
+      uni->allocate_state_slots(array_count * statevar->num_elements);
 
    for (unsigned a = 0; a < array_count; a++) {
       for (unsigned j = 0; j < statevar->num_elements; j++) {
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index 1924098..c47a1fb 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -537,6 +537,37 @@ public:
       return this->max_ifc_array_access;
    }
 
+   inline unsigned get_num_state_slots() const
+   {
+      return this->data._num_state_slots;
+   }
+
+   inline void set_num_state_slots(unsigned n)
+   {
+      this->data._num_state_slots = n;
+   }
+
+   inline ir_state_slot *get_state_slots()
+   {
+      return this->state_slots;
+   }
+
+   inline const ir_state_slot *get_state_slots() const
+   {
+      return this->state_slots;
+   }
+
+   inline ir_state_slot *allocate_state_slots(unsigned n)
+   {
+      this->state_slots = ralloc_array(this, ir_state_slot, n);
+      this->data._num_state_slots = 0;
+
+      if (this->state_slots != NULL)
+         this->data._num_state_slots = n;
+
+      return this->state_slots;
+   }
+
    /**
     * Enable emitting extension warnings for this variable
     */
@@ -729,6 +760,10 @@ public:
       /** Image internal format if specified explicitly, otherwise GL_NONE. */
       uint16_t image_format;
 
+   private:
+      unsigned _num_state_slots;    /**< Number of state slots used */
+
+   public:
       /**
        * Storage location of the base of this variable
        *
@@ -782,22 +817,6 @@ public:
    } data;
 
    /**
-    * Built-in state that backs this uniform
-    *
-    * Once set at variable creation, \c state_slots must remain invariant.
-    * This is because, ideally, this array would be shared by all clones of
-    * this variable in the IR tree.  In other words, we'd really like for it
-    * to be a fly-weight.
-    *
-    * If the variable is not a uniform, \c num_state_slots will be zero and
-    * \c state_slots will be \c NULL.
-    */
-   /*@{*/
-   unsigned num_state_slots;    /**< Number of state slots used */
-   ir_state_slot *state_slots;  /**< State descriptors. */
-   /*@}*/
-
-   /**
     * Value assigned in the initializer of a variable declared "const"
     */
    ir_constant *constant_value;
@@ -829,6 +848,16 @@ private:
    unsigned *max_ifc_array_access;
 
    /**
+    * Built-in state that backs this uniform
+    *
+    * Once set at variable creation, \c state_slots must remain invariant.
+    *
+    * If the variable is not a uniform, \c _num_state_slots will be zero and
+    * \c state_slots will be \c NULL.
+    */
+   ir_state_slot *state_slots;
+
+   /**
     * For variables that are in an interface block or are an instance of an
     * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
     *
diff --git a/src/glsl/ir_clone.cpp b/src/glsl/ir_clone.cpp
index 86c0e31..64019fe 100644
--- a/src/glsl/ir_clone.cpp
+++ b/src/glsl/ir_clone.cpp
@@ -53,15 +53,10 @@ ir_variable::clone(void *mem_ctx, struct hash_table *ht) const
 
    memcpy(&var->data, &this->data, sizeof(var->data));
 
-   var->num_state_slots = this->num_state_slots;
-   if (this->state_slots) {
-      /* FINISHME: This really wants to use something like talloc_reference, but
-       * FINISHME: ralloc doesn't have any similar function.
-       */
-      var->state_slots = ralloc_array(var, ir_state_slot,
-				      this->num_state_slots);
-      memcpy(var->state_slots, this->state_slots,
-	     sizeof(this->state_slots[0]) * var->num_state_slots);
+   if (this->get_state_slots()) {
+      ir_state_slot *s = var->allocate_state_slots(this->get_num_state_slots());
+      memcpy(s, this->get_state_slots(),
+             sizeof(s[0]) * var->get_num_state_slots());
    }
 
    if (this->constant_value)
diff --git a/src/glsl/ir_validate.cpp b/src/glsl/ir_validate.cpp
index b7afd08..12b8874 100644
--- a/src/glsl/ir_validate.cpp
+++ b/src/glsl/ir_validate.cpp
@@ -703,7 +703,7 @@ ir_validate::visit(ir_variable *ir)
 
    if (ir->data.mode == ir_var_uniform
        && strncmp(ir->name, "gl_", 3) == 0
-       && ir->state_slots == NULL) {
+       && ir->get_state_slots() == NULL) {
       printf("built-in uniform has no state\n");
       ir->print();
       abort();
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
index 8c1dfec..2119943 100644
--- a/src/glsl/linker.cpp
+++ b/src/glsl/linker.cpp
@@ -1813,9 +1813,10 @@ update_array_sizes(struct gl_shader_program *prog)
 	     * Determine the number of slots per array element by dividing by
 	     * the old (total) size.
 	     */
-	    if (var->num_state_slots > 0) {
-	       var->num_state_slots = (size + 1)
-		  * (var->num_state_slots / var->type->length);
+            const unsigned num_slots = var->get_num_state_slots();
+	    if (num_slots > 0) {
+	       var->set_num_state_slots((size + 1)
+                                        * (num_slots / var->type->length));
 	    }
 
 	    var->type = glsl_type::get_array_instance(var->type->fields.array,
diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 951d69f..bb41ec2 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -979,10 +979,10 @@ fs_visitor::setup_uniform_values(ir_variable *ir)
 void
 fs_visitor::setup_builtin_uniform_values(ir_variable *ir)
 {
-   const ir_state_slot *const slots = ir->state_slots;
-   assert(ir->state_slots != NULL);
+   const ir_state_slot *const slots = ir->get_state_slots();
+   assert(slots != NULL);
 
-   for (unsigned int i = 0; i < ir->num_state_slots; i++) {
+   for (unsigned int i = 0; i < ir->get_num_state_slots(); i++) {
       /* This state reference has already been setup by ir_to_mesa, but we'll
        * get the same index back here.
        */
diff --git a/src/mesa/drivers/dri/i965/brw_shader.cpp b/src/mesa/drivers/dri/i965/brw_shader.cpp
index 318802b..a2e953e 100644
--- a/src/mesa/drivers/dri/i965/brw_shader.cpp
+++ b/src/mesa/drivers/dri/i965/brw_shader.cpp
@@ -220,10 +220,10 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
 	     || (strncmp(var->name, "gl_", 3) != 0))
 	    continue;
 
-	 const ir_state_slot *const slots = var->state_slots;
-	 assert(var->state_slots != NULL);
+	 const ir_state_slot *const slots = var->get_state_slots();
+	 assert(slots != NULL);
 
-	 for (unsigned int i = 0; i < var->num_state_slots; i++) {
+	 for (unsigned int i = 0; i < var->get_num_state_slots(); i++) {
 	    _mesa_add_state_reference(prog->Parameters,
 				      (gl_state_index *) slots[i].tokens);
 	 }
diff --git a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
index 481b885..bbd3046 100644
--- a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
+++ b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
@@ -729,10 +729,10 @@ vec4_visitor::setup_uniform_clipplane_values()
 void
 vec4_visitor::setup_builtin_uniform_values(ir_variable *ir)
 {
-   const ir_state_slot *const slots = ir->state_slots;
-   assert(ir->state_slots != NULL);
+   const ir_state_slot *const slots = ir->get_state_slots();
+   assert(slots != NULL);
 
-   for (unsigned int i = 0; i < ir->num_state_slots; i++) {
+   for (unsigned int i = 0; i < ir->get_num_state_slots(); i++) {
       /* This state reference has already been setup by ir_to_mesa,
        * but we'll get the same index back here.  We can reference
        * ParameterValues directly, since unlike brw_fs.cpp, we never
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index 8aefcf7..def21b2 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -683,8 +683,8 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
 
    if (ir->data.mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
       unsigned int i;
-      const ir_state_slot *const slots = ir->state_slots;
-      assert(ir->state_slots != NULL);
+      const ir_state_slot *const slots = ir->get_state_slots();
+      assert(slots != NULL);
 
       /* Check if this statevar's setup in the STATE file exactly
        * matches how we'll want to reference it as a
@@ -692,7 +692,7 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
        * temporary storage and hope that it'll get copy-propagated
        * out.
        */
-      for (i = 0; i < ir->num_state_slots; i++) {
+      for (i = 0; i < ir->get_num_state_slots(); i++) {
 	 if (slots[i].swizzle != SWIZZLE_XYZW) {
 	    break;
 	 }
@@ -700,7 +700,7 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
 
       variable_storage *storage;
       dst_reg dst;
-      if (i == ir->num_state_slots) {
+      if (i == ir->get_num_state_slots()) {
 	 /* We'll set the index later. */
 	 storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
 	 this->variables.push_tail(storage);
@@ -711,7 +711,7 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
 	  * of the type.  However, this had better match the number of state
 	  * elements that we're going to copy into the new temporary.
 	  */
-	 assert((int) ir->num_state_slots == type_size(ir->type));
+	 assert((int) ir->get_num_state_slots() == type_size(ir->type));
 
 	 storage = new(mem_ctx) variable_storage(ir, PROGRAM_TEMPORARY,
 						 this->next_temp);
@@ -722,7 +722,7 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
       }
 
 
-      for (unsigned int i = 0; i < ir->num_state_slots; i++) {
+      for (unsigned int i = 0; i < ir->get_num_state_slots(); i++) {
 	 int index = _mesa_add_state_reference(this->prog->Parameters,
 					       (gl_state_index *)slots[i].tokens);
 
@@ -742,7 +742,7 @@ ir_to_mesa_visitor::visit(ir_variable *ir)
       }
 
       if (storage->file == PROGRAM_TEMPORARY &&
-	  dst.index != storage->index + (int) ir->num_state_slots) {
+	  dst.index != storage->index + (int) ir->get_num_state_slots()) {
 	 linker_error(this->shader_program,
 		      "failed to load builtin uniform `%s' "
 		      "(%d/%d regs loaded)\n",
diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
index 5c51e63..ef3f3e0 100644
--- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
@@ -1079,8 +1079,8 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
 
    if (ir->data.mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
       unsigned int i;
-      const ir_state_slot *const slots = ir->state_slots;
-      assert(ir->state_slots != NULL);
+      const ir_state_slot *const slots = ir->get_state_slots();
+      assert(slots != NULL);
 
       /* Check if this statevar's setup in the STATE file exactly
        * matches how we'll want to reference it as a
@@ -1088,7 +1088,7 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
        * temporary storage and hope that it'll get copy-propagated
        * out.
        */
-      for (i = 0; i < ir->num_state_slots; i++) {
+      for (i = 0; i < ir->get_num_state_slots(); i++) {
          if (slots[i].swizzle != SWIZZLE_XYZW) {
             break;
          }
@@ -1096,7 +1096,7 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
 
       variable_storage *storage;
       st_dst_reg dst;
-      if (i == ir->num_state_slots) {
+      if (i == ir->get_num_state_slots()) {
          /* We'll set the index later. */
          storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
          this->variables.push_tail(storage);
@@ -1107,7 +1107,7 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
           * of the type.  However, this had better match the number of state
           * elements that we're going to copy into the new temporary.
           */
-         assert((int) ir->num_state_slots == type_size(ir->type));
+         assert((int) ir->get_num_state_slots() == type_size(ir->type));
 
          dst = st_dst_reg(get_temp(ir->type));
 
@@ -1117,7 +1117,7 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
       }
 
 
-      for (unsigned int i = 0; i < ir->num_state_slots; i++) {
+      for (unsigned int i = 0; i < ir->get_num_state_slots(); i++) {
          int index = _mesa_add_state_reference(this->prog->Parameters,
         				       (gl_state_index *)slots[i].tokens);
 
@@ -1142,7 +1142,7 @@ glsl_to_tgsi_visitor::visit(ir_variable *ir)
       }
 
       if (storage->file == PROGRAM_TEMPORARY &&
-          dst.index != storage->index + (int) ir->num_state_slots) {
+          dst.index != storage->index + (int) ir->get_num_state_slots()) {
          fail_link(this->shader_program,
         	   "failed to load builtin uniform `%s'  (%d/%d regs loaded)\n",
         	   ir->name, dst.index - storage->index,
-- 
1.8.1.4



More information about the mesa-dev mailing list