glsl: Refactor AST-to-HIR code handling variable initializers
[mesa.git] / src / glsl / linker.cpp
index d7638facce66ce8fc8a13b53dd3df01435549199..7db5c5e8d5313b4c5040bad5caeb53b51862c235 100644 (file)
  *
  * \author Ian Romanick <ian.d.romanick@intel.com>
  */
-#include <cstdlib>
-#include <cstdio>
-#include <cstdarg>
-#include <climits>
-
-extern "C" {
-#include <talloc.h>
-}
 
 #include "main/core.h"
 #include "glsl_symbol_table.h"
@@ -176,9 +168,9 @@ linker_error_printf(gl_shader_program *prog, const char *fmt, ...)
 {
    va_list ap;
 
-   prog->InfoLog = talloc_strdup_append(prog->InfoLog, "error: ");
+   ralloc_strcat(&prog->InfoLog, "error: ");
    va_start(ap, fmt);
-   prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, ap);
+   ralloc_vasprintf_append(&prog->InfoLog, fmt, ap);
    va_end(ap);
 }
 
@@ -303,6 +295,7 @@ mode_string(const ir_variable *var)
    case ir_var_out:     return "shader output";
    case ir_var_inout:   return "shader inout";
 
+   case ir_var_const_in:
    case ir_var_temporary:
    default:
       assert(!"Should not get here.");
@@ -360,11 +353,8 @@ cross_validate_globals(struct gl_shader_program *prog,
                   && (var->type->fields.array == existing->type->fields.array)
                   && ((var->type->length == 0)
                       || (existing->type->length == 0))) {
-                 if (existing->type->length == 0) {
+                 if (var->type->length != 0) {
                     existing->type = var->type;
-                    existing->max_array_access =
-                       MAX2(existing->max_array_access,
-                            var->max_array_access);
                  }
               } else {
                  linker_error_printf(prog, "%s `%s' declared as type "
@@ -389,6 +379,32 @@ cross_validate_globals(struct gl_shader_program *prog,
               existing->explicit_location = true;
            }
 
+        /* Validate layout qualifiers for gl_FragDepth.
+         *
+         * From the AMD_conservative_depth spec:
+         *    "If gl_FragDepth is redeclared in any fragment shader in
+         *    a program, it must be redeclared in all fragment shaders in that
+         *    program that have static assignments to gl_FragDepth. All
+         *    redeclarations of gl_FragDepth in all fragment shaders in
+         *    a single program must have the same set of qualifiers."
+         */
+        if (strcmp(var->name, "gl_FragDepth") == 0) {
+           bool layout_declared = var->depth_layout != ir_depth_layout_none;
+           bool layout_differs = var->depth_layout != existing->depth_layout;
+           if (layout_declared && layout_differs) {
+              linker_error_printf(prog,
+                 "All redeclarations of gl_FragDepth in all fragment shaders "
+                 "in a single program must have the same set of qualifiers.");
+           }
+           if (var->used && layout_differs) {
+              linker_error_printf(prog,
+                    "If gl_FragDepth is redeclared with a layout qualifier in"
+                    "any fragment shader, it must be redeclared with the same"
+                    "layout qualifier in all fragment shaders that have"
+                    "assignments to gl_FragDepth");
+           }
+        }
+
            /* FINISHME: Handle non-constant initializers.
             */
            if (var->constant_value != NULL) {
@@ -413,7 +429,7 @@ cross_validate_globals(struct gl_shader_program *prog,
                   * FINISHME: will fail.
                   */
                  existing->constant_value =
-                    var->constant_value->clone(talloc_parent(existing), NULL);
+                    var->constant_value->clone(ralloc_parent(existing), NULL);
            }
 
            if (existing->invariant != var->invariant) {
@@ -422,6 +438,12 @@ cross_validate_globals(struct gl_shader_program *prog,
                                   mode_string(var), var->name);
               return false;
            }
+            if (existing->centroid != var->centroid) {
+               linker_error_printf(prog, "declarations for %s `%s' have "
+                                   "mismatching centroid qualifiers\n",
+                                   mode_string(var), var->name);
+               return false;
+            }
         } else
            variables.add_variable(var);
       }
@@ -487,14 +509,35 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
         /* Check that the types match between stages.
          */
         if (input->type != output->type) {
-           linker_error_printf(prog,
-                               "%s shader output `%s' declared as "
-                               "type `%s', but %s shader input declared "
-                               "as type `%s'\n",
-                               producer_stage, output->name,
-                               output->type->name,
-                               consumer_stage, input->type->name);
-           return false;
+           /* There is a bit of a special case for gl_TexCoord.  This
+            * built-in is unsized by default.  Appliations that variable
+            * access it must redeclare it with a size.  There is some
+            * language in the GLSL spec that implies the fragment shader
+            * and vertex shader do not have to agree on this size.  Other
+            * driver behave this way, and one or two applications seem to
+            * rely on it.
+            *
+            * Neither declaration needs to be modified here because the array
+            * sizes are fixed later when update_array_sizes is called.
+            *
+            * From page 48 (page 54 of the PDF) of the GLSL 1.10 spec:
+            *
+            *     "Unlike user-defined varying variables, the built-in
+            *     varying variables don't have a strict one-to-one
+            *     correspondence between the vertex language and the
+            *     fragment language."
+            */
+           if (!output->type->is_array()
+               || (strncmp("gl_", output->name, 3) != 0)) {
+              linker_error_printf(prog,
+                                  "%s shader output `%s' declared as "
+                                  "type `%s', but %s shader input declared "
+                                  "as type `%s'\n",
+                                  producer_stage, output->name,
+                                  output->type->name,
+                                  consumer_stage, input->type->name);
+              return false;
+           }
         }
 
         /* Check that all of the qualifiers match between stages.
@@ -867,28 +910,31 @@ link_intrastage_shaders(void *mem_ctx,
 
    free(linking_shaders);
 
-   /* Make a pass over all global variables to ensure that arrays with
+   /* Make a pass over all variable declarations to ensure that arrays with
     * unspecified sizes have a size specified.  The size is inferred from the
     * max_array_access field.
     */
-   foreach_list(node, linked->ir) {
-      ir_variable *const var = ((ir_instruction *) node)->as_variable();
-
-      if (var == NULL)
-        continue;
-
-      if (!var->type->is_array() || (var->type->length != 0))
-        continue;
+   if (linked != NULL) {
+      class array_sizing_visitor : public ir_hierarchical_visitor {
+      public:
+        virtual ir_visitor_status visit(ir_variable *var)
+        {
+           if (var->type->is_array() && (var->type->length == 0)) {
+              const glsl_type *type =
+                 glsl_type::get_array_instance(var->type->fields.array,
+                                               var->max_array_access + 1);
+
+              assert(type != NULL);
+              var->type = type;
+           }
 
-      const glsl_type *type =
-        glsl_type::get_array_instance(var->type->fields.array,
-                                      var->max_array_access);
+           return visit_continue;
+        }
+      } v;
 
-      assert(type != NULL);
-      var->type = type;
+      v.run(linked->ir);
    }
 
-
    return linked;
 }
 
@@ -966,7 +1012,7 @@ add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
    if (type->is_record()) {
       for (unsigned int i = 0; i < type->length; i++) {
         const glsl_type *field_type = type->fields.structure[i].type;
-        char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name,
+        char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
                                            type->fields.structure[i].name);
 
         add_uniform(mem_ctx, uniforms, ht, field_name, field_type,
@@ -982,7 +1028,7 @@ add_uniform(void *mem_ctx, exec_list *uniforms, struct hash_table *ht,
         /* Array of structures. */
         if (array_elem_type->is_record()) {
            for (unsigned int i = 0; i < type->length; i++) {
-              char *elem_name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
+              char *elem_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
               add_uniform(mem_ctx, uniforms, ht, elem_name, array_elem_type,
                           shader_type, next_shader_pos, total_uniforms);
            }
@@ -1044,7 +1090,7 @@ assign_uniform_locations(struct gl_shader_program *prog)
    unsigned total_uniforms = 0;
    hash_table *ht = hash_table_ctor(32, hash_table_string_hash,
                                    hash_table_string_compare);
-   void *mem_ctx = talloc_new(NULL);
+   void *mem_ctx = ralloc_context(NULL);
 
    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
       if (prog->_LinkedShaders[i] == NULL)
@@ -1073,7 +1119,7 @@ assign_uniform_locations(struct gl_shader_program *prog)
       }
    }
 
-   talloc_free(mem_ctx);
+   ralloc_free(mem_ctx);
 
    gl_uniform_list *ul = (gl_uniform_list *)
       calloc(1, sizeof(gl_uniform_list));
@@ -1441,16 +1487,16 @@ assign_varying_locations(struct gl_shader_program *prog,
 void
 link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
 {
-   void *mem_ctx = talloc_init("temporary linker context");
+   void *mem_ctx = ralloc_context(NULL); // temporary linker context
 
    prog->LinkStatus = false;
    prog->Validated = false;
    prog->_Used = false;
 
    if (prog->InfoLog != NULL)
-      talloc_free(prog->InfoLog);
+      ralloc_free(prog->InfoLog);
 
-   prog->InfoLog = talloc_strdup(NULL, "");
+   prog->InfoLog = ralloc_strdup(NULL, "");
 
    /* Separate the shaders into groups based on their type.
     */
@@ -1632,6 +1678,20 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
       demote_shader_inputs_and_outputs(sh, ir_var_in);
    }
 
+   /* OpenGL ES requires that a vertex shader and a fragment shader both be
+    * present in a linked program.  By checking for use of shading language
+    * version 1.00, we also catch the GL_ARB_ES2_compatibility case.
+    */
+   if (ctx->API == API_OPENGLES2 || prog->Version == 100) {
+      if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
+        linker_error_printf(prog, "program lacks a vertex shader\n");
+        prog->LinkStatus = false;
+      } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
+        linker_error_printf(prog, "program lacks a fragment shader\n");
+        prog->LinkStatus = false;
+      }
+   }
+
    /* FINISHME: Assign fragment shader output locations. */
 
 done:
@@ -1645,5 +1705,5 @@ done:
       reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
    }
 
-   talloc_free(mem_ctx);
+   ralloc_free(mem_ctx);
 }