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"
40 #include "util/mesa-sha1.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
;
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
);
531 compute_resource_key(GLenum programInterface
, const char *name
)
533 struct mesa_sha1 ctx
;
534 unsigned char sha1
[20];
536 _mesa_sha1_init(&ctx
);
537 _mesa_sha1_update(&ctx
, &programInterface
, sizeof(programInterface
));
538 _mesa_sha1_update(&ctx
, name
, strlen(name
));
539 _mesa_sha1_final(&ctx
, sha1
);
541 return _mesa_hash_data(sha1
, sizeof(sha1
));
544 static struct gl_program_resource
*
545 search_resource_hash(struct gl_shader_program
*shProg
,
546 GLenum programInterface
, const char *name
,
547 unsigned *array_index
)
549 const char *base_name_end
;
550 long index
= parse_program_resource_name(name
, &base_name_end
);
553 /* If dealing with array, we need to get the basename. */
555 name_copy
= (char *) malloc(base_name_end
- name
+ 1);
556 memcpy(name_copy
, name
, base_name_end
- name
);
557 name_copy
[base_name_end
- name
] = '\0';
559 name_copy
= (char*) name
;
562 uint32_t key
= compute_resource_key(programInterface
, name_copy
);
563 struct gl_program_resource
*res
= (struct gl_program_resource
*)
564 _mesa_hash_table_u64_search(shProg
->data
->ProgramResourceHash
, key
);
566 if (name_copy
!= name
)
569 if (res
&& array_index
)
570 *array_index
= index
>= 0 ? index
: 0;
575 /* Find a program resource with specific name in given interface.
577 struct gl_program_resource
*
578 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
579 GLenum programInterface
, const char *name
,
580 unsigned *array_index
)
582 struct gl_program_resource
*res
= NULL
;
584 /* If we have a name, try the ProgramResourceHash first. */
585 if (name
&& shProg
->data
->ProgramResourceHash
)
586 res
= search_resource_hash(shProg
, programInterface
, name
, array_index
);
591 res
= shProg
->data
->ProgramResourceList
;
592 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
593 if (res
->Type
!= programInterface
)
596 /* Resource basename. */
597 const char *rname
= _mesa_program_resource_name(res
);
599 /* Since ARB_gl_spirv lack of name reflections is a possibility */
603 unsigned baselen
= strlen(rname
);
604 unsigned baselen_without_array_index
= baselen
;
605 const char *rname_last_square_bracket
= strrchr(rname
, '[');
607 bool rname_has_array_index_zero
= false;
608 /* From ARB_program_interface_query spec:
610 * "uint GetProgramResourceIndex(uint program, enum programInterface,
613 * If <name> exactly matches the name string of one of the active
614 * resources for <programInterface>, the index of the matched resource is
615 * returned. Additionally, if <name> would exactly match the name string
616 * of an active resource if "[0]" were appended to <name>, the index of
617 * the matched resource is returned. [...]"
619 * "A string provided to GetProgramResourceLocation or
620 * GetProgramResourceLocationIndex is considered to match an active variable
623 * * the string exactly matches the name of the active variable;
625 * * if the string identifies the base name of an active array, where the
626 * string would exactly match the name of the variable if the suffix
627 * "[0]" were appended to the string; [...]"
629 /* Remove array's index from interface block name comparison only if
630 * array's index is zero and the resulting string length is the same
631 * than the provided name's length.
633 if (rname_last_square_bracket
) {
634 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
635 rname_has_array_index_zero
=
636 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
637 (baselen_without_array_index
== strlen(name
));
640 if (strncmp(rname
, name
, baselen
) == 0)
642 else if (rname_has_array_index_zero
&&
643 strncmp(rname
, name
, baselen_without_array_index
) == 0)
647 switch (programInterface
) {
648 case GL_UNIFORM_BLOCK
:
649 case GL_SHADER_STORAGE_BLOCK
:
650 /* Basename match, check if array or struct. */
651 if (rname_has_array_index_zero
||
652 name
[baselen
] == '\0' ||
653 name
[baselen
] == '[' ||
654 name
[baselen
] == '.') {
658 case GL_TRANSFORM_FEEDBACK_VARYING
:
659 case GL_BUFFER_VARIABLE
:
661 case GL_VERTEX_SUBROUTINE_UNIFORM
:
662 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
663 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
664 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
665 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
666 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
667 case GL_VERTEX_SUBROUTINE
:
668 case GL_GEOMETRY_SUBROUTINE
:
669 case GL_FRAGMENT_SUBROUTINE
:
670 case GL_COMPUTE_SUBROUTINE
:
671 case GL_TESS_CONTROL_SUBROUTINE
:
672 case GL_TESS_EVALUATION_SUBROUTINE
:
673 if (name
[baselen
] == '.') {
677 case GL_PROGRAM_INPUT
:
678 case GL_PROGRAM_OUTPUT
:
679 if (name
[baselen
] == '\0') {
681 } else if (name
[baselen
] == '[' &&
682 valid_array_index(name
, array_index
)) {
687 assert(!"not implemented for given interface");
694 /* Find an uniform or buffer variable program resource with an specific offset
695 * inside a block with an specific binding.
697 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
699 static struct gl_program_resource
*
700 program_resource_find_binding_offset(struct gl_shader_program
*shProg
,
701 GLenum programInterface
,
702 const GLuint binding
,
706 /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
707 GLenum blockInterface
;
709 switch (programInterface
) {
710 case GL_BUFFER_VARIABLE
:
711 blockInterface
= GL_SHADER_STORAGE_BLOCK
;
714 blockInterface
= GL_UNIFORM_BLOCK
;
717 assert("Invalid program interface");
721 int block_index
= -1;
722 int starting_index
= -1;
723 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
725 /* Blocks are added to the resource list in the same order that they are
726 * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
727 * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
730 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
731 if (res
->Type
!= blockInterface
)
734 /* Store the first index where a resource of the specific interface is. */
735 if (starting_index
== -1)
738 const struct gl_uniform_block
*block
= RESOURCE_UBO(res
);
740 if (block
->Binding
== binding
) {
741 /* For arrays, or arrays of arrays of blocks, we want the resource
742 * for the block with base index. Most properties for members of each
743 * block are inherited from the block with the base index, including
744 * a uniform being active or not.
746 block_index
= i
- starting_index
- block
->linearized_array_index
;
751 if (block_index
== -1)
754 /* We now look for the resource corresponding to the uniform or buffer
755 * variable using the BLOCK_INDEX and OFFSET.
757 res
= shProg
->data
->ProgramResourceList
;
758 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
759 if (res
->Type
!= programInterface
)
762 const struct gl_uniform_storage
*uniform
= RESOURCE_UNI(res
);
764 if (uniform
->block_index
== block_index
&& uniform
->offset
== offset
) {
772 /* Checks if an uniform or buffer variable is in the active program resource
775 * It takes into accout that for variables coming from SPIR-V binaries their
776 * names could not be available (ARB_gl_spirv). In that case, it will use the
777 * the offset and the block binding to locate the resource.
779 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
781 struct gl_program_resource
*
782 _mesa_program_resource_find_active_variable(struct gl_shader_program
*shProg
,
783 GLenum programInterface
,
784 const gl_uniform_block
*block
,
787 struct gl_program_resource
*res
;
788 struct gl_uniform_buffer_variable uni
= block
->Uniforms
[index
];
790 assert(programInterface
== GL_UNIFORM
||
791 programInterface
== GL_BUFFER_VARIABLE
);
794 res
= _mesa_program_resource_find_name(shProg
, programInterface
, uni
.IndexName
,
797 /* As the resource has no associated name (ARB_gl_spirv),
798 * we can use the UBO/SSBO binding and offset to find it.
800 res
= program_resource_find_binding_offset(shProg
, programInterface
,
801 block
->Binding
, uni
.Offset
);
808 calc_resource_index(struct gl_shader_program
*shProg
,
809 struct gl_program_resource
*res
)
813 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
814 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
816 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
819 return GL_INVALID_INDEX
;
823 * Calculate index for the given resource.
826 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
827 struct gl_program_resource
*res
)
830 return GL_INVALID_INDEX
;
833 case GL_ATOMIC_COUNTER_BUFFER
:
834 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
835 case GL_VERTEX_SUBROUTINE
:
836 case GL_GEOMETRY_SUBROUTINE
:
837 case GL_FRAGMENT_SUBROUTINE
:
838 case GL_COMPUTE_SUBROUTINE
:
839 case GL_TESS_CONTROL_SUBROUTINE
:
840 case GL_TESS_EVALUATION_SUBROUTINE
:
841 return RESOURCE_SUB(res
)->index
;
842 case GL_UNIFORM_BLOCK
:
843 case GL_SHADER_STORAGE_BLOCK
:
844 case GL_TRANSFORM_FEEDBACK_BUFFER
:
845 case GL_TRANSFORM_FEEDBACK_VARYING
:
847 return calc_resource_index(shProg
, res
);
852 * Find a program resource that points to given data.
854 static struct gl_program_resource
*
855 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
857 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
858 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
860 if (res
->Data
== data
)
866 /* Find a program resource with specific index in given interface.
868 struct gl_program_resource
*
869 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
870 GLenum programInterface
, GLuint index
)
872 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
875 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
877 if (res
->Type
!= programInterface
)
881 case GL_UNIFORM_BLOCK
:
882 case GL_ATOMIC_COUNTER_BUFFER
:
883 case GL_SHADER_STORAGE_BLOCK
:
884 case GL_TRANSFORM_FEEDBACK_BUFFER
:
885 if (_mesa_program_resource_index(shProg
, res
) == index
)
888 case GL_TRANSFORM_FEEDBACK_VARYING
:
889 case GL_PROGRAM_INPUT
:
890 case GL_PROGRAM_OUTPUT
:
892 case GL_VERTEX_SUBROUTINE_UNIFORM
:
893 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
894 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
895 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
896 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
897 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
898 case GL_VERTEX_SUBROUTINE
:
899 case GL_GEOMETRY_SUBROUTINE
:
900 case GL_FRAGMENT_SUBROUTINE
:
901 case GL_COMPUTE_SUBROUTINE
:
902 case GL_TESS_CONTROL_SUBROUTINE
:
903 case GL_TESS_EVALUATION_SUBROUTINE
:
904 case GL_BUFFER_VARIABLE
:
905 if (++idx
== (int) index
)
909 assert(!"not implemented for given interface");
915 /* Function returns if resource name is expected to have index
919 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
922 * "If the active uniform is an array, the uniform name returned in
923 * name will always be the name of the uniform array appended with
926 * The same text also appears in the OpenGL 4.2 spec. It does not,
927 * however, appear in any previous spec. Previous specifications are
928 * ambiguous in this regard. However, either name can later be passed
929 * to glGetUniformLocation (and related APIs), so there shouldn't be any
930 * harm in always appending "[0]" to uniform array names.
933 add_index_to_name(struct gl_program_resource
*res
)
935 /* Transform feedback varyings have array index already appended
938 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
941 /* Get name length of a program resource. This consists of
942 * base name + 3 for '[0]' if resource is an array.
945 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
947 const char* name
= _mesa_program_resource_name(res
);
949 /* For shaders constructed from SPIR-V binaries, variables may not
950 * have names associated with them.
955 unsigned length
= strlen(name
);
956 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
961 /* Get full name of a program resource.
964 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
965 GLenum programInterface
, GLuint index
,
966 GLsizei bufSize
, GLsizei
*length
,
967 GLchar
*name
, const char *caller
)
969 GET_CURRENT_CONTEXT(ctx
);
971 /* Find resource with given interface and index. */
972 struct gl_program_resource
*res
=
973 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
975 /* The error INVALID_VALUE is generated if <index> is greater than
976 * or equal to the number of entries in the active resource list for
977 * <programInterface>.
980 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
985 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
992 length
= &localLength
;
994 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
996 /* The resource name can be NULL for shaders constructed from SPIR-V
997 * binaries. In that case, we do not add the '[0]'.
999 if (name
&& name
[0] != '\0' &&
1000 _mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
1003 /* The comparison is strange because *length does *NOT* include the
1004 * terminating NUL, but maxLength does.
1006 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
1007 name
[*length
+ i
] = "[0]"[i
];
1009 name
[*length
+ i
] = '\0';
1016 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
1018 switch (res
->Type
) {
1019 case GL_PROGRAM_INPUT
: {
1020 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
1022 if (var
->location
== -1)
1025 /* If the input is an array, fail if the index is out of bounds. */
1027 && array_index
>= var
->type
->length
) {
1030 return var
->location
+
1031 (array_index
* var
->type
->without_array()->matrix_columns
);
1033 case GL_PROGRAM_OUTPUT
:
1034 if (RESOURCE_VAR(res
)->location
== -1)
1037 /* If the output is an array, fail if the index is out of bounds. */
1039 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
1042 return RESOURCE_VAR(res
)->location
+ array_index
;
1044 /* If the uniform is built-in, fail. */
1045 if (RESOURCE_UNI(res
)->builtin
)
1048 /* From page 79 of the OpenGL 4.2 spec:
1050 * "A valid name cannot be a structure, an array of structures, or any
1051 * portion of a single vector or a matrix."
1053 if (RESOURCE_UNI(res
)->type
->without_array()->is_struct())
1056 /* From the GL_ARB_uniform_buffer_object spec:
1058 * "The value -1 will be returned if <name> does not correspond to an
1059 * active uniform variable name in <program>, if <name> is associated
1060 * with a named uniform block, or if <name> starts with the reserved
1063 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
1064 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
1068 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1069 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1070 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1071 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1072 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1073 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1074 /* If the uniform is an array, fail if the index is out of bounds. */
1076 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
1080 /* location in remap table + array element offset */
1081 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
1088 * Function implements following location queries:
1089 * glGetUniformLocation
1092 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
1093 GLenum programInterface
, const char *name
)
1095 unsigned array_index
= 0;
1096 struct gl_program_resource
*res
=
1097 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
1100 /* Resource not found. */
1104 return program_resource_location(res
, array_index
);
1108 _get_resource_location_index(struct gl_program_resource
*res
)
1110 /* Non-existent variable or resource is not referenced by fragment stage. */
1111 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
1114 /* From OpenGL 4.5 spec, 7.3 Program Objects
1115 * "The value -1 will be returned by either command...
1116 * ... or if name identifies an active variable that does not have a
1117 * valid location assigned.
1119 if (RESOURCE_VAR(res
)->location
== -1)
1121 return RESOURCE_VAR(res
)->index
;
1125 * Function implements following index queries:
1126 * glGetFragDataIndex
1129 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
1130 GLenum programInterface
, const char *name
)
1132 struct gl_program_resource
*res
=
1133 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
1135 return _get_resource_location_index(res
);
1139 stage_from_enum(GLenum ref
)
1142 case GL_REFERENCED_BY_VERTEX_SHADER
:
1143 return MESA_SHADER_VERTEX
;
1144 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1145 return MESA_SHADER_TESS_CTRL
;
1146 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1147 return MESA_SHADER_TESS_EVAL
;
1148 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1149 return MESA_SHADER_GEOMETRY
;
1150 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1151 return MESA_SHADER_FRAGMENT
;
1152 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1153 return MESA_SHADER_COMPUTE
;
1155 assert(!"shader stage not supported");
1156 return MESA_SHADER_STAGES
;
1161 * Check if resource is referenced by given 'referenced by' stage enum.
1162 * ATC and UBO resources hold stage references of their own.
1165 is_resource_referenced(struct gl_shader_program
*shProg
,
1166 struct gl_program_resource
*res
,
1167 GLuint index
, uint8_t stage
)
1169 /* First, check if we even have such a stage active. */
1170 if (!shProg
->_LinkedShaders
[stage
])
1173 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
1174 return RESOURCE_ATC(res
)->StageReferences
[stage
];
1176 if (res
->Type
== GL_UNIFORM_BLOCK
)
1177 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
1179 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
1180 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
1182 return res
->StageReferences
& (1 << stage
);
1186 get_buffer_property(struct gl_shader_program
*shProg
,
1187 struct gl_program_resource
*res
, const GLenum prop
,
1188 GLint
*val
, const char *caller
)
1190 GET_CURRENT_CONTEXT(ctx
);
1191 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1192 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1193 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1194 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1195 goto invalid_operation
;
1197 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1199 case GL_BUFFER_BINDING
:
1200 *val
= RESOURCE_UBO(res
)->Binding
;
1202 case GL_BUFFER_DATA_SIZE
:
1203 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1205 case GL_NUM_ACTIVE_VARIABLES
:
1207 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1208 struct gl_program_resource
*uni
=
1209 _mesa_program_resource_find_active_variable(
1220 case GL_ACTIVE_VARIABLES
: {
1221 unsigned num_values
= 0;
1222 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1223 struct gl_program_resource
*uni
=
1224 _mesa_program_resource_find_active_variable(
1233 _mesa_program_resource_index(shProg
, uni
);
1239 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1241 case GL_BUFFER_BINDING
:
1242 *val
= RESOURCE_UBO(res
)->Binding
;
1244 case GL_BUFFER_DATA_SIZE
:
1245 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1247 case GL_NUM_ACTIVE_VARIABLES
:
1249 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1250 struct gl_program_resource
*uni
=
1251 _mesa_program_resource_find_active_variable(
1262 case GL_ACTIVE_VARIABLES
: {
1263 unsigned num_values
= 0;
1264 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1265 struct gl_program_resource
*uni
=
1266 _mesa_program_resource_find_active_variable(
1275 _mesa_program_resource_index(shProg
, uni
);
1281 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1283 case GL_BUFFER_BINDING
:
1284 *val
= RESOURCE_ATC(res
)->Binding
;
1286 case GL_BUFFER_DATA_SIZE
:
1287 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1289 case GL_NUM_ACTIVE_VARIABLES
:
1290 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1292 case GL_ACTIVE_VARIABLES
:
1293 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1294 /* Active atomic buffer contains index to UniformStorage. Find
1295 * out gl_program_resource via data pointer and then calculate
1296 * index of that uniform.
1298 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1299 struct gl_program_resource
*uni
=
1300 program_resource_find_data(shProg
,
1301 &shProg
->data
->UniformStorage
[idx
]);
1303 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1305 return RESOURCE_ATC(res
)->NumUniforms
;
1307 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1309 case GL_BUFFER_BINDING
:
1310 *val
= RESOURCE_XFB(res
)->Binding
;
1312 case GL_NUM_ACTIVE_VARIABLES
:
1313 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1315 case GL_ACTIVE_VARIABLES
:
1316 struct gl_transform_feedback_info
*linked_xfb
=
1317 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1318 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1319 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1320 struct gl_program_resource
*buf_res
=
1321 _mesa_program_resource_find_index(shProg
,
1322 GL_TRANSFORM_FEEDBACK_BUFFER
,
1325 if (res
== buf_res
) {
1329 return RESOURCE_XFB(res
)->NumVaryings
;
1332 assert(!"support for property type not implemented");
1335 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1336 _mesa_enum_to_string(res
->Type
),
1337 _mesa_enum_to_string(prop
));
1343 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1344 struct gl_program_resource
*res
, GLuint index
,
1345 const GLenum prop
, GLint
*val
, const char *caller
)
1347 GET_CURRENT_CONTEXT(ctx
);
1349 #define VALIDATE_TYPE(type)\
1350 if (res->Type != type)\
1351 goto invalid_operation;
1353 #define VALIDATE_TYPE_2(type1, type2)\
1354 if (res->Type != type1 && res->Type != type2)\
1355 goto invalid_operation;
1358 case GL_NAME_LENGTH
:
1359 switch (res
->Type
) {
1360 case GL_ATOMIC_COUNTER_BUFFER
:
1361 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1362 goto invalid_operation
;
1364 /* Resource name length + terminator. */
1365 *val
= _mesa_program_resource_name_len(res
) + 1;
1369 switch (res
->Type
) {
1371 case GL_BUFFER_VARIABLE
:
1372 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1374 case GL_PROGRAM_INPUT
:
1375 case GL_PROGRAM_OUTPUT
:
1376 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1378 case GL_TRANSFORM_FEEDBACK_VARYING
:
1379 *val
= RESOURCE_XFV(res
)->Type
;
1382 goto invalid_operation
;
1385 switch (res
->Type
) {
1387 case GL_BUFFER_VARIABLE
:
1388 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1389 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1390 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1391 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1392 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1393 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1395 /* Test if a buffer variable is an array or an unsized array.
1396 * Unsized arrays return zero as array size.
1398 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1399 RESOURCE_UNI(res
)->array_stride
> 0)
1400 *val
= RESOURCE_UNI(res
)->array_elements
;
1402 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1404 case GL_PROGRAM_INPUT
:
1405 case GL_PROGRAM_OUTPUT
:
1406 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1408 case GL_TRANSFORM_FEEDBACK_VARYING
:
1409 *val
= RESOURCE_XFV(res
)->Size
;
1412 goto invalid_operation
;
1415 switch (res
->Type
) {
1417 case GL_BUFFER_VARIABLE
:
1418 *val
= RESOURCE_UNI(res
)->offset
;
1420 case GL_TRANSFORM_FEEDBACK_VARYING
:
1421 *val
= RESOURCE_XFV(res
)->Offset
;
1424 goto invalid_operation
;
1426 case GL_BLOCK_INDEX
:
1427 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1428 *val
= RESOURCE_UNI(res
)->block_index
;
1430 case GL_ARRAY_STRIDE
:
1431 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1432 *val
= RESOURCE_UNI(res
)->array_stride
;
1434 case GL_MATRIX_STRIDE
:
1435 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1436 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1438 case GL_IS_ROW_MAJOR
:
1439 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1440 *val
= RESOURCE_UNI(res
)->row_major
;
1442 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1443 VALIDATE_TYPE(GL_UNIFORM
);
1444 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1446 case GL_BUFFER_BINDING
:
1447 case GL_BUFFER_DATA_SIZE
:
1448 case GL_NUM_ACTIVE_VARIABLES
:
1449 case GL_ACTIVE_VARIABLES
:
1450 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1451 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1452 if (!_mesa_has_compute_shaders(ctx
))
1455 case GL_REFERENCED_BY_VERTEX_SHADER
:
1456 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1457 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1458 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1459 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1460 switch (res
->Type
) {
1462 case GL_PROGRAM_INPUT
:
1463 case GL_PROGRAM_OUTPUT
:
1464 case GL_UNIFORM_BLOCK
:
1465 case GL_BUFFER_VARIABLE
:
1466 case GL_SHADER_STORAGE_BLOCK
:
1467 case GL_ATOMIC_COUNTER_BUFFER
:
1468 *val
= is_resource_referenced(shProg
, res
, index
,
1469 stage_from_enum(prop
));
1472 goto invalid_operation
;
1475 switch (res
->Type
) {
1477 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1478 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1479 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1480 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1481 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1482 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1483 case GL_PROGRAM_INPUT
:
1484 case GL_PROGRAM_OUTPUT
:
1485 *val
= program_resource_location(res
, 0);
1488 goto invalid_operation
;
1490 case GL_LOCATION_COMPONENT
:
1491 switch (res
->Type
) {
1492 case GL_PROGRAM_INPUT
:
1493 case GL_PROGRAM_OUTPUT
:
1494 *val
= RESOURCE_VAR(res
)->component
;
1497 goto invalid_operation
;
1499 case GL_LOCATION_INDEX
: {
1501 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1502 goto invalid_operation
;
1503 tmp
= program_resource_location(res
, 0);
1507 *val
= _get_resource_location_index(res
);
1510 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1511 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1512 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1513 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1514 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1515 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1516 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1517 goto invalid_operation
;
1518 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1520 case GL_COMPATIBLE_SUBROUTINES
: {
1521 const struct gl_uniform_storage
*uni
;
1522 struct gl_program
*p
;
1526 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1527 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1528 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1529 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1530 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1531 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1532 goto invalid_operation
;
1533 uni
= RESOURCE_UNI(res
);
1535 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1537 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1538 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1539 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1540 if (fn
->types
[j
] == uni
->type
) {
1549 case GL_TOP_LEVEL_ARRAY_SIZE
:
1550 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1551 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1554 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1555 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1556 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1559 /* GL_ARB_tessellation_shader */
1560 case GL_IS_PER_PATCH
:
1561 switch (res
->Type
) {
1562 case GL_PROGRAM_INPUT
:
1563 case GL_PROGRAM_OUTPUT
:
1564 *val
= RESOURCE_VAR(res
)->patch
;
1567 goto invalid_operation
;
1570 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1571 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1572 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1574 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1575 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1576 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1583 #undef VALIDATE_TYPE
1584 #undef VALIDATE_TYPE_2
1587 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1588 _mesa_enum_to_string(res
->Type
),
1589 _mesa_enum_to_string(prop
));
1593 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1594 _mesa_enum_to_string(res
->Type
),
1595 _mesa_enum_to_string(prop
));
1600 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1601 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1602 const GLenum
*props
, GLsizei bufSize
,
1603 GLsizei
*length
, GLint
*params
)
1605 GET_CURRENT_CONTEXT(ctx
);
1606 GLint
*val
= (GLint
*) params
;
1607 const GLenum
*prop
= props
;
1610 struct gl_program_resource
*res
=
1611 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1613 /* No such resource found or bufSize negative. */
1614 if (!res
|| bufSize
< 0) {
1615 _mesa_error(ctx
, GL_INVALID_VALUE
,
1616 "glGetProgramResourceiv(%s index %d bufSize %d)",
1617 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1621 /* Write propCount values until error occurs or bufSize reached. */
1622 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1624 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1625 "glGetProgramResourceiv");
1627 /* Error happened. */
1628 if (props_written
== 0)
1631 amount
+= props_written
;
1634 /* If <length> is not NULL, the actual number of integer values
1635 * written to <params> will be written to <length>.
1642 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1644 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1647 const bool producer_is_array_stage
=
1648 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1649 const bool consumer_is_array_stage
=
1650 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1651 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1652 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1656 gl_shader_variable
const **outputs
=
1657 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1658 sizeof(gl_shader_variable
*));
1659 if (outputs
== NULL
)
1662 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1665 * At an interface between program objects, the set of inputs and
1666 * outputs are considered to match exactly if and only if:
1668 * - Every declared input variable has a matching output, as described
1670 * - There are no user-defined output variables declared without a
1671 * matching input variable declaration.
1673 * Every input has an output, and every output has an input. Scan the list
1674 * of producer resources once, and generate the list of outputs. As inputs
1675 * and outputs are matched, remove the matched outputs from the set. At
1676 * the end, the set must be empty. If the set is not empty, then there is
1677 * some output that did not have an input.
1679 unsigned num_outputs
= 0;
1680 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1681 struct gl_program_resource
*res
=
1682 &producer
->sh
.data
->ProgramResourceList
[i
];
1684 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1687 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1689 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1692 * Built-in inputs or outputs do not affect interface matching.
1694 if (is_gl_identifier(var
->name
))
1697 outputs
[num_outputs
++] = var
;
1700 unsigned match_index
= 0;
1701 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1702 struct gl_program_resource
*res
=
1703 &consumer
->sh
.data
->ProgramResourceList
[i
];
1705 if (res
->Type
!= GL_PROGRAM_INPUT
)
1708 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1709 gl_shader_variable
const *producer_var
= NULL
;
1711 if (is_gl_identifier(consumer_var
->name
))
1714 /* Inputs with explicit locations match other outputs with explicit
1715 * locations by location instead of by name.
1717 if (consumer_var
->explicit_location
) {
1718 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1719 const gl_shader_variable
*const var
= outputs
[j
];
1721 if (var
->explicit_location
&&
1722 consumer_var
->location
== var
->location
) {
1729 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1730 const gl_shader_variable
*const var
= outputs
[j
];
1732 if (!var
->explicit_location
&&
1733 strcmp(consumer_var
->name
, var
->name
) == 0) {
1741 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1744 * - An output variable is considered to match an input variable in
1745 * the subsequent shader if:
1747 * - the two variables match in name, type, and qualification; or
1749 * - the two variables are declared with the same location
1750 * qualifier and match in type and qualification.
1752 if (producer_var
== NULL
) {
1757 /* An output cannot match more than one input, so remove the output from
1758 * the set of possible outputs.
1760 outputs
[match_index
] = NULL
;
1762 if (match_index
< num_outputs
)
1763 outputs
[match_index
] = outputs
[num_outputs
];
1765 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1767 * "Tessellation control shader per-vertex output variables and
1768 * blocks and tessellation control, tessellation evaluation, and
1769 * geometry shader per-vertex input variables and blocks are
1770 * required to be declared as arrays, with each element representing
1771 * input or output values for a single vertex of a multi-vertex
1772 * primitive. For the purposes of interface matching, such variables
1773 * and blocks are treated as though they were not declared as
1776 * So we unwrap those types before matching.
1778 const glsl_type
*consumer_type
= consumer_var
->type
;
1779 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1780 const glsl_type
*producer_type
= producer_var
->type
;
1781 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1783 if (consumer_is_array_stage
) {
1784 if (consumer_interface_type
) {
1785 /* the interface is the array; the underlying types should match */
1786 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1787 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1789 if (consumer_type
->is_array() && !consumer_var
->patch
)
1790 consumer_type
= consumer_type
->fields
.array
;
1794 if (producer_is_array_stage
) {
1795 if (producer_interface_type
) {
1796 /* the interface is the array; the underlying types should match */
1797 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1798 producer_interface_type
= producer_interface_type
->fields
.array
;
1800 if (producer_type
->is_array() && !producer_var
->patch
)
1801 producer_type
= producer_type
->fields
.array
;
1805 if (producer_type
!= consumer_type
) {
1810 if (producer_interface_type
!= consumer_interface_type
) {
1815 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1817 * Qualifier Class| Qualifier |in/out
1818 * ---------------+-------------+------
1822 * ---------------+-------------+------
1823 * Auxiliary | centroid | No
1824 * ---------------+-------------+------
1826 * | Block layout| N/A
1830 * ---------------+-------------+------
1831 * Interpolation | smooth |
1833 * ---------------+-------------+------
1835 * Precision | mediump | Yes
1837 * ---------------+-------------+------
1838 * Variance | invariant | No
1839 * ---------------+-------------+------
1840 * Memory | all | N/A
1842 * Note that location mismatches are detected by the loops above that
1843 * find the producer variable that goes with the consumer variable.
1845 unsigned producer_interpolation
= producer_var
->interpolation
;
1846 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1847 if (producer_interpolation
== INTERP_MODE_NONE
)
1848 producer_interpolation
= INTERP_MODE_SMOOTH
;
1849 if (consumer_interpolation
== INTERP_MODE_NONE
)
1850 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1851 if (producer_interpolation
!= consumer_interpolation
) {
1856 if (producer_var
->precision
!= consumer_var
->precision
) {
1861 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1869 return valid
&& num_outputs
== 0;
1873 * Validate inputs against outputs in a program pipeline.
1876 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1878 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1880 /* Find first active stage in pipeline. */
1881 unsigned idx
, prev
= 0;
1882 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1889 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1891 /* Pipeline might include both non-compute and a compute program, do
1892 * not attempt to validate varyings between non-compute and compute
1895 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1898 if (!validate_io(prog
[prev
], prog
[idx
]))
1908 _mesa_create_program_resource_hash(struct gl_shader_program
*shProg
)
1910 /* Rebuild resource hash. */
1911 if (shProg
->data
->ProgramResourceHash
)
1912 _mesa_hash_table_u64_destroy(shProg
->data
->ProgramResourceHash
, NULL
);
1914 shProg
->data
->ProgramResourceHash
= _mesa_hash_table_u64_create(shProg
);
1916 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
1917 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
1918 const char *name
= _mesa_program_resource_name(res
);
1920 uint32_t key
= compute_resource_key(res
->Type
, name
);
1921 _mesa_hash_table_u64_insert(shProg
->data
->ProgramResourceHash
, key
,