[Mesa-dev] [PATCH 1/2] st/glsl_to_nir: generate NIR earlier

Timothy Arceri tarceri at itsqueeze.com
Wed Nov 1 11:01:37 UTC 2017


We want to use nir_shader_gather_info() the GLSL IR version might
be including varyings that NIR later eliminates. To do this we
need to generate NIR before we we start using the in/out bitmasks.
---
 src/mesa/state_tracker/st_glsl_to_nir.cpp |  6 +++++
 src/mesa/state_tracker/st_program.c       | 45 ++++++-------------------------
 2 files changed, 14 insertions(+), 37 deletions(-)

diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index bbef830a2e..d59e472584 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -400,20 +400,21 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
       NIR_PASS_V(nir, nir_lower_samplers_as_deref, shader_program);
    else
       NIR_PASS_V(nir, nir_lower_samplers, shader_program);
 }
 
 struct gl_program *
 st_nir_get_mesa_program(struct gl_context *ctx,
                         struct gl_shader_program *shader_program,
                         struct gl_linked_shader *shader)
 {
+   struct st_context *st = st_context(ctx);
    struct gl_program *prog;
 
    validate_ir_tree(shader->ir);
 
    prog = shader->Program;
 
    prog->Parameters = _mesa_new_parameter_list();
 
    do_set_program_inouts(shader->ir, prog, shader->Stage);
 
@@ -455,14 +456,19 @@ st_nir_get_mesa_program(struct gl_context *ctx,
       break;
    case MESA_SHADER_COMPUTE:
       stcp = (struct st_compute_program *)prog;
       stcp->shader_program = shader_program;
       break;
    default:
       assert(!"should not be reached");
       return NULL;
    }
 
+   struct st_common_program *st_comm_prog = (struct st_common_program *)prog;
+   nir_shader *nir = st_glsl_to_nir(st, prog, shader_program, shader->Stage);
+   st_comm_prog->tgsi.type = PIPE_SHADER_IR_NIR;
+   st_comm_prog->tgsi.ir.nir = nir;
+
    return prog;
 }
 
 } /* extern "C" */
diff --git a/src/mesa/state_tracker/st_program.c b/src/mesa/state_tracker/st_program.c
index 25a849bb18..e3649a8b7c 100644
--- a/src/mesa/state_tracker/st_program.c
+++ b/src/mesa/state_tracker/st_program.c
@@ -440,26 +440,20 @@ st_translate_vertex_program(struct st_context *st,
                               ST_NEW_RASTERIZER |
                               ST_NEW_VERTEX_ARRAYS;
 
       if (stvp->Base.Parameters->NumParameters)
          stvp->affected_states |= ST_NEW_VS_CONSTANTS;
 
       /* No samplers are allowed in ARB_vp. */
    }
 
    if (stvp->shader_program) {
-      nir_shader *nir = st_glsl_to_nir(st, &stvp->Base, stvp->shader_program,
-                                       MESA_SHADER_VERTEX);
-
-      stvp->tgsi.type = PIPE_SHADER_IR_NIR;
-      stvp->tgsi.ir.nir = nir;
-
       struct gl_program *prog = stvp->shader_program->last_vert_prog;
       if (prog) {
          st_translate_stream_output_info2(prog->sh.LinkedTransformFeedback,
                                           stvp->result_to_output,
                                           &stvp->tgsi.stream_output);
       }
 
       return true;
    }
 
@@ -888,29 +882,23 @@ st_translate_fragment_program(struct st_context *st,
             fs_output_semantic_index[fs_num_outputs] = index;
             outputMapping[attr] = fs_num_outputs;
             break;
          }
          }
 
          fs_num_outputs++;
       }
    }
 
-   if (stfp->shader_program) {
-      nir_shader *nir = st_glsl_to_nir(st, &stfp->Base, stfp->shader_program,
-                                       MESA_SHADER_FRAGMENT);
-
-      stfp->tgsi.type = PIPE_SHADER_IR_NIR;
-      stfp->tgsi.ir.nir = nir;
-
+   /* We have already compiler to NIR so just return */
+   if (stfp->shader_program)
       return true;
-   }
 
    ureg = ureg_create_with_screen(PIPE_SHADER_FRAGMENT, st->pipe->screen);
    if (ureg == NULL)
       return false;
 
    if (ST_DEBUG & DEBUG_MESA) {
       _mesa_print_program(&stfp->Base);
       _mesa_print_program_parameters(st->ctx, &stfp->Base);
       debug_printf("\n");
    }
@@ -1674,29 +1662,23 @@ st_get_basic_variant(struct st_context *st,
 
 /**
  * Translate a tessellation control program to create a new variant.
  */
 bool
 st_translate_tessctrl_program(struct st_context *st,
                               struct st_common_program *sttcp)
 {
    struct ureg_program *ureg;
 
-   if (sttcp->shader_program) {
-      nir_shader *nir = st_glsl_to_nir(st, &sttcp->Base, sttcp->shader_program,
-                                       MESA_SHADER_TESS_EVAL);
-
-      sttcp->tgsi.type = PIPE_SHADER_IR_NIR;
-      sttcp->tgsi.ir.nir = nir;
-
+   /* We have already compiler to NIR so just return */
+   if (sttcp->shader_program)
       return true;
-   }
 
    ureg = ureg_create_with_screen(PIPE_SHADER_TESS_CTRL, st->pipe->screen);
    if (ureg == NULL)
       return false;
 
    ureg_property(ureg, TGSI_PROPERTY_TCS_VERTICES_OUT,
                  sttcp->Base.info.tess.tcs_vertices_out);
 
    st_translate_program_common(st, &sttcp->Base, sttcp->glsl_to_tgsi, ureg,
                                PIPE_SHADER_TESS_CTRL, &sttcp->tgsi);
@@ -1709,29 +1691,23 @@ st_translate_tessctrl_program(struct st_context *st,
 
 /**
  * Translate a tessellation evaluation program to create a new variant.
  */
 bool
 st_translate_tesseval_program(struct st_context *st,
                               struct st_common_program *sttep)
 {
    struct ureg_program *ureg;
 
-   if (sttep->shader_program) {
-      nir_shader *nir = st_glsl_to_nir(st, &sttep->Base, sttep->shader_program,
-                                       MESA_SHADER_TESS_EVAL);
-
-      sttep->tgsi.type = PIPE_SHADER_IR_NIR;
-      sttep->tgsi.ir.nir = nir;
-
+   /* We have already compiler to NIR so just return */
+   if (sttep->shader_program)
       return true;
-   }
 
    ureg = ureg_create_with_screen(PIPE_SHADER_TESS_EVAL, st->pipe->screen);
    if (ureg == NULL)
       return false;
 
    if (sttep->Base.info.tess.primitive_mode == GL_ISOLINES)
       ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE, GL_LINES);
    else
       ureg_property(ureg, TGSI_PROPERTY_TES_PRIM_MODE,
                     sttep->Base.info.tess.primitive_mode);
@@ -1763,28 +1739,23 @@ st_translate_tesseval_program(struct st_context *st,
  * Translate a compute program to create a new variant.
  */
 bool
 st_translate_compute_program(struct st_context *st,
                              struct st_compute_program *stcp)
 {
    struct ureg_program *ureg;
    struct pipe_shader_state prog;
 
    if (stcp->shader_program) {
-      nir_shader *nir = st_glsl_to_nir(st, &stcp->Base, stcp->shader_program,
-                                       MESA_SHADER_COMPUTE);
-
       /* no compute variants: */
-      st_finalize_nir(st, &stcp->Base, stcp->shader_program, nir);
-
-      stcp->tgsi.ir_type = PIPE_SHADER_IR_NIR;
-      stcp->tgsi.prog = nir;
+      st_finalize_nir(st, &stcp->Base, stcp->shader_program,
+                      (struct nir_shader *) stcp->tgsi.prog);
 
       return true;
    }
 
    ureg = ureg_create_with_screen(PIPE_SHADER_COMPUTE, st->pipe->screen);
    if (ureg == NULL)
       return false;
 
    st_translate_program_common(st, &stcp->Base, stcp->glsl_to_tgsi, ureg,
                                PIPE_SHADER_COMPUTE, &prog);
-- 
2.14.3



More information about the mesa-dev mailing list