2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/context.h"
32 #include "main/enums.h"
33 #include "main/shaderapi.h"
34 #include "main/shaderobj.h"
35 #include "main/uniforms.h"
36 #include "compiler/glsl/glsl_symbol_table.h"
37 #include "compiler/glsl/ir.h"
38 #include "compiler/glsl/program.h"
39 #include "compiler/glsl/string_to_uint_map.h"
43 program_resource_location(struct gl_program_resource
*res
,
44 unsigned array_index
);
47 * Declare convenience functions to return resource data in a given type.
48 * Warning! this is not type safe so be *very* careful when using these.
50 #define DECL_RESOURCE_FUNC(name, type) \
51 const type * RESOURCE_ ## name (gl_program_resource *res) { \
53 return (type *) res->Data; \
56 DECL_RESOURCE_FUNC(VAR
, gl_shader_variable
);
57 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
58 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
59 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
60 DECL_RESOURCE_FUNC(XFV
, gl_transform_feedback_varying_info
);
61 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
62 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
65 bind_attrib_location(struct gl_context
*ctx
,
66 struct gl_shader_program
*const shProg
, GLuint index
,
67 const GLchar
*name
, bool no_error
)
73 if (strncmp(name
, "gl_", 3) == 0) {
74 _mesa_error(ctx
, GL_INVALID_OPERATION
,
75 "glBindAttribLocation(illegal name)");
79 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
80 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(%u >= %u)",
81 index
, ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
);
86 /* Replace the current value if it's already in the list. Add
87 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
88 * between built-in attributes and user-defined attributes.
90 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
93 * Note that this attribute binding won't go into effect until
94 * glLinkProgram is called again.
99 _mesa_BindAttribLocation_no_error(GLuint program
, GLuint index
,
102 GET_CURRENT_CONTEXT(ctx
);
104 struct gl_shader_program
*const shProg
=
105 _mesa_lookup_shader_program(ctx
, program
);
106 bind_attrib_location(ctx
, shProg
, index
, name
, true);
110 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
113 GET_CURRENT_CONTEXT(ctx
);
115 struct gl_shader_program
*const shProg
=
116 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
120 bind_attrib_location(ctx
, shProg
, index
, name
, false);
124 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
125 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
126 GLenum
* type
, GLchar
* name
)
128 GET_CURRENT_CONTEXT(ctx
);
129 struct gl_shader_program
*shProg
;
132 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
136 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
140 if (!shProg
->data
->LinkStatus
) {
141 _mesa_error(ctx
, GL_INVALID_VALUE
,
142 "glGetActiveAttrib(program not linked)");
146 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
147 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
151 struct gl_program_resource
*res
=
152 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
155 /* User asked for index that does not exist. */
157 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
161 const gl_shader_variable
*const var
= RESOURCE_VAR(res
);
163 const char *var_name
= var
->name
;
165 _mesa_copy_string(name
, maxLength
, length
, var_name
);
168 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
169 size
, "glGetActiveAttrib");
172 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
173 (GLint
*) type
, "glGetActiveAttrib");
177 _mesa_GetAttribLocation(GLuint program
, const GLchar
* name
)
179 GET_CURRENT_CONTEXT(ctx
);
180 struct gl_shader_program
*const shProg
=
181 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
187 if (!shProg
->data
->LinkStatus
) {
188 _mesa_error(ctx
, GL_INVALID_OPERATION
,
189 "glGetAttribLocation(program not linked)");
196 /* Not having a vertex shader is not an error.
198 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
201 unsigned array_index
= 0;
202 struct gl_program_resource
*res
=
203 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
209 return program_resource_location(res
, array_index
);
213 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
215 if (!shProg
->data
->LinkStatus
216 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
220 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
222 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
224 if (res
->Type
== GL_PROGRAM_INPUT
&&
225 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
))
233 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
235 if (!shProg
->data
->LinkStatus
236 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
240 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
242 for (unsigned j
= 0; j
< shProg
->data
->NumProgramResourceList
;
244 if (res
->Type
== GL_PROGRAM_INPUT
&&
245 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
247 /* From the ARB_gl_spirv spec:
249 * "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the
250 * longest active attribute name, including a null terminator, is
251 * returned. If no active attributes exist, zero is returned. If
252 * no name reflection information is available, one is returned."
254 const size_t length
= RESOURCE_VAR(res
)->name
!= NULL
?
255 strlen(RESOURCE_VAR(res
)->name
) : 0;
257 if (length
>= longest
)
258 longest
= length
+ 1;
266 bind_frag_data_location(struct gl_shader_program
*const shProg
,
267 const char *name
, unsigned colorNumber
,
270 /* Replace the current value if it's already in the list. Add
271 * FRAG_RESULT_DATA0 because that's how the linker differentiates
272 * between built-in attributes and user-defined attributes.
274 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
275 shProg
->FragDataIndexBindings
->put(index
, name
);
278 * Note that this binding won't go into effect until
279 * glLinkProgram is called again.
284 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
287 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
291 _mesa_BindFragDataLocation_no_error(GLuint program
, GLuint colorNumber
,
294 GET_CURRENT_CONTEXT(ctx
);
299 struct gl_shader_program
*const shProg
=
300 _mesa_lookup_shader_program(ctx
, program
);
302 bind_frag_data_location(shProg
, name
, colorNumber
, 0);
306 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
307 GLuint index
, const GLchar
*name
)
309 GET_CURRENT_CONTEXT(ctx
);
311 struct gl_shader_program
*const shProg
=
312 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
319 if (strncmp(name
, "gl_", 3) == 0) {
320 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
325 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
329 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
330 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
334 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
335 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
339 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
343 _mesa_BindFragDataLocationIndexed_no_error(GLuint program
, GLuint colorNumber
,
344 GLuint index
, const GLchar
*name
)
346 GET_CURRENT_CONTEXT(ctx
);
351 struct gl_shader_program
*const shProg
=
352 _mesa_lookup_shader_program(ctx
, program
);
354 bind_frag_data_location(shProg
, name
, colorNumber
, index
);
358 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
360 GET_CURRENT_CONTEXT(ctx
);
361 struct gl_shader_program
*const shProg
=
362 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
368 if (!shProg
->data
->LinkStatus
) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
,
370 "glGetFragDataIndex(program not linked)");
377 if (strncmp(name
, "gl_", 3) == 0) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
379 "glGetFragDataIndex(illegal name)");
383 /* Not having a fragment shader is not an error.
385 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
388 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
393 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
395 GET_CURRENT_CONTEXT(ctx
);
396 struct gl_shader_program
*const shProg
=
397 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
403 if (!shProg
->data
->LinkStatus
) {
404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
405 "glGetFragDataLocation(program not linked)");
412 if (strncmp(name
, "gl_", 3) == 0) {
413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
414 "glGetFragDataLocation(illegal name)");
418 /* Not having a fragment shader is not an error.
420 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
423 unsigned array_index
= 0;
424 struct gl_program_resource
*res
=
425 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
431 return program_resource_location(res
, array_index
);
435 _mesa_program_resource_name(struct gl_program_resource
*res
)
438 case GL_UNIFORM_BLOCK
:
439 case GL_SHADER_STORAGE_BLOCK
:
440 return RESOURCE_UBO(res
)->Name
;
441 case GL_TRANSFORM_FEEDBACK_VARYING
:
442 return RESOURCE_XFV(res
)->Name
;
443 case GL_PROGRAM_INPUT
:
444 case GL_PROGRAM_OUTPUT
:
445 return RESOURCE_VAR(res
)->name
;
447 case GL_BUFFER_VARIABLE
:
448 return RESOURCE_UNI(res
)->name
;
449 case GL_VERTEX_SUBROUTINE_UNIFORM
:
450 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
451 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
452 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
453 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
454 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
455 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
456 case GL_VERTEX_SUBROUTINE
:
457 case GL_GEOMETRY_SUBROUTINE
:
458 case GL_FRAGMENT_SUBROUTINE
:
459 case GL_COMPUTE_SUBROUTINE
:
460 case GL_TESS_CONTROL_SUBROUTINE
:
461 case GL_TESS_EVALUATION_SUBROUTINE
:
462 return RESOURCE_SUB(res
)->name
;
464 assert(!"support for resource type not implemented");
471 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
474 case GL_TRANSFORM_FEEDBACK_VARYING
:
475 return RESOURCE_XFV(res
)->Size
> 1 ?
476 RESOURCE_XFV(res
)->Size
: 0;
477 case GL_PROGRAM_INPUT
:
478 case GL_PROGRAM_OUTPUT
:
479 return RESOURCE_VAR(res
)->type
->length
;
481 case GL_VERTEX_SUBROUTINE_UNIFORM
:
482 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
483 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
484 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
485 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
486 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
487 return RESOURCE_UNI(res
)->array_elements
;
488 case GL_BUFFER_VARIABLE
:
490 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
491 RESOURCE_UNI(res
)->array_elements
== 0)
494 return RESOURCE_UNI(res
)->array_elements
;
495 case GL_VERTEX_SUBROUTINE
:
496 case GL_GEOMETRY_SUBROUTINE
:
497 case GL_FRAGMENT_SUBROUTINE
:
498 case GL_COMPUTE_SUBROUTINE
:
499 case GL_TESS_CONTROL_SUBROUTINE
:
500 case GL_TESS_EVALUATION_SUBROUTINE
:
501 case GL_ATOMIC_COUNTER_BUFFER
:
502 case GL_UNIFORM_BLOCK
:
503 case GL_SHADER_STORAGE_BLOCK
:
506 assert(!"support for resource type not implemented");
512 * Checks if array subscript is valid and if so sets array_index.
515 valid_array_index(const GLchar
*name
, unsigned *array_index
)
518 const GLchar
*out_base_name_end
;
520 idx
= parse_program_resource_name(name
, &out_base_name_end
);
530 /* Find a program resource with specific name in given interface.
532 struct gl_program_resource
*
533 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
534 GLenum programInterface
, const char *name
,
535 unsigned *array_index
)
537 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
538 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
540 if (res
->Type
!= programInterface
)
543 /* Resource basename. */
544 const char *rname
= _mesa_program_resource_name(res
);
546 /* Since ARB_gl_spirv lack of name reflections is a possibility */
550 unsigned baselen
= strlen(rname
);
551 unsigned baselen_without_array_index
= baselen
;
552 const char *rname_last_square_bracket
= strrchr(rname
, '[');
554 bool rname_has_array_index_zero
= false;
555 /* From ARB_program_interface_query spec:
557 * "uint GetProgramResourceIndex(uint program, enum programInterface,
560 * If <name> exactly matches the name string of one of the active
561 * resources for <programInterface>, the index of the matched resource is
562 * returned. Additionally, if <name> would exactly match the name string
563 * of an active resource if "[0]" were appended to <name>, the index of
564 * the matched resource is returned. [...]"
566 * "A string provided to GetProgramResourceLocation or
567 * GetProgramResourceLocationIndex is considered to match an active variable
570 * * the string exactly matches the name of the active variable;
572 * * if the string identifies the base name of an active array, where the
573 * string would exactly match the name of the variable if the suffix
574 * "[0]" were appended to the string; [...]"
576 /* Remove array's index from interface block name comparison only if
577 * array's index is zero and the resulting string length is the same
578 * than the provided name's length.
580 if (rname_last_square_bracket
) {
581 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
582 rname_has_array_index_zero
=
583 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
584 (baselen_without_array_index
== strlen(name
));
587 if (strncmp(rname
, name
, baselen
) == 0)
589 else if (rname_has_array_index_zero
&&
590 strncmp(rname
, name
, baselen_without_array_index
) == 0)
594 switch (programInterface
) {
595 case GL_UNIFORM_BLOCK
:
596 case GL_SHADER_STORAGE_BLOCK
:
597 /* Basename match, check if array or struct. */
598 if (rname_has_array_index_zero
||
599 name
[baselen
] == '\0' ||
600 name
[baselen
] == '[' ||
601 name
[baselen
] == '.') {
605 case GL_TRANSFORM_FEEDBACK_VARYING
:
606 case GL_BUFFER_VARIABLE
:
608 case GL_VERTEX_SUBROUTINE_UNIFORM
:
609 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
610 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
611 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
612 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
613 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
614 case GL_VERTEX_SUBROUTINE
:
615 case GL_GEOMETRY_SUBROUTINE
:
616 case GL_FRAGMENT_SUBROUTINE
:
617 case GL_COMPUTE_SUBROUTINE
:
618 case GL_TESS_CONTROL_SUBROUTINE
:
619 case GL_TESS_EVALUATION_SUBROUTINE
:
620 if (name
[baselen
] == '.') {
624 case GL_PROGRAM_INPUT
:
625 case GL_PROGRAM_OUTPUT
:
626 if (name
[baselen
] == '\0') {
628 } else if (name
[baselen
] == '[' &&
629 valid_array_index(name
, array_index
)) {
634 assert(!"not implemented for given interface");
641 /* Find an uniform or buffer variable program resource with an specific offset
642 * inside a block with an specific binding.
644 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
646 static struct gl_program_resource
*
647 program_resource_find_binding_offset(struct gl_shader_program
*shProg
,
648 GLenum programInterface
,
649 const GLuint binding
,
653 /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
654 GLenum blockInterface
;
656 switch (programInterface
) {
657 case GL_BUFFER_VARIABLE
:
658 blockInterface
= GL_SHADER_STORAGE_BLOCK
;
661 blockInterface
= GL_UNIFORM_BLOCK
;
664 assert("Invalid program interface");
668 int block_index
= -1;
669 int starting_index
= -1;
670 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
672 /* Blocks are added to the resource list in the same order that they are
673 * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
674 * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
677 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
678 if (res
->Type
!= blockInterface
)
681 /* Store the first index where a resource of the specific interface is. */
682 if (starting_index
== -1)
685 const struct gl_uniform_block
*block
= RESOURCE_UBO(res
);
687 if (block
->Binding
== binding
) {
688 /* For arrays, or arrays of arrays of blocks, we want the resource
689 * for the block with base index. Most properties for members of each
690 * block are inherited from the block with the base index, including
691 * a uniform being active or not.
693 block_index
= i
- starting_index
- block
->linearized_array_index
;
698 if (block_index
== -1)
701 /* We now look for the resource corresponding to the uniform or buffer
702 * variable using the BLOCK_INDEX and OFFSET.
704 res
= shProg
->data
->ProgramResourceList
;
705 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
706 if (res
->Type
!= programInterface
)
709 const struct gl_uniform_storage
*uniform
= RESOURCE_UNI(res
);
711 if (uniform
->block_index
== block_index
&& uniform
->offset
== offset
) {
719 /* Checks if an uniform or buffer variable is in the active program resource
722 * It takes into accout that for variables coming from SPIR-V binaries their
723 * names could not be available (ARB_gl_spirv). In that case, it will use the
724 * the offset and the block binding to locate the resource.
726 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
728 struct gl_program_resource
*
729 _mesa_program_resource_find_active_variable(struct gl_shader_program
*shProg
,
730 GLenum programInterface
,
731 const gl_uniform_block
*block
,
734 struct gl_program_resource
*res
;
735 struct gl_uniform_buffer_variable uni
= block
->Uniforms
[index
];
737 assert(programInterface
== GL_UNIFORM
||
738 programInterface
== GL_BUFFER_VARIABLE
);
741 res
= _mesa_program_resource_find_name(shProg
, programInterface
, uni
.IndexName
,
744 /* As the resource has no associated name (ARB_gl_spirv),
745 * we can use the UBO/SSBO binding and offset to find it.
747 res
= program_resource_find_binding_offset(shProg
, programInterface
,
748 block
->Binding
, uni
.Offset
);
755 calc_resource_index(struct gl_shader_program
*shProg
,
756 struct gl_program_resource
*res
)
760 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
761 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
763 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
766 return GL_INVALID_INDEX
;
770 * Calculate index for the given resource.
773 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
774 struct gl_program_resource
*res
)
777 return GL_INVALID_INDEX
;
780 case GL_ATOMIC_COUNTER_BUFFER
:
781 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
782 case GL_VERTEX_SUBROUTINE
:
783 case GL_GEOMETRY_SUBROUTINE
:
784 case GL_FRAGMENT_SUBROUTINE
:
785 case GL_COMPUTE_SUBROUTINE
:
786 case GL_TESS_CONTROL_SUBROUTINE
:
787 case GL_TESS_EVALUATION_SUBROUTINE
:
788 return RESOURCE_SUB(res
)->index
;
789 case GL_UNIFORM_BLOCK
:
790 case GL_SHADER_STORAGE_BLOCK
:
791 case GL_TRANSFORM_FEEDBACK_BUFFER
:
792 case GL_TRANSFORM_FEEDBACK_VARYING
:
794 return calc_resource_index(shProg
, res
);
799 * Find a program resource that points to given data.
801 static struct gl_program_resource
*
802 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
804 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
805 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
807 if (res
->Data
== data
)
813 /* Find a program resource with specific index in given interface.
815 struct gl_program_resource
*
816 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
817 GLenum programInterface
, GLuint index
)
819 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
822 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
824 if (res
->Type
!= programInterface
)
828 case GL_UNIFORM_BLOCK
:
829 case GL_ATOMIC_COUNTER_BUFFER
:
830 case GL_SHADER_STORAGE_BLOCK
:
831 case GL_TRANSFORM_FEEDBACK_BUFFER
:
832 if (_mesa_program_resource_index(shProg
, res
) == index
)
835 case GL_TRANSFORM_FEEDBACK_VARYING
:
836 case GL_PROGRAM_INPUT
:
837 case GL_PROGRAM_OUTPUT
:
839 case GL_VERTEX_SUBROUTINE_UNIFORM
:
840 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
841 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
842 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
843 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
844 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
845 case GL_VERTEX_SUBROUTINE
:
846 case GL_GEOMETRY_SUBROUTINE
:
847 case GL_FRAGMENT_SUBROUTINE
:
848 case GL_COMPUTE_SUBROUTINE
:
849 case GL_TESS_CONTROL_SUBROUTINE
:
850 case GL_TESS_EVALUATION_SUBROUTINE
:
851 case GL_BUFFER_VARIABLE
:
852 if (++idx
== (int) index
)
856 assert(!"not implemented for given interface");
862 /* Function returns if resource name is expected to have index
866 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
869 * "If the active uniform is an array, the uniform name returned in
870 * name will always be the name of the uniform array appended with
873 * The same text also appears in the OpenGL 4.2 spec. It does not,
874 * however, appear in any previous spec. Previous specifications are
875 * ambiguous in this regard. However, either name can later be passed
876 * to glGetUniformLocation (and related APIs), so there shouldn't be any
877 * harm in always appending "[0]" to uniform array names.
880 add_index_to_name(struct gl_program_resource
*res
)
882 /* Transform feedback varyings have array index already appended
885 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
888 /* Get name length of a program resource. This consists of
889 * base name + 3 for '[0]' if resource is an array.
892 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
894 const char* name
= _mesa_program_resource_name(res
);
896 /* For shaders constructed from SPIR-V binaries, variables may not
897 * have names associated with them.
902 unsigned length
= strlen(name
);
903 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
908 /* Get full name of a program resource.
911 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
912 GLenum programInterface
, GLuint index
,
913 GLsizei bufSize
, GLsizei
*length
,
914 GLchar
*name
, const char *caller
)
916 GET_CURRENT_CONTEXT(ctx
);
918 /* Find resource with given interface and index. */
919 struct gl_program_resource
*res
=
920 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
922 /* The error INVALID_VALUE is generated if <index> is greater than
923 * or equal to the number of entries in the active resource list for
924 * <programInterface>.
927 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
932 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
939 length
= &localLength
;
941 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
943 /* The resource name can be NULL for shaders constructed from SPIR-V
944 * binaries. In that case, we do not add the '[0]'.
946 if (name
&& name
[0] != '\0' &&
947 _mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
950 /* The comparison is strange because *length does *NOT* include the
951 * terminating NUL, but maxLength does.
953 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
954 name
[*length
+ i
] = "[0]"[i
];
956 name
[*length
+ i
] = '\0';
963 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
966 case GL_PROGRAM_INPUT
: {
967 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
969 if (var
->location
== -1)
972 /* If the input is an array, fail if the index is out of bounds. */
974 && array_index
>= var
->type
->length
) {
977 return var
->location
+
978 (array_index
* var
->type
->without_array()->matrix_columns
);
980 case GL_PROGRAM_OUTPUT
:
981 if (RESOURCE_VAR(res
)->location
== -1)
984 /* If the output is an array, fail if the index is out of bounds. */
986 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
989 return RESOURCE_VAR(res
)->location
+ array_index
;
991 /* If the uniform is built-in, fail. */
992 if (RESOURCE_UNI(res
)->builtin
)
995 /* From page 79 of the OpenGL 4.2 spec:
997 * "A valid name cannot be a structure, an array of structures, or any
998 * portion of a single vector or a matrix."
1000 if (RESOURCE_UNI(res
)->type
->without_array()->is_struct())
1003 /* From the GL_ARB_uniform_buffer_object spec:
1005 * "The value -1 will be returned if <name> does not correspond to an
1006 * active uniform variable name in <program>, if <name> is associated
1007 * with a named uniform block, or if <name> starts with the reserved
1010 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
1011 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
1015 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1016 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1017 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1018 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1019 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1020 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1021 /* If the uniform is an array, fail if the index is out of bounds. */
1023 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
1027 /* location in remap table + array element offset */
1028 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
1035 * Function implements following location queries:
1036 * glGetUniformLocation
1039 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
1040 GLenum programInterface
, const char *name
)
1042 unsigned array_index
= 0;
1043 struct gl_program_resource
*res
=
1044 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
1047 /* Resource not found. */
1051 return program_resource_location(res
, array_index
);
1055 _get_resource_location_index(struct gl_program_resource
*res
)
1057 /* Non-existent variable or resource is not referenced by fragment stage. */
1058 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
1061 /* From OpenGL 4.5 spec, 7.3 Program Objects
1062 * "The value -1 will be returned by either command...
1063 * ... or if name identifies an active variable that does not have a
1064 * valid location assigned.
1066 if (RESOURCE_VAR(res
)->location
== -1)
1068 return RESOURCE_VAR(res
)->index
;
1072 * Function implements following index queries:
1073 * glGetFragDataIndex
1076 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
1077 GLenum programInterface
, const char *name
)
1079 struct gl_program_resource
*res
=
1080 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
1082 return _get_resource_location_index(res
);
1086 stage_from_enum(GLenum ref
)
1089 case GL_REFERENCED_BY_VERTEX_SHADER
:
1090 return MESA_SHADER_VERTEX
;
1091 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1092 return MESA_SHADER_TESS_CTRL
;
1093 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1094 return MESA_SHADER_TESS_EVAL
;
1095 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1096 return MESA_SHADER_GEOMETRY
;
1097 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1098 return MESA_SHADER_FRAGMENT
;
1099 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1100 return MESA_SHADER_COMPUTE
;
1102 assert(!"shader stage not supported");
1103 return MESA_SHADER_STAGES
;
1108 * Check if resource is referenced by given 'referenced by' stage enum.
1109 * ATC and UBO resources hold stage references of their own.
1112 is_resource_referenced(struct gl_shader_program
*shProg
,
1113 struct gl_program_resource
*res
,
1114 GLuint index
, uint8_t stage
)
1116 /* First, check if we even have such a stage active. */
1117 if (!shProg
->_LinkedShaders
[stage
])
1120 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
1121 return RESOURCE_ATC(res
)->StageReferences
[stage
];
1123 if (res
->Type
== GL_UNIFORM_BLOCK
)
1124 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
1126 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
1127 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
1129 return res
->StageReferences
& (1 << stage
);
1133 get_buffer_property(struct gl_shader_program
*shProg
,
1134 struct gl_program_resource
*res
, const GLenum prop
,
1135 GLint
*val
, const char *caller
)
1137 GET_CURRENT_CONTEXT(ctx
);
1138 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1139 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1140 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1141 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1142 goto invalid_operation
;
1144 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1146 case GL_BUFFER_BINDING
:
1147 *val
= RESOURCE_UBO(res
)->Binding
;
1149 case GL_BUFFER_DATA_SIZE
:
1150 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1152 case GL_NUM_ACTIVE_VARIABLES
:
1154 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1155 struct gl_program_resource
*uni
=
1156 _mesa_program_resource_find_active_variable(
1167 case GL_ACTIVE_VARIABLES
: {
1168 unsigned num_values
= 0;
1169 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1170 struct gl_program_resource
*uni
=
1171 _mesa_program_resource_find_active_variable(
1180 _mesa_program_resource_index(shProg
, uni
);
1186 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1188 case GL_BUFFER_BINDING
:
1189 *val
= RESOURCE_UBO(res
)->Binding
;
1191 case GL_BUFFER_DATA_SIZE
:
1192 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1194 case GL_NUM_ACTIVE_VARIABLES
:
1196 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1197 struct gl_program_resource
*uni
=
1198 _mesa_program_resource_find_active_variable(
1209 case GL_ACTIVE_VARIABLES
: {
1210 unsigned num_values
= 0;
1211 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1212 struct gl_program_resource
*uni
=
1213 _mesa_program_resource_find_active_variable(
1222 _mesa_program_resource_index(shProg
, uni
);
1228 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1230 case GL_BUFFER_BINDING
:
1231 *val
= RESOURCE_ATC(res
)->Binding
;
1233 case GL_BUFFER_DATA_SIZE
:
1234 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1236 case GL_NUM_ACTIVE_VARIABLES
:
1237 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1239 case GL_ACTIVE_VARIABLES
:
1240 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1241 /* Active atomic buffer contains index to UniformStorage. Find
1242 * out gl_program_resource via data pointer and then calculate
1243 * index of that uniform.
1245 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1246 struct gl_program_resource
*uni
=
1247 program_resource_find_data(shProg
,
1248 &shProg
->data
->UniformStorage
[idx
]);
1250 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1252 return RESOURCE_ATC(res
)->NumUniforms
;
1254 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1256 case GL_BUFFER_BINDING
:
1257 *val
= RESOURCE_XFB(res
)->Binding
;
1259 case GL_NUM_ACTIVE_VARIABLES
:
1260 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1262 case GL_ACTIVE_VARIABLES
:
1263 struct gl_transform_feedback_info
*linked_xfb
=
1264 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1265 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1266 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1267 struct gl_program_resource
*buf_res
=
1268 _mesa_program_resource_find_index(shProg
,
1269 GL_TRANSFORM_FEEDBACK_BUFFER
,
1272 if (res
== buf_res
) {
1276 return RESOURCE_XFB(res
)->NumVaryings
;
1279 assert(!"support for property type not implemented");
1282 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1283 _mesa_enum_to_string(res
->Type
),
1284 _mesa_enum_to_string(prop
));
1290 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1291 struct gl_program_resource
*res
, GLuint index
,
1292 const GLenum prop
, GLint
*val
, const char *caller
)
1294 GET_CURRENT_CONTEXT(ctx
);
1296 #define VALIDATE_TYPE(type)\
1297 if (res->Type != type)\
1298 goto invalid_operation;
1300 #define VALIDATE_TYPE_2(type1, type2)\
1301 if (res->Type != type1 && res->Type != type2)\
1302 goto invalid_operation;
1305 case GL_NAME_LENGTH
:
1306 switch (res
->Type
) {
1307 case GL_ATOMIC_COUNTER_BUFFER
:
1308 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1309 goto invalid_operation
;
1311 /* Resource name length + terminator. */
1312 *val
= _mesa_program_resource_name_len(res
) + 1;
1316 switch (res
->Type
) {
1318 case GL_BUFFER_VARIABLE
:
1319 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1321 case GL_PROGRAM_INPUT
:
1322 case GL_PROGRAM_OUTPUT
:
1323 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1325 case GL_TRANSFORM_FEEDBACK_VARYING
:
1326 *val
= RESOURCE_XFV(res
)->Type
;
1329 goto invalid_operation
;
1332 switch (res
->Type
) {
1334 case GL_BUFFER_VARIABLE
:
1335 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1336 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1337 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1338 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1339 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1340 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1342 /* Test if a buffer variable is an array or an unsized array.
1343 * Unsized arrays return zero as array size.
1345 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1346 RESOURCE_UNI(res
)->array_stride
> 0)
1347 *val
= RESOURCE_UNI(res
)->array_elements
;
1349 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1351 case GL_PROGRAM_INPUT
:
1352 case GL_PROGRAM_OUTPUT
:
1353 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1355 case GL_TRANSFORM_FEEDBACK_VARYING
:
1356 *val
= RESOURCE_XFV(res
)->Size
;
1359 goto invalid_operation
;
1362 switch (res
->Type
) {
1364 case GL_BUFFER_VARIABLE
:
1365 *val
= RESOURCE_UNI(res
)->offset
;
1367 case GL_TRANSFORM_FEEDBACK_VARYING
:
1368 *val
= RESOURCE_XFV(res
)->Offset
;
1371 goto invalid_operation
;
1373 case GL_BLOCK_INDEX
:
1374 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1375 *val
= RESOURCE_UNI(res
)->block_index
;
1377 case GL_ARRAY_STRIDE
:
1378 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1379 *val
= RESOURCE_UNI(res
)->array_stride
;
1381 case GL_MATRIX_STRIDE
:
1382 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1383 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1385 case GL_IS_ROW_MAJOR
:
1386 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1387 *val
= RESOURCE_UNI(res
)->row_major
;
1389 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1390 VALIDATE_TYPE(GL_UNIFORM
);
1391 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1393 case GL_BUFFER_BINDING
:
1394 case GL_BUFFER_DATA_SIZE
:
1395 case GL_NUM_ACTIVE_VARIABLES
:
1396 case GL_ACTIVE_VARIABLES
:
1397 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1398 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1399 if (!_mesa_has_compute_shaders(ctx
))
1402 case GL_REFERENCED_BY_VERTEX_SHADER
:
1403 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1404 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1405 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1406 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1407 switch (res
->Type
) {
1409 case GL_PROGRAM_INPUT
:
1410 case GL_PROGRAM_OUTPUT
:
1411 case GL_UNIFORM_BLOCK
:
1412 case GL_BUFFER_VARIABLE
:
1413 case GL_SHADER_STORAGE_BLOCK
:
1414 case GL_ATOMIC_COUNTER_BUFFER
:
1415 *val
= is_resource_referenced(shProg
, res
, index
,
1416 stage_from_enum(prop
));
1419 goto invalid_operation
;
1422 switch (res
->Type
) {
1424 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1425 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1426 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1427 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1428 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1429 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1430 case GL_PROGRAM_INPUT
:
1431 case GL_PROGRAM_OUTPUT
:
1432 *val
= program_resource_location(res
, 0);
1435 goto invalid_operation
;
1437 case GL_LOCATION_COMPONENT
:
1438 switch (res
->Type
) {
1439 case GL_PROGRAM_INPUT
:
1440 case GL_PROGRAM_OUTPUT
:
1441 *val
= RESOURCE_VAR(res
)->component
;
1444 goto invalid_operation
;
1446 case GL_LOCATION_INDEX
: {
1448 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1449 goto invalid_operation
;
1450 tmp
= program_resource_location(res
, 0);
1454 *val
= _get_resource_location_index(res
);
1457 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1458 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1459 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1460 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1461 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1462 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1463 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1464 goto invalid_operation
;
1465 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1467 case GL_COMPATIBLE_SUBROUTINES
: {
1468 const struct gl_uniform_storage
*uni
;
1469 struct gl_program
*p
;
1473 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1474 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1475 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1476 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1477 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1478 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1479 goto invalid_operation
;
1480 uni
= RESOURCE_UNI(res
);
1482 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1484 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1485 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1486 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1487 if (fn
->types
[j
] == uni
->type
) {
1496 case GL_TOP_LEVEL_ARRAY_SIZE
:
1497 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1498 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1501 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1502 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1503 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1506 /* GL_ARB_tessellation_shader */
1507 case GL_IS_PER_PATCH
:
1508 switch (res
->Type
) {
1509 case GL_PROGRAM_INPUT
:
1510 case GL_PROGRAM_OUTPUT
:
1511 *val
= RESOURCE_VAR(res
)->patch
;
1514 goto invalid_operation
;
1517 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1518 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1519 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1521 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1522 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1523 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1530 #undef VALIDATE_TYPE
1531 #undef VALIDATE_TYPE_2
1534 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1535 _mesa_enum_to_string(res
->Type
),
1536 _mesa_enum_to_string(prop
));
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1541 _mesa_enum_to_string(res
->Type
),
1542 _mesa_enum_to_string(prop
));
1547 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1548 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1549 const GLenum
*props
, GLsizei bufSize
,
1550 GLsizei
*length
, GLint
*params
)
1552 GET_CURRENT_CONTEXT(ctx
);
1553 GLint
*val
= (GLint
*) params
;
1554 const GLenum
*prop
= props
;
1557 struct gl_program_resource
*res
=
1558 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1560 /* No such resource found or bufSize negative. */
1561 if (!res
|| bufSize
< 0) {
1562 _mesa_error(ctx
, GL_INVALID_VALUE
,
1563 "glGetProgramResourceiv(%s index %d bufSize %d)",
1564 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1568 /* Write propCount values until error occurs or bufSize reached. */
1569 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1571 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1572 "glGetProgramResourceiv");
1574 /* Error happened. */
1575 if (props_written
== 0)
1578 amount
+= props_written
;
1581 /* If <length> is not NULL, the actual number of integer values
1582 * written to <params> will be written to <length>.
1589 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1591 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1594 const bool producer_is_array_stage
=
1595 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1596 const bool consumer_is_array_stage
=
1597 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1598 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1599 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1603 gl_shader_variable
const **outputs
=
1604 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1605 sizeof(gl_shader_variable
*));
1606 if (outputs
== NULL
)
1609 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1612 * At an interface between program objects, the set of inputs and
1613 * outputs are considered to match exactly if and only if:
1615 * - Every declared input variable has a matching output, as described
1617 * - There are no user-defined output variables declared without a
1618 * matching input variable declaration.
1620 * Every input has an output, and every output has an input. Scan the list
1621 * of producer resources once, and generate the list of outputs. As inputs
1622 * and outputs are matched, remove the matched outputs from the set. At
1623 * the end, the set must be empty. If the set is not empty, then there is
1624 * some output that did not have an input.
1626 unsigned num_outputs
= 0;
1627 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1628 struct gl_program_resource
*res
=
1629 &producer
->sh
.data
->ProgramResourceList
[i
];
1631 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1634 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1636 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1639 * Built-in inputs or outputs do not affect interface matching.
1641 if (is_gl_identifier(var
->name
))
1644 outputs
[num_outputs
++] = var
;
1647 unsigned match_index
= 0;
1648 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1649 struct gl_program_resource
*res
=
1650 &consumer
->sh
.data
->ProgramResourceList
[i
];
1652 if (res
->Type
!= GL_PROGRAM_INPUT
)
1655 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1656 gl_shader_variable
const *producer_var
= NULL
;
1658 if (is_gl_identifier(consumer_var
->name
))
1661 /* Inputs with explicit locations match other outputs with explicit
1662 * locations by location instead of by name.
1664 if (consumer_var
->explicit_location
) {
1665 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1666 const gl_shader_variable
*const var
= outputs
[j
];
1668 if (var
->explicit_location
&&
1669 consumer_var
->location
== var
->location
) {
1676 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1677 const gl_shader_variable
*const var
= outputs
[j
];
1679 if (!var
->explicit_location
&&
1680 strcmp(consumer_var
->name
, var
->name
) == 0) {
1688 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1691 * - An output variable is considered to match an input variable in
1692 * the subsequent shader if:
1694 * - the two variables match in name, type, and qualification; or
1696 * - the two variables are declared with the same location
1697 * qualifier and match in type and qualification.
1699 if (producer_var
== NULL
) {
1704 /* An output cannot match more than one input, so remove the output from
1705 * the set of possible outputs.
1707 outputs
[match_index
] = NULL
;
1709 if (match_index
< num_outputs
)
1710 outputs
[match_index
] = outputs
[num_outputs
];
1712 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1714 * "Tessellation control shader per-vertex output variables and
1715 * blocks and tessellation control, tessellation evaluation, and
1716 * geometry shader per-vertex input variables and blocks are
1717 * required to be declared as arrays, with each element representing
1718 * input or output values for a single vertex of a multi-vertex
1719 * primitive. For the purposes of interface matching, such variables
1720 * and blocks are treated as though they were not declared as
1723 * So we unwrap those types before matching.
1725 const glsl_type
*consumer_type
= consumer_var
->type
;
1726 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1727 const glsl_type
*producer_type
= producer_var
->type
;
1728 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1730 if (consumer_is_array_stage
) {
1731 if (consumer_interface_type
) {
1732 /* the interface is the array; the underlying types should match */
1733 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1734 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1736 if (consumer_type
->is_array() && !consumer_var
->patch
)
1737 consumer_type
= consumer_type
->fields
.array
;
1741 if (producer_is_array_stage
) {
1742 if (producer_interface_type
) {
1743 /* the interface is the array; the underlying types should match */
1744 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1745 producer_interface_type
= producer_interface_type
->fields
.array
;
1747 if (producer_type
->is_array() && !producer_var
->patch
)
1748 producer_type
= producer_type
->fields
.array
;
1752 if (producer_type
!= consumer_type
) {
1757 if (producer_interface_type
!= consumer_interface_type
) {
1762 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1764 * Qualifier Class| Qualifier |in/out
1765 * ---------------+-------------+------
1769 * ---------------+-------------+------
1770 * Auxiliary | centroid | No
1771 * ---------------+-------------+------
1773 * | Block layout| N/A
1777 * ---------------+-------------+------
1778 * Interpolation | smooth |
1780 * ---------------+-------------+------
1782 * Precision | mediump | Yes
1784 * ---------------+-------------+------
1785 * Variance | invariant | No
1786 * ---------------+-------------+------
1787 * Memory | all | N/A
1789 * Note that location mismatches are detected by the loops above that
1790 * find the producer variable that goes with the consumer variable.
1792 unsigned producer_interpolation
= producer_var
->interpolation
;
1793 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1794 if (producer_interpolation
== INTERP_MODE_NONE
)
1795 producer_interpolation
= INTERP_MODE_SMOOTH
;
1796 if (consumer_interpolation
== INTERP_MODE_NONE
)
1797 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1798 if (producer_interpolation
!= consumer_interpolation
) {
1803 if (producer_var
->precision
!= consumer_var
->precision
) {
1808 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1816 return valid
&& num_outputs
== 0;
1820 * Validate inputs against outputs in a program pipeline.
1823 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1825 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1827 /* Find first active stage in pipeline. */
1828 unsigned idx
, prev
= 0;
1829 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1836 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1838 /* Pipeline might include both non-compute and a compute program, do
1839 * not attempt to validate varyings between non-compute and compute
1842 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1845 if (!validate_io(prog
[prev
], prog
[idx
]))