2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/context.h"
32 #include "main/core.h"
33 #include "main/enums.h"
34 #include "main/shaderapi.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "glsl/glsl_symbol_table.h"
39 #include "glsl/program.h"
40 #include "program/hash_table.h"
41 #include "util/strndup.h"
45 program_resource_location(struct gl_shader_program
*shProg
,
46 struct gl_program_resource
*res
, const char *name
,
47 unsigned array_index
);
50 * Declare convenience functions to return resource data in a given type.
51 * Warning! this is not type safe so be *very* careful when using these.
53 #define DECL_RESOURCE_FUNC(name, type) \
54 const type * RESOURCE_ ## name (gl_program_resource *res) { \
56 return (type *) res->Data; \
59 DECL_RESOURCE_FUNC(VAR
, ir_variable
);
60 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
61 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
62 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
63 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_varying_info
);
64 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
67 _mesa_BindAttribLocation(GLhandleARB program
, GLuint index
,
68 const GLcharARB
*name
)
70 GET_CURRENT_CONTEXT(ctx
);
72 struct gl_shader_program
*const shProg
=
73 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
80 if (strncmp(name
, "gl_", 3) == 0) {
81 _mesa_error(ctx
, GL_INVALID_OPERATION
,
82 "glBindAttribLocation(illegal name)");
86 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
87 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
91 /* Replace the current value if it's already in the list. Add
92 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
93 * between built-in attributes and user-defined attributes.
95 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
98 * Note that this attribute binding won't go into effect until
99 * glLinkProgram is called again.
104 is_active_attrib(const ir_variable
*var
)
109 switch (var
->data
.mode
) {
110 case ir_var_shader_in
:
111 return var
->data
.location
!= -1;
113 case ir_var_system_value
:
114 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
115 * "For GetActiveAttrib, all active vertex shader input variables
116 * are enumerated, including the special built-in inputs gl_VertexID
117 * and gl_InstanceID."
119 return var
->data
.location
== SYSTEM_VALUE_VERTEX_ID
||
120 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
||
121 var
->data
.location
== SYSTEM_VALUE_INSTANCE_ID
;
129 _mesa_GetActiveAttrib(GLhandleARB program
, GLuint desired_index
,
130 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
131 GLenum
* type
, GLcharARB
* name
)
133 GET_CURRENT_CONTEXT(ctx
);
134 struct gl_shader_program
*shProg
;
137 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
141 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
145 if (!shProg
->LinkStatus
) {
146 _mesa_error(ctx
, GL_INVALID_VALUE
,
147 "glGetActiveAttrib(program not linked)");
151 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
152 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
156 struct gl_program_resource
*res
=
157 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
160 /* User asked for index that does not exist. */
162 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
166 const ir_variable
*const var
= RESOURCE_VAR(res
);
168 if (!is_active_attrib(var
))
171 const char *var_name
= var
->name
;
173 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
174 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
177 if (var
->data
.mode
== ir_var_system_value
&&
178 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
179 var_name
= "gl_VertexID";
182 _mesa_copy_string(name
, maxLength
, length
, var_name
);
185 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
186 size
, "glGetActiveAttrib");
189 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
190 (GLint
*) type
, "glGetActiveAttrib");
194 _mesa_GetAttribLocation(GLhandleARB program
, const GLcharARB
* name
)
196 GET_CURRENT_CONTEXT(ctx
);
197 struct gl_shader_program
*const shProg
=
198 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
204 if (!shProg
->LinkStatus
) {
205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
206 "glGetAttribLocation(program not linked)");
213 /* Not having a vertex shader is not an error.
215 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
218 unsigned array_index
= 0;
219 struct gl_program_resource
*res
=
220 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
226 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
228 /* The extra check against against 0 is made because of builtin-attribute
229 * locations that have offset applied. Function program_resource_location
230 * can return built-in attribute locations < 0 and glGetAttribLocation
231 * cannot be used on "conventional" attributes.
233 * From page 95 of the OpenGL 3.0 spec:
235 * "If name is not an active attribute, if name is a conventional
236 * attribute, or if an error occurs, -1 will be returned."
238 return (loc
>= 0) ? loc
: -1;
242 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
244 if (!shProg
->LinkStatus
245 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
249 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
251 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
252 if (res
->Type
== GL_PROGRAM_INPUT
&&
253 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
) &&
254 is_active_attrib(RESOURCE_VAR(res
)))
262 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
264 if (!shProg
->LinkStatus
265 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
269 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
271 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
272 if (res
->Type
== GL_PROGRAM_INPUT
&&
273 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
275 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
276 if (length
>= longest
)
277 longest
= length
+ 1;
285 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
288 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
292 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
293 GLuint index
, const GLchar
*name
)
295 GET_CURRENT_CONTEXT(ctx
);
297 struct gl_shader_program
*const shProg
=
298 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
305 if (strncmp(name
, "gl_", 3) == 0) {
306 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
311 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
315 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
316 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
320 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
321 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
325 /* Replace the current value if it's already in the list. Add
326 * FRAG_RESULT_DATA0 because that's how the linker differentiates
327 * between built-in attributes and user-defined attributes.
329 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
330 shProg
->FragDataIndexBindings
->put(index
, name
);
332 * Note that this binding won't go into effect until
333 * glLinkProgram is called again.
339 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
341 GET_CURRENT_CONTEXT(ctx
);
342 struct gl_shader_program
*const shProg
=
343 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
349 if (!shProg
->LinkStatus
) {
350 _mesa_error(ctx
, GL_INVALID_OPERATION
,
351 "glGetFragDataIndex(program not linked)");
358 if (strncmp(name
, "gl_", 3) == 0) {
359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
360 "glGetFragDataIndex(illegal name)");
364 /* Not having a fragment shader is not an error.
366 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
369 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
374 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
376 GET_CURRENT_CONTEXT(ctx
);
377 struct gl_shader_program
*const shProg
=
378 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
384 if (!shProg
->LinkStatus
) {
385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
386 "glGetFragDataLocation(program not linked)");
393 if (strncmp(name
, "gl_", 3) == 0) {
394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
395 "glGetFragDataLocation(illegal name)");
399 /* Not having a fragment shader is not an error.
401 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
404 unsigned array_index
= 0;
405 struct gl_program_resource
*res
=
406 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
412 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
414 /* The extra check against against 0 is made because of builtin-attribute
415 * locations that have offset applied. Function program_resource_location
416 * can return built-in attribute locations < 0 and glGetFragDataLocation
417 * cannot be used on "conventional" attributes.
419 * From page 95 of the OpenGL 3.0 spec:
421 * "If name is not an active attribute, if name is a conventional
422 * attribute, or if an error occurs, -1 will be returned."
424 return (loc
>= 0) ? loc
: -1;
428 _mesa_program_resource_name(struct gl_program_resource
*res
)
430 const ir_variable
*var
;
432 case GL_UNIFORM_BLOCK
:
433 case GL_SHADER_STORAGE_BLOCK
:
434 return RESOURCE_UBO(res
)->Name
;
435 case GL_TRANSFORM_FEEDBACK_VARYING
:
436 return RESOURCE_XFB(res
)->Name
;
437 case GL_PROGRAM_INPUT
:
438 var
= RESOURCE_VAR(res
);
439 /* Special case gl_VertexIDMESA -> gl_VertexID. */
440 if (var
->data
.mode
== ir_var_system_value
&&
441 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
442 return "gl_VertexID";
445 case GL_PROGRAM_OUTPUT
:
446 return RESOURCE_VAR(res
)->name
;
448 case GL_BUFFER_VARIABLE
:
449 return RESOURCE_UNI(res
)->name
;
450 case GL_VERTEX_SUBROUTINE_UNIFORM
:
451 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
452 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
453 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
454 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
455 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
456 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
457 case GL_VERTEX_SUBROUTINE
:
458 case GL_GEOMETRY_SUBROUTINE
:
459 case GL_FRAGMENT_SUBROUTINE
:
460 case GL_COMPUTE_SUBROUTINE
:
461 case GL_TESS_CONTROL_SUBROUTINE
:
462 case GL_TESS_EVALUATION_SUBROUTINE
:
463 return RESOURCE_SUB(res
)->name
;
465 assert(!"support for resource type not implemented");
472 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
475 case GL_TRANSFORM_FEEDBACK_VARYING
:
476 return RESOURCE_XFB(res
)->Size
> 1 ?
477 RESOURCE_XFB(res
)->Size
: 0;
478 case GL_PROGRAM_INPUT
:
479 case GL_PROGRAM_OUTPUT
:
480 return RESOURCE_VAR(res
)->type
->length
;
482 case GL_VERTEX_SUBROUTINE_UNIFORM
:
483 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
484 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
485 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
486 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
487 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
488 return RESOURCE_UNI(res
)->array_elements
;
489 case GL_BUFFER_VARIABLE
:
491 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
492 RESOURCE_UNI(res
)->array_elements
== 0)
495 return RESOURCE_UNI(res
)->array_elements
;
496 case GL_VERTEX_SUBROUTINE
:
497 case GL_GEOMETRY_SUBROUTINE
:
498 case GL_FRAGMENT_SUBROUTINE
:
499 case GL_COMPUTE_SUBROUTINE
:
500 case GL_TESS_CONTROL_SUBROUTINE
:
501 case GL_TESS_EVALUATION_SUBROUTINE
:
502 case GL_ATOMIC_COUNTER_BUFFER
:
503 case GL_UNIFORM_BLOCK
:
504 case GL_SHADER_STORAGE_BLOCK
:
507 assert(!"support for resource type not implemented");
513 * Checks if array subscript is valid and if so sets array_index.
516 valid_array_index(const GLchar
*name
, unsigned *array_index
)
519 const GLchar
*out_base_name_end
;
521 idx
= parse_program_resource_name(name
, &out_base_name_end
);
531 /* Find a program resource with specific name in given interface.
533 struct gl_program_resource
*
534 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
535 GLenum programInterface
, const char *name
,
536 unsigned *array_index
)
538 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
539 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
540 if (res
->Type
!= programInterface
)
543 /* Resource basename. */
544 const char *rname
= _mesa_program_resource_name(res
);
545 unsigned baselen
= strlen(rname
);
547 if (strncmp(rname
, name
, baselen
) == 0) {
548 switch (programInterface
) {
549 case GL_UNIFORM_BLOCK
:
550 case GL_SHADER_STORAGE_BLOCK
:
551 /* Basename match, check if array or struct. */
552 if (name
[baselen
] == '\0' ||
553 name
[baselen
] == '[' ||
554 name
[baselen
] == '.') {
558 case GL_TRANSFORM_FEEDBACK_VARYING
:
559 case GL_BUFFER_VARIABLE
:
561 case GL_VERTEX_SUBROUTINE_UNIFORM
:
562 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
563 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
564 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
565 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
566 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
567 case GL_VERTEX_SUBROUTINE
:
568 case GL_GEOMETRY_SUBROUTINE
:
569 case GL_FRAGMENT_SUBROUTINE
:
570 case GL_COMPUTE_SUBROUTINE
:
571 case GL_TESS_CONTROL_SUBROUTINE
:
572 case GL_TESS_EVALUATION_SUBROUTINE
:
573 if (name
[baselen
] == '.') {
577 case GL_PROGRAM_INPUT
:
578 case GL_PROGRAM_OUTPUT
:
579 if (name
[baselen
] == '\0') {
581 } else if (name
[baselen
] == '[' &&
582 valid_array_index(name
, array_index
)) {
587 assert(!"not implemented for given interface");
595 calc_resource_index(struct gl_shader_program
*shProg
,
596 struct gl_program_resource
*res
)
600 for (i
= 0; i
< shProg
->NumProgramResourceList
; i
++) {
601 if (&shProg
->ProgramResourceList
[i
] == res
)
603 if (shProg
->ProgramResourceList
[i
].Type
== res
->Type
)
606 return GL_INVALID_INDEX
;
610 * Calculate index for the given resource.
613 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
614 struct gl_program_resource
*res
)
617 return GL_INVALID_INDEX
;
620 case GL_ATOMIC_COUNTER_BUFFER
:
621 return RESOURCE_ATC(res
) - shProg
->AtomicBuffers
;
622 case GL_UNIFORM_BLOCK
:
623 case GL_SHADER_STORAGE_BLOCK
:
624 case GL_TRANSFORM_FEEDBACK_VARYING
:
626 return calc_resource_index(shProg
, res
);
630 /* Find a program resource with specific index in given interface.
632 struct gl_program_resource
*
633 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
634 GLenum programInterface
, GLuint index
)
636 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
639 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
640 if (res
->Type
!= programInterface
)
644 case GL_UNIFORM_BLOCK
:
645 case GL_ATOMIC_COUNTER_BUFFER
:
646 case GL_SHADER_STORAGE_BLOCK
:
647 if (_mesa_program_resource_index(shProg
, res
) == index
)
650 case GL_TRANSFORM_FEEDBACK_VARYING
:
651 case GL_PROGRAM_INPUT
:
652 case GL_PROGRAM_OUTPUT
:
654 case GL_VERTEX_SUBROUTINE_UNIFORM
:
655 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
656 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
657 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
658 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
659 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
660 case GL_VERTEX_SUBROUTINE
:
661 case GL_GEOMETRY_SUBROUTINE
:
662 case GL_FRAGMENT_SUBROUTINE
:
663 case GL_COMPUTE_SUBROUTINE
:
664 case GL_TESS_CONTROL_SUBROUTINE
:
665 case GL_TESS_EVALUATION_SUBROUTINE
:
666 case GL_BUFFER_VARIABLE
:
667 if (++idx
== (int) index
)
671 assert(!"not implemented for given interface");
677 /* Function returns if resource name is expected to have index
681 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
684 * "If the active uniform is an array, the uniform name returned in
685 * name will always be the name of the uniform array appended with
688 * The same text also appears in the OpenGL 4.2 spec. It does not,
689 * however, appear in any previous spec. Previous specifications are
690 * ambiguous in this regard. However, either name can later be passed
691 * to glGetUniformLocation (and related APIs), so there shouldn't be any
692 * harm in always appending "[0]" to uniform array names.
694 * Geometry shader stage has different naming convention where the 'normal'
695 * condition is an array, therefore for variables referenced in geometry
696 * stage we do not add '[0]'.
698 * Note, that TCS outputs and TES inputs should not have index appended
702 add_index_to_name(struct gl_program_resource
*res
)
704 bool add_index
= !(((res
->Type
== GL_PROGRAM_INPUT
) &&
705 res
->StageReferences
& (1 << MESA_SHADER_GEOMETRY
)));
707 /* Transform feedback varyings have array index already appended
710 if (res
->Type
== GL_TRANSFORM_FEEDBACK_VARYING
)
716 /* Get name length of a program resource. This consists of
717 * base name + 3 for '[0]' if resource is an array.
720 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
722 unsigned length
= strlen(_mesa_program_resource_name(res
));
723 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
728 /* Get full name of a program resource.
731 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
732 GLenum programInterface
, GLuint index
,
733 GLsizei bufSize
, GLsizei
*length
,
734 GLchar
*name
, const char *caller
)
736 GET_CURRENT_CONTEXT(ctx
);
738 /* Find resource with given interface and index. */
739 struct gl_program_resource
*res
=
740 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
742 /* The error INVALID_VALUE is generated if <index> is greater than
743 * or equal to the number of entries in the active resource list for
744 * <programInterface>.
747 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
752 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
759 length
= &localLength
;
761 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
763 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
766 /* The comparison is strange because *length does *NOT* include the
767 * terminating NUL, but maxLength does.
769 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
770 name
[*length
+ i
] = "[0]"[i
];
772 name
[*length
+ i
] = '\0';
779 program_resource_location(struct gl_shader_program
*shProg
,
780 struct gl_program_resource
*res
, const char *name
,
781 unsigned array_index
)
783 /* Built-in locations should report GL_INVALID_INDEX. */
784 if (is_gl_identifier(name
))
785 return GL_INVALID_INDEX
;
787 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
788 * offsets are used internally to differentiate between built-in attributes
789 * and user-defined attributes.
792 case GL_PROGRAM_INPUT
:
793 /* If the input is an array, fail if the index is out of bounds. */
795 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
798 return RESOURCE_VAR(res
)->data
.location
+ array_index
- VERT_ATTRIB_GENERIC0
;
799 case GL_PROGRAM_OUTPUT
:
800 /* If the output is an array, fail if the index is out of bounds. */
802 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
805 return RESOURCE_VAR(res
)->data
.location
+ array_index
- FRAG_RESULT_DATA0
;
807 /* If the uniform is built-in, fail. */
808 if (RESOURCE_UNI(res
)->builtin
)
811 /* From the GL_ARB_uniform_buffer_object spec:
813 * "The value -1 will be returned if <name> does not correspond to an
814 * active uniform variable name in <program>, if <name> is associated
815 * with a named uniform block, or if <name> starts with the reserved
818 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
819 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
823 case GL_VERTEX_SUBROUTINE_UNIFORM
:
824 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
825 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
826 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
827 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
828 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
829 /* If the uniform is an array, fail if the index is out of bounds. */
831 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
835 /* location in remap table + array element offset */
836 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
843 * Function implements following location queries:
844 * glGetUniformLocation
847 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
848 GLenum programInterface
, const char *name
)
850 unsigned array_index
= 0;
851 struct gl_program_resource
*res
=
852 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
855 /* Resource not found. */
859 return program_resource_location(shProg
, res
, name
, array_index
);
863 * Function implements following index queries:
867 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
868 GLenum programInterface
, const char *name
)
870 struct gl_program_resource
*res
=
871 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
873 /* Non-existent variable or resource is not referenced by fragment stage. */
874 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
877 return RESOURCE_VAR(res
)->data
.index
;
881 stage_from_enum(GLenum ref
)
884 case GL_REFERENCED_BY_VERTEX_SHADER
:
885 return MESA_SHADER_VERTEX
;
886 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
887 return MESA_SHADER_TESS_CTRL
;
888 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
889 return MESA_SHADER_TESS_EVAL
;
890 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
891 return MESA_SHADER_GEOMETRY
;
892 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
893 return MESA_SHADER_FRAGMENT
;
894 case GL_REFERENCED_BY_COMPUTE_SHADER
:
895 return MESA_SHADER_COMPUTE
;
897 assert(!"shader stage not supported");
898 return MESA_SHADER_STAGES
;
903 * Check if resource is referenced by given 'referenced by' stage enum.
904 * ATC and UBO resources hold stage references of their own.
907 is_resource_referenced(struct gl_shader_program
*shProg
,
908 struct gl_program_resource
*res
,
909 GLuint index
, uint8_t stage
)
911 /* First, check if we even have such a stage active. */
912 if (!shProg
->_LinkedShaders
[stage
])
915 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
916 return RESOURCE_ATC(res
)->StageReferences
[stage
];
918 if (res
->Type
== GL_UNIFORM_BLOCK
|| res
->Type
== GL_SHADER_STORAGE_BLOCK
)
919 return shProg
->UniformBlockStageIndex
[stage
][index
] != -1;
921 return res
->StageReferences
& (1 << stage
);
925 get_buffer_property(struct gl_shader_program
*shProg
,
926 struct gl_program_resource
*res
, const GLenum prop
,
927 GLint
*val
, const char *caller
)
929 GET_CURRENT_CONTEXT(ctx
);
930 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
931 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
932 res
->Type
!= GL_SHADER_STORAGE_BLOCK
)
933 goto invalid_operation
;
935 if (res
->Type
== GL_UNIFORM_BLOCK
) {
937 case GL_BUFFER_BINDING
:
938 *val
= RESOURCE_UBO(res
)->Binding
;
940 case GL_BUFFER_DATA_SIZE
:
941 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
943 case GL_NUM_ACTIVE_VARIABLES
:
945 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
946 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
947 struct gl_program_resource
*uni
=
948 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
955 case GL_ACTIVE_VARIABLES
: {
956 unsigned num_values
= 0;
957 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
958 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
959 struct gl_program_resource
*uni
=
960 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
965 _mesa_program_resource_index(shProg
, uni
);
971 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
973 case GL_BUFFER_BINDING
:
974 *val
= RESOURCE_UBO(res
)->Binding
;
976 case GL_BUFFER_DATA_SIZE
:
977 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
979 case GL_NUM_ACTIVE_VARIABLES
:
981 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
982 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
983 struct gl_program_resource
*uni
=
984 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
991 case GL_ACTIVE_VARIABLES
: {
992 unsigned num_values
= 0;
993 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
994 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
995 struct gl_program_resource
*uni
=
996 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1001 _mesa_program_resource_index(shProg
, uni
);
1007 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1009 case GL_BUFFER_BINDING
:
1010 *val
= RESOURCE_ATC(res
)->Binding
;
1012 case GL_BUFFER_DATA_SIZE
:
1013 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1015 case GL_NUM_ACTIVE_VARIABLES
:
1016 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1018 case GL_ACTIVE_VARIABLES
:
1019 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++)
1020 *val
++ = RESOURCE_ATC(res
)->Uniforms
[i
];
1021 return RESOURCE_ATC(res
)->NumUniforms
;
1024 assert(!"support for property type not implemented");
1027 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1028 _mesa_enum_to_string(res
->Type
),
1029 _mesa_enum_to_string(prop
));
1035 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1036 struct gl_program_resource
*res
, GLuint index
,
1037 const GLenum prop
, GLint
*val
, const char *caller
)
1039 GET_CURRENT_CONTEXT(ctx
);
1041 #define VALIDATE_TYPE(type)\
1042 if (res->Type != type)\
1043 goto invalid_operation;
1045 #define VALIDATE_TYPE_2(type1, type2)\
1046 if (res->Type != type1 && res->Type != type2)\
1047 goto invalid_operation;
1050 case GL_NAME_LENGTH
:
1051 switch (res
->Type
) {
1052 case GL_ATOMIC_COUNTER_BUFFER
:
1053 goto invalid_operation
;
1055 /* Resource name length + terminator. */
1056 *val
= _mesa_program_resource_name_len(res
) + 1;
1060 switch (res
->Type
) {
1062 case GL_BUFFER_VARIABLE
:
1063 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1065 case GL_PROGRAM_INPUT
:
1066 case GL_PROGRAM_OUTPUT
:
1067 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1069 case GL_TRANSFORM_FEEDBACK_VARYING
:
1070 *val
= RESOURCE_XFB(res
)->Type
;
1073 goto invalid_operation
;
1076 switch (res
->Type
) {
1078 case GL_BUFFER_VARIABLE
:
1079 /* Test if a buffer variable is an array or an unsized array.
1080 * Unsized arrays return zero as array size.
1082 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1083 RESOURCE_UNI(res
)->array_stride
> 0)
1084 *val
= RESOURCE_UNI(res
)->array_elements
;
1086 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1088 case GL_PROGRAM_INPUT
:
1089 case GL_PROGRAM_OUTPUT
:
1090 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1092 case GL_TRANSFORM_FEEDBACK_VARYING
:
1093 *val
= MAX2(RESOURCE_XFB(res
)->Size
, 1);
1096 goto invalid_operation
;
1099 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1100 *val
= RESOURCE_UNI(res
)->offset
;
1102 case GL_BLOCK_INDEX
:
1103 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1104 *val
= RESOURCE_UNI(res
)->block_index
;
1106 case GL_ARRAY_STRIDE
:
1107 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1108 *val
= RESOURCE_UNI(res
)->array_stride
;
1110 case GL_MATRIX_STRIDE
:
1111 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1112 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1114 case GL_IS_ROW_MAJOR
:
1115 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1116 *val
= RESOURCE_UNI(res
)->row_major
;
1118 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1119 VALIDATE_TYPE(GL_UNIFORM
);
1120 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1122 case GL_BUFFER_BINDING
:
1123 case GL_BUFFER_DATA_SIZE
:
1124 case GL_NUM_ACTIVE_VARIABLES
:
1125 case GL_ACTIVE_VARIABLES
:
1126 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1127 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1128 if (!_mesa_has_compute_shaders(ctx
))
1131 case GL_REFERENCED_BY_VERTEX_SHADER
:
1132 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1133 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1134 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1135 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1136 switch (res
->Type
) {
1138 case GL_PROGRAM_INPUT
:
1139 case GL_PROGRAM_OUTPUT
:
1140 case GL_UNIFORM_BLOCK
:
1141 case GL_BUFFER_VARIABLE
:
1142 case GL_SHADER_STORAGE_BLOCK
:
1143 case GL_ATOMIC_COUNTER_BUFFER
:
1144 *val
= is_resource_referenced(shProg
, res
, index
,
1145 stage_from_enum(prop
));
1148 goto invalid_operation
;
1151 switch (res
->Type
) {
1153 case GL_PROGRAM_INPUT
:
1154 case GL_PROGRAM_OUTPUT
:
1155 *val
= program_resource_location(shProg
, res
,
1156 _mesa_program_resource_name(res
),
1160 goto invalid_operation
;
1162 case GL_LOCATION_INDEX
:
1163 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1164 goto invalid_operation
;
1165 *val
= RESOURCE_VAR(res
)->data
.index
;
1168 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1169 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1170 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1171 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1172 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1173 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1174 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1175 goto invalid_operation
;
1176 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1178 case GL_COMPATIBLE_SUBROUTINES
: {
1179 const struct gl_uniform_storage
*uni
;
1180 struct gl_shader
*sh
;
1184 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1185 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1186 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1187 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1188 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1189 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1190 goto invalid_operation
;
1191 uni
= RESOURCE_UNI(res
);
1193 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1195 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1196 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1197 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1198 if (fn
->types
[j
] == uni
->type
) {
1207 case GL_TOP_LEVEL_ARRAY_SIZE
:
1208 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1209 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1212 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1213 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1214 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1217 /* GL_ARB_tessellation_shader */
1218 case GL_IS_PER_PATCH
:
1219 switch (res
->Type
) {
1220 case GL_PROGRAM_INPUT
:
1221 case GL_PROGRAM_OUTPUT
:
1222 *val
= RESOURCE_VAR(res
)->data
.patch
;
1225 goto invalid_operation
;
1231 #undef VALIDATE_TYPE
1232 #undef VALIDATE_TYPE_2
1235 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1236 _mesa_enum_to_string(res
->Type
),
1237 _mesa_enum_to_string(prop
));
1241 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1242 _mesa_enum_to_string(res
->Type
),
1243 _mesa_enum_to_string(prop
));
1248 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1249 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1250 const GLenum
*props
, GLsizei bufSize
,
1251 GLsizei
*length
, GLint
*params
)
1253 GET_CURRENT_CONTEXT(ctx
);
1254 GLint
*val
= (GLint
*) params
;
1255 const GLenum
*prop
= props
;
1258 struct gl_program_resource
*res
=
1259 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1261 /* No such resource found or bufSize negative. */
1262 if (!res
|| bufSize
< 0) {
1263 _mesa_error(ctx
, GL_INVALID_VALUE
,
1264 "glGetProgramResourceiv(%s index %d bufSize %d)",
1265 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1269 /* Write propCount values until error occurs or bufSize reached. */
1270 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1272 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1273 "glGetProgramResourceiv");
1275 /* Error happened. */
1276 if (props_written
== 0)
1279 amount
+= props_written
;
1282 /* If <length> is not NULL, the actual number of integer values
1283 * written to <params> will be written to <length>.