glsl/nir: Add a shared helper for building float64 shaders
authorJason Ekstrand <jason.ekstrand@intel.com>
Sun, 3 Mar 2019 16:00:14 +0000 (10:00 -0600)
committerJason Ekstrand <jason@jlekstrand.net>
Wed, 6 Mar 2019 17:24:57 +0000 (17:24 +0000)
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/compiler/glsl/glsl_to_nir.cpp
src/compiler/glsl/glsl_to_nir.h
src/compiler/glsl/standalone_scaffolding.cpp
src/compiler/glsl/standalone_scaffolding.h
src/mesa/drivers/dri/i965/brw_program.c
src/mesa/drivers/dri/i965/meson.build
src/mesa/state_tracker/st_glsl_to_nir.cpp

index f7df91d887d3513f8a31a46063b4e45b09c2555a..950885a1e005348174199f333d6b3cfb85ca56c3 100644 (file)
  *
  */
 
+#include "float64_glsl.h"
 #include "glsl_to_nir.h"
 #include "ir_visitor.h"
 #include "ir_hierarchical_visitor.h"
 #include "ir.h"
+#include "program.h"
 #include "compiler/nir/nir_control_flow.h"
 #include "compiler/nir/nir_builder.h"
+#include "main/errors.h"
 #include "main/imports.h"
 #include "main/mtypes.h"
+#include "main/shaderobj.h"
 #include "util/u_math.h"
 
 /*
@@ -2324,3 +2328,40 @@ nir_visitor::visit(ir_barrier *)
       nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
    nir_builder_instr_insert(&b, &instr->instr);
 }
+
+nir_shader *
+glsl_float64_funcs_to_nir(struct gl_context *ctx,
+                          const nir_shader_compiler_options *options)
+{
+   /* We pretend it's a vertex shader.  Ultimately, the stage shouldn't
+    * matter because we're not optimizing anything here.
+    */
+   struct gl_shader *sh = _mesa_new_shader(-1, MESA_SHADER_VERTEX);
+   sh->Source = float64_source;
+   sh->CompileStatus = COMPILE_FAILURE;
+   _mesa_glsl_compile_shader(ctx, sh, false, false, true);
+
+   if (!sh->CompileStatus) {
+      if (sh->InfoLog) {
+         _mesa_problem(ctx,
+                       "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
+                       sh->InfoLog, float64_source);
+      }
+      return NULL;
+   }
+
+   nir_shader *nir = nir_shader_create(NULL, MESA_SHADER_VERTEX, options, NULL);
+
+   nir_visitor v1(nir);
+   nir_function_visitor v2(&v1);
+   v2.run(sh->ir);
+   visit_exec_list(sh->ir, &v1);
+
+   /* _mesa_delete_shader will try to free sh->Source but it's static const */
+   sh->Source = NULL;
+   _mesa_delete_shader(ctx, sh);
+
+   nir_validate_shader(nir, "float64_funcs_to_nir");
+
+   return nir;
+}
index 8e38e0e1c9c3503fae9a4da095189cf0879beb19..115c8e1270bfa8a31eb2113c11c49a52cb05f3e6 100644 (file)
@@ -40,6 +40,9 @@ nir_shader *glsl_to_nir(const struct gl_shader_program *shader_prog,
                         gl_shader_stage stage,
                         const nir_shader_compiler_options *options);
 
+nir_shader *glsl_float64_funcs_to_nir(struct gl_context *ctx,
+                                      const nir_shader_compiler_options *options);
+
 #ifdef __cplusplus
 }
 #endif
index ec5f28ae40cb9c505d69fc08dea60bf016c025ad..b80054a47075678f12a6315e65d649f5707d2fa9 100644 (file)
@@ -54,6 +54,24 @@ _mesa_warning(struct gl_context *ctx, const char *fmt, ...)
     va_end(vargs);
 }
 
+void
+_mesa_problem(struct gl_context *ctx, const char *fmt, ...)
+{
+    va_list vargs;
+    (void) ctx;
+
+    va_start(vargs, fmt);
+
+    /* This output is not thread-safe, but that's good enough for the
+     * standalone compiler.
+     */
+    fprintf(stderr, "Mesa problem: ");
+    vfprintf(stderr, fmt, vargs);
+    fprintf(stderr, "\n");
+
+    va_end(vargs);
+}
+
 void
 _mesa_reference_shader_program_data(struct gl_context *ctx,
                                     struct gl_shader_program_data **ptr,
index 7da76f06fefff8d3bb258d759d30293e77ed4ddb..d7d1a9ea7ff22f59c205c10ad08217c872b53b85 100644 (file)
@@ -37,6 +37,9 @@
 extern "C" void
 _mesa_warning(struct gl_context *ctx, const char *fmtString, ... );
 
+extern "C" void
+_mesa_problem(struct gl_context *ctx, const char *fmtString, ... );
+
 extern "C" void
 _mesa_reference_shader_program_data(struct gl_context *ctx,
                                     struct gl_shader_program_data **ptr,
index 5b7c1afe55a3a49d90a9cd404625d2d3915cecda..f40d2c3354980f4889bf2015ee4ffd0b96547a48 100644 (file)
@@ -43,7 +43,6 @@
 #include "compiler/glsl/program.h"
 #include "compiler/glsl/gl_nir.h"
 #include "compiler/glsl/glsl_to_nir.h"
-#include "glsl/float64_glsl.h"
 
 #include "brw_program.h"
 #include "brw_context.h"
@@ -76,51 +75,6 @@ brw_nir_lower_uniforms(nir_shader *nir, bool is_scalar)
 static struct gl_program *brwNewProgram(struct gl_context *ctx, GLenum target,
                                         GLuint id, bool is_arb_asm);
 
-static nir_shader *
-compile_fp64_funcs(struct gl_context *ctx,
-                   const nir_shader_compiler_options *options,
-                   void *mem_ctx,
-                   gl_shader_stage stage)
-{
-   const GLuint name = ~0;
-   struct gl_shader *sh;
-
-   sh = _mesa_new_shader(name, stage);
-
-   sh->Source = float64_source;
-   sh->CompileStatus = COMPILE_FAILURE;
-   _mesa_glsl_compile_shader(ctx, sh, false, false, true);
-
-   if (!sh->CompileStatus) {
-      if (sh->InfoLog) {
-         _mesa_problem(ctx,
-                       "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
-                       sh->InfoLog, float64_source);
-      }
-   }
-
-   struct gl_shader_program *sh_prog;
-   sh_prog = _mesa_new_shader_program(name);
-   sh_prog->Label = NULL;
-   sh_prog->NumShaders = 1;
-   sh_prog->Shaders = malloc(sizeof(struct gl_shader *));
-   sh_prog->Shaders[0] = sh;
-
-   struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
-   linked->Stage = stage;
-   linked->Program =
-      brwNewProgram(ctx,
-                    _mesa_shader_stage_to_program(stage),
-                    name, false);
-
-   linked->ir = sh->ir;
-   sh_prog->_LinkedShaders[stage] = linked;
-
-   nir_shader *nir = glsl_to_nir(sh_prog, stage, options);
-
-   return nir_shader_clone(mem_ctx, nir);
-}
-
 nir_shader *
 brw_create_nir(struct brw_context *brw,
                const struct gl_shader_program *shader_prog,
@@ -160,9 +114,8 @@ brw_create_nir(struct brw_context *brw,
 
    if ((options->lower_doubles_options & nir_lower_fp64_full_software) &&
        nir->info.uses_64bit) {
-      nir_shader *fp64 = compile_fp64_funcs(ctx, options, ralloc_parent(nir), stage);
-
-      nir_validate_shader(fp64, "fp64");
+      nir_shader *fp64 = glsl_float64_funcs_to_nir(ctx, options);
+      ralloc_steal(ralloc_parent(nir), fp64);
       exec_list_append(&nir->functions, &fp64->functions);
    }
 
index ea63af9cc4b6e6100b699c5d1237f2e09ad7dc1a..bebaf8ea8b5e84d99af183290cb579f63bc1b7ad 100644 (file)
@@ -177,7 +177,7 @@ i965_oa_sources = custom_target(
 libi965 = static_library(
   'i965',
   [files_i965, i965_oa_sources, ir_expression_operation_h,
-   xmlpool_options_h, float64_glsl_h],
+   xmlpool_options_h],
   include_directories : [
     inc_common, inc_intel, inc_dri_common, inc_util, inc_include,
   ],
index 8ed7059a82eb557d85682b74261f1d255f78d93c..d62a89ec5b1a4befe4a6512c29dfe2cb709e33d4 100644 (file)
@@ -48,7 +48,6 @@
 #include "compiler/glsl/ir.h"
 #include "compiler/glsl/ir_optimization.h"
 #include "compiler/glsl/string_to_uint_map.h"
-#include "compiler/glsl/float64_glsl.h"
 
 static int
 type_size(const struct glsl_type *type)
@@ -341,50 +340,6 @@ st_nir_opts(nir_shader *nir, bool scalar)
    } while (progress);
 }
 
-static nir_shader *
-compile_fp64_funcs(struct gl_context *ctx,
-                   const nir_shader_compiler_options *options,
-                   void *mem_ctx,
-                   gl_shader_stage stage)
-{
-   const GLuint name = ~0;
-   struct gl_shader *sh;
-
-   sh = _mesa_new_shader(name, stage);
-
-   sh->Source = float64_source;
-   sh->CompileStatus = COMPILE_FAILURE;
-   _mesa_compile_shader(ctx, sh);
-
-   if (!sh->CompileStatus) {
-      if (sh->InfoLog) {
-         _mesa_problem(ctx,
-                       "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
-                       sh->InfoLog, float64_source);
-      }
-   }
-
-   struct gl_shader_program *sh_prog;
-   sh_prog = _mesa_new_shader_program(name);
-   sh_prog->Label = NULL;
-   sh_prog->NumShaders = 1;
-   sh_prog->Shaders = (struct gl_shader **)malloc(sizeof(struct gl_shader *));
-   sh_prog->Shaders[0] = sh;
-
-   struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
-   linked->Stage = stage;
-   linked->Program =
-      ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
-                             name, false);
-
-   linked->ir = sh->ir;
-   sh_prog->_LinkedShaders[stage] = linked;
-
-   nir_shader *nir = glsl_to_nir(sh_prog, stage, options);
-
-   return nir_shader_clone(mem_ctx, nir);
-}
-
 /* First third of converting glsl_to_nir.. this leaves things in a pre-
  * nir_lower_io state, so that shader variants can more easily insert/
  * replace variables, etc.
@@ -426,10 +381,8 @@ st_glsl_to_nir(struct st_context *st, struct gl_program *prog,
    nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
    if (nir->info.uses_64bit &&
        (options->lower_doubles_options & nir_lower_fp64_full_software) != 0) {
-      nir_shader *fp64 =
-         compile_fp64_funcs(st->ctx, options, ralloc_parent(nir),
-                            nir->info.stage);
-      nir_validate_shader(fp64, "fp64");
+      nir_shader *fp64 = glsl_float64_funcs_to_nir(st->ctx, options);
+      ralloc_steal(ralloc_parent(nir), fp64);
       exec_list_append(&nir->functions, &fp64->functions);
    }