[Mesa-dev] [PATCH 08/12] i965: add support for packing arrays

Timothy Arceri timothy.arceri at collabora.com
Wed May 25 03:07:12 UTC 2016


Here we add a new param to the type_size functions in order to pass
in the size of a varying once packing is taken into account.
---
 src/compiler/nir/nir.h                         |  6 +++--
 src/compiler/nir/nir_lower_io.c                | 35 +++++++++++++++++---------
 src/mesa/drivers/dri/i965/brw_blorp.c          |  6 +++--
 src/mesa/drivers/dri/i965/brw_fs.cpp           | 23 ++++++++++++++---
 src/mesa/drivers/dri/i965/brw_nir.c            | 22 ++++++++--------
 src/mesa/drivers/dri/i965/brw_shader.h         |  6 +++--
 src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp |  7 ++++++
 src/mesa/state_tracker/st_glsl_to_nir.cpp      |  2 +-
 src/mesa/state_tracker/st_glsl_to_tgsi.cpp     |  2 +-
 src/mesa/state_tracker/st_glsl_types.cpp       |  3 ++-
 src/mesa/state_tracker/st_glsl_types.h         |  2 +-
 11 files changed, 78 insertions(+), 36 deletions(-)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index e06c4d5..39887cc 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2316,11 +2316,13 @@ void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
 
 void nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
                               unsigned base_offset,
-                              int (*type_size)(const struct glsl_type *));
+                              int (*type_size)(const struct glsl_type *,
+                                               unsigned num_packed_components));
 
 void nir_lower_io(nir_shader *shader,
                   nir_variable_mode modes,
-                  int (*type_size)(const struct glsl_type *));
+                  int (*type_size)(const struct glsl_type *,
+                                   unsigned num_packed_components));
 nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
 nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);
 
diff --git a/src/compiler/nir/nir_lower_io.c b/src/compiler/nir/nir_lower_io.c
index 2079004..c3c0e2e 100644
--- a/src/compiler/nir/nir_lower_io.c
+++ b/src/compiler/nir/nir_lower_io.c
@@ -37,14 +37,16 @@
 struct lower_io_state {
    nir_builder builder;
    void *mem_ctx;
-   int (*type_size)(const struct glsl_type *type);
+   int (*type_size)(const struct glsl_type *type,
+                    unsigned num_packed_components);
    nir_variable_mode modes;
 };
 
 void
 nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
                          unsigned base_offset,
-                         int (*type_size)(const struct glsl_type *))
+                         int (*type_size)(const struct glsl_type *,
+                                          unsigned num_packed_components))
 {
    unsigned location = 0;
 
@@ -80,7 +82,7 @@ nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
       } else {
          var->data.driver_location = location;
       }
-      location += type_size(var->type);
+      location += type_size(var->type, var->data.num_packed_components);
    }
 
    *size = location;
@@ -112,7 +114,9 @@ is_per_vertex_output(struct lower_io_state *state, nir_variable *var)
 static nir_ssa_def *
 get_io_offset(nir_builder *b, nir_deref_var *deref,
               nir_ssa_def **vertex_index,
-              int (*type_size)(const struct glsl_type *))
+              int (*type_size)(const struct glsl_type *,
+                               unsigned num_packed_components),
+              unsigned num_packed_components)
 {
    nir_deref *tail = &deref->deref;
 
@@ -140,7 +144,7 @@ get_io_offset(nir_builder *b, nir_deref_var *deref,
 
       if (tail->deref_type == nir_deref_type_array) {
          nir_deref_array *deref_array = nir_deref_as_array(tail);
-         unsigned size = type_size(tail->type);
+         unsigned size = type_size(tail->type, num_packed_components);
 
          offset = nir_iadd(b, offset,
                            nir_imm_int(b, size * deref_array->base_offset));
@@ -157,7 +161,8 @@ get_io_offset(nir_builder *b, nir_deref_var *deref,
 
          unsigned field_offset = 0;
          for (unsigned i = 0; i < deref_struct->index; i++) {
-            field_offset += type_size(glsl_get_struct_field(parent_type, i));
+            field_offset +=
+               type_size(glsl_get_struct_field(parent_type, i), 0);
          }
          offset = nir_iadd(b, offset, nir_imm_int(b, field_offset));
       }
@@ -288,7 +293,9 @@ nir_lower_io_block(nir_block *block,
 
          offset = get_io_offset(b, intrin->variables[0],
                                 per_vertex ? &vertex_index : NULL,
-                                state->type_size);
+                                state->type_size,
+                                intrin->variables[0]->var->
+                                   data.num_packed_components);
 
          nir_intrinsic_instr *load =
             nir_intrinsic_instr_create(state->mem_ctx,
@@ -304,7 +311,7 @@ nir_lower_io_block(nir_block *block,
 
          if (load->intrinsic == nir_intrinsic_load_uniform) {
             nir_intrinsic_set_range(load,
-               state->type_size(intrin->variables[0]->var->type));
+               state->type_size(intrin->variables[0]->var->type, 0));
          }
 
          if (per_vertex)
@@ -338,7 +345,9 @@ nir_lower_io_block(nir_block *block,
 
          offset = get_io_offset(b, intrin->variables[0],
                                 per_vertex ? &vertex_index : NULL,
-                                state->type_size);
+                                state->type_size,
+                                intrin->variables[0]->var->
+                                   data.num_packed_components);
 
          nir_intrinsic_instr *store =
             nir_intrinsic_instr_create(state->mem_ctx,
@@ -380,7 +389,7 @@ nir_lower_io_block(nir_block *block,
          nir_ssa_def *offset;
 
          offset = get_io_offset(b, intrin->variables[0],
-                                NULL, state->type_size);
+                                NULL, state->type_size, 0);
 
          nir_intrinsic_instr *atomic =
             nir_intrinsic_instr_create(state->mem_ctx,
@@ -423,7 +432,8 @@ nir_lower_io_block(nir_block *block,
 static void
 nir_lower_io_impl(nir_function_impl *impl,
                   nir_variable_mode modes,
-                  int (*type_size)(const struct glsl_type *))
+                  int (*type_size)(const struct glsl_type *,
+                                   unsigned num_packed_components))
 {
    struct lower_io_state state;
 
@@ -442,7 +452,8 @@ nir_lower_io_impl(nir_function_impl *impl,
 
 void
 nir_lower_io(nir_shader *shader, nir_variable_mode modes,
-             int (*type_size)(const struct glsl_type *))
+             int (*type_size)(const struct glsl_type *,
+                              unsigned num_packed_components))
 {
    nir_foreach_function(function, shader) {
       if (function->impl)
diff --git a/src/mesa/drivers/dri/i965/brw_blorp.c b/src/mesa/drivers/dri/i965/brw_blorp.c
index 9590968..97ddfa9 100644
--- a/src/mesa/drivers/dri/i965/brw_blorp.c
+++ b/src/mesa/drivers/dri/i965/brw_blorp.c
@@ -157,8 +157,10 @@ brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key)
 }
 
 static int
-nir_uniform_type_size(const struct glsl_type *type)
+nir_uniform_type_size(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
+
    /* Only very basic types are allowed */
    assert(glsl_type_is_vector_or_scalar(type));
    assert(glsl_get_bit_size(type) == 32);
@@ -216,7 +218,7 @@ brw_blorp_compile_nir_shader(struct brw_context *brw, struct nir_shader *nir,
    nir->num_uniforms = 0;
    nir_foreach_variable(var, &nir->uniforms) {
       var->data.driver_location = var->data.location;
-      unsigned end = var->data.location + nir_uniform_type_size(var->type);
+      unsigned end = var->data.location + nir_uniform_type_size(var->type, 0);
       nir->num_uniforms = MAX2(nir->num_uniforms, end);
    }
    nir_lower_io(nir, nir_var_uniform, nir_uniform_type_size);
diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index c24813f..ddcf320 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -491,7 +491,8 @@ fs_reg::component_size(unsigned width) const
 }
 
 extern "C" int
-type_size_scalar(const struct glsl_type *type)
+type_size_scalar_packed(const struct glsl_type *type,
+                        unsigned num_packed_components)
 {
    unsigned int size, i;
 
@@ -500,8 +501,14 @@ type_size_scalar(const struct glsl_type *type)
    case GLSL_TYPE_INT:
    case GLSL_TYPE_FLOAT:
    case GLSL_TYPE_BOOL:
-      return type->components();
+      if (num_packed_components)
+         return num_packed_components;
+      else
+         return type->components();
    case GLSL_TYPE_DOUBLE:
+      if (num_packed_components)
+         return num_packed_components * 2;
+      else
       return type->components() * 2;
    case GLSL_TYPE_ARRAY:
       return type_size_scalar(type->fields.array) * type->length;
@@ -532,6 +539,12 @@ type_size_scalar(const struct glsl_type *type)
    return 0;
 }
 
+extern int
+type_size_scalar(const struct glsl_type *type)
+{
+   return type_size_scalar_packed(type, 0);
+}
+
 /**
  * Returns the number of scalar components needed to store type, assuming
  * that vectors are padded out to vec4.
@@ -540,8 +553,9 @@ type_size_scalar(const struct glsl_type *type)
  * similar to type_size_scalar().
  */
 extern "C" int
-type_size_vec4_times_4(const struct glsl_type *type)
+type_size_vec4_times_4(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
    return 4 * type_size_vec4(type);
 }
 
@@ -549,8 +563,9 @@ type_size_vec4_times_4(const struct glsl_type *type)
  * except for double-precision types, which are loaded as one dvec4.
  */
 extern "C" int
-type_size_vs_input(const struct glsl_type *type)
+type_size_vs_input(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
    if (type->is_double()) {
       return type_size_vec4(type) / 2;
    } else {
diff --git a/src/mesa/drivers/dri/i965/brw_nir.c b/src/mesa/drivers/dri/i965/brw_nir.c
index e60d398..257cb6e 100644
--- a/src/mesa/drivers/dri/i965/brw_nir.c
+++ b/src/mesa/drivers/dri/i965/brw_nir.c
@@ -239,7 +239,7 @@ brw_nir_lower_vue_inputs(nir_shader *nir, bool is_scalar,
    }
 
    /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
-   nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
+   nir_lower_io(nir, nir_var_shader_in, type_size_vec4_packed);
 
    if (is_scalar || nir->stage != MESA_SHADER_GEOMETRY) {
       /* This pass needs actual constants */
@@ -264,7 +264,7 @@ brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
       var->data.driver_location = var->data.location;
    }
 
-   nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
+   nir_lower_io(nir, nir_var_shader_in, type_size_vec4_packed);
 
    /* This pass needs actual constants */
    nir_opt_constant_folding(nir);
@@ -286,8 +286,8 @@ void
 brw_nir_lower_fs_inputs(nir_shader *nir)
 {
    nir_assign_var_locations(&nir->inputs, &nir->num_inputs, VARYING_SLOT_VAR0,
-                            type_size_scalar);
-   nir_lower_io(nir, nir_var_shader_in, type_size_scalar);
+                            type_size_scalar_packed);
+   nir_lower_io(nir, nir_var_shader_in, type_size_scalar_packed);
 }
 
 void
@@ -302,7 +302,7 @@ brw_nir_lower_vue_outputs(nir_shader *nir,
    } else {
       nir_foreach_variable(var, &nir->outputs)
          var->data.driver_location = var->data.location;
-      nir_lower_io(nir, nir_var_shader_out, type_size_vec4);
+      nir_lower_io(nir, nir_var_shader_out, type_size_vec4_packed);
    }
 }
 
@@ -313,7 +313,7 @@ brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map)
       var->data.driver_location = var->data.location;
    }
 
-   nir_lower_io(nir, nir_var_shader_out, type_size_vec4);
+   nir_lower_io(nir, nir_var_shader_out, type_size_vec4_packed);
 
    /* This pass needs actual constants */
    nir_opt_constant_folding(nir);
@@ -335,19 +335,21 @@ void
 brw_nir_lower_fs_outputs(nir_shader *nir)
 {
    nir_assign_var_locations(&nir->outputs, &nir->num_outputs,
-                            FRAG_RESULT_DATA0, type_size_scalar);
-   nir_lower_io(nir, nir_var_shader_out, type_size_scalar);
+                            FRAG_RESULT_DATA0, type_size_scalar_packed);
+   nir_lower_io(nir, nir_var_shader_out, type_size_scalar_packed);
 }
 
 static int
-type_size_scalar_bytes(const struct glsl_type *type)
+type_size_scalar_bytes(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
    return type_size_scalar(type) * 4;
 }
 
 static int
-type_size_vec4_bytes(const struct glsl_type *type)
+type_size_vec4_bytes(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
    return type_size_vec4(type) * 16;
 }
 
diff --git a/src/mesa/drivers/dri/i965/brw_shader.h b/src/mesa/drivers/dri/i965/brw_shader.h
index 60f3b5f..7e9defa 100644
--- a/src/mesa/drivers/dri/i965/brw_shader.h
+++ b/src/mesa/drivers/dri/i965/brw_shader.h
@@ -293,8 +293,10 @@ struct gl_shader *brw_new_shader(struct gl_context *ctx, GLuint name, GLuint typ
 
 int type_size_scalar(const struct glsl_type *type);
 int type_size_vec4(const struct glsl_type *type);
-int type_size_vec4_times_4(const struct glsl_type *type);
-int type_size_vs_input(const struct glsl_type *type);
+int type_size_vs_input(const struct glsl_type *type, unsigned x);
+int type_size_scalar_packed(const struct glsl_type *type, unsigned x);
+int type_size_vec4_packed(const struct glsl_type *type, unsigned x);
+int type_size_vec4_times_4(const struct glsl_type *type, unsigned x);
 
 unsigned tesslevel_outer_components(GLenum tes_primitive_mode);
 unsigned tesslevel_inner_components(GLenum tes_primitive_mode);
diff --git a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
index f73d678..bf0a567 100644
--- a/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
+++ b/src/mesa/drivers/dri/i965/brw_vec4_visitor.cpp
@@ -566,6 +566,13 @@ vec4_visitor::emit_pack_snorm_4x8(const dst_reg &dst, const src_reg &src0)
    emit(VEC4_OPCODE_PACK_BYTES, dst, bytes);
 }
 
+extern "C" int
+type_size_vec4_packed(const struct glsl_type *type, unsigned x)
+{
+   (void) x;
+   return type_size_vec4(type);
+}
+
 /**
  * Returns the minimum number of vec4 elements needed to pack a type.
  *
diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index 4fd8666..36eb9a7 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -188,7 +188,7 @@ st_nir_assign_uniform_locations(struct gl_program *prog,
 
       uniform->data.driver_location = loc;
 
-      max = MAX2(max, loc + st_glsl_type_size(uniform->type));
+      max = MAX2(max, loc + st_glsl_type_size(uniform->type, 0));
    }
    *size = max;
 }
diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
index aa443a5..6031910 100644
--- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
@@ -1173,7 +1173,7 @@ attrib_type_size(const struct glsl_type *type, bool is_vs_input)
 static int
 type_size(const struct glsl_type *type)
 {
-   return st_glsl_type_size(type);
+   return st_glsl_type_size(type, 0);
 }
 
 /**
diff --git a/src/mesa/state_tracker/st_glsl_types.cpp b/src/mesa/state_tracker/st_glsl_types.cpp
index 857e143..14ec5d8 100644
--- a/src/mesa/state_tracker/st_glsl_types.cpp
+++ b/src/mesa/state_tracker/st_glsl_types.cpp
@@ -95,7 +95,8 @@ st_glsl_attrib_type_size(const struct glsl_type *type, bool is_vs_input)
 }
 
 int
-st_glsl_type_size(const struct glsl_type *type)
+st_glsl_type_size(const struct glsl_type *type, unsigned x)
 {
+   (void) x;
    return st_glsl_attrib_type_size(type, false);
 }
diff --git a/src/mesa/state_tracker/st_glsl_types.h b/src/mesa/state_tracker/st_glsl_types.h
index 3a39cee..86d155a 100644
--- a/src/mesa/state_tracker/st_glsl_types.h
+++ b/src/mesa/state_tracker/st_glsl_types.h
@@ -34,7 +34,7 @@ extern "C" {
 #endif
 
 int st_glsl_attrib_type_size(const struct glsl_type *type, bool is_vs_input);
-int st_glsl_type_size(const struct glsl_type *type);
+int st_glsl_type_size(const struct glsl_type *type, unsigned x);
 
 
 #ifdef __cplusplus
-- 
2.5.5



More information about the mesa-dev mailing list