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
;
587 /* If we have a name, try the ProgramResourceHash first. */
588 if (shProg
->data
->ProgramResourceHash
)
589 res
= search_resource_hash(shProg
, programInterface
, name
, array_index
);
594 res
= shProg
->data
->ProgramResourceList
;
595 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
596 if (res
->Type
!= programInterface
)
599 /* Resource basename. */
600 const char *rname
= _mesa_program_resource_name(res
);
602 /* Since ARB_gl_spirv lack of name reflections is a possibility */
606 unsigned baselen
= strlen(rname
);
607 unsigned baselen_without_array_index
= baselen
;
608 const char *rname_last_square_bracket
= strrchr(rname
, '[');
610 bool rname_has_array_index_zero
= false;
611 /* From ARB_program_interface_query spec:
613 * "uint GetProgramResourceIndex(uint program, enum programInterface,
616 * If <name> exactly matches the name string of one of the active
617 * resources for <programInterface>, the index of the matched resource is
618 * returned. Additionally, if <name> would exactly match the name string
619 * of an active resource if "[0]" were appended to <name>, the index of
620 * the matched resource is returned. [...]"
622 * "A string provided to GetProgramResourceLocation or
623 * GetProgramResourceLocationIndex is considered to match an active variable
626 * * the string exactly matches the name of the active variable;
628 * * if the string identifies the base name of an active array, where the
629 * string would exactly match the name of the variable if the suffix
630 * "[0]" were appended to the string; [...]"
632 /* Remove array's index from interface block name comparison only if
633 * array's index is zero and the resulting string length is the same
634 * than the provided name's length.
636 if (rname_last_square_bracket
) {
637 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
638 rname_has_array_index_zero
=
639 (strcmp(rname_last_square_bracket
, "[0]") == 0) &&
640 (baselen_without_array_index
== strlen(name
));
643 if (strncmp(rname
, name
, baselen
) == 0)
645 else if (rname_has_array_index_zero
&&
646 strncmp(rname
, name
, baselen_without_array_index
) == 0)
650 switch (programInterface
) {
651 case GL_UNIFORM_BLOCK
:
652 case GL_SHADER_STORAGE_BLOCK
:
653 /* Basename match, check if array or struct. */
654 if (rname_has_array_index_zero
||
655 name
[baselen
] == '\0' ||
656 name
[baselen
] == '[' ||
657 name
[baselen
] == '.') {
661 case GL_TRANSFORM_FEEDBACK_VARYING
:
662 case GL_BUFFER_VARIABLE
:
664 case GL_VERTEX_SUBROUTINE_UNIFORM
:
665 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
666 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
667 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
668 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
669 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
670 case GL_VERTEX_SUBROUTINE
:
671 case GL_GEOMETRY_SUBROUTINE
:
672 case GL_FRAGMENT_SUBROUTINE
:
673 case GL_COMPUTE_SUBROUTINE
:
674 case GL_TESS_CONTROL_SUBROUTINE
:
675 case GL_TESS_EVALUATION_SUBROUTINE
:
676 if (name
[baselen
] == '.') {
680 case GL_PROGRAM_INPUT
:
681 case GL_PROGRAM_OUTPUT
:
682 if (name
[baselen
] == '\0') {
684 } else if (name
[baselen
] == '[' &&
685 valid_array_index(name
, array_index
)) {
690 assert(!"not implemented for given interface");
697 /* Find an uniform or buffer variable program resource with an specific offset
698 * inside a block with an specific binding.
700 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
702 static struct gl_program_resource
*
703 program_resource_find_binding_offset(struct gl_shader_program
*shProg
,
704 GLenum programInterface
,
705 const GLuint binding
,
709 /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
710 GLenum blockInterface
;
712 switch (programInterface
) {
713 case GL_BUFFER_VARIABLE
:
714 blockInterface
= GL_SHADER_STORAGE_BLOCK
;
717 blockInterface
= GL_UNIFORM_BLOCK
;
720 assert("Invalid program interface");
724 int block_index
= -1;
725 int starting_index
= -1;
726 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
728 /* Blocks are added to the resource list in the same order that they are
729 * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
730 * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
733 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
734 if (res
->Type
!= blockInterface
)
737 /* Store the first index where a resource of the specific interface is. */
738 if (starting_index
== -1)
741 const struct gl_uniform_block
*block
= RESOURCE_UBO(res
);
743 if (block
->Binding
== binding
) {
744 /* For arrays, or arrays of arrays of blocks, we want the resource
745 * for the block with base index. Most properties for members of each
746 * block are inherited from the block with the base index, including
747 * a uniform being active or not.
749 block_index
= i
- starting_index
- block
->linearized_array_index
;
754 if (block_index
== -1)
757 /* We now look for the resource corresponding to the uniform or buffer
758 * variable using the BLOCK_INDEX and OFFSET.
760 res
= shProg
->data
->ProgramResourceList
;
761 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
762 if (res
->Type
!= programInterface
)
765 const struct gl_uniform_storage
*uniform
= RESOURCE_UNI(res
);
767 if (uniform
->block_index
== block_index
&& uniform
->offset
== offset
) {
775 /* Checks if an uniform or buffer variable is in the active program resource
778 * It takes into accout that for variables coming from SPIR-V binaries their
779 * names could not be available (ARB_gl_spirv). In that case, it will use the
780 * the offset and the block binding to locate the resource.
782 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
784 struct gl_program_resource
*
785 _mesa_program_resource_find_active_variable(struct gl_shader_program
*shProg
,
786 GLenum programInterface
,
787 const gl_uniform_block
*block
,
790 struct gl_program_resource
*res
;
791 struct gl_uniform_buffer_variable uni
= block
->Uniforms
[index
];
793 assert(programInterface
== GL_UNIFORM
||
794 programInterface
== GL_BUFFER_VARIABLE
);
797 res
= _mesa_program_resource_find_name(shProg
, programInterface
, uni
.IndexName
,
800 /* As the resource has no associated name (ARB_gl_spirv),
801 * we can use the UBO/SSBO binding and offset to find it.
803 res
= program_resource_find_binding_offset(shProg
, programInterface
,
804 block
->Binding
, uni
.Offset
);
811 calc_resource_index(struct gl_shader_program
*shProg
,
812 struct gl_program_resource
*res
)
816 for (i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++) {
817 if (&shProg
->data
->ProgramResourceList
[i
] == res
)
819 if (shProg
->data
->ProgramResourceList
[i
].Type
== res
->Type
)
822 return GL_INVALID_INDEX
;
826 * Calculate index for the given resource.
829 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
830 struct gl_program_resource
*res
)
833 return GL_INVALID_INDEX
;
836 case GL_ATOMIC_COUNTER_BUFFER
:
837 return RESOURCE_ATC(res
) - shProg
->data
->AtomicBuffers
;
838 case GL_VERTEX_SUBROUTINE
:
839 case GL_GEOMETRY_SUBROUTINE
:
840 case GL_FRAGMENT_SUBROUTINE
:
841 case GL_COMPUTE_SUBROUTINE
:
842 case GL_TESS_CONTROL_SUBROUTINE
:
843 case GL_TESS_EVALUATION_SUBROUTINE
:
844 return RESOURCE_SUB(res
)->index
;
845 case GL_UNIFORM_BLOCK
:
846 case GL_SHADER_STORAGE_BLOCK
:
847 case GL_TRANSFORM_FEEDBACK_BUFFER
:
848 case GL_TRANSFORM_FEEDBACK_VARYING
:
850 return calc_resource_index(shProg
, res
);
855 * Find a program resource that points to given data.
857 static struct gl_program_resource
*
858 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
860 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
861 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
863 if (res
->Data
== data
)
869 /* Find a program resource with specific index in given interface.
871 struct gl_program_resource
*
872 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
873 GLenum programInterface
, GLuint index
)
875 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
878 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
;
880 if (res
->Type
!= programInterface
)
884 case GL_UNIFORM_BLOCK
:
885 case GL_ATOMIC_COUNTER_BUFFER
:
886 case GL_SHADER_STORAGE_BLOCK
:
887 case GL_TRANSFORM_FEEDBACK_BUFFER
:
888 if (_mesa_program_resource_index(shProg
, res
) == index
)
891 case GL_TRANSFORM_FEEDBACK_VARYING
:
892 case GL_PROGRAM_INPUT
:
893 case GL_PROGRAM_OUTPUT
:
895 case GL_VERTEX_SUBROUTINE_UNIFORM
:
896 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
897 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
898 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
899 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
900 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
901 case GL_VERTEX_SUBROUTINE
:
902 case GL_GEOMETRY_SUBROUTINE
:
903 case GL_FRAGMENT_SUBROUTINE
:
904 case GL_COMPUTE_SUBROUTINE
:
905 case GL_TESS_CONTROL_SUBROUTINE
:
906 case GL_TESS_EVALUATION_SUBROUTINE
:
907 case GL_BUFFER_VARIABLE
:
908 if (++idx
== (int) index
)
912 assert(!"not implemented for given interface");
918 /* Function returns if resource name is expected to have index
922 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
925 * "If the active uniform is an array, the uniform name returned in
926 * name will always be the name of the uniform array appended with
929 * The same text also appears in the OpenGL 4.2 spec. It does not,
930 * however, appear in any previous spec. Previous specifications are
931 * ambiguous in this regard. However, either name can later be passed
932 * to glGetUniformLocation (and related APIs), so there shouldn't be any
933 * harm in always appending "[0]" to uniform array names.
936 add_index_to_name(struct gl_program_resource
*res
)
938 /* Transform feedback varyings have array index already appended
941 return res
->Type
!= GL_TRANSFORM_FEEDBACK_VARYING
;
944 /* Get name length of a program resource. This consists of
945 * base name + 3 for '[0]' if resource is an array.
948 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
950 const char* name
= _mesa_program_resource_name(res
);
952 /* For shaders constructed from SPIR-V binaries, variables may not
953 * have names associated with them.
958 unsigned length
= strlen(name
);
959 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
964 /* Get full name of a program resource.
967 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
968 GLenum programInterface
, GLuint index
,
969 GLsizei bufSize
, GLsizei
*length
,
970 GLchar
*name
, const char *caller
)
972 GET_CURRENT_CONTEXT(ctx
);
974 /* Find resource with given interface and index. */
975 struct gl_program_resource
*res
=
976 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
978 /* The error INVALID_VALUE is generated if <index> is greater than
979 * or equal to the number of entries in the active resource list for
980 * <programInterface>.
983 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
988 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
995 length
= &localLength
;
997 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
999 /* The resource name can be NULL for shaders constructed from SPIR-V
1000 * binaries. In that case, we do not add the '[0]'.
1002 if (name
&& name
[0] != '\0' &&
1003 _mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
1006 /* The comparison is strange because *length does *NOT* include the
1007 * terminating NUL, but maxLength does.
1009 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
1010 name
[*length
+ i
] = "[0]"[i
];
1012 name
[*length
+ i
] = '\0';
1019 program_resource_location(struct gl_program_resource
*res
, unsigned array_index
)
1021 switch (res
->Type
) {
1022 case GL_PROGRAM_INPUT
: {
1023 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
1025 if (var
->location
== -1)
1028 /* If the input is an array, fail if the index is out of bounds. */
1030 && array_index
>= var
->type
->length
) {
1033 return var
->location
+
1034 (array_index
* var
->type
->without_array()->matrix_columns
);
1036 case GL_PROGRAM_OUTPUT
:
1037 if (RESOURCE_VAR(res
)->location
== -1)
1040 /* If the output is an array, fail if the index is out of bounds. */
1042 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
1045 return RESOURCE_VAR(res
)->location
+ array_index
;
1047 /* If the uniform is built-in, fail. */
1048 if (RESOURCE_UNI(res
)->builtin
)
1051 /* From page 79 of the OpenGL 4.2 spec:
1053 * "A valid name cannot be a structure, an array of structures, or any
1054 * portion of a single vector or a matrix."
1056 if (RESOURCE_UNI(res
)->type
->without_array()->is_struct())
1059 /* From the GL_ARB_uniform_buffer_object spec:
1061 * "The value -1 will be returned if <name> does not correspond to an
1062 * active uniform variable name in <program>, if <name> is associated
1063 * with a named uniform block, or if <name> starts with the reserved
1066 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
1067 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
1071 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1072 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1073 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1074 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1075 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1076 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1077 /* If the uniform is an array, fail if the index is out of bounds. */
1079 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
1083 /* location in remap table + array element offset */
1084 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
1091 * Function implements following location queries:
1092 * glGetUniformLocation
1095 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
1096 GLenum programInterface
, const char *name
)
1098 unsigned array_index
= 0;
1099 struct gl_program_resource
*res
=
1100 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
1103 /* Resource not found. */
1107 return program_resource_location(res
, array_index
);
1111 _get_resource_location_index(struct gl_program_resource
*res
)
1113 /* Non-existent variable or resource is not referenced by fragment stage. */
1114 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
1117 /* From OpenGL 4.5 spec, 7.3 Program Objects
1118 * "The value -1 will be returned by either command...
1119 * ... or if name identifies an active variable that does not have a
1120 * valid location assigned.
1122 if (RESOURCE_VAR(res
)->location
== -1)
1124 return RESOURCE_VAR(res
)->index
;
1128 * Function implements following index queries:
1129 * glGetFragDataIndex
1132 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
1133 GLenum programInterface
, const char *name
)
1135 struct gl_program_resource
*res
=
1136 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
1138 return _get_resource_location_index(res
);
1142 stage_from_enum(GLenum ref
)
1145 case GL_REFERENCED_BY_VERTEX_SHADER
:
1146 return MESA_SHADER_VERTEX
;
1147 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1148 return MESA_SHADER_TESS_CTRL
;
1149 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1150 return MESA_SHADER_TESS_EVAL
;
1151 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1152 return MESA_SHADER_GEOMETRY
;
1153 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1154 return MESA_SHADER_FRAGMENT
;
1155 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1156 return MESA_SHADER_COMPUTE
;
1158 assert(!"shader stage not supported");
1159 return MESA_SHADER_STAGES
;
1164 * Check if resource is referenced by given 'referenced by' stage enum.
1165 * ATC and UBO resources hold stage references of their own.
1168 is_resource_referenced(struct gl_shader_program
*shProg
,
1169 struct gl_program_resource
*res
,
1170 GLuint index
, uint8_t stage
)
1172 /* First, check if we even have such a stage active. */
1173 if (!shProg
->_LinkedShaders
[stage
])
1176 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
1177 return RESOURCE_ATC(res
)->StageReferences
[stage
];
1179 if (res
->Type
== GL_UNIFORM_BLOCK
)
1180 return shProg
->data
->UniformBlocks
[index
].stageref
& (1 << stage
);
1182 if (res
->Type
== GL_SHADER_STORAGE_BLOCK
)
1183 return shProg
->data
->ShaderStorageBlocks
[index
].stageref
& (1 << stage
);
1185 return res
->StageReferences
& (1 << stage
);
1189 get_buffer_property(struct gl_shader_program
*shProg
,
1190 struct gl_program_resource
*res
, const GLenum prop
,
1191 GLint
*val
, const char *caller
)
1193 GET_CURRENT_CONTEXT(ctx
);
1194 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1195 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1196 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1197 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1198 goto invalid_operation
;
1200 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1202 case GL_BUFFER_BINDING
:
1203 *val
= RESOURCE_UBO(res
)->Binding
;
1205 case GL_BUFFER_DATA_SIZE
:
1206 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1208 case GL_NUM_ACTIVE_VARIABLES
:
1210 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1211 struct gl_program_resource
*uni
=
1212 _mesa_program_resource_find_active_variable(
1223 case GL_ACTIVE_VARIABLES
: {
1224 unsigned num_values
= 0;
1225 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1226 struct gl_program_resource
*uni
=
1227 _mesa_program_resource_find_active_variable(
1236 _mesa_program_resource_index(shProg
, uni
);
1242 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1244 case GL_BUFFER_BINDING
:
1245 *val
= RESOURCE_UBO(res
)->Binding
;
1247 case GL_BUFFER_DATA_SIZE
:
1248 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1250 case GL_NUM_ACTIVE_VARIABLES
:
1252 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1253 struct gl_program_resource
*uni
=
1254 _mesa_program_resource_find_active_variable(
1265 case GL_ACTIVE_VARIABLES
: {
1266 unsigned num_values
= 0;
1267 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1268 struct gl_program_resource
*uni
=
1269 _mesa_program_resource_find_active_variable(
1278 _mesa_program_resource_index(shProg
, uni
);
1284 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1286 case GL_BUFFER_BINDING
:
1287 *val
= RESOURCE_ATC(res
)->Binding
;
1289 case GL_BUFFER_DATA_SIZE
:
1290 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1292 case GL_NUM_ACTIVE_VARIABLES
:
1293 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1295 case GL_ACTIVE_VARIABLES
:
1296 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1297 /* Active atomic buffer contains index to UniformStorage. Find
1298 * out gl_program_resource via data pointer and then calculate
1299 * index of that uniform.
1301 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1302 struct gl_program_resource
*uni
=
1303 program_resource_find_data(shProg
,
1304 &shProg
->data
->UniformStorage
[idx
]);
1306 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1308 return RESOURCE_ATC(res
)->NumUniforms
;
1310 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1312 case GL_BUFFER_BINDING
:
1313 *val
= RESOURCE_XFB(res
)->Binding
;
1315 case GL_NUM_ACTIVE_VARIABLES
:
1316 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1318 case GL_ACTIVE_VARIABLES
:
1319 struct gl_transform_feedback_info
*linked_xfb
=
1320 shProg
->last_vert_prog
->sh
.LinkedTransformFeedback
;
1321 for (int i
= 0; i
< linked_xfb
->NumVarying
; i
++) {
1322 unsigned index
= linked_xfb
->Varyings
[i
].BufferIndex
;
1323 struct gl_program_resource
*buf_res
=
1324 _mesa_program_resource_find_index(shProg
,
1325 GL_TRANSFORM_FEEDBACK_BUFFER
,
1328 if (res
== buf_res
) {
1332 return RESOURCE_XFB(res
)->NumVaryings
;
1335 assert(!"support for property type not implemented");
1338 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1339 _mesa_enum_to_string(res
->Type
),
1340 _mesa_enum_to_string(prop
));
1346 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1347 struct gl_program_resource
*res
, GLuint index
,
1348 const GLenum prop
, GLint
*val
, const char *caller
)
1350 GET_CURRENT_CONTEXT(ctx
);
1352 #define VALIDATE_TYPE(type)\
1353 if (res->Type != type)\
1354 goto invalid_operation;
1356 #define VALIDATE_TYPE_2(type1, type2)\
1357 if (res->Type != type1 && res->Type != type2)\
1358 goto invalid_operation;
1361 case GL_NAME_LENGTH
:
1362 switch (res
->Type
) {
1363 case GL_ATOMIC_COUNTER_BUFFER
:
1364 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1365 goto invalid_operation
;
1367 /* Resource name length + terminator. */
1368 *val
= _mesa_program_resource_name_len(res
) + 1;
1372 switch (res
->Type
) {
1374 case GL_BUFFER_VARIABLE
:
1375 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1377 case GL_PROGRAM_INPUT
:
1378 case GL_PROGRAM_OUTPUT
:
1379 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1381 case GL_TRANSFORM_FEEDBACK_VARYING
:
1382 *val
= RESOURCE_XFV(res
)->Type
;
1385 goto invalid_operation
;
1388 switch (res
->Type
) {
1390 case GL_BUFFER_VARIABLE
:
1391 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1392 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1393 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1394 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1395 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1396 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1398 /* Test if a buffer variable is an array or an unsized array.
1399 * Unsized arrays return zero as array size.
1401 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1402 RESOURCE_UNI(res
)->array_stride
> 0)
1403 *val
= RESOURCE_UNI(res
)->array_elements
;
1405 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1407 case GL_PROGRAM_INPUT
:
1408 case GL_PROGRAM_OUTPUT
:
1409 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1411 case GL_TRANSFORM_FEEDBACK_VARYING
:
1412 *val
= RESOURCE_XFV(res
)->Size
;
1415 goto invalid_operation
;
1418 switch (res
->Type
) {
1420 case GL_BUFFER_VARIABLE
:
1421 *val
= RESOURCE_UNI(res
)->offset
;
1423 case GL_TRANSFORM_FEEDBACK_VARYING
:
1424 *val
= RESOURCE_XFV(res
)->Offset
;
1427 goto invalid_operation
;
1429 case GL_BLOCK_INDEX
:
1430 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1431 *val
= RESOURCE_UNI(res
)->block_index
;
1433 case GL_ARRAY_STRIDE
:
1434 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1435 *val
= RESOURCE_UNI(res
)->array_stride
;
1437 case GL_MATRIX_STRIDE
:
1438 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1439 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1441 case GL_IS_ROW_MAJOR
:
1442 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1443 *val
= RESOURCE_UNI(res
)->row_major
;
1445 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1446 VALIDATE_TYPE(GL_UNIFORM
);
1447 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1449 case GL_BUFFER_BINDING
:
1450 case GL_BUFFER_DATA_SIZE
:
1451 case GL_NUM_ACTIVE_VARIABLES
:
1452 case GL_ACTIVE_VARIABLES
:
1453 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1454 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1455 if (!_mesa_has_compute_shaders(ctx
))
1458 case GL_REFERENCED_BY_VERTEX_SHADER
:
1459 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1460 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1461 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1462 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1463 switch (res
->Type
) {
1465 case GL_PROGRAM_INPUT
:
1466 case GL_PROGRAM_OUTPUT
:
1467 case GL_UNIFORM_BLOCK
:
1468 case GL_BUFFER_VARIABLE
:
1469 case GL_SHADER_STORAGE_BLOCK
:
1470 case GL_ATOMIC_COUNTER_BUFFER
:
1471 *val
= is_resource_referenced(shProg
, res
, index
,
1472 stage_from_enum(prop
));
1475 goto invalid_operation
;
1478 switch (res
->Type
) {
1480 case GL_VERTEX_SUBROUTINE_UNIFORM
:
1481 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
1482 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
1483 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
1484 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
1485 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
1486 case GL_PROGRAM_INPUT
:
1487 case GL_PROGRAM_OUTPUT
:
1488 *val
= program_resource_location(res
, 0);
1491 goto invalid_operation
;
1493 case GL_LOCATION_COMPONENT
:
1494 switch (res
->Type
) {
1495 case GL_PROGRAM_INPUT
:
1496 case GL_PROGRAM_OUTPUT
:
1497 *val
= RESOURCE_VAR(res
)->component
;
1500 goto invalid_operation
;
1502 case GL_LOCATION_INDEX
: {
1504 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1505 goto invalid_operation
;
1506 tmp
= program_resource_location(res
, 0);
1510 *val
= _get_resource_location_index(res
);
1513 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1514 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1515 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1516 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1517 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1518 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1519 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1520 goto invalid_operation
;
1521 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1523 case GL_COMPATIBLE_SUBROUTINES
: {
1524 const struct gl_uniform_storage
*uni
;
1525 struct gl_program
*p
;
1529 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1530 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1531 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1532 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1533 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1534 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1535 goto invalid_operation
;
1536 uni
= RESOURCE_UNI(res
);
1538 p
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)]->Program
;
1540 for (i
= 0; i
< p
->sh
.NumSubroutineFunctions
; i
++) {
1541 struct gl_subroutine_function
*fn
= &p
->sh
.SubroutineFunctions
[i
];
1542 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1543 if (fn
->types
[j
] == uni
->type
) {
1552 case GL_TOP_LEVEL_ARRAY_SIZE
:
1553 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1554 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1557 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1558 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1559 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1562 /* GL_ARB_tessellation_shader */
1563 case GL_IS_PER_PATCH
:
1564 switch (res
->Type
) {
1565 case GL_PROGRAM_INPUT
:
1566 case GL_PROGRAM_OUTPUT
:
1567 *val
= RESOURCE_VAR(res
)->patch
;
1570 goto invalid_operation
;
1573 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1574 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1575 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1577 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1578 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1579 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1586 #undef VALIDATE_TYPE
1587 #undef VALIDATE_TYPE_2
1590 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1591 _mesa_enum_to_string(res
->Type
),
1592 _mesa_enum_to_string(prop
));
1596 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1597 _mesa_enum_to_string(res
->Type
),
1598 _mesa_enum_to_string(prop
));
1603 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1604 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1605 const GLenum
*props
, GLsizei bufSize
,
1606 GLsizei
*length
, GLint
*params
)
1608 GET_CURRENT_CONTEXT(ctx
);
1609 GLint
*val
= (GLint
*) params
;
1610 const GLenum
*prop
= props
;
1613 struct gl_program_resource
*res
=
1614 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1616 /* No such resource found or bufSize negative. */
1617 if (!res
|| bufSize
< 0) {
1618 _mesa_error(ctx
, GL_INVALID_VALUE
,
1619 "glGetProgramResourceiv(%s index %d bufSize %d)",
1620 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1624 /* Write propCount values until error occurs or bufSize reached. */
1625 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1627 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1628 "glGetProgramResourceiv");
1630 /* Error happened. */
1631 if (props_written
== 0)
1634 amount
+= props_written
;
1637 /* If <length> is not NULL, the actual number of integer values
1638 * written to <params> will be written to <length>.
1645 validate_io(struct gl_program
*producer
, struct gl_program
*consumer
)
1647 if (producer
->sh
.data
->linked_stages
== consumer
->sh
.data
->linked_stages
)
1650 const bool producer_is_array_stage
=
1651 producer
->info
.stage
== MESA_SHADER_TESS_CTRL
;
1652 const bool consumer_is_array_stage
=
1653 consumer
->info
.stage
== MESA_SHADER_GEOMETRY
||
1654 consumer
->info
.stage
== MESA_SHADER_TESS_CTRL
||
1655 consumer
->info
.stage
== MESA_SHADER_TESS_EVAL
;
1659 gl_shader_variable
const **outputs
=
1660 (gl_shader_variable
const **) calloc(producer
->sh
.data
->NumProgramResourceList
,
1661 sizeof(gl_shader_variable
*));
1662 if (outputs
== NULL
)
1665 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1668 * At an interface between program objects, the set of inputs and
1669 * outputs are considered to match exactly if and only if:
1671 * - Every declared input variable has a matching output, as described
1673 * - There are no user-defined output variables declared without a
1674 * matching input variable declaration.
1676 * Every input has an output, and every output has an input. Scan the list
1677 * of producer resources once, and generate the list of outputs. As inputs
1678 * and outputs are matched, remove the matched outputs from the set. At
1679 * the end, the set must be empty. If the set is not empty, then there is
1680 * some output that did not have an input.
1682 unsigned num_outputs
= 0;
1683 for (unsigned i
= 0; i
< producer
->sh
.data
->NumProgramResourceList
; i
++) {
1684 struct gl_program_resource
*res
=
1685 &producer
->sh
.data
->ProgramResourceList
[i
];
1687 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1690 gl_shader_variable
const *const var
= RESOURCE_VAR(res
);
1692 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1695 * Built-in inputs or outputs do not affect interface matching.
1697 if (is_gl_identifier(var
->name
))
1700 outputs
[num_outputs
++] = var
;
1703 unsigned match_index
= 0;
1704 for (unsigned i
= 0; i
< consumer
->sh
.data
->NumProgramResourceList
; i
++) {
1705 struct gl_program_resource
*res
=
1706 &consumer
->sh
.data
->ProgramResourceList
[i
];
1708 if (res
->Type
!= GL_PROGRAM_INPUT
)
1711 gl_shader_variable
const *const consumer_var
= RESOURCE_VAR(res
);
1712 gl_shader_variable
const *producer_var
= NULL
;
1714 if (is_gl_identifier(consumer_var
->name
))
1717 /* Inputs with explicit locations match other outputs with explicit
1718 * locations by location instead of by name.
1720 if (consumer_var
->explicit_location
) {
1721 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1722 const gl_shader_variable
*const var
= outputs
[j
];
1724 if (var
->explicit_location
&&
1725 consumer_var
->location
== var
->location
) {
1732 for (unsigned j
= 0; j
< num_outputs
; j
++) {
1733 const gl_shader_variable
*const var
= outputs
[j
];
1735 if (!var
->explicit_location
&&
1736 strcmp(consumer_var
->name
, var
->name
) == 0) {
1744 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1747 * - An output variable is considered to match an input variable in
1748 * the subsequent shader if:
1750 * - the two variables match in name, type, and qualification; or
1752 * - the two variables are declared with the same location
1753 * qualifier and match in type and qualification.
1755 if (producer_var
== NULL
) {
1760 /* An output cannot match more than one input, so remove the output from
1761 * the set of possible outputs.
1763 outputs
[match_index
] = NULL
;
1765 if (match_index
< num_outputs
)
1766 outputs
[match_index
] = outputs
[num_outputs
];
1768 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1770 * "Tessellation control shader per-vertex output variables and
1771 * blocks and tessellation control, tessellation evaluation, and
1772 * geometry shader per-vertex input variables and blocks are
1773 * required to be declared as arrays, with each element representing
1774 * input or output values for a single vertex of a multi-vertex
1775 * primitive. For the purposes of interface matching, such variables
1776 * and blocks are treated as though they were not declared as
1779 * So we unwrap those types before matching.
1781 const glsl_type
*consumer_type
= consumer_var
->type
;
1782 const glsl_type
*consumer_interface_type
= consumer_var
->interface_type
;
1783 const glsl_type
*producer_type
= producer_var
->type
;
1784 const glsl_type
*producer_interface_type
= producer_var
->interface_type
;
1786 if (consumer_is_array_stage
) {
1787 if (consumer_interface_type
) {
1788 /* the interface is the array; the underlying types should match */
1789 if (consumer_interface_type
->is_array() && !consumer_var
->patch
)
1790 consumer_interface_type
= consumer_interface_type
->fields
.array
;
1792 if (consumer_type
->is_array() && !consumer_var
->patch
)
1793 consumer_type
= consumer_type
->fields
.array
;
1797 if (producer_is_array_stage
) {
1798 if (producer_interface_type
) {
1799 /* the interface is the array; the underlying types should match */
1800 if (producer_interface_type
->is_array() && !producer_var
->patch
)
1801 producer_interface_type
= producer_interface_type
->fields
.array
;
1803 if (producer_type
->is_array() && !producer_var
->patch
)
1804 producer_type
= producer_type
->fields
.array
;
1808 if (producer_type
!= consumer_type
) {
1813 if (producer_interface_type
!= consumer_interface_type
) {
1818 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1820 * Qualifier Class| Qualifier |in/out
1821 * ---------------+-------------+------
1825 * ---------------+-------------+------
1826 * Auxiliary | centroid | No
1827 * ---------------+-------------+------
1829 * | Block layout| N/A
1833 * ---------------+-------------+------
1834 * Interpolation | smooth |
1836 * ---------------+-------------+------
1838 * Precision | mediump | Yes
1840 * ---------------+-------------+------
1841 * Variance | invariant | No
1842 * ---------------+-------------+------
1843 * Memory | all | N/A
1845 * Note that location mismatches are detected by the loops above that
1846 * find the producer variable that goes with the consumer variable.
1848 unsigned producer_interpolation
= producer_var
->interpolation
;
1849 unsigned consumer_interpolation
= consumer_var
->interpolation
;
1850 if (producer_interpolation
== INTERP_MODE_NONE
)
1851 producer_interpolation
= INTERP_MODE_SMOOTH
;
1852 if (consumer_interpolation
== INTERP_MODE_NONE
)
1853 consumer_interpolation
= INTERP_MODE_SMOOTH
;
1854 if (producer_interpolation
!= consumer_interpolation
) {
1859 if (producer_var
->precision
!= consumer_var
->precision
) {
1864 if (producer_var
->outermost_struct_type
!= consumer_var
->outermost_struct_type
) {
1872 return valid
&& num_outputs
== 0;
1876 * Validate inputs against outputs in a program pipeline.
1879 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1881 struct gl_program
**prog
= (struct gl_program
**) pipeline
->CurrentProgram
;
1883 /* Find first active stage in pipeline. */
1884 unsigned idx
, prev
= 0;
1885 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1892 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1894 /* Pipeline might include both non-compute and a compute program, do
1895 * not attempt to validate varyings between non-compute and compute
1898 if (prog
[idx
]->info
.stage
== MESA_SHADER_COMPUTE
)
1901 if (!validate_io(prog
[prev
], prog
[idx
]))
1911 _mesa_create_program_resource_hash(struct gl_shader_program
*shProg
)
1913 /* Rebuild resource hash. */
1914 if (shProg
->data
->ProgramResourceHash
)
1915 _mesa_hash_table_u64_destroy(shProg
->data
->ProgramResourceHash
, NULL
);
1917 shProg
->data
->ProgramResourceHash
= _mesa_hash_table_u64_create(shProg
);
1919 struct gl_program_resource
*res
= shProg
->data
->ProgramResourceList
;
1920 for (unsigned i
= 0; i
< shProg
->data
->NumProgramResourceList
; i
++, res
++) {
1921 const char *name
= _mesa_program_resource_name(res
);
1923 uint32_t key
= compute_resource_key(res
->Type
, name
);
1924 _mesa_hash_table_u64_insert(shProg
->data
->ProgramResourceHash
, key
,