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"
41 #include "util/strndup.h"
45 program_resource_location(struct gl_program_resource
*res
,
46 unsigned array_index
);
49 * Declare convenience functions to return resource data in a given type.
50 * Warning! this is not type safe so be *very* careful when using these.
52 #define DECL_RESOURCE_FUNC(name, type) \
53 const type * RESOURCE_ ## name (gl_program_resource *res) { \
55 return (type *) res->Data; \
58 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
59 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
60 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
61 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
62 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
63 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
64 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
67 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
70 GET_CURRENT_CONTEXT(ctx
);
72 struct gl_shader_program
*const shProg
=
73 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
80 if (strncmp(name
, "gl_", 3) == 0) {
81 _mesa_error(ctx
, GL_INVALID_OPERATION
,
82 "glBindAttribLocation(illegal name)");
86 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
87 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(%u >= %u)",
88 index
, ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
);
92 /* Replace the current value if it's already in the list. Add
93 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
94 * between built-in attributes and user-defined attributes.
96 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
99 * Note that this attribute binding won't go into effect until
100 * glLinkProgram is called again.
105 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
106 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
107 GLenum
* type
, GLchar
* name
)
109 GET_CURRENT_CONTEXT(ctx
);
110 struct gl_shader_program
*shProg
;
113 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
117 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
121 if (!shProg
->data
->LinkStatus
) {
122 _mesa_error(ctx
, GL_INVALID_VALUE
,
123 "glGetActiveAttrib(program not linked)");
127 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
128 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
132 struct gl_program_resource
*res
=
133 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
136 /* User asked for index that does not exist. */
138 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
142 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
144 const char *var_name
= var
->name
;
146 _mesa_copy_string(name
, maxLength
, length
, var_name
);
149 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
150 size
, "glGetActiveAttrib");
153 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
154 (GLint
*) type
, "glGetActiveAttrib");
158 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
160 GET_CURRENT_CONTEXT(ctx
);
161 struct gl_shader_program
*const shProg
=
162 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
168 if (!shProg
->data
->LinkStatus
) {
169 _mesa_error(ctx
, GL_INVALID_OPERATION
,
170 "glGetAttribLocation(program not linked)");
177 /* Not having a vertex shader is not an error.
179 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
182 unsigned array_index
= 0;
183 struct gl_program_resource
*res
=
184 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
190 return program_resource_location(res
, array_index
);
194 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
196 if (!shProg
->data
->LinkStatus
197 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
201 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
203 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
205 if (res
->Type
== GL_PROGRAM_INPUT
&&
206 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
))
214 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
216 if (!shProg
->data
->LinkStatus
217 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
221 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
223 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
225 if (res
->Type
== GL_PROGRAM_INPUT
&&
226 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
228 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
229 if (length
>= longest
)
230 longest
= length
+ 1;
238 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
241 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
245 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
246 GLuint index
, const GLchar
*name
)
248 GET_CURRENT_CONTEXT(ctx
);
250 struct gl_shader_program
*const shProg
=
251 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
258 if (strncmp(name
, "gl_", 3) == 0) {
259 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
264 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
268 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
269 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
273 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
274 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
278 /* Replace the current value if it's already in the list. Add
279 * FRAG_RESULT_DATA0 because that's how the linker differentiates
280 * between built-in attributes and user-defined attributes.
282 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
283 shProg
->FragDataIndexBindings
->put(index
, name
);
285 * Note that this binding won't go into effect until
286 * glLinkProgram is called again.
292 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
294 GET_CURRENT_CONTEXT(ctx
);
295 struct gl_shader_program
*const shProg
=
296 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
302 if (!shProg
->data
->LinkStatus
) {
303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
304 "glGetFragDataIndex(program not linked)");
311 if (strncmp(name
, "gl_", 3) == 0) {
312 _mesa_error(ctx
, GL_INVALID_OPERATION
,
313 "glGetFragDataIndex(illegal name)");
317 /* Not having a fragment shader is not an error.
319 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
322 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
327 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
329 GET_CURRENT_CONTEXT(ctx
);
330 struct gl_shader_program
*const shProg
=
331 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
337 if (!shProg
->data
->LinkStatus
) {
338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
339 "glGetFragDataLocation(program not linked)");
346 if (strncmp(name
, "gl_", 3) == 0) {
347 _mesa_error(ctx
, GL_INVALID_OPERATION
,
348 "glGetFragDataLocation(illegal name)");
352 /* Not having a fragment shader is not an error.
354 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
357 unsigned array_index
= 0;
358 struct gl_program_resource
*res
=
359 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
365 return program_resource_location(res
, array_index
);
369 _mesa_program_resource_name(struct gl_program_resource
*res
)
372 case GL_UNIFORM_BLOCK
:
373 case GL_SHADER_STORAGE_BLOCK
:
374 return RESOURCE_UBO(res
)->Name
;
375 case GL_TRANSFORM_FEEDBACK_VARYING
:
376 return RESOURCE_XFV(res
)->Name
;
377 case GL_PROGRAM_INPUT
:
378 case GL_PROGRAM_OUTPUT
:
379 return RESOURCE_VAR(res
)->name
;
381 case GL_BUFFER_VARIABLE
:
382 return RESOURCE_UNI(res
)->name
;
383 case GL_VERTEX_SUBROUTINE_UNIFORM
:
384 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
385 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
386 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
387 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
388 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
389 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
390 case GL_VERTEX_SUBROUTINE
:
391 case GL_GEOMETRY_SUBROUTINE
:
392 case GL_FRAGMENT_SUBROUTINE
:
393 case GL_COMPUTE_SUBROUTINE
:
394 case GL_TESS_CONTROL_SUBROUTINE
:
395 case GL_TESS_EVALUATION_SUBROUTINE
:
396 return RESOURCE_SUB(res
)->name
;
398 assert(!"support for resource type not implemented");
405 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
408 case GL_TRANSFORM_FEEDBACK_VARYING
:
409 return RESOURCE_XFV(res
)->Size
> 1 ?
410 RESOURCE_XFV(res
)->Size
: 0;
411 case GL_PROGRAM_INPUT
:
412 case GL_PROGRAM_OUTPUT
:
413 return RESOURCE_VAR(res
)->type
->length
;
415 case GL_VERTEX_SUBROUTINE_UNIFORM
:
416 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
417 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
418 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
419 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
420 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
421 return RESOURCE_UNI(res
)->array_elements
;
422 case GL_BUFFER_VARIABLE
:
424 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
425 RESOURCE_UNI(res
)->array_elements
== 0)
428 return RESOURCE_UNI(res
)->array_elements
;
429 case GL_VERTEX_SUBROUTINE
:
430 case GL_GEOMETRY_SUBROUTINE
:
431 case GL_FRAGMENT_SUBROUTINE
:
432 case GL_COMPUTE_SUBROUTINE
:
433 case GL_TESS_CONTROL_SUBROUTINE
:
434 case GL_TESS_EVALUATION_SUBROUTINE
:
435 case GL_ATOMIC_COUNTER_BUFFER
:
436 case GL_UNIFORM_BLOCK
:
437 case GL_SHADER_STORAGE_BLOCK
:
440 assert(!"support for resource type not implemented");
446 * Checks if array subscript is valid and if so sets array_index.
449 valid_array_index(const GLchar
*name
, unsigned *array_index
)
452 const GLchar
*out_base_name_end
;
454 idx
= parse_program_resource_name(name
, &out_base_name_end
);
464 /* Find a program resource with specific name in given interface.
466 struct gl_program_resource
*
467 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
468 GLenum programInterface
, const char *name
,
469 unsigned *array_index
)
471 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
472 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
474 if (res
->Type
!= programInterface
)
477 /* Resource basename. */
478 const char *rname
= _mesa_program_resource_name(res
);
479 unsigned baselen
= strlen(rname
);
480 unsigned baselen_without_array_index
= baselen
;
481 const char *rname_last_square_bracket
= strrchr(rname
, '[');
483 bool rname_has_array_index_zero
= false;
484 /* From ARB_program_interface_query spec:
486 * "uint GetProgramResourceIndex(uint program, enum programInterface,
489 * If <name> exactly matches the name string of one of the active
490 * resources for <programInterface>, the index of the matched resource is
491 * returned. Additionally, if <name> would exactly match the name string
492 * of an active resource if "[0]" were appended to <name>, the index of
493 * the matched resource is returned. [...]"
495 * "A string provided to GetProgramResourceLocation or
496 * GetProgramResourceLocationIndex is considered to match an active variable
499 * * the string exactly matches the name of the active variable;
501 * * if the string identifies the base name of an active array, where the
502 * string would exactly match the name of the variable if the suffix
503 * "[0]" were appended to the string; [...]"
505 /* Remove array's index from interface block name comparison only if
506 * array's index is zero and the resulting string length is the same
507 * than the provided name's length.
509 if (rname_last_square_bracket
) {
510 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
511 rname_has_array_index_zero
=
512 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
513 (baselen_without_array_index
== strlen(name
));
516 if (strncmp(rname
, name
, baselen
) == 0)
518 else if (rname_has_array_index_zero
&&
519 strncmp(rname
, name
, baselen_without_array_index
) == 0)
523 switch (programInterface
) {
524 case GL_UNIFORM_BLOCK
:
525 case GL_SHADER_STORAGE_BLOCK
:
526 /* Basename match, check if array or struct. */
527 if (rname_has_array_index_zero
||
528 name
[baselen
] == '\0' ||
529 name
[baselen
] == '[' ||
530 name
[baselen
] == '.') {
534 case GL_TRANSFORM_FEEDBACK_VARYING
:
535 case GL_BUFFER_VARIABLE
:
537 case GL_VERTEX_SUBROUTINE_UNIFORM
:
538 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
539 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
540 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
541 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
542 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
543 case GL_VERTEX_SUBROUTINE
:
544 case GL_GEOMETRY_SUBROUTINE
:
545 case GL_FRAGMENT_SUBROUTINE
:
546 case GL_COMPUTE_SUBROUTINE
:
547 case GL_TESS_CONTROL_SUBROUTINE
:
548 case GL_TESS_EVALUATION_SUBROUTINE
:
549 if (name
[baselen
] == '.') {
553 case GL_PROGRAM_INPUT
:
554 case GL_PROGRAM_OUTPUT
:
555 if (name
[baselen
] == '\0') {
557 } else if (name
[baselen
] == '[' &&
558 valid_array_index(name
, array_index
)) {
563 assert(!"not implemented for given interface");
571 calc_resource_index(struct gl_shader_program
*shProg
,
572 struct gl_program_resource
*res
)
576 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
577 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
579 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
582 return GL_INVALID_INDEX
;
586 * Calculate index for the given resource.
589 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
590 struct gl_program_resource
*res
)
593 return GL_INVALID_INDEX
;
596 case GL_ATOMIC_COUNTER_BUFFER
:
597 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
598 case GL_VERTEX_SUBROUTINE
:
599 case GL_GEOMETRY_SUBROUTINE
:
600 case GL_FRAGMENT_SUBROUTINE
:
601 case GL_COMPUTE_SUBROUTINE
:
602 case GL_TESS_CONTROL_SUBROUTINE
:
603 case GL_TESS_EVALUATION_SUBROUTINE
:
604 return RESOURCE_SUB(res
)->index
;
605 case GL_UNIFORM_BLOCK
:
606 case GL_SHADER_STORAGE_BLOCK
:
607 case GL_TRANSFORM_FEEDBACK_BUFFER
:
608 case GL_TRANSFORM_FEEDBACK_VARYING
:
610 return calc_resource_index(shProg
, res
);
615 * Find a program resource that points to given data.
617 static struct gl_program_resource
*
618 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
620 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
621 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
623 if (res
->Data
== data
)
629 /* Find a program resource with specific index in given interface.
631 struct gl_program_resource
*
632 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
633 GLenum programInterface
, GLuint index
)
635 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
638 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
640 if (res
->Type
!= programInterface
)
644 case GL_UNIFORM_BLOCK
:
645 case GL_ATOMIC_COUNTER_BUFFER
:
646 case GL_SHADER_STORAGE_BLOCK
:
647 case GL_TRANSFORM_FEEDBACK_BUFFER
:
648 if (_mesa_program_resource_index(shProg
, res
) == index
)
651 case GL_TRANSFORM_FEEDBACK_VARYING
:
652 case GL_PROGRAM_INPUT
:
653 case GL_PROGRAM_OUTPUT
:
655 case GL_VERTEX_SUBROUTINE_UNIFORM
:
656 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
657 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
658 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
659 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
660 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
661 case GL_VERTEX_SUBROUTINE
:
662 case GL_GEOMETRY_SUBROUTINE
:
663 case GL_FRAGMENT_SUBROUTINE
:
664 case GL_COMPUTE_SUBROUTINE
:
665 case GL_TESS_CONTROL_SUBROUTINE
:
666 case GL_TESS_EVALUATION_SUBROUTINE
:
667 case GL_BUFFER_VARIABLE
:
668 if (++idx
== (int) index
)
672 assert(!"not implemented for given interface");
678 /* Function returns if resource name is expected to have index
682 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
685 * "If the active uniform is an array, the uniform name returned in
686 * name will always be the name of the uniform array appended with
689 * The same text also appears in the OpenGL 4.2 spec. It does not,
690 * however, appear in any previous spec. Previous specifications are
691 * ambiguous in this regard. However, either name can later be passed
692 * to glGetUniformLocation (and related APIs), so there shouldn't be any
693 * harm in always appending "[0]" to uniform array names.
696 add_index_to_name(struct gl_program_resource
*res
)
698 /* Transform feedback varyings have array index already appended
701 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
704 /* Get name length of a program resource. This consists of
705 * base name + 3 for '[0]' if resource is an array.
708 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
710 unsigned length
= strlen(_mesa_program_resource_name(res
));
711 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
716 /* Get full name of a program resource.
719 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
720 GLenum programInterface
, GLuint index
,
721 GLsizei bufSize
, GLsizei
*length
,
722 GLchar
*name
, const char *caller
)
724 GET_CURRENT_CONTEXT(ctx
);
726 /* Find resource with given interface and index. */
727 struct gl_program_resource
*res
=
728 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
730 /* The error INVALID_VALUE is generated if <index> is greater than
731 * or equal to the number of entries in the active resource list for
732 * <programInterface>.
735 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
740 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
747 length
= &localLength
;
749 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
751 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
754 /* The comparison is strange because *length does *NOT* include the
755 * terminating NUL, but maxLength does.
757 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
758 name
[*length
+ i
] = "[0]"[i
];
760 name
[*length
+ i
] = '\0';
767 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
770 case GL_PROGRAM_INPUT
: {
771 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
773 if (var
->location
== -1)
776 /* If the input is an array, fail if the index is out of bounds. */
778 && array_index
>= var
->type
->length
) {
781 return var
->location
+
782 (array_index
* var
->type
->without_array()->matrix_columns
);
784 case GL_PROGRAM_OUTPUT
:
785 if (RESOURCE_VAR(res
)->location
== -1)
788 /* If the output is an array, fail if the index is out of bounds. */
790 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
793 return RESOURCE_VAR(res
)->location
+ array_index
;
795 /* If the uniform is built-in, fail. */
796 if (RESOURCE_UNI(res
)->builtin
)
799 /* From page 79 of the OpenGL 4.2 spec:
801 * "A valid name cannot be a structure, an array of structures, or any
802 * portion of a single vector or a matrix."
804 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
807 /* From the GL_ARB_uniform_buffer_object spec:
809 * "The value -1 will be returned if <name> does not correspond to an
810 * active uniform variable name in <program>, if <name> is associated
811 * with a named uniform block, or if <name> starts with the reserved
814 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
815 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
819 case GL_VERTEX_SUBROUTINE_UNIFORM
:
820 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
821 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
822 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
823 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
824 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
825 /* If the uniform is an array, fail if the index is out of bounds. */
827 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
831 /* location in remap table + array element offset */
832 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
839 * Function implements following location queries:
840 * glGetUniformLocation
843 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
844 GLenum programInterface
, const char *name
)
846 unsigned array_index
= 0;
847 struct gl_program_resource
*res
=
848 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
851 /* Resource not found. */
855 return program_resource_location(res
, array_index
);
859 * Function implements following index queries:
863 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
864 GLenum programInterface
, const char *name
)
866 struct gl_program_resource
*res
=
867 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
869 /* Non-existent variable or resource is not referenced by fragment stage. */
870 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
873 /* From OpenGL 4.5 spec, 7.3 Program Objects
874 * "The value -1 will be returned by either command...
875 * ... or if name identifies an active variable that does not have a
876 * valid location assigned.
878 if (RESOURCE_VAR(res
)->location
== -1)
880 return RESOURCE_VAR(res
)->index
;
884 stage_from_enum(GLenum ref
)
887 case GL_REFERENCED_BY_VERTEX_SHADER
:
888 return MESA_SHADER_VERTEX
;
889 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
890 return MESA_SHADER_TESS_CTRL
;
891 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
892 return MESA_SHADER_TESS_EVAL
;
893 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
894 return MESA_SHADER_GEOMETRY
;
895 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
896 return MESA_SHADER_FRAGMENT
;
897 case GL_REFERENCED_BY_COMPUTE_SHADER
:
898 return MESA_SHADER_COMPUTE
;
900 assert(!"shader stage not supported");
901 return MESA_SHADER_STAGES
;
906 * Check if resource is referenced by given 'referenced by' stage enum.
907 * ATC and UBO resources hold stage references of their own.
910 is_resource_referenced(struct gl_shader_program
*shProg
,
911 struct gl_program_resource
*res
,
912 GLuint index
, uint8_t stage
)
914 /* First, check if we even have such a stage active. */
915 if (!shProg
->_LinkedShaders
[stage
])
918 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
919 return RESOURCE_ATC(res
)->StageReferences
[stage
];
921 if (res
->Type
== GL_UNIFORM_BLOCK
)
922 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
924 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
925 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
927 return res
->StageReferences
& (1 << stage
);
931 get_buffer_property(struct gl_shader_program
*shProg
,
932 struct gl_program_resource
*res
, const GLenum prop
,
933 GLint
*val
, const char *caller
)
935 GET_CURRENT_CONTEXT(ctx
);
936 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
937 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
938 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
939 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
940 goto invalid_operation
;
942 if (res
->Type
== GL_UNIFORM_BLOCK
) {
944 case GL_BUFFER_BINDING
:
945 *val
= RESOURCE_UBO(res
)->Binding
;
947 case GL_BUFFER_DATA_SIZE
:
948 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
950 case GL_NUM_ACTIVE_VARIABLES
:
952 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
953 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
954 struct gl_program_resource
*uni
=
955 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
962 case GL_ACTIVE_VARIABLES
: {
963 unsigned num_values
= 0;
964 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
965 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
966 struct gl_program_resource
*uni
=
967 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
972 _mesa_program_resource_index(shProg
, uni
);
978 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
980 case GL_BUFFER_BINDING
:
981 *val
= RESOURCE_UBO(res
)->Binding
;
983 case GL_BUFFER_DATA_SIZE
:
984 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
986 case GL_NUM_ACTIVE_VARIABLES
:
988 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
989 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
990 struct gl_program_resource
*uni
=
991 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
998 case GL_ACTIVE_VARIABLES
: {
999 unsigned num_values
= 0;
1000 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1001 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1002 struct gl_program_resource
*uni
=
1003 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1008 _mesa_program_resource_index(shProg
, uni
);
1014 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1016 case GL_BUFFER_BINDING
:
1017 *val
= RESOURCE_ATC(res
)->Binding
;
1019 case GL_BUFFER_DATA_SIZE
:
1020 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1022 case GL_NUM_ACTIVE_VARIABLES
:
1023 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1025 case GL_ACTIVE_VARIABLES
:
1026 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1027 /* Active atomic buffer contains index to UniformStorage. Find
1028 * out gl_program_resource via data pointer and then calculate
1029 * index of that uniform.
1031 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1032 struct gl_program_resource
*uni
=
1033 program_resource_find_data(shProg
,
1034 &shProg
->data
->UniformStorage
[idx
]);
1036 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1038 return RESOURCE_ATC(res
)->NumUniforms
;
1040 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1042 case GL_BUFFER_BINDING
:
1043 *val
= RESOURCE_XFB(res
)->Binding
;
1045 case GL_NUM_ACTIVE_VARIABLES
:
1046 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1048 case GL_ACTIVE_VARIABLES
:
1049 struct gl_transform_feedback_info
*linked_xfb
=
1050 shProg
->xfb_program
->sh
.LinkedTransformFeedback
;
1051 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1052 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1053 struct gl_program_resource
*buf_res
=
1054 _mesa_program_resource_find_index(shProg
,
1055 GL_TRANSFORM_FEEDBACK_BUFFER
,
1058 if (res
== buf_res
) {
1062 return RESOURCE_XFB(res
)->NumVaryings
;
1065 assert(!"support for property type not implemented");
1068 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1069 _mesa_enum_to_string(res
->Type
),
1070 _mesa_enum_to_string(prop
));
1076 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1077 struct gl_program_resource
*res
, GLuint index
,
1078 const GLenum prop
, GLint
*val
, const char *caller
)
1080 GET_CURRENT_CONTEXT(ctx
);
1082 #define VALIDATE_TYPE(type)\
1083 if (res->Type != type)\
1084 goto invalid_operation;
1086 #define VALIDATE_TYPE_2(type1, type2)\
1087 if (res->Type != type1 && res->Type != type2)\
1088 goto invalid_operation;
1091 case GL_NAME_LENGTH
:
1092 switch (res
->Type
) {
1093 case GL_ATOMIC_COUNTER_BUFFER
:
1094 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1095 goto invalid_operation
;
1097 /* Resource name length + terminator. */
1098 *val
= _mesa_program_resource_name_len(res
) + 1;
1102 switch (res
->Type
) {
1104 case GL_BUFFER_VARIABLE
:
1105 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1107 case GL_PROGRAM_INPUT
:
1108 case GL_PROGRAM_OUTPUT
:
1109 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1111 case GL_TRANSFORM_FEEDBACK_VARYING
:
1112 *val
= RESOURCE_XFV(res
)->Type
;
1115 goto invalid_operation
;
1118 switch (res
->Type
) {
1120 case GL_BUFFER_VARIABLE
:
1121 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1122 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1123 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1124 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1125 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1126 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1128 /* Test if a buffer variable is an array or an unsized array.
1129 * Unsized arrays return zero as array size.
1131 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1132 RESOURCE_UNI(res
)->array_stride
> 0)
1133 *val
= RESOURCE_UNI(res
)->array_elements
;
1135 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1137 case GL_PROGRAM_INPUT
:
1138 case GL_PROGRAM_OUTPUT
:
1139 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1141 case GL_TRANSFORM_FEEDBACK_VARYING
:
1142 *val
= RESOURCE_XFV(res
)->Size
;
1145 goto invalid_operation
;
1148 switch (res
->Type
) {
1150 case GL_BUFFER_VARIABLE
:
1151 *val
= RESOURCE_UNI(res
)->offset
;
1153 case GL_TRANSFORM_FEEDBACK_VARYING
:
1154 *val
= RESOURCE_XFV(res
)->Offset
;
1157 goto invalid_operation
;
1159 case GL_BLOCK_INDEX
:
1160 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1161 *val
= RESOURCE_UNI(res
)->block_index
;
1163 case GL_ARRAY_STRIDE
:
1164 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1165 *val
= RESOURCE_UNI(res
)->array_stride
;
1167 case GL_MATRIX_STRIDE
:
1168 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1169 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1171 case GL_IS_ROW_MAJOR
:
1172 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1173 *val
= RESOURCE_UNI(res
)->row_major
;
1175 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1176 VALIDATE_TYPE(GL_UNIFORM
);
1177 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1179 case GL_BUFFER_BINDING
:
1180 case GL_BUFFER_DATA_SIZE
:
1181 case GL_NUM_ACTIVE_VARIABLES
:
1182 case GL_ACTIVE_VARIABLES
:
1183 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1184 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1185 if (!_mesa_has_compute_shaders(ctx
))
1188 case GL_REFERENCED_BY_VERTEX_SHADER
:
1189 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1190 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1191 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1192 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1193 switch (res
->Type
) {
1195 case GL_PROGRAM_INPUT
:
1196 case GL_PROGRAM_OUTPUT
:
1197 case GL_UNIFORM_BLOCK
:
1198 case GL_BUFFER_VARIABLE
:
1199 case GL_SHADER_STORAGE_BLOCK
:
1200 case GL_ATOMIC_COUNTER_BUFFER
:
1201 *val
= is_resource_referenced(shProg
, res
, index
,
1202 stage_from_enum(prop
));
1205 goto invalid_operation
;
1208 switch (res
->Type
) {
1210 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1211 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1212 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1213 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1214 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1215 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1216 case GL_PROGRAM_INPUT
:
1217 case GL_PROGRAM_OUTPUT
:
1218 *val
= program_resource_location(res
, 0);
1221 goto invalid_operation
;
1223 case GL_LOCATION_COMPONENT
:
1224 switch (res
->Type
) {
1225 case GL_PROGRAM_INPUT
:
1226 case GL_PROGRAM_OUTPUT
:
1227 *val
= RESOURCE_VAR(res
)->component
;
1230 goto invalid_operation
;
1232 case GL_LOCATION_INDEX
: {
1234 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1235 goto invalid_operation
;
1236 tmp
= program_resource_location(res
, 0);
1240 *val
= _mesa_program_resource_location_index(shProg
, res
->Type
,
1241 RESOURCE_VAR(res
)->name
);
1244 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1245 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1246 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1247 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1248 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1249 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1250 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1251 goto invalid_operation
;
1252 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1254 case GL_COMPATIBLE_SUBROUTINES
: {
1255 const struct gl_uniform_storage
*uni
;
1256 struct gl_program
*p
;
1260 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1261 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1262 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1263 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1264 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1265 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1266 goto invalid_operation
;
1267 uni
= RESOURCE_UNI(res
);
1269 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1271 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1272 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1273 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1274 if (fn
->types
[j
] == uni
->type
) {
1283 case GL_TOP_LEVEL_ARRAY_SIZE
:
1284 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1285 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1288 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1289 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1290 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1293 /* GL_ARB_tessellation_shader */
1294 case GL_IS_PER_PATCH
:
1295 switch (res
->Type
) {
1296 case GL_PROGRAM_INPUT
:
1297 case GL_PROGRAM_OUTPUT
:
1298 *val
= RESOURCE_VAR(res
)->patch
;
1301 goto invalid_operation
;
1304 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1305 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1306 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1308 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1309 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1310 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1317 #undef VALIDATE_TYPE
1318 #undef VALIDATE_TYPE_2
1321 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1322 _mesa_enum_to_string(res
->Type
),
1323 _mesa_enum_to_string(prop
));
1327 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1328 _mesa_enum_to_string(res
->Type
),
1329 _mesa_enum_to_string(prop
));
1334 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1335 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1336 const GLenum
*props
, GLsizei bufSize
,
1337 GLsizei
*length
, GLint
*params
)
1339 GET_CURRENT_CONTEXT(ctx
);
1340 GLint
*val
= (GLint
*) params
;
1341 const GLenum
*prop
= props
;
1344 struct gl_program_resource
*res
=
1345 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1347 /* No such resource found or bufSize negative. */
1348 if (!res
|| bufSize
< 0) {
1349 _mesa_error(ctx
, GL_INVALID_VALUE
,
1350 "glGetProgramResourceiv(%s index %d bufSize %d)",
1351 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1355 /* Write propCount values until error occurs or bufSize reached. */
1356 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1358 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1359 "glGetProgramResourceiv");
1361 /* Error happened. */
1362 if (props_written
== 0)
1365 amount
+= props_written
;
1368 /* If <length> is not NULL, the actual number of integer values
1369 * written to <params> will be written to <length>.
1376 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1378 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1381 const bool nonarray_stage_to_array_stage
=
1382 producer
->info
.stage
!= MESA_SHADER_TESS_CTRL
&&
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 9.2.2 (Separable Programs) of the GLSL ES spec says:
1500 * Qualifier Class| Qualifier |in/out
1501 * ---------------+-------------+------
1505 * ---------------+-------------+------
1506 * Auxiliary | centroid | No
1507 * ---------------+-------------+------
1509 * | Block layout| N/A
1513 * ---------------+-------------+------
1514 * Interpolation | smooth |
1516 * ---------------+-------------+------
1518 * Precision | mediump | Yes
1520 * ---------------+-------------+------
1521 * Variance | invariant | No
1522 * ---------------+-------------+------
1523 * Memory | all | N/A
1525 * Note that location mismatches are detected by the loops above that
1526 * find the producer variable that goes with the consumer variable.
1528 if (nonarray_stage_to_array_stage
) {
1529 if (consumer_var
->interface_type
!= NULL
) {
1530 /* the interface is the array; underlying types should match */
1531 if (producer_var
->type
!= consumer_var
->type
) {
1536 if (!consumer_var
->interface_type
->is_array() ||
1537 consumer_var
->interface_type
->fields
.array
!= producer_var
->interface_type
) {
1542 if (producer_var
->interface_type
!= NULL
) {
1547 if (!consumer_var
->type
->is_array() ||
1548 consumer_var
->type
->fields
.array
!= producer_var
->type
) {
1554 if (producer_var
->type
!= consumer_var
->type
) {
1559 if (producer_var
->interface_type
!= consumer_var
->interface_type
) {
1565 if (producer_var
->interpolation
!= consumer_var
->interpolation
) {
1570 if (producer_var
->precision
!= consumer_var
->precision
) {
1575 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1583 return valid
&& num_outputs
== 0;
1587 * Validate inputs against outputs in a program pipeline.
1590 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1592 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1594 /* Find first active stage in pipeline. */
1595 unsigned idx
, prev
= 0;
1596 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1603 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1605 /* Pipeline might include both non-compute and a compute program, do
1606 * not attempt to validate varyings between non-compute and compute
1609 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1612 if (!validate_io(prog
[prev
], prog
[idx
]))