nir/lower_indirect_derefs: Add a threshold
[mesa.git] / src / compiler / spirv / spirv2nir.c
index 0ae14fb19b1fca8ff5fc46411c2cbc4008d0d6fd..bbec4d2af6a699e78128c07036d4baf78e6cfb80 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <getopt.h>
 
 #define WORD_SIZE 4
 
+static gl_shader_stage
+stage_to_enum(char *stage)
+{
+   if (!strcmp(stage, "vertex"))
+      return MESA_SHADER_VERTEX;
+   else if (!strcmp(stage, "tess-ctrl"))
+      return MESA_SHADER_TESS_CTRL;
+   else if (!strcmp(stage, "tess-eval"))
+      return MESA_SHADER_TESS_EVAL;
+   else if (!strcmp(stage, "geometry"))
+      return MESA_SHADER_GEOMETRY;
+   else if (!strcmp(stage, "fragment"))
+      return MESA_SHADER_FRAGMENT;
+   else if (!strcmp(stage, "compute"))
+      return MESA_SHADER_COMPUTE;
+   else if (!strcmp(stage, "kernel"))
+      return MESA_SHADER_KERNEL;
+   else
+      return MESA_SHADER_NONE;
+}
+
 int main(int argc, char **argv)
 {
+   gl_shader_stage shader_stage = MESA_SHADER_FRAGMENT;
+   char *entry_point = "main";
+   int ch;
+
+   static struct option long_options[] =
+     {
+       {"stage",  required_argument, 0, 's'},
+       {"entry",  required_argument, 0, 'e'},
+       {0, 0, 0, 0}
+     };
+
+   while ((ch = getopt_long(argc - 1, argv + 1, "s:e:", long_options, NULL)) != -1)
+   {
+      switch (ch)
+      {
+         case 's':
+            shader_stage = stage_to_enum(optarg);
+            if (shader_stage == MESA_SHADER_NONE)
+            {
+               fprintf(stderr, "Unknown stage %s\n", optarg);
+               return 1;
+            }
+            break;
+         case 'e':
+            entry_point = optarg;
+            break;
+         default:
+            fprintf(stderr, "Unrecognized option.\n");
+            return 1;
+      }
+   }
+
    int fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
@@ -72,9 +126,29 @@ int main(int argc, char **argv)
       return 1;
    }
 
-   nir_function *func = spirv_to_nir(map, word_count, NULL, 0,
-                                     MESA_SHADER_FRAGMENT, "main", NULL, NULL);
-   nir_print_shader(func->shader, stderr);
+   glsl_type_singleton_init_or_ref();
+
+   struct spirv_to_nir_options spirv_opts = {0};
+   if (shader_stage == MESA_SHADER_KERNEL) {
+      spirv_opts.environment = NIR_SPIRV_OPENCL;
+      spirv_opts.caps.address = true;
+      spirv_opts.caps.float64 = true;
+      spirv_opts.caps.int8 = true;
+      spirv_opts.caps.int16 = true;
+      spirv_opts.caps.int64 = true;
+      spirv_opts.caps.kernel = true;
+   }
+
+   nir_shader *nir = spirv_to_nir(map, word_count, NULL, 0,
+                                  shader_stage, entry_point,
+                                  &spirv_opts, NULL);
+
+   if (nir)
+      nir_print_shader(nir, stderr);
+   else
+      fprintf(stderr, "SPIRV to NIR compilation failed\n");
+
+   glsl_type_singleton_decref();
 
    return 0;
 }