xref: /aosp_15_r20/external/mesa3d/src/mesa/main/shader_query.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file shader_query.cpp
26  * C-to-C++ bridge functions to query GLSL shader data
27  *
28  * \author Ian Romanick <[email protected]>
29  */
30 
31 #include "main/context.h"
32 #include "main/enums.h"
33 #include "main/shaderapi.h"
34 #include "main/shaderobj.h"
35 #include "main/uniforms.h"
36 #include "compiler/glsl/glsl_symbol_table.h"
37 #include "compiler/glsl/ir.h"
38 #include "compiler/glsl/linker_util.h"
39 #include "compiler/glsl/string_to_uint_map.h"
40 #include "c99_alloca.h"
41 #include "api_exec_decl.h"
42 
43 static GLint
44 program_resource_location(struct gl_program_resource *res,
45                           unsigned array_index);
46 
47 /**
48  * Declare convenience functions to return resource data in a given type.
49  * Warning! this is not type safe so be *very* careful when using these.
50  */
51 #define DECL_RESOURCE_FUNC(name, type) \
52 const type * RESOURCE_ ## name (gl_program_resource *res) { \
53    assert(res->Data); \
54    return (type *) res->Data; \
55 }
56 
57 DECL_RESOURCE_FUNC(VAR, gl_shader_variable);
58 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
59 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
60 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
61 DECL_RESOURCE_FUNC(XFV, gl_transform_feedback_varying_info);
62 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
63 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
64 
65 static GLenum
mediump_to_highp_type(struct gl_shader_program * shProg,GLenum type)66 mediump_to_highp_type(struct gl_shader_program *shProg, GLenum type)
67 {
68    if (!shProg->IsES)
69       return type;
70 
71    switch (type) {
72    case GL_FLOAT16_NV:
73       return GL_FLOAT;
74    case GL_FLOAT16_VEC2_NV:
75       return GL_FLOAT_VEC2;
76    case GL_FLOAT16_VEC3_NV:
77       return GL_FLOAT_VEC3;
78    case GL_FLOAT16_VEC4_NV:
79       return GL_FLOAT_VEC4;
80    case GL_FLOAT16_MAT2_AMD:
81       return GL_FLOAT_MAT2;
82    case GL_FLOAT16_MAT3_AMD:
83       return GL_FLOAT_MAT3;
84    case GL_FLOAT16_MAT4_AMD:
85       return GL_FLOAT_MAT4;
86    case GL_FLOAT16_MAT2x3_AMD:
87       return GL_FLOAT_MAT2x3;
88    case GL_FLOAT16_MAT2x4_AMD:
89       return GL_FLOAT_MAT2x4;
90    case GL_FLOAT16_MAT3x2_AMD:
91       return GL_FLOAT_MAT3x2;
92    case GL_FLOAT16_MAT3x4_AMD:
93       return GL_FLOAT_MAT3x4;
94    case GL_FLOAT16_MAT4x2_AMD:
95       return GL_FLOAT_MAT4x2;
96    case GL_FLOAT16_MAT4x3_AMD:
97       return GL_FLOAT_MAT4x3;
98    default:
99       return type;
100    }
101 }
102 
103 static void
bind_attrib_location(struct gl_context * ctx,struct gl_shader_program * const shProg,GLuint index,const GLchar * name,bool no_error)104 bind_attrib_location(struct gl_context *ctx,
105                      struct gl_shader_program *const shProg, GLuint index,
106                      const GLchar *name, bool no_error)
107 {
108    if (!name)
109       return;
110 
111    if (!no_error) {
112       if (strncmp(name, "gl_", 3) == 0) {
113          _mesa_error(ctx, GL_INVALID_OPERATION,
114                      "glBindAttribLocation(illegal name)");
115          return;
116       }
117 
118       if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
119          _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(%u >= %u)",
120                      index, ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs);
121          return;
122       }
123    }
124 
125    /* Replace the current value if it's already in the list.  Add
126     * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
127     * between built-in attributes and user-defined attributes.
128     */
129    shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
130 
131    /*
132     * Note that this attribute binding won't go into effect until
133     * glLinkProgram is called again.
134     */
135 }
136 
137 void GLAPIENTRY
_mesa_BindAttribLocation_no_error(GLuint program,GLuint index,const GLchar * name)138 _mesa_BindAttribLocation_no_error(GLuint program, GLuint index,
139                                   const GLchar *name)
140 {
141    GET_CURRENT_CONTEXT(ctx);
142 
143    struct gl_shader_program *const shProg =
144       _mesa_lookup_shader_program(ctx, program);
145    bind_attrib_location(ctx, shProg, index, name, true);
146 }
147 
148 void GLAPIENTRY
_mesa_BindAttribLocation(GLuint program,GLuint index,const GLchar * name)149 _mesa_BindAttribLocation(GLuint program, GLuint index,
150                          const GLchar *name)
151 {
152    GET_CURRENT_CONTEXT(ctx);
153 
154    struct gl_shader_program *const shProg =
155       _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
156    if (!shProg)
157       return;
158 
159    bind_attrib_location(ctx, shProg, index, name, false);
160 }
161 
162 void GLAPIENTRY
_mesa_GetActiveAttrib(GLuint program,GLuint desired_index,GLsizei maxLength,GLsizei * length,GLint * size,GLenum * type,GLchar * name)163 _mesa_GetActiveAttrib(GLuint program, GLuint desired_index,
164                       GLsizei maxLength, GLsizei * length, GLint * size,
165                       GLenum * type, GLchar * name)
166 {
167    GET_CURRENT_CONTEXT(ctx);
168    struct gl_shader_program *shProg;
169 
170    if (maxLength < 0) {
171       _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
172       return;
173    }
174 
175    shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
176    if (!shProg)
177       return;
178 
179    if (!shProg->data->LinkStatus) {
180       _mesa_error(ctx, GL_INVALID_VALUE,
181                   "glGetActiveAttrib(program not linked)");
182       return;
183    }
184 
185    if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
186       _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
187       return;
188    }
189 
190    struct gl_program_resource *res =
191       _mesa_program_resource_find_index(shProg, GL_PROGRAM_INPUT,
192                                         desired_index);
193 
194    /* User asked for index that does not exist. */
195    if (!res) {
196       _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
197       return;
198    }
199 
200    const gl_shader_variable *const var = RESOURCE_VAR(res);
201 
202    const char *var_name = var->name.string;
203 
204    _mesa_copy_string(name, maxLength, length, var_name);
205 
206    if (size)
207       _mesa_program_resource_prop(shProg, res, desired_index, GL_ARRAY_SIZE,
208                                   size, false, "glGetActiveAttrib");
209 
210    if (type)
211       _mesa_program_resource_prop(shProg, res, desired_index, GL_TYPE,
212                                   (GLint *) type, false, "glGetActiveAttrib");
213 }
214 
215 GLint GLAPIENTRY
_mesa_GetAttribLocation(GLuint program,const GLchar * name)216 _mesa_GetAttribLocation(GLuint program, const GLchar * name)
217 {
218    GET_CURRENT_CONTEXT(ctx);
219    struct gl_shader_program *const shProg =
220       _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
221 
222    if (!shProg) {
223       return -1;
224    }
225 
226    if (!shProg->data->LinkStatus) {
227       _mesa_error(ctx, GL_INVALID_OPERATION,
228                   "glGetAttribLocation(program not linked)");
229       return -1;
230    }
231 
232    if (!name)
233       return -1;
234 
235    /* Not having a vertex shader is not an error.
236     */
237    if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
238       return -1;
239 
240    unsigned array_index = 0;
241    struct gl_program_resource *res =
242       _mesa_program_resource_find_name(shProg, GL_PROGRAM_INPUT, name,
243                                        &array_index);
244 
245    if (!res)
246       return -1;
247 
248    return program_resource_location(res, array_index);
249 }
250 
251 unsigned
_mesa_count_active_attribs(struct gl_shader_program * shProg)252 _mesa_count_active_attribs(struct gl_shader_program *shProg)
253 {
254    if (!shProg->data->LinkStatus
255        || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
256       return 0;
257    }
258 
259    struct gl_program_resource *res = shProg->data->ProgramResourceList;
260    unsigned count = 0;
261    for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
262         j++, res++) {
263       if (res->Type == GL_PROGRAM_INPUT &&
264           res->StageReferences & (1 << MESA_SHADER_VERTEX))
265          count++;
266    }
267    return count;
268 }
269 
270 
271 size_t
_mesa_longest_attribute_name_length(struct gl_shader_program * shProg)272 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
273 {
274    if (!shProg->data->LinkStatus
275        || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
276       return 0;
277    }
278 
279    struct gl_program_resource *res = shProg->data->ProgramResourceList;
280    size_t longest = 0;
281    for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
282         j++, res++) {
283       if (res->Type == GL_PROGRAM_INPUT &&
284           res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
285 
286          /* From the ARB_gl_spirv spec:
287           *
288           *   "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the
289           *    longest active attribute name, including a null terminator, is
290           *    returned.  If no active attributes exist, zero is returned. If
291           *    no name reflection information is available, one is returned."
292           */
293          const size_t length = RESOURCE_VAR(res)->name.length;
294 
295          if (length >= longest)
296             longest = length + 1;
297       }
298    }
299 
300    return longest;
301 }
302 
303 void static
bind_frag_data_location(struct gl_shader_program * const shProg,const char * name,unsigned colorNumber,unsigned index)304 bind_frag_data_location(struct gl_shader_program *const shProg,
305                         const char *name, unsigned colorNumber,
306                         unsigned index)
307 {
308    /* Replace the current value if it's already in the list.  Add
309     * FRAG_RESULT_DATA0 because that's how the linker differentiates
310     * between built-in attributes and user-defined attributes.
311     */
312    shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
313    shProg->FragDataIndexBindings->put(index, name);
314 
315    /*
316     * Note that this binding won't go into effect until
317     * glLinkProgram is called again.
318     */
319 }
320 
321 void GLAPIENTRY
_mesa_BindFragDataLocation(GLuint program,GLuint colorNumber,const GLchar * name)322 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
323 			   const GLchar *name)
324 {
325    _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
326 }
327 
328 void GLAPIENTRY
_mesa_BindFragDataLocation_no_error(GLuint program,GLuint colorNumber,const GLchar * name)329 _mesa_BindFragDataLocation_no_error(GLuint program, GLuint colorNumber,
330                                     const GLchar *name)
331 {
332    GET_CURRENT_CONTEXT(ctx);
333 
334    if (!name)
335       return;
336 
337    struct gl_shader_program *const shProg =
338       _mesa_lookup_shader_program(ctx, program);
339 
340    bind_frag_data_location(shProg, name, colorNumber, 0);
341 }
342 
343 void GLAPIENTRY
_mesa_BindFragDataLocationIndexed(GLuint program,GLuint colorNumber,GLuint index,const GLchar * name)344 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
345                                   GLuint index, const GLchar *name)
346 {
347    GET_CURRENT_CONTEXT(ctx);
348 
349    struct gl_shader_program *const shProg =
350       _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
351    if (!shProg)
352       return;
353 
354    if (!name)
355       return;
356 
357    if (strncmp(name, "gl_", 3) == 0) {
358       _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
359       return;
360    }
361 
362    if (index > 1) {
363       _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
364       return;
365    }
366 
367    if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
368       _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
369       return;
370    }
371 
372    if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
373       _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
374       return;
375    }
376 
377    bind_frag_data_location(shProg, name, colorNumber, index);
378 }
379 
380 void GLAPIENTRY
_mesa_BindFragDataLocationIndexed_no_error(GLuint program,GLuint colorNumber,GLuint index,const GLchar * name)381 _mesa_BindFragDataLocationIndexed_no_error(GLuint program, GLuint colorNumber,
382                                            GLuint index, const GLchar *name)
383 {
384    GET_CURRENT_CONTEXT(ctx);
385 
386    if (!name)
387       return;
388 
389    struct gl_shader_program *const shProg =
390       _mesa_lookup_shader_program(ctx, program);
391 
392    bind_frag_data_location(shProg, name, colorNumber, index);
393 }
394 
395 GLint GLAPIENTRY
_mesa_GetFragDataIndex(GLuint program,const GLchar * name)396 _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
397 {
398    GET_CURRENT_CONTEXT(ctx);
399    struct gl_shader_program *const shProg =
400       _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
401 
402    if (!shProg) {
403       return -1;
404    }
405 
406    if (!shProg->data->LinkStatus) {
407       _mesa_error(ctx, GL_INVALID_OPERATION,
408                   "glGetFragDataIndex(program not linked)");
409       return -1;
410    }
411 
412    if (!name)
413       return -1;
414 
415    /* Not having a fragment shader is not an error.
416     */
417    if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
418       return -1;
419 
420    return _mesa_program_resource_location_index(shProg, GL_PROGRAM_OUTPUT,
421                                                 name);
422 }
423 
424 GLint GLAPIENTRY
_mesa_GetFragDataLocation(GLuint program,const GLchar * name)425 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
426 {
427    GET_CURRENT_CONTEXT(ctx);
428    struct gl_shader_program *const shProg =
429       _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
430 
431    if (!shProg) {
432       return -1;
433    }
434 
435    if (!shProg->data->LinkStatus) {
436       _mesa_error(ctx, GL_INVALID_OPERATION,
437                   "glGetFragDataLocation(program not linked)");
438       return -1;
439    }
440 
441    if (!name)
442       return -1;
443 
444    /* Not having a fragment shader is not an error.
445     */
446    if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
447       return -1;
448 
449    unsigned array_index = 0;
450    struct gl_program_resource *res =
451       _mesa_program_resource_find_name(shProg, GL_PROGRAM_OUTPUT, name,
452                                        &array_index);
453 
454    if (!res)
455       return -1;
456 
457    return program_resource_location(res, array_index);
458 }
459 
460 const char*
_mesa_program_resource_name(struct gl_program_resource * res)461 _mesa_program_resource_name(struct gl_program_resource *res)
462 {
463    switch (res->Type) {
464    case GL_UNIFORM_BLOCK:
465    case GL_SHADER_STORAGE_BLOCK:
466       return RESOURCE_UBO(res)->name.string;
467    case GL_TRANSFORM_FEEDBACK_VARYING:
468       return RESOURCE_XFV(res)->name.string;
469    case GL_PROGRAM_INPUT:
470    case GL_PROGRAM_OUTPUT:
471       return RESOURCE_VAR(res)->name.string;
472    case GL_UNIFORM:
473    case GL_BUFFER_VARIABLE:
474       return RESOURCE_UNI(res)->name.string;
475    case GL_VERTEX_SUBROUTINE_UNIFORM:
476    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
477    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
478    case GL_COMPUTE_SUBROUTINE_UNIFORM:
479    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
480    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
481       return RESOURCE_UNI(res)->name.string + MESA_SUBROUTINE_PREFIX_LEN;
482    case GL_VERTEX_SUBROUTINE:
483    case GL_GEOMETRY_SUBROUTINE:
484    case GL_FRAGMENT_SUBROUTINE:
485    case GL_COMPUTE_SUBROUTINE:
486    case GL_TESS_CONTROL_SUBROUTINE:
487    case GL_TESS_EVALUATION_SUBROUTINE:
488       return RESOURCE_SUB(res)->name.string;
489    default:
490       break;
491    }
492    return NULL;
493 }
494 
495 int
_mesa_program_resource_name_length(struct gl_program_resource * res)496 _mesa_program_resource_name_length(struct gl_program_resource *res)
497 {
498    switch (res->Type) {
499    case GL_UNIFORM_BLOCK:
500    case GL_SHADER_STORAGE_BLOCK:
501       return RESOURCE_UBO(res)->name.length;
502    case GL_TRANSFORM_FEEDBACK_VARYING:
503       return RESOURCE_XFV(res)->name.length;
504    case GL_PROGRAM_INPUT:
505    case GL_PROGRAM_OUTPUT:
506       return RESOURCE_VAR(res)->name.length;
507    case GL_UNIFORM:
508    case GL_BUFFER_VARIABLE:
509       return RESOURCE_UNI(res)->name.length;
510    case GL_VERTEX_SUBROUTINE_UNIFORM:
511    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
512    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
513    case GL_COMPUTE_SUBROUTINE_UNIFORM:
514    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
515    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
516       return RESOURCE_UNI(res)->name.length - MESA_SUBROUTINE_PREFIX_LEN;
517    case GL_VERTEX_SUBROUTINE:
518    case GL_GEOMETRY_SUBROUTINE:
519    case GL_FRAGMENT_SUBROUTINE:
520    case GL_COMPUTE_SUBROUTINE:
521    case GL_TESS_CONTROL_SUBROUTINE:
522    case GL_TESS_EVALUATION_SUBROUTINE:
523       return RESOURCE_SUB(res)->name.length;
524    default:
525       break;
526    }
527    return 0;
528 }
529 
530 bool
_mesa_program_get_resource_name(struct gl_program_resource * res,struct gl_resource_name * out)531 _mesa_program_get_resource_name(struct gl_program_resource *res,
532                                 struct gl_resource_name *out)
533 {
534    switch (res->Type) {
535    case GL_UNIFORM_BLOCK:
536    case GL_SHADER_STORAGE_BLOCK:
537       *out = RESOURCE_UBO(res)->name;
538       return out->string != NULL;
539    case GL_TRANSFORM_FEEDBACK_VARYING:
540       *out = RESOURCE_XFV(res)->name;
541       return out->string != NULL;
542    case GL_PROGRAM_INPUT:
543    case GL_PROGRAM_OUTPUT:
544       *out = RESOURCE_VAR(res)->name;
545       return out->string != NULL;
546    case GL_UNIFORM:
547    case GL_BUFFER_VARIABLE:
548       *out = RESOURCE_UNI(res)->name;
549       return out->string != NULL;
550    case GL_VERTEX_SUBROUTINE_UNIFORM:
551    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
552    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
553    case GL_COMPUTE_SUBROUTINE_UNIFORM:
554    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
555    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
556       *out = RESOURCE_UNI(res)->name;
557       out->string += MESA_SUBROUTINE_PREFIX_LEN;
558       out->length -= MESA_SUBROUTINE_PREFIX_LEN;
559       assert(out->string); /* always non-NULL */
560       return true;
561    case GL_VERTEX_SUBROUTINE:
562    case GL_GEOMETRY_SUBROUTINE:
563    case GL_FRAGMENT_SUBROUTINE:
564    case GL_COMPUTE_SUBROUTINE:
565    case GL_TESS_CONTROL_SUBROUTINE:
566    case GL_TESS_EVALUATION_SUBROUTINE:
567       *out = RESOURCE_SUB(res)->name;
568       return out->string != NULL;
569    default:
570       return false;
571    }
572 }
573 
574 unsigned
_mesa_program_resource_array_size(struct gl_program_resource * res)575 _mesa_program_resource_array_size(struct gl_program_resource *res)
576 {
577    switch (res->Type) {
578    case GL_TRANSFORM_FEEDBACK_VARYING:
579       return RESOURCE_XFV(res)->Size > 1 ?
580              RESOURCE_XFV(res)->Size : 0;
581    case GL_PROGRAM_INPUT:
582    case GL_PROGRAM_OUTPUT:
583       return RESOURCE_VAR(res)->type->length;
584    case GL_UNIFORM:
585    case GL_VERTEX_SUBROUTINE_UNIFORM:
586    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
587    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
588    case GL_COMPUTE_SUBROUTINE_UNIFORM:
589    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
590    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
591       return RESOURCE_UNI(res)->array_elements;
592    case GL_BUFFER_VARIABLE:
593       /* Unsized arrays */
594       if (RESOURCE_UNI(res)->array_stride > 0 &&
595           RESOURCE_UNI(res)->array_elements == 0)
596          return 1;
597       else
598          return RESOURCE_UNI(res)->array_elements;
599    case GL_VERTEX_SUBROUTINE:
600    case GL_GEOMETRY_SUBROUTINE:
601    case GL_FRAGMENT_SUBROUTINE:
602    case GL_COMPUTE_SUBROUTINE:
603    case GL_TESS_CONTROL_SUBROUTINE:
604    case GL_TESS_EVALUATION_SUBROUTINE:
605    case GL_ATOMIC_COUNTER_BUFFER:
606    case GL_UNIFORM_BLOCK:
607    case GL_SHADER_STORAGE_BLOCK:
608       return 0;
609    default:
610       assert(!"support for resource type not implemented");
611    }
612    return 0;
613 }
614 
615 /**
616  * Checks if array subscript is valid and if so sets array_index.
617  */
618 static bool
valid_array_index(const GLchar * name,int len,unsigned * array_index)619 valid_array_index(const GLchar *name, int len, unsigned *array_index)
620 {
621    long idx = 0;
622    const GLchar *out_base_name_end;
623 
624    idx = link_util_parse_program_resource_name(name, len, &out_base_name_end);
625    if (idx < 0)
626       return false;
627 
628    if (array_index)
629       *array_index = idx;
630 
631    return true;
632 }
633 
634 static struct gl_program_resource *
search_resource_hash(struct gl_shader_program * shProg,GLenum programInterface,const char * name,int len,unsigned * array_index)635 search_resource_hash(struct gl_shader_program *shProg,
636                      GLenum programInterface, const char *name, int len,
637                      unsigned *array_index)
638 {
639    unsigned type = GET_PROGRAM_RESOURCE_TYPE_FROM_GLENUM(programInterface);
640    assert(type < ARRAY_SIZE(shProg->data->ProgramResourceHash));
641 
642    if (!shProg->data->ProgramResourceHash[type])
643       return NULL;
644 
645    const char *base_name_end;
646    long index = link_util_parse_program_resource_name(name, len, &base_name_end);
647    char *name_copy;
648 
649    /* If dealing with array, we need to get the basename. */
650    if (index >= 0) {
651       name_copy = (char *) alloca(base_name_end - name + 1);
652       memcpy(name_copy, name, base_name_end - name);
653       name_copy[base_name_end - name] = '\0';
654       len = base_name_end - name;
655    } else {
656       name_copy = (char*) name;
657    }
658 
659    uint32_t hash = _mesa_hash_string_with_length(name_copy, len);
660    struct hash_entry *entry =
661       _mesa_hash_table_search_pre_hashed(shProg->data->ProgramResourceHash[type],
662                                          hash, name_copy);
663    if (!entry)
664       return NULL;
665 
666    if (array_index)
667       *array_index = index >= 0 ? index : 0;
668 
669    return (struct gl_program_resource *)entry->data;
670 }
671 
672 /* Find a program resource with specific name in given interface.
673  */
674 struct gl_program_resource *
_mesa_program_resource_find_name(struct gl_shader_program * shProg,GLenum programInterface,const char * name,unsigned * array_index)675 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
676                                  GLenum programInterface, const char *name,
677                                  unsigned *array_index)
678 {
679    if (name == NULL)
680       return NULL;
681 
682    int len = strlen(name);
683 
684    /* If we have a name, try the ProgramResourceHash first. */
685    struct gl_program_resource *res =
686       search_resource_hash(shProg, programInterface, name, len, array_index);
687 
688    if (res)
689       return res;
690 
691    res = shProg->data->ProgramResourceList;
692    for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
693       if (res->Type != programInterface)
694          continue;
695 
696       struct gl_resource_name rname;
697 
698       /* Since ARB_gl_spirv lack of name reflections is a possibility */
699       if (!_mesa_program_get_resource_name(res, &rname))
700          continue;
701 
702       bool found = false;
703 
704       /* From ARB_program_interface_query spec:
705        *
706        * "uint GetProgramResourceIndex(uint program, enum programInterface,
707        *                               const char *name);
708        *  [...]
709        *  If <name> exactly matches the name string of one of the active
710        *  resources for <programInterface>, the index of the matched resource is
711        *  returned. Additionally, if <name> would exactly match the name string
712        *  of an active resource if "[0]" were appended to <name>, the index of
713        *  the matched resource is returned. [...]"
714        *
715        * "A string provided to GetProgramResourceLocation or
716        * GetProgramResourceLocationIndex is considered to match an active variable
717        * if:
718        *
719        *  * the string exactly matches the name of the active variable;
720        *
721        *  * if the string identifies the base name of an active array, where the
722        *    string would exactly match the name of the variable if the suffix
723        *    "[0]" were appended to the string; [...]"
724        */
725       /* Remove array's index from interface block name comparison only if
726        * array's index is zero and the resulting string length is the same
727        * than the provided name's length.
728        */
729       int length_without_array_index =
730          rname.last_square_bracket >= 0 ? rname.last_square_bracket : rname.length;
731       bool rname_has_array_index_zero = rname.suffix_is_zero_square_bracketed &&
732                                         rname.last_square_bracket == len;
733 
734       if (len >= rname.length && strncmp(rname.string, name, rname.length) == 0)
735          found = true;
736       else if (rname_has_array_index_zero &&
737                strncmp(rname.string, name, length_without_array_index) == 0)
738          found = true;
739 
740       if (found) {
741          switch (programInterface) {
742          case GL_UNIFORM_BLOCK:
743          case GL_SHADER_STORAGE_BLOCK:
744             /* Basename match, check if array or struct. */
745             if (rname_has_array_index_zero ||
746                 name[rname.length] == '\0' ||
747                 name[rname.length] == '[' ||
748                 name[rname.length] == '.') {
749                return res;
750             }
751             break;
752          case GL_TRANSFORM_FEEDBACK_VARYING:
753          case GL_BUFFER_VARIABLE:
754          case GL_UNIFORM:
755          case GL_VERTEX_SUBROUTINE_UNIFORM:
756          case GL_GEOMETRY_SUBROUTINE_UNIFORM:
757          case GL_FRAGMENT_SUBROUTINE_UNIFORM:
758          case GL_COMPUTE_SUBROUTINE_UNIFORM:
759          case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
760          case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
761          case GL_VERTEX_SUBROUTINE:
762          case GL_GEOMETRY_SUBROUTINE:
763          case GL_FRAGMENT_SUBROUTINE:
764          case GL_COMPUTE_SUBROUTINE:
765          case GL_TESS_CONTROL_SUBROUTINE:
766          case GL_TESS_EVALUATION_SUBROUTINE:
767             if (name[rname.length] == '.') {
768                return res;
769             }
770             FALLTHROUGH;
771          case GL_PROGRAM_INPUT:
772          case GL_PROGRAM_OUTPUT:
773             if (name[rname.length] == '\0') {
774                return res;
775             } else if (name[rname.length] == '[' &&
776                 valid_array_index(name, len, array_index)) {
777                return res;
778             }
779             break;
780          default:
781             assert(!"not implemented for given interface");
782          }
783       }
784    }
785    return NULL;
786 }
787 
788 /* Find an uniform or buffer variable program resource with an specific offset
789  * inside a block with an specific binding.
790  *
791  * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
792  */
793 static struct gl_program_resource *
program_resource_find_binding_offset(struct gl_shader_program * shProg,GLenum programInterface,const GLuint binding,const GLint offset)794 program_resource_find_binding_offset(struct gl_shader_program *shProg,
795                                      GLenum programInterface,
796                                      const GLuint binding,
797                                      const GLint offset)
798 {
799 
800    /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
801    GLenum blockInterface;
802 
803    switch (programInterface) {
804    case GL_BUFFER_VARIABLE:
805       blockInterface = GL_SHADER_STORAGE_BLOCK;
806       break;
807    case GL_UNIFORM:
808       blockInterface = GL_UNIFORM_BLOCK;
809       break;
810    default:
811       assert("Invalid program interface");
812       return NULL;
813    }
814 
815    int block_index = -1;
816    int starting_index = -1;
817    struct gl_program_resource *res = shProg->data->ProgramResourceList;
818 
819    /* Blocks are added to the resource list in the same order that they are
820     * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
821     * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
822     * the resource list.
823     */
824    for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
825       if (res->Type != blockInterface)
826          continue;
827 
828       /* Store the first index where a resource of the specific interface is. */
829       if (starting_index == -1)
830          starting_index = i;
831 
832       const struct gl_uniform_block *block = RESOURCE_UBO(res);
833 
834       if (block->Binding == binding) {
835          /* For arrays, or arrays of arrays of blocks, we want the resource
836           * for the block with base index. Most properties for members of each
837           * block are inherited from the block with the base index, including
838           * a uniform being active or not.
839           */
840          block_index = i - starting_index - block->linearized_array_index;
841          break;
842       }
843    }
844 
845    if (block_index == -1)
846       return NULL;
847 
848    /* We now look for the resource corresponding to the uniform or buffer
849     * variable using the BLOCK_INDEX and OFFSET.
850     */
851    res = shProg->data->ProgramResourceList;
852    for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
853       if (res->Type != programInterface)
854          continue;
855 
856       const struct gl_uniform_storage *uniform = RESOURCE_UNI(res);
857 
858       if (uniform->block_index == block_index && uniform->offset == offset) {
859          return res;
860       }
861    }
862 
863    return NULL;
864 }
865 
866 /* Checks if an uniform or buffer variable is in the active program resource
867  * list.
868  *
869  * It takes into accout that for variables coming from SPIR-V binaries their
870  * names could not be available (ARB_gl_spirv). In that case, it will use the
871  * the offset and the block binding to locate the resource.
872  *
873  * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
874  */
875 struct gl_program_resource *
_mesa_program_resource_find_active_variable(struct gl_shader_program * shProg,GLenum programInterface,const gl_uniform_block * block,unsigned index)876 _mesa_program_resource_find_active_variable(struct gl_shader_program *shProg,
877                                             GLenum programInterface,
878                                             const gl_uniform_block *block,
879                                             unsigned index)
880 {
881    struct gl_program_resource *res;
882    struct gl_uniform_buffer_variable uni = block->Uniforms[index];
883 
884    assert(programInterface == GL_UNIFORM ||
885           programInterface == GL_BUFFER_VARIABLE);
886 
887    if (uni.IndexName) {
888       res = _mesa_program_resource_find_name(shProg, programInterface, uni.IndexName,
889                                              NULL);
890    } else {
891       /* As the resource has no associated name (ARB_gl_spirv),
892        * we can use the UBO/SSBO binding and offset to find it.
893        */
894       res = program_resource_find_binding_offset(shProg, programInterface,
895                                                  block->Binding, uni.Offset);
896    }
897 
898    return res;
899 }
900 
901 static GLuint
calc_resource_index(struct gl_shader_program * shProg,struct gl_program_resource * res)902 calc_resource_index(struct gl_shader_program *shProg,
903                     struct gl_program_resource *res)
904 {
905    unsigned i;
906    GLuint index = 0;
907    for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
908       if (&shProg->data->ProgramResourceList[i] == res)
909          return index;
910       if (shProg->data->ProgramResourceList[i].Type == res->Type)
911          index++;
912    }
913    return GL_INVALID_INDEX;
914 }
915 
916 /**
917  * Calculate index for the given resource.
918  */
919 GLuint
_mesa_program_resource_index(struct gl_shader_program * shProg,struct gl_program_resource * res)920 _mesa_program_resource_index(struct gl_shader_program *shProg,
921                              struct gl_program_resource *res)
922 {
923    if (!res)
924       return GL_INVALID_INDEX;
925 
926    switch (res->Type) {
927    case GL_ATOMIC_COUNTER_BUFFER:
928       return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
929    case GL_VERTEX_SUBROUTINE:
930    case GL_GEOMETRY_SUBROUTINE:
931    case GL_FRAGMENT_SUBROUTINE:
932    case GL_COMPUTE_SUBROUTINE:
933    case GL_TESS_CONTROL_SUBROUTINE:
934    case GL_TESS_EVALUATION_SUBROUTINE:
935       return RESOURCE_SUB(res)->index;
936    case GL_UNIFORM_BLOCK:
937    case GL_SHADER_STORAGE_BLOCK:
938    case GL_TRANSFORM_FEEDBACK_BUFFER:
939    case GL_TRANSFORM_FEEDBACK_VARYING:
940    default:
941       return calc_resource_index(shProg, res);
942    }
943 }
944 
945 /**
946  * Find a program resource that points to given data.
947  */
948 static struct gl_program_resource*
program_resource_find_data(struct gl_shader_program * shProg,void * data)949 program_resource_find_data(struct gl_shader_program *shProg, void *data)
950 {
951    struct gl_program_resource *res = shProg->data->ProgramResourceList;
952    for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
953         i++, res++) {
954       if (res->Data == data)
955          return res;
956    }
957    return NULL;
958 }
959 
960 /* Find a program resource with specific index in given interface.
961  */
962 struct gl_program_resource *
_mesa_program_resource_find_index(struct gl_shader_program * shProg,GLenum programInterface,GLuint index)963 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
964                                   GLenum programInterface, GLuint index)
965 {
966    struct gl_program_resource *res = shProg->data->ProgramResourceList;
967    int idx = -1;
968 
969    for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
970         i++, res++) {
971       if (res->Type != programInterface)
972          continue;
973 
974       switch (res->Type) {
975       case GL_UNIFORM_BLOCK:
976       case GL_ATOMIC_COUNTER_BUFFER:
977       case GL_SHADER_STORAGE_BLOCK:
978       case GL_TRANSFORM_FEEDBACK_BUFFER:
979          if (_mesa_program_resource_index(shProg, res) == index)
980             return res;
981          break;
982       case GL_TRANSFORM_FEEDBACK_VARYING:
983       case GL_PROGRAM_INPUT:
984       case GL_PROGRAM_OUTPUT:
985       case GL_UNIFORM:
986       case GL_VERTEX_SUBROUTINE_UNIFORM:
987       case GL_GEOMETRY_SUBROUTINE_UNIFORM:
988       case GL_FRAGMENT_SUBROUTINE_UNIFORM:
989       case GL_COMPUTE_SUBROUTINE_UNIFORM:
990       case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
991       case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
992       case GL_VERTEX_SUBROUTINE:
993       case GL_GEOMETRY_SUBROUTINE:
994       case GL_FRAGMENT_SUBROUTINE:
995       case GL_COMPUTE_SUBROUTINE:
996       case GL_TESS_CONTROL_SUBROUTINE:
997       case GL_TESS_EVALUATION_SUBROUTINE:
998       case GL_BUFFER_VARIABLE:
999          if (++idx == (int) index)
1000             return res;
1001          break;
1002       default:
1003          assert(!"not implemented for given interface");
1004       }
1005    }
1006    return NULL;
1007 }
1008 
1009 /* Function returns if resource name is expected to have index
1010  * appended into it.
1011  *
1012  *
1013  * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
1014  * spec says:
1015  *
1016  *     "If the active uniform is an array, the uniform name returned in
1017  *     name will always be the name of the uniform array appended with
1018  *     "[0]"."
1019  *
1020  * The same text also appears in the OpenGL 4.2 spec.  It does not,
1021  * however, appear in any previous spec.  Previous specifications are
1022  * ambiguous in this regard.  However, either name can later be passed
1023  * to glGetUniformLocation (and related APIs), so there shouldn't be any
1024  * harm in always appending "[0]" to uniform array names.
1025  */
1026 static bool
add_index_to_name(struct gl_program_resource * res)1027 add_index_to_name(struct gl_program_resource *res)
1028 {
1029    /* Transform feedback varyings have array index already appended
1030     * in their names.
1031     */
1032    return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
1033 }
1034 
1035 /* Get name length of a program resource. This consists of
1036  * base name + 3 for '[0]' if resource is an array.
1037  */
1038 extern unsigned
_mesa_program_resource_name_length_array(struct gl_program_resource * res)1039 _mesa_program_resource_name_length_array(struct gl_program_resource *res)
1040 {
1041    int length = _mesa_program_resource_name_length(res);
1042 
1043    /* For shaders constructed from SPIR-V binaries, variables may not
1044     * have names associated with them.
1045     */
1046    if (!length)
1047       return 0;
1048 
1049    if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
1050       length += 3;
1051    return length;
1052 }
1053 
1054 /* Get full name of a program resource.
1055  */
1056 bool
_mesa_get_program_resource_name(struct gl_shader_program * shProg,GLenum programInterface,GLuint index,GLsizei bufSize,GLsizei * length,GLchar * name,bool glthread,const char * caller)1057 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
1058                                 GLenum programInterface, GLuint index,
1059                                 GLsizei bufSize, GLsizei *length,
1060                                 GLchar *name, bool glthread,
1061                                 const char *caller)
1062 {
1063    GET_CURRENT_CONTEXT(ctx);
1064 
1065    /* Find resource with given interface and index. */
1066    struct gl_program_resource *res =
1067       _mesa_program_resource_find_index(shProg, programInterface, index);
1068 
1069    /* The error INVALID_VALUE is generated if <index> is greater than
1070    * or equal to the number of entries in the active resource list for
1071    * <programInterface>.
1072    */
1073    if (!res) {
1074       _mesa_error_glthread_safe(ctx, GL_INVALID_VALUE, glthread,
1075                                 "%s(index %u)", caller, index);
1076       return false;
1077    }
1078 
1079    if (bufSize < 0) {
1080       _mesa_error_glthread_safe(ctx, GL_INVALID_VALUE, glthread,
1081                                 "%s(bufSize %d)", caller, bufSize);
1082       return false;
1083    }
1084 
1085    GLsizei localLength;
1086 
1087    if (length == NULL)
1088       length = &localLength;
1089 
1090    _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
1091 
1092    /* The resource name can be NULL for shaders constructed from SPIR-V
1093     * binaries. In that case, we do not add the '[0]'.
1094     */
1095    if (name && name[0] != '\0' &&
1096        _mesa_program_resource_array_size(res) && add_index_to_name(res)) {
1097       int i;
1098 
1099       /* The comparison is strange because *length does *NOT* include the
1100        * terminating NUL, but maxLength does.
1101        */
1102       for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
1103          name[*length + i] = "[0]"[i];
1104 
1105       name[*length + i] = '\0';
1106       *length += i;
1107    }
1108    return true;
1109 }
1110 
1111 static GLint
program_resource_location(struct gl_program_resource * res,unsigned array_index)1112 program_resource_location(struct gl_program_resource *res, unsigned array_index)
1113 {
1114    switch (res->Type) {
1115    case GL_PROGRAM_INPUT: {
1116       const gl_shader_variable *var = RESOURCE_VAR(res);
1117 
1118       if (var->location == -1)
1119          return -1;
1120 
1121       /* If the input is an array, fail if the index is out of bounds. */
1122       if (array_index > 0
1123           && array_index >= var->type->length) {
1124          return -1;
1125       }
1126       return var->location +
1127 	     (array_index * glsl_without_array(var->type)->matrix_columns);
1128    }
1129    case GL_PROGRAM_OUTPUT:
1130       if (RESOURCE_VAR(res)->location == -1)
1131          return -1;
1132 
1133       /* If the output is an array, fail if the index is out of bounds. */
1134       if (array_index > 0
1135           && array_index >= RESOURCE_VAR(res)->type->length) {
1136          return -1;
1137       }
1138       return RESOURCE_VAR(res)->location + array_index;
1139    case GL_UNIFORM:
1140       /* If the uniform is built-in, fail. */
1141       if (RESOURCE_UNI(res)->builtin)
1142          return -1;
1143 
1144      /* From page 79 of the OpenGL 4.2 spec:
1145       *
1146       *     "A valid name cannot be a structure, an array of structures, or any
1147       *     portion of a single vector or a matrix."
1148       */
1149       if (glsl_type_is_struct(glsl_without_array(RESOURCE_UNI(res)->type)))
1150          return -1;
1151 
1152       /* From the GL_ARB_uniform_buffer_object spec:
1153        *
1154        *     "The value -1 will be returned if <name> does not correspond to an
1155        *     active uniform variable name in <program>, if <name> is associated
1156        *     with a named uniform block, or if <name> starts with the reserved
1157        *     prefix "gl_"."
1158        */
1159       if (RESOURCE_UNI(res)->block_index != -1 ||
1160           RESOURCE_UNI(res)->atomic_buffer_index != -1)
1161          return -1;
1162 
1163       FALLTHROUGH;
1164    case GL_VERTEX_SUBROUTINE_UNIFORM:
1165    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1166    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1167    case GL_COMPUTE_SUBROUTINE_UNIFORM:
1168    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1169    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1170       /* If the uniform is an array, fail if the index is out of bounds. */
1171       if (array_index > 0
1172           && array_index >= RESOURCE_UNI(res)->array_elements) {
1173          return -1;
1174       }
1175 
1176       /* location in remap table + array element offset */
1177       return RESOURCE_UNI(res)->remap_location + array_index;
1178    default:
1179       return -1;
1180    }
1181 }
1182 
1183 /**
1184  * Function implements following location queries:
1185  *    glGetUniformLocation
1186  */
1187 GLint
_mesa_program_resource_location(struct gl_shader_program * shProg,GLenum programInterface,const char * name)1188 _mesa_program_resource_location(struct gl_shader_program *shProg,
1189                                 GLenum programInterface, const char *name)
1190 {
1191    unsigned array_index = 0;
1192    struct gl_program_resource *res =
1193       _mesa_program_resource_find_name(shProg, programInterface, name,
1194                                        &array_index);
1195 
1196    /* Resource not found. */
1197    if (!res)
1198       return -1;
1199 
1200    return program_resource_location(res, array_index);
1201 }
1202 
1203 static GLint
_get_resource_location_index(struct gl_program_resource * res)1204 _get_resource_location_index(struct gl_program_resource *res)
1205 {
1206    /* Non-existent variable or resource is not referenced by fragment stage. */
1207    if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1208       return -1;
1209 
1210    /* From OpenGL 4.5 spec, 7.3 Program Objects
1211     * "The value -1 will be returned by either command...
1212     *  ... or if name identifies an active variable that does not have a
1213     * valid location assigned.
1214     */
1215    if (RESOURCE_VAR(res)->location == -1)
1216       return -1;
1217    return RESOURCE_VAR(res)->index;
1218 }
1219 
1220 /**
1221  * Function implements following index queries:
1222  *    glGetFragDataIndex
1223  */
1224 GLint
_mesa_program_resource_location_index(struct gl_shader_program * shProg,GLenum programInterface,const char * name)1225 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1226                                       GLenum programInterface, const char *name)
1227 {
1228    struct gl_program_resource *res =
1229       _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1230 
1231    return _get_resource_location_index(res);
1232 }
1233 
1234 static uint8_t
stage_from_enum(GLenum ref)1235 stage_from_enum(GLenum ref)
1236 {
1237    switch (ref) {
1238    case GL_REFERENCED_BY_VERTEX_SHADER:
1239       return MESA_SHADER_VERTEX;
1240    case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1241       return MESA_SHADER_TESS_CTRL;
1242    case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1243       return MESA_SHADER_TESS_EVAL;
1244    case GL_REFERENCED_BY_GEOMETRY_SHADER:
1245       return MESA_SHADER_GEOMETRY;
1246    case GL_REFERENCED_BY_FRAGMENT_SHADER:
1247       return MESA_SHADER_FRAGMENT;
1248    case GL_REFERENCED_BY_COMPUTE_SHADER:
1249       return MESA_SHADER_COMPUTE;
1250    default:
1251       assert(!"shader stage not supported");
1252       return MESA_SHADER_STAGES;
1253    }
1254 }
1255 
1256 /**
1257  * Check if resource is referenced by given 'referenced by' stage enum.
1258  * ATC and UBO resources hold stage references of their own.
1259  */
1260 static bool
is_resource_referenced(struct gl_shader_program * shProg,struct gl_program_resource * res,GLuint index,uint8_t stage)1261 is_resource_referenced(struct gl_shader_program *shProg,
1262                        struct gl_program_resource *res,
1263                        GLuint index, uint8_t stage)
1264 {
1265    /* First, check if we even have such a stage active. */
1266    if (!shProg->_LinkedShaders[stage])
1267       return false;
1268 
1269    if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1270       return RESOURCE_ATC(res)->StageReferences[stage];
1271 
1272    if (res->Type == GL_UNIFORM_BLOCK)
1273       return shProg->data->UniformBlocks[index].stageref & (1 << stage);
1274 
1275    if (res->Type == GL_SHADER_STORAGE_BLOCK)
1276       return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
1277 
1278    return res->StageReferences & (1 << stage);
1279 }
1280 
1281 static unsigned
get_buffer_property(struct gl_shader_program * shProg,struct gl_program_resource * res,const GLenum prop,GLint * val,bool glthread,const char * caller)1282 get_buffer_property(struct gl_shader_program *shProg,
1283                     struct gl_program_resource *res, const GLenum prop,
1284                     GLint *val, bool glthread, const char *caller)
1285 {
1286    GET_CURRENT_CONTEXT(ctx);
1287    if (res->Type != GL_UNIFORM_BLOCK &&
1288        res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1289        res->Type != GL_SHADER_STORAGE_BLOCK &&
1290        res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
1291       goto invalid_operation;
1292 
1293    if (res->Type == GL_UNIFORM_BLOCK) {
1294       switch (prop) {
1295       case GL_BUFFER_BINDING:
1296          *val = RESOURCE_UBO(res)->Binding;
1297          return 1;
1298       case GL_BUFFER_DATA_SIZE:
1299          *val = RESOURCE_UBO(res)->UniformBufferSize;
1300          return 1;
1301       case GL_NUM_ACTIVE_VARIABLES:
1302          *val = 0;
1303          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1304             struct gl_program_resource *uni =
1305                _mesa_program_resource_find_active_variable(
1306                   shProg,
1307                   GL_UNIFORM,
1308                   RESOURCE_UBO(res),
1309                   i);
1310 
1311             if (!uni)
1312                continue;
1313             (*val)++;
1314          }
1315          return 1;
1316       case GL_ACTIVE_VARIABLES: {
1317          unsigned num_values = 0;
1318          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1319             struct gl_program_resource *uni =
1320                _mesa_program_resource_find_active_variable(
1321                   shProg,
1322                   GL_UNIFORM,
1323                   RESOURCE_UBO(res),
1324                   i);
1325 
1326             if (!uni)
1327                continue;
1328             *val++ =
1329                _mesa_program_resource_index(shProg, uni);
1330             num_values++;
1331          }
1332          return num_values;
1333       }
1334       }
1335    } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1336       switch (prop) {
1337       case GL_BUFFER_BINDING:
1338          *val = RESOURCE_UBO(res)->Binding;
1339          return 1;
1340       case GL_BUFFER_DATA_SIZE:
1341          *val = RESOURCE_UBO(res)->UniformBufferSize;
1342          return 1;
1343       case GL_NUM_ACTIVE_VARIABLES:
1344          *val = 0;
1345          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1346             struct gl_program_resource *uni =
1347                _mesa_program_resource_find_active_variable(
1348                   shProg,
1349                   GL_BUFFER_VARIABLE,
1350                   RESOURCE_UBO(res),
1351                   i);
1352 
1353             if (!uni)
1354                continue;
1355             (*val)++;
1356          }
1357          return 1;
1358       case GL_ACTIVE_VARIABLES: {
1359          unsigned num_values = 0;
1360          for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1361             struct gl_program_resource *uni =
1362                _mesa_program_resource_find_active_variable(
1363                   shProg,
1364                   GL_BUFFER_VARIABLE,
1365                   RESOURCE_UBO(res),
1366                   i);
1367 
1368             if (!uni)
1369                continue;
1370             *val++ =
1371                _mesa_program_resource_index(shProg, uni);
1372             num_values++;
1373          }
1374          return num_values;
1375       }
1376       }
1377    } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1378       switch (prop) {
1379       case GL_BUFFER_BINDING:
1380          *val = RESOURCE_ATC(res)->Binding;
1381          return 1;
1382       case GL_BUFFER_DATA_SIZE:
1383          *val = RESOURCE_ATC(res)->MinimumSize;
1384          return 1;
1385       case GL_NUM_ACTIVE_VARIABLES:
1386          *val = RESOURCE_ATC(res)->NumUniforms;
1387          return 1;
1388       case GL_ACTIVE_VARIABLES:
1389          for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1390             /* Active atomic buffer contains index to UniformStorage. Find
1391              * out gl_program_resource via data pointer and then calculate
1392              * index of that uniform.
1393              */
1394             unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1395             struct gl_program_resource *uni =
1396                program_resource_find_data(shProg,
1397                                           &shProg->data->UniformStorage[idx]);
1398             assert(uni);
1399             *val++ = _mesa_program_resource_index(shProg, uni);
1400          }
1401          return RESOURCE_ATC(res)->NumUniforms;
1402       }
1403    } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1404       switch (prop) {
1405       case GL_BUFFER_BINDING:
1406          *val = RESOURCE_XFB(res)->Binding;
1407          return 1;
1408       case GL_NUM_ACTIVE_VARIABLES:
1409          *val = RESOURCE_XFB(res)->NumVaryings;
1410          return 1;
1411       case GL_ACTIVE_VARIABLES:
1412          struct gl_transform_feedback_info *linked_xfb =
1413             shProg->last_vert_prog->sh.LinkedTransformFeedback;
1414          for (int i = 0; i < linked_xfb->NumVarying; i++) {
1415             unsigned index = linked_xfb->Varyings[i].BufferIndex;
1416             struct gl_program_resource *buf_res =
1417                _mesa_program_resource_find_index(shProg,
1418                                                  GL_TRANSFORM_FEEDBACK_BUFFER,
1419                                                  index);
1420             assert(buf_res);
1421             if (res == buf_res) {
1422                *val++ = i;
1423             }
1424          }
1425          return RESOURCE_XFB(res)->NumVaryings;
1426       }
1427    }
1428    assert(!"support for property type not implemented");
1429 
1430 invalid_operation:
1431    _mesa_error_glthread_safe(ctx, GL_INVALID_OPERATION, glthread,
1432                              "%s(%s prop %s)", caller,
1433                              _mesa_enum_to_string(res->Type),
1434                              _mesa_enum_to_string(prop));
1435 
1436    return 0;
1437 }
1438 
1439 unsigned
_mesa_program_resource_prop(struct gl_shader_program * shProg,struct gl_program_resource * res,GLuint index,const GLenum prop,GLint * val,bool glthread,const char * caller)1440 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1441                             struct gl_program_resource *res, GLuint index,
1442                             const GLenum prop, GLint *val, bool glthread,
1443                             const char *caller)
1444 {
1445    GET_CURRENT_CONTEXT(ctx);
1446 
1447 #define VALIDATE_TYPE(type)\
1448    if (res->Type != type)\
1449       goto invalid_operation;
1450 
1451 #define VALIDATE_TYPE_2(type1, type2)\
1452    if (res->Type != type1 && res->Type != type2)\
1453       goto invalid_operation;
1454 
1455    switch(prop) {
1456    case GL_NAME_LENGTH:
1457       switch (res->Type) {
1458       case GL_ATOMIC_COUNTER_BUFFER:
1459       case GL_TRANSFORM_FEEDBACK_BUFFER:
1460          goto invalid_operation;
1461       default:
1462          /* Resource name length + terminator. */
1463          *val = _mesa_program_resource_name_length_array(res) + 1;
1464       }
1465       return 1;
1466    case GL_TYPE:
1467       switch (res->Type) {
1468       case GL_UNIFORM:
1469       case GL_BUFFER_VARIABLE:
1470          *val = RESOURCE_UNI(res)->type->gl_type;
1471          *val = mediump_to_highp_type(shProg, *val);
1472          return 1;
1473       case GL_PROGRAM_INPUT:
1474       case GL_PROGRAM_OUTPUT:
1475          *val = RESOURCE_VAR(res)->type->gl_type;
1476          *val = mediump_to_highp_type(shProg, *val);
1477          return 1;
1478       case GL_TRANSFORM_FEEDBACK_VARYING:
1479          *val = RESOURCE_XFV(res)->Type;
1480          *val = mediump_to_highp_type(shProg, *val);
1481          return 1;
1482       default:
1483          goto invalid_operation;
1484       }
1485    case GL_ARRAY_SIZE:
1486       switch (res->Type) {
1487       case GL_UNIFORM:
1488       case GL_BUFFER_VARIABLE:
1489       case GL_VERTEX_SUBROUTINE_UNIFORM:
1490       case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1491       case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1492       case GL_COMPUTE_SUBROUTINE_UNIFORM:
1493       case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1494       case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1495 
1496          /* Test if a buffer variable is an array or an unsized array.
1497           * Unsized arrays return zero as array size.
1498           */
1499          if (RESOURCE_UNI(res)->is_shader_storage &&
1500              RESOURCE_UNI(res)->array_stride > 0)
1501             *val = RESOURCE_UNI(res)->array_elements;
1502          else
1503             *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1504          return 1;
1505       case GL_PROGRAM_INPUT:
1506       case GL_PROGRAM_OUTPUT:
1507          *val = MAX2(_mesa_program_resource_array_size(res), 1);
1508          return 1;
1509       case GL_TRANSFORM_FEEDBACK_VARYING:
1510          *val = RESOURCE_XFV(res)->Size;
1511          return 1;
1512       default:
1513          goto invalid_operation;
1514       }
1515    case GL_OFFSET:
1516       switch (res->Type) {
1517       case GL_UNIFORM:
1518       case GL_BUFFER_VARIABLE:
1519          *val = RESOURCE_UNI(res)->offset;
1520          return 1;
1521       case GL_TRANSFORM_FEEDBACK_VARYING:
1522          *val = RESOURCE_XFV(res)->Offset;
1523          return 1;
1524       default:
1525          goto invalid_operation;
1526       }
1527    case GL_BLOCK_INDEX:
1528       VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1529       *val = RESOURCE_UNI(res)->block_index;
1530       return 1;
1531    case GL_ARRAY_STRIDE:
1532       VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1533       *val = RESOURCE_UNI(res)->array_stride;
1534       return 1;
1535    case GL_MATRIX_STRIDE:
1536       VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1537       *val = RESOURCE_UNI(res)->matrix_stride;
1538       return 1;
1539    case GL_IS_ROW_MAJOR:
1540       VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1541       *val = RESOURCE_UNI(res)->row_major;
1542       return 1;
1543    case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1544       VALIDATE_TYPE(GL_UNIFORM);
1545       *val = RESOURCE_UNI(res)->atomic_buffer_index;
1546       return 1;
1547    case GL_BUFFER_BINDING:
1548    case GL_BUFFER_DATA_SIZE:
1549    case GL_NUM_ACTIVE_VARIABLES:
1550    case GL_ACTIVE_VARIABLES:
1551       return get_buffer_property(shProg, res, prop, val, glthread, caller);
1552    case GL_REFERENCED_BY_COMPUTE_SHADER:
1553       if (!_mesa_has_compute_shaders(ctx))
1554          goto invalid_enum;
1555       FALLTHROUGH;
1556    case GL_REFERENCED_BY_VERTEX_SHADER:
1557    case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1558    case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1559    case GL_REFERENCED_BY_GEOMETRY_SHADER:
1560    case GL_REFERENCED_BY_FRAGMENT_SHADER:
1561       switch (res->Type) {
1562       case GL_UNIFORM:
1563       case GL_PROGRAM_INPUT:
1564       case GL_PROGRAM_OUTPUT:
1565       case GL_UNIFORM_BLOCK:
1566       case GL_BUFFER_VARIABLE:
1567       case GL_SHADER_STORAGE_BLOCK:
1568       case GL_ATOMIC_COUNTER_BUFFER:
1569          *val = is_resource_referenced(shProg, res, index,
1570                                        stage_from_enum(prop));
1571          return 1;
1572       default:
1573          goto invalid_operation;
1574       }
1575    case GL_LOCATION:
1576       switch (res->Type) {
1577       case GL_UNIFORM:
1578       case GL_VERTEX_SUBROUTINE_UNIFORM:
1579       case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1580       case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1581       case GL_COMPUTE_SUBROUTINE_UNIFORM:
1582       case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1583       case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1584       case GL_PROGRAM_INPUT:
1585       case GL_PROGRAM_OUTPUT:
1586          *val = program_resource_location(res, 0);
1587          return 1;
1588       default:
1589          goto invalid_operation;
1590       }
1591    case GL_LOCATION_COMPONENT:
1592       switch (res->Type) {
1593       case GL_PROGRAM_INPUT:
1594       case GL_PROGRAM_OUTPUT:
1595          *val = RESOURCE_VAR(res)->component;
1596          return 1;
1597       default:
1598          goto invalid_operation;
1599       }
1600    case GL_LOCATION_INDEX: {
1601       int tmp;
1602       if (res->Type != GL_PROGRAM_OUTPUT)
1603          goto invalid_operation;
1604       tmp = program_resource_location(res, 0);
1605       if (tmp == -1)
1606          *val = -1;
1607       else
1608          *val = _get_resource_location_index(res);
1609       return 1;
1610    }
1611    case GL_NUM_COMPATIBLE_SUBROUTINES:
1612       if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1613           res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1614           res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1615           res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1616           res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1617           res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1618          goto invalid_operation;
1619       *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1620       return 1;
1621    case GL_COMPATIBLE_SUBROUTINES: {
1622       const struct gl_uniform_storage *uni;
1623       struct gl_program *p;
1624       unsigned count, i;
1625       int j;
1626 
1627       if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1628           res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1629           res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1630           res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1631           res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1632           res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1633          goto invalid_operation;
1634       uni = RESOURCE_UNI(res);
1635 
1636       p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
1637       count = 0;
1638       for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
1639          struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
1640          for (j = 0; j < fn->num_compat_types; j++) {
1641             if (fn->types[j] == uni->type) {
1642                val[count++] = i;
1643                break;
1644             }
1645          }
1646       }
1647       return count;
1648    }
1649 
1650    case GL_TOP_LEVEL_ARRAY_SIZE:
1651       VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1652       *val = RESOURCE_UNI(res)->top_level_array_size;
1653       return 1;
1654 
1655    case GL_TOP_LEVEL_ARRAY_STRIDE:
1656       VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1657       *val = RESOURCE_UNI(res)->top_level_array_stride;
1658       return 1;
1659 
1660    /* GL_ARB_tessellation_shader */
1661    case GL_IS_PER_PATCH:
1662       switch (res->Type) {
1663       case GL_PROGRAM_INPUT:
1664       case GL_PROGRAM_OUTPUT:
1665          *val = RESOURCE_VAR(res)->patch;
1666          return 1;
1667       default:
1668          goto invalid_operation;
1669       }
1670 
1671    case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1672       VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1673       *val = RESOURCE_XFV(res)->BufferIndex;
1674       return 1;
1675    case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1676       VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1677       *val = RESOURCE_XFB(res)->Stride * 4;
1678       return 1;
1679 
1680    default:
1681       goto invalid_enum;
1682    }
1683 
1684 #undef VALIDATE_TYPE
1685 #undef VALIDATE_TYPE_2
1686 
1687 invalid_enum:
1688    _mesa_error_glthread_safe(ctx, GL_INVALID_ENUM, glthread,
1689                              "%s(%s prop %s)", caller,
1690                              _mesa_enum_to_string(res->Type),
1691                              _mesa_enum_to_string(prop));
1692    return 0;
1693 
1694 invalid_operation:
1695    _mesa_error_glthread_safe(ctx, GL_INVALID_OPERATION, glthread,
1696                              "%s(%s prop %s)", caller,
1697                              _mesa_enum_to_string(res->Type),
1698                              _mesa_enum_to_string(prop));
1699    return 0;
1700 }
1701 
1702 extern void
_mesa_get_program_resourceiv(struct gl_shader_program * shProg,GLenum programInterface,GLuint index,GLsizei propCount,const GLenum * props,GLsizei bufSize,GLsizei * length,GLint * params)1703 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1704                              GLenum programInterface, GLuint index, GLsizei propCount,
1705                              const GLenum *props, GLsizei bufSize,
1706                              GLsizei *length, GLint *params)
1707 {
1708    GET_CURRENT_CONTEXT(ctx);
1709    GLint *val = (GLint *) params;
1710    const GLenum *prop = props;
1711    GLsizei amount = 0;
1712 
1713    struct gl_program_resource *res =
1714       _mesa_program_resource_find_index(shProg, programInterface, index);
1715 
1716    /* No such resource found or bufSize negative. */
1717    if (!res || bufSize < 0) {
1718       _mesa_error(ctx, GL_INVALID_VALUE,
1719                   "glGetProgramResourceiv(%s index %d bufSize %d)",
1720                   _mesa_enum_to_string(programInterface), index, bufSize);
1721       return;
1722    }
1723 
1724    /* Write propCount values until error occurs or bufSize reached. */
1725    for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1726       int props_written =
1727          _mesa_program_resource_prop(shProg, res, index, *prop, val,
1728                                      false, "glGetProgramResourceiv");
1729 
1730       /* Error happened. */
1731       if (props_written == 0)
1732          return;
1733 
1734       amount += props_written;
1735    }
1736 
1737    /* If <length> is not NULL, the actual number of integer values
1738     * written to <params> will be written to <length>.
1739     */
1740    if (length)
1741       *length = amount;
1742 }
1743 
1744 extern void
_mesa_get_program_interfaceiv(struct gl_shader_program * shProg,GLenum programInterface,GLenum pname,GLint * params)1745 _mesa_get_program_interfaceiv(struct gl_shader_program *shProg,
1746                               GLenum programInterface, GLenum pname,
1747                               GLint *params)
1748 {
1749    GET_CURRENT_CONTEXT(ctx);
1750    unsigned i;
1751 
1752    /* Validate pname against interface. */
1753    switch(pname) {
1754    case GL_ACTIVE_RESOURCES:
1755       for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++)
1756          if (shProg->data->ProgramResourceList[i].Type == programInterface)
1757             (*params)++;
1758       break;
1759    case GL_MAX_NAME_LENGTH:
1760       if (programInterface == GL_ATOMIC_COUNTER_BUFFER ||
1761           programInterface == GL_TRANSFORM_FEEDBACK_BUFFER) {
1762          _mesa_error(ctx, GL_INVALID_OPERATION,
1763                      "glGetProgramInterfaceiv(%s pname %s)",
1764                      _mesa_enum_to_string(programInterface),
1765                      _mesa_enum_to_string(pname));
1766          return;
1767       }
1768       /* Name length consists of base name, 3 additional chars '[0]' if
1769        * resource is an array and finally 1 char for string terminator.
1770        */
1771       for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1772          if (shProg->data->ProgramResourceList[i].Type != programInterface)
1773             continue;
1774          unsigned len =
1775             _mesa_program_resource_name_length_array(&shProg->data->ProgramResourceList[i]);
1776          *params = MAX2((unsigned)*params, len + 1);
1777       }
1778       break;
1779    case GL_MAX_NUM_ACTIVE_VARIABLES:
1780       switch (programInterface) {
1781       case GL_UNIFORM_BLOCK:
1782          for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1783             if (shProg->data->ProgramResourceList[i].Type == programInterface) {
1784                struct gl_uniform_block *block =
1785                   (struct gl_uniform_block *)
1786                   shProg->data->ProgramResourceList[i].Data;
1787                *params = MAX2((unsigned)*params, block->NumUniforms);
1788             }
1789          }
1790          break;
1791       case GL_SHADER_STORAGE_BLOCK:
1792          for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1793             if (shProg->data->ProgramResourceList[i].Type == programInterface) {
1794                struct gl_uniform_block *block =
1795                   (struct gl_uniform_block *)
1796                   shProg->data->ProgramResourceList[i].Data;
1797                GLint block_params = 0;
1798                for (unsigned j = 0; j < block->NumUniforms; j++) {
1799                   struct gl_program_resource *uni =
1800                      _mesa_program_resource_find_active_variable(
1801                         shProg,
1802                         GL_BUFFER_VARIABLE,
1803                         block,
1804                         j);
1805                   if (!uni)
1806                      continue;
1807                   block_params++;
1808                }
1809                *params = MAX2(*params, block_params);
1810             }
1811          }
1812          break;
1813       case GL_ATOMIC_COUNTER_BUFFER:
1814          for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1815             if (shProg->data->ProgramResourceList[i].Type == programInterface) {
1816                struct gl_active_atomic_buffer *buffer =
1817                   (struct gl_active_atomic_buffer *)
1818                   shProg->data->ProgramResourceList[i].Data;
1819                *params = MAX2((unsigned)*params, buffer->NumUniforms);
1820             }
1821          }
1822          break;
1823       case GL_TRANSFORM_FEEDBACK_BUFFER:
1824          for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1825             if (shProg->data->ProgramResourceList[i].Type == programInterface) {
1826                struct gl_transform_feedback_buffer *buffer =
1827                   (struct gl_transform_feedback_buffer *)
1828                   shProg->data->ProgramResourceList[i].Data;
1829                *params = MAX2((unsigned)*params, buffer->NumVaryings);
1830             }
1831          }
1832          break;
1833       default:
1834         _mesa_error(ctx, GL_INVALID_OPERATION,
1835                     "glGetProgramInterfaceiv(%s pname %s)",
1836                     _mesa_enum_to_string(programInterface),
1837                     _mesa_enum_to_string(pname));
1838       }
1839       break;
1840    case GL_MAX_NUM_COMPATIBLE_SUBROUTINES:
1841       switch (programInterface) {
1842       case GL_VERTEX_SUBROUTINE_UNIFORM:
1843       case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1844       case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1845       case GL_COMPUTE_SUBROUTINE_UNIFORM:
1846       case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1847       case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM: {
1848          for (i = 0, *params = 0; i < shProg->data->NumProgramResourceList; i++) {
1849             if (shProg->data->ProgramResourceList[i].Type == programInterface) {
1850                struct gl_uniform_storage *uni =
1851                   (struct gl_uniform_storage *)
1852                   shProg->data->ProgramResourceList[i].Data;
1853                *params = MAX2((unsigned)*params, uni->num_compatible_subroutines);
1854             }
1855          }
1856          break;
1857       }
1858 
1859       default:
1860          _mesa_error(ctx, GL_INVALID_OPERATION,
1861                      "glGetProgramInterfaceiv(%s pname %s)",
1862                      _mesa_enum_to_string(programInterface),
1863                      _mesa_enum_to_string(pname));
1864       }
1865       break;
1866    default:
1867       _mesa_error(ctx, GL_INVALID_OPERATION,
1868                   "glGetProgramInterfaceiv(pname %s)",
1869                   _mesa_enum_to_string(pname));
1870    }
1871 }
1872 
1873 static bool
validate_io(struct gl_program * producer,struct gl_program * consumer)1874 validate_io(struct gl_program *producer, struct gl_program *consumer)
1875 {
1876    if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
1877       return true;
1878 
1879    const bool producer_is_array_stage =
1880       producer->info.stage == MESA_SHADER_TESS_CTRL;
1881    const bool consumer_is_array_stage =
1882       consumer->info.stage == MESA_SHADER_GEOMETRY ||
1883       consumer->info.stage == MESA_SHADER_TESS_CTRL ||
1884       consumer->info.stage == MESA_SHADER_TESS_EVAL;
1885 
1886    bool valid = true;
1887 
1888    gl_shader_variable const **outputs =
1889       (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
1890                                            sizeof(gl_shader_variable *));
1891    if (outputs == NULL)
1892       return false;
1893 
1894    /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1895     * says:
1896     *
1897     *    At an interface between program objects, the set of inputs and
1898     *    outputs are considered to match exactly if and only if:
1899     *
1900     *    - Every declared input variable has a matching output, as described
1901     *      above.
1902     *    - There are no user-defined output variables declared without a
1903     *      matching input variable declaration.
1904     *
1905     * Every input has an output, and every output has an input.  Scan the list
1906     * of producer resources once, and generate the list of outputs.  As inputs
1907     * and outputs are matched, remove the matched outputs from the set.  At
1908     * the end, the set must be empty.  If the set is not empty, then there is
1909     * some output that did not have an input.
1910     */
1911    unsigned num_outputs = 0;
1912    for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
1913       struct gl_program_resource *res =
1914          &producer->sh.data->ProgramResourceList[i];
1915 
1916       if (res->Type != GL_PROGRAM_OUTPUT)
1917          continue;
1918 
1919       gl_shader_variable const *const var = RESOURCE_VAR(res);
1920 
1921       /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1922        * says:
1923        *
1924        *    Built-in inputs or outputs do not affect interface matching.
1925        */
1926       if (is_gl_identifier(var->name.string))
1927          continue;
1928 
1929       outputs[num_outputs++] = var;
1930    }
1931 
1932    unsigned match_index = 0;
1933    for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
1934       struct gl_program_resource *res =
1935          &consumer->sh.data->ProgramResourceList[i];
1936 
1937       if (res->Type != GL_PROGRAM_INPUT)
1938          continue;
1939 
1940       gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
1941       gl_shader_variable const *producer_var = NULL;
1942 
1943       if (is_gl_identifier(consumer_var->name.string))
1944          continue;
1945 
1946       /* Inputs with explicit locations match other outputs with explicit
1947        * locations by location instead of by name.
1948        */
1949       if (consumer_var->explicit_location) {
1950          for (unsigned j = 0; j < num_outputs; j++) {
1951             const gl_shader_variable *const var = outputs[j];
1952 
1953             if (var->explicit_location &&
1954                 consumer_var->location == var->location) {
1955                producer_var = var;
1956                match_index = j;
1957                break;
1958             }
1959          }
1960       } else {
1961          for (unsigned j = 0; j < num_outputs; j++) {
1962             const gl_shader_variable *const var = outputs[j];
1963 
1964             if (!var->explicit_location &&
1965                 strcmp(consumer_var->name.string, var->name.string) == 0) {
1966                producer_var = var;
1967                match_index = j;
1968                break;
1969             }
1970          }
1971       }
1972 
1973       /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1974        * says:
1975        *
1976        *    - An output variable is considered to match an input variable in
1977        *      the subsequent shader if:
1978        *
1979        *      - the two variables match in name, type, and qualification; or
1980        *
1981        *      - the two variables are declared with the same location
1982        *        qualifier and match in type and qualification.
1983        */
1984       if (producer_var == NULL) {
1985          valid = false;
1986          goto out;
1987       }
1988 
1989       /* An output cannot match more than one input, so remove the output from
1990        * the set of possible outputs.
1991        */
1992       outputs[match_index] = NULL;
1993       num_outputs--;
1994       if (match_index < num_outputs)
1995          outputs[match_index] = outputs[num_outputs];
1996 
1997       /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1998        *
1999        *    "Tessellation control shader per-vertex output variables and
2000        *     blocks and tessellation control, tessellation evaluation, and
2001        *     geometry shader per-vertex input variables and blocks are
2002        *     required to be declared as arrays, with each element representing
2003        *     input or output values for a single vertex of a multi-vertex
2004        *     primitive. For the purposes of interface matching, such variables
2005        *     and blocks are treated as though they were not declared as
2006        *     arrays."
2007        *
2008        * So we unwrap those types before matching.
2009        */
2010       const glsl_type *consumer_type = consumer_var->type;
2011       const glsl_type *consumer_interface_type = consumer_var->interface_type;
2012       const glsl_type *producer_type = producer_var->type;
2013       const glsl_type *producer_interface_type = producer_var->interface_type;
2014 
2015       if (consumer_is_array_stage) {
2016          if (consumer_interface_type) {
2017             /* the interface is the array; the underlying types should match */
2018             if (glsl_type_is_array(consumer_interface_type) && !consumer_var->patch)
2019                consumer_interface_type = consumer_interface_type->fields.array;
2020          } else {
2021             if (glsl_type_is_array(consumer_type) && !consumer_var->patch)
2022                consumer_type = consumer_type->fields.array;
2023          }
2024       }
2025 
2026       if (producer_is_array_stage) {
2027          if (producer_interface_type) {
2028             /* the interface is the array; the underlying types should match */
2029             if (glsl_type_is_array(producer_interface_type) && !producer_var->patch)
2030                producer_interface_type = producer_interface_type->fields.array;
2031          } else {
2032             if (glsl_type_is_array(producer_type) && !producer_var->patch)
2033                producer_type = producer_type->fields.array;
2034          }
2035       }
2036 
2037       if (producer_type != consumer_type) {
2038          valid = false;
2039          goto out;
2040       }
2041 
2042       if (producer_interface_type != consumer_interface_type) {
2043          valid = false;
2044          goto out;
2045       }
2046 
2047       /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
2048        *
2049        *    Qualifier Class|  Qualifier  |in/out
2050        *    ---------------+-------------+------
2051        *    Storage        |     in      |
2052        *                   |     out     |  N/A
2053        *                   |   uniform   |
2054        *    ---------------+-------------+------
2055        *    Auxiliary      |   centroid  |   No
2056        *    ---------------+-------------+------
2057        *                   |   location  |  Yes
2058        *                   | Block layout|  N/A
2059        *                   |   binding   |  N/A
2060        *                   |   offset    |  N/A
2061        *                   |   format    |  N/A
2062        *    ---------------+-------------+------
2063        *    Interpolation  |   smooth    |
2064        *                   |    flat     |  Yes
2065        *    ---------------+-------------+------
2066        *                   |    lowp     |
2067        *    Precision      |   mediump   |  Yes
2068        *                   |    highp    |
2069        *    ---------------+-------------+------
2070        *    Variance       |  invariant  |   No
2071        *    ---------------+-------------+------
2072        *    Memory         |     all     |  N/A
2073        *
2074        * Note that location mismatches are detected by the loops above that
2075        * find the producer variable that goes with the consumer variable.
2076        */
2077       unsigned producer_interpolation = producer_var->interpolation;
2078       unsigned consumer_interpolation = consumer_var->interpolation;
2079       if (producer_interpolation == INTERP_MODE_NONE)
2080          producer_interpolation = INTERP_MODE_SMOOTH;
2081       if (consumer_interpolation == INTERP_MODE_NONE)
2082          consumer_interpolation = INTERP_MODE_SMOOTH;
2083       if (producer_interpolation != consumer_interpolation) {
2084          valid = false;
2085          goto out;
2086       }
2087 
2088       if (producer_var->precision != consumer_var->precision) {
2089          valid = false;
2090          goto out;
2091       }
2092 
2093       if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
2094          valid = false;
2095          goto out;
2096       }
2097    }
2098 
2099  out:
2100    free(outputs);
2101    return valid && num_outputs == 0;
2102 }
2103 
2104 /**
2105  * Validate inputs against outputs in a program pipeline.
2106  */
2107 extern "C" bool
_mesa_validate_pipeline_io(struct gl_pipeline_object * pipeline)2108 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
2109 {
2110    struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
2111 
2112    /* Find first active stage in pipeline. */
2113    unsigned idx, prev = 0;
2114    for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
2115       if (prog[idx]) {
2116          prev = idx;
2117          break;
2118       }
2119    }
2120 
2121    for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
2122       if (prog[idx]) {
2123          /* Pipeline might include both non-compute and a compute program, do
2124           * not attempt to validate varyings between non-compute and compute
2125           * stage.
2126           */
2127          if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
2128             break;
2129 
2130          if (!validate_io(prog[prev], prog[idx]))
2131             return false;
2132 
2133          prev = idx;
2134       }
2135    }
2136    return true;
2137 }
2138 
2139 extern "C" void
_mesa_program_resource_hash_destroy(struct gl_shader_program * shProg)2140 _mesa_program_resource_hash_destroy(struct gl_shader_program *shProg)
2141 {
2142    for (unsigned i = 0; i < ARRAY_SIZE(shProg->data->ProgramResourceHash); i++) {
2143       if (shProg->data->ProgramResourceHash[i]) {
2144          _mesa_hash_table_destroy(shProg->data->ProgramResourceHash[i], NULL);
2145          shProg->data->ProgramResourceHash[i] = NULL;
2146       }
2147    }
2148 }
2149 
2150 extern "C" void
_mesa_create_program_resource_hash(struct gl_shader_program * shProg)2151 _mesa_create_program_resource_hash(struct gl_shader_program *shProg)
2152 {
2153    /* Rebuild resource hash. */
2154    _mesa_program_resource_hash_destroy(shProg);
2155 
2156    struct gl_program_resource *res = shProg->data->ProgramResourceList;
2157    for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
2158       struct gl_resource_name name;
2159       if (_mesa_program_get_resource_name(res, &name)) {
2160          unsigned type = GET_PROGRAM_RESOURCE_TYPE_FROM_GLENUM(res->Type);
2161          assert(type < ARRAY_SIZE(shProg->data->ProgramResourceHash));
2162 
2163          if (!shProg->data->ProgramResourceHash[type]) {
2164             shProg->data->ProgramResourceHash[type] =
2165                _mesa_hash_table_create(shProg, _mesa_hash_string,
2166                                        _mesa_key_string_equal);
2167          }
2168 
2169          _mesa_hash_table_insert(shProg->data->ProgramResourceHash[type],
2170                                  name.string, res);
2171       }
2172    }
2173 }
2174