diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/Makefile.glsl.am | 16 | ||||
-rw-r--r-- | src/compiler/Makefile.sources | 3 | ||||
-rw-r--r-- | src/compiler/SConscript.glsl | 2 | ||||
-rw-r--r-- | src/compiler/glsl/main.cpp | 380 | ||||
-rw-r--r-- | src/compiler/glsl/standalone.cpp | 433 | ||||
-rw-r--r-- | src/compiler/glsl/standalone.h | 51 |
6 files changed, 514 insertions, 371 deletions
diff --git a/src/compiler/Makefile.glsl.am b/src/compiler/Makefile.glsl.am index daf98f61244..23c2a6b24c2 100644 --- a/src/compiler/Makefile.glsl.am +++ b/src/compiler/Makefile.glsl.am @@ -57,7 +57,6 @@ glsl_tests_blob_test_LDADD = \ glsl/libglsl.la glsl_tests_general_ir_test_SOURCES = \ - glsl/standalone_scaffolding.cpp \ glsl/tests/builtin_variable_test.cpp \ glsl/tests/invalidate_locations_test.cpp \ glsl/tests/general_ir_test.cpp \ @@ -67,6 +66,7 @@ glsl_tests_general_ir_test_CFLAGS = \ glsl_tests_general_ir_test_LDADD = \ $(top_builddir)/src/gtest/libgtest.la \ glsl/libglsl.la \ + glsl/libstandalone.la \ $(top_builddir)/src/libglsl_util.la \ $(PTHREAD_LIBS) @@ -93,7 +93,7 @@ glsl_tests_sampler_types_test_LDADD = \ $(top_builddir)/src/libglsl_util.la \ $(PTHREAD_LIBS) -noinst_LTLIBRARIES += glsl/libglsl.la glsl/libglcpp.la +noinst_LTLIBRARIES += glsl/libglsl.la glsl/libglcpp.la glsl/libstandalone.la glsl_libglcpp_la_LIBADD = \ $(top_builddir)/src/util/libmesautil.la @@ -121,23 +121,29 @@ glsl_libglsl_la_SOURCES = \ $(LIBGLSL_FILES) -glsl_compiler_SOURCES = \ +glsl_libstandalone_la_SOURCES = \ $(GLSL_COMPILER_CXX_FILES) -glsl_compiler_LDADD = \ +glsl_libstandalone_la_LIBADD = \ glsl/libglsl.la \ $(top_builddir)/src/libglsl_util.la \ $(top_builddir)/src/util/libmesautil.la \ $(PTHREAD_LIBS) +glsl_compiler_SOURCES = \ + glsl/main.cpp + +glsl_compiler_LDADD = \ + glsl/libstandalone.la + glsl_glsl_test_SOURCES = \ - glsl/standalone_scaffolding.cpp \ glsl/test.cpp \ glsl/test_optpass.cpp \ glsl/test_optpass.h glsl_glsl_test_LDADD = \ glsl/libglsl.la \ + glsl/libstandalone.la \ $(top_builddir)/src/libglsl_util.la \ $(PTHREAD_LIBS) diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index b8f2b4950b4..ebc5953eb97 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -138,7 +138,8 @@ LIBGLSL_FILES = \ GLSL_COMPILER_CXX_FILES = \ glsl/standalone_scaffolding.cpp \ glsl/standalone_scaffolding.h \ - glsl/main.cpp + glsl/standalone.cpp \ + glsl/standalone.h # libglsl generated sources LIBGLSL_GENERATED_CXX_FILES = \ diff --git a/src/compiler/SConscript.glsl b/src/compiler/SConscript.glsl index 73abaf13acf..474df11d566 100644 --- a/src/compiler/SConscript.glsl +++ b/src/compiler/SConscript.glsl @@ -109,6 +109,8 @@ if env['platform'] == 'windows': env.Prepend(LIBS = [compiler, glsl]) +compiler_objs += env.StaticObject("glsl/main.cpp") + glsl_compiler = env.Program( target = 'glsl_compiler', source = compiler_objs, diff --git a/src/compiler/glsl/main.cpp b/src/compiler/glsl/main.cpp index d2535758e1a..f65b1854d32 100644 --- a/src/compiler/glsl/main.cpp +++ b/src/compiler/glsl/main.cpp @@ -20,6 +20,8 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ + +#include <stdio.h> #include <getopt.h> /** @file main.cpp @@ -31,255 +33,16 @@ * offline compile GLSL code and examine the resulting GLSL IR. */ -#include "ast.h" -#include "glsl_parser_extras.h" -#include "ir_optimization.h" -#include "program.h" -#include "program/hash_table.h" -#include "loop_analysis.h" -#include "standalone_scaffolding.h" - -static int glsl_version = 330; - -static void -initialize_context(struct gl_context *ctx, gl_api api) -{ - initialize_context_to_defaults(ctx, api); - - /* The standalone compiler needs to claim support for almost - * everything in order to compile the built-in functions. - */ - ctx->Const.GLSLVersion = glsl_version; - ctx->Extensions.ARB_ES3_compatibility = true; - ctx->Const.MaxComputeWorkGroupCount[0] = 65535; - ctx->Const.MaxComputeWorkGroupCount[1] = 65535; - ctx->Const.MaxComputeWorkGroupCount[2] = 65535; - ctx->Const.MaxComputeWorkGroupSize[0] = 1024; - ctx->Const.MaxComputeWorkGroupSize[1] = 1024; - ctx->Const.MaxComputeWorkGroupSize[2] = 64; - ctx->Const.MaxComputeWorkGroupInvocations = 1024; - ctx->Const.MaxComputeSharedMemorySize = 32768; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8; - ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12; - - switch (ctx->Const.GLSLVersion) { - case 100: - ctx->Const.MaxClipPlanes = 0; - ctx->Const.MaxCombinedTextureImageUnits = 8; - ctx->Const.MaxDrawBuffers = 2; - ctx->Const.MinProgramTexelOffset = 0; - ctx->Const.MaxProgramTexelOffset = 0; - ctx->Const.MaxLights = 0; - ctx->Const.MaxTextureCoordUnits = 0; - ctx->Const.MaxTextureUnits = 8; - - ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; - - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = - ctx->Const.MaxCombinedTextureImageUnits; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ - - ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; - break; - case 110: - case 120: - ctx->Const.MaxClipPlanes = 6; - ctx->Const.MaxCombinedTextureImageUnits = 2; - ctx->Const.MaxDrawBuffers = 1; - ctx->Const.MinProgramTexelOffset = 0; - ctx->Const.MaxProgramTexelOffset = 0; - ctx->Const.MaxLights = 8; - ctx->Const.MaxTextureCoordUnits = 2; - ctx->Const.MaxTextureUnits = 2; - - ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; - - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = - ctx->Const.MaxCombinedTextureImageUnits; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ - - ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; - break; - case 130: - case 140: - ctx->Const.MaxClipPlanes = 8; - ctx->Const.MaxCombinedTextureImageUnits = 16; - ctx->Const.MaxDrawBuffers = 8; - ctx->Const.MinProgramTexelOffset = -8; - ctx->Const.MaxProgramTexelOffset = 7; - ctx->Const.MaxLights = 8; - ctx->Const.MaxTextureCoordUnits = 8; - ctx->Const.MaxTextureUnits = 2; - - ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; - - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ - - ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; - break; - case 150: - case 330: - ctx->Const.MaxClipPlanes = 8; - ctx->Const.MaxDrawBuffers = 8; - ctx->Const.MinProgramTexelOffset = -8; - ctx->Const.MaxProgramTexelOffset = 7; - ctx->Const.MaxLights = 8; - ctx->Const.MaxTextureCoordUnits = 8; - ctx->Const.MaxTextureUnits = 2; - - ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; - - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128; - - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = - ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ - - ctx->Const.MaxCombinedTextureImageUnits = - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits - + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits - + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits; - - ctx->Const.MaxGeometryOutputVertices = 256; - ctx->Const.MaxGeometryTotalOutputComponents = 1024; - - ctx->Const.MaxVarying = 60 / 4; - break; - case 300: - ctx->Const.MaxClipPlanes = 8; - ctx->Const.MaxCombinedTextureImageUnits = 32; - ctx->Const.MaxDrawBuffers = 4; - ctx->Const.MinProgramTexelOffset = -8; - ctx->Const.MaxProgramTexelOffset = 7; - ctx->Const.MaxLights = 0; - ctx->Const.MaxTextureCoordUnits = 0; - ctx->Const.MaxTextureUnits = 0; - - ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; - ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ - ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4; - - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4; - ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ - - ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4; - break; - } - - ctx->Const.GenerateTemporaryNames = true; - ctx->Const.MaxPatchVertices = 32; - - ctx->Driver.NewShader = _mesa_new_shader; -} - -/* Returned string will have 'ctx' as its ralloc owner. */ -static char * -load_text_file(void *ctx, const char *file_name) -{ - char *text = NULL; - size_t size; - size_t total_read = 0; - FILE *fp = fopen(file_name, "rb"); - - if (!fp) { - return NULL; - } - - fseek(fp, 0L, SEEK_END); - size = ftell(fp); - fseek(fp, 0L, SEEK_SET); - - text = (char *) ralloc_size(ctx, size + 1); - if (text != NULL) { - do { - size_t bytes = fread(text + total_read, - 1, size - total_read, fp); - if (bytes < size - total_read) { - free(text); - text = NULL; - goto error; - } - - if (bytes == 0) { - break; - } - - total_read += bytes; - } while (total_read < size); +#include "main/mtypes.h" +#include "standalone.h" - text[total_read] = '\0'; -error:; - } - - fclose(fp); - - return text; -} - -int dump_ast = 0; -int dump_hir = 0; -int dump_lir = 0; -int do_link = 0; +static struct standalone_options options; const struct option compiler_opts[] = { - { "dump-ast", no_argument, &dump_ast, 1 }, - { "dump-hir", no_argument, &dump_hir, 1 }, - { "dump-lir", no_argument, &dump_lir, 1 }, - { "link", no_argument, &do_link, 1 }, + { "dump-ast", no_argument, &options.dump_ast, 1 }, + { "dump-hir", no_argument, &options.dump_hir, 1 }, + { "dump-lir", no_argument, &options.dump_lir, 1 }, + { "link", no_argument, &options.do_link, 1 }, { "version", required_argument, NULL, 'v' }, { NULL, 0, NULL, 0 } }; @@ -302,147 +65,34 @@ usage_fail(const char *name) exit(EXIT_FAILURE); } - -void -compile_shader(struct gl_context *ctx, struct gl_shader *shader) -{ - struct _mesa_glsl_parse_state *state = - new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader); - - _mesa_glsl_compile_shader(ctx, shader, dump_ast, dump_hir); - - /* Print out the resulting IR */ - if (!state->error && dump_lir) { - _mesa_print_ir(stdout, shader->ir, state); - } - - return; -} - int -main(int argc, char **argv) +main(int argc, char * const* argv) { int status = EXIT_SUCCESS; - struct gl_context local_ctx; - struct gl_context *ctx = &local_ctx; - bool glsl_es = false; int c; int idx = 0; while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) { switch (c) { case 'v': - glsl_version = strtol(optarg, NULL, 10); - switch (glsl_version) { - case 100: - case 300: - glsl_es = true; - break; - case 110: - case 120: - case 130: - case 140: - case 150: - case 330: - glsl_es = false; - break; - default: - fprintf(stderr, "Unrecognized GLSL version `%s'\n", optarg); - usage_fail(argv[0]); - break; - } + options.glsl_version = strtol(optarg, NULL, 10); break; default: break; } } - if (argc <= optind) usage_fail(argv[0]); - initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT); - struct gl_shader_program *whole_program; - whole_program = rzalloc (NULL, struct gl_shader_program); - assert(whole_program != NULL); - whole_program->InfoLog = ralloc_strdup(whole_program, ""); - - /* Created just to avoid segmentation faults */ - whole_program->AttributeBindings = new string_to_uint_map; - whole_program->FragDataBindings = new string_to_uint_map; - whole_program->FragDataIndexBindings = new string_to_uint_map; - - for (/* empty */; argc > optind; optind++) { - whole_program->Shaders = - reralloc(whole_program, whole_program->Shaders, - struct gl_shader *, whole_program->NumShaders + 1); - assert(whole_program->Shaders != NULL); + whole_program = standalone_compile_shader(&options, argc - optind, &argv[optind]); - struct gl_shader *shader = rzalloc(whole_program, gl_shader); - - whole_program->Shaders[whole_program->NumShaders] = shader; - whole_program->NumShaders++; - - const unsigned len = strlen(argv[optind]); - if (len < 6) - usage_fail(argv[0]); - - const char *const ext = & argv[optind][len - 5]; - if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0) - shader->Type = GL_VERTEX_SHADER; - else if (strncmp(".tesc", ext, 5) == 0) - shader->Type = GL_TESS_CONTROL_SHADER; - else if (strncmp(".tese", ext, 5) == 0) - shader->Type = GL_TESS_EVALUATION_SHADER; - else if (strncmp(".geom", ext, 5) == 0) - shader->Type = GL_GEOMETRY_SHADER; - else if (strncmp(".frag", ext, 5) == 0) - shader->Type = GL_FRAGMENT_SHADER; - else if (strncmp(".comp", ext, 5) == 0) - shader->Type = GL_COMPUTE_SHADER; - else - usage_fail(argv[0]); - shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type); - - shader->Source = load_text_file(whole_program, argv[optind]); - if (shader->Source == NULL) { - printf("File \"%s\" does not exist.\n", argv[optind]); - exit(EXIT_FAILURE); - } - - compile_shader(ctx, shader); - - if (strlen(shader->InfoLog) > 0) - printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog); - - if (!shader->CompileStatus) { - status = EXIT_FAILURE; - break; - } - } - - if ((status == EXIT_SUCCESS) && do_link) { - _mesa_clear_shader_program_data(whole_program); - - link_shaders(ctx, whole_program); - status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; - - if (strlen(whole_program->InfoLog) > 0) - printf("Info log for linking:\n%s\n", whole_program->InfoLog); - } - - for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) - ralloc_free(whole_program->_LinkedShaders[i]); - - delete whole_program->AttributeBindings; - delete whole_program->FragDataBindings; - delete whole_program->FragDataIndexBindings; + if (!whole_program) + usage_fail(argv[0]); - ralloc_free(whole_program); - _mesa_glsl_release_types(); - _mesa_glsl_release_builtin_functions(); + standalone_compiler_cleanup(whole_program); return status; } diff --git a/src/compiler/glsl/standalone.cpp b/src/compiler/glsl/standalone.cpp new file mode 100644 index 00000000000..e5b9057cd01 --- /dev/null +++ b/src/compiler/glsl/standalone.cpp @@ -0,0 +1,433 @@ +/* + * Copyright © 2008, 2009 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +#include <getopt.h> + +/** @file standalone.cpp + * + * Standalone compiler helper lib. Used by standalone glsl_compiler and + * also available to drivers to implement their own standalone compiler + * with driver backend. + */ + +#include "ast.h" +#include "glsl_parser_extras.h" +#include "ir_optimization.h" +#include "program.h" +#include "program/hash_table.h" +#include "loop_analysis.h" +#include "standalone_scaffolding.h" +#include "standalone.h" + +static const struct standalone_options *options; + +static void +initialize_context(struct gl_context *ctx, gl_api api) +{ + initialize_context_to_defaults(ctx, api); + + /* The standalone compiler needs to claim support for almost + * everything in order to compile the built-in functions. + */ + ctx->Const.GLSLVersion = options->glsl_version; + ctx->Extensions.ARB_ES3_compatibility = true; + ctx->Const.MaxComputeWorkGroupCount[0] = 65535; + ctx->Const.MaxComputeWorkGroupCount[1] = 65535; + ctx->Const.MaxComputeWorkGroupCount[2] = 65535; + ctx->Const.MaxComputeWorkGroupSize[0] = 1024; + ctx->Const.MaxComputeWorkGroupSize[1] = 1024; + ctx->Const.MaxComputeWorkGroupSize[2] = 64; + ctx->Const.MaxComputeWorkGroupInvocations = 1024; + ctx->Const.MaxComputeSharedMemorySize = 32768; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8; + ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12; + + switch (ctx->Const.GLSLVersion) { + case 100: + ctx->Const.MaxClipPlanes = 0; + ctx->Const.MaxCombinedTextureImageUnits = 8; + ctx->Const.MaxDrawBuffers = 2; + ctx->Const.MinProgramTexelOffset = 0; + ctx->Const.MaxProgramTexelOffset = 0; + ctx->Const.MaxLights = 0; + ctx->Const.MaxTextureCoordUnits = 0; + ctx->Const.MaxTextureUnits = 8; + + ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = + ctx->Const.MaxCombinedTextureImageUnits; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ + + ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; + break; + case 110: + case 120: + ctx->Const.MaxClipPlanes = 6; + ctx->Const.MaxCombinedTextureImageUnits = 2; + ctx->Const.MaxDrawBuffers = 1; + ctx->Const.MinProgramTexelOffset = 0; + ctx->Const.MaxProgramTexelOffset = 0; + ctx->Const.MaxLights = 8; + ctx->Const.MaxTextureCoordUnits = 2; + ctx->Const.MaxTextureUnits = 2; + + ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32; + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = + ctx->Const.MaxCombinedTextureImageUnits; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ + + ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; + break; + case 130: + case 140: + ctx->Const.MaxClipPlanes = 8; + ctx->Const.MaxCombinedTextureImageUnits = 16; + ctx->Const.MaxDrawBuffers = 8; + ctx->Const.MinProgramTexelOffset = -8; + ctx->Const.MaxProgramTexelOffset = 7; + ctx->Const.MaxLights = 8; + ctx->Const.MaxTextureCoordUnits = 8; + ctx->Const.MaxTextureUnits = 2; + + ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ + + ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4; + break; + case 150: + case 330: + ctx->Const.MaxClipPlanes = 8; + ctx->Const.MaxDrawBuffers = 8; + ctx->Const.MinProgramTexelOffset = -8; + ctx->Const.MaxProgramTexelOffset = 7; + ctx->Const.MaxLights = 8; + ctx->Const.MaxTextureCoordUnits = 8; + ctx->Const.MaxTextureUnits = 2; + + ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64; + + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents = + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents; + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128; + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ + + ctx->Const.MaxCombinedTextureImageUnits = + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits + + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits; + + ctx->Const.MaxGeometryOutputVertices = 256; + ctx->Const.MaxGeometryTotalOutputComponents = 1024; + + ctx->Const.MaxVarying = 60 / 4; + break; + case 300: + ctx->Const.MaxClipPlanes = 8; + ctx->Const.MaxCombinedTextureImageUnits = 32; + ctx->Const.MaxDrawBuffers = 4; + ctx->Const.MinProgramTexelOffset = -8; + ctx->Const.MaxProgramTexelOffset = 7; + ctx->Const.MaxLights = 0; + ctx->Const.MaxTextureCoordUnits = 0; + ctx->Const.MaxTextureUnits = 0; + + ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024; + ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */ + ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4; + + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4; + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */ + + ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4; + break; + } + + ctx->Const.GenerateTemporaryNames = true; + ctx->Const.MaxPatchVertices = 32; + + ctx->Driver.NewShader = _mesa_new_shader; +} + +/* Returned string will have 'ctx' as its ralloc owner. */ +static char * +load_text_file(void *ctx, const char *file_name) +{ + char *text = NULL; + size_t size; + size_t total_read = 0; + FILE *fp = fopen(file_name, "rb"); + + if (!fp) { + return NULL; + } + + fseek(fp, 0L, SEEK_END); + size = ftell(fp); + fseek(fp, 0L, SEEK_SET); + + text = (char *) ralloc_size(ctx, size + 1); + if (text != NULL) { + do { + size_t bytes = fread(text + total_read, + 1, size - total_read, fp); + if (bytes < size - total_read) { + free(text); + text = NULL; + goto error; + } + + if (bytes == 0) { + break; + } + + total_read += bytes; + } while (total_read < size); + + text[total_read] = '\0'; + error:; + } + + fclose(fp); + + return text; +} + +void +compile_shader(struct gl_context *ctx, struct gl_shader *shader) +{ + struct _mesa_glsl_parse_state *state = + new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader); + + _mesa_glsl_compile_shader(ctx, shader, options->dump_ast, options->dump_hir); + + /* Print out the resulting IR */ + if (!state->error && options->dump_lir) { + _mesa_print_ir(stdout, shader->ir, state); + } + + return; +} + +void +init_gl_program(struct gl_program *prog, GLenum target) +{ + mtx_init(&prog->Mutex, mtx_plain); + + prog->RefCount = 1; + prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; + + /* default mapping from samplers to texture units */ + for (int i = 0; i < MAX_SAMPLERS; i++) + prog->SamplerUnits[i] = i; +} + +extern "C" struct gl_shader_program * +standalone_compile_shader(const struct standalone_options *_options, + unsigned num_files, char* const* files) +{ + int status = EXIT_SUCCESS; + static struct gl_context local_ctx; + struct gl_context *ctx = &local_ctx; + bool glsl_es = false; + + options = _options; + + switch (options->glsl_version) { + case 100: + case 300: + glsl_es = true; + break; + case 110: + case 120: + case 130: + case 140: + case 150: + case 330: + glsl_es = false; + break; + default: + fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version); + return NULL; + } + + initialize_context(ctx, (glsl_es) ? API_OPENGLES2 : API_OPENGL_COMPAT); + + struct gl_shader_program *whole_program; + + whole_program = rzalloc (NULL, struct gl_shader_program); + assert(whole_program != NULL); + whole_program->InfoLog = ralloc_strdup(whole_program, ""); + + /* Created just to avoid segmentation faults */ + whole_program->AttributeBindings = new string_to_uint_map; + whole_program->FragDataBindings = new string_to_uint_map; + whole_program->FragDataIndexBindings = new string_to_uint_map; + + for (unsigned i = 0; i < num_files; i++) { + whole_program->Shaders = + reralloc(whole_program, whole_program->Shaders, + struct gl_shader *, whole_program->NumShaders + 1); + assert(whole_program->Shaders != NULL); + + struct gl_shader *shader = rzalloc(whole_program, gl_shader); + + whole_program->Shaders[whole_program->NumShaders] = shader; + whole_program->NumShaders++; + + const unsigned len = strlen(files[i]); + if (len < 6) + goto fail; + + const char *const ext = & files[i][len - 5]; + /* TODO add support to read a .shader_test */ + if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0) + shader->Type = GL_VERTEX_SHADER; + else if (strncmp(".tesc", ext, 5) == 0) + shader->Type = GL_TESS_CONTROL_SHADER; + else if (strncmp(".tese", ext, 5) == 0) + shader->Type = GL_TESS_EVALUATION_SHADER; + else if (strncmp(".geom", ext, 5) == 0) + shader->Type = GL_GEOMETRY_SHADER; + else if (strncmp(".frag", ext, 5) == 0) + shader->Type = GL_FRAGMENT_SHADER; + else if (strncmp(".comp", ext, 5) == 0) + shader->Type = GL_COMPUTE_SHADER; + else + goto fail; + shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type); + + shader->Source = load_text_file(whole_program, files[i]); + if (shader->Source == NULL) { + printf("File \"%s\" does not exist.\n", files[i]); + exit(EXIT_FAILURE); + } + + compile_shader(ctx, shader); + + if (strlen(shader->InfoLog) > 0) + printf("Info log for %s:\n%s\n", files[i], shader->InfoLog); + + if (!shader->CompileStatus) { + status = EXIT_FAILURE; + break; + } + } + + if ((status == EXIT_SUCCESS) && options->do_link) { + _mesa_clear_shader_program_data(whole_program); + + link_shaders(ctx, whole_program); + status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; + + if (strlen(whole_program->InfoLog) > 0) + printf("Info log for linking:\n%s\n", whole_program->InfoLog); + + for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) { + struct gl_shader *shader = whole_program->_LinkedShaders[i]; + + if (!shader) + continue; + + shader->Program = rzalloc(shader, gl_program); + init_gl_program(shader->Program, shader->Type); + } + } + + return whole_program; + +fail: + ralloc_free(whole_program); + return NULL; +} + +extern "C" void +standalone_compiler_cleanup(struct gl_shader_program *whole_program) +{ + for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) + ralloc_free(whole_program->_LinkedShaders[i]); + + delete whole_program->AttributeBindings; + delete whole_program->FragDataBindings; + delete whole_program->FragDataIndexBindings; + + ralloc_free(whole_program); + _mesa_glsl_release_types(); + _mesa_glsl_release_builtin_functions(); +} diff --git a/src/compiler/glsl/standalone.h b/src/compiler/glsl/standalone.h new file mode 100644 index 00000000000..5b387c52d28 --- /dev/null +++ b/src/compiler/glsl/standalone.h @@ -0,0 +1,51 @@ +/* + * Copyright © 2016 Red Hat + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef GLSL_STANDALONE_H +#define GLSL_STANDALONE_H + +#ifdef __cplusplus +extern "C" { +#endif + +struct standalone_options { + int glsl_version; + int dump_ast; + int dump_hir; + int dump_lir; + int do_link; +}; + +struct gl_shader_program; + +struct gl_shader_program * standalone_compile_shader( + const struct standalone_options *options, + unsigned num_files, char* const* files); + +void standalone_compiler_cleanup(struct gl_shader_program *prog); + +#ifdef __cplusplus +} +#endif + +#endif /* GLSL_STANDALONE_H */ |