2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/context.h"
32 #include "main/core.h"
33 #include "main/enums.h"
34 #include "main/shaderapi.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "compiler/glsl/glsl_symbol_table.h"
38 #include "compiler/glsl/ir.h"
39 #include "compiler/glsl/program.h"
40 #include "util/string_to_uint_map.h"
44 program_resource_location(struct gl_program_resource
*res
,
45 unsigned array_index
);
48 * Declare convenience functions to return resource data in a given type.
49 * Warning! this is not type safe so be *very* careful when using these.
51 #define DECL_RESOURCE_FUNC(name, type) \
52 const type * RESOURCE_ ## name (gl_program_resource *res) { \
54 return (type *) res->Data; \
57 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
58 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
59 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
60 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
61 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
62 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
63 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
66 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
69 GET_CURRENT_CONTEXT(ctx
);
71 struct gl_shader_program
*const shProg
=
72 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
79 if (strncmp(name
, "gl_", 3) == 0) {
80 _mesa_error(ctx
, GL_INVALID_OPERATION
,
81 "glBindAttribLocation(illegal name)");
85 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
86 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(%u >= %u)",
87 index
, ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
);
91 /* Replace the current value if it's already in the list. Add
92 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
93 * between built-in attributes and user-defined attributes.
95 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
98 * Note that this attribute binding won't go into effect until
99 * glLinkProgram is called again.
104 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
105 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
106 GLenum
* type
, GLchar
* name
)
108 GET_CURRENT_CONTEXT(ctx
);
109 struct gl_shader_program
*shProg
;
112 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
116 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
120 if (!shProg
->data
->LinkStatus
) {
121 _mesa_error(ctx
, GL_INVALID_VALUE
,
122 "glGetActiveAttrib(program not linked)");
126 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
127 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
131 struct gl_program_resource
*res
=
132 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
135 /* User asked for index that does not exist. */
137 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
141 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
143 const char *var_name
= var
->name
;
145 _mesa_copy_string(name
, maxLength
, length
, var_name
);
148 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
149 size
, "glGetActiveAttrib");
152 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
153 (GLint
*) type
, "glGetActiveAttrib");
157 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
159 GET_CURRENT_CONTEXT(ctx
);
160 struct gl_shader_program
*const shProg
=
161 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
167 if (!shProg
->data
->LinkStatus
) {
168 _mesa_error(ctx
, GL_INVALID_OPERATION
,
169 "glGetAttribLocation(program not linked)");
176 /* Not having a vertex shader is not an error.
178 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
181 unsigned array_index
= 0;
182 struct gl_program_resource
*res
=
183 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
189 return program_resource_location(res
, array_index
);
193 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
195 if (!shProg
->data
->LinkStatus
196 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
200 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
202 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
204 if (res
->Type
== GL_PROGRAM_INPUT
&&
205 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
))
213 _mesa_longest_attribute_name_length(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
)) {
227 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
228 if (length
>= longest
)
229 longest
= length
+ 1;
237 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
240 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
244 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
245 GLuint index
, const GLchar
*name
)
247 GET_CURRENT_CONTEXT(ctx
);
249 struct gl_shader_program
*const shProg
=
250 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
257 if (strncmp(name
, "gl_", 3) == 0) {
258 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
263 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
267 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
268 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
272 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
273 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
277 /* Replace the current value if it's already in the list. Add
278 * FRAG_RESULT_DATA0 because that's how the linker differentiates
279 * between built-in attributes and user-defined attributes.
281 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
282 shProg
->FragDataIndexBindings
->put(index
, name
);
284 * Note that this binding won't go into effect until
285 * glLinkProgram is called again.
291 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
293 GET_CURRENT_CONTEXT(ctx
);
294 struct gl_shader_program
*const shProg
=
295 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
301 if (!shProg
->data
->LinkStatus
) {
302 _mesa_error(ctx
, GL_INVALID_OPERATION
,
303 "glGetFragDataIndex(program not linked)");
310 if (strncmp(name
, "gl_", 3) == 0) {
311 _mesa_error(ctx
, GL_INVALID_OPERATION
,
312 "glGetFragDataIndex(illegal name)");
316 /* Not having a fragment shader is not an error.
318 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
321 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
326 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
328 GET_CURRENT_CONTEXT(ctx
);
329 struct gl_shader_program
*const shProg
=
330 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
336 if (!shProg
->data
->LinkStatus
) {
337 _mesa_error(ctx
, GL_INVALID_OPERATION
,
338 "glGetFragDataLocation(program not linked)");
345 if (strncmp(name
, "gl_", 3) == 0) {
346 _mesa_error(ctx
, GL_INVALID_OPERATION
,
347 "glGetFragDataLocation(illegal name)");
351 /* Not having a fragment shader is not an error.
353 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
356 unsigned array_index
= 0;
357 struct gl_program_resource
*res
=
358 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
364 return program_resource_location(res
, array_index
);
368 _mesa_program_resource_name(struct gl_program_resource
*res
)
371 case GL_UNIFORM_BLOCK
:
372 case GL_SHADER_STORAGE_BLOCK
:
373 return RESOURCE_UBO(res
)->Name
;
374 case GL_TRANSFORM_FEEDBACK_VARYING
:
375 return RESOURCE_XFV(res
)->Name
;
376 case GL_PROGRAM_INPUT
:
377 case GL_PROGRAM_OUTPUT
:
378 return RESOURCE_VAR(res
)->name
;
380 case GL_BUFFER_VARIABLE
:
381 return RESOURCE_UNI(res
)->name
;
382 case GL_VERTEX_SUBROUTINE_UNIFORM
:
383 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
384 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
385 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
386 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
387 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
388 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
389 case GL_VERTEX_SUBROUTINE
:
390 case GL_GEOMETRY_SUBROUTINE
:
391 case GL_FRAGMENT_SUBROUTINE
:
392 case GL_COMPUTE_SUBROUTINE
:
393 case GL_TESS_CONTROL_SUBROUTINE
:
394 case GL_TESS_EVALUATION_SUBROUTINE
:
395 return RESOURCE_SUB(res
)->name
;
397 assert(!"support for resource type not implemented");
404 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
407 case GL_TRANSFORM_FEEDBACK_VARYING
:
408 return RESOURCE_XFV(res
)->Size
> 1 ?
409 RESOURCE_XFV(res
)->Size
: 0;
410 case GL_PROGRAM_INPUT
:
411 case GL_PROGRAM_OUTPUT
:
412 return RESOURCE_VAR(res
)->type
->length
;
414 case GL_VERTEX_SUBROUTINE_UNIFORM
:
415 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
416 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
417 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
418 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
419 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
420 return RESOURCE_UNI(res
)->array_elements
;
421 case GL_BUFFER_VARIABLE
:
423 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
424 RESOURCE_UNI(res
)->array_elements
== 0)
427 return RESOURCE_UNI(res
)->array_elements
;
428 case GL_VERTEX_SUBROUTINE
:
429 case GL_GEOMETRY_SUBROUTINE
:
430 case GL_FRAGMENT_SUBROUTINE
:
431 case GL_COMPUTE_SUBROUTINE
:
432 case GL_TESS_CONTROL_SUBROUTINE
:
433 case GL_TESS_EVALUATION_SUBROUTINE
:
434 case GL_ATOMIC_COUNTER_BUFFER
:
435 case GL_UNIFORM_BLOCK
:
436 case GL_SHADER_STORAGE_BLOCK
:
439 assert(!"support for resource type not implemented");
445 * Checks if array subscript is valid and if so sets array_index.
448 valid_array_index(const GLchar
*name
, unsigned *array_index
)
451 const GLchar
*out_base_name_end
;
453 idx
= parse_program_resource_name(name
, &out_base_name_end
);
463 /* Find a program resource with specific name in given interface.
465 struct gl_program_resource
*
466 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
467 GLenum programInterface
, const char *name
,
468 unsigned *array_index
)
470 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
471 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
473 if (res
->Type
!= programInterface
)
476 /* Resource basename. */
477 const char *rname
= _mesa_program_resource_name(res
);
478 unsigned baselen
= strlen(rname
);
479 unsigned baselen_without_array_index
= baselen
;
480 const char *rname_last_square_bracket
= strrchr(rname
, '[');
482 bool rname_has_array_index_zero
= false;
483 /* From ARB_program_interface_query spec:
485 * "uint GetProgramResourceIndex(uint program, enum programInterface,
488 * If <name> exactly matches the name string of one of the active
489 * resources for <programInterface>, the index of the matched resource is
490 * returned. Additionally, if <name> would exactly match the name string
491 * of an active resource if "[0]" were appended to <name>, the index of
492 * the matched resource is returned. [...]"
494 * "A string provided to GetProgramResourceLocation or
495 * GetProgramResourceLocationIndex is considered to match an active variable
498 * * the string exactly matches the name of the active variable;
500 * * if the string identifies the base name of an active array, where the
501 * string would exactly match the name of the variable if the suffix
502 * "[0]" were appended to the string; [...]"
504 /* Remove array's index from interface block name comparison only if
505 * array's index is zero and the resulting string length is the same
506 * than the provided name's length.
508 if (rname_last_square_bracket
) {
509 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
510 rname_has_array_index_zero
=
511 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
512 (baselen_without_array_index
== strlen(name
));
515 if (strncmp(rname
, name
, baselen
) == 0)
517 else if (rname_has_array_index_zero
&&
518 strncmp(rname
, name
, baselen_without_array_index
) == 0)
522 switch (programInterface
) {
523 case GL_UNIFORM_BLOCK
:
524 case GL_SHADER_STORAGE_BLOCK
:
525 /* Basename match, check if array or struct. */
526 if (rname_has_array_index_zero
||
527 name
[baselen
] == '\0' ||
528 name
[baselen
] == '[' ||
529 name
[baselen
] == '.') {
533 case GL_TRANSFORM_FEEDBACK_VARYING
:
534 case GL_BUFFER_VARIABLE
:
536 case GL_VERTEX_SUBROUTINE_UNIFORM
:
537 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
538 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
539 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
540 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
541 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
542 case GL_VERTEX_SUBROUTINE
:
543 case GL_GEOMETRY_SUBROUTINE
:
544 case GL_FRAGMENT_SUBROUTINE
:
545 case GL_COMPUTE_SUBROUTINE
:
546 case GL_TESS_CONTROL_SUBROUTINE
:
547 case GL_TESS_EVALUATION_SUBROUTINE
:
548 if (name
[baselen
] == '.') {
552 case GL_PROGRAM_INPUT
:
553 case GL_PROGRAM_OUTPUT
:
554 if (name
[baselen
] == '\0') {
556 } else if (name
[baselen
] == '[' &&
557 valid_array_index(name
, array_index
)) {
562 assert(!"not implemented for given interface");
570 calc_resource_index(struct gl_shader_program
*shProg
,
571 struct gl_program_resource
*res
)
575 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
576 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
578 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
581 return GL_INVALID_INDEX
;
585 * Calculate index for the given resource.
588 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
589 struct gl_program_resource
*res
)
592 return GL_INVALID_INDEX
;
595 case GL_ATOMIC_COUNTER_BUFFER
:
596 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
597 case GL_VERTEX_SUBROUTINE
:
598 case GL_GEOMETRY_SUBROUTINE
:
599 case GL_FRAGMENT_SUBROUTINE
:
600 case GL_COMPUTE_SUBROUTINE
:
601 case GL_TESS_CONTROL_SUBROUTINE
:
602 case GL_TESS_EVALUATION_SUBROUTINE
:
603 return RESOURCE_SUB(res
)->index
;
604 case GL_UNIFORM_BLOCK
:
605 case GL_SHADER_STORAGE_BLOCK
:
606 case GL_TRANSFORM_FEEDBACK_BUFFER
:
607 case GL_TRANSFORM_FEEDBACK_VARYING
:
609 return calc_resource_index(shProg
, res
);
614 * Find a program resource that points to given data.
616 static struct gl_program_resource
*
617 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
619 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
620 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
622 if (res
->Data
== data
)
628 /* Find a program resource with specific index in given interface.
630 struct gl_program_resource
*
631 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
632 GLenum programInterface
, GLuint index
)
634 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
637 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
639 if (res
->Type
!= programInterface
)
643 case GL_UNIFORM_BLOCK
:
644 case GL_ATOMIC_COUNTER_BUFFER
:
645 case GL_SHADER_STORAGE_BLOCK
:
646 case GL_TRANSFORM_FEEDBACK_BUFFER
:
647 if (_mesa_program_resource_index(shProg
, res
) == index
)
650 case GL_TRANSFORM_FEEDBACK_VARYING
:
651 case GL_PROGRAM_INPUT
:
652 case GL_PROGRAM_OUTPUT
:
654 case GL_VERTEX_SUBROUTINE_UNIFORM
:
655 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
656 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
657 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
658 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
659 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
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 case GL_BUFFER_VARIABLE
:
667 if (++idx
== (int) index
)
671 assert(!"not implemented for given interface");
677 /* Function returns if resource name is expected to have index
681 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
684 * "If the active uniform is an array, the uniform name returned in
685 * name will always be the name of the uniform array appended with
688 * The same text also appears in the OpenGL 4.2 spec. It does not,
689 * however, appear in any previous spec. Previous specifications are
690 * ambiguous in this regard. However, either name can later be passed
691 * to glGetUniformLocation (and related APIs), so there shouldn't be any
692 * harm in always appending "[0]" to uniform array names.
695 add_index_to_name(struct gl_program_resource
*res
)
697 /* Transform feedback varyings have array index already appended
700 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
703 /* Get name length of a program resource. This consists of
704 * base name + 3 for '[0]' if resource is an array.
707 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
709 unsigned length
= strlen(_mesa_program_resource_name(res
));
710 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
715 /* Get full name of a program resource.
718 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
719 GLenum programInterface
, GLuint index
,
720 GLsizei bufSize
, GLsizei
*length
,
721 GLchar
*name
, const char *caller
)
723 GET_CURRENT_CONTEXT(ctx
);
725 /* Find resource with given interface and index. */
726 struct gl_program_resource
*res
=
727 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
729 /* The error INVALID_VALUE is generated if <index> is greater than
730 * or equal to the number of entries in the active resource list for
731 * <programInterface>.
734 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
739 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
746 length
= &localLength
;
748 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
750 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
753 /* The comparison is strange because *length does *NOT* include the
754 * terminating NUL, but maxLength does.
756 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
757 name
[*length
+ i
] = "[0]"[i
];
759 name
[*length
+ i
] = '\0';
766 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
769 case GL_PROGRAM_INPUT
: {
770 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
772 if (var
->location
== -1)
775 /* If the input is an array, fail if the index is out of bounds. */
777 && array_index
>= var
->type
->length
) {
780 return var
->location
+
781 (array_index
* var
->type
->without_array()->matrix_columns
);
783 case GL_PROGRAM_OUTPUT
:
784 if (RESOURCE_VAR(res
)->location
== -1)
787 /* If the output is an array, fail if the index is out of bounds. */
789 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
792 return RESOURCE_VAR(res
)->location
+ array_index
;
794 /* If the uniform is built-in, fail. */
795 if (RESOURCE_UNI(res
)->builtin
)
798 /* From page 79 of the OpenGL 4.2 spec:
800 * "A valid name cannot be a structure, an array of structures, or any
801 * portion of a single vector or a matrix."
803 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
806 /* From the GL_ARB_uniform_buffer_object spec:
808 * "The value -1 will be returned if <name> does not correspond to an
809 * active uniform variable name in <program>, if <name> is associated
810 * with a named uniform block, or if <name> starts with the reserved
813 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
814 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
818 case GL_VERTEX_SUBROUTINE_UNIFORM
:
819 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
820 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
821 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
822 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
823 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
824 /* If the uniform is an array, fail if the index is out of bounds. */
826 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
830 /* location in remap table + array element offset */
831 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
838 * Function implements following location queries:
839 * glGetUniformLocation
842 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
843 GLenum programInterface
, const char *name
)
845 unsigned array_index
= 0;
846 struct gl_program_resource
*res
=
847 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
850 /* Resource not found. */
854 return program_resource_location(res
, array_index
);
858 * Function implements following index queries:
862 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
863 GLenum programInterface
, const char *name
)
865 struct gl_program_resource
*res
=
866 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
868 /* Non-existent variable or resource is not referenced by fragment stage. */
869 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
872 /* From OpenGL 4.5 spec, 7.3 Program Objects
873 * "The value -1 will be returned by either command...
874 * ... or if name identifies an active variable that does not have a
875 * valid location assigned.
877 if (RESOURCE_VAR(res
)->location
== -1)
879 return RESOURCE_VAR(res
)->index
;
883 stage_from_enum(GLenum ref
)
886 case GL_REFERENCED_BY_VERTEX_SHADER
:
887 return MESA_SHADER_VERTEX
;
888 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
889 return MESA_SHADER_TESS_CTRL
;
890 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
891 return MESA_SHADER_TESS_EVAL
;
892 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
893 return MESA_SHADER_GEOMETRY
;
894 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
895 return MESA_SHADER_FRAGMENT
;
896 case GL_REFERENCED_BY_COMPUTE_SHADER
:
897 return MESA_SHADER_COMPUTE
;
899 assert(!"shader stage not supported");
900 return MESA_SHADER_STAGES
;
905 * Check if resource is referenced by given 'referenced by' stage enum.
906 * ATC and UBO resources hold stage references of their own.
909 is_resource_referenced(struct gl_shader_program
*shProg
,
910 struct gl_program_resource
*res
,
911 GLuint index
, uint8_t stage
)
913 /* First, check if we even have such a stage active. */
914 if (!shProg
->_LinkedShaders
[stage
])
917 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
918 return RESOURCE_ATC(res
)->StageReferences
[stage
];
920 if (res
->Type
== GL_UNIFORM_BLOCK
)
921 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
923 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
924 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
926 return res
->StageReferences
& (1 << stage
);
930 get_buffer_property(struct gl_shader_program
*shProg
,
931 struct gl_program_resource
*res
, const GLenum prop
,
932 GLint
*val
, const char *caller
)
934 GET_CURRENT_CONTEXT(ctx
);
935 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
936 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
937 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
938 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
939 goto invalid_operation
;
941 if (res
->Type
== GL_UNIFORM_BLOCK
) {
943 case GL_BUFFER_BINDING
:
944 *val
= RESOURCE_UBO(res
)->Binding
;
946 case GL_BUFFER_DATA_SIZE
:
947 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
949 case GL_NUM_ACTIVE_VARIABLES
:
951 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
952 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
953 struct gl_program_resource
*uni
=
954 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
961 case GL_ACTIVE_VARIABLES
: {
962 unsigned num_values
= 0;
963 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
964 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
965 struct gl_program_resource
*uni
=
966 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
971 _mesa_program_resource_index(shProg
, uni
);
977 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
979 case GL_BUFFER_BINDING
:
980 *val
= RESOURCE_UBO(res
)->Binding
;
982 case GL_BUFFER_DATA_SIZE
:
983 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
985 case GL_NUM_ACTIVE_VARIABLES
:
987 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
988 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
989 struct gl_program_resource
*uni
=
990 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
997 case GL_ACTIVE_VARIABLES
: {
998 unsigned num_values
= 0;
999 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1000 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1001 struct gl_program_resource
*uni
=
1002 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1007 _mesa_program_resource_index(shProg
, uni
);
1013 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1015 case GL_BUFFER_BINDING
:
1016 *val
= RESOURCE_ATC(res
)->Binding
;
1018 case GL_BUFFER_DATA_SIZE
:
1019 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1021 case GL_NUM_ACTIVE_VARIABLES
:
1022 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1024 case GL_ACTIVE_VARIABLES
:
1025 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1026 /* Active atomic buffer contains index to UniformStorage. Find
1027 * out gl_program_resource via data pointer and then calculate
1028 * index of that uniform.
1030 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1031 struct gl_program_resource
*uni
=
1032 program_resource_find_data(shProg
,
1033 &shProg
->data
->UniformStorage
[idx
]);
1035 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1037 return RESOURCE_ATC(res
)->NumUniforms
;
1039 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1041 case GL_BUFFER_BINDING
:
1042 *val
= RESOURCE_XFB(res
)->Binding
;
1044 case GL_NUM_ACTIVE_VARIABLES
:
1045 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1047 case GL_ACTIVE_VARIABLES
:
1048 struct gl_transform_feedback_info
*linked_xfb
=
1049 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1050 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1051 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1052 struct gl_program_resource
*buf_res
=
1053 _mesa_program_resource_find_index(shProg
,
1054 GL_TRANSFORM_FEEDBACK_BUFFER
,
1057 if (res
== buf_res
) {
1061 return RESOURCE_XFB(res
)->NumVaryings
;
1064 assert(!"support for property type not implemented");
1067 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1068 _mesa_enum_to_string(res
->Type
),
1069 _mesa_enum_to_string(prop
));
1075 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1076 struct gl_program_resource
*res
, GLuint index
,
1077 const GLenum prop
, GLint
*val
, const char *caller
)
1079 GET_CURRENT_CONTEXT(ctx
);
1081 #define VALIDATE_TYPE(type)\
1082 if (res->Type != type)\
1083 goto invalid_operation;
1085 #define VALIDATE_TYPE_2(type1, type2)\
1086 if (res->Type != type1 && res->Type != type2)\
1087 goto invalid_operation;
1090 case GL_NAME_LENGTH
:
1091 switch (res
->Type
) {
1092 case GL_ATOMIC_COUNTER_BUFFER
:
1093 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1094 goto invalid_operation
;
1096 /* Resource name length + terminator. */
1097 *val
= _mesa_program_resource_name_len(res
) + 1;
1101 switch (res
->Type
) {
1103 case GL_BUFFER_VARIABLE
:
1104 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1106 case GL_PROGRAM_INPUT
:
1107 case GL_PROGRAM_OUTPUT
:
1108 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1110 case GL_TRANSFORM_FEEDBACK_VARYING
:
1111 *val
= RESOURCE_XFV(res
)->Type
;
1114 goto invalid_operation
;
1117 switch (res
->Type
) {
1119 case GL_BUFFER_VARIABLE
:
1120 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1121 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1122 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1123 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1124 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1125 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1127 /* Test if a buffer variable is an array or an unsized array.
1128 * Unsized arrays return zero as array size.
1130 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1131 RESOURCE_UNI(res
)->array_stride
> 0)
1132 *val
= RESOURCE_UNI(res
)->array_elements
;
1134 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1136 case GL_PROGRAM_INPUT
:
1137 case GL_PROGRAM_OUTPUT
:
1138 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1140 case GL_TRANSFORM_FEEDBACK_VARYING
:
1141 *val
= RESOURCE_XFV(res
)->Size
;
1144 goto invalid_operation
;
1147 switch (res
->Type
) {
1149 case GL_BUFFER_VARIABLE
:
1150 *val
= RESOURCE_UNI(res
)->offset
;
1152 case GL_TRANSFORM_FEEDBACK_VARYING
:
1153 *val
= RESOURCE_XFV(res
)->Offset
;
1156 goto invalid_operation
;
1158 case GL_BLOCK_INDEX
:
1159 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1160 *val
= RESOURCE_UNI(res
)->block_index
;
1162 case GL_ARRAY_STRIDE
:
1163 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1164 *val
= RESOURCE_UNI(res
)->array_stride
;
1166 case GL_MATRIX_STRIDE
:
1167 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1168 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1170 case GL_IS_ROW_MAJOR
:
1171 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1172 *val
= RESOURCE_UNI(res
)->row_major
;
1174 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1175 VALIDATE_TYPE(GL_UNIFORM
);
1176 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1178 case GL_BUFFER_BINDING
:
1179 case GL_BUFFER_DATA_SIZE
:
1180 case GL_NUM_ACTIVE_VARIABLES
:
1181 case GL_ACTIVE_VARIABLES
:
1182 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1183 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1184 if (!_mesa_has_compute_shaders(ctx
))
1187 case GL_REFERENCED_BY_VERTEX_SHADER
:
1188 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1189 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1190 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1191 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1192 switch (res
->Type
) {
1194 case GL_PROGRAM_INPUT
:
1195 case GL_PROGRAM_OUTPUT
:
1196 case GL_UNIFORM_BLOCK
:
1197 case GL_BUFFER_VARIABLE
:
1198 case GL_SHADER_STORAGE_BLOCK
:
1199 case GL_ATOMIC_COUNTER_BUFFER
:
1200 *val
= is_resource_referenced(shProg
, res
, index
,
1201 stage_from_enum(prop
));
1204 goto invalid_operation
;
1207 switch (res
->Type
) {
1209 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1210 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1211 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1212 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1213 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1214 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1215 case GL_PROGRAM_INPUT
:
1216 case GL_PROGRAM_OUTPUT
:
1217 *val
= program_resource_location(res
, 0);
1220 goto invalid_operation
;
1222 case GL_LOCATION_COMPONENT
:
1223 switch (res
->Type
) {
1224 case GL_PROGRAM_INPUT
:
1225 case GL_PROGRAM_OUTPUT
:
1226 *val
= RESOURCE_VAR(res
)->component
;
1229 goto invalid_operation
;
1231 case GL_LOCATION_INDEX
: {
1233 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1234 goto invalid_operation
;
1235 tmp
= program_resource_location(res
, 0);
1239 *val
= _mesa_program_resource_location_index(shProg
, res
->Type
,
1240 RESOURCE_VAR(res
)->name
);
1243 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1244 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1245 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1246 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1247 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1248 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1249 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1250 goto invalid_operation
;
1251 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1253 case GL_COMPATIBLE_SUBROUTINES
: {
1254 const struct gl_uniform_storage
*uni
;
1255 struct gl_program
*p
;
1259 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1260 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1261 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1262 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1263 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1264 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1265 goto invalid_operation
;
1266 uni
= RESOURCE_UNI(res
);
1268 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1270 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1271 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1272 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1273 if (fn
->types
[j
] == uni
->type
) {
1282 case GL_TOP_LEVEL_ARRAY_SIZE
:
1283 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1284 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1287 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1288 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1289 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1292 /* GL_ARB_tessellation_shader */
1293 case GL_IS_PER_PATCH
:
1294 switch (res
->Type
) {
1295 case GL_PROGRAM_INPUT
:
1296 case GL_PROGRAM_OUTPUT
:
1297 *val
= RESOURCE_VAR(res
)->patch
;
1300 goto invalid_operation
;
1303 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1304 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1305 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1307 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1308 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1309 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1316 #undef VALIDATE_TYPE
1317 #undef VALIDATE_TYPE_2
1320 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1321 _mesa_enum_to_string(res
->Type
),
1322 _mesa_enum_to_string(prop
));
1326 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1327 _mesa_enum_to_string(res
->Type
),
1328 _mesa_enum_to_string(prop
));
1333 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1334 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1335 const GLenum
*props
, GLsizei bufSize
,
1336 GLsizei
*length
, GLint
*params
)
1338 GET_CURRENT_CONTEXT(ctx
);
1339 GLint
*val
= (GLint
*) params
;
1340 const GLenum
*prop
= props
;
1343 struct gl_program_resource
*res
=
1344 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1346 /* No such resource found or bufSize negative. */
1347 if (!res
|| bufSize
< 0) {
1348 _mesa_error(ctx
, GL_INVALID_VALUE
,
1349 "glGetProgramResourceiv(%s index %d bufSize %d)",
1350 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1354 /* Write propCount values until error occurs or bufSize reached. */
1355 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1357 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1358 "glGetProgramResourceiv");
1360 /* Error happened. */
1361 if (props_written
== 0)
1364 amount
+= props_written
;
1367 /* If <length> is not NULL, the actual number of integer values
1368 * written to <params> will be written to <length>.
1375 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1377 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1380 const bool producer_is_array_stage
=
1381 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1382 const bool consumer_is_array_stage
=
1383 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1384 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1385 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1389 gl_shader_variable
const **outputs
=
1390 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1391 sizeof(gl_shader_variable
*));
1392 if (outputs
== NULL
)
1395 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1398 * At an interface between program objects, the set of inputs and
1399 * outputs are considered to match exactly if and only if:
1401 * - Every declared input variable has a matching output, as described
1403 * - There are no user-defined output variables declared without a
1404 * matching input variable declaration.
1406 * Every input has an output, and every output has an input. Scan the list
1407 * of producer resources once, and generate the list of outputs. As inputs
1408 * and outputs are matched, remove the matched outputs from the set. At
1409 * the end, the set must be empty. If the set is not empty, then there is
1410 * some output that did not have an input.
1412 unsigned num_outputs
= 0;
1413 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1414 struct gl_program_resource
*res
=
1415 &producer
->sh
.data
->ProgramResourceList
[i
];
1417 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1420 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1422 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1425 * Built-in inputs or outputs do not affect interface matching.
1427 if (is_gl_identifier(var
->name
))
1430 outputs
[num_outputs
++] = var
;
1433 unsigned match_index
= 0;
1434 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1435 struct gl_program_resource
*res
=
1436 &consumer
->sh
.data
->ProgramResourceList
[i
];
1438 if (res
->Type
!= GL_PROGRAM_INPUT
)
1441 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1442 gl_shader_variable
const *producer_var
= NULL
;
1444 if (is_gl_identifier(consumer_var
->name
))
1447 /* Inputs with explicit locations match other outputs with explicit
1448 * locations by location instead of by name.
1450 if (consumer_var
->explicit_location
) {
1451 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1452 const gl_shader_variable
*const var
= outputs
[j
];
1454 if (var
->explicit_location
&&
1455 consumer_var
->location
== var
->location
) {
1462 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1463 const gl_shader_variable
*const var
= outputs
[j
];
1465 if (!var
->explicit_location
&&
1466 strcmp(consumer_var
->name
, var
->name
) == 0) {
1474 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1477 * - An output variable is considered to match an input variable in
1478 * the subsequent shader if:
1480 * - the two variables match in name, type, and qualification; or
1482 * - the two variables are declared with the same location
1483 * qualifier and match in type and qualification.
1485 if (producer_var
== NULL
) {
1490 /* An output cannot match more than one input, so remove the output from
1491 * the set of possible outputs.
1493 outputs
[match_index
] = NULL
;
1495 if (match_index
< num_outputs
)
1496 outputs
[match_index
] = outputs
[num_outputs
];
1498 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1500 * "Tessellation control shader per-vertex output variables and
1501 * blocks and tessellation control, tessellation evaluation, and
1502 * geometry shader per-vertex input variables and blocks are
1503 * required to be declared as arrays, with each element representing
1504 * input or output values for a single vertex of a multi-vertex
1505 * primitive. For the purposes of interface matching, such variables
1506 * and blocks are treated as though they were not declared as
1509 * So we unwrap those types before matching.
1511 const glsl_type
*consumer_type
= consumer_var
->type
;
1512 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1513 const glsl_type
*producer_type
= producer_var
->type
;
1514 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1516 if (consumer_is_array_stage
) {
1517 if (consumer_interface_type
) {
1518 /* the interface is the array; the underlying types should match */
1519 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1520 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1522 if (consumer_type
->is_array() && !consumer_var
->patch
)
1523 consumer_type
= consumer_type
->fields
.array
;
1527 if (producer_is_array_stage
) {
1528 if (producer_interface_type
) {
1529 /* the interface is the array; the underlying types should match */
1530 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1531 producer_interface_type
= producer_interface_type
->fields
.array
;
1533 if (producer_type
->is_array() && !producer_var
->patch
)
1534 producer_type
= producer_type
->fields
.array
;
1538 if (producer_type
!= consumer_type
) {
1543 if (producer_interface_type
!= consumer_interface_type
) {
1548 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1550 * Qualifier Class| Qualifier |in/out
1551 * ---------------+-------------+------
1555 * ---------------+-------------+------
1556 * Auxiliary | centroid | No
1557 * ---------------+-------------+------
1559 * | Block layout| N/A
1563 * ---------------+-------------+------
1564 * Interpolation | smooth |
1566 * ---------------+-------------+------
1568 * Precision | mediump | Yes
1570 * ---------------+-------------+------
1571 * Variance | invariant | No
1572 * ---------------+-------------+------
1573 * Memory | all | N/A
1575 * Note that location mismatches are detected by the loops above that
1576 * find the producer variable that goes with the consumer variable.
1578 if (producer_var
->interpolation
!= consumer_var
->interpolation
) {
1583 if (producer_var
->precision
!= consumer_var
->precision
) {
1588 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1596 return valid
&& num_outputs
== 0;
1600 * Validate inputs against outputs in a program pipeline.
1603 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1605 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1607 /* Find first active stage in pipeline. */
1608 unsigned idx
, prev
= 0;
1609 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1616 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1618 /* Pipeline might include both non-compute and a compute program, do
1619 * not attempt to validate varyings between non-compute and compute
1622 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1625 if (!validate_io(prog
[prev
], prog
[idx
]))