From: Jason Ekstrand Date: Sun, 3 Mar 2019 16:00:14 +0000 (-0600) Subject: glsl/nir: Add a shared helper for building float64 shaders X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=82d9a37a59c94ba3678b33acb9f2749cbbd7bfcc;p=mesa.git glsl/nir: Add a shared helper for building float64 shaders Reviewed-by: Matt Turner Reviewed-by: Jordan Justen Reviewed-by: Kenneth Graunke --- diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp index f7df91d887d..950885a1e00 100644 --- a/src/compiler/glsl/glsl_to_nir.cpp +++ b/src/compiler/glsl/glsl_to_nir.cpp @@ -25,14 +25,18 @@ * */ +#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; +} diff --git a/src/compiler/glsl/glsl_to_nir.h b/src/compiler/glsl/glsl_to_nir.h index 8e38e0e1c9c..115c8e1270b 100644 --- a/src/compiler/glsl/glsl_to_nir.h +++ b/src/compiler/glsl/glsl_to_nir.h @@ -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 diff --git a/src/compiler/glsl/standalone_scaffolding.cpp b/src/compiler/glsl/standalone_scaffolding.cpp index ec5f28ae40c..b80054a4707 100644 --- a/src/compiler/glsl/standalone_scaffolding.cpp +++ b/src/compiler/glsl/standalone_scaffolding.cpp @@ -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, diff --git a/src/compiler/glsl/standalone_scaffolding.h b/src/compiler/glsl/standalone_scaffolding.h index 7da76f06fef..d7d1a9ea7ff 100644 --- a/src/compiler/glsl/standalone_scaffolding.h +++ b/src/compiler/glsl/standalone_scaffolding.h @@ -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, diff --git a/src/mesa/drivers/dri/i965/brw_program.c b/src/mesa/drivers/dri/i965/brw_program.c index 5b7c1afe55a..f40d2c33549 100644 --- a/src/mesa/drivers/dri/i965/brw_program.c +++ b/src/mesa/drivers/dri/i965/brw_program.c @@ -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); } diff --git a/src/mesa/drivers/dri/i965/meson.build b/src/mesa/drivers/dri/i965/meson.build index ea63af9cc4b..bebaf8ea8b5 100644 --- a/src/mesa/drivers/dri/i965/meson.build +++ b/src/mesa/drivers/dri/i965/meson.build @@ -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, ], diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp index 8ed7059a82e..d62a89ec5b1 100644 --- a/src/mesa/state_tracker/st_glsl_to_nir.cpp +++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp @@ -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); }