2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/context.h"
32 #include "main/core.h"
33 #include "main/enums.h"
34 #include "main/shaderapi.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "compiler/glsl/glsl_symbol_table.h"
38 #include "compiler/glsl/ir.h"
39 #include "compiler/glsl/program.h"
40 #include "program/hash_table.h"
41 #include "util/strndup.h"
45 program_resource_location(struct gl_program_resource
*res
,
46 unsigned array_index
);
49 * Declare convenience functions to return resource data in a given type.
50 * Warning! this is not type safe so be *very* careful when using these.
52 #define DECL_RESOURCE_FUNC(name, type) \
53 const type * RESOURCE_ ## name (gl_program_resource *res) { \
55 return (type *) res->Data; \
58 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
59 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
60 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
61 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
62 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
63 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
64 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
67 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
70 GET_CURRENT_CONTEXT(ctx
);
72 struct gl_shader_program
*const shProg
=
73 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
80 if (strncmp(name
, "gl_", 3) == 0) {
81 _mesa_error(ctx
, GL_INVALID_OPERATION
,
82 "glBindAttribLocation(illegal name)");
86 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
87 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
91 /* Replace the current value if it's already in the list. Add
92 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
93 * between built-in attributes and user-defined attributes.
95 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
98 * Note that this attribute binding won't go into effect until
99 * glLinkProgram is called again.
104 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
105 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
106 GLenum
* type
, GLchar
* name
)
108 GET_CURRENT_CONTEXT(ctx
);
109 struct gl_shader_program
*shProg
;
112 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
116 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
120 if (!shProg
->LinkStatus
) {
121 _mesa_error(ctx
, GL_INVALID_VALUE
,
122 "glGetActiveAttrib(program not linked)");
126 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
127 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
131 struct gl_program_resource
*res
=
132 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
135 /* User asked for index that does not exist. */
137 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
141 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
143 const char *var_name
= var
->name
;
145 _mesa_copy_string(name
, maxLength
, length
, var_name
);
148 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
149 size
, "glGetActiveAttrib");
152 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
153 (GLint
*) type
, "glGetActiveAttrib");
157 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
159 GET_CURRENT_CONTEXT(ctx
);
160 struct gl_shader_program
*const shProg
=
161 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
167 if (!shProg
->LinkStatus
) {
168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
169 "glGetAttribLocation(program not linked)");
176 /* Not having a vertex shader is not an error.
178 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
181 unsigned array_index
= 0;
182 struct gl_program_resource
*res
=
183 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
189 return program_resource_location(res
, array_index
);
193 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
195 if (!shProg
->LinkStatus
196 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
200 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
202 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
203 if (res
->Type
== GL_PROGRAM_INPUT
&&
204 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
))
212 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
214 if (!shProg
->LinkStatus
215 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
219 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
221 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
222 if (res
->Type
== GL_PROGRAM_INPUT
&&
223 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
225 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
226 if (length
>= longest
)
227 longest
= length
+ 1;
235 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
238 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
242 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
243 GLuint index
, const GLchar
*name
)
245 GET_CURRENT_CONTEXT(ctx
);
247 struct gl_shader_program
*const shProg
=
248 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
255 if (strncmp(name
, "gl_", 3) == 0) {
256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
261 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
265 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
270 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
271 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
275 /* Replace the current value if it's already in the list. Add
276 * FRAG_RESULT_DATA0 because that's how the linker differentiates
277 * between built-in attributes and user-defined attributes.
279 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
280 shProg
->FragDataIndexBindings
->put(index
, name
);
282 * Note that this binding won't go into effect until
283 * glLinkProgram is called again.
289 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
291 GET_CURRENT_CONTEXT(ctx
);
292 struct gl_shader_program
*const shProg
=
293 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
299 if (!shProg
->LinkStatus
) {
300 _mesa_error(ctx
, GL_INVALID_OPERATION
,
301 "glGetFragDataIndex(program not linked)");
308 if (strncmp(name
, "gl_", 3) == 0) {
309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
310 "glGetFragDataIndex(illegal name)");
314 /* Not having a fragment shader is not an error.
316 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
319 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
324 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
326 GET_CURRENT_CONTEXT(ctx
);
327 struct gl_shader_program
*const shProg
=
328 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
334 if (!shProg
->LinkStatus
) {
335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
336 "glGetFragDataLocation(program not linked)");
343 if (strncmp(name
, "gl_", 3) == 0) {
344 _mesa_error(ctx
, GL_INVALID_OPERATION
,
345 "glGetFragDataLocation(illegal name)");
349 /* Not having a fragment shader is not an error.
351 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
354 unsigned array_index
= 0;
355 struct gl_program_resource
*res
=
356 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
362 return program_resource_location(res
, array_index
);
366 _mesa_program_resource_name(struct gl_program_resource
*res
)
369 case GL_UNIFORM_BLOCK
:
370 case GL_SHADER_STORAGE_BLOCK
:
371 return RESOURCE_UBO(res
)->Name
;
372 case GL_TRANSFORM_FEEDBACK_VARYING
:
373 return RESOURCE_XFV(res
)->Name
;
374 case GL_PROGRAM_INPUT
:
375 case GL_PROGRAM_OUTPUT
:
376 return RESOURCE_VAR(res
)->name
;
378 case GL_BUFFER_VARIABLE
:
379 return RESOURCE_UNI(res
)->name
;
380 case GL_VERTEX_SUBROUTINE_UNIFORM
:
381 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
382 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
383 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
384 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
385 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
386 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
387 case GL_VERTEX_SUBROUTINE
:
388 case GL_GEOMETRY_SUBROUTINE
:
389 case GL_FRAGMENT_SUBROUTINE
:
390 case GL_COMPUTE_SUBROUTINE
:
391 case GL_TESS_CONTROL_SUBROUTINE
:
392 case GL_TESS_EVALUATION_SUBROUTINE
:
393 return RESOURCE_SUB(res
)->name
;
395 assert(!"support for resource type not implemented");
402 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
405 case GL_TRANSFORM_FEEDBACK_VARYING
:
406 return RESOURCE_XFV(res
)->Size
> 1 ?
407 RESOURCE_XFV(res
)->Size
: 0;
408 case GL_PROGRAM_INPUT
:
409 case GL_PROGRAM_OUTPUT
:
410 return RESOURCE_VAR(res
)->type
->length
;
412 case GL_VERTEX_SUBROUTINE_UNIFORM
:
413 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
414 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
415 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
416 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
417 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
418 return RESOURCE_UNI(res
)->array_elements
;
419 case GL_BUFFER_VARIABLE
:
421 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
422 RESOURCE_UNI(res
)->array_elements
== 0)
425 return RESOURCE_UNI(res
)->array_elements
;
426 case GL_VERTEX_SUBROUTINE
:
427 case GL_GEOMETRY_SUBROUTINE
:
428 case GL_FRAGMENT_SUBROUTINE
:
429 case GL_COMPUTE_SUBROUTINE
:
430 case GL_TESS_CONTROL_SUBROUTINE
:
431 case GL_TESS_EVALUATION_SUBROUTINE
:
432 case GL_ATOMIC_COUNTER_BUFFER
:
433 case GL_UNIFORM_BLOCK
:
434 case GL_SHADER_STORAGE_BLOCK
:
437 assert(!"support for resource type not implemented");
443 * Checks if array subscript is valid and if so sets array_index.
446 valid_array_index(const GLchar
*name
, unsigned *array_index
)
449 const GLchar
*out_base_name_end
;
451 idx
= parse_program_resource_name(name
, &out_base_name_end
);
461 /* Find a program resource with specific name in given interface.
463 struct gl_program_resource
*
464 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
465 GLenum programInterface
, const char *name
,
466 unsigned *array_index
)
468 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
469 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
470 if (res
->Type
!= programInterface
)
473 /* Resource basename. */
474 const char *rname
= _mesa_program_resource_name(res
);
475 unsigned baselen
= strlen(rname
);
476 unsigned baselen_without_array_index
= baselen
;
477 const char *rname_last_square_bracket
= strrchr(rname
, '[');
479 bool rname_has_array_index_zero
= false;
480 /* From ARB_program_interface_query spec:
482 * "uint GetProgramResourceIndex(uint program, enum programInterface,
485 * If <name> exactly matches the name string of one of the active
486 * resources for <programInterface>, the index of the matched resource is
487 * returned. Additionally, if <name> would exactly match the name string
488 * of an active resource if "[0]" were appended to <name>, the index of
489 * the matched resource is returned. [...]"
491 * "A string provided to GetProgramResourceLocation or
492 * GetProgramResourceLocationIndex is considered to match an active variable
495 * * the string exactly matches the name of the active variable;
497 * * if the string identifies the base name of an active array, where the
498 * string would exactly match the name of the variable if the suffix
499 * "[0]" were appended to the string; [...]"
501 /* Remove array's index from interface block name comparison only if
502 * array's index is zero and the resulting string length is the same
503 * than the provided name's length.
505 if (rname_last_square_bracket
) {
506 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
507 rname_has_array_index_zero
=
508 (strncmp(rname_last_square_bracket
, "[0]\0", 4) == 0) &&
509 (baselen_without_array_index
== strlen(name
));
512 if (strncmp(rname
, name
, baselen
) == 0)
514 else if (rname_has_array_index_zero
&&
515 strncmp(rname
, name
, baselen_without_array_index
) == 0)
519 switch (programInterface
) {
520 case GL_UNIFORM_BLOCK
:
521 case GL_SHADER_STORAGE_BLOCK
:
522 /* Basename match, check if array or struct. */
523 if (rname_has_array_index_zero
||
524 name
[baselen
] == '\0' ||
525 name
[baselen
] == '[' ||
526 name
[baselen
] == '.') {
530 case GL_TRANSFORM_FEEDBACK_VARYING
:
531 case GL_BUFFER_VARIABLE
:
533 case GL_VERTEX_SUBROUTINE_UNIFORM
:
534 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
535 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
536 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
537 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
538 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
539 case GL_VERTEX_SUBROUTINE
:
540 case GL_GEOMETRY_SUBROUTINE
:
541 case GL_FRAGMENT_SUBROUTINE
:
542 case GL_COMPUTE_SUBROUTINE
:
543 case GL_TESS_CONTROL_SUBROUTINE
:
544 case GL_TESS_EVALUATION_SUBROUTINE
:
545 if (name
[baselen
] == '.') {
549 case GL_PROGRAM_INPUT
:
550 case GL_PROGRAM_OUTPUT
:
551 if (name
[baselen
] == '\0') {
553 } else if (name
[baselen
] == '[' &&
554 valid_array_index(name
, array_index
)) {
559 assert(!"not implemented for given interface");
567 calc_resource_index(struct gl_shader_program
*shProg
,
568 struct gl_program_resource
*res
)
572 for (i
= 0; i
< shProg
->NumProgramResourceList
; i
++) {
573 if (&shProg
->ProgramResourceList
[i
] == res
)
575 if (shProg
->ProgramResourceList
[i
].Type
== res
->Type
)
578 return GL_INVALID_INDEX
;
582 * Calculate index for the given resource.
585 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
586 struct gl_program_resource
*res
)
589 return GL_INVALID_INDEX
;
592 case GL_ATOMIC_COUNTER_BUFFER
:
593 return RESOURCE_ATC(res
) - shProg
->AtomicBuffers
;
594 case GL_VERTEX_SUBROUTINE
:
595 case GL_GEOMETRY_SUBROUTINE
:
596 case GL_FRAGMENT_SUBROUTINE
:
597 case GL_COMPUTE_SUBROUTINE
:
598 case GL_TESS_CONTROL_SUBROUTINE
:
599 case GL_TESS_EVALUATION_SUBROUTINE
:
600 return RESOURCE_SUB(res
)->index
;
601 case GL_UNIFORM_BLOCK
:
602 case GL_SHADER_STORAGE_BLOCK
:
603 case GL_TRANSFORM_FEEDBACK_BUFFER
:
604 case GL_TRANSFORM_FEEDBACK_VARYING
:
606 return calc_resource_index(shProg
, res
);
611 * Find a program resource that points to given data.
613 static struct gl_program_resource
*
614 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
616 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
617 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
618 if (res
->Data
== data
)
624 /* Find a program resource with specific index in given interface.
626 struct gl_program_resource
*
627 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
628 GLenum programInterface
, GLuint index
)
630 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
633 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
634 if (res
->Type
!= programInterface
)
638 case GL_UNIFORM_BLOCK
:
639 case GL_ATOMIC_COUNTER_BUFFER
:
640 case GL_SHADER_STORAGE_BLOCK
:
641 case GL_TRANSFORM_FEEDBACK_BUFFER
:
642 if (_mesa_program_resource_index(shProg
, res
) == index
)
645 case GL_TRANSFORM_FEEDBACK_VARYING
:
646 case GL_PROGRAM_INPUT
:
647 case GL_PROGRAM_OUTPUT
:
649 case GL_VERTEX_SUBROUTINE_UNIFORM
:
650 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
651 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
652 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
653 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
654 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
655 case GL_VERTEX_SUBROUTINE
:
656 case GL_GEOMETRY_SUBROUTINE
:
657 case GL_FRAGMENT_SUBROUTINE
:
658 case GL_COMPUTE_SUBROUTINE
:
659 case GL_TESS_CONTROL_SUBROUTINE
:
660 case GL_TESS_EVALUATION_SUBROUTINE
:
661 case GL_BUFFER_VARIABLE
:
662 if (++idx
== (int) index
)
666 assert(!"not implemented for given interface");
672 /* Function returns if resource name is expected to have index
676 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
679 * "If the active uniform is an array, the uniform name returned in
680 * name will always be the name of the uniform array appended with
683 * The same text also appears in the OpenGL 4.2 spec. It does not,
684 * however, appear in any previous spec. Previous specifications are
685 * ambiguous in this regard. However, either name can later be passed
686 * to glGetUniformLocation (and related APIs), so there shouldn't be any
687 * harm in always appending "[0]" to uniform array names.
689 * Geometry shader stage has different naming convention where the 'normal'
690 * condition is an array, therefore for variables referenced in geometry
691 * stage we do not add '[0]'.
693 * Note, that TCS outputs and TES inputs should not have index appended
697 add_index_to_name(struct gl_program_resource
*res
)
699 bool add_index
= !((res
->Type
== GL_PROGRAM_INPUT
&&
700 res
->StageReferences
& (1 << MESA_SHADER_GEOMETRY
|
701 1 << MESA_SHADER_TESS_CTRL
|
702 1 << MESA_SHADER_TESS_EVAL
)) ||
703 (res
->Type
== GL_PROGRAM_OUTPUT
&&
704 res
->StageReferences
& 1 << MESA_SHADER_TESS_CTRL
));
706 /* Transform feedback varyings have array index already appended
709 if (res
->Type
== GL_TRANSFORM_FEEDBACK_VARYING
)
715 /* Get name length of a program resource. This consists of
716 * base name + 3 for '[0]' if resource is an array.
719 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
721 unsigned length
= strlen(_mesa_program_resource_name(res
));
722 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
727 /* Get full name of a program resource.
730 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
731 GLenum programInterface
, GLuint index
,
732 GLsizei bufSize
, GLsizei
*length
,
733 GLchar
*name
, const char *caller
)
735 GET_CURRENT_CONTEXT(ctx
);
737 /* Find resource with given interface and index. */
738 struct gl_program_resource
*res
=
739 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
741 /* The error INVALID_VALUE is generated if <index> is greater than
742 * or equal to the number of entries in the active resource list for
743 * <programInterface>.
746 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
751 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
758 length
= &localLength
;
760 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
762 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
765 /* The comparison is strange because *length does *NOT* include the
766 * terminating NUL, but maxLength does.
768 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
769 name
[*length
+ i
] = "[0]"[i
];
771 name
[*length
+ i
] = '\0';
778 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
781 case GL_PROGRAM_INPUT
: {
782 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
784 if (var
->location
== -1)
787 /* If the input is an array, fail if the index is out of bounds. */
789 && array_index
>= var
->type
->length
) {
792 return var
->location
+
793 (array_index
* var
->type
->without_array()->matrix_columns
);
795 case GL_PROGRAM_OUTPUT
:
796 if (RESOURCE_VAR(res
)->location
== -1)
799 /* If the output is an array, fail if the index is out of bounds. */
801 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
804 return RESOURCE_VAR(res
)->location
+ array_index
;
806 /* If the uniform is built-in, fail. */
807 if (RESOURCE_UNI(res
)->builtin
)
810 /* From page 79 of the OpenGL 4.2 spec:
812 * "A valid name cannot be a structure, an array of structures, or any
813 * portion of a single vector or a matrix."
815 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
818 /* From the GL_ARB_uniform_buffer_object spec:
820 * "The value -1 will be returned if <name> does not correspond to an
821 * active uniform variable name in <program>, if <name> is associated
822 * with a named uniform block, or if <name> starts with the reserved
825 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
826 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
830 case GL_VERTEX_SUBROUTINE_UNIFORM
:
831 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
832 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
833 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
834 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
835 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
836 /* If the uniform is an array, fail if the index is out of bounds. */
838 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
842 /* location in remap table + array element offset */
843 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
850 * Function implements following location queries:
851 * glGetUniformLocation
854 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
855 GLenum programInterface
, const char *name
)
857 unsigned array_index
= 0;
858 struct gl_program_resource
*res
=
859 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
862 /* Resource not found. */
866 return program_resource_location(res
, array_index
);
870 * Function implements following index queries:
874 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
875 GLenum programInterface
, const char *name
)
877 struct gl_program_resource
*res
=
878 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
880 /* Non-existent variable or resource is not referenced by fragment stage. */
881 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
884 return RESOURCE_VAR(res
)->index
;
888 stage_from_enum(GLenum ref
)
891 case GL_REFERENCED_BY_VERTEX_SHADER
:
892 return MESA_SHADER_VERTEX
;
893 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
894 return MESA_SHADER_TESS_CTRL
;
895 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
896 return MESA_SHADER_TESS_EVAL
;
897 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
898 return MESA_SHADER_GEOMETRY
;
899 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
900 return MESA_SHADER_FRAGMENT
;
901 case GL_REFERENCED_BY_COMPUTE_SHADER
:
902 return MESA_SHADER_COMPUTE
;
904 assert(!"shader stage not supported");
905 return MESA_SHADER_STAGES
;
910 * Check if resource is referenced by given 'referenced by' stage enum.
911 * ATC and UBO resources hold stage references of their own.
914 is_resource_referenced(struct gl_shader_program
*shProg
,
915 struct gl_program_resource
*res
,
916 GLuint index
, uint8_t stage
)
918 /* First, check if we even have such a stage active. */
919 if (!shProg
->_LinkedShaders
[stage
])
922 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
923 return RESOURCE_ATC(res
)->StageReferences
[stage
];
925 if (res
->Type
== GL_UNIFORM_BLOCK
)
926 return shProg
->UniformBlocks
[index
].stageref
& (1 << stage
);
928 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
929 return shProg
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
931 return res
->StageReferences
& (1 << stage
);
935 get_buffer_property(struct gl_shader_program
*shProg
,
936 struct gl_program_resource
*res
, const GLenum prop
,
937 GLint
*val
, const char *caller
)
939 GET_CURRENT_CONTEXT(ctx
);
940 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
941 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
942 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
943 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
944 goto invalid_operation
;
946 if (res
->Type
== GL_UNIFORM_BLOCK
) {
948 case GL_BUFFER_BINDING
:
949 *val
= RESOURCE_UBO(res
)->Binding
;
951 case GL_BUFFER_DATA_SIZE
:
952 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
954 case GL_NUM_ACTIVE_VARIABLES
:
956 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
957 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
958 struct gl_program_resource
*uni
=
959 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
966 case GL_ACTIVE_VARIABLES
: {
967 unsigned num_values
= 0;
968 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
969 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
970 struct gl_program_resource
*uni
=
971 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
976 _mesa_program_resource_index(shProg
, uni
);
982 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
984 case GL_BUFFER_BINDING
:
985 *val
= RESOURCE_UBO(res
)->Binding
;
987 case GL_BUFFER_DATA_SIZE
:
988 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
990 case GL_NUM_ACTIVE_VARIABLES
:
992 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
993 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
994 struct gl_program_resource
*uni
=
995 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1002 case GL_ACTIVE_VARIABLES
: {
1003 unsigned num_values
= 0;
1004 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1005 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1006 struct gl_program_resource
*uni
=
1007 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1012 _mesa_program_resource_index(shProg
, uni
);
1018 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1020 case GL_BUFFER_BINDING
:
1021 *val
= RESOURCE_ATC(res
)->Binding
;
1023 case GL_BUFFER_DATA_SIZE
:
1024 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1026 case GL_NUM_ACTIVE_VARIABLES
:
1027 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1029 case GL_ACTIVE_VARIABLES
:
1030 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1031 /* Active atomic buffer contains index to UniformStorage. Find
1032 * out gl_program_resource via data pointer and then calculate
1033 * index of that uniform.
1035 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1036 struct gl_program_resource
*uni
=
1037 program_resource_find_data(shProg
,
1038 &shProg
->UniformStorage
[idx
]);
1040 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1042 return RESOURCE_ATC(res
)->NumUniforms
;
1044 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1046 case GL_BUFFER_BINDING
:
1047 *val
= RESOURCE_XFB(res
)->Binding
;
1049 case GL_NUM_ACTIVE_VARIABLES
:
1050 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1052 case GL_ACTIVE_VARIABLES
:
1054 for ( ; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
1056 shProg
->LinkedTransformFeedback
.Varyings
[i
].BufferIndex
;
1057 struct gl_program_resource
*buf_res
=
1058 _mesa_program_resource_find_index(shProg
,
1059 GL_TRANSFORM_FEEDBACK_BUFFER
,
1062 if (res
== buf_res
) {
1066 return RESOURCE_XFB(res
)->NumVaryings
;
1069 assert(!"support for property type not implemented");
1072 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1073 _mesa_enum_to_string(res
->Type
),
1074 _mesa_enum_to_string(prop
));
1080 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1081 struct gl_program_resource
*res
, GLuint index
,
1082 const GLenum prop
, GLint
*val
, const char *caller
)
1084 GET_CURRENT_CONTEXT(ctx
);
1086 #define VALIDATE_TYPE(type)\
1087 if (res->Type != type)\
1088 goto invalid_operation;
1090 #define VALIDATE_TYPE_2(type1, type2)\
1091 if (res->Type != type1 && res->Type != type2)\
1092 goto invalid_operation;
1095 case GL_NAME_LENGTH
:
1096 switch (res
->Type
) {
1097 case GL_ATOMIC_COUNTER_BUFFER
:
1098 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1099 goto invalid_operation
;
1101 /* Resource name length + terminator. */
1102 *val
= _mesa_program_resource_name_len(res
) + 1;
1106 switch (res
->Type
) {
1108 case GL_BUFFER_VARIABLE
:
1109 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1111 case GL_PROGRAM_INPUT
:
1112 case GL_PROGRAM_OUTPUT
:
1113 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1115 case GL_TRANSFORM_FEEDBACK_VARYING
:
1116 *val
= RESOURCE_XFV(res
)->Type
;
1119 goto invalid_operation
;
1122 switch (res
->Type
) {
1124 case GL_BUFFER_VARIABLE
:
1125 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1126 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1127 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1128 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1129 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1130 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1132 /* Test if a buffer variable is an array or an unsized array.
1133 * Unsized arrays return zero as array size.
1135 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1136 RESOURCE_UNI(res
)->array_stride
> 0)
1137 *val
= RESOURCE_UNI(res
)->array_elements
;
1139 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1141 case GL_PROGRAM_INPUT
:
1142 case GL_PROGRAM_OUTPUT
:
1143 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1145 case GL_TRANSFORM_FEEDBACK_VARYING
:
1146 *val
= MAX2(RESOURCE_XFV(res
)->Size
, 1);
1149 goto invalid_operation
;
1152 switch (res
->Type
) {
1154 case GL_BUFFER_VARIABLE
:
1155 *val
= RESOURCE_UNI(res
)->offset
;
1157 case GL_TRANSFORM_FEEDBACK_VARYING
:
1158 *val
= RESOURCE_XFV(res
)->Offset
;
1161 goto invalid_operation
;
1163 case GL_BLOCK_INDEX
:
1164 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1165 *val
= RESOURCE_UNI(res
)->block_index
;
1167 case GL_ARRAY_STRIDE
:
1168 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1169 *val
= RESOURCE_UNI(res
)->array_stride
;
1171 case GL_MATRIX_STRIDE
:
1172 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1173 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1175 case GL_IS_ROW_MAJOR
:
1176 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1177 *val
= RESOURCE_UNI(res
)->row_major
;
1179 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1180 VALIDATE_TYPE(GL_UNIFORM
);
1181 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1183 case GL_BUFFER_BINDING
:
1184 case GL_BUFFER_DATA_SIZE
:
1185 case GL_NUM_ACTIVE_VARIABLES
:
1186 case GL_ACTIVE_VARIABLES
:
1187 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1188 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1189 if (!_mesa_has_compute_shaders(ctx
))
1192 case GL_REFERENCED_BY_VERTEX_SHADER
:
1193 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1194 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1195 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1196 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1197 switch (res
->Type
) {
1199 case GL_PROGRAM_INPUT
:
1200 case GL_PROGRAM_OUTPUT
:
1201 case GL_UNIFORM_BLOCK
:
1202 case GL_BUFFER_VARIABLE
:
1203 case GL_SHADER_STORAGE_BLOCK
:
1204 case GL_ATOMIC_COUNTER_BUFFER
:
1205 *val
= is_resource_referenced(shProg
, res
, index
,
1206 stage_from_enum(prop
));
1209 goto invalid_operation
;
1212 switch (res
->Type
) {
1214 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1215 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1216 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1217 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1218 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1219 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1220 case GL_PROGRAM_INPUT
:
1221 case GL_PROGRAM_OUTPUT
:
1222 *val
= program_resource_location(res
, 0);
1225 goto invalid_operation
;
1227 case GL_LOCATION_COMPONENT
:
1228 switch (res
->Type
) {
1229 case GL_PROGRAM_INPUT
:
1230 case GL_PROGRAM_OUTPUT
:
1231 *val
= RESOURCE_VAR(res
)->component
;
1234 goto invalid_operation
;
1236 case GL_LOCATION_INDEX
:
1237 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1238 goto invalid_operation
;
1239 *val
= RESOURCE_VAR(res
)->index
;
1242 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1243 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1244 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1245 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1246 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1247 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1248 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1249 goto invalid_operation
;
1250 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1252 case GL_COMPATIBLE_SUBROUTINES
: {
1253 const struct gl_uniform_storage
*uni
;
1254 struct gl_shader
*sh
;
1258 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1259 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1260 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1261 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1262 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1263 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1264 goto invalid_operation
;
1265 uni
= RESOURCE_UNI(res
);
1267 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1269 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1270 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1271 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1272 if (fn
->types
[j
] == uni
->type
) {
1281 case GL_TOP_LEVEL_ARRAY_SIZE
:
1282 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1283 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1286 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1287 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1288 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1291 /* GL_ARB_tessellation_shader */
1292 case GL_IS_PER_PATCH
:
1293 switch (res
->Type
) {
1294 case GL_PROGRAM_INPUT
:
1295 case GL_PROGRAM_OUTPUT
:
1296 *val
= RESOURCE_VAR(res
)->patch
;
1299 goto invalid_operation
;
1302 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1303 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1304 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1306 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1307 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1308 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1315 #undef VALIDATE_TYPE
1316 #undef VALIDATE_TYPE_2
1319 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1320 _mesa_enum_to_string(res
->Type
),
1321 _mesa_enum_to_string(prop
));
1325 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1326 _mesa_enum_to_string(res
->Type
),
1327 _mesa_enum_to_string(prop
));
1332 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1333 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1334 const GLenum
*props
, GLsizei bufSize
,
1335 GLsizei
*length
, GLint
*params
)
1337 GET_CURRENT_CONTEXT(ctx
);
1338 GLint
*val
= (GLint
*) params
;
1339 const GLenum
*prop
= props
;
1342 struct gl_program_resource
*res
=
1343 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1345 /* No such resource found or bufSize negative. */
1346 if (!res
|| bufSize
< 0) {
1347 _mesa_error(ctx
, GL_INVALID_VALUE
,
1348 "glGetProgramResourceiv(%s index %d bufSize %d)",
1349 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1353 /* Write propCount values until error occurs or bufSize reached. */
1354 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1356 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1357 "glGetProgramResourceiv");
1359 /* Error happened. */
1360 if (props_written
== 0)
1363 amount
+= props_written
;
1366 /* If <length> is not NULL, the actual number of integer values
1367 * written to <params> will be written to <length>.
1374 validate_io(struct gl_shader_program
*producer
,
1375 struct gl_shader_program
*consumer
)
1377 if (producer
== consumer
)
1382 gl_shader_variable
const **outputs
=
1383 (gl_shader_variable
const **) calloc(producer
->NumProgramResourceList
,
1384 sizeof(gl_shader_variable
*));
1385 if (outputs
== NULL
)
1388 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1391 * At an interface between program objects, the set of inputs and
1392 * outputs are considered to match exactly if and only if:
1394 * - Every declared input variable has a matching output, as described
1396 * - There are no user-defined output variables declared without a
1397 * matching input variable declaration.
1399 * Every input has an output, and every output has an input. Scan the list
1400 * of producer resources once, and generate the list of outputs. As inputs
1401 * and outputs are matched, remove the matched outputs from the set. At
1402 * the end, the set must be empty. If the set is not empty, then there is
1403 * some output that did not have an input.
1405 unsigned num_outputs
= 0;
1406 for (unsigned i
= 0; i
< producer
->NumProgramResourceList
; i
++) {
1407 struct gl_program_resource
*res
= &producer
->ProgramResourceList
[i
];
1409 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1412 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1414 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1417 * Built-in inputs or outputs do not affect interface matching.
1419 if (is_gl_identifier(var
->name
))
1422 outputs
[num_outputs
++] = var
;
1425 unsigned match_index
= 0;
1426 for (unsigned i
= 0; i
< consumer
->NumProgramResourceList
; i
++) {
1427 struct gl_program_resource
*res
= &consumer
->ProgramResourceList
[i
];
1429 if (res
->Type
!= GL_PROGRAM_INPUT
)
1432 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1433 gl_shader_variable
const *producer_var
= NULL
;
1435 if (is_gl_identifier(consumer_var
->name
))
1438 /* Inputs with explicit locations match other outputs with explicit
1439 * locations by location instead of by name.
1441 if (consumer_var
->explicit_location
) {
1442 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1443 const gl_shader_variable
*const var
= outputs
[j
];
1445 if (var
->explicit_location
&&
1446 consumer_var
->location
== var
->location
) {
1453 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1454 const gl_shader_variable
*const var
= outputs
[j
];
1456 if (!var
->explicit_location
&&
1457 strcmp(consumer_var
->name
, var
->name
) == 0) {
1465 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1468 * - An output variable is considered to match an input variable in
1469 * the subsequent shader if:
1471 * - the two variables match in name, type, and qualification; or
1473 * - the two variables are declared with the same location
1474 * qualifier and match in type and qualification.
1476 if (producer_var
== NULL
) {
1481 /* An output cannot match more than one input, so remove the output from
1482 * the set of possible outputs.
1484 outputs
[match_index
] = NULL
;
1486 if (match_index
< num_outputs
)
1487 outputs
[match_index
] = outputs
[num_outputs
];
1489 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1491 * Qualifier Class| Qualifier |in/out
1492 * ---------------+-------------+------
1496 * ---------------+-------------+------
1497 * Auxiliary | centroid | No
1498 * ---------------+-------------+------
1500 * | Block layout| N/A
1504 * ---------------+-------------+------
1505 * Interpolation | smooth |
1507 * ---------------+-------------+------
1509 * Precision | mediump | Yes
1511 * ---------------+-------------+------
1512 * Variance | invariant | No
1513 * ---------------+-------------+------
1514 * Memory | all | N/A
1516 * Note that location mismatches are detected by the loops above that
1517 * find the producer variable that goes with the consumer variable.
1519 if (producer_var
->type
!= consumer_var
->type
||
1520 producer_var
->interpolation
!= consumer_var
->interpolation
||
1521 producer_var
->precision
!= consumer_var
->precision
) {
1526 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1531 if (producer_var
->interface_type
!= consumer_var
->interface_type
) {
1539 return valid
&& num_outputs
== 0;
1543 * Validate inputs against outputs in a program pipeline.
1546 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1548 struct gl_shader_program
**shProg
=
1549 (struct gl_shader_program
**) pipeline
->CurrentProgram
;
1551 /* Find first active stage in pipeline. */
1552 unsigned idx
, prev
= 0;
1553 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1560 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1562 /* Pipeline might include both non-compute and a compute program, do
1563 * not attempt to validate varyings between non-compute and compute
1566 if (shProg
[idx
]->_LinkedShaders
[idx
]->Stage
== MESA_SHADER_COMPUTE
)
1569 if (!validate_io(shProg
[prev
], shProg
[idx
]))