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 unsigned length
= strlen(_mesa_program_resource_name(res
));
782 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
787 /* Get full name of a program resource.
790 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
791 GLenum programInterface
, GLuint index
,
792 GLsizei bufSize
, GLsizei
*length
,
793 GLchar
*name
, const char *caller
)
795 GET_CURRENT_CONTEXT(ctx
);
797 /* Find resource with given interface and index. */
798 struct gl_program_resource
*res
=
799 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
801 /* The error INVALID_VALUE is generated if <index> is greater than
802 * or equal to the number of entries in the active resource list for
803 * <programInterface>.
806 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
811 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
818 length
= &localLength
;
820 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
822 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
825 /* The comparison is strange because *length does *NOT* include the
826 * terminating NUL, but maxLength does.
828 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
829 name
[*length
+ i
] = "[0]"[i
];
831 name
[*length
+ i
] = '\0';
838 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
841 case GL_PROGRAM_INPUT
: {
842 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
844 if (var
->location
== -1)
847 /* If the input is an array, fail if the index is out of bounds. */
849 && array_index
>= var
->type
->length
) {
852 return var
->location
+
853 (array_index
* var
->type
->without_array()->matrix_columns
);
855 case GL_PROGRAM_OUTPUT
:
856 if (RESOURCE_VAR(res
)->location
== -1)
859 /* If the output is an array, fail if the index is out of bounds. */
861 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
864 return RESOURCE_VAR(res
)->location
+ array_index
;
866 /* If the uniform is built-in, fail. */
867 if (RESOURCE_UNI(res
)->builtin
)
870 /* From page 79 of the OpenGL 4.2 spec:
872 * "A valid name cannot be a structure, an array of structures, or any
873 * portion of a single vector or a matrix."
875 if (RESOURCE_UNI(res
)->type
->without_array()->is_struct())
878 /* From the GL_ARB_uniform_buffer_object spec:
880 * "The value -1 will be returned if <name> does not correspond to an
881 * active uniform variable name in <program>, if <name> is associated
882 * with a named uniform block, or if <name> starts with the reserved
885 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
886 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
890 case GL_VERTEX_SUBROUTINE_UNIFORM
:
891 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
892 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
893 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
894 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
895 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
896 /* If the uniform is an array, fail if the index is out of bounds. */
898 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
902 /* location in remap table + array element offset */
903 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
910 * Function implements following location queries:
911 * glGetUniformLocation
914 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
915 GLenum programInterface
, const char *name
)
917 unsigned array_index
= 0;
918 struct gl_program_resource
*res
=
919 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
922 /* Resource not found. */
926 return program_resource_location(res
, array_index
);
930 * Function implements following index queries:
934 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
935 GLenum programInterface
, const char *name
)
937 struct gl_program_resource
*res
=
938 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
940 /* Non-existent variable or resource is not referenced by fragment stage. */
941 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
944 /* From OpenGL 4.5 spec, 7.3 Program Objects
945 * "The value -1 will be returned by either command...
946 * ... or if name identifies an active variable that does not have a
947 * valid location assigned.
949 if (RESOURCE_VAR(res
)->location
== -1)
951 return RESOURCE_VAR(res
)->index
;
955 stage_from_enum(GLenum ref
)
958 case GL_REFERENCED_BY_VERTEX_SHADER
:
959 return MESA_SHADER_VERTEX
;
960 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
961 return MESA_SHADER_TESS_CTRL
;
962 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
963 return MESA_SHADER_TESS_EVAL
;
964 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
965 return MESA_SHADER_GEOMETRY
;
966 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
967 return MESA_SHADER_FRAGMENT
;
968 case GL_REFERENCED_BY_COMPUTE_SHADER
:
969 return MESA_SHADER_COMPUTE
;
971 assert(!"shader stage not supported");
972 return MESA_SHADER_STAGES
;
977 * Check if resource is referenced by given 'referenced by' stage enum.
978 * ATC and UBO resources hold stage references of their own.
981 is_resource_referenced(struct gl_shader_program
*shProg
,
982 struct gl_program_resource
*res
,
983 GLuint index
, uint8_t stage
)
985 /* First, check if we even have such a stage active. */
986 if (!shProg
->_LinkedShaders
[stage
])
989 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
990 return RESOURCE_ATC(res
)->StageReferences
[stage
];
992 if (res
->Type
== GL_UNIFORM_BLOCK
)
993 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
995 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
996 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
998 return res
->StageReferences
& (1 << stage
);
1002 get_buffer_property(struct gl_shader_program
*shProg
,
1003 struct gl_program_resource
*res
, const GLenum prop
,
1004 GLint
*val
, const char *caller
)
1006 GET_CURRENT_CONTEXT(ctx
);
1007 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1008 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1009 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1010 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1011 goto invalid_operation
;
1013 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1015 case GL_BUFFER_BINDING
:
1016 *val
= RESOURCE_UBO(res
)->Binding
;
1018 case GL_BUFFER_DATA_SIZE
:
1019 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1021 case GL_NUM_ACTIVE_VARIABLES
:
1023 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1024 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1025 struct gl_program_resource
*uni
=
1026 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1033 case GL_ACTIVE_VARIABLES
: {
1034 unsigned num_values
= 0;
1035 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1036 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1037 struct gl_program_resource
*uni
=
1038 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1043 _mesa_program_resource_index(shProg
, uni
);
1049 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1051 case GL_BUFFER_BINDING
:
1052 *val
= RESOURCE_UBO(res
)->Binding
;
1054 case GL_BUFFER_DATA_SIZE
:
1055 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1057 case GL_NUM_ACTIVE_VARIABLES
:
1059 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1060 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1061 struct gl_program_resource
*uni
=
1062 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1069 case GL_ACTIVE_VARIABLES
: {
1070 unsigned num_values
= 0;
1071 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1072 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1073 struct gl_program_resource
*uni
=
1074 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1079 _mesa_program_resource_index(shProg
, uni
);
1085 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1087 case GL_BUFFER_BINDING
:
1088 *val
= RESOURCE_ATC(res
)->Binding
;
1090 case GL_BUFFER_DATA_SIZE
:
1091 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1093 case GL_NUM_ACTIVE_VARIABLES
:
1094 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1096 case GL_ACTIVE_VARIABLES
:
1097 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1098 /* Active atomic buffer contains index to UniformStorage. Find
1099 * out gl_program_resource via data pointer and then calculate
1100 * index of that uniform.
1102 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1103 struct gl_program_resource
*uni
=
1104 program_resource_find_data(shProg
,
1105 &shProg
->data
->UniformStorage
[idx
]);
1107 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1109 return RESOURCE_ATC(res
)->NumUniforms
;
1111 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1113 case GL_BUFFER_BINDING
:
1114 *val
= RESOURCE_XFB(res
)->Binding
;
1116 case GL_NUM_ACTIVE_VARIABLES
:
1117 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1119 case GL_ACTIVE_VARIABLES
:
1120 struct gl_transform_feedback_info
*linked_xfb
=
1121 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1122 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1123 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1124 struct gl_program_resource
*buf_res
=
1125 _mesa_program_resource_find_index(shProg
,
1126 GL_TRANSFORM_FEEDBACK_BUFFER
,
1129 if (res
== buf_res
) {
1133 return RESOURCE_XFB(res
)->NumVaryings
;
1136 assert(!"support for property type not implemented");
1139 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1140 _mesa_enum_to_string(res
->Type
),
1141 _mesa_enum_to_string(prop
));
1147 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1148 struct gl_program_resource
*res
, GLuint index
,
1149 const GLenum prop
, GLint
*val
, const char *caller
)
1151 GET_CURRENT_CONTEXT(ctx
);
1153 #define VALIDATE_TYPE(type)\
1154 if (res->Type != type)\
1155 goto invalid_operation;
1157 #define VALIDATE_TYPE_2(type1, type2)\
1158 if (res->Type != type1 && res->Type != type2)\
1159 goto invalid_operation;
1162 case GL_NAME_LENGTH
:
1163 switch (res
->Type
) {
1164 case GL_ATOMIC_COUNTER_BUFFER
:
1165 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1166 goto invalid_operation
;
1168 /* Resource name length + terminator. */
1169 *val
= _mesa_program_resource_name_len(res
) + 1;
1173 switch (res
->Type
) {
1175 case GL_BUFFER_VARIABLE
:
1176 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1178 case GL_PROGRAM_INPUT
:
1179 case GL_PROGRAM_OUTPUT
:
1180 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1182 case GL_TRANSFORM_FEEDBACK_VARYING
:
1183 *val
= RESOURCE_XFV(res
)->Type
;
1186 goto invalid_operation
;
1189 switch (res
->Type
) {
1191 case GL_BUFFER_VARIABLE
:
1192 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1193 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1194 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1195 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1196 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1197 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1199 /* Test if a buffer variable is an array or an unsized array.
1200 * Unsized arrays return zero as array size.
1202 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1203 RESOURCE_UNI(res
)->array_stride
> 0)
1204 *val
= RESOURCE_UNI(res
)->array_elements
;
1206 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1208 case GL_PROGRAM_INPUT
:
1209 case GL_PROGRAM_OUTPUT
:
1210 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1212 case GL_TRANSFORM_FEEDBACK_VARYING
:
1213 *val
= RESOURCE_XFV(res
)->Size
;
1216 goto invalid_operation
;
1219 switch (res
->Type
) {
1221 case GL_BUFFER_VARIABLE
:
1222 *val
= RESOURCE_UNI(res
)->offset
;
1224 case GL_TRANSFORM_FEEDBACK_VARYING
:
1225 *val
= RESOURCE_XFV(res
)->Offset
;
1228 goto invalid_operation
;
1230 case GL_BLOCK_INDEX
:
1231 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1232 *val
= RESOURCE_UNI(res
)->block_index
;
1234 case GL_ARRAY_STRIDE
:
1235 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1236 *val
= RESOURCE_UNI(res
)->array_stride
;
1238 case GL_MATRIX_STRIDE
:
1239 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1240 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1242 case GL_IS_ROW_MAJOR
:
1243 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1244 *val
= RESOURCE_UNI(res
)->row_major
;
1246 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1247 VALIDATE_TYPE(GL_UNIFORM
);
1248 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1250 case GL_BUFFER_BINDING
:
1251 case GL_BUFFER_DATA_SIZE
:
1252 case GL_NUM_ACTIVE_VARIABLES
:
1253 case GL_ACTIVE_VARIABLES
:
1254 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1255 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1256 if (!_mesa_has_compute_shaders(ctx
))
1259 case GL_REFERENCED_BY_VERTEX_SHADER
:
1260 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1261 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1262 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1263 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1264 switch (res
->Type
) {
1266 case GL_PROGRAM_INPUT
:
1267 case GL_PROGRAM_OUTPUT
:
1268 case GL_UNIFORM_BLOCK
:
1269 case GL_BUFFER_VARIABLE
:
1270 case GL_SHADER_STORAGE_BLOCK
:
1271 case GL_ATOMIC_COUNTER_BUFFER
:
1272 *val
= is_resource_referenced(shProg
, res
, index
,
1273 stage_from_enum(prop
));
1276 goto invalid_operation
;
1279 switch (res
->Type
) {
1281 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1282 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1283 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1284 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1285 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1286 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1287 case GL_PROGRAM_INPUT
:
1288 case GL_PROGRAM_OUTPUT
:
1289 *val
= program_resource_location(res
, 0);
1292 goto invalid_operation
;
1294 case GL_LOCATION_COMPONENT
:
1295 switch (res
->Type
) {
1296 case GL_PROGRAM_INPUT
:
1297 case GL_PROGRAM_OUTPUT
:
1298 *val
= RESOURCE_VAR(res
)->component
;
1301 goto invalid_operation
;
1303 case GL_LOCATION_INDEX
: {
1305 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1306 goto invalid_operation
;
1307 tmp
= program_resource_location(res
, 0);
1311 *val
= _mesa_program_resource_location_index(shProg
, res
->Type
,
1312 RESOURCE_VAR(res
)->name
);
1315 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1316 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1317 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1318 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1319 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1320 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1321 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1322 goto invalid_operation
;
1323 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1325 case GL_COMPATIBLE_SUBROUTINES
: {
1326 const struct gl_uniform_storage
*uni
;
1327 struct gl_program
*p
;
1331 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1332 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1333 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1334 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1335 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1336 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1337 goto invalid_operation
;
1338 uni
= RESOURCE_UNI(res
);
1340 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1342 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1343 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1344 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1345 if (fn
->types
[j
] == uni
->type
) {
1354 case GL_TOP_LEVEL_ARRAY_SIZE
:
1355 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1356 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1359 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1360 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1361 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1364 /* GL_ARB_tessellation_shader */
1365 case GL_IS_PER_PATCH
:
1366 switch (res
->Type
) {
1367 case GL_PROGRAM_INPUT
:
1368 case GL_PROGRAM_OUTPUT
:
1369 *val
= RESOURCE_VAR(res
)->patch
;
1372 goto invalid_operation
;
1375 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1376 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1377 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1379 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1380 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1381 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1388 #undef VALIDATE_TYPE
1389 #undef VALIDATE_TYPE_2
1392 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1393 _mesa_enum_to_string(res
->Type
),
1394 _mesa_enum_to_string(prop
));
1398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1399 _mesa_enum_to_string(res
->Type
),
1400 _mesa_enum_to_string(prop
));
1405 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1406 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1407 const GLenum
*props
, GLsizei bufSize
,
1408 GLsizei
*length
, GLint
*params
)
1410 GET_CURRENT_CONTEXT(ctx
);
1411 GLint
*val
= (GLint
*) params
;
1412 const GLenum
*prop
= props
;
1415 struct gl_program_resource
*res
=
1416 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1418 /* No such resource found or bufSize negative. */
1419 if (!res
|| bufSize
< 0) {
1420 _mesa_error(ctx
, GL_INVALID_VALUE
,
1421 "glGetProgramResourceiv(%s index %d bufSize %d)",
1422 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1426 /* Write propCount values until error occurs or bufSize reached. */
1427 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1429 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1430 "glGetProgramResourceiv");
1432 /* Error happened. */
1433 if (props_written
== 0)
1436 amount
+= props_written
;
1439 /* If <length> is not NULL, the actual number of integer values
1440 * written to <params> will be written to <length>.
1447 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1449 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1452 const bool producer_is_array_stage
=
1453 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1454 const bool consumer_is_array_stage
=
1455 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1456 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1457 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1461 gl_shader_variable
const **outputs
=
1462 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1463 sizeof(gl_shader_variable
*));
1464 if (outputs
== NULL
)
1467 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1470 * At an interface between program objects, the set of inputs and
1471 * outputs are considered to match exactly if and only if:
1473 * - Every declared input variable has a matching output, as described
1475 * - There are no user-defined output variables declared without a
1476 * matching input variable declaration.
1478 * Every input has an output, and every output has an input. Scan the list
1479 * of producer resources once, and generate the list of outputs. As inputs
1480 * and outputs are matched, remove the matched outputs from the set. At
1481 * the end, the set must be empty. If the set is not empty, then there is
1482 * some output that did not have an input.
1484 unsigned num_outputs
= 0;
1485 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1486 struct gl_program_resource
*res
=
1487 &producer
->sh
.data
->ProgramResourceList
[i
];
1489 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1492 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1494 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1497 * Built-in inputs or outputs do not affect interface matching.
1499 if (is_gl_identifier(var
->name
))
1502 outputs
[num_outputs
++] = var
;
1505 unsigned match_index
= 0;
1506 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1507 struct gl_program_resource
*res
=
1508 &consumer
->sh
.data
->ProgramResourceList
[i
];
1510 if (res
->Type
!= GL_PROGRAM_INPUT
)
1513 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1514 gl_shader_variable
const *producer_var
= NULL
;
1516 if (is_gl_identifier(consumer_var
->name
))
1519 /* Inputs with explicit locations match other outputs with explicit
1520 * locations by location instead of by name.
1522 if (consumer_var
->explicit_location
) {
1523 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1524 const gl_shader_variable
*const var
= outputs
[j
];
1526 if (var
->explicit_location
&&
1527 consumer_var
->location
== var
->location
) {
1534 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1535 const gl_shader_variable
*const var
= outputs
[j
];
1537 if (!var
->explicit_location
&&
1538 strcmp(consumer_var
->name
, var
->name
) == 0) {
1546 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1549 * - An output variable is considered to match an input variable in
1550 * the subsequent shader if:
1552 * - the two variables match in name, type, and qualification; or
1554 * - the two variables are declared with the same location
1555 * qualifier and match in type and qualification.
1557 if (producer_var
== NULL
) {
1562 /* An output cannot match more than one input, so remove the output from
1563 * the set of possible outputs.
1565 outputs
[match_index
] = NULL
;
1567 if (match_index
< num_outputs
)
1568 outputs
[match_index
] = outputs
[num_outputs
];
1570 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1572 * "Tessellation control shader per-vertex output variables and
1573 * blocks and tessellation control, tessellation evaluation, and
1574 * geometry shader per-vertex input variables and blocks are
1575 * required to be declared as arrays, with each element representing
1576 * input or output values for a single vertex of a multi-vertex
1577 * primitive. For the purposes of interface matching, such variables
1578 * and blocks are treated as though they were not declared as
1581 * So we unwrap those types before matching.
1583 const glsl_type
*consumer_type
= consumer_var
->type
;
1584 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1585 const glsl_type
*producer_type
= producer_var
->type
;
1586 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1588 if (consumer_is_array_stage
) {
1589 if (consumer_interface_type
) {
1590 /* the interface is the array; the underlying types should match */
1591 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1592 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1594 if (consumer_type
->is_array() && !consumer_var
->patch
)
1595 consumer_type
= consumer_type
->fields
.array
;
1599 if (producer_is_array_stage
) {
1600 if (producer_interface_type
) {
1601 /* the interface is the array; the underlying types should match */
1602 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1603 producer_interface_type
= producer_interface_type
->fields
.array
;
1605 if (producer_type
->is_array() && !producer_var
->patch
)
1606 producer_type
= producer_type
->fields
.array
;
1610 if (producer_type
!= consumer_type
) {
1615 if (producer_interface_type
!= consumer_interface_type
) {
1620 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1622 * Qualifier Class| Qualifier |in/out
1623 * ---------------+-------------+------
1627 * ---------------+-------------+------
1628 * Auxiliary | centroid | No
1629 * ---------------+-------------+------
1631 * | Block layout| N/A
1635 * ---------------+-------------+------
1636 * Interpolation | smooth |
1638 * ---------------+-------------+------
1640 * Precision | mediump | Yes
1642 * ---------------+-------------+------
1643 * Variance | invariant | No
1644 * ---------------+-------------+------
1645 * Memory | all | N/A
1647 * Note that location mismatches are detected by the loops above that
1648 * find the producer variable that goes with the consumer variable.
1650 unsigned producer_interpolation
= producer_var
->interpolation
;
1651 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1652 if (producer_interpolation
== INTERP_MODE_NONE
)
1653 producer_interpolation
= INTERP_MODE_SMOOTH
;
1654 if (consumer_interpolation
== INTERP_MODE_NONE
)
1655 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1656 if (producer_interpolation
!= consumer_interpolation
) {
1661 if (producer_var
->precision
!= consumer_var
->precision
) {
1666 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1674 return valid
&& num_outputs
== 0;
1678 * Validate inputs against outputs in a program pipeline.
1681 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1683 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1685 /* Find first active stage in pipeline. */
1686 unsigned idx
, prev
= 0;
1687 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1694 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1696 /* Pipeline might include both non-compute and a compute program, do
1697 * not attempt to validate varyings between non-compute and compute
1700 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1703 if (!validate_io(prog
[prev
], prog
[idx
]))