mesa: fix potential mem leak in generate_mipmap_compressed()
[mesa.git] / src / mesa / main / shader_query.cpp
index e9e6dbf7122269d3f02c9e44f92ca13e7b1d07ed..8ab18126c9ea6ec345df685609a2e53b14d533ad 100644 (file)
@@ -45,10 +45,6 @@ _mesa_BindAttribLocationARB(GLhandleARB program, GLuint index,
 {
    GET_CURRENT_CONTEXT(ctx);
 
-   const GLint size = -1; /* unknown size */
-   GLint i;
-   GLenum datatype = GL_FLOAT_VEC4;
-
    struct gl_shader_program *const shProg =
       _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
    if (!shProg)
@@ -111,8 +107,7 @@ _mesa_GetActiveAttribARB(GLhandleARB program, GLuint desired_index,
 
       if (var == NULL
          || var->mode != ir_var_in
-         || var->location == -1
-         || var->location < VERT_ATTRIB_GENERIC0)
+         || var->location == -1)
         continue;
 
       if (current_index == desired_index) {
@@ -203,8 +198,7 @@ _mesa_count_active_attribs(struct gl_shader_program *shProg)
 
       if (var == NULL
          || var->mode != ir_var_in
-         || var->location == -1
-         || var->location < VERT_ATTRIB_GENERIC0)
+         || var->location == -1)
         continue;
 
       i++;
@@ -212,3 +206,124 @@ _mesa_count_active_attribs(struct gl_shader_program *shProg)
 
    return i;
 }
+
+
+size_t
+_mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
+{
+   if (!shProg->LinkStatus
+       || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
+      return 0;
+   }
+
+   exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
+   size_t longest = 0;
+
+   foreach_list(node, ir) {
+      const ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+      if (var == NULL
+         || var->mode != ir_var_in
+         || var->location == -1)
+        continue;
+
+      const size_t len = strlen(var->name);
+      if (len >= longest)
+        longest = len + 1;
+   }
+
+   return longest;
+}
+
+void GLAPIENTRY
+_mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
+                          const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocation");
+   if (!shProg)
+      return;
+
+   if (!name)
+      return;
+
+   if (strncmp(name, "gl_", 3) == 0) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glBindFragDataLocation(illegal name)");
+      return;
+   }
+
+   if (colorNumber >= ctx->Const.MaxDrawBuffers) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocation(index)");
+      return;
+   }
+
+   /* Replace the current value if it's already in the list.  Add
+    * FRAG_RESULT_DATA0 because that's how the linker differentiates
+    * between built-in attributes and user-defined attributes.
+    */
+   shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
+
+   /*
+    * Note that this binding won't go into effect until
+    * glLinkProgram is called again.
+    */
+}
+
+GLint GLAPIENTRY
+_mesa_GetFragDataLocation(GLuint program, const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
+
+   if (!shProg) {
+      return -1;
+   }
+
+   if (!shProg->LinkStatus) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetFragDataLocation(program not linked)");
+      return -1;
+   }
+
+   if (!name)
+      return -1;
+
+   if (strncmp(name, "gl_", 3) == 0) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetFragDataLocation(illegal name)");
+      return -1;
+   }
+
+   /* Not having a fragment shader is not an error.
+    */
+   if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
+      return -1;
+
+   exec_list *ir = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
+   foreach_list(node, ir) {
+      const ir_variable *const var = ((ir_instruction *) node)->as_variable();
+
+      /* The extra check against FRAG_RESULT_DATA0 is because
+       * glGetFragDataLocation cannot be used on "conventional" attributes.
+       *
+       * From page 95 of the OpenGL 3.0 spec:
+       *
+       *     "If name is not an active attribute, if name is a conventional
+       *     attribute, or if an error occurs, -1 will be returned."
+       */
+      if (var == NULL
+         || var->mode != ir_var_out
+         || var->location == -1
+         || var->location < FRAG_RESULT_DATA0)
+        continue;
+
+      if (strcmp(var->name, name) == 0)
+        return var->location - FRAG_RESULT_DATA0;
+   }
+
+   return -1;
+}