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 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
248 if (length
>= longest
)
249 longest
= length
+ 1;
257 bind_frag_data_location(struct gl_shader_program
*const shProg
,
258 const char *name
, unsigned colorNumber
,
261 /* Replace the current value if it's already in the list. Add
262 * FRAG_RESULT_DATA0 because that's how the linker differentiates
263 * between built-in attributes and user-defined attributes.
265 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
266 shProg
->FragDataIndexBindings
->put(index
, name
);
269 * Note that this binding won't go into effect until
270 * glLinkProgram is called again.
275 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
278 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
282 _mesa_BindFragDataLocation_no_error(GLuint program
, GLuint colorNumber
,
285 GET_CURRENT_CONTEXT(ctx
);
290 struct gl_shader_program
*const shProg
=
291 _mesa_lookup_shader_program(ctx
, program
);
293 bind_frag_data_location(shProg
, name
, colorNumber
, 0);
297 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
298 GLuint index
, const GLchar
*name
)
300 GET_CURRENT_CONTEXT(ctx
);
302 struct gl_shader_program
*const shProg
=
303 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
310 if (strncmp(name
, "gl_", 3) == 0) {
311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
316 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
320 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
321 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
325 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
326 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
330 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
334 _mesa_BindFragDataLocationIndexed_no_error(GLuint program
, GLuint colorNumber
,
335 GLuint index
, const GLchar
*name
)
337 GET_CURRENT_CONTEXT(ctx
);
342 struct gl_shader_program
*const shProg
=
343 _mesa_lookup_shader_program(ctx
, program
);
345 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
349 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
351 GET_CURRENT_CONTEXT(ctx
);
352 struct gl_shader_program
*const shProg
=
353 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
359 if (!shProg
->data
->LinkStatus
) {
360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
361 "glGetFragDataIndex(program not linked)");
368 if (strncmp(name
, "gl_", 3) == 0) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
,
370 "glGetFragDataIndex(illegal name)");
374 /* Not having a fragment shader is not an error.
376 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
379 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
384 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
386 GET_CURRENT_CONTEXT(ctx
);
387 struct gl_shader_program
*const shProg
=
388 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
394 if (!shProg
->data
->LinkStatus
) {
395 _mesa_error(ctx
, GL_INVALID_OPERATION
,
396 "glGetFragDataLocation(program not linked)");
403 if (strncmp(name
, "gl_", 3) == 0) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glGetFragDataLocation(illegal name)");
409 /* Not having a fragment shader is not an error.
411 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
414 unsigned array_index
= 0;
415 struct gl_program_resource
*res
=
416 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
422 return program_resource_location(res
, array_index
);
426 _mesa_program_resource_name(struct gl_program_resource
*res
)
429 case GL_UNIFORM_BLOCK
:
430 case GL_SHADER_STORAGE_BLOCK
:
431 return RESOURCE_UBO(res
)->Name
;
432 case GL_TRANSFORM_FEEDBACK_VARYING
:
433 return RESOURCE_XFV(res
)->Name
;
434 case GL_PROGRAM_INPUT
:
435 case GL_PROGRAM_OUTPUT
:
436 return RESOURCE_VAR(res
)->name
;
438 case GL_BUFFER_VARIABLE
:
439 return RESOURCE_UNI(res
)->name
;
440 case GL_VERTEX_SUBROUTINE_UNIFORM
:
441 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
442 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
443 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
444 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
445 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
446 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
447 case GL_VERTEX_SUBROUTINE
:
448 case GL_GEOMETRY_SUBROUTINE
:
449 case GL_FRAGMENT_SUBROUTINE
:
450 case GL_COMPUTE_SUBROUTINE
:
451 case GL_TESS_CONTROL_SUBROUTINE
:
452 case GL_TESS_EVALUATION_SUBROUTINE
:
453 return RESOURCE_SUB(res
)->name
;
455 assert(!"support for resource type not implemented");
462 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
465 case GL_TRANSFORM_FEEDBACK_VARYING
:
466 return RESOURCE_XFV(res
)->Size
> 1 ?
467 RESOURCE_XFV(res
)->Size
: 0;
468 case GL_PROGRAM_INPUT
:
469 case GL_PROGRAM_OUTPUT
:
470 return RESOURCE_VAR(res
)->type
->length
;
472 case GL_VERTEX_SUBROUTINE_UNIFORM
:
473 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
474 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
475 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
476 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
477 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
478 return RESOURCE_UNI(res
)->array_elements
;
479 case GL_BUFFER_VARIABLE
:
481 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
482 RESOURCE_UNI(res
)->array_elements
== 0)
485 return RESOURCE_UNI(res
)->array_elements
;
486 case GL_VERTEX_SUBROUTINE
:
487 case GL_GEOMETRY_SUBROUTINE
:
488 case GL_FRAGMENT_SUBROUTINE
:
489 case GL_COMPUTE_SUBROUTINE
:
490 case GL_TESS_CONTROL_SUBROUTINE
:
491 case GL_TESS_EVALUATION_SUBROUTINE
:
492 case GL_ATOMIC_COUNTER_BUFFER
:
493 case GL_UNIFORM_BLOCK
:
494 case GL_SHADER_STORAGE_BLOCK
:
497 assert(!"support for resource type not implemented");
503 * Checks if array subscript is valid and if so sets array_index.
506 valid_array_index(const GLchar
*name
, unsigned *array_index
)
509 const GLchar
*out_base_name_end
;
511 idx
= parse_program_resource_name(name
, &out_base_name_end
);
521 /* Find a program resource with specific name in given interface.
523 struct gl_program_resource
*
524 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
525 GLenum programInterface
, const char *name
,
526 unsigned *array_index
)
528 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
529 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
531 if (res
->Type
!= programInterface
)
534 /* Resource basename. */
535 const char *rname
= _mesa_program_resource_name(res
);
537 /* Since ARB_gl_spirv lack of name reflections is a possibility */
541 unsigned baselen
= strlen(rname
);
542 unsigned baselen_without_array_index
= baselen
;
543 const char *rname_last_square_bracket
= strrchr(rname
, '[');
545 bool rname_has_array_index_zero
= false;
546 /* From ARB_program_interface_query spec:
548 * "uint GetProgramResourceIndex(uint program, enum programInterface,
551 * If <name> exactly matches the name string of one of the active
552 * resources for <programInterface>, the index of the matched resource is
553 * returned. Additionally, if <name> would exactly match the name string
554 * of an active resource if "[0]" were appended to <name>, the index of
555 * the matched resource is returned. [...]"
557 * "A string provided to GetProgramResourceLocation or
558 * GetProgramResourceLocationIndex is considered to match an active variable
561 * * the string exactly matches the name of the active variable;
563 * * if the string identifies the base name of an active array, where the
564 * string would exactly match the name of the variable if the suffix
565 * "[0]" were appended to the string; [...]"
567 /* Remove array's index from interface block name comparison only if
568 * array's index is zero and the resulting string length is the same
569 * than the provided name's length.
571 if (rname_last_square_bracket
) {
572 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
573 rname_has_array_index_zero
=
574 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
575 (baselen_without_array_index
== strlen(name
));
578 if (strncmp(rname
, name
, baselen
) == 0)
580 else if (rname_has_array_index_zero
&&
581 strncmp(rname
, name
, baselen_without_array_index
) == 0)
585 switch (programInterface
) {
586 case GL_UNIFORM_BLOCK
:
587 case GL_SHADER_STORAGE_BLOCK
:
588 /* Basename match, check if array or struct. */
589 if (rname_has_array_index_zero
||
590 name
[baselen
] == '\0' ||
591 name
[baselen
] == '[' ||
592 name
[baselen
] == '.') {
596 case GL_TRANSFORM_FEEDBACK_VARYING
:
597 case GL_BUFFER_VARIABLE
:
599 case GL_VERTEX_SUBROUTINE_UNIFORM
:
600 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
601 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
602 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
603 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
604 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
605 case GL_VERTEX_SUBROUTINE
:
606 case GL_GEOMETRY_SUBROUTINE
:
607 case GL_FRAGMENT_SUBROUTINE
:
608 case GL_COMPUTE_SUBROUTINE
:
609 case GL_TESS_CONTROL_SUBROUTINE
:
610 case GL_TESS_EVALUATION_SUBROUTINE
:
611 if (name
[baselen
] == '.') {
615 case GL_PROGRAM_INPUT
:
616 case GL_PROGRAM_OUTPUT
:
617 if (name
[baselen
] == '\0') {
619 } else if (name
[baselen
] == '[' &&
620 valid_array_index(name
, array_index
)) {
625 assert(!"not implemented for given interface");
633 calc_resource_index(struct gl_shader_program
*shProg
,
634 struct gl_program_resource
*res
)
638 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
639 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
641 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
644 return GL_INVALID_INDEX
;
648 * Calculate index for the given resource.
651 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
652 struct gl_program_resource
*res
)
655 return GL_INVALID_INDEX
;
658 case GL_ATOMIC_COUNTER_BUFFER
:
659 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
660 case GL_VERTEX_SUBROUTINE
:
661 case GL_GEOMETRY_SUBROUTINE
:
662 case GL_FRAGMENT_SUBROUTINE
:
663 case GL_COMPUTE_SUBROUTINE
:
664 case GL_TESS_CONTROL_SUBROUTINE
:
665 case GL_TESS_EVALUATION_SUBROUTINE
:
666 return RESOURCE_SUB(res
)->index
;
667 case GL_UNIFORM_BLOCK
:
668 case GL_SHADER_STORAGE_BLOCK
:
669 case GL_TRANSFORM_FEEDBACK_BUFFER
:
670 case GL_TRANSFORM_FEEDBACK_VARYING
:
672 return calc_resource_index(shProg
, res
);
677 * Find a program resource that points to given data.
679 static struct gl_program_resource
*
680 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
682 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
683 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
685 if (res
->Data
== data
)
691 /* Find a program resource with specific index in given interface.
693 struct gl_program_resource
*
694 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
695 GLenum programInterface
, GLuint index
)
697 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
700 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
702 if (res
->Type
!= programInterface
)
706 case GL_UNIFORM_BLOCK
:
707 case GL_ATOMIC_COUNTER_BUFFER
:
708 case GL_SHADER_STORAGE_BLOCK
:
709 case GL_TRANSFORM_FEEDBACK_BUFFER
:
710 if (_mesa_program_resource_index(shProg
, res
) == index
)
713 case GL_TRANSFORM_FEEDBACK_VARYING
:
714 case GL_PROGRAM_INPUT
:
715 case GL_PROGRAM_OUTPUT
:
717 case GL_VERTEX_SUBROUTINE_UNIFORM
:
718 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
719 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
720 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
721 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
722 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
723 case GL_VERTEX_SUBROUTINE
:
724 case GL_GEOMETRY_SUBROUTINE
:
725 case GL_FRAGMENT_SUBROUTINE
:
726 case GL_COMPUTE_SUBROUTINE
:
727 case GL_TESS_CONTROL_SUBROUTINE
:
728 case GL_TESS_EVALUATION_SUBROUTINE
:
729 case GL_BUFFER_VARIABLE
:
730 if (++idx
== (int) index
)
734 assert(!"not implemented for given interface");
740 /* Function returns if resource name is expected to have index
744 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
747 * "If the active uniform is an array, the uniform name returned in
748 * name will always be the name of the uniform array appended with
751 * The same text also appears in the OpenGL 4.2 spec. It does not,
752 * however, appear in any previous spec. Previous specifications are
753 * ambiguous in this regard. However, either name can later be passed
754 * to glGetUniformLocation (and related APIs), so there shouldn't be any
755 * harm in always appending "[0]" to uniform array names.
758 add_index_to_name(struct gl_program_resource
*res
)
760 /* Transform feedback varyings have array index already appended
763 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
766 /* Get name length of a program resource. This consists of
767 * base name + 3 for '[0]' if resource is an array.
770 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
772 unsigned length
= strlen(_mesa_program_resource_name(res
));
773 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
778 /* Get full name of a program resource.
781 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
782 GLenum programInterface
, GLuint index
,
783 GLsizei bufSize
, GLsizei
*length
,
784 GLchar
*name
, const char *caller
)
786 GET_CURRENT_CONTEXT(ctx
);
788 /* Find resource with given interface and index. */
789 struct gl_program_resource
*res
=
790 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
792 /* The error INVALID_VALUE is generated if <index> is greater than
793 * or equal to the number of entries in the active resource list for
794 * <programInterface>.
797 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
802 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
809 length
= &localLength
;
811 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
813 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
816 /* The comparison is strange because *length does *NOT* include the
817 * terminating NUL, but maxLength does.
819 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
820 name
[*length
+ i
] = "[0]"[i
];
822 name
[*length
+ i
] = '\0';
829 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
832 case GL_PROGRAM_INPUT
: {
833 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
835 if (var
->location
== -1)
838 /* If the input is an array, fail if the index is out of bounds. */
840 && array_index
>= var
->type
->length
) {
843 return var
->location
+
844 (array_index
* var
->type
->without_array()->matrix_columns
);
846 case GL_PROGRAM_OUTPUT
:
847 if (RESOURCE_VAR(res
)->location
== -1)
850 /* If the output is an array, fail if the index is out of bounds. */
852 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
855 return RESOURCE_VAR(res
)->location
+ array_index
;
857 /* If the uniform is built-in, fail. */
858 if (RESOURCE_UNI(res
)->builtin
)
861 /* From page 79 of the OpenGL 4.2 spec:
863 * "A valid name cannot be a structure, an array of structures, or any
864 * portion of a single vector or a matrix."
866 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
869 /* From the GL_ARB_uniform_buffer_object spec:
871 * "The value -1 will be returned if <name> does not correspond to an
872 * active uniform variable name in <program>, if <name> is associated
873 * with a named uniform block, or if <name> starts with the reserved
876 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
877 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
881 case GL_VERTEX_SUBROUTINE_UNIFORM
:
882 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
883 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
884 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
885 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
886 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
887 /* If the uniform is an array, fail if the index is out of bounds. */
889 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
893 /* location in remap table + array element offset */
894 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
901 * Function implements following location queries:
902 * glGetUniformLocation
905 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
906 GLenum programInterface
, const char *name
)
908 unsigned array_index
= 0;
909 struct gl_program_resource
*res
=
910 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
913 /* Resource not found. */
917 return program_resource_location(res
, array_index
);
921 * Function implements following index queries:
925 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
926 GLenum programInterface
, const char *name
)
928 struct gl_program_resource
*res
=
929 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
931 /* Non-existent variable or resource is not referenced by fragment stage. */
932 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
935 /* From OpenGL 4.5 spec, 7.3 Program Objects
936 * "The value -1 will be returned by either command...
937 * ... or if name identifies an active variable that does not have a
938 * valid location assigned.
940 if (RESOURCE_VAR(res
)->location
== -1)
942 return RESOURCE_VAR(res
)->index
;
946 stage_from_enum(GLenum ref
)
949 case GL_REFERENCED_BY_VERTEX_SHADER
:
950 return MESA_SHADER_VERTEX
;
951 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
952 return MESA_SHADER_TESS_CTRL
;
953 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
954 return MESA_SHADER_TESS_EVAL
;
955 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
956 return MESA_SHADER_GEOMETRY
;
957 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
958 return MESA_SHADER_FRAGMENT
;
959 case GL_REFERENCED_BY_COMPUTE_SHADER
:
960 return MESA_SHADER_COMPUTE
;
962 assert(!"shader stage not supported");
963 return MESA_SHADER_STAGES
;
968 * Check if resource is referenced by given 'referenced by' stage enum.
969 * ATC and UBO resources hold stage references of their own.
972 is_resource_referenced(struct gl_shader_program
*shProg
,
973 struct gl_program_resource
*res
,
974 GLuint index
, uint8_t stage
)
976 /* First, check if we even have such a stage active. */
977 if (!shProg
->_LinkedShaders
[stage
])
980 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
981 return RESOURCE_ATC(res
)->StageReferences
[stage
];
983 if (res
->Type
== GL_UNIFORM_BLOCK
)
984 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
986 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
987 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
989 return res
->StageReferences
& (1 << stage
);
993 get_buffer_property(struct gl_shader_program
*shProg
,
994 struct gl_program_resource
*res
, const GLenum prop
,
995 GLint
*val
, const char *caller
)
997 GET_CURRENT_CONTEXT(ctx
);
998 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
999 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1000 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1001 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1002 goto invalid_operation
;
1004 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1006 case GL_BUFFER_BINDING
:
1007 *val
= RESOURCE_UBO(res
)->Binding
;
1009 case GL_BUFFER_DATA_SIZE
:
1010 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1012 case GL_NUM_ACTIVE_VARIABLES
:
1014 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1015 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1016 struct gl_program_resource
*uni
=
1017 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1024 case GL_ACTIVE_VARIABLES
: {
1025 unsigned num_values
= 0;
1026 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1027 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1028 struct gl_program_resource
*uni
=
1029 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1034 _mesa_program_resource_index(shProg
, uni
);
1040 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1042 case GL_BUFFER_BINDING
:
1043 *val
= RESOURCE_UBO(res
)->Binding
;
1045 case GL_BUFFER_DATA_SIZE
:
1046 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1048 case GL_NUM_ACTIVE_VARIABLES
:
1050 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1051 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1052 struct gl_program_resource
*uni
=
1053 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1060 case GL_ACTIVE_VARIABLES
: {
1061 unsigned num_values
= 0;
1062 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1063 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1064 struct gl_program_resource
*uni
=
1065 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1070 _mesa_program_resource_index(shProg
, uni
);
1076 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1078 case GL_BUFFER_BINDING
:
1079 *val
= RESOURCE_ATC(res
)->Binding
;
1081 case GL_BUFFER_DATA_SIZE
:
1082 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1084 case GL_NUM_ACTIVE_VARIABLES
:
1085 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1087 case GL_ACTIVE_VARIABLES
:
1088 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1089 /* Active atomic buffer contains index to UniformStorage. Find
1090 * out gl_program_resource via data pointer and then calculate
1091 * index of that uniform.
1093 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1094 struct gl_program_resource
*uni
=
1095 program_resource_find_data(shProg
,
1096 &shProg
->data
->UniformStorage
[idx
]);
1098 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1100 return RESOURCE_ATC(res
)->NumUniforms
;
1102 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1104 case GL_BUFFER_BINDING
:
1105 *val
= RESOURCE_XFB(res
)->Binding
;
1107 case GL_NUM_ACTIVE_VARIABLES
:
1108 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1110 case GL_ACTIVE_VARIABLES
:
1111 struct gl_transform_feedback_info
*linked_xfb
=
1112 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1113 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1114 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1115 struct gl_program_resource
*buf_res
=
1116 _mesa_program_resource_find_index(shProg
,
1117 GL_TRANSFORM_FEEDBACK_BUFFER
,
1120 if (res
== buf_res
) {
1124 return RESOURCE_XFB(res
)->NumVaryings
;
1127 assert(!"support for property type not implemented");
1130 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1131 _mesa_enum_to_string(res
->Type
),
1132 _mesa_enum_to_string(prop
));
1138 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1139 struct gl_program_resource
*res
, GLuint index
,
1140 const GLenum prop
, GLint
*val
, const char *caller
)
1142 GET_CURRENT_CONTEXT(ctx
);
1144 #define VALIDATE_TYPE(type)\
1145 if (res->Type != type)\
1146 goto invalid_operation;
1148 #define VALIDATE_TYPE_2(type1, type2)\
1149 if (res->Type != type1 && res->Type != type2)\
1150 goto invalid_operation;
1153 case GL_NAME_LENGTH
:
1154 switch (res
->Type
) {
1155 case GL_ATOMIC_COUNTER_BUFFER
:
1156 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1157 goto invalid_operation
;
1159 /* Resource name length + terminator. */
1160 *val
= _mesa_program_resource_name_len(res
) + 1;
1164 switch (res
->Type
) {
1166 case GL_BUFFER_VARIABLE
:
1167 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1169 case GL_PROGRAM_INPUT
:
1170 case GL_PROGRAM_OUTPUT
:
1171 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1173 case GL_TRANSFORM_FEEDBACK_VARYING
:
1174 *val
= RESOURCE_XFV(res
)->Type
;
1177 goto invalid_operation
;
1180 switch (res
->Type
) {
1182 case GL_BUFFER_VARIABLE
:
1183 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1184 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1185 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1186 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1187 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1188 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1190 /* Test if a buffer variable is an array or an unsized array.
1191 * Unsized arrays return zero as array size.
1193 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1194 RESOURCE_UNI(res
)->array_stride
> 0)
1195 *val
= RESOURCE_UNI(res
)->array_elements
;
1197 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1199 case GL_PROGRAM_INPUT
:
1200 case GL_PROGRAM_OUTPUT
:
1201 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1203 case GL_TRANSFORM_FEEDBACK_VARYING
:
1204 *val
= RESOURCE_XFV(res
)->Size
;
1207 goto invalid_operation
;
1210 switch (res
->Type
) {
1212 case GL_BUFFER_VARIABLE
:
1213 *val
= RESOURCE_UNI(res
)->offset
;
1215 case GL_TRANSFORM_FEEDBACK_VARYING
:
1216 *val
= RESOURCE_XFV(res
)->Offset
;
1219 goto invalid_operation
;
1221 case GL_BLOCK_INDEX
:
1222 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1223 *val
= RESOURCE_UNI(res
)->block_index
;
1225 case GL_ARRAY_STRIDE
:
1226 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1227 *val
= RESOURCE_UNI(res
)->array_stride
;
1229 case GL_MATRIX_STRIDE
:
1230 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1231 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1233 case GL_IS_ROW_MAJOR
:
1234 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1235 *val
= RESOURCE_UNI(res
)->row_major
;
1237 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1238 VALIDATE_TYPE(GL_UNIFORM
);
1239 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1241 case GL_BUFFER_BINDING
:
1242 case GL_BUFFER_DATA_SIZE
:
1243 case GL_NUM_ACTIVE_VARIABLES
:
1244 case GL_ACTIVE_VARIABLES
:
1245 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1246 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1247 if (!_mesa_has_compute_shaders(ctx
))
1250 case GL_REFERENCED_BY_VERTEX_SHADER
:
1251 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1252 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1253 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1254 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1255 switch (res
->Type
) {
1257 case GL_PROGRAM_INPUT
:
1258 case GL_PROGRAM_OUTPUT
:
1259 case GL_UNIFORM_BLOCK
:
1260 case GL_BUFFER_VARIABLE
:
1261 case GL_SHADER_STORAGE_BLOCK
:
1262 case GL_ATOMIC_COUNTER_BUFFER
:
1263 *val
= is_resource_referenced(shProg
, res
, index
,
1264 stage_from_enum(prop
));
1267 goto invalid_operation
;
1270 switch (res
->Type
) {
1272 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1273 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1274 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1275 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1276 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1277 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1278 case GL_PROGRAM_INPUT
:
1279 case GL_PROGRAM_OUTPUT
:
1280 *val
= program_resource_location(res
, 0);
1283 goto invalid_operation
;
1285 case GL_LOCATION_COMPONENT
:
1286 switch (res
->Type
) {
1287 case GL_PROGRAM_INPUT
:
1288 case GL_PROGRAM_OUTPUT
:
1289 *val
= RESOURCE_VAR(res
)->component
;
1292 goto invalid_operation
;
1294 case GL_LOCATION_INDEX
: {
1296 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1297 goto invalid_operation
;
1298 tmp
= program_resource_location(res
, 0);
1302 *val
= _mesa_program_resource_location_index(shProg
, res
->Type
,
1303 RESOURCE_VAR(res
)->name
);
1306 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1307 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1308 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1309 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1310 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1311 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1312 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1313 goto invalid_operation
;
1314 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1316 case GL_COMPATIBLE_SUBROUTINES
: {
1317 const struct gl_uniform_storage
*uni
;
1318 struct gl_program
*p
;
1322 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1323 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1324 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1325 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1326 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1327 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1328 goto invalid_operation
;
1329 uni
= RESOURCE_UNI(res
);
1331 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1333 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1334 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1335 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1336 if (fn
->types
[j
] == uni
->type
) {
1345 case GL_TOP_LEVEL_ARRAY_SIZE
:
1346 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1347 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1350 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1351 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1352 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1355 /* GL_ARB_tessellation_shader */
1356 case GL_IS_PER_PATCH
:
1357 switch (res
->Type
) {
1358 case GL_PROGRAM_INPUT
:
1359 case GL_PROGRAM_OUTPUT
:
1360 *val
= RESOURCE_VAR(res
)->patch
;
1363 goto invalid_operation
;
1366 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1367 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1368 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1370 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1371 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1372 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1379 #undef VALIDATE_TYPE
1380 #undef VALIDATE_TYPE_2
1383 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1384 _mesa_enum_to_string(res
->Type
),
1385 _mesa_enum_to_string(prop
));
1389 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1390 _mesa_enum_to_string(res
->Type
),
1391 _mesa_enum_to_string(prop
));
1396 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1397 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1398 const GLenum
*props
, GLsizei bufSize
,
1399 GLsizei
*length
, GLint
*params
)
1401 GET_CURRENT_CONTEXT(ctx
);
1402 GLint
*val
= (GLint
*) params
;
1403 const GLenum
*prop
= props
;
1406 struct gl_program_resource
*res
=
1407 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1409 /* No such resource found or bufSize negative. */
1410 if (!res
|| bufSize
< 0) {
1411 _mesa_error(ctx
, GL_INVALID_VALUE
,
1412 "glGetProgramResourceiv(%s index %d bufSize %d)",
1413 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1417 /* Write propCount values until error occurs or bufSize reached. */
1418 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1420 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1421 "glGetProgramResourceiv");
1423 /* Error happened. */
1424 if (props_written
== 0)
1427 amount
+= props_written
;
1430 /* If <length> is not NULL, the actual number of integer values
1431 * written to <params> will be written to <length>.
1438 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1440 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1443 const bool producer_is_array_stage
=
1444 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1445 const bool consumer_is_array_stage
=
1446 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1447 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1448 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1452 gl_shader_variable
const **outputs
=
1453 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1454 sizeof(gl_shader_variable
*));
1455 if (outputs
== NULL
)
1458 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1461 * At an interface between program objects, the set of inputs and
1462 * outputs are considered to match exactly if and only if:
1464 * - Every declared input variable has a matching output, as described
1466 * - There are no user-defined output variables declared without a
1467 * matching input variable declaration.
1469 * Every input has an output, and every output has an input. Scan the list
1470 * of producer resources once, and generate the list of outputs. As inputs
1471 * and outputs are matched, remove the matched outputs from the set. At
1472 * the end, the set must be empty. If the set is not empty, then there is
1473 * some output that did not have an input.
1475 unsigned num_outputs
= 0;
1476 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1477 struct gl_program_resource
*res
=
1478 &producer
->sh
.data
->ProgramResourceList
[i
];
1480 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1483 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1485 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1488 * Built-in inputs or outputs do not affect interface matching.
1490 if (is_gl_identifier(var
->name
))
1493 outputs
[num_outputs
++] = var
;
1496 unsigned match_index
= 0;
1497 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1498 struct gl_program_resource
*res
=
1499 &consumer
->sh
.data
->ProgramResourceList
[i
];
1501 if (res
->Type
!= GL_PROGRAM_INPUT
)
1504 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1505 gl_shader_variable
const *producer_var
= NULL
;
1507 if (is_gl_identifier(consumer_var
->name
))
1510 /* Inputs with explicit locations match other outputs with explicit
1511 * locations by location instead of by name.
1513 if (consumer_var
->explicit_location
) {
1514 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1515 const gl_shader_variable
*const var
= outputs
[j
];
1517 if (var
->explicit_location
&&
1518 consumer_var
->location
== var
->location
) {
1525 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1526 const gl_shader_variable
*const var
= outputs
[j
];
1528 if (!var
->explicit_location
&&
1529 strcmp(consumer_var
->name
, var
->name
) == 0) {
1537 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1540 * - An output variable is considered to match an input variable in
1541 * the subsequent shader if:
1543 * - the two variables match in name, type, and qualification; or
1545 * - the two variables are declared with the same location
1546 * qualifier and match in type and qualification.
1548 if (producer_var
== NULL
) {
1553 /* An output cannot match more than one input, so remove the output from
1554 * the set of possible outputs.
1556 outputs
[match_index
] = NULL
;
1558 if (match_index
< num_outputs
)
1559 outputs
[match_index
] = outputs
[num_outputs
];
1561 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1563 * "Tessellation control shader per-vertex output variables and
1564 * blocks and tessellation control, tessellation evaluation, and
1565 * geometry shader per-vertex input variables and blocks are
1566 * required to be declared as arrays, with each element representing
1567 * input or output values for a single vertex of a multi-vertex
1568 * primitive. For the purposes of interface matching, such variables
1569 * and blocks are treated as though they were not declared as
1572 * So we unwrap those types before matching.
1574 const glsl_type
*consumer_type
= consumer_var
->type
;
1575 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1576 const glsl_type
*producer_type
= producer_var
->type
;
1577 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1579 if (consumer_is_array_stage
) {
1580 if (consumer_interface_type
) {
1581 /* the interface is the array; the underlying types should match */
1582 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1583 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1585 if (consumer_type
->is_array() && !consumer_var
->patch
)
1586 consumer_type
= consumer_type
->fields
.array
;
1590 if (producer_is_array_stage
) {
1591 if (producer_interface_type
) {
1592 /* the interface is the array; the underlying types should match */
1593 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1594 producer_interface_type
= producer_interface_type
->fields
.array
;
1596 if (producer_type
->is_array() && !producer_var
->patch
)
1597 producer_type
= producer_type
->fields
.array
;
1601 if (producer_type
!= consumer_type
) {
1606 if (producer_interface_type
!= consumer_interface_type
) {
1611 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1613 * Qualifier Class| Qualifier |in/out
1614 * ---------------+-------------+------
1618 * ---------------+-------------+------
1619 * Auxiliary | centroid | No
1620 * ---------------+-------------+------
1622 * | Block layout| N/A
1626 * ---------------+-------------+------
1627 * Interpolation | smooth |
1629 * ---------------+-------------+------
1631 * Precision | mediump | Yes
1633 * ---------------+-------------+------
1634 * Variance | invariant | No
1635 * ---------------+-------------+------
1636 * Memory | all | N/A
1638 * Note that location mismatches are detected by the loops above that
1639 * find the producer variable that goes with the consumer variable.
1641 unsigned producer_interpolation
= producer_var
->interpolation
;
1642 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1643 if (producer_interpolation
== INTERP_MODE_NONE
)
1644 producer_interpolation
= INTERP_MODE_SMOOTH
;
1645 if (consumer_interpolation
== INTERP_MODE_NONE
)
1646 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1647 if (producer_interpolation
!= consumer_interpolation
) {
1652 if (producer_var
->precision
!= consumer_var
->precision
) {
1657 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1665 return valid
&& num_outputs
== 0;
1669 * Validate inputs against outputs in a program pipeline.
1672 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1674 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1676 /* Find first active stage in pipeline. */
1677 unsigned idx
, prev
= 0;
1678 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1685 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1687 /* Pipeline might include both non-compute and a compute program, do
1688 * not attempt to validate varyings between non-compute and compute
1691 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1694 if (!validate_io(prog
[prev
], prog
[idx
]))