radeonsi: stop using TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
[mesa.git] / src / gallium / drivers / zink / zink_compiler.c
index 6583040d5c75e0f51c7f6cad6f51c02394dbbd64..b9d1b666b14cda3fd087189d15a8714a01704a4e 100644 (file)
@@ -21,7 +21,9 @@
  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include "zink_context.h"
 #include "zink_compiler.h"
+#include "zink_program.h"
 #include "zink_screen.h"
 #include "nir_to_spirv/nir_to_spirv.h"
 
@@ -50,8 +52,49 @@ lower_discard_if_instr(nir_intrinsic_instr *instr, nir_builder *b)
       nir_instr_remove(&instr->instr);
       return true;
    }
-   assert(instr->intrinsic != nir_intrinsic_discard ||
-          nir_block_last_instr(instr->instr.block) == &instr->instr);
+   /* a shader like this (shaders@glsl-fs-discard-04):
+
+      uniform int j, k;
+
+      void main()
+      {
+       for (int i = 0; i < j; i++) {
+        if (i > k)
+         continue;
+        discard;
+       }
+       gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);
+      }
+
+
+
+      will generate nir like:
+
+      loop   {
+         //snip
+         if   ssa_11   {
+            block   block_5:
+            /   preds:   block_4   /
+            vec1   32   ssa_17   =   iadd   ssa_50,   ssa_31
+            /   succs:   block_7   /
+         }   else   {
+            block   block_6:
+            /   preds:   block_4   /
+            intrinsic   discard   ()   () <-- not last instruction
+            vec1   32   ssa_23   =   iadd   ssa_50,   ssa_31 <-- dead code loop itr increment
+            /   succs:   block_7   /
+         }
+         //snip
+      }
+
+      which means that we can't assert like this:
+
+      assert(instr->intrinsic != nir_intrinsic_discard ||
+             nir_block_last_instr(instr->instr.block) == &instr->instr);
+
+
+      and it's unnecessary anyway since post-vtn optimizing will dce the instructions following the discard
+    */
 
    return false;
 }
@@ -88,6 +131,11 @@ static const struct nir_shader_compiler_options nir_options = {
    .lower_flrp32 = true,
    .lower_fpow = true,
    .lower_fsat = true,
+   .lower_extract_byte = true,
+   .lower_extract_word = true,
+   .lower_mul_high = true,
+   .lower_rotate = true,
+   .lower_uadd_carry = true,
 };
 
 const void *
@@ -108,7 +156,7 @@ zink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens)
       fprintf(stderr, "---8<---\n\n");
    }
 
-   return tgsi_to_nir(tokens, screen);
+   return tgsi_to_nir(tokens, screen, false);
 }
 
 static void
@@ -131,17 +179,93 @@ optimize_nir(struct nir_shader *s)
    } while (progress);
 }
 
+/* check for a genuine gl_PointSize output vs one from nir_lower_point_size_mov */
+static bool
+check_psiz(struct nir_shader *s)
+{
+   nir_foreach_shader_out_variable(var, s) {
+      if (var->data.location == VARYING_SLOT_PSIZ) {
+         /* genuine PSIZ outputs will have this set */
+         return !!var->data.explicit_location;
+      }
+   }
+   return false;
+}
+
+/* semi-copied from iris */
+static void
+update_so_info(struct zink_shader *sh,
+               uint64_t outputs_written, bool have_psiz)
+{
+   uint8_t reverse_map[64] = {};
+   unsigned slot = 0;
+   while (outputs_written) {
+      int bit = u_bit_scan64(&outputs_written);
+      /* PSIZ from nir_lower_point_size_mov breaks stream output, so always skip it */
+      if (bit == VARYING_SLOT_PSIZ && !have_psiz)
+         continue;
+      reverse_map[slot++] = bit;
+   }
+
+   for (unsigned i = 0; i < sh->streamout.so_info.num_outputs; i++) {
+      struct pipe_stream_output *output = &sh->streamout.so_info.output[i];
+      /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
+      sh->streamout.so_info_slots[i] = reverse_map[output->register_index];
+   }
+}
+
+VkShaderModule
+zink_shader_compile(struct zink_screen *screen, struct zink_shader *zs)
+{
+   VkShaderModule mod = VK_NULL_HANDLE;
+   void *streamout = zs->streamout.so_info_slots ? &zs->streamout : NULL;
+   struct spirv_shader *spirv = nir_to_spirv(zs->nir, streamout);
+   assert(spirv);
+
+   if (zink_debug & ZINK_DEBUG_SPIRV) {
+      char buf[256];
+      static int i;
+      snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
+      FILE *fp = fopen(buf, "wb");
+      if (fp) {
+         fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
+         fclose(fp);
+         fprintf(stderr, "wrote '%s'...\n", buf);
+      }
+   }
+
+   VkShaderModuleCreateInfo smci = {};
+   smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+   smci.codeSize = spirv->num_words * sizeof(uint32_t);
+   smci.pCode = spirv->words;
+
+   if (vkCreateShaderModule(screen->dev, &smci, NULL, &mod) != VK_SUCCESS)
+      mod = VK_NULL_HANDLE;
+
+   /* TODO: determine if there's any reason to cache spirv output? */
+   free(spirv->words);
+   free(spirv);
+   return mod;
+}
+
 struct zink_shader *
-zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
+zink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
+                   const struct pipe_stream_output_info *so_info)
 {
    struct zink_shader *ret = CALLOC_STRUCT(zink_shader);
+   bool have_psiz = false;
+
+   ret->programs = _mesa_pointer_set_create(NULL);
 
    NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 1);
    NIR_PASS_V(nir, nir_lower_clip_halfz);
+   if (nir->info.stage == MESA_SHADER_VERTEX)
+      have_psiz = check_psiz(nir);
    NIR_PASS_V(nir, nir_lower_regs_to_ssa);
    optimize_nir(nir);
-   NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp);
+   NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp, NULL);
    NIR_PASS_V(nir, lower_discard_if);
+   NIR_PASS_V(nir, nir_lower_fragcolor);
    NIR_PASS_V(nir, nir_convert_from_ssa, true);
 
    if (zink_debug & ZINK_DEBUG_NIR) {
@@ -151,7 +275,8 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
    }
 
    ret->num_bindings = 0;
-   nir_foreach_variable(var, &nir->uniforms) {
+   nir_foreach_variable_with_modes(var, nir, nir_var_uniform |
+                                             nir_var_mem_ubo) {
       if (var->data.mode == nir_var_mem_ubo) {
          int binding = zink_binding(nir->info.stage,
                                     VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
@@ -185,35 +310,29 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir)
       }
    }
 
-   ret->info = nir->info;
-
-   struct spirv_shader *spirv = nir_to_spirv(nir);
-   assert(spirv);
-
-   if (zink_debug & ZINK_DEBUG_SPIRV) {
-      char buf[256];
-      static int i;
-      snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
-      FILE *fp = fopen(buf, "wb");
-      fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
-      fclose(fp);
-      fprintf(stderr, "wrote '%s'...\n", buf);
+   ret->nir = nir;
+   if (so_info) {
+      memcpy(&ret->streamout.so_info, so_info, sizeof(struct pipe_stream_output_info));
+      ret->streamout.so_info_slots = malloc(so_info->num_outputs * sizeof(unsigned int));
+      assert(ret->streamout.so_info_slots);
+      update_so_info(ret, nir->info.outputs_written, have_psiz);
    }
 
-   VkShaderModuleCreateInfo smci = {};
-   smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
-   smci.codeSize = spirv->num_words * sizeof(uint32_t);
-   smci.pCode = spirv->words;
-
-   if (vkCreateShaderModule(screen->dev, &smci, NULL, &ret->shader_module) != VK_SUCCESS)
-      return NULL;
-
    return ret;
 }
 
 void
-zink_shader_free(struct zink_screen *screen, struct zink_shader *shader)
+zink_shader_free(struct zink_context *ctx, struct zink_shader *shader)
 {
-   vkDestroyShaderModule(screen->dev, shader->shader_module, NULL);
+   struct zink_screen *screen = zink_screen(ctx->base.screen);
+   set_foreach(shader->programs, entry) {
+      struct zink_gfx_program *prog = (void*)entry->key;
+      _mesa_hash_table_remove_key(ctx->program_cache, prog->shaders);
+      prog->shaders[pipe_shader_type_from_mesa(shader->nir->info.stage)] = NULL;
+      zink_gfx_program_reference(screen, &prog, NULL);
+   }
+   _mesa_set_destroy(shader->programs, NULL);
+   free(shader->streamout.so_info_slots);
+   ralloc_free(shader->nir);
    FREE(shader);
 }