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 "program/hash_table.h"
41 #include "util/strndup.h"
45 program_resource_location(struct gl_shader_program
*shProg
,
46 struct gl_program_resource
*res
, const char *name
,
47 unsigned array_index
);
50 * Declare convenience functions to return resource data in a given type.
51 * Warning! this is not type safe so be *very* careful when using these.
53 #define DECL_RESOURCE_FUNC(name, type) \
54 const type * RESOURCE_ ## name (gl_program_resource *res) { \
56 return (type *) res->Data; \
59 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
60 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
61 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
62 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
63 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
64 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
65 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
68 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
71 GET_CURRENT_CONTEXT(ctx
);
73 struct gl_shader_program
*const shProg
=
74 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
81 if (strncmp(name
, "gl_", 3) == 0) {
82 _mesa_error(ctx
, GL_INVALID_OPERATION
,
83 "glBindAttribLocation(illegal name)");
87 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
88 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
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 is_active_attrib(const gl_shader_variable
*var
)
111 case ir_var_shader_in
:
112 return var
->location
!= -1;
114 case ir_var_system_value
:
123 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
124 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
125 GLenum
* type
, GLchar
* name
)
127 GET_CURRENT_CONTEXT(ctx
);
128 struct gl_shader_program
*shProg
;
131 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
135 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
139 if (!shProg
->LinkStatus
) {
140 _mesa_error(ctx
, GL_INVALID_VALUE
,
141 "glGetActiveAttrib(program not linked)");
145 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
146 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
150 struct gl_program_resource
*res
=
151 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
154 /* User asked for index that does not exist. */
156 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
160 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
162 if (!is_active_attrib(var
))
165 const char *var_name
= var
->name
;
167 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
168 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
171 if (var
->mode
== ir_var_system_value
&&
172 var
->location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
173 var_name
= "gl_VertexID";
176 _mesa_copy_string(name
, maxLength
, length
, var_name
);
179 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
180 size
, "glGetActiveAttrib");
183 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
184 (GLint
*) type
, "glGetActiveAttrib");
188 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
190 GET_CURRENT_CONTEXT(ctx
);
191 struct gl_shader_program
*const shProg
=
192 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
198 if (!shProg
->LinkStatus
) {
199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
200 "glGetAttribLocation(program not linked)");
207 /* Not having a vertex shader is not an error.
209 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
212 unsigned array_index
= 0;
213 struct gl_program_resource
*res
=
214 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
220 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
222 /* The extra check against against 0 is made because of builtin-attribute
223 * locations that have offset applied. Function program_resource_location
224 * can return built-in attribute locations < 0 and glGetAttribLocation
225 * cannot be used on "conventional" attributes.
227 * From page 95 of the OpenGL 3.0 spec:
229 * "If name is not an active attribute, if name is a conventional
230 * attribute, or if an error occurs, -1 will be returned."
232 return (loc
>= 0) ? loc
: -1;
236 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
238 if (!shProg
->LinkStatus
239 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
243 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
245 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
246 if (res
->Type
== GL_PROGRAM_INPUT
&&
247 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
) &&
248 is_active_attrib(RESOURCE_VAR(res
)))
256 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
258 if (!shProg
->LinkStatus
259 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
263 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
265 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
266 if (res
->Type
== GL_PROGRAM_INPUT
&&
267 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
269 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
270 if (length
>= longest
)
271 longest
= length
+ 1;
279 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
282 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
286 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
287 GLuint index
, const GLchar
*name
)
289 GET_CURRENT_CONTEXT(ctx
);
291 struct gl_shader_program
*const shProg
=
292 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
299 if (strncmp(name
, "gl_", 3) == 0) {
300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
305 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
309 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
314 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
315 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
319 /* Replace the current value if it's already in the list. Add
320 * FRAG_RESULT_DATA0 because that's how the linker differentiates
321 * between built-in attributes and user-defined attributes.
323 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
324 shProg
->FragDataIndexBindings
->put(index
, name
);
326 * Note that this binding won't go into effect until
327 * glLinkProgram is called again.
333 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
335 GET_CURRENT_CONTEXT(ctx
);
336 struct gl_shader_program
*const shProg
=
337 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
343 if (!shProg
->LinkStatus
) {
344 _mesa_error(ctx
, GL_INVALID_OPERATION
,
345 "glGetFragDataIndex(program not linked)");
352 if (strncmp(name
, "gl_", 3) == 0) {
353 _mesa_error(ctx
, GL_INVALID_OPERATION
,
354 "glGetFragDataIndex(illegal name)");
358 /* Not having a fragment shader is not an error.
360 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
363 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
368 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
370 GET_CURRENT_CONTEXT(ctx
);
371 struct gl_shader_program
*const shProg
=
372 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
378 if (!shProg
->LinkStatus
) {
379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
380 "glGetFragDataLocation(program not linked)");
387 if (strncmp(name
, "gl_", 3) == 0) {
388 _mesa_error(ctx
, GL_INVALID_OPERATION
,
389 "glGetFragDataLocation(illegal name)");
393 /* Not having a fragment shader is not an error.
395 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
398 unsigned array_index
= 0;
399 struct gl_program_resource
*res
=
400 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
406 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
408 /* The extra check against against 0 is made because of builtin-attribute
409 * locations that have offset applied. Function program_resource_location
410 * can return built-in attribute locations < 0 and glGetFragDataLocation
411 * cannot be used on "conventional" attributes.
413 * From page 95 of the OpenGL 3.0 spec:
415 * "If name is not an active attribute, if name is a conventional
416 * attribute, or if an error occurs, -1 will be returned."
418 return (loc
>= 0) ? loc
: -1;
422 _mesa_program_resource_name(struct gl_program_resource
*res
)
424 const gl_shader_variable
*var
;
426 case GL_UNIFORM_BLOCK
:
427 case GL_SHADER_STORAGE_BLOCK
:
428 return RESOURCE_UBO(res
)->Name
;
429 case GL_TRANSFORM_FEEDBACK_VARYING
:
430 return RESOURCE_XFV(res
)->Name
;
431 case GL_PROGRAM_INPUT
:
432 var
= RESOURCE_VAR(res
);
433 /* Special case gl_VertexIDMESA -> gl_VertexID. */
434 if (var
->mode
== ir_var_system_value
&&
435 var
->location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
436 return "gl_VertexID";
439 case GL_PROGRAM_OUTPUT
:
440 return RESOURCE_VAR(res
)->name
;
442 case GL_BUFFER_VARIABLE
:
443 return RESOURCE_UNI(res
)->name
;
444 case GL_VERTEX_SUBROUTINE_UNIFORM
:
445 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
446 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
447 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
448 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
449 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
450 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
451 case GL_VERTEX_SUBROUTINE
:
452 case GL_GEOMETRY_SUBROUTINE
:
453 case GL_FRAGMENT_SUBROUTINE
:
454 case GL_COMPUTE_SUBROUTINE
:
455 case GL_TESS_CONTROL_SUBROUTINE
:
456 case GL_TESS_EVALUATION_SUBROUTINE
:
457 return RESOURCE_SUB(res
)->name
;
459 assert(!"support for resource type not implemented");
466 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
469 case GL_TRANSFORM_FEEDBACK_VARYING
:
470 return RESOURCE_XFV(res
)->Size
> 1 ?
471 RESOURCE_XFV(res
)->Size
: 0;
472 case GL_PROGRAM_INPUT
:
473 case GL_PROGRAM_OUTPUT
:
474 return RESOURCE_VAR(res
)->type
->length
;
476 case GL_VERTEX_SUBROUTINE_UNIFORM
:
477 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
478 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
479 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
480 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
481 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
482 return RESOURCE_UNI(res
)->array_elements
;
483 case GL_BUFFER_VARIABLE
:
485 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
486 RESOURCE_UNI(res
)->array_elements
== 0)
489 return RESOURCE_UNI(res
)->array_elements
;
490 case GL_VERTEX_SUBROUTINE
:
491 case GL_GEOMETRY_SUBROUTINE
:
492 case GL_FRAGMENT_SUBROUTINE
:
493 case GL_COMPUTE_SUBROUTINE
:
494 case GL_TESS_CONTROL_SUBROUTINE
:
495 case GL_TESS_EVALUATION_SUBROUTINE
:
496 case GL_ATOMIC_COUNTER_BUFFER
:
497 case GL_UNIFORM_BLOCK
:
498 case GL_SHADER_STORAGE_BLOCK
:
501 assert(!"support for resource type not implemented");
507 * Checks if array subscript is valid and if so sets array_index.
510 valid_array_index(const GLchar
*name
, unsigned *array_index
)
513 const GLchar
*out_base_name_end
;
515 idx
= parse_program_resource_name(name
, &out_base_name_end
);
525 /* Find a program resource with specific name in given interface.
527 struct gl_program_resource
*
528 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
529 GLenum programInterface
, const char *name
,
530 unsigned *array_index
)
532 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
533 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
534 if (res
->Type
!= programInterface
)
537 /* Resource basename. */
538 const char *rname
= _mesa_program_resource_name(res
);
539 unsigned baselen
= strlen(rname
);
540 unsigned baselen_without_array_index
= baselen
;
541 const char *rname_last_square_bracket
= strrchr(rname
, '[');
543 bool rname_has_array_index_zero
= false;
544 /* From ARB_program_interface_query spec:
546 * "uint GetProgramResourceIndex(uint program, enum programInterface,
549 * If <name> exactly matches the name string of one of the active
550 * resources for <programInterface>, the index of the matched resource is
551 * returned. Additionally, if <name> would exactly match the name string
552 * of an active resource if "[0]" were appended to <name>, the index of
553 * the matched resource is returned. [...]"
555 * "A string provided to GetProgramResourceLocation or
556 * GetProgramResourceLocationIndex is considered to match an active variable
559 * * the string exactly matches the name of the active variable;
561 * * if the string identifies the base name of an active array, where the
562 * string would exactly match the name of the variable if the suffix
563 * "[0]" were appended to the string; [...]"
565 /* Remove array's index from interface block name comparison only if
566 * array's index is zero and the resulting string length is the same
567 * than the provided name's length.
569 if (rname_last_square_bracket
) {
570 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
571 rname_has_array_index_zero
=
572 (strncmp(rname_last_square_bracket
, "[0]\0", 4) == 0) &&
573 (baselen_without_array_index
== strlen(name
));
576 if (strncmp(rname
, name
, baselen
) == 0)
578 else if (rname_has_array_index_zero
&&
579 strncmp(rname
, name
, baselen_without_array_index
) == 0)
583 switch (programInterface
) {
584 case GL_UNIFORM_BLOCK
:
585 case GL_SHADER_STORAGE_BLOCK
:
586 /* Basename match, check if array or struct. */
587 if (rname_has_array_index_zero
||
588 name
[baselen
] == '\0' ||
589 name
[baselen
] == '[' ||
590 name
[baselen
] == '.') {
594 case GL_TRANSFORM_FEEDBACK_VARYING
:
595 case GL_BUFFER_VARIABLE
:
597 case GL_VERTEX_SUBROUTINE_UNIFORM
:
598 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
599 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
600 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
601 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
602 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
603 case GL_VERTEX_SUBROUTINE
:
604 case GL_GEOMETRY_SUBROUTINE
:
605 case GL_FRAGMENT_SUBROUTINE
:
606 case GL_COMPUTE_SUBROUTINE
:
607 case GL_TESS_CONTROL_SUBROUTINE
:
608 case GL_TESS_EVALUATION_SUBROUTINE
:
609 if (name
[baselen
] == '.') {
613 case GL_PROGRAM_INPUT
:
614 case GL_PROGRAM_OUTPUT
:
615 if (name
[baselen
] == '\0') {
617 } else if (name
[baselen
] == '[' &&
618 valid_array_index(name
, array_index
)) {
623 assert(!"not implemented for given interface");
631 calc_resource_index(struct gl_shader_program
*shProg
,
632 struct gl_program_resource
*res
)
636 for (i
= 0; i
< shProg
->NumProgramResourceList
; i
++) {
637 if (&shProg
->ProgramResourceList
[i
] == res
)
639 if (shProg
->ProgramResourceList
[i
].Type
== res
->Type
)
642 return GL_INVALID_INDEX
;
646 * Calculate index for the given resource.
649 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
650 struct gl_program_resource
*res
)
653 return GL_INVALID_INDEX
;
656 case GL_ATOMIC_COUNTER_BUFFER
:
657 return RESOURCE_ATC(res
) - shProg
->AtomicBuffers
;
658 case GL_VERTEX_SUBROUTINE
:
659 case GL_GEOMETRY_SUBROUTINE
:
660 case GL_FRAGMENT_SUBROUTINE
:
661 case GL_COMPUTE_SUBROUTINE
:
662 case GL_TESS_CONTROL_SUBROUTINE
:
663 case GL_TESS_EVALUATION_SUBROUTINE
:
664 return RESOURCE_SUB(res
)->index
;
665 case GL_UNIFORM_BLOCK
:
666 case GL_SHADER_STORAGE_BLOCK
:
667 case GL_TRANSFORM_FEEDBACK_BUFFER
:
668 case GL_TRANSFORM_FEEDBACK_VARYING
:
670 return calc_resource_index(shProg
, res
);
675 * Find a program resource that points to given data.
677 static struct gl_program_resource
*
678 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
680 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
681 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
682 if (res
->Data
== data
)
688 /* Find a program resource with specific index in given interface.
690 struct gl_program_resource
*
691 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
692 GLenum programInterface
, GLuint index
)
694 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
697 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
698 if (res
->Type
!= programInterface
)
702 case GL_UNIFORM_BLOCK
:
703 case GL_ATOMIC_COUNTER_BUFFER
:
704 case GL_SHADER_STORAGE_BLOCK
:
705 case GL_TRANSFORM_FEEDBACK_BUFFER
:
706 if (_mesa_program_resource_index(shProg
, res
) == index
)
709 case GL_TRANSFORM_FEEDBACK_VARYING
:
710 case GL_PROGRAM_INPUT
:
711 case GL_PROGRAM_OUTPUT
:
713 case GL_VERTEX_SUBROUTINE_UNIFORM
:
714 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
715 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
716 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
717 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
718 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
719 case GL_VERTEX_SUBROUTINE
:
720 case GL_GEOMETRY_SUBROUTINE
:
721 case GL_FRAGMENT_SUBROUTINE
:
722 case GL_COMPUTE_SUBROUTINE
:
723 case GL_TESS_CONTROL_SUBROUTINE
:
724 case GL_TESS_EVALUATION_SUBROUTINE
:
725 case GL_BUFFER_VARIABLE
:
726 if (++idx
== (int) index
)
730 assert(!"not implemented for given interface");
736 /* Function returns if resource name is expected to have index
740 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
743 * "If the active uniform is an array, the uniform name returned in
744 * name will always be the name of the uniform array appended with
747 * The same text also appears in the OpenGL 4.2 spec. It does not,
748 * however, appear in any previous spec. Previous specifications are
749 * ambiguous in this regard. However, either name can later be passed
750 * to glGetUniformLocation (and related APIs), so there shouldn't be any
751 * harm in always appending "[0]" to uniform array names.
753 * Geometry shader stage has different naming convention where the 'normal'
754 * condition is an array, therefore for variables referenced in geometry
755 * stage we do not add '[0]'.
757 * Note, that TCS outputs and TES inputs should not have index appended
761 add_index_to_name(struct gl_program_resource
*res
)
763 bool add_index
= !((res
->Type
== GL_PROGRAM_INPUT
&&
764 res
->StageReferences
& (1 << MESA_SHADER_GEOMETRY
|
765 1 << MESA_SHADER_TESS_CTRL
|
766 1 << MESA_SHADER_TESS_EVAL
)) ||
767 (res
->Type
== GL_PROGRAM_OUTPUT
&&
768 res
->StageReferences
& 1 << MESA_SHADER_TESS_CTRL
));
770 /* Transform feedback varyings have array index already appended
773 if (res
->Type
== GL_TRANSFORM_FEEDBACK_VARYING
)
779 /* Get name length of a program resource. This consists of
780 * base name + 3 for '[0]' if resource is an array.
783 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
785 unsigned length
= strlen(_mesa_program_resource_name(res
));
786 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
791 /* Get full name of a program resource.
794 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
795 GLenum programInterface
, GLuint index
,
796 GLsizei bufSize
, GLsizei
*length
,
797 GLchar
*name
, const char *caller
)
799 GET_CURRENT_CONTEXT(ctx
);
801 /* Find resource with given interface and index. */
802 struct gl_program_resource
*res
=
803 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
805 /* The error INVALID_VALUE is generated if <index> is greater than
806 * or equal to the number of entries in the active resource list for
807 * <programInterface>.
810 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
815 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
822 length
= &localLength
;
824 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
826 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
829 /* The comparison is strange because *length does *NOT* include the
830 * terminating NUL, but maxLength does.
832 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
833 name
[*length
+ i
] = "[0]"[i
];
835 name
[*length
+ i
] = '\0';
842 program_resource_location(struct gl_shader_program
*shProg
,
843 struct gl_program_resource
*res
, const char *name
,
844 unsigned array_index
)
846 /* Built-in locations should report GL_INVALID_INDEX. */
847 if (is_gl_identifier(name
))
848 return GL_INVALID_INDEX
;
850 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
851 * offsets are used internally to differentiate between built-in attributes
852 * and user-defined attributes.
855 case GL_PROGRAM_INPUT
: {
856 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
858 /* If the input is an array, fail if the index is out of bounds. */
860 && array_index
>= var
->type
->length
) {
863 return (var
->location
+
864 (array_index
* var
->type
->without_array()->matrix_columns
) -
865 VERT_ATTRIB_GENERIC0
);
867 case GL_PROGRAM_OUTPUT
:
868 /* If the output is an array, fail if the index is out of bounds. */
870 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
873 return RESOURCE_VAR(res
)->location
+ array_index
- FRAG_RESULT_DATA0
;
875 /* If the uniform is built-in, fail. */
876 if (RESOURCE_UNI(res
)->builtin
)
879 /* From page 79 of the OpenGL 4.2 spec:
881 * "A valid name cannot be a structure, an array of structures, or any
882 * portion of a single vector or a matrix."
884 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
887 /* From the GL_ARB_uniform_buffer_object spec:
889 * "The value -1 will be returned if <name> does not correspond to an
890 * active uniform variable name in <program>, if <name> is associated
891 * with a named uniform block, or if <name> starts with the reserved
894 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
895 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
899 case GL_VERTEX_SUBROUTINE_UNIFORM
:
900 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
901 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
902 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
903 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
904 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
905 /* If the uniform is an array, fail if the index is out of bounds. */
907 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
911 /* location in remap table + array element offset */
912 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
919 * Function implements following location queries:
920 * glGetUniformLocation
923 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
924 GLenum programInterface
, const char *name
)
926 unsigned array_index
= 0;
927 struct gl_program_resource
*res
=
928 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
931 /* Resource not found. */
935 return program_resource_location(shProg
, res
, name
, array_index
);
939 * Function implements following index queries:
943 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
944 GLenum programInterface
, const char *name
)
946 struct gl_program_resource
*res
=
947 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
949 /* Non-existent variable or resource is not referenced by fragment stage. */
950 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
953 return RESOURCE_VAR(res
)->index
;
957 stage_from_enum(GLenum ref
)
960 case GL_REFERENCED_BY_VERTEX_SHADER
:
961 return MESA_SHADER_VERTEX
;
962 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
963 return MESA_SHADER_TESS_CTRL
;
964 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
965 return MESA_SHADER_TESS_EVAL
;
966 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
967 return MESA_SHADER_GEOMETRY
;
968 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
969 return MESA_SHADER_FRAGMENT
;
970 case GL_REFERENCED_BY_COMPUTE_SHADER
:
971 return MESA_SHADER_COMPUTE
;
973 assert(!"shader stage not supported");
974 return MESA_SHADER_STAGES
;
979 * Check if resource is referenced by given 'referenced by' stage enum.
980 * ATC and UBO resources hold stage references of their own.
983 is_resource_referenced(struct gl_shader_program
*shProg
,
984 struct gl_program_resource
*res
,
985 GLuint index
, uint8_t stage
)
987 /* First, check if we even have such a stage active. */
988 if (!shProg
->_LinkedShaders
[stage
])
991 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
992 return RESOURCE_ATC(res
)->StageReferences
[stage
];
994 if (res
->Type
== GL_UNIFORM_BLOCK
|| res
->Type
== GL_SHADER_STORAGE_BLOCK
)
995 return shProg
->InterfaceBlockStageIndex
[stage
][index
] != -1;
997 return res
->StageReferences
& (1 << stage
);
1001 get_buffer_property(struct gl_shader_program
*shProg
,
1002 struct gl_program_resource
*res
, const GLenum prop
,
1003 GLint
*val
, const char *caller
)
1005 GET_CURRENT_CONTEXT(ctx
);
1006 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1007 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1008 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1009 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1010 goto invalid_operation
;
1012 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1014 case GL_BUFFER_BINDING
:
1015 *val
= RESOURCE_UBO(res
)->Binding
;
1017 case GL_BUFFER_DATA_SIZE
:
1018 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1020 case GL_NUM_ACTIVE_VARIABLES
:
1022 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1023 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1024 struct gl_program_resource
*uni
=
1025 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1032 case GL_ACTIVE_VARIABLES
: {
1033 unsigned num_values
= 0;
1034 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1035 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1036 struct gl_program_resource
*uni
=
1037 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1042 _mesa_program_resource_index(shProg
, uni
);
1048 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1050 case GL_BUFFER_BINDING
:
1051 *val
= RESOURCE_UBO(res
)->Binding
;
1053 case GL_BUFFER_DATA_SIZE
:
1054 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1056 case GL_NUM_ACTIVE_VARIABLES
:
1058 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1059 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1060 struct gl_program_resource
*uni
=
1061 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1068 case GL_ACTIVE_VARIABLES
: {
1069 unsigned num_values
= 0;
1070 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1071 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1072 struct gl_program_resource
*uni
=
1073 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1078 _mesa_program_resource_index(shProg
, uni
);
1084 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1086 case GL_BUFFER_BINDING
:
1087 *val
= RESOURCE_ATC(res
)->Binding
;
1089 case GL_BUFFER_DATA_SIZE
:
1090 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1092 case GL_NUM_ACTIVE_VARIABLES
:
1093 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1095 case GL_ACTIVE_VARIABLES
:
1096 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1097 /* Active atomic buffer contains index to UniformStorage. Find
1098 * out gl_program_resource via data pointer and then calculate
1099 * index of that uniform.
1101 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1102 struct gl_program_resource
*uni
=
1103 program_resource_find_data(shProg
,
1104 &shProg
->UniformStorage
[idx
]);
1106 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1108 return RESOURCE_ATC(res
)->NumUniforms
;
1110 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1112 case GL_BUFFER_BINDING
:
1113 *val
= RESOURCE_XFB(res
)->Binding
;
1115 case GL_NUM_ACTIVE_VARIABLES
:
1116 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1118 case GL_ACTIVE_VARIABLES
:
1120 for ( ; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
1122 shProg
->LinkedTransformFeedback
.Varyings
[i
].BufferIndex
;
1123 struct gl_program_resource
*buf_res
=
1124 _mesa_program_resource_find_index(shProg
,
1125 GL_TRANSFORM_FEEDBACK_BUFFER
,
1128 if (res
== buf_res
) {
1132 return RESOURCE_XFB(res
)->NumVaryings
;
1135 assert(!"support for property type not implemented");
1138 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1139 _mesa_enum_to_string(res
->Type
),
1140 _mesa_enum_to_string(prop
));
1146 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1147 struct gl_program_resource
*res
, GLuint index
,
1148 const GLenum prop
, GLint
*val
, const char *caller
)
1150 GET_CURRENT_CONTEXT(ctx
);
1152 #define VALIDATE_TYPE(type)\
1153 if (res->Type != type)\
1154 goto invalid_operation;
1156 #define VALIDATE_TYPE_2(type1, type2)\
1157 if (res->Type != type1 && res->Type != type2)\
1158 goto invalid_operation;
1161 case GL_NAME_LENGTH
:
1162 switch (res
->Type
) {
1163 case GL_ATOMIC_COUNTER_BUFFER
:
1164 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1165 goto invalid_operation
;
1167 /* Resource name length + terminator. */
1168 *val
= _mesa_program_resource_name_len(res
) + 1;
1172 switch (res
->Type
) {
1174 case GL_BUFFER_VARIABLE
:
1175 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1177 case GL_PROGRAM_INPUT
:
1178 case GL_PROGRAM_OUTPUT
:
1179 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1181 case GL_TRANSFORM_FEEDBACK_VARYING
:
1182 *val
= RESOURCE_XFV(res
)->Type
;
1185 goto invalid_operation
;
1188 switch (res
->Type
) {
1190 case GL_BUFFER_VARIABLE
:
1191 /* Test if a buffer variable is an array or an unsized array.
1192 * Unsized arrays return zero as array size.
1194 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1195 RESOURCE_UNI(res
)->array_stride
> 0)
1196 *val
= RESOURCE_UNI(res
)->array_elements
;
1198 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1200 case GL_PROGRAM_INPUT
:
1201 case GL_PROGRAM_OUTPUT
:
1202 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1204 case GL_TRANSFORM_FEEDBACK_VARYING
:
1205 *val
= MAX2(RESOURCE_XFV(res
)->Size
, 1);
1208 goto invalid_operation
;
1211 switch (res
->Type
) {
1213 case GL_BUFFER_VARIABLE
:
1214 *val
= RESOURCE_UNI(res
)->offset
;
1216 case GL_TRANSFORM_FEEDBACK_VARYING
:
1217 *val
= RESOURCE_XFV(res
)->Offset
;
1220 goto invalid_operation
;
1222 case GL_BLOCK_INDEX
:
1223 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1224 *val
= RESOURCE_UNI(res
)->block_index
;
1226 case GL_ARRAY_STRIDE
:
1227 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1228 *val
= RESOURCE_UNI(res
)->array_stride
;
1230 case GL_MATRIX_STRIDE
:
1231 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1232 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1234 case GL_IS_ROW_MAJOR
:
1235 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1236 *val
= RESOURCE_UNI(res
)->row_major
;
1238 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1239 VALIDATE_TYPE(GL_UNIFORM
);
1240 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1242 case GL_BUFFER_BINDING
:
1243 case GL_BUFFER_DATA_SIZE
:
1244 case GL_NUM_ACTIVE_VARIABLES
:
1245 case GL_ACTIVE_VARIABLES
:
1246 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1247 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1248 if (!_mesa_has_compute_shaders(ctx
))
1251 case GL_REFERENCED_BY_VERTEX_SHADER
:
1252 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1253 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1254 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1255 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1256 switch (res
->Type
) {
1258 case GL_PROGRAM_INPUT
:
1259 case GL_PROGRAM_OUTPUT
:
1260 case GL_UNIFORM_BLOCK
:
1261 case GL_BUFFER_VARIABLE
:
1262 case GL_SHADER_STORAGE_BLOCK
:
1263 case GL_ATOMIC_COUNTER_BUFFER
:
1264 *val
= is_resource_referenced(shProg
, res
, index
,
1265 stage_from_enum(prop
));
1268 goto invalid_operation
;
1271 switch (res
->Type
) {
1273 case GL_PROGRAM_INPUT
:
1274 case GL_PROGRAM_OUTPUT
:
1275 *val
= program_resource_location(shProg
, res
,
1276 _mesa_program_resource_name(res
),
1280 goto invalid_operation
;
1282 case GL_LOCATION_INDEX
:
1283 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1284 goto invalid_operation
;
1285 *val
= RESOURCE_VAR(res
)->index
;
1288 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1289 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1290 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1291 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1292 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1293 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1294 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1295 goto invalid_operation
;
1296 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1298 case GL_COMPATIBLE_SUBROUTINES
: {
1299 const struct gl_uniform_storage
*uni
;
1300 struct gl_shader
*sh
;
1304 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1305 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1306 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1307 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1308 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1309 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1310 goto invalid_operation
;
1311 uni
= RESOURCE_UNI(res
);
1313 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1315 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1316 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1317 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1318 if (fn
->types
[j
] == uni
->type
) {
1327 case GL_TOP_LEVEL_ARRAY_SIZE
:
1328 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1329 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1332 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1333 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1334 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1337 /* GL_ARB_tessellation_shader */
1338 case GL_IS_PER_PATCH
:
1339 switch (res
->Type
) {
1340 case GL_PROGRAM_INPUT
:
1341 case GL_PROGRAM_OUTPUT
:
1342 *val
= RESOURCE_VAR(res
)->patch
;
1345 goto invalid_operation
;
1348 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1349 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1350 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1352 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1353 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1354 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1361 #undef VALIDATE_TYPE
1362 #undef VALIDATE_TYPE_2
1365 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1366 _mesa_enum_to_string(res
->Type
),
1367 _mesa_enum_to_string(prop
));
1371 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1372 _mesa_enum_to_string(res
->Type
),
1373 _mesa_enum_to_string(prop
));
1378 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1379 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1380 const GLenum
*props
, GLsizei bufSize
,
1381 GLsizei
*length
, GLint
*params
)
1383 GET_CURRENT_CONTEXT(ctx
);
1384 GLint
*val
= (GLint
*) params
;
1385 const GLenum
*prop
= props
;
1388 struct gl_program_resource
*res
=
1389 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1391 /* No such resource found or bufSize negative. */
1392 if (!res
|| bufSize
< 0) {
1393 _mesa_error(ctx
, GL_INVALID_VALUE
,
1394 "glGetProgramResourceiv(%s index %d bufSize %d)",
1395 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1399 /* Write propCount values until error occurs or bufSize reached. */
1400 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1402 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1403 "glGetProgramResourceiv");
1405 /* Error happened. */
1406 if (props_written
== 0)
1409 amount
+= props_written
;
1412 /* If <length> is not NULL, the actual number of integer values
1413 * written to <params> will be written to <length>.
1420 validate_io(const struct gl_shader
*producer
,
1421 const struct gl_shader
*consumer
, bool isES
)
1423 assert(producer
&& consumer
);
1424 unsigned inputs
= 0, outputs
= 0;
1426 /* From OpenGL ES 3.1 spec (Interface matching):
1428 * "An output variable is considered to match an input variable in the
1429 * subsequent shader if:
1431 * - the two variables match in name, type, and qualification; or
1432 * - the two variables are declared with the same location qualifier and
1433 * match in type and qualification.
1437 * At an interface between program objects, the set of inputs and outputs
1438 * are considered to match exactly if and only if:
1440 * - Every declared input variable has a matching output, as described
1443 * - There are no user-defined output variables declared without a
1444 * matching input variable declaration.
1446 * - All matched input and output variables have identical precision
1449 * When the set of inputs and outputs on an interface between programs
1450 * matches exactly, all inputs are well-defined except when the
1451 * corresponding outputs were not written in the previous shader. However,
1452 * any mismatch between inputs and outputs will result in a validation
1455 * OpenGL Core 4.5 spec includes same paragraph as above but without check
1456 * for precision and the last 'validation failure' clause. Therefore
1457 * behaviour is more relaxed, input and output amount is not required by the
1458 * spec to be validated.
1460 * FIXME: Update once Khronos spec bug #15331 is resolved.
1461 * FIXME: Add validation by type, currently information loss during varying
1462 * packing makes this challenging.
1465 /* Currently no matching done for desktop. */
1469 /* For each output in a, find input in b and do any required checks. */
1470 foreach_in_list(ir_instruction
, out
, producer
->ir
) {
1471 ir_variable
*out_var
= out
->as_variable();
1472 if (!out_var
|| out_var
->data
.mode
!= ir_var_shader_out
||
1473 is_gl_identifier(out_var
->name
))
1479 foreach_in_list(ir_instruction
, in
, consumer
->ir
) {
1480 ir_variable
*in_var
= in
->as_variable();
1481 if (!in_var
|| in_var
->data
.mode
!= ir_var_shader_in
||
1482 is_gl_identifier(in_var
->name
))
1487 /* Match by location qualifier and precision.
1489 * FIXME: Add explicit location matching validation here. Be careful
1490 * not to match varyings with explicit locations to varyings without
1491 * explicit locations.
1493 if ((in_var
->data
.explicit_location
&&
1494 out_var
->data
.explicit_location
) &&
1495 in_var
->data
.location
== out_var
->data
.location
&&
1496 in_var
->data
.precision
== out_var
->data
.precision
)
1499 unsigned len
= strlen(in_var
->name
);
1501 /* Handle input swizzle in variable name. */
1502 const char *dot
= strchr(in_var
->name
, '.');
1504 len
= dot
- in_var
->name
;
1506 /* Match by name and precision. */
1507 if (strncmp(in_var
->name
, out_var
->name
, len
) == 0) {
1508 /* From OpenGL ES 3.1 spec:
1509 * "When both shaders are in separate programs, mismatched
1510 * precision qualifiers will result in a program interface
1511 * mismatch that will result in program pipeline validation
1512 * failures, as described in section 7.4.1 (“Shader Interface
1513 * Matching”) of the OpenGL ES 3.1 Specification."
1515 if (in_var
->data
.precision
!= out_var
->data
.precision
)
1520 return inputs
== outputs
;
1524 * Validate inputs against outputs in a program pipeline.
1527 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1529 struct gl_shader_program
**shProg
=
1530 (struct gl_shader_program
**) pipeline
->CurrentProgram
;
1532 /* Find first active stage in pipeline. */
1533 unsigned idx
, prev
= 0;
1534 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1541 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1543 /* Pipeline might include both non-compute and a compute program, do
1544 * not attempt to validate varyings between non-compute and compute
1547 if (shProg
[idx
]->_LinkedShaders
[idx
]->Stage
== MESA_SHADER_COMPUTE
)
1550 if (!validate_io(shProg
[prev
]->_LinkedShaders
[prev
],
1551 shProg
[idx
]->_LinkedShaders
[idx
],
1552 shProg
[prev
]->IsES
|| shProg
[idx
]->IsES
))