mesa: glGetProgramResourceIndex
authorTapani Pälli <tapani.palli@intel.com>
Tue, 10 Mar 2015 07:30:30 +0000 (09:30 +0200)
committerTapani Pälli <tapani.palli@intel.com>
Thu, 16 Apr 2015 04:55:56 +0000 (07:55 +0300)
Patch adds required helper functions to shaderapi.h and
the actual implementation.

v2: code cleanup (Ilia Mirkin)

corresponding Piglit test:
   arb_program_interface_query-getprogramresourceindex

Signed-off-by: Tapani Pälli <tapani.palli@intel.com>
Reviewed-by: Martin Peres <martin.peres@linux.intel.org>
src/mesa/main/program_resource.c
src/mesa/main/shader_query.cpp
src/mesa/main/shaderapi.h

index ae28a26d3e44e77ee0c006a6fb69926a8d01d305..7a2732503a98e720e962e601a53445896ad458b7 100644 (file)
@@ -150,11 +150,94 @@ _mesa_GetProgramInterfaceiv(GLuint program, GLenum programInterface,
    }
 }
 
+static bool
+is_xfb_marker(const char *str)
+{
+   static const char *markers[] = {
+      "gl_NextBuffer",
+      "gl_SkipComponents1",
+      "gl_SkipComponents2",
+      "gl_SkipComponents3",
+      "gl_SkipComponents4",
+      NULL
+   };
+   const char **m = markers;
+
+   if (strncmp(str, "gl_", 3) != 0)
+      return false;
+
+   for (; *m; m++)
+      if (strcmp(*m, str) == 0)
+         return true;
+
+   return false;
+}
+
+/**
+ * Checks if given name index is legal for GetProgramResourceIndex,
+ * check is written to be compatible with GL_ARB_array_of_arrays.
+ */
+static bool
+valid_program_resource_index_name(const GLchar *name)
+{
+   const char *array = strstr(name, "[");
+   const char *close = strrchr(name, ']');
+
+   /* Not array, no need for the check. */
+   if (!array)
+      return true;
+
+   /* Last array index has to be zero. */
+   if (!close || *--close != '0')
+      return false;
+
+   return true;
+}
+
 GLuint GLAPIENTRY
 _mesa_GetProgramResourceIndex(GLuint program, GLenum programInterface,
                               const GLchar *name)
 {
-   return 0;
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_program_resource *res;
+   struct gl_shader_program *shProg =
+      _mesa_lookup_shader_program_err(ctx, program,
+                                      "glGetProgramResourceIndex");
+   if (!shProg || !name)
+      return GL_INVALID_INDEX;
+
+   /*
+    * For the interface TRANSFORM_FEEDBACK_VARYING, the value INVALID_INDEX
+    * should be returned when querying the index assigned to the special names
+    * "gl_NextBuffer", "gl_SkipComponents1", "gl_SkipComponents2",
+    * "gl_SkipComponents3", and "gl_SkipComponents4".
+    */
+   if (programInterface == GL_TRANSFORM_FEEDBACK_VARYING &&
+       is_xfb_marker(name))
+      return GL_INVALID_INDEX;
+
+   switch (programInterface) {
+   case GL_PROGRAM_INPUT:
+   case GL_PROGRAM_OUTPUT:
+   case GL_UNIFORM:
+   case GL_UNIFORM_BLOCK:
+   case GL_TRANSFORM_FEEDBACK_VARYING:
+      /* Validate name syntax for arrays. */
+      if (!valid_program_resource_index_name(name))
+         return GL_INVALID_INDEX;
+
+      res = _mesa_program_resource_find_name(shProg, programInterface, name);
+      if (!res)
+         return GL_INVALID_INDEX;
+
+      return _mesa_program_resource_index(shProg, res);
+   case GL_ATOMIC_COUNTER_BUFFER:
+   default:
+      _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramResourceIndex(%s)",
+                  _mesa_lookup_enum_by_nr(programInterface));
+   }
+
+   return GL_INVALID_INDEX;
 }
 
 void GLAPIENTRY
index 4e0247ea817dd1b24e269c7ecbdcfdc5b0247e8d..61eec68683f97671aafa87ffa7a523ea22cb4f2a 100644 (file)
@@ -557,3 +557,94 @@ _mesa_program_resource_array_size(struct gl_program_resource *res)
    }
    return 0;
 }
+
+static int
+array_index_of_resource(struct gl_program_resource *res,
+                        const char *name)
+{
+   assert(res->Data);
+
+   switch (res->Type) {
+   case GL_PROGRAM_INPUT:
+   case GL_PROGRAM_OUTPUT:
+      return get_matching_index(RESOURCE_VAR(res), name);
+   default:
+      assert(!"support for resource type not implemented");
+   }
+}
+
+/* Find a program resource with specific name in given interface.
+ */
+struct gl_program_resource *
+_mesa_program_resource_find_name(struct gl_shader_program *shProg,
+                                 GLenum interface, const char *name)
+{
+   struct gl_program_resource *res = shProg->ProgramResourceList;
+   for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+      if (res->Type != interface)
+         continue;
+
+      /* Resource basename. */
+      const char *rname = _mesa_program_resource_name(res);
+      unsigned baselen = strlen(rname);
+
+      switch (interface) {
+      case GL_TRANSFORM_FEEDBACK_VARYING:
+      case GL_UNIFORM_BLOCK:
+      case GL_UNIFORM:
+         if (strncmp(rname, name, baselen) == 0) {
+            /* Basename match, check if array or struct. */
+            if (name[baselen] == '\0' ||
+                name[baselen] == '[' ||
+                name[baselen] == '.') {
+               return res;
+            }
+         }
+         break;
+      case GL_PROGRAM_INPUT:
+      case GL_PROGRAM_OUTPUT:
+         if (array_index_of_resource(res, name) >= 0)
+            return res;
+         break;
+      default:
+         assert(!"not implemented for given interface");
+      }
+   }
+   return NULL;
+}
+
+static GLuint
+calc_resource_index(struct gl_shader_program *shProg,
+                    struct gl_program_resource *res)
+{
+   unsigned i;
+   GLuint index = 0;
+   for (i = 0; i < shProg->NumProgramResourceList; i++) {
+      if (&shProg->ProgramResourceList[i] == res)
+         return index;
+      if (shProg->ProgramResourceList[i].Type == res->Type)
+         index++;
+   }
+   return GL_INVALID_INDEX;
+}
+
+/**
+ * Calculate index for the given resource.
+ */
+GLuint
+_mesa_program_resource_index(struct gl_shader_program *shProg,
+                             struct gl_program_resource *res)
+{
+   if (!res)
+      return GL_INVALID_INDEX;
+
+   switch (res->Type) {
+   case GL_UNIFORM_BLOCK:
+      return RESOURCE_UBO(res)- shProg->UniformBlocks;
+   case GL_ATOMIC_COUNTER_BUFFER:
+      return RESOURCE_ATC(res) - shProg->AtomicBuffers;
+   case GL_TRANSFORM_FEEDBACK_VARYING:
+   default:
+      return calc_resource_index(shProg, res);
+   }
+}
index 6db52f798b83c32fe4e68af2d91598cf9e56267b..d80252b460ac4878aa4e46bb929e52b666faaa7c 100644 (file)
@@ -226,6 +226,14 @@ _mesa_program_resource_name(struct gl_program_resource *res);
 extern unsigned
 _mesa_program_resource_array_size(struct gl_program_resource *res);
 
+extern GLuint
+_mesa_program_resource_index(struct gl_shader_program *shProg,
+                             struct gl_program_resource *res);
+
+extern struct gl_program_resource *
+_mesa_program_resource_find_name(struct gl_shader_program *shProg,
+                                 GLenum interface, const char *name);
+
 #ifdef __cplusplus
 }
 #endif