replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / mesa / state_tracker / st_glsl_to_nir.cpp
index c252babb7115776462bf68208c6e5d075232973a..cf27da7e0c7f82ebd58b9baf3abe4933f80cf2f2 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"
@@ -39,7 +40,6 @@
 
 #include "main/shaderobj.h"
 #include "st_context.h"
-#include "st_glsl_types.h"
 #include "st_program.h"
 #include "st_shader_cache.h"
 
@@ -368,9 +368,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 ||
@@ -394,7 +401,7 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
    }
 
    /* before buffers and vars_to_ssa */
-   NIR_PASS_V(nir, gl_nir_lower_bindless_images);
+   NIR_PASS_V(nir, gl_nir_lower_images, true);
 
    /* TODO: Change GLSL to not lower shared memory. */
    if (prog->nir->info.stage == MESA_SHADER_COMPUTE &&
@@ -496,8 +503,8 @@ 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_finalize_nir_before_variants(nir);
 
@@ -654,18 +661,14 @@ st_link_nir(struct gl_context *ctx,
       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. */
+      /* Parameters will be filled during NIR linking. */
+      prog->Parameters = _mesa_new_parameter_list();
 
+      if (shader_program->data->spirv) {
          prog->nir = _mesa_spirv_to_nir(ctx, shader_program, shader->Stage, options);
       } else {
          validate_ir_tree(shader->ir);
 
-         prog->Parameters = _mesa_new_parameter_list();
-         _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
-                                                     prog->Parameters);
-
          if (ctx->_Shader->Flags & GLSL_DUMP) {
             _mesa_log("\n");
             _mesa_log("GLSL IR for linked %s program %d:\n",
@@ -675,9 +678,6 @@ st_link_nir(struct gl_context *ctx,
             _mesa_log("\n\n");
          }
 
-         prog->ExternalSamplersUsed = gl_external_samplers(prog);
-         _mesa_update_shader_textures_used(shader_program, prog);
-
          prog->nir = glsl_to_nir(st->ctx, shader_program, shader->Stage, options);
          st_nir_preprocess(st, prog, shader_program, shader->Stage);
       }
@@ -696,10 +696,10 @@ 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);
+      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];
@@ -720,19 +720,35 @@ st_link_nir(struct gl_context *ctx,
       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);
+
+   if (!shader_program->data->spirv) {
+      if (!gl_nir_link_glsl(ctx, shader_program))
+         return GL_FALSE;
+
+      for (unsigned i = 0; i < num_shaders; i++) {
+         struct gl_program *prog = linked_shader[i]->Program;
+         prog->ExternalSamplersUsed = gl_external_samplers(prog);
+         _mesa_update_shader_textures_used(shader_program, prog);
+      }
+
+      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;
 
+      /* 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);
 
-      /* Linked shaders are optimized in st_nir_link_shaders. Separate shaders
-       * and shaders with a fixed-func VS or FS are optimized here.
-       */
-      if (num_shaders == 1)
-         st_nir_opts(nir);
-
       /* 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
@@ -776,6 +792,25 @@ st_link_nir(struct gl_context *ctx,
       }
    }
 
+   struct shader_info *prev_info = NULL;
+
+   for (unsigned i = 0; i < num_shaders; i++) {
+      struct gl_linked_shader *shader = linked_shader[i];
+      struct shader_info *info = &shader->Program->nir->info;
+
+      if (prev_info &&
+          ctx->Const.ShaderCompilerOptions[shader->Stage].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;
+   }
+
    for (unsigned i = 0; i < num_shaders; i++) {
       struct gl_linked_shader *shader = linked_shader[i];
       struct gl_program *prog = shader->Program;
@@ -853,6 +888,34 @@ st_nir_lower_samplers(struct pipe_screen *screen, nir_shader *nir,
    if (prog) {
       prog->info.textures_used = nir->info.textures_used;
       prog->info.textures_used_by_txf = nir->info.textures_used_by_txf;
+      prog->info.images_used = nir->info.images_used;
+   }
+}
+
+static int
+st_packed_uniforms_type_size(const struct glsl_type *type, bool bindless)
+{
+   return glsl_count_dword_slots(type, bindless);
+}
+
+static int
+st_unpacked_uniforms_type_size(const struct glsl_type *type, bool bindless)
+{
+   return glsl_count_vec4_slots(type, false, bindless);
+}
+
+void
+st_nir_lower_uniforms(struct st_context *st, nir_shader *nir)
+{
+   if (st->ctx->Const.PackedDriverUniformStorage) {
+      NIR_PASS_V(nir, nir_lower_io, nir_var_uniform,
+                 st_packed_uniforms_type_size,
+                 (nir_lower_io_options)0);
+      NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
+   } else {
+      NIR_PASS_V(nir, nir_lower_io, nir_var_uniform,
+                 st_unpacked_uniforms_type_size,
+                 (nir_lower_io_options)0);
    }
 }
 
@@ -876,16 +939,10 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
    /* Set num_uniforms in number of attribute slots (vec4s) */
    nir->num_uniforms = DIV_ROUND_UP(prog->Parameters->NumParameterValues, 4);
 
-   if (st->ctx->Const.PackedDriverUniformStorage) {
-      NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
-                 (nir_lower_io_options)0);
-      NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
-   } else {
-      NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_uniforms_type_size,
-                 (nir_lower_io_options)0);
-   }
-
+   st_nir_lower_uniforms(st, nir);
    st_nir_lower_samplers(screen, nir, shader_program, prog);
+   if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
+      NIR_PASS_V(nir, gl_nir_lower_images, false);
 
    if (finalize_by_driver && screen->finalize_nir)
       screen->finalize_nir(screen, nir, false);