[Mesa-dev] [PATCH 02/16] mesa: rework _mesa_add_parameter() to only add a single param

Timothy Arceri tarceri at itsqueeze.com
Tue Jun 20 01:50:31 UTC 2017


This is more inline with what the functions name suggests it should
do, and makes the code much easier to follow.

This will also make adding uniform packing support much simpler.
---
 src/compiler/glsl/shader_cache.cpp |  4 +--
 src/mesa/program/ir_to_mesa.cpp    | 19 ++++++++++---
 src/mesa/program/prog_parameter.c  | 57 +++++++++++++++-----------------------
 3 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/src/compiler/glsl/shader_cache.cpp b/src/compiler/glsl/shader_cache.cpp
index fdeab8e..f3d3d78 100644
--- a/src/compiler/glsl/shader_cache.cpp
+++ b/src/compiler/glsl/shader_cache.cpp
@@ -1079,21 +1079,21 @@ write_shader_parameters(struct blob *metadata,
    while (i < params->NumParameters) {
       struct gl_program_parameter *param = &params->Parameters[i];
 
       blob_write_uint32(metadata, param->Type);
       blob_write_string(metadata, param->Name);
       blob_write_uint32(metadata, param->Size);
       blob_write_uint32(metadata, param->DataType);
       blob_write_bytes(metadata, param->StateIndexes,
                        sizeof(param->StateIndexes));
 
-      i += (param->Size + 3) / 4;
+      i++;
    }
 
    blob_write_bytes(metadata, params->ParameterValues,
                     sizeof(gl_constant_value) * 4 * params->NumParameters);
 
    blob_write_uint32(metadata, params->StateFlags);
 }
 
 static void
 read_shader_parameters(struct blob_reader *metadata,
@@ -1108,21 +1108,21 @@ read_shader_parameters(struct blob_reader *metadata,
       gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
       const char *name = blob_read_string(metadata);
       unsigned size = blob_read_uint32(metadata);
       unsigned data_type = blob_read_uint32(metadata);
       blob_copy_bytes(metadata, (uint8_t *) state_indexes,
                       sizeof(state_indexes));
 
       _mesa_add_parameter(params, type, name, size, data_type,
                           NULL, state_indexes);
 
-      i += (size + 3) / 4;
+      i++;
    }
 
    blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
                     sizeof(gl_constant_value) * 4 * params->NumParameters);
 
    params->StateFlags = blob_read_uint32(metadata);
 }
 
 static void
 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index d70494c..dd757c6 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2448,24 +2448,35 @@ add_uniform_to_shader::visit_field(const glsl_type *type, const char *name,
                                    bool /* last_field */)
 {
    /* opaque types don't use storage in the param list unless they are
     * bindless samplers or images.
     */
    if (type->contains_opaque() && !var->data.bindless)
       return;
 
    int index = _mesa_lookup_parameter_index(params, name);
    if (index < 0) {
-      unsigned size = type_size(type) * 4;
-
-      index = _mesa_add_parameter(params, PROGRAM_UNIFORM, name, size,
-                                  type->gl_type, NULL, NULL);
+      unsigned num_params = type->arrays_of_arrays_size();
+      num_params = MAX2(num_params, 1);
+      num_params *= type->without_array()->matrix_columns;
+
+      bool is_dual_slot = type->without_array()->is_dual_slot();
+      if (is_dual_slot)
+         num_params *= 2;
+
+      _mesa_reserve_parameter_storage(params, num_params);
+      index = params->NumParameters;
+      for (unsigned i = 0; i < num_params; i++) {
+         unsigned comps = 4;
+         _mesa_add_parameter(params, PROGRAM_UNIFORM, name, comps,
+                             type->gl_type, NULL, NULL);
+      }
    }
 
    /* The first part of the uniform that's processed determines the base
     * location of the whole uniform (for structures).
     */
    if (this->idx < 0)
       this->idx = index;
 }
 
 /**
diff --git a/src/mesa/program/prog_parameter.c b/src/mesa/program/prog_parameter.c
index 40bc47d..81609f5 100644
--- a/src/mesa/program/prog_parameter.c
+++ b/src/mesa/program/prog_parameter.c
@@ -227,73 +227,62 @@ _mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
  * \param state  state indexes, or NULL
  * \return  index of new parameter in the list, or -1 if error (out of mem)
  */
 GLint
 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
                     gl_register_file type, const char *name,
                     GLuint size, GLenum datatype,
                     const gl_constant_value *values,
                     const gl_state_index state[STATE_LENGTH])
 {
+   assert(0 < size && size <=4);
    const GLuint oldNum = paramList->NumParameters;
-   const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
 
-   assert(size > 0);
-
-   _mesa_reserve_parameter_storage(paramList, sz4);
+   _mesa_reserve_parameter_storage(paramList, 1);
 
    if (!paramList->Parameters ||
        !paramList->ParameterValues) {
       /* out of memory */
       paramList->NumParameters = 0;
       paramList->Size = 0;
       return -1;
    }
 
-   GLuint i, j;
-
-   paramList->NumParameters = oldNum + sz4;
+   paramList->NumParameters = oldNum + 1;
 
    memset(&paramList->Parameters[oldNum], 0,
-          sz4 * sizeof(struct gl_program_parameter));
+          sizeof(struct gl_program_parameter));
 
-   for (i = 0; i < sz4; i++) {
-      struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
-      p->Name = strdup(name ? name : "");
-      p->Type = type;
-      p->Size = size;
-      p->DataType = datatype;
-      if (values) {
-         if (size >= 4) {
-            COPY_4V(paramList->ParameterValues[oldNum + i], values);
-         } else {
-            /* copy 1, 2 or 3 values */
-            assert(size < 4);
-            for (j = 0; j < size; j++) {
-               paramList->ParameterValues[oldNum + i][j].f = values[j].f;
-            }
-            /* fill in remaining positions with zeros */
-            for (; j < 4; j++) {
-               paramList->ParameterValues[oldNum + i][j].f = 0.0f;
-            }
-         }
-         values += 4;
+   struct gl_program_parameter *p = paramList->Parameters + oldNum;
+   p->Name = strdup(name ? name : "");
+   p->Type = type;
+   p->Size = size;
+   p->DataType = datatype;
+
+   if (values) {
+      if (size >= 4) {
+         COPY_4V(paramList->ParameterValues[oldNum], values);
       } else {
-         /* silence valgrind */
-         for (j = 0; j < 4; j++)
-            paramList->ParameterValues[oldNum + i][j].f = 0;
+         /* copy 1, 2 or 3 values */
+         assert(size < 4);
+         for (unsigned j = 0; j < size; j++) {
+            paramList->ParameterValues[oldNum][j].f = values[j].f;
+         }
+      }
+   } else {
+      for (unsigned j = 0; j < 4; j++) {
+         paramList->ParameterValues[oldNum][j].f = 0;
       }
-      size -= 4;
    }
 
    if (state) {
-      for (i = 0; i < STATE_LENGTH; i++)
+      for (unsigned i = 0; i < STATE_LENGTH; i++)
          paramList->Parameters[oldNum].StateIndexes[i] = state[i];
    }
 
    return (GLint) oldNum;
 }
 
 
 /**
  * Add a new unnamed constant to the parameter list.  This will be used
  * when a fragment/vertex program contains something like this:
-- 
2.9.4



More information about the mesa-dev mailing list