glsl: lower mediump temporaries to 16 bits except structures (v2)
[mesa.git] / src / mesa / main / shader_query.cpp
index 81277b6175c715d2cd4fcfe737c51b8ff1f3a42e..9a16a28d39337e6b4d52b36f905b319c368d0d3a 100644 (file)
@@ -29,7 +29,6 @@
  */
 
 #include "main/context.h"
-#include "main/core.h"
 #include "main/enums.h"
 #include "main/shaderapi.h"
 #include "main/shaderobj.h"
@@ -37,9 +36,8 @@
 #include "compiler/glsl/glsl_symbol_table.h"
 #include "compiler/glsl/ir.h"
 #include "compiler/glsl/program.h"
-#include "program/hash_table.h"
-#include "util/strndup.h"
-
+#include "compiler/glsl/string_to_uint_map.h"
+#include "util/mesa-sha1.h"
 
 static GLint
 program_resource_location(struct gl_program_resource *res,
@@ -63,29 +61,26 @@ DECL_RESOURCE_FUNC(XFV, gl_transform_feedback_varying_info);
 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
 
-void GLAPIENTRY
-_mesa_BindAttribLocation(GLuint program, GLuint index,
-                         const GLchar *name)
+static void
+bind_attrib_location(struct gl_context *ctx,
+                     struct gl_shader_program *const shProg, GLuint index,
+                     const GLchar *name, bool no_error)
 {
-   GET_CURRENT_CONTEXT(ctx);
-
-   struct gl_shader_program *const shProg =
-      _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
-   if (!shProg)
-      return;
-
    if (!name)
       return;
 
-   if (strncmp(name, "gl_", 3) == 0) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glBindAttribLocation(illegal name)");
-      return;
-   }
+   if (!no_error) {
+      if (strncmp(name, "gl_", 3) == 0) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindAttribLocation(illegal name)");
+         return;
+      }
 
-   if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
-      return;
+      if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(%u >= %u)",
+                     index, ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs);
+         return;
+      }
    }
 
    /* Replace the current value if it's already in the list.  Add
@@ -100,6 +95,31 @@ _mesa_BindAttribLocation(GLuint program, GLuint index,
     */
 }
 
+void GLAPIENTRY
+_mesa_BindAttribLocation_no_error(GLuint program, GLuint index,
+                                  const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program(ctx, program);
+   bind_attrib_location(ctx, shProg, index, name, true);
+}
+
+void GLAPIENTRY
+_mesa_BindAttribLocation(GLuint program, GLuint index,
+                         const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
+   if (!shProg)
+      return;
+
+   bind_attrib_location(ctx, shProg, index, name, false);
+}
+
 void GLAPIENTRY
 _mesa_GetActiveAttrib(GLuint program, GLuint desired_index,
                       GLsizei maxLength, GLsizei * length, GLint * size,
@@ -117,7 +137,7 @@ _mesa_GetActiveAttrib(GLuint program, GLuint desired_index,
    if (!shProg)
       return;
 
-   if (!shProg->LinkStatus) {
+   if (!shProg->data->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_VALUE,
                   "glGetActiveAttrib(program not linked)");
       return;
@@ -164,7 +184,7 @@ _mesa_GetAttribLocation(GLuint program, const GLchar * name)
       return -1;
    }
 
-   if (!shProg->LinkStatus) {
+   if (!shProg->data->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetAttribLocation(program not linked)");
       return -1;
@@ -192,14 +212,15 @@ _mesa_GetAttribLocation(GLuint program, const GLchar * name)
 unsigned
 _mesa_count_active_attribs(struct gl_shader_program *shProg)
 {
-   if (!shProg->LinkStatus
+   if (!shProg->data->LinkStatus
        || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
       return 0;
    }
 
-   struct gl_program_resource *res = shProg->ProgramResourceList;
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
    unsigned count = 0;
-   for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+   for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
+        j++, res++) {
       if (res->Type == GL_PROGRAM_INPUT &&
           res->StageReferences & (1 << MESA_SHADER_VERTEX))
          count++;
@@ -211,26 +232,54 @@ _mesa_count_active_attribs(struct gl_shader_program *shProg)
 size_t
 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
 {
-   if (!shProg->LinkStatus
+   if (!shProg->data->LinkStatus
        || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
       return 0;
    }
 
-   struct gl_program_resource *res = shProg->ProgramResourceList;
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
    size_t longest = 0;
-   for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
+   for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
+        j++, res++) {
       if (res->Type == GL_PROGRAM_INPUT &&
           res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
 
-          const size_t length = strlen(RESOURCE_VAR(res)->name);
-          if (length >= longest)
-             longest = length + 1;
+         /* From the ARB_gl_spirv spec:
+          *
+          *   "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the
+          *    longest active attribute name, including a null terminator, is
+          *    returned.  If no active attributes exist, zero is returned. If
+          *    no name reflection information is available, one is returned."
+          */
+         const size_t length = RESOURCE_VAR(res)->name != NULL ?
+            strlen(RESOURCE_VAR(res)->name) : 0;
+
+         if (length >= longest)
+            longest = length + 1;
       }
    }
 
    return longest;
 }
 
+void static
+bind_frag_data_location(struct gl_shader_program *const shProg,
+                        const char *name, unsigned colorNumber,
+                        unsigned index)
+{
+   /* 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);
+   shProg->FragDataIndexBindings->put(index, name);
+
+   /*
+    * Note that this binding won't go into effect until
+    * glLinkProgram is called again.
+    */
+}
+
 void GLAPIENTRY
 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
                           const GLchar *name)
@@ -238,6 +287,21 @@ _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
    _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
 }
 
+void GLAPIENTRY
+_mesa_BindFragDataLocation_no_error(GLuint program, GLuint colorNumber,
+                                    const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   if (!name)
+      return;
+
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program(ctx, program);
+
+   bind_frag_data_location(shProg, name, colorNumber, 0);
+}
+
 void GLAPIENTRY
 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
                                   GLuint index, const GLchar *name)
@@ -272,17 +336,22 @@ _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
       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);
-   shProg->FragDataIndexBindings->put(index, name);
-   /*
-    * Note that this binding won't go into effect until
-    * glLinkProgram is called again.
-    */
+   bind_frag_data_location(shProg, name, colorNumber, index);
+}
+
+void GLAPIENTRY
+_mesa_BindFragDataLocationIndexed_no_error(GLuint program, GLuint colorNumber,
+                                           GLuint index, const GLchar *name)
+{
+   GET_CURRENT_CONTEXT(ctx);
 
+   if (!name)
+      return;
+
+   struct gl_shader_program *const shProg =
+      _mesa_lookup_shader_program(ctx, program);
+
+   bind_frag_data_location(shProg, name, colorNumber, index);
 }
 
 GLint GLAPIENTRY
@@ -296,7 +365,7 @@ _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
       return -1;
    }
 
-   if (!shProg->LinkStatus) {
+   if (!shProg->data->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetFragDataIndex(program not linked)");
       return -1;
@@ -331,7 +400,7 @@ _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
       return -1;
    }
 
-   if (!shProg->LinkStatus) {
+   if (!shProg->data->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetFragDataLocation(program not linked)");
       return -1;
@@ -392,7 +461,7 @@ _mesa_program_resource_name(struct gl_program_resource *res)
    case GL_TESS_EVALUATION_SUBROUTINE:
       return RESOURCE_SUB(res)->name;
    default:
-      assert(!"support for resource type not implemented");
+      break;
    }
    return NULL;
 }
@@ -458,6 +527,51 @@ valid_array_index(const GLchar *name, unsigned *array_index)
    return true;
 }
 
+static uint32_t
+compute_resource_key(GLenum programInterface, const char *name)
+{
+   struct mesa_sha1 ctx;
+   unsigned char sha1[20];
+
+   _mesa_sha1_init(&ctx);
+   _mesa_sha1_update(&ctx, &programInterface, sizeof(programInterface));
+   _mesa_sha1_update(&ctx, name, strlen(name));
+   _mesa_sha1_final(&ctx, sha1);
+
+   return _mesa_hash_data(sha1, sizeof(sha1));
+}
+
+static struct gl_program_resource *
+search_resource_hash(struct gl_shader_program *shProg,
+                     GLenum programInterface, const char *name,
+                     unsigned *array_index)
+{
+   const char *base_name_end;
+   long index = parse_program_resource_name(name, &base_name_end);
+   char *name_copy;
+
+   /* If dealing with array, we need to get the basename. */
+   if (index >= 0) {
+      name_copy = (char *) malloc(base_name_end - name + 1);
+      memcpy(name_copy, name, base_name_end - name);
+      name_copy[base_name_end - name] = '\0';
+   } else {
+      name_copy = (char*) name;
+   }
+
+   uint32_t key = compute_resource_key(programInterface, name_copy);
+   struct gl_program_resource *res = (struct gl_program_resource *)
+      _mesa_hash_table_u64_search(shProg->data->ProgramResourceHash, key);
+
+   if (name_copy != name)
+      free(name_copy);
+
+   if (res && array_index)
+      *array_index = index >= 0 ? index : 0;
+
+   return res;
+}
+
 /* Find a program resource with specific name in given interface.
  */
 struct gl_program_resource *
@@ -465,13 +579,30 @@ _mesa_program_resource_find_name(struct gl_shader_program *shProg,
                                  GLenum programInterface, const char *name,
                                  unsigned *array_index)
 {
-   struct gl_program_resource *res = shProg->ProgramResourceList;
-   for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+   struct gl_program_resource *res = NULL;
+
+   if (name == NULL)
+      return NULL;
+
+   /* If we have a name, try the ProgramResourceHash first. */
+   if (shProg->data->ProgramResourceHash)
+      res = search_resource_hash(shProg, programInterface, name, array_index);
+
+   if (res)
+      return res;
+
+   res = shProg->data->ProgramResourceList;
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
       if (res->Type != programInterface)
          continue;
 
       /* Resource basename. */
       const char *rname = _mesa_program_resource_name(res);
+
+      /* Since ARB_gl_spirv lack of name reflections is a possibility */
+      if (rname == NULL)
+         continue;
+
       unsigned baselen = strlen(rname);
       unsigned baselen_without_array_index = baselen;
       const char *rname_last_square_bracket = strrchr(rname, '[');
@@ -505,7 +636,7 @@ _mesa_program_resource_find_name(struct gl_shader_program *shProg,
       if (rname_last_square_bracket) {
          baselen_without_array_index -= strlen(rname_last_square_bracket);
          rname_has_array_index_zero =
-            (strncmp(rname_last_square_bracket, "[0]\0", 4) == 0) &&
+            (strcmp(rname_last_square_bracket, "[0]") == 0) &&
             (baselen_without_array_index == strlen(name));
       }
 
@@ -563,16 +694,129 @@ _mesa_program_resource_find_name(struct gl_shader_program *shProg,
    return NULL;
 }
 
+/* Find an uniform or buffer variable program resource with an specific offset
+ * inside a block with an specific binding.
+ *
+ * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
+ */
+static struct gl_program_resource *
+program_resource_find_binding_offset(struct gl_shader_program *shProg,
+                                     GLenum programInterface,
+                                     const GLuint binding,
+                                     const GLint offset)
+{
+
+   /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
+   GLenum blockInterface;
+
+   switch (programInterface) {
+   case GL_BUFFER_VARIABLE:
+      blockInterface = GL_SHADER_STORAGE_BLOCK;
+      break;
+   case GL_UNIFORM:
+      blockInterface = GL_UNIFORM_BLOCK;
+      break;
+   default:
+      assert("Invalid program interface");
+      return NULL;
+   }
+
+   int block_index = -1;
+   int starting_index = -1;
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
+
+   /* Blocks are added to the resource list in the same order that they are
+    * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
+    * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
+    * the resource list.
+    */
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
+      if (res->Type != blockInterface)
+         continue;
+
+      /* Store the first index where a resource of the specific interface is. */
+      if (starting_index == -1)
+         starting_index = i;
+
+      const struct gl_uniform_block *block = RESOURCE_UBO(res);
+
+      if (block->Binding == binding) {
+         /* For arrays, or arrays of arrays of blocks, we want the resource
+          * for the block with base index. Most properties for members of each
+          * block are inherited from the block with the base index, including
+          * a uniform being active or not.
+          */
+         block_index = i - starting_index - block->linearized_array_index;
+         break;
+      }
+   }
+
+   if (block_index == -1)
+      return NULL;
+
+   /* We now look for the resource corresponding to the uniform or buffer
+    * variable using the BLOCK_INDEX and OFFSET.
+    */
+   res = shProg->data->ProgramResourceList;
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
+      if (res->Type != programInterface)
+         continue;
+
+      const struct gl_uniform_storage *uniform = RESOURCE_UNI(res);
+
+      if (uniform->block_index == block_index && uniform->offset == offset) {
+         return res;
+      }
+   }
+
+   return NULL;
+}
+
+/* Checks if an uniform or buffer variable is in the active program resource
+ * list.
+ *
+ * It takes into accout that for variables coming from SPIR-V binaries their
+ * names could not be available (ARB_gl_spirv). In that case, it will use the
+ * the offset and the block binding to locate the resource.
+ *
+ * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
+ */
+struct gl_program_resource *
+_mesa_program_resource_find_active_variable(struct gl_shader_program *shProg,
+                                            GLenum programInterface,
+                                            const gl_uniform_block *block,
+                                            unsigned index)
+{
+   struct gl_program_resource *res;
+   struct gl_uniform_buffer_variable uni = block->Uniforms[index];
+
+   assert(programInterface == GL_UNIFORM ||
+          programInterface == GL_BUFFER_VARIABLE);
+
+   if (uni.IndexName) {
+      res = _mesa_program_resource_find_name(shProg, programInterface, uni.IndexName,
+                                             NULL);
+   } else {
+      /* As the resource has no associated name (ARB_gl_spirv),
+       * we can use the UBO/SSBO binding and offset to find it.
+       */
+      res = program_resource_find_binding_offset(shProg, programInterface,
+                                                 block->Binding, uni.Offset);
+   }
+
+   return res;
+}
+
 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)
+   for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
+      if (&shProg->data->ProgramResourceList[i] == res)
          return index;
-      if (shProg->ProgramResourceList[i].Type == res->Type)
+      if (shProg->data->ProgramResourceList[i].Type == res->Type)
          index++;
    }
    return GL_INVALID_INDEX;
@@ -590,7 +834,7 @@ _mesa_program_resource_index(struct gl_shader_program *shProg,
 
    switch (res->Type) {
    case GL_ATOMIC_COUNTER_BUFFER:
-      return RESOURCE_ATC(res) - shProg->AtomicBuffers;
+      return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
    case GL_VERTEX_SUBROUTINE:
    case GL_GEOMETRY_SUBROUTINE:
    case GL_FRAGMENT_SUBROUTINE:
@@ -613,8 +857,9 @@ _mesa_program_resource_index(struct gl_shader_program *shProg,
 static struct gl_program_resource*
 program_resource_find_data(struct gl_shader_program *shProg, void *data)
 {
-   struct gl_program_resource *res = shProg->ProgramResourceList;
-   for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
+        i++, res++) {
       if (res->Data == data)
          return res;
    }
@@ -627,10 +872,11 @@ struct gl_program_resource *
 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
                                   GLenum programInterface, GLuint index)
 {
-   struct gl_program_resource *res = shProg->ProgramResourceList;
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
    int idx = -1;
 
-   for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
+        i++, res++) {
       if (res->Type != programInterface)
          continue;
 
@@ -685,31 +931,14 @@ _mesa_program_resource_find_index(struct gl_shader_program *shProg,
  * ambiguous in this regard.  However, either name can later be passed
  * to glGetUniformLocation (and related APIs), so there shouldn't be any
  * harm in always appending "[0]" to uniform array names.
- *
- * Geometry shader stage has different naming convention where the 'normal'
- * condition is an array, therefore for variables referenced in geometry
- * stage we do not add '[0]'.
- *
- * Note, that TCS outputs and TES inputs should not have index appended
- * either.
  */
 static bool
 add_index_to_name(struct gl_program_resource *res)
 {
-   bool add_index = !((res->Type == GL_PROGRAM_INPUT &&
-                       res->StageReferences & (1 << MESA_SHADER_GEOMETRY |
-                                               1 << MESA_SHADER_TESS_CTRL |
-                                               1 << MESA_SHADER_TESS_EVAL)) ||
-                      (res->Type == GL_PROGRAM_OUTPUT &&
-                       res->StageReferences & 1 << MESA_SHADER_TESS_CTRL));
-
    /* Transform feedback varyings have array index already appended
     * in their names.
     */
-   if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
-      add_index = false;
-
-   return add_index;
+   return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
 }
 
 /* Get name length of a program resource. This consists of
@@ -718,7 +947,15 @@ add_index_to_name(struct gl_program_resource *res)
 extern unsigned
 _mesa_program_resource_name_len(struct gl_program_resource *res)
 {
-   unsigned length = strlen(_mesa_program_resource_name(res));
+   const char* name = _mesa_program_resource_name(res);
+
+   /* For shaders constructed from SPIR-V binaries, variables may not
+    * have names associated with them.
+    */
+   if (!name)
+      return 0;
+
+   unsigned length = strlen(name);
    if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
       length += 3;
    return length;
@@ -759,7 +996,11 @@ _mesa_get_program_resource_name(struct gl_shader_program *shProg,
 
    _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
 
-   if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
+   /* The resource name can be NULL for shaders constructed from SPIR-V
+    * binaries. In that case, we do not add the '[0]'.
+    */
+   if (name && name[0] != '\0' &&
+       _mesa_program_resource_array_size(res) && add_index_to_name(res)) {
       int i;
 
       /* The comparison is strange because *length does *NOT* include the
@@ -812,7 +1053,7 @@ program_resource_location(struct gl_program_resource *res, unsigned array_index)
       *     "A valid name cannot be a structure, an array of structures, or any
       *     portion of a single vector or a matrix."
       */
-      if (RESOURCE_UNI(res)->type->without_array()->is_record())
+      if (RESOURCE_UNI(res)->type->without_array()->is_struct())
          return -1;
 
       /* From the GL_ARB_uniform_buffer_object spec:
@@ -866,6 +1107,23 @@ _mesa_program_resource_location(struct gl_shader_program *shProg,
    return program_resource_location(res, array_index);
 }
 
+static GLint
+_get_resource_location_index(struct gl_program_resource *res)
+{
+   /* Non-existent variable or resource is not referenced by fragment stage. */
+   if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
+      return -1;
+
+   /* From OpenGL 4.5 spec, 7.3 Program Objects
+    * "The value -1 will be returned by either command...
+    *  ... or if name identifies an active variable that does not have a
+    * valid location assigned.
+    */
+   if (RESOURCE_VAR(res)->location == -1)
+      return -1;
+   return RESOURCE_VAR(res)->index;
+}
+
 /**
  * Function implements following index queries:
  *    glGetFragDataIndex
@@ -877,11 +1135,7 @@ _mesa_program_resource_location_index(struct gl_shader_program *shProg,
    struct gl_program_resource *res =
       _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
 
-   /* Non-existent variable or resource is not referenced by fragment stage. */
-   if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
-      return -1;
-
-   return RESOURCE_VAR(res)->index;
+   return _get_resource_location_index(res);
 }
 
 static uint8_t
@@ -923,10 +1177,10 @@ is_resource_referenced(struct gl_shader_program *shProg,
       return RESOURCE_ATC(res)->StageReferences[stage];
 
    if (res->Type == GL_UNIFORM_BLOCK)
-      return shProg->UniformBlocks[index].stageref & (1 << stage);
+      return shProg->data->UniformBlocks[index].stageref & (1 << stage);
 
    if (res->Type == GL_SHADER_STORAGE_BLOCK)
-      return shProg->ShaderStorageBlocks[index].stageref & (1 << stage);
+      return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
 
    return res->StageReferences & (1 << stage);
 }
@@ -954,10 +1208,13 @@ get_buffer_property(struct gl_shader_program *shProg,
       case GL_NUM_ACTIVE_VARIABLES:
          *val = 0;
          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
-            const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
             struct gl_program_resource *uni =
-               _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
-                                                NULL);
+               _mesa_program_resource_find_active_variable(
+                  shProg,
+                  GL_UNIFORM,
+                  RESOURCE_UBO(res),
+                  i);
+
             if (!uni)
                continue;
             (*val)++;
@@ -966,10 +1223,13 @@ get_buffer_property(struct gl_shader_program *shProg,
       case GL_ACTIVE_VARIABLES: {
          unsigned num_values = 0;
          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
-            const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
             struct gl_program_resource *uni =
-               _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
-                                                NULL);
+               _mesa_program_resource_find_active_variable(
+                  shProg,
+                  GL_UNIFORM,
+                  RESOURCE_UBO(res),
+                  i);
+
             if (!uni)
                continue;
             *val++ =
@@ -990,10 +1250,13 @@ get_buffer_property(struct gl_shader_program *shProg,
       case GL_NUM_ACTIVE_VARIABLES:
          *val = 0;
          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
-            const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
             struct gl_program_resource *uni =
-               _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
-                                                iname, NULL);
+               _mesa_program_resource_find_active_variable(
+                  shProg,
+                  GL_BUFFER_VARIABLE,
+                  RESOURCE_UBO(res),
+                  i);
+
             if (!uni)
                continue;
             (*val)++;
@@ -1002,10 +1265,13 @@ get_buffer_property(struct gl_shader_program *shProg,
       case GL_ACTIVE_VARIABLES: {
          unsigned num_values = 0;
          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
-            const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
             struct gl_program_resource *uni =
-               _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
-                                                iname, NULL);
+               _mesa_program_resource_find_active_variable(
+                  shProg,
+                  GL_BUFFER_VARIABLE,
+                  RESOURCE_UBO(res),
+                  i);
+
             if (!uni)
                continue;
             *val++ =
@@ -1035,7 +1301,7 @@ get_buffer_property(struct gl_shader_program *shProg,
             unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
             struct gl_program_resource *uni =
                program_resource_find_data(shProg,
-                                          &shProg->UniformStorage[idx]);
+                                          &shProg->data->UniformStorage[idx]);
             assert(uni);
             *val++ = _mesa_program_resource_index(shProg, uni);
          }
@@ -1050,10 +1316,10 @@ get_buffer_property(struct gl_shader_program *shProg,
          *val = RESOURCE_XFB(res)->NumVaryings;
          return 1;
       case GL_ACTIVE_VARIABLES:
-         int i = 0;
-         for ( ; i < shProg->LinkedTransformFeedback.NumVarying; i++) {
-            unsigned index =
-               shProg->LinkedTransformFeedback.Varyings[i].BufferIndex;
+         struct gl_transform_feedback_info *linked_xfb =
+            shProg->last_vert_prog->sh.LinkedTransformFeedback;
+         for (int i = 0; i < linked_xfb->NumVarying; i++) {
+            unsigned index = linked_xfb->Varyings[i].BufferIndex;
             struct gl_program_resource *buf_res =
                _mesa_program_resource_find_index(shProg,
                                                  GL_TRANSFORM_FEEDBACK_BUFFER,
@@ -1143,7 +1409,7 @@ _mesa_program_resource_prop(struct gl_shader_program *shProg,
          *val = MAX2(_mesa_program_resource_array_size(res), 1);
          return 1;
       case GL_TRANSFORM_FEEDBACK_VARYING:
-         *val = MAX2(RESOURCE_XFV(res)->Size, 1);
+         *val = RESOURCE_XFV(res)->Size;
          return 1;
       default:
          goto invalid_operation;
@@ -1233,12 +1499,17 @@ _mesa_program_resource_prop(struct gl_shader_program *shProg,
       default:
          goto invalid_operation;
       }
-   case GL_LOCATION_INDEX:
+   case GL_LOCATION_INDEX: {
+      int tmp;
       if (res->Type != GL_PROGRAM_OUTPUT)
          goto invalid_operation;
-      *val = RESOURCE_VAR(res)->index;
+      tmp = program_resource_location(res, 0);
+      if (tmp == -1)
+         *val = -1;
+      else
+         *val = _get_resource_location_index(res);
       return 1;
-
+   }
    case GL_NUM_COMPATIBLE_SUBROUTINES:
       if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
           res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
@@ -1251,7 +1522,7 @@ _mesa_program_resource_prop(struct gl_shader_program *shProg,
       return 1;
    case GL_COMPATIBLE_SUBROUTINES: {
       const struct gl_uniform_storage *uni;
-      struct gl_shader *sh;
+      struct gl_program *p;
       unsigned count, i;
       int j;
 
@@ -1264,10 +1535,10 @@ _mesa_program_resource_prop(struct gl_shader_program *shProg,
          goto invalid_operation;
       uni = RESOURCE_UNI(res);
 
-      sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
+      p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
       count = 0;
-      for (i = 0; i < sh->NumSubroutineFunctions; i++) {
-         struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
+      for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
+         struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
          for (j = 0; j < fn->num_compat_types; j++) {
             if (fn->types[j] == uni->type) {
                val[count++] = i;
@@ -1371,19 +1642,22 @@ _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
 }
 
 static bool
-validate_io(struct gl_shader_program *producer,
-            struct gl_shader_program *consumer)
+validate_io(struct gl_program *producer, struct gl_program *consumer)
 {
-   if (producer == consumer)
+   if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
       return true;
 
-   if (!producer->IsES && !consumer->IsES)
-      return true;
+   const bool producer_is_array_stage =
+      producer->info.stage == MESA_SHADER_TESS_CTRL;
+   const bool consumer_is_array_stage =
+      consumer->info.stage == MESA_SHADER_GEOMETRY ||
+      consumer->info.stage == MESA_SHADER_TESS_CTRL ||
+      consumer->info.stage == MESA_SHADER_TESS_EVAL;
 
    bool valid = true;
 
    gl_shader_variable const **outputs =
-      (gl_shader_variable const **) calloc(producer->NumProgramResourceList,
+      (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
                                            sizeof(gl_shader_variable *));
    if (outputs == NULL)
       return false;
@@ -1406,8 +1680,9 @@ validate_io(struct gl_shader_program *producer,
     * some output that did not have an input.
     */
    unsigned num_outputs = 0;
-   for (unsigned i = 0; i < producer->NumProgramResourceList; i++) {
-      struct gl_program_resource *res = &producer->ProgramResourceList[i];
+   for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
+      struct gl_program_resource *res =
+         &producer->sh.data->ProgramResourceList[i];
 
       if (res->Type != GL_PROGRAM_OUTPUT)
          continue;
@@ -1426,8 +1701,9 @@ validate_io(struct gl_shader_program *producer,
    }
 
    unsigned match_index = 0;
-   for (unsigned i = 0; i < consumer->NumProgramResourceList; i++) {
-      struct gl_program_resource *res = &consumer->ProgramResourceList[i];
+   for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
+      struct gl_program_resource *res =
+         &consumer->sh.data->ProgramResourceList[i];
 
       if (res->Type != GL_PROGRAM_INPUT)
          continue;
@@ -1489,6 +1765,56 @@ validate_io(struct gl_shader_program *producer,
       if (match_index < num_outputs)
          outputs[match_index] = outputs[num_outputs];
 
+      /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
+       *
+       *    "Tessellation control shader per-vertex output variables and
+       *     blocks and tessellation control, tessellation evaluation, and
+       *     geometry shader per-vertex input variables and blocks are
+       *     required to be declared as arrays, with each element representing
+       *     input or output values for a single vertex of a multi-vertex
+       *     primitive. For the purposes of interface matching, such variables
+       *     and blocks are treated as though they were not declared as
+       *     arrays."
+       *
+       * So we unwrap those types before matching.
+       */
+      const glsl_type *consumer_type = consumer_var->type;
+      const glsl_type *consumer_interface_type = consumer_var->interface_type;
+      const glsl_type *producer_type = producer_var->type;
+      const glsl_type *producer_interface_type = producer_var->interface_type;
+
+      if (consumer_is_array_stage) {
+         if (consumer_interface_type) {
+            /* the interface is the array; the underlying types should match */
+            if (consumer_interface_type->is_array() && !consumer_var->patch)
+               consumer_interface_type = consumer_interface_type->fields.array;
+         } else {
+            if (consumer_type->is_array() && !consumer_var->patch)
+               consumer_type = consumer_type->fields.array;
+         }
+      }
+
+      if (producer_is_array_stage) {
+         if (producer_interface_type) {
+            /* the interface is the array; the underlying types should match */
+            if (producer_interface_type->is_array() && !producer_var->patch)
+               producer_interface_type = producer_interface_type->fields.array;
+         } else {
+            if (producer_type->is_array() && !producer_var->patch)
+               producer_type = producer_type->fields.array;
+         }
+      }
+
+      if (producer_type != consumer_type) {
+         valid = false;
+         goto out;
+      }
+
+      if (producer_interface_type != consumer_interface_type) {
+         valid = false;
+         goto out;
+      }
+
       /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
        *
        *    Qualifier Class|  Qualifier  |in/out
@@ -1519,19 +1845,23 @@ validate_io(struct gl_shader_program *producer,
        * Note that location mismatches are detected by the loops above that
        * find the producer variable that goes with the consumer variable.
        */
-      if (producer_var->type != consumer_var->type ||
-          producer_var->interpolation != consumer_var->interpolation ||
-          producer_var->precision != consumer_var->precision) {
+      unsigned producer_interpolation = producer_var->interpolation;
+      unsigned consumer_interpolation = consumer_var->interpolation;
+      if (producer_interpolation == INTERP_MODE_NONE)
+         producer_interpolation = INTERP_MODE_SMOOTH;
+      if (consumer_interpolation == INTERP_MODE_NONE)
+         consumer_interpolation = INTERP_MODE_SMOOTH;
+      if (producer_interpolation != consumer_interpolation) {
          valid = false;
          goto out;
       }
 
-      if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
+      if (producer_var->precision != consumer_var->precision) {
          valid = false;
          goto out;
       }
 
-      if (producer_var->interface_type != consumer_var->interface_type) {
+      if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
          valid = false;
          goto out;
       }
@@ -1548,28 +1878,27 @@ validate_io(struct gl_shader_program *producer,
 extern "C" bool
 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
 {
-   struct gl_shader_program **shProg =
-      (struct gl_shader_program **) pipeline->CurrentProgram;
+   struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
 
    /* Find first active stage in pipeline. */
    unsigned idx, prev = 0;
    for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
-      if (shProg[idx]) {
+      if (prog[idx]) {
          prev = idx;
          break;
       }
    }
 
    for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
-      if (shProg[idx]) {
+      if (prog[idx]) {
          /* Pipeline might include both non-compute and a compute program, do
           * not attempt to validate varyings between non-compute and compute
           * stage.
           */
-         if (shProg[idx]->_LinkedShaders[idx]->Stage == MESA_SHADER_COMPUTE)
+         if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
             break;
 
-         if (!validate_io(shProg[prev], shProg[idx]))
+         if (!validate_io(prog[prev], prog[idx]))
             return false;
 
          prev = idx;
@@ -1577,3 +1906,23 @@ _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
    }
    return true;
 }
+
+extern "C" void
+_mesa_create_program_resource_hash(struct gl_shader_program *shProg)
+{
+   /* Rebuild resource hash. */
+   if (shProg->data->ProgramResourceHash)
+      _mesa_hash_table_u64_destroy(shProg->data->ProgramResourceHash, NULL);
+
+   shProg->data->ProgramResourceHash = _mesa_hash_table_u64_create(shProg);
+
+   struct gl_program_resource *res = shProg->data->ProgramResourceList;
+   for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
+      const char *name = _mesa_program_resource_name(res);
+      if (name) {
+         uint32_t key = compute_resource_key(res->Type, name);
+         _mesa_hash_table_u64_insert(shProg->data->ProgramResourceHash, key,
+                                     res);
+      }
+   }
+}