glsl: use nir linker to link atomics
[mesa.git] / src / mesa / state_tracker / st_glsl_to_nir.cpp
index 0aad8a723e7b19e5fec48ac38b41b26d4aea4674..c92b1c365a7707aba1e78c9d07dc3d35e7046cd6 100644 (file)
@@ -31,6 +31,7 @@
 #include "program/prog_statevars.h"
 #include "program/prog_parameter.h"
 #include "program/ir_to_mesa.h"
+#include "main/context.h"
 #include "main/mtypes.h"
 #include "main/errors.h"
 #include "main/glspirv.h"
@@ -368,9 +369,16 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
       st->ctx->SoftFP64 = glsl_float64_funcs_to_nir(st->ctx, options);
    }
 
-   nir_variable_mode mask =
-      (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
-   nir_remove_dead_variables(nir, mask);
+   /* ES has strict SSO validation rules for shader IO matching so we can't
+    * remove dead IO until the resource list has been built. Here we skip
+    * removing them until later. This will potentially make the IO lowering
+    * calls below do a little extra work but should otherwise have no impact.
+    */
+   if (!_mesa_is_gles(st->ctx) || !nir->info.separate_shader) {
+      nir_variable_mode mask =
+         (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
+      nir_remove_dead_variables(nir, mask);
+   }
 
    if (options->lower_all_io_to_temps ||
        nir->info.stage == MESA_SHADER_VERTEX ||
@@ -405,7 +413,6 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
                  nir_var_mem_shared, nir_address_format_32bit_offset);
    }
 
-   NIR_PASS_V(nir, gl_nir_lower_buffers, shader_program);
    /* Do a round of constant folding to clean up address calculations */
    NIR_PASS_V(nir, nir_opt_constant_folding);
 }
@@ -497,8 +504,9 @@ st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
       (nir_var_shader_in | nir_var_shader_out | nir_var_function_temp );
    nir_remove_dead_variables(nir, mask);
 
-   NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
-              st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
+   if (!st->has_hw_atomics)
+      NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
+                 st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
 
    st_finalize_nir_before_variants(nir);
 
@@ -515,36 +523,6 @@ st_glsl_to_nir_post_opts(struct st_context *st, struct gl_program *prog,
    }
 }
 
-static void
-set_st_program(struct gl_program *prog,
-               struct gl_shader_program *shader_program,
-               nir_shader *nir)
-{
-   struct st_program *stvp;
-   struct st_program *stp;
-
-   switch (prog->info.stage) {
-   case MESA_SHADER_VERTEX:
-      stvp = (struct st_program *)prog;
-      stvp->shader_program = shader_program;
-      stvp->state.type = PIPE_SHADER_IR_NIR;
-      stvp->state.ir.nir = nir;
-      break;
-   case MESA_SHADER_GEOMETRY:
-   case MESA_SHADER_TESS_CTRL:
-   case MESA_SHADER_TESS_EVAL:
-   case MESA_SHADER_COMPUTE:
-   case MESA_SHADER_FRAGMENT:
-      stp = (struct st_program *)prog;
-      stp->shader_program = shader_program;
-      stp->state.type = PIPE_SHADER_IR_NIR;
-      stp->state.ir.nir = nir;
-      break;
-   default:
-      unreachable("unknown shader stage");
-   }
-}
-
 static void
 st_nir_vectorize_io(nir_shader *producer, nir_shader *consumer)
 {
@@ -568,37 +546,37 @@ st_nir_vectorize_io(nir_shader *producer, nir_shader *consumer)
 }
 
 static void
-st_nir_link_shaders(nir_shader **producer, nir_shader **consumer)
+st_nir_link_shaders(nir_shader *producer, nir_shader *consumer)
 {
-   if ((*producer)->options->lower_to_scalar) {
-      NIR_PASS_V(*producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
-      NIR_PASS_V(*consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
+   if (producer->options->lower_to_scalar) {
+      NIR_PASS_V(producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
+      NIR_PASS_V(consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
    }
 
-   nir_lower_io_arrays_to_elements(*producer, *consumer);
+   nir_lower_io_arrays_to_elements(producer, consumer);
 
-   st_nir_opts(*producer);
-   st_nir_opts(*consumer);
+   st_nir_opts(producer);
+   st_nir_opts(consumer);
 
-   if (nir_link_opt_varyings(*producer, *consumer))
-      st_nir_opts(*consumer);
+   if (nir_link_opt_varyings(producer, consumer))
+      st_nir_opts(consumer);
 
-   NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
-   NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
+   NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
+   NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
 
-   if (nir_remove_unused_varyings(*producer, *consumer)) {
-      NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
-      NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
+   if (nir_remove_unused_varyings(producer, consumer)) {
+      NIR_PASS_V(producer, nir_lower_global_vars_to_local);
+      NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
 
-      st_nir_opts(*producer);
-      st_nir_opts(*consumer);
+      st_nir_opts(producer);
+      st_nir_opts(consumer);
 
       /* Optimizations can cause varyings to become unused.
        * nir_compact_varyings() depends on all dead varyings being removed so
        * we need to call nir_remove_dead_variables() again here.
        */
-      NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
-      NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
+      NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
+      NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
    }
 }
 
@@ -664,29 +642,32 @@ st_link_nir(struct gl_context *ctx,
             struct gl_shader_program *shader_program)
 {
    struct st_context *st = st_context(ctx);
-   unsigned num_linked_shaders = 0;
+   struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
+   unsigned num_shaders = 0;
 
-   unsigned last_stage = 0;
    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
-      if (shader == NULL)
-         continue;
-
-      num_linked_shaders++;
+      if (shader_program->_LinkedShaders[i])
+         linked_shader[num_shaders++] = shader_program->_LinkedShaders[i];
+   }
 
+   for (unsigned i = 0; i < num_shaders; i++) {
+      struct gl_linked_shader *shader = linked_shader[i];
       const nir_shader_compiler_options *options =
          st->ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions;
       struct gl_program *prog = shader->Program;
+      struct st_program *stp = (struct st_program *)prog;
+
       _mesa_copy_linked_program_data(shader_program, shader);
 
       assert(!prog->nir);
+      stp->shader_program = shader_program;
+      stp->state.type = PIPE_SHADER_IR_NIR;
 
       if (shader_program->data->spirv) {
          prog->Parameters = _mesa_new_parameter_list();
          /* Parameters will be filled during NIR linking. */
 
          prog->nir = _mesa_spirv_to_nir(ctx, shader_program, shader->Stage, options);
-         set_st_program(prog, shader_program, prog->nir);
       } else {
          validate_ir_tree(shader->ir);
 
@@ -707,17 +688,16 @@ st_link_nir(struct gl_context *ctx,
          _mesa_update_shader_textures_used(shader_program, prog);
 
          prog->nir = glsl_to_nir(st->ctx, shader_program, shader->Stage, options);
-         set_st_program(prog, shader_program, prog->nir);
          st_nir_preprocess(st, prog, shader_program, shader->Stage);
       }
 
-      last_stage = i;
-
       if (options->lower_to_scalar) {
          NIR_PASS_V(shader->Program->nir, nir_lower_load_const_to_scalar);
       }
    }
 
+   st_lower_patch_vertices_in(shader_program);
+
    /* For SPIR-V, we have to perform the NIR linking before applying
     * st_nir_preprocess.
     */
@@ -725,20 +705,17 @@ st_link_nir(struct gl_context *ctx,
       static const gl_nir_linker_options opts = {
          true /*fill_parameters */
       };
-      if (!gl_nir_link(ctx, shader_program, &opts))
+      if (!gl_nir_link_spirv(ctx, shader_program, &opts))
          return GL_FALSE;
 
-      nir_build_program_resource_list(ctx, shader_program);
-
-      for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-         struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
-         if (shader == NULL)
-            continue;
+      nir_build_program_resource_list(ctx, shader_program, true);
 
+      for (unsigned i = 0; i < num_shaders; i++) {
+         struct gl_linked_shader *shader = linked_shader[i];
          struct gl_program *prog = shader->Program;
+
          prog->ExternalSamplersUsed = gl_external_samplers(prog);
          _mesa_update_shader_textures_used(shader_program, prog);
-
          st_nir_preprocess(st, prog, shader_program, shader->Stage);
       }
    }
@@ -748,30 +725,40 @@ st_link_nir(struct gl_context *ctx,
     * are eliminated if they are (transitively) not used in a later
     * stage.
     */
-   int next = last_stage;
-   for (int i = next - 1; i >= 0; i--) {
-      struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
-      if (shader == NULL)
-         continue;
-
-      st_nir_link_shaders(&shader->Program->nir,
-                          &shader_program->_LinkedShaders[next]->Program->nir);
-      next = i;
+   for (int i = num_shaders - 2; i >= 0; i--) {
+      st_nir_link_shaders(linked_shader[i]->Program->nir,
+                          linked_shader[i + 1]->Program->nir);
    }
+   /* Linking shaders also optimizes them. Separate shaders, compute shaders
+    * and shaders with a fixed-func VS or FS that don't need linking are
+    * optimized here.
+    */
+   if (num_shaders == 1)
+      st_nir_opts(linked_shader[0]->Program->nir);
 
-   int prev = -1;
-   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
-      if (shader == NULL)
-         continue;
+   if (!shader_program->data->spirv) {
+      if (!gl_nir_link_glsl(ctx, shader_program))
+         return GL_FALSE;
+
+      nir_build_program_resource_list(ctx, shader_program, false);
+   }
 
+   for (unsigned i = 0; i < num_shaders; i++) {
+      struct gl_linked_shader *shader = linked_shader[i];
       nir_shader *nir = shader->Program->nir;
 
-      /* Linked shaders are optimized in st_nir_link_shaders. Separate shaders
-       * and shaders with a fixed-func VS or FS are optimized here.
+      /* This needs to run after the initial pass of nir_lower_vars_to_ssa, so
+       * that the buffer indices are constants in nir where they where
+       * constants in GLSL. */
+      NIR_PASS_V(nir, gl_nir_lower_buffers, shader_program);
+
+      /* Remap the locations to slots so those requiring two slots will occupy
+       * two locations. For instance, if we have in the IR code a dvec3 attr0 in
+       * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
+       * locations/slots 0 and 1, and attr1 will use location/slot 2
        */
-      if (num_linked_shaders == 1)
-         st_nir_opts(nir);
+      if (nir->info.stage == MESA_SHADER_VERTEX && !shader_program->data->spirv)
+         nir_remap_dual_slot_attributes(nir, &shader->Program->DualSlotInputs);
 
       NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, shader->Program,
                  st->pipe->screen);
@@ -781,7 +768,7 @@ st_link_nir(struct gl_context *ctx,
 
       nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
       shader->Program->info = nir->info;
-      if (i == MESA_SHADER_VERTEX) {
+      if (shader->Stage == MESA_SHADER_VERTEX) {
          /* NIR expands dual-slot inputs out to two locations.  We need to
           * compact things back down GL-style single-slot inputs to avoid
           * confusing the state tracker.
@@ -791,9 +778,8 @@ st_link_nir(struct gl_context *ctx,
                                              shader->Program->DualSlotInputs);
       }
 
-      if (prev != -1) {
-         struct gl_program *prev_shader =
-            shader_program->_LinkedShaders[prev]->Program;
+      if (i >= 1) {
+         struct gl_program *prev_shader = linked_shader[i - 1]->Program;
 
          /* We can't use nir_compact_varyings with transform feedback, since
           * the pipe_stream_output->output_register field is based on the
@@ -801,51 +787,62 @@ st_link_nir(struct gl_context *ctx,
           */
          if (!(prev_shader->sh.LinkedTransformFeedback &&
                prev_shader->sh.LinkedTransformFeedback->NumVarying > 0))
-            nir_compact_varyings(shader_program->_LinkedShaders[prev]->Program->nir,
-                              nir, ctx->API != API_OPENGL_COMPAT);
+            nir_compact_varyings(prev_shader->nir,
+                                 nir, ctx->API != API_OPENGL_COMPAT);
 
-         if (ctx->Const.ShaderCompilerOptions[i].NirOptions->vectorize_io)
+         if (ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions->vectorize_io)
             st_nir_vectorize_io(prev_shader->nir, nir);
       }
-      prev = i;
    }
 
-   st_lower_patch_vertices_in(shader_program);
-
-   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
-      if (shader == NULL)
-         continue;
-
+   for (unsigned i = 0; i < num_shaders; i++) {
+      struct gl_linked_shader *shader = linked_shader[i];
       struct gl_program *prog = shader->Program;
+      struct st_program *stp = st_program(prog);
       st_glsl_to_nir_post_opts(st, prog, shader_program);
 
       /* Initialize st_vertex_program members. */
-      if (i == MESA_SHADER_VERTEX)
-         st_prepare_vertex_program(st_program(prog));
+      if (shader->Stage == MESA_SHADER_VERTEX)
+         st_prepare_vertex_program(stp);
 
       /* Get pipe_stream_output_info. */
-      if (i == MESA_SHADER_VERTEX ||
-          i == MESA_SHADER_TESS_EVAL ||
-          i == MESA_SHADER_GEOMETRY)
+      if (shader->Stage == MESA_SHADER_VERTEX ||
+          shader->Stage == MESA_SHADER_TESS_EVAL ||
+          shader->Stage == MESA_SHADER_GEOMETRY)
          st_translate_stream_output_info(prog);
 
       st_store_ir_in_disk_cache(st, prog, true);
 
-      if (!ctx->Driver.ProgramStringNotify(ctx,
-                                           _mesa_shader_stage_to_program(i),
-                                           prog)) {
-         _mesa_reference_program(ctx, &shader->Program, NULL);
-         return false;
-      }
-
-      nir_sweep(prog->nir);
+      st_release_variants(st, stp);
+      st_finalize_program(st, prog);
 
       /* The GLSL IR won't be needed anymore. */
       ralloc_free(shader->ir);
       shader->ir = NULL;
    }
 
+   struct shader_info *prev_info = NULL;
+
+   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+      struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
+      if (!shader)
+         continue;
+
+      struct shader_info *info = &shader->Program->nir->info;
+
+      if (prev_info &&
+          ctx->Const.ShaderCompilerOptions[i].NirOptions->unify_interfaces) {
+         prev_info->outputs_written |= info->inputs_read &
+            ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
+         info->inputs_read |= prev_info->outputs_written &
+            ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
+
+         prev_info->patch_outputs_written |= info->patch_inputs_read;
+         info->patch_inputs_read |= prev_info->patch_outputs_written;
+      }
+      prev_info = info;
+   }
+
    return true;
 }