prog/nir: Simplify some load/store operations
authorJason Ekstrand <jason.ekstrand@intel.com>
Mon, 19 Mar 2018 20:43:35 +0000 (13:43 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 5 Apr 2018 20:20:39 +0000 (13:20 -0700)
Reviewed-by: Eric Anholt <eric@anholt.net>
src/compiler/nir/nir_builder.h
src/mesa/program/prog_to_nir.c

index 8f7ddf1483cca960538a9660698aab8d3b3fe6b6..d699b5ee1bf1c91d60a15ec559c9da0e24647f3c 100644 (file)
@@ -525,6 +525,12 @@ nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
    return nir_imov_alu(build, *src, num_components);
 }
 
+static inline nir_ssa_def *
+nir_load_reg(nir_builder *build, nir_register *reg)
+{
+   return nir_ssa_for_src(build, nir_src_for_reg(reg), reg->num_components);
+}
+
 static inline nir_ssa_def *
 nir_load_var(nir_builder *build, nir_variable *var)
 {
index b49616c0cf914fa9825d38c95e671ebb7534d1e9..3ee44ce489eb89eb3120e1eecdc29a71a65b9103 100644 (file)
@@ -136,15 +136,8 @@ ptn_get_src(struct ptn_compile *c, const struct prog_src_register *prog_src)
 
       assert(prog_src->Index >= 0 && prog_src->Index < VARYING_SLOT_MAX);
 
-      nir_intrinsic_instr *load =
-         nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
-      load->num_components = 4;
-      load->variables[0] = nir_deref_var_create(load, c->input_vars[prog_src->Index]);
-
-      nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
-      nir_builder_instr_insert(b, &load->instr);
-
-      src.src = nir_src_for_ssa(&load->dest.ssa);
+      nir_variable *var = c->input_vars[prog_src->Index];
+      src.src = nir_src_for_ssa(nir_load_var(b, var));
       break;
    }
    case PROGRAM_STATE_VAR:
@@ -861,27 +854,17 @@ ptn_add_output_stores(struct ptn_compile *c)
    nir_builder *b = &c->build;
 
    nir_foreach_variable(var, &b->shader->outputs) {
-      nir_intrinsic_instr *store =
-         nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
-      store->num_components = glsl_get_vector_elements(var->type);
-      nir_intrinsic_set_write_mask(store, (1 << store->num_components) - 1);
-      store->variables[0] =
-         nir_deref_var_create(store, c->output_vars[var->data.location]);
-
+      nir_ssa_def *src = nir_load_reg(b, c->output_regs[var->data.location]);
       if (c->prog->Target == GL_FRAGMENT_PROGRAM_ARB &&
           var->data.location == FRAG_RESULT_DEPTH) {
          /* result.depth has this strange convention of being the .z component of
           * a vec4 with undefined .xyw components.  We resolve it to a scalar, to
           * match GLSL's gl_FragDepth and the expectations of most backends.
           */
-         nir_alu_src alu_src = { NIR_SRC_INIT };
-         alu_src.src = nir_src_for_reg(c->output_regs[FRAG_RESULT_DEPTH]);
-         alu_src.swizzle[0] = SWIZZLE_Z;
-         store->src[0] = nir_src_for_ssa(nir_fmov_alu(b, alu_src, 1));
-      } else {
-         store->src[0].reg.reg = c->output_regs[var->data.location];
+         src = nir_channel(b, src, 2);
       }
-      nir_builder_instr_insert(b, &store->instr);
+      unsigned num_components = glsl_get_vector_elements(var->type);
+      nir_store_var(b, var, src, (1 << num_components) - 1);
    }
 }
 
@@ -914,26 +897,16 @@ setup_registers_and_variables(struct ptn_compile *c)
              */
             var->type = glsl_float_type();
 
-            nir_intrinsic_instr *load_x =
-               nir_intrinsic_instr_create(shader, nir_intrinsic_load_var);
-            load_x->num_components = 1;
-            load_x->variables[0] = nir_deref_var_create(load_x, var);
-            nir_ssa_dest_init(&load_x->instr, &load_x->dest, 1, 32, NULL);
-            nir_builder_instr_insert(b, &load_x->instr);
-
-            nir_ssa_def *f001 = nir_vec4(b, &load_x->dest.ssa, nir_imm_float(b, 0.0),
-                                         nir_imm_float(b, 0.0), nir_imm_float(b, 1.0));
-
             nir_variable *fullvar =
                nir_local_variable_create(b->impl, glsl_vec4_type(),
                                          "fogcoord_tmp");
-            nir_intrinsic_instr *store =
-               nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
-            store->num_components = 4;
-            nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
-            store->variables[0] = nir_deref_var_create(store, fullvar);
-            store->src[0] = nir_src_for_ssa(f001);
-            nir_builder_instr_insert(b, &store->instr);
+
+            nir_store_var(b, fullvar,
+                          nir_vec4(b, nir_load_var(b, var),
+                                   nir_imm_float(b, 0.0),
+                                   nir_imm_float(b, 0.0),
+                                   nir_imm_float(b, 1.0)),
+                          WRITEMASK_XYZW);
 
             /* We inserted the real input into the list so the driver has real
              * inputs, but we set c->input_vars[i] to the temporary so we use