glsl: Refactor AST-to-HIR code handling variable initializers
[mesa.git] / src / glsl / glsl_parser_extras.cpp
index 4ac062b42c1ec7a9c24791dfd825c93e7703d460..e8c60936fb6877c0722307522d7eb280b78dd649 100644 (file)
 #include <assert.h>
 
 extern "C" {
-#include <talloc.h>
-#include "main/core.h" /* for struct __GLcontextRec */
+#include "main/core.h" /* for struct gl_context */
 }
 
+#include "ralloc.h"
 #include "ast.h"
 #include "glsl_parser_extras.h"
 #include "glsl_parser.h"
 #include "ir_optimization.h"
 #include "loop_analysis.h"
 
-_mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
+_mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *ctx,
                                               GLenum target, void *mem_ctx)
 {
    switch (target) {
@@ -48,7 +48,7 @@ _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
    this->scanner = NULL;
    this->translation_unit.make_empty();
    this->symbols = new(mem_ctx) glsl_symbol_table;
-   this->info_log = talloc_strdup(mem_ctx, "");
+   this->info_log = ralloc_strdup(mem_ctx, "");
    this->error = false;
    this->loop_or_switch_nesting = NULL;
 
@@ -79,6 +79,38 @@ _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
    this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
 
    this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
+
+   /* Note: Once the OpenGL 3.0 'forward compatible' context or the OpenGL 3.2
+    * Core context is supported, this logic will need change.  Older versions of
+    * GLSL are no longer supported outside the compatibility contexts of 3.x.
+    */
+   this->Const.GLSL_100ES = (ctx->API == API_OPENGLES2)
+      || ctx->Extensions.ARB_ES2_compatibility;
+   this->Const.GLSL_110 = (ctx->API == API_OPENGL);
+   this->Const.GLSL_120 = (ctx->API == API_OPENGL)
+      && (ctx->Const.GLSLVersion >= 120);
+   this->Const.GLSL_130 = (ctx->API == API_OPENGL)
+      && (ctx->Const.GLSLVersion >= 130);
+
+   const unsigned lowest_version =
+      (ctx->API == API_OPENGLES2) || ctx->Extensions.ARB_ES2_compatibility
+      ? 100 : 110;
+   const unsigned highest_version =
+      (ctx->API == API_OPENGL) ? ctx->Const.GLSLVersion : 100;
+   char *supported = ralloc_strdup(this, "");
+
+   for (unsigned ver = lowest_version; ver <= highest_version; ver += 10) {
+      const char *const prefix = (ver == lowest_version)
+        ? ""
+        : ((ver == highest_version) ? ", and " : ", ");
+
+      ralloc_asprintf_append(& supported, "%s%d.%02d%s",
+                            prefix,
+                            ver / 100, ver % 100,
+                            (ver == 100) ? " ES" : "");
+   }
+
+   this->supported_version_string = supported;
 }
 
 const char *
@@ -88,7 +120,6 @@ _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
    case vertex_shader:   return "vertex";
    case fragment_shader: return "fragment";
    case geometry_shader: return "geometry";
-   case ir_shader:       break;
    }
 
    assert(!"Should not get here.");
@@ -105,15 +136,14 @@ _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
    state->error = true;
 
    assert(state->info_log != NULL);
-   state->info_log = talloc_asprintf_append(state->info_log,
-                                           "%u:%u(%u): error: ",
+   ralloc_asprintf_append(&state->info_log, "%u:%u(%u): error: ",
                                            locp->source,
                                            locp->first_line,
                                            locp->first_column);
    va_start(ap, fmt);
-   state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
+   ralloc_vasprintf_append(&state->info_log, fmt, ap);
    va_end(ap);
-   state->info_log = talloc_strdup_append(state->info_log, "\n");
+   ralloc_strcat(&state->info_log, "\n");
 }
 
 
@@ -124,15 +154,14 @@ _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
    va_list ap;
 
    assert(state->info_log != NULL);
-   state->info_log = talloc_asprintf_append(state->info_log,
-                                           "%u:%u(%u): warning: ",
+   ralloc_asprintf_append(&state->info_log, "%u:%u(%u): warning: ",
                                            locp->source,
                                            locp->first_line,
                                            locp->first_column);
    va_start(ap, fmt);
-   state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
+   ralloc_vasprintf_append(&state->info_log, fmt, ap);
    va_end(ap);
-   state->info_log = talloc_strdup_append(state->info_log, "\n");
+   ralloc_strcat(&state->info_log, "\n");
 }
 
 
@@ -181,6 +210,22 @@ _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
         state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
         state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
       }
+   } else if (strcmp(name, "GL_ARB_draw_instanced") == 0) {
+      /* This extension is only supported in vertex shaders.
+       */
+      if (state->target != vertex_shader) {
+        unsupported = true;
+      } else {
+        state->ARB_draw_instanced_enable = (ext_mode != extension_disable);
+        state->ARB_draw_instanced_warn = (ext_mode == extension_warn);
+      }
+   } else if (strcmp(name, "GL_ARB_explicit_attrib_location") == 0) {
+      state->ARB_explicit_attrib_location_enable =
+        (ext_mode != extension_disable);
+      state->ARB_explicit_attrib_location_warn =
+        (ext_mode == extension_warn);
+
+      unsupported = !state->extensions->ARB_explicit_attrib_location;
    } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
       state->ARB_fragment_coord_conventions_enable =
         (ext_mode != extension_disable);
@@ -196,6 +241,26 @@ _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
       state->EXT_texture_array_warn = (ext_mode == extension_warn);
 
       unsupported = !state->extensions->EXT_texture_array;
+   } else if (strcmp(name, "GL_ARB_shader_stencil_export") == 0) {
+      if (state->target != fragment_shader) {
+        unsupported = true;
+      } else {
+        state->ARB_shader_stencil_export_enable = (ext_mode != extension_disable);
+        state->ARB_shader_stencil_export_warn = (ext_mode == extension_warn);
+        unsupported = !state->extensions->ARB_shader_stencil_export;
+      }
+   } else if (strcmp(name, "GL_AMD_conservative_depth") == 0) {
+      /* The AMD_conservative spec does not forbid requiring the extension in
+       * the vertex shader.
+       */
+      state->AMD_conservative_depth_enable = (ext_mode != extension_disable);
+      state->AMD_conservative_depth_warn = (ext_mode == extension_warn);
+      unsupported = !state->extensions->AMD_conservative_depth;
+   } else if (strcmp(name, "GL_OES_texture_3D") == 0 && state->es_shader) {
+      state->OES_texture_3D_enable = (ext_mode != extension_disable);
+      state->OES_texture_3D_warn = (ext_mode == extension_warn);
+
+      unsupported = !state->extensions->EXT_texture3D;
    } else {
       unsupported = true;
    }
@@ -219,37 +284,37 @@ _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
 void
 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
 {
-   if (q->constant)
+   if (q->flags.q.constant)
       printf("const ");
 
-   if (q->invariant)
+   if (q->flags.q.invariant)
       printf("invariant ");
 
-   if (q->attribute)
+   if (q->flags.q.attribute)
       printf("attribute ");
 
-   if (q->varying)
+   if (q->flags.q.varying)
       printf("varying ");
 
-   if (q->in && q->out) 
+   if (q->flags.q.in && q->flags.q.out)
       printf("inout ");
    else {
-      if (q->in)
+      if (q->flags.q.in)
         printf("in ");
 
-      if (q->out)
+      if (q->flags.q.out)
         printf("out ");
    }
 
-   if (q->centroid)
+   if (q->flags.q.centroid)
       printf("centroid ");
-   if (q->uniform)
+   if (q->flags.q.uniform)
       printf("uniform ");
-   if (q->smooth)
+   if (q->flags.q.smooth)
       printf("smooth ");
-   if (q->flat)
+   if (q->flags.q.flat)
       printf("flat ");
-   if (q->noperspective)
+   if (q->flags.q.noperspective)
       printf("noperspective ");
 }
 
@@ -680,6 +745,11 @@ ast_struct_specifier::print(void) const
 ast_struct_specifier::ast_struct_specifier(char *identifier,
                                           ast_node *declarator_list)
 {
+   if (identifier == NULL) {
+      static unsigned anon_count = 1;
+      identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
+      anon_count++;
+   }
    name = identifier;
    this->declarations.push_degenerate_list_at_head(&declarator_list->link);
 }
@@ -689,7 +759,7 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
 {
    GLboolean progress = GL_FALSE;
 
-   progress = do_sub_to_add_neg(ir) || progress;
+   progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
 
    if (linked) {
       progress = do_function_inlining(ir) || progress;
@@ -697,7 +767,9 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
    }
    progress = do_structure_splitting(ir) || progress;
    progress = do_if_simplification(ir) || progress;
+   progress = do_discard_simplification(ir) || progress;
    progress = do_copy_propagation(ir) || progress;
+   /*progress = do_copy_propagation_elements(ir) || progress;*/
    if (linked)
       progress = do_dead_code(ir) || progress;
    else
@@ -719,8 +791,10 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
    progress = optimize_redundant_jumps(ir) || progress;
 
    loop_state *ls = analyze_loop_variables(ir);
-   progress = set_loop_controls(ir, ls) || progress;
-   progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
+   if (ls->loop_found) {
+      progress = set_loop_controls(ir, ls) || progress;
+      progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
+   }
    delete ls;
 
    return progress;