mesa: Fix memory leak in out-of-memory path.
[mesa.git] / src / glsl / linker.cpp
index b54ef41080af20e76a45cd2a0d6b90da5f03085e..a7c38a3426a5f914d638476d698112aa9cc113ad 100644 (file)
@@ -244,7 +244,9 @@ count_attribute_slots(const glsl_type *t)
 
 
 /**
- * Verify that a vertex shader executable meets all semantic requirements
+ * Verify that a vertex shader executable meets all semantic requirements.
+ *
+ * Also sets prog->Vert.UsesClipDistance as a side effect.
  *
  * \param shader  Vertex shader executable to be verified
  */
@@ -262,6 +264,26 @@ validate_vertex_shader_executable(struct gl_shader_program *prog,
       return false;
    }
 
+   if (prog->Version >= 130) {
+      /* From section 7.1 (Vertex Shader Special Variables) of the
+       * GLSL 1.30 spec:
+       *
+       *   "It is an error for a shader to statically write both
+       *   gl_ClipVertex and gl_ClipDistance."
+       */
+      find_assignment_visitor clip_vertex("gl_ClipVertex");
+      find_assignment_visitor clip_distance("gl_ClipDistance");
+
+      clip_vertex.run(shader->ir);
+      clip_distance.run(shader->ir);
+      if (clip_vertex.variable_found() && clip_distance.variable_found()) {
+         linker_error(prog, "vertex shader writes to both `gl_ClipVertex' "
+                      "and `gl_ClipDistance'\n");
+         return false;
+      }
+      prog->Vert.UsesClipDistance = clip_distance.variable_found();
+   }
+
    return true;
 }
 
@@ -395,7 +417,7 @@ cross_validate_globals(struct gl_shader_program *prog,
 
         /* Validate layout qualifiers for gl_FragDepth.
          *
-         * From the AMD_conservative_depth spec:
+         * From the AMD/ARB_conservative_depth specs:
          *    "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
@@ -1276,71 +1298,6 @@ assign_attribute_or_color_locations(gl_shader_program *prog,
 
    invalidate_variable_locations(sh, direction, generic_base);
 
-   if ((target_index == MESA_SHADER_VERTEX) && (prog->Attributes != NULL)) {
-      for (unsigned i = 0; i < prog->Attributes->NumParameters; i++) {
-        ir_variable *const var =
-           sh->symbols->get_variable(prog->Attributes->Parameters[i].Name);
-
-        /* Note: attributes that occupy multiple slots, such as arrays or
-         * matrices, may appear in the attrib array multiple times.
-         */
-        if ((var == NULL) || (var->location != -1))
-           continue;
-
-        /* From page 61 of the OpenGL 4.0 spec:
-         *
-         *     "LinkProgram will fail if the attribute bindings assigned by
-         *     BindAttribLocation do not leave not enough space to assign a
-         *     location for an active matrix attribute or an active attribute
-         *     array, both of which require multiple contiguous generic
-         *     attributes."
-         *
-         * Previous versions of the spec contain similar language but omit the
-         * bit about attribute arrays.
-         *
-         * Page 61 of the OpenGL 4.0 spec also says:
-         *
-         *     "It is possible for an application to bind more than one
-         *     attribute name to the same location. This is referred to as
-         *     aliasing. This will only work if only one of the aliased
-         *     attributes is active in the executable program, or if no path
-         *     through the shader consumes more than one attribute of a set
-         *     of attributes aliased to the same location. A link error can
-         *     occur if the linker determines that every path through the
-         *     shader consumes multiple aliased attributes, but
-         *     implementations are not required to generate an error in this
-         *     case."
-         *
-         * These two paragraphs are either somewhat contradictory, or I don't
-         * fully understand one or both of them.
-         */
-        /* FINISHME: The code as currently written does not support attribute
-         * FINISHME: location aliasing (see comment above).
-         */
-        const int attr = prog->Attributes->Parameters[i].StateIndexes[0];
-        const unsigned slots = count_attribute_slots(var->type);
-
-        /* Mask representing the contiguous slots that will be used by this
-         * attribute.
-         */
-        const unsigned use_mask = (1 << slots) - 1;
-
-        /* Generate a link error if the set of bits requested for this
-         * attribute overlaps any previously allocated bits.
-         */
-        if ((~(use_mask << attr) & used_locations) != used_locations) {
-           linker_error(prog,
-                        "insufficient contiguous attribute locations "
-                        "available for vertex shader input `%s'",
-                        var->name);
-           return false;
-        }
-
-        var->location = VERT_ATTRIB_GENERIC0 + attr;
-        used_locations |= (use_mask << attr);
-      }
-   }
-
    /* Temporary storage for the set of attributes that need locations assigned.
     */
    struct temp_attr {
@@ -1367,28 +1324,86 @@ assign_attribute_or_color_locations(gl_shader_program *prog,
         continue;
 
       if (var->explicit_location) {
-        const unsigned slots = count_attribute_slots(var->type);
-        const unsigned use_mask = (1 << slots) - 1;
-        const int attr = var->location - generic_base;
-
         if ((var->location >= (int)(max_index + generic_base))
             || (var->location < 0)) {
            linker_error(prog,
                         "invalid explicit location %d specified for `%s'\n",
-                        (var->location < 0) ? var->location : attr,
+                        (var->location < 0)
+                        ? var->location : var->location - generic_base,
                         var->name);
            return false;
-        } else if (var->location >= generic_base) {
-           used_locations |= (use_mask << attr);
+        }
+      } else if (target_index == MESA_SHADER_VERTEX) {
+        unsigned binding;
+
+        if (prog->AttributeBindings->get(binding, var->name)) {
+           assert(binding >= VERT_ATTRIB_GENERIC0);
+           var->location = binding;
         }
       }
 
-      /* The location was explicitly assigned, nothing to do here.
+      /* If the variable is not a built-in and has a location statically
+       * assigned in the shader (presumably via a layout qualifier), make sure
+       * that it doesn't collide with other assigned locations.  Otherwise,
+       * add it to the list of variables that need linker-assigned locations.
        */
-      if (var->location != -1)
+      const unsigned slots = count_attribute_slots(var->type);
+      if (var->location != -1) {
+        if (var->location >= generic_base) {
+           /* From page 61 of the OpenGL 4.0 spec:
+            *
+            *     "LinkProgram will fail if the attribute bindings assigned
+            *     by BindAttribLocation do not leave not enough space to
+            *     assign a location for an active matrix attribute or an
+            *     active attribute array, both of which require multiple
+            *     contiguous generic attributes."
+            *
+            * Previous versions of the spec contain similar language but omit
+            * the bit about attribute arrays.
+            *
+            * Page 61 of the OpenGL 4.0 spec also says:
+            *
+            *     "It is possible for an application to bind more than one
+            *     attribute name to the same location. This is referred to as
+            *     aliasing. This will only work if only one of the aliased
+            *     attributes is active in the executable program, or if no
+            *     path through the shader consumes more than one attribute of
+            *     a set of attributes aliased to the same location. A link
+            *     error can occur if the linker determines that every path
+            *     through the shader consumes multiple aliased attributes,
+            *     but implementations are not required to generate an error
+            *     in this case."
+            *
+            * These two paragraphs are either somewhat contradictory, or I
+            * don't fully understand one or both of them.
+            */
+           /* FINISHME: The code as currently written does not support
+            * FINISHME: attribute location aliasing (see comment above).
+            */
+           /* Mask representing the contiguous slots that will be used by
+            * this attribute.
+            */
+           const unsigned attr = var->location - generic_base;
+           const unsigned use_mask = (1 << slots) - 1;
+
+           /* Generate a link error if the set of bits requested for this
+            * attribute overlaps any previously allocated bits.
+            */
+           if ((~(use_mask << attr) & used_locations) != used_locations) {
+              linker_error(prog,
+                           "insufficient contiguous attribute locations "
+                           "available for vertex shader input `%s'",
+                           var->name);
+              return false;
+           }
+
+           used_locations |= (use_mask << attr);
+        }
+
         continue;
+      }
 
-      to_assign[num_attr].slots = count_attribute_slots(var->type);
+      to_assign[num_attr].slots = slots;
       to_assign[num_attr].var = var;
       num_attr++;
    }
@@ -1724,6 +1739,9 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
       if (!prog->LinkStatus)
         goto done;
 
+      if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
+         lower_clip_distance(prog->_LinkedShaders[i]->ir);
+
       while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
         ;
    }
@@ -1787,7 +1805,8 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
     * 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->InternalSeparateShader &&
+       (ctx->API == API_OPENGLES2 || prog->Version == 100)) {
       if (prog->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
         linker_error(prog, "program lacks a vertex shader\n");
       } else if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL) {
@@ -1806,6 +1825,14 @@ done:
 
       /* Retain any live IR, but trash the rest. */
       reparent_ir(prog->_LinkedShaders[i]->ir, prog->_LinkedShaders[i]->ir);
+
+      /* The symbol table in the linked shaders may contain references to
+       * variables that were removed (e.g., unused uniforms).  Since it may
+       * contain junk, there is no possible valid use.  Delete it and set the
+       * pointer to NULL.
+       */
+      delete prog->_LinkedShaders[i]->symbols;
+      prog->_LinkedShaders[i]->symbols = NULL;
    }
 
    ralloc_free(mem_ctx);