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/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/program.h"
39 #include "compiler/glsl/string_to_uint_map.h"
43 program_resource_location(struct gl_program_resource
*res
,
44 unsigned array_index
);
47 * Declare convenience functions to return resource data in a given type.
48 * Warning! this is not type safe so be *very* careful when using these.
50 #define DECL_RESOURCE_FUNC(name, type) \
51 const type * RESOURCE_ ## name (gl_program_resource *res) { \
53 return (type *) res->Data; \
56 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
57 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
58 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
59 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
60 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
61 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
62 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
65 bind_attrib_location(struct gl_context
*ctx
,
66 struct gl_shader_program
*const shProg
, GLuint index
,
67 const GLchar
*name
, bool no_error
)
73 if (strncmp(name
, "gl_", 3) == 0) {
74 _mesa_error(ctx
, GL_INVALID_OPERATION
,
75 "glBindAttribLocation(illegal name)");
79 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
80 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(%u >= %u)",
81 index
, ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
);
86 /* Replace the current value if it's already in the list. Add
87 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
88 * between built-in attributes and user-defined attributes.
90 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
93 * Note that this attribute binding won't go into effect until
94 * glLinkProgram is called again.
99 _mesa_BindAttribLocation_no_error(GLuint program
, GLuint index
,
102 GET_CURRENT_CONTEXT(ctx
);
104 struct gl_shader_program
*const shProg
=
105 _mesa_lookup_shader_program(ctx
, program
);
106 bind_attrib_location(ctx
, shProg
, index
, name
, true);
110 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
113 GET_CURRENT_CONTEXT(ctx
);
115 struct gl_shader_program
*const shProg
=
116 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
120 bind_attrib_location(ctx
, shProg
, index
, name
, false);
124 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
125 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
126 GLenum
* type
, GLchar
* name
)
128 GET_CURRENT_CONTEXT(ctx
);
129 struct gl_shader_program
*shProg
;
132 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
136 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
140 if (!shProg
->data
->LinkStatus
) {
141 _mesa_error(ctx
, GL_INVALID_VALUE
,
142 "glGetActiveAttrib(program not linked)");
146 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
147 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
151 struct gl_program_resource
*res
=
152 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
155 /* User asked for index that does not exist. */
157 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
161 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
163 const char *var_name
= var
->name
;
165 _mesa_copy_string(name
, maxLength
, length
, var_name
);
168 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
169 size
, "glGetActiveAttrib");
172 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
173 (GLint
*) type
, "glGetActiveAttrib");
177 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
179 GET_CURRENT_CONTEXT(ctx
);
180 struct gl_shader_program
*const shProg
=
181 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
187 if (!shProg
->data
->LinkStatus
) {
188 _mesa_error(ctx
, GL_INVALID_OPERATION
,
189 "glGetAttribLocation(program not linked)");
196 /* Not having a vertex shader is not an error.
198 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
201 unsigned array_index
= 0;
202 struct gl_program_resource
*res
=
203 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
209 return program_resource_location(res
, array_index
);
213 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
215 if (!shProg
->data
->LinkStatus
216 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
220 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
222 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
224 if (res
->Type
== GL_PROGRAM_INPUT
&&
225 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
))
233 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
235 if (!shProg
->data
->LinkStatus
236 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
240 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
242 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
244 if (res
->Type
== GL_PROGRAM_INPUT
&&
245 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
247 /* From the ARB_gl_spirv spec:
249 * "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the
250 * longest active attribute name, including a null terminator, is
251 * returned. If no active attributes exist, zero is returned. If
252 * no name reflection information is available, one is returned."
254 const size_t length
= RESOURCE_VAR(res
)->name
!= NULL
?
255 strlen(RESOURCE_VAR(res
)->name
) : 0;
257 if (length
>= longest
)
258 longest
= length
+ 1;
266 bind_frag_data_location(struct gl_shader_program
*const shProg
,
267 const char *name
, unsigned colorNumber
,
270 /* Replace the current value if it's already in the list. Add
271 * FRAG_RESULT_DATA0 because that's how the linker differentiates
272 * between built-in attributes and user-defined attributes.
274 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
275 shProg
->FragDataIndexBindings
->put(index
, name
);
278 * Note that this binding won't go into effect until
279 * glLinkProgram is called again.
284 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
287 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
291 _mesa_BindFragDataLocation_no_error(GLuint program
, GLuint colorNumber
,
294 GET_CURRENT_CONTEXT(ctx
);
299 struct gl_shader_program
*const shProg
=
300 _mesa_lookup_shader_program(ctx
, program
);
302 bind_frag_data_location(shProg
, name
, colorNumber
, 0);
306 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
307 GLuint index
, const GLchar
*name
)
309 GET_CURRENT_CONTEXT(ctx
);
311 struct gl_shader_program
*const shProg
=
312 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
319 if (strncmp(name
, "gl_", 3) == 0) {
320 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
325 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
329 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
330 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
334 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
335 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
339 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
343 _mesa_BindFragDataLocationIndexed_no_error(GLuint program
, GLuint colorNumber
,
344 GLuint index
, const GLchar
*name
)
346 GET_CURRENT_CONTEXT(ctx
);
351 struct gl_shader_program
*const shProg
=
352 _mesa_lookup_shader_program(ctx
, program
);
354 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
358 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
360 GET_CURRENT_CONTEXT(ctx
);
361 struct gl_shader_program
*const shProg
=
362 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
368 if (!shProg
->data
->LinkStatus
) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
,
370 "glGetFragDataIndex(program not linked)");
377 if (strncmp(name
, "gl_", 3) == 0) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
379 "glGetFragDataIndex(illegal name)");
383 /* Not having a fragment shader is not an error.
385 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
388 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
393 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
395 GET_CURRENT_CONTEXT(ctx
);
396 struct gl_shader_program
*const shProg
=
397 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
403 if (!shProg
->data
->LinkStatus
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glGetFragDataLocation(program not linked)");
412 if (strncmp(name
, "gl_", 3) == 0) {
413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
414 "glGetFragDataLocation(illegal name)");
418 /* Not having a fragment shader is not an error.
420 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
423 unsigned array_index
= 0;
424 struct gl_program_resource
*res
=
425 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
431 return program_resource_location(res
, array_index
);
435 _mesa_program_resource_name(struct gl_program_resource
*res
)
438 case GL_UNIFORM_BLOCK
:
439 case GL_SHADER_STORAGE_BLOCK
:
440 return RESOURCE_UBO(res
)->Name
;
441 case GL_TRANSFORM_FEEDBACK_VARYING
:
442 return RESOURCE_XFV(res
)->Name
;
443 case GL_PROGRAM_INPUT
:
444 case GL_PROGRAM_OUTPUT
:
445 return RESOURCE_VAR(res
)->name
;
447 case GL_BUFFER_VARIABLE
:
448 return RESOURCE_UNI(res
)->name
;
449 case GL_VERTEX_SUBROUTINE_UNIFORM
:
450 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
451 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
452 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
453 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
454 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
455 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
456 case GL_VERTEX_SUBROUTINE
:
457 case GL_GEOMETRY_SUBROUTINE
:
458 case GL_FRAGMENT_SUBROUTINE
:
459 case GL_COMPUTE_SUBROUTINE
:
460 case GL_TESS_CONTROL_SUBROUTINE
:
461 case GL_TESS_EVALUATION_SUBROUTINE
:
462 return RESOURCE_SUB(res
)->name
;
464 assert(!"support for resource type not implemented");
471 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
474 case GL_TRANSFORM_FEEDBACK_VARYING
:
475 return RESOURCE_XFV(res
)->Size
> 1 ?
476 RESOURCE_XFV(res
)->Size
: 0;
477 case GL_PROGRAM_INPUT
:
478 case GL_PROGRAM_OUTPUT
:
479 return RESOURCE_VAR(res
)->type
->length
;
481 case GL_VERTEX_SUBROUTINE_UNIFORM
:
482 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
483 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
484 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
485 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
486 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
487 return RESOURCE_UNI(res
)->array_elements
;
488 case GL_BUFFER_VARIABLE
:
490 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
491 RESOURCE_UNI(res
)->array_elements
== 0)
494 return RESOURCE_UNI(res
)->array_elements
;
495 case GL_VERTEX_SUBROUTINE
:
496 case GL_GEOMETRY_SUBROUTINE
:
497 case GL_FRAGMENT_SUBROUTINE
:
498 case GL_COMPUTE_SUBROUTINE
:
499 case GL_TESS_CONTROL_SUBROUTINE
:
500 case GL_TESS_EVALUATION_SUBROUTINE
:
501 case GL_ATOMIC_COUNTER_BUFFER
:
502 case GL_UNIFORM_BLOCK
:
503 case GL_SHADER_STORAGE_BLOCK
:
506 assert(!"support for resource type not implemented");
512 * Checks if array subscript is valid and if so sets array_index.
515 valid_array_index(const GLchar
*name
, unsigned *array_index
)
518 const GLchar
*out_base_name_end
;
520 idx
= parse_program_resource_name(name
, &out_base_name_end
);
530 /* Find a program resource with specific name in given interface.
532 struct gl_program_resource
*
533 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
534 GLenum programInterface
, const char *name
,
535 unsigned *array_index
)
537 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
538 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
540 if (res
->Type
!= programInterface
)
543 /* Resource basename. */
544 const char *rname
= _mesa_program_resource_name(res
);
546 /* Since ARB_gl_spirv lack of name reflections is a possibility */
550 unsigned baselen
= strlen(rname
);
551 unsigned baselen_without_array_index
= baselen
;
552 const char *rname_last_square_bracket
= strrchr(rname
, '[');
554 bool rname_has_array_index_zero
= false;
555 /* From ARB_program_interface_query spec:
557 * "uint GetProgramResourceIndex(uint program, enum programInterface,
560 * If <name> exactly matches the name string of one of the active
561 * resources for <programInterface>, the index of the matched resource is
562 * returned. Additionally, if <name> would exactly match the name string
563 * of an active resource if "[0]" were appended to <name>, the index of
564 * the matched resource is returned. [...]"
566 * "A string provided to GetProgramResourceLocation or
567 * GetProgramResourceLocationIndex is considered to match an active variable
570 * * the string exactly matches the name of the active variable;
572 * * if the string identifies the base name of an active array, where the
573 * string would exactly match the name of the variable if the suffix
574 * "[0]" were appended to the string; [...]"
576 /* Remove array's index from interface block name comparison only if
577 * array's index is zero and the resulting string length is the same
578 * than the provided name's length.
580 if (rname_last_square_bracket
) {
581 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
582 rname_has_array_index_zero
=
583 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
584 (baselen_without_array_index
== strlen(name
));
587 if (strncmp(rname
, name
, baselen
) == 0)
589 else if (rname_has_array_index_zero
&&
590 strncmp(rname
, name
, baselen_without_array_index
) == 0)
594 switch (programInterface
) {
595 case GL_UNIFORM_BLOCK
:
596 case GL_SHADER_STORAGE_BLOCK
:
597 /* Basename match, check if array or struct. */
598 if (rname_has_array_index_zero
||
599 name
[baselen
] == '\0' ||
600 name
[baselen
] == '[' ||
601 name
[baselen
] == '.') {
605 case GL_TRANSFORM_FEEDBACK_VARYING
:
606 case GL_BUFFER_VARIABLE
:
608 case GL_VERTEX_SUBROUTINE_UNIFORM
:
609 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
610 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
611 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
612 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
613 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
614 case GL_VERTEX_SUBROUTINE
:
615 case GL_GEOMETRY_SUBROUTINE
:
616 case GL_FRAGMENT_SUBROUTINE
:
617 case GL_COMPUTE_SUBROUTINE
:
618 case GL_TESS_CONTROL_SUBROUTINE
:
619 case GL_TESS_EVALUATION_SUBROUTINE
:
620 if (name
[baselen
] == '.') {
624 case GL_PROGRAM_INPUT
:
625 case GL_PROGRAM_OUTPUT
:
626 if (name
[baselen
] == '\0') {
628 } else if (name
[baselen
] == '[' &&
629 valid_array_index(name
, array_index
)) {
634 assert(!"not implemented for given interface");
642 calc_resource_index(struct gl_shader_program
*shProg
,
643 struct gl_program_resource
*res
)
647 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
648 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
650 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
653 return GL_INVALID_INDEX
;
657 * Calculate index for the given resource.
660 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
661 struct gl_program_resource
*res
)
664 return GL_INVALID_INDEX
;
667 case GL_ATOMIC_COUNTER_BUFFER
:
668 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
669 case GL_VERTEX_SUBROUTINE
:
670 case GL_GEOMETRY_SUBROUTINE
:
671 case GL_FRAGMENT_SUBROUTINE
:
672 case GL_COMPUTE_SUBROUTINE
:
673 case GL_TESS_CONTROL_SUBROUTINE
:
674 case GL_TESS_EVALUATION_SUBROUTINE
:
675 return RESOURCE_SUB(res
)->index
;
676 case GL_UNIFORM_BLOCK
:
677 case GL_SHADER_STORAGE_BLOCK
:
678 case GL_TRANSFORM_FEEDBACK_BUFFER
:
679 case GL_TRANSFORM_FEEDBACK_VARYING
:
681 return calc_resource_index(shProg
, res
);
686 * Find a program resource that points to given data.
688 static struct gl_program_resource
*
689 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
691 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
692 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
694 if (res
->Data
== data
)
700 /* Find a program resource with specific index in given interface.
702 struct gl_program_resource
*
703 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
704 GLenum programInterface
, GLuint index
)
706 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
709 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
711 if (res
->Type
!= programInterface
)
715 case GL_UNIFORM_BLOCK
:
716 case GL_ATOMIC_COUNTER_BUFFER
:
717 case GL_SHADER_STORAGE_BLOCK
:
718 case GL_TRANSFORM_FEEDBACK_BUFFER
:
719 if (_mesa_program_resource_index(shProg
, res
) == index
)
722 case GL_TRANSFORM_FEEDBACK_VARYING
:
723 case GL_PROGRAM_INPUT
:
724 case GL_PROGRAM_OUTPUT
:
726 case GL_VERTEX_SUBROUTINE_UNIFORM
:
727 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
728 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
729 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
730 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
731 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
732 case GL_VERTEX_SUBROUTINE
:
733 case GL_GEOMETRY_SUBROUTINE
:
734 case GL_FRAGMENT_SUBROUTINE
:
735 case GL_COMPUTE_SUBROUTINE
:
736 case GL_TESS_CONTROL_SUBROUTINE
:
737 case GL_TESS_EVALUATION_SUBROUTINE
:
738 case GL_BUFFER_VARIABLE
:
739 if (++idx
== (int) index
)
743 assert(!"not implemented for given interface");
749 /* Function returns if resource name is expected to have index
753 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
756 * "If the active uniform is an array, the uniform name returned in
757 * name will always be the name of the uniform array appended with
760 * The same text also appears in the OpenGL 4.2 spec. It does not,
761 * however, appear in any previous spec. Previous specifications are
762 * ambiguous in this regard. However, either name can later be passed
763 * to glGetUniformLocation (and related APIs), so there shouldn't be any
764 * harm in always appending "[0]" to uniform array names.
767 add_index_to_name(struct gl_program_resource
*res
)
769 /* Transform feedback varyings have array index already appended
772 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
775 /* Get name length of a program resource. This consists of
776 * base name + 3 for '[0]' if resource is an array.
779 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
781 const char* name
= _mesa_program_resource_name(res
);
783 /* For shaders constructed from SPIR-V binaries, variables may not
784 * have names associated with them.
789 unsigned length
= strlen(name
);
790 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
795 /* Get full name of a program resource.
798 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
799 GLenum programInterface
, GLuint index
,
800 GLsizei bufSize
, GLsizei
*length
,
801 GLchar
*name
, const char *caller
)
803 GET_CURRENT_CONTEXT(ctx
);
805 /* Find resource with given interface and index. */
806 struct gl_program_resource
*res
=
807 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
809 /* The error INVALID_VALUE is generated if <index> is greater than
810 * or equal to the number of entries in the active resource list for
811 * <programInterface>.
814 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
819 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
826 length
= &localLength
;
828 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
830 /* The resource name can be NULL for shaders constructed from SPIR-V
831 * binaries. In that case, we do not add the '[0]'.
833 if (name
&& name
[0] != '\0' &&
834 _mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
837 /* The comparison is strange because *length does *NOT* include the
838 * terminating NUL, but maxLength does.
840 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
841 name
[*length
+ i
] = "[0]"[i
];
843 name
[*length
+ i
] = '\0';
850 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
853 case GL_PROGRAM_INPUT
: {
854 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
856 if (var
->location
== -1)
859 /* If the input is an array, fail if the index is out of bounds. */
861 && array_index
>= var
->type
->length
) {
864 return var
->location
+
865 (array_index
* var
->type
->without_array()->matrix_columns
);
867 case GL_PROGRAM_OUTPUT
:
868 if (RESOURCE_VAR(res
)->location
== -1)
871 /* If the output is an array, fail if the index is out of bounds. */
873 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
876 return RESOURCE_VAR(res
)->location
+ array_index
;
878 /* If the uniform is built-in, fail. */
879 if (RESOURCE_UNI(res
)->builtin
)
882 /* From page 79 of the OpenGL 4.2 spec:
884 * "A valid name cannot be a structure, an array of structures, or any
885 * portion of a single vector or a matrix."
887 if (RESOURCE_UNI(res
)->type
->without_array()->is_struct())
890 /* From the GL_ARB_uniform_buffer_object spec:
892 * "The value -1 will be returned if <name> does not correspond to an
893 * active uniform variable name in <program>, if <name> is associated
894 * with a named uniform block, or if <name> starts with the reserved
897 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
898 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
902 case GL_VERTEX_SUBROUTINE_UNIFORM
:
903 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
904 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
905 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
906 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
907 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
908 /* If the uniform is an array, fail if the index is out of bounds. */
910 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
914 /* location in remap table + array element offset */
915 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
922 * Function implements following location queries:
923 * glGetUniformLocation
926 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
927 GLenum programInterface
, const char *name
)
929 unsigned array_index
= 0;
930 struct gl_program_resource
*res
=
931 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
934 /* Resource not found. */
938 return program_resource_location(res
, array_index
);
942 _get_resource_location_index(struct gl_program_resource
*res
)
944 /* Non-existent variable or resource is not referenced by fragment stage. */
945 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
948 /* From OpenGL 4.5 spec, 7.3 Program Objects
949 * "The value -1 will be returned by either command...
950 * ... or if name identifies an active variable that does not have a
951 * valid location assigned.
953 if (RESOURCE_VAR(res
)->location
== -1)
955 return RESOURCE_VAR(res
)->index
;
959 * Function implements following index queries:
963 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
964 GLenum programInterface
, const char *name
)
966 struct gl_program_resource
*res
=
967 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
969 return _get_resource_location_index(res
);
973 stage_from_enum(GLenum ref
)
976 case GL_REFERENCED_BY_VERTEX_SHADER
:
977 return MESA_SHADER_VERTEX
;
978 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
979 return MESA_SHADER_TESS_CTRL
;
980 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
981 return MESA_SHADER_TESS_EVAL
;
982 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
983 return MESA_SHADER_GEOMETRY
;
984 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
985 return MESA_SHADER_FRAGMENT
;
986 case GL_REFERENCED_BY_COMPUTE_SHADER
:
987 return MESA_SHADER_COMPUTE
;
989 assert(!"shader stage not supported");
990 return MESA_SHADER_STAGES
;
995 * Check if resource is referenced by given 'referenced by' stage enum.
996 * ATC and UBO resources hold stage references of their own.
999 is_resource_referenced(struct gl_shader_program
*shProg
,
1000 struct gl_program_resource
*res
,
1001 GLuint index
, uint8_t stage
)
1003 /* First, check if we even have such a stage active. */
1004 if (!shProg
->_LinkedShaders
[stage
])
1007 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
1008 return RESOURCE_ATC(res
)->StageReferences
[stage
];
1010 if (res
->Type
== GL_UNIFORM_BLOCK
)
1011 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
1013 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
1014 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
1016 return res
->StageReferences
& (1 << stage
);
1020 get_buffer_property(struct gl_shader_program
*shProg
,
1021 struct gl_program_resource
*res
, const GLenum prop
,
1022 GLint
*val
, const char *caller
)
1024 GET_CURRENT_CONTEXT(ctx
);
1025 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1026 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1027 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1028 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1029 goto invalid_operation
;
1031 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1033 case GL_BUFFER_BINDING
:
1034 *val
= RESOURCE_UBO(res
)->Binding
;
1036 case GL_BUFFER_DATA_SIZE
:
1037 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1039 case GL_NUM_ACTIVE_VARIABLES
:
1041 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1042 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1043 struct gl_program_resource
*uni
=
1044 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1051 case GL_ACTIVE_VARIABLES
: {
1052 unsigned num_values
= 0;
1053 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1054 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1055 struct gl_program_resource
*uni
=
1056 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1061 _mesa_program_resource_index(shProg
, uni
);
1067 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1069 case GL_BUFFER_BINDING
:
1070 *val
= RESOURCE_UBO(res
)->Binding
;
1072 case GL_BUFFER_DATA_SIZE
:
1073 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1075 case GL_NUM_ACTIVE_VARIABLES
:
1077 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1078 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1079 struct gl_program_resource
*uni
=
1080 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1087 case GL_ACTIVE_VARIABLES
: {
1088 unsigned num_values
= 0;
1089 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1090 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1091 struct gl_program_resource
*uni
=
1092 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1097 _mesa_program_resource_index(shProg
, uni
);
1103 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1105 case GL_BUFFER_BINDING
:
1106 *val
= RESOURCE_ATC(res
)->Binding
;
1108 case GL_BUFFER_DATA_SIZE
:
1109 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1111 case GL_NUM_ACTIVE_VARIABLES
:
1112 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1114 case GL_ACTIVE_VARIABLES
:
1115 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1116 /* Active atomic buffer contains index to UniformStorage. Find
1117 * out gl_program_resource via data pointer and then calculate
1118 * index of that uniform.
1120 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1121 struct gl_program_resource
*uni
=
1122 program_resource_find_data(shProg
,
1123 &shProg
->data
->UniformStorage
[idx
]);
1125 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1127 return RESOURCE_ATC(res
)->NumUniforms
;
1129 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1131 case GL_BUFFER_BINDING
:
1132 *val
= RESOURCE_XFB(res
)->Binding
;
1134 case GL_NUM_ACTIVE_VARIABLES
:
1135 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1137 case GL_ACTIVE_VARIABLES
:
1138 struct gl_transform_feedback_info
*linked_xfb
=
1139 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1140 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1141 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1142 struct gl_program_resource
*buf_res
=
1143 _mesa_program_resource_find_index(shProg
,
1144 GL_TRANSFORM_FEEDBACK_BUFFER
,
1147 if (res
== buf_res
) {
1151 return RESOURCE_XFB(res
)->NumVaryings
;
1154 assert(!"support for property type not implemented");
1157 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1158 _mesa_enum_to_string(res
->Type
),
1159 _mesa_enum_to_string(prop
));
1165 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1166 struct gl_program_resource
*res
, GLuint index
,
1167 const GLenum prop
, GLint
*val
, const char *caller
)
1169 GET_CURRENT_CONTEXT(ctx
);
1171 #define VALIDATE_TYPE(type)\
1172 if (res->Type != type)\
1173 goto invalid_operation;
1175 #define VALIDATE_TYPE_2(type1, type2)\
1176 if (res->Type != type1 && res->Type != type2)\
1177 goto invalid_operation;
1180 case GL_NAME_LENGTH
:
1181 switch (res
->Type
) {
1182 case GL_ATOMIC_COUNTER_BUFFER
:
1183 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1184 goto invalid_operation
;
1186 /* Resource name length + terminator. */
1187 *val
= _mesa_program_resource_name_len(res
) + 1;
1191 switch (res
->Type
) {
1193 case GL_BUFFER_VARIABLE
:
1194 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1196 case GL_PROGRAM_INPUT
:
1197 case GL_PROGRAM_OUTPUT
:
1198 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1200 case GL_TRANSFORM_FEEDBACK_VARYING
:
1201 *val
= RESOURCE_XFV(res
)->Type
;
1204 goto invalid_operation
;
1207 switch (res
->Type
) {
1209 case GL_BUFFER_VARIABLE
:
1210 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1211 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1212 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1213 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1214 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1215 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1217 /* Test if a buffer variable is an array or an unsized array.
1218 * Unsized arrays return zero as array size.
1220 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1221 RESOURCE_UNI(res
)->array_stride
> 0)
1222 *val
= RESOURCE_UNI(res
)->array_elements
;
1224 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1226 case GL_PROGRAM_INPUT
:
1227 case GL_PROGRAM_OUTPUT
:
1228 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1230 case GL_TRANSFORM_FEEDBACK_VARYING
:
1231 *val
= RESOURCE_XFV(res
)->Size
;
1234 goto invalid_operation
;
1237 switch (res
->Type
) {
1239 case GL_BUFFER_VARIABLE
:
1240 *val
= RESOURCE_UNI(res
)->offset
;
1242 case GL_TRANSFORM_FEEDBACK_VARYING
:
1243 *val
= RESOURCE_XFV(res
)->Offset
;
1246 goto invalid_operation
;
1248 case GL_BLOCK_INDEX
:
1249 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1250 *val
= RESOURCE_UNI(res
)->block_index
;
1252 case GL_ARRAY_STRIDE
:
1253 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1254 *val
= RESOURCE_UNI(res
)->array_stride
;
1256 case GL_MATRIX_STRIDE
:
1257 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1258 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1260 case GL_IS_ROW_MAJOR
:
1261 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1262 *val
= RESOURCE_UNI(res
)->row_major
;
1264 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1265 VALIDATE_TYPE(GL_UNIFORM
);
1266 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1268 case GL_BUFFER_BINDING
:
1269 case GL_BUFFER_DATA_SIZE
:
1270 case GL_NUM_ACTIVE_VARIABLES
:
1271 case GL_ACTIVE_VARIABLES
:
1272 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1273 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1274 if (!_mesa_has_compute_shaders(ctx
))
1277 case GL_REFERENCED_BY_VERTEX_SHADER
:
1278 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1279 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1280 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1281 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1282 switch (res
->Type
) {
1284 case GL_PROGRAM_INPUT
:
1285 case GL_PROGRAM_OUTPUT
:
1286 case GL_UNIFORM_BLOCK
:
1287 case GL_BUFFER_VARIABLE
:
1288 case GL_SHADER_STORAGE_BLOCK
:
1289 case GL_ATOMIC_COUNTER_BUFFER
:
1290 *val
= is_resource_referenced(shProg
, res
, index
,
1291 stage_from_enum(prop
));
1294 goto invalid_operation
;
1297 switch (res
->Type
) {
1299 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1300 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1301 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1302 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1303 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1304 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1305 case GL_PROGRAM_INPUT
:
1306 case GL_PROGRAM_OUTPUT
:
1307 *val
= program_resource_location(res
, 0);
1310 goto invalid_operation
;
1312 case GL_LOCATION_COMPONENT
:
1313 switch (res
->Type
) {
1314 case GL_PROGRAM_INPUT
:
1315 case GL_PROGRAM_OUTPUT
:
1316 *val
= RESOURCE_VAR(res
)->component
;
1319 goto invalid_operation
;
1321 case GL_LOCATION_INDEX
: {
1323 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1324 goto invalid_operation
;
1325 tmp
= program_resource_location(res
, 0);
1329 *val
= _get_resource_location_index(res
);
1332 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1333 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1334 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1335 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1336 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1337 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1338 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1339 goto invalid_operation
;
1340 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1342 case GL_COMPATIBLE_SUBROUTINES
: {
1343 const struct gl_uniform_storage
*uni
;
1344 struct gl_program
*p
;
1348 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1349 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1350 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1351 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1352 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1353 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1354 goto invalid_operation
;
1355 uni
= RESOURCE_UNI(res
);
1357 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1359 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1360 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1361 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1362 if (fn
->types
[j
] == uni
->type
) {
1371 case GL_TOP_LEVEL_ARRAY_SIZE
:
1372 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1373 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1376 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1377 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1378 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1381 /* GL_ARB_tessellation_shader */
1382 case GL_IS_PER_PATCH
:
1383 switch (res
->Type
) {
1384 case GL_PROGRAM_INPUT
:
1385 case GL_PROGRAM_OUTPUT
:
1386 *val
= RESOURCE_VAR(res
)->patch
;
1389 goto invalid_operation
;
1392 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1393 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1394 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1396 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1397 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1398 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1405 #undef VALIDATE_TYPE
1406 #undef VALIDATE_TYPE_2
1409 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1410 _mesa_enum_to_string(res
->Type
),
1411 _mesa_enum_to_string(prop
));
1415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1416 _mesa_enum_to_string(res
->Type
),
1417 _mesa_enum_to_string(prop
));
1422 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1423 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1424 const GLenum
*props
, GLsizei bufSize
,
1425 GLsizei
*length
, GLint
*params
)
1427 GET_CURRENT_CONTEXT(ctx
);
1428 GLint
*val
= (GLint
*) params
;
1429 const GLenum
*prop
= props
;
1432 struct gl_program_resource
*res
=
1433 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1435 /* No such resource found or bufSize negative. */
1436 if (!res
|| bufSize
< 0) {
1437 _mesa_error(ctx
, GL_INVALID_VALUE
,
1438 "glGetProgramResourceiv(%s index %d bufSize %d)",
1439 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1443 /* Write propCount values until error occurs or bufSize reached. */
1444 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1446 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1447 "glGetProgramResourceiv");
1449 /* Error happened. */
1450 if (props_written
== 0)
1453 amount
+= props_written
;
1456 /* If <length> is not NULL, the actual number of integer values
1457 * written to <params> will be written to <length>.
1464 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1466 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1469 const bool producer_is_array_stage
=
1470 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1471 const bool consumer_is_array_stage
=
1472 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1473 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1474 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1478 gl_shader_variable
const **outputs
=
1479 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1480 sizeof(gl_shader_variable
*));
1481 if (outputs
== NULL
)
1484 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1487 * At an interface between program objects, the set of inputs and
1488 * outputs are considered to match exactly if and only if:
1490 * - Every declared input variable has a matching output, as described
1492 * - There are no user-defined output variables declared without a
1493 * matching input variable declaration.
1495 * Every input has an output, and every output has an input. Scan the list
1496 * of producer resources once, and generate the list of outputs. As inputs
1497 * and outputs are matched, remove the matched outputs from the set. At
1498 * the end, the set must be empty. If the set is not empty, then there is
1499 * some output that did not have an input.
1501 unsigned num_outputs
= 0;
1502 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1503 struct gl_program_resource
*res
=
1504 &producer
->sh
.data
->ProgramResourceList
[i
];
1506 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1509 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1511 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1514 * Built-in inputs or outputs do not affect interface matching.
1516 if (is_gl_identifier(var
->name
))
1519 outputs
[num_outputs
++] = var
;
1522 unsigned match_index
= 0;
1523 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1524 struct gl_program_resource
*res
=
1525 &consumer
->sh
.data
->ProgramResourceList
[i
];
1527 if (res
->Type
!= GL_PROGRAM_INPUT
)
1530 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1531 gl_shader_variable
const *producer_var
= NULL
;
1533 if (is_gl_identifier(consumer_var
->name
))
1536 /* Inputs with explicit locations match other outputs with explicit
1537 * locations by location instead of by name.
1539 if (consumer_var
->explicit_location
) {
1540 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1541 const gl_shader_variable
*const var
= outputs
[j
];
1543 if (var
->explicit_location
&&
1544 consumer_var
->location
== var
->location
) {
1551 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1552 const gl_shader_variable
*const var
= outputs
[j
];
1554 if (!var
->explicit_location
&&
1555 strcmp(consumer_var
->name
, var
->name
) == 0) {
1563 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1566 * - An output variable is considered to match an input variable in
1567 * the subsequent shader if:
1569 * - the two variables match in name, type, and qualification; or
1571 * - the two variables are declared with the same location
1572 * qualifier and match in type and qualification.
1574 if (producer_var
== NULL
) {
1579 /* An output cannot match more than one input, so remove the output from
1580 * the set of possible outputs.
1582 outputs
[match_index
] = NULL
;
1584 if (match_index
< num_outputs
)
1585 outputs
[match_index
] = outputs
[num_outputs
];
1587 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1589 * "Tessellation control shader per-vertex output variables and
1590 * blocks and tessellation control, tessellation evaluation, and
1591 * geometry shader per-vertex input variables and blocks are
1592 * required to be declared as arrays, with each element representing
1593 * input or output values for a single vertex of a multi-vertex
1594 * primitive. For the purposes of interface matching, such variables
1595 * and blocks are treated as though they were not declared as
1598 * So we unwrap those types before matching.
1600 const glsl_type
*consumer_type
= consumer_var
->type
;
1601 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1602 const glsl_type
*producer_type
= producer_var
->type
;
1603 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1605 if (consumer_is_array_stage
) {
1606 if (consumer_interface_type
) {
1607 /* the interface is the array; the underlying types should match */
1608 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1609 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1611 if (consumer_type
->is_array() && !consumer_var
->patch
)
1612 consumer_type
= consumer_type
->fields
.array
;
1616 if (producer_is_array_stage
) {
1617 if (producer_interface_type
) {
1618 /* the interface is the array; the underlying types should match */
1619 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1620 producer_interface_type
= producer_interface_type
->fields
.array
;
1622 if (producer_type
->is_array() && !producer_var
->patch
)
1623 producer_type
= producer_type
->fields
.array
;
1627 if (producer_type
!= consumer_type
) {
1632 if (producer_interface_type
!= consumer_interface_type
) {
1637 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1639 * Qualifier Class| Qualifier |in/out
1640 * ---------------+-------------+------
1644 * ---------------+-------------+------
1645 * Auxiliary | centroid | No
1646 * ---------------+-------------+------
1648 * | Block layout| N/A
1652 * ---------------+-------------+------
1653 * Interpolation | smooth |
1655 * ---------------+-------------+------
1657 * Precision | mediump | Yes
1659 * ---------------+-------------+------
1660 * Variance | invariant | No
1661 * ---------------+-------------+------
1662 * Memory | all | N/A
1664 * Note that location mismatches are detected by the loops above that
1665 * find the producer variable that goes with the consumer variable.
1667 unsigned producer_interpolation
= producer_var
->interpolation
;
1668 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1669 if (producer_interpolation
== INTERP_MODE_NONE
)
1670 producer_interpolation
= INTERP_MODE_SMOOTH
;
1671 if (consumer_interpolation
== INTERP_MODE_NONE
)
1672 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1673 if (producer_interpolation
!= consumer_interpolation
) {
1678 if (producer_var
->precision
!= consumer_var
->precision
) {
1683 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1691 return valid
&& num_outputs
== 0;
1695 * Validate inputs against outputs in a program pipeline.
1698 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1700 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1702 /* Find first active stage in pipeline. */
1703 unsigned idx
, prev
= 0;
1704 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1711 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1713 /* Pipeline might include both non-compute and a compute program, do
1714 * not attempt to validate varyings between non-compute and compute
1717 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1720 if (!validate_io(prog
[prev
], prog
[idx
]))