mesa: Only complain about an infinite loop in a swrast program once.
[mesa.git] / src / glsl / main.cpp
index e78af42ac6592daab3da20ab2fb1e9e5b93aed0a..2ecf57f8ce2e3f1567363ecf5b1b1666e64754a0 100644 (file)
 #include "ir_print_visitor.h"
 #include "program.h"
 
+extern "C" struct gl_shader *
+_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
+
+/* Copied from shader_api.c for the stand-alone compiler.
+ */
+struct gl_shader *
+_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
+{
+   struct gl_shader *shader;
+   assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
+   shader = talloc_zero(NULL, struct gl_shader);
+   if (shader) {
+      shader->Type = type;
+      shader->Name = name;
+      shader->RefCount = 1;
+   }
+   return shader;
+}
+
 /* Returned string will have 'ctx' as its talloc owner. */
 static char *
 load_text_file(void *ctx, const char *file_name)
@@ -103,6 +122,7 @@ void
 compile_shader(struct gl_shader *shader)
 {
    struct _mesa_glsl_parse_state *state;
+   struct gl_extensions ext;
 
    state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state);
 
@@ -117,17 +137,33 @@ compile_shader(struct gl_shader *shader)
    state->symbols = new(shader) glsl_symbol_table;
    state->info_log = talloc_strdup(shader, "");
    state->error = false;
-   state->temp_index = 0;
    state->loop_or_switch_nesting = NULL;
    state->ARB_texture_rectangle_enable = true;
 
+   memset(&ext, 0, sizeof(ext));
+   state->extensions = &ext;
+   /* 1.10 minimums. */
+   state->Const.MaxLights = 8;
+   state->Const.MaxClipPlanes = 8;
+   state->Const.MaxTextureUnits = 2;
+
+   /* More than the 1.10 minimum to appease parser tests taken from
+    * apps that (hopefully) already checked the number of coords.
+    */
+   state->Const.MaxTextureCoords = 4;
+
+   state->Const.MaxVertexAttribs = 16;
+   state->Const.MaxVertexUniformComponents = 512;
+   state->Const.MaxVaryingFloats = 32;
+   state->Const.MaxVertexTextureImageUnits = 0;
+   state->Const.MaxCombinedTextureImageUnits = 2;
+   state->Const.MaxTextureImageUnits = 2;
+   state->Const.MaxFragmentUniformComponents = 64;
+
    state->Const.MaxDrawBuffers = 2;
 
-   /* Create a new context for the preprocessor output.  Ultimately, this
-    * should probably be the parser context, but there isn't one yet.
-   */
    const char *source = shader->Source;
-   state->error = preprocess(shader, &source, &state->info_log);
+   state->error = preprocess(state, &source, &state->info_log, &ext);
 
    if (!state->error) {
       _mesa_glsl_lexer_ctor(state, source);
@@ -147,10 +183,9 @@ compile_shader(struct gl_shader *shader)
    if (!state->error && !state->translation_unit.is_empty())
       _mesa_ast_to_hir(shader->ir, state);
 
-   validate_ir_tree(shader->ir);
-
    /* Print out the unoptimized IR. */
    if (!state->error && dump_hir) {
+      validate_ir_tree(shader->ir);
       _mesa_print_ir(shader->ir, state);
    }
 
@@ -168,11 +203,13 @@ compile_shader(struct gl_shader *shader)
         progress = do_constant_variable_unlinked(shader->ir) || progress;
         progress = do_constant_folding(shader->ir) || progress;
         progress = do_vec_index_to_swizzle(shader->ir) || progress;
+        progress = do_vec_index_to_cond_assign(shader->ir) || progress;
         progress = do_swizzle_swizzle(shader->ir) || progress;
       } while (progress);
+
+      validate_ir_tree(shader->ir);
    }
 
-   validate_ir_tree(shader->ir);
 
    /* Print out the resulting IR */
    if (!state->error && dump_lir) {
@@ -181,12 +218,19 @@ compile_shader(struct gl_shader *shader)
 
    shader->symbols = state->symbols;
    shader->CompileStatus = !state->error;
+   shader->Version = state->language_version;
+   memcpy(shader->builtins_to_link, state->builtins_to_link,
+         sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
+   shader->num_builtins_to_link = state->num_builtins_to_link;
 
    if (shader->InfoLog)
       talloc_free(shader->InfoLog);
 
    shader->InfoLog = state->info_log;
 
+   /* Retain any live IR, but trash the rest. */
+   reparent_ir(shader->ir, shader);
+
    talloc_free(state);
 
    return;
@@ -206,9 +250,9 @@ main(int argc, char **argv)
    if (argc <= optind)
       usage_fail(argv[0]);
 
-   struct glsl_program *whole_program;
+   struct gl_shader_program *whole_program;
 
-   whole_program = talloc_zero (NULL, struct glsl_program);
+   whole_program = talloc_zero (NULL, struct gl_shader_program);
    assert(whole_program != NULL);
 
    for (/* empty */; argc > optind; optind++) {
@@ -254,10 +298,17 @@ main(int argc, char **argv)
    if ((status == EXIT_SUCCESS) && do_link)  {
       link_shaders(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 < whole_program->_NumLinkedShaders; i++)
+      talloc_free(whole_program->_LinkedShaders[i]);
+
    talloc_free(whole_program);
    _mesa_glsl_release_types();
+   _mesa_glsl_release_functions();
 
    return status;
 }