st/glsl_to_nir: delay adding built-in uniforms to Parameters list
authorTimothy Arceri <tarceri@itsqueeze.com>
Wed, 1 Nov 2017 03:15:22 +0000 (14:15 +1100)
committerTimothy Arceri <tarceri@itsqueeze.com>
Wed, 8 Nov 2017 06:33:14 +0000 (17:33 +1100)
Delaying adding built-in uniforms until after we convert to NIR
gives us a better chance to optimise them away. Also NIR allows
us to iterate over the uniforms directly so should be faster.

Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
src/mesa/state_tracker/st_glsl_to_nir.cpp
src/mesa/state_tracker/st_glsl_to_tgsi.cpp

index 5b37d2cd6377aeddd3a54e95d32ffc5bbdaf0cc2..bbef830a2e652411d251c0583740c025de95612d 100644 (file)
@@ -242,6 +242,39 @@ st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
 
    nir = glsl_to_nir(shader_program, stage, options);
 
+   /* Make a pass over the IR to add state references for any built-in
+    * uniforms that are used.  This has to be done now (during linking).
+    * Code generation doesn't happen until the first time this shader is
+    * used for rendering.  Waiting until then to generate the parameters is
+    * too late.  At that point, the values for the built-in uniforms won't
+    * get sent to the shader.
+    */
+   nir_foreach_variable(var, &nir->uniforms) {
+      if (strncmp(var->name, "gl_", 3) == 0) {
+         const nir_state_slot *const slots = var->state_slots;
+         assert(var->state_slots != NULL);
+
+         for (unsigned int i = 0; i < var->num_state_slots; i++) {
+            _mesa_add_state_reference(prog->Parameters,
+                                      (gl_state_index *)slots[i].tokens);
+         }
+      }
+   }
+
+   /* Avoid reallocation of the program parameter list, because the uniform
+    * storage is only associated with the original parameter list.
+    * This should be enough for Bitmap and DrawPixels constants.
+    */
+   _mesa_reserve_parameter_storage(prog->Parameters, 8);
+
+   /* This has to be done last.  Any operation the can cause
+    * prog->ParameterValues to get reallocated (e.g., anything that adds a
+    * program constant) has to happen before creating this linkage.
+    */
+   _mesa_associate_uniform_storage(st->ctx, shader_program, prog, true);
+
+   st_set_prog_affected_state_flags(prog);
+
    NIR_PASS_V(nir, nir_lower_io_to_temporaries,
          nir_shader_get_entrypoint(nir),
          true, true);
@@ -388,29 +421,6 @@ st_nir_get_mesa_program(struct gl_context *ctx,
    _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
                                                prog->Parameters);
 
-   /* Make a pass over the IR to add state references for any built-in
-    * uniforms that are used.  This has to be done now (during linking).
-    * Code generation doesn't happen until the first time this shader is
-    * used for rendering.  Waiting until then to generate the parameters is
-    * too late.  At that point, the values for the built-in uniforms won't
-    * get sent to the shader.
-    */
-   foreach_in_list(ir_instruction, node, shader->ir) {
-      ir_variable *var = node->as_variable();
-
-      if ((var == NULL) || (var->data.mode != ir_var_uniform) ||
-          (strncmp(var->name, "gl_", 3) != 0))
-         continue;
-
-      const ir_state_slot *const slots = var->get_state_slots();
-      assert(slots != NULL);
-
-      for (unsigned int i = 0; i < var->get_num_state_slots(); i++) {
-         _mesa_add_state_reference(prog->Parameters,
-                                   (gl_state_index *) slots[i].tokens);
-      }
-   }
-
    if (ctx->_Shader->Flags & GLSL_DUMP) {
       _mesa_log("\n");
       _mesa_log("GLSL IR for linked %s program %d:\n",
@@ -423,18 +433,6 @@ st_nir_get_mesa_program(struct gl_context *ctx,
    prog->ExternalSamplersUsed = gl_external_samplers(prog);
    _mesa_update_shader_textures_used(shader_program, prog);
 
-   /* Avoid reallocation of the program parameter list, because the uniform
-    * storage is only associated with the original parameter list.
-    * This should be enough for Bitmap and DrawPixels constants.
-    */
-   _mesa_reserve_parameter_storage(prog->Parameters, 8);
-
-   /* This has to be done last.  Any operation the can cause
-    * prog->ParameterValues to get reallocated (e.g., anything that adds a
-    * program constant) has to happen before creating this linkage.
-    */
-   _mesa_associate_uniform_storage(ctx, shader_program, prog, true);
-
    struct st_vertex_program *stvp;
    struct st_common_program *stp;
    struct st_fragment_program *stfp;
index 54e1961830cedd61054f366b57f93f9506534086..fd9df61f4f79fd1aafef7f7a198c33020b3665aa 100644 (file)
@@ -6956,10 +6956,10 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
          linked_prog = st_nir_get_mesa_program(ctx, prog, shader);
       } else {
          linked_prog = get_mesa_program_tgsi(ctx, prog, shader);
+         st_set_prog_affected_state_flags(linked_prog);
       }
 
       if (linked_prog) {
-         st_set_prog_affected_state_flags(linked_prog);
          if (!ctx->Driver.ProgramStringNotify(ctx,
                                               _mesa_shader_stage_to_program(i),
                                               linked_prog)) {