[Mesa-dev] [PATCH 037/133] nir: Add NIR_TRUE and NIR_FALSE constants and use them for boolean immediates

Jason Ekstrand jason at jlekstrand.net
Mon Dec 15 22:04:47 PST 2014


---
 src/glsl/nir/nir.h                        |  3 +++
 src/glsl/nir/nir_lower_variables_scalar.c | 20 ++++++++++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index c5500f7..df7f0b6 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -40,6 +40,9 @@
 extern "C" {
 #endif
 
+#define NIR_FALSE 0u
+#define NIR_TRUE (~0u)
+
 struct nir_function_overload;
 struct nir_function;
 
diff --git a/src/glsl/nir/nir_lower_variables_scalar.c b/src/glsl/nir/nir_lower_variables_scalar.c
index 701b656..52e1ce6 100644
--- a/src/glsl/nir/nir_lower_variables_scalar.c
+++ b/src/glsl/nir/nir_lower_variables_scalar.c
@@ -358,13 +358,24 @@ get_deref_tail(nir_deref *deref)
 /* helper for reg_const_load which emits a single instruction */
 static void
 reg_const_load_single_instr(nir_reg_dest reg, nir_constant *constant,
+                            enum glsl_base_type base_type,
                             unsigned num_components, unsigned offset,
                             nir_function_impl *impl, void *mem_ctx)
 {
    nir_load_const_instr *instr = nir_load_const_instr_create(mem_ctx);
    instr->num_components = num_components;
    for (unsigned i = 0; i < num_components; i++) {
-      instr->value.u[i] = constant->value.u[i + offset];
+      switch (base_type) {
+      case GLSL_TYPE_FLOAT:
+      case GLSL_TYPE_INT:
+      case GLSL_TYPE_UINT:
+         instr->value.u[i] = constant->value.u[i + offset];
+         break;
+      case GLSL_TYPE_BOOL:
+         instr->value.u[i] = constant->value.u[i + offset] ?
+                             NIR_TRUE : NIR_FALSE;
+         break;
+      }
    }
    instr->dest.reg = reg;
    instr->dest.reg.base_offset += offset;
@@ -382,20 +393,21 @@ reg_const_load(nir_reg_dest reg, nir_constant *constant,
    const struct glsl_type *subtype;
    unsigned subtype_size;
 
-   switch (glsl_get_base_type(type)) {
+   enum glsl_base_type base_type = glsl_get_base_type(type);
+   switch (base_type) {
       case GLSL_TYPE_FLOAT:
       case GLSL_TYPE_INT:
       case GLSL_TYPE_UINT:
       case GLSL_TYPE_BOOL:
          if (glsl_type_is_matrix(type)) {
             for (unsigned i = 0; i < glsl_get_matrix_columns(type); i++) {
-               reg_const_load_single_instr(reg, constant,
+               reg_const_load_single_instr(reg, constant, base_type,
                                            glsl_get_vector_elements(type),
                                            i * glsl_get_vector_elements(type),
                                            impl, mem_ctx);
             }
          } else {
-            reg_const_load_single_instr(reg, constant,
+            reg_const_load_single_instr(reg, constant, base_type,
                                         glsl_get_vector_elements(type), 0,
                                         impl, mem_ctx);
          }
-- 
2.2.0



More information about the mesa-dev mailing list