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 "glsl_symbol_table.h"
35 #include "shaderobj.h"
36 #include "program/hash_table.h"
37 #include "../glsl/program.h"
39 #include "main/enums.h"
42 #include "shaderapi.h"
46 program_resource_location(struct gl_shader_program
*shProg
,
47 struct gl_program_resource
*res
, const char *name
,
48 unsigned array_index
);
51 * Declare convenience functions to return resource data in a given type.
52 * Warning! this is not type safe so be *very* careful when using these.
54 #define DECL_RESOURCE_FUNC(name, type) \
55 const type * RESOURCE_ ## name (gl_program_resource *res) { \
57 return (type *) res->Data; \
60 DECL_RESOURCE_FUNC(VAR
, ir_variable
);
61 DECL_RESOURCE_FUNC(UBO
, gl_uniform_block
);
62 DECL_RESOURCE_FUNC(UNI
, gl_uniform_storage
);
63 DECL_RESOURCE_FUNC(ATC
, gl_active_atomic_buffer
);
64 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_varying_info
);
65 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
68 _mesa_BindAttribLocation(GLhandleARB program
, GLuint index
,
69 const GLcharARB
*name
)
71 GET_CURRENT_CONTEXT(ctx
);
73 struct gl_shader_program
*const shProg
=
74 _mesa_lookup_shader_program_err(ctx
, program
, "glBindAttribLocation");
81 if (strncmp(name
, "gl_", 3) == 0) {
82 _mesa_error(ctx
, GL_INVALID_OPERATION
,
83 "glBindAttribLocation(illegal name)");
87 if (index
>= ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAttribs
) {
88 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindAttribLocation(index)");
92 /* Replace the current value if it's already in the list. Add
93 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
94 * between built-in attributes and user-defined attributes.
96 shProg
->AttributeBindings
->put(index
+ VERT_ATTRIB_GENERIC0
, name
);
99 * Note that this attribute binding won't go into effect until
100 * glLinkProgram is called again.
105 is_active_attrib(const ir_variable
*var
)
110 switch (var
->data
.mode
) {
111 case ir_var_shader_in
:
112 return var
->data
.location
!= -1;
114 case ir_var_system_value
:
115 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
116 * "For GetActiveAttrib, all active vertex shader input variables
117 * are enumerated, including the special built-in inputs gl_VertexID
118 * and gl_InstanceID."
120 return var
->data
.location
== SYSTEM_VALUE_VERTEX_ID
||
121 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
||
122 var
->data
.location
== SYSTEM_VALUE_INSTANCE_ID
;
130 _mesa_GetActiveAttrib(GLhandleARB program
, GLuint desired_index
,
131 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
132 GLenum
* type
, GLcharARB
* name
)
134 GET_CURRENT_CONTEXT(ctx
);
135 struct gl_shader_program
*shProg
;
138 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(maxLength < 0)");
142 shProg
= _mesa_lookup_shader_program_err(ctx
, program
, "glGetActiveAttrib");
146 if (!shProg
->LinkStatus
) {
147 _mesa_error(ctx
, GL_INVALID_VALUE
,
148 "glGetActiveAttrib(program not linked)");
152 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
153 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(no vertex shader)");
157 struct gl_program_resource
*res
=
158 _mesa_program_resource_find_index(shProg
, GL_PROGRAM_INPUT
,
161 /* User asked for index that does not exist. */
163 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetActiveAttrib(index)");
167 const ir_variable
*const var
= RESOURCE_VAR(res
);
169 if (!is_active_attrib(var
))
172 const char *var_name
= var
->name
;
174 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
175 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
178 if (var
->data
.mode
== ir_var_system_value
&&
179 var
->data
.location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
180 var_name
= "gl_VertexID";
183 _mesa_copy_string(name
, maxLength
, length
, var_name
);
186 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_ARRAY_SIZE
,
187 size
, "glGetActiveAttrib");
190 _mesa_program_resource_prop(shProg
, res
, desired_index
, GL_TYPE
,
191 (GLint
*) type
, "glGetActiveAttrib");
195 _mesa_GetAttribLocation(GLhandleARB program
, const GLcharARB
* name
)
197 GET_CURRENT_CONTEXT(ctx
);
198 struct gl_shader_program
*const shProg
=
199 _mesa_lookup_shader_program_err(ctx
, program
, "glGetAttribLocation");
205 if (!shProg
->LinkStatus
) {
206 _mesa_error(ctx
, GL_INVALID_OPERATION
,
207 "glGetAttribLocation(program not linked)");
214 /* Not having a vertex shader is not an error.
216 if (shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
)
219 unsigned array_index
= 0;
220 struct gl_program_resource
*res
=
221 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_INPUT
, name
,
227 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
229 /* The extra check against against 0 is made because of builtin-attribute
230 * locations that have offset applied. Function program_resource_location
231 * can return built-in attribute locations < 0 and glGetAttribLocation
232 * cannot be used on "conventional" attributes.
234 * From page 95 of the OpenGL 3.0 spec:
236 * "If name is not an active attribute, if name is a conventional
237 * attribute, or if an error occurs, -1 will be returned."
239 return (loc
>= 0) ? loc
: -1;
243 _mesa_count_active_attribs(struct gl_shader_program
*shProg
)
245 if (!shProg
->LinkStatus
246 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
250 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
252 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
253 if (res
->Type
== GL_PROGRAM_INPUT
&&
254 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
) &&
255 is_active_attrib(RESOURCE_VAR(res
)))
263 _mesa_longest_attribute_name_length(struct gl_shader_program
*shProg
)
265 if (!shProg
->LinkStatus
266 || shProg
->_LinkedShaders
[MESA_SHADER_VERTEX
] == NULL
) {
270 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
272 for (unsigned j
= 0; j
< shProg
->NumProgramResourceList
; j
++, res
++) {
273 if (res
->Type
== GL_PROGRAM_INPUT
&&
274 res
->StageReferences
& (1 << MESA_SHADER_VERTEX
)) {
276 const size_t length
= strlen(RESOURCE_VAR(res
)->name
);
277 if (length
>= longest
)
278 longest
= length
+ 1;
286 _mesa_BindFragDataLocation(GLuint program
, GLuint colorNumber
,
289 _mesa_BindFragDataLocationIndexed(program
, colorNumber
, 0, name
);
293 _mesa_BindFragDataLocationIndexed(GLuint program
, GLuint colorNumber
,
294 GLuint index
, const GLchar
*name
)
296 GET_CURRENT_CONTEXT(ctx
);
298 struct gl_shader_program
*const shProg
=
299 _mesa_lookup_shader_program_err(ctx
, program
, "glBindFragDataLocationIndexed");
306 if (strncmp(name
, "gl_", 3) == 0) {
307 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindFragDataLocationIndexed(illegal name)");
312 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(index)");
316 if (index
== 0 && colorNumber
>= ctx
->Const
.MaxDrawBuffers
) {
317 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
321 if (index
== 1 && colorNumber
>= ctx
->Const
.MaxDualSourceDrawBuffers
) {
322 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindFragDataLocationIndexed(colorNumber)");
326 /* Replace the current value if it's already in the list. Add
327 * FRAG_RESULT_DATA0 because that's how the linker differentiates
328 * between built-in attributes and user-defined attributes.
330 shProg
->FragDataBindings
->put(colorNumber
+ FRAG_RESULT_DATA0
, name
);
331 shProg
->FragDataIndexBindings
->put(index
, name
);
333 * Note that this binding won't go into effect until
334 * glLinkProgram is called again.
340 _mesa_GetFragDataIndex(GLuint program
, const GLchar
*name
)
342 GET_CURRENT_CONTEXT(ctx
);
343 struct gl_shader_program
*const shProg
=
344 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataIndex");
350 if (!shProg
->LinkStatus
) {
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "glGetFragDataIndex(program not linked)");
359 if (strncmp(name
, "gl_", 3) == 0) {
360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
361 "glGetFragDataIndex(illegal name)");
365 /* Not having a fragment shader is not an error.
367 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
370 return _mesa_program_resource_location_index(shProg
, GL_PROGRAM_OUTPUT
,
375 _mesa_GetFragDataLocation(GLuint program
, const GLchar
*name
)
377 GET_CURRENT_CONTEXT(ctx
);
378 struct gl_shader_program
*const shProg
=
379 _mesa_lookup_shader_program_err(ctx
, program
, "glGetFragDataLocation");
385 if (!shProg
->LinkStatus
) {
386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
387 "glGetFragDataLocation(program not linked)");
394 if (strncmp(name
, "gl_", 3) == 0) {
395 _mesa_error(ctx
, GL_INVALID_OPERATION
,
396 "glGetFragDataLocation(illegal name)");
400 /* Not having a fragment shader is not an error.
402 if (shProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
] == NULL
)
405 unsigned array_index
= 0;
406 struct gl_program_resource
*res
=
407 _mesa_program_resource_find_name(shProg
, GL_PROGRAM_OUTPUT
, name
,
413 GLint loc
= program_resource_location(shProg
, res
, name
, array_index
);
415 /* The extra check against against 0 is made because of builtin-attribute
416 * locations that have offset applied. Function program_resource_location
417 * can return built-in attribute locations < 0 and glGetFragDataLocation
418 * cannot be used on "conventional" attributes.
420 * From page 95 of the OpenGL 3.0 spec:
422 * "If name is not an active attribute, if name is a conventional
423 * attribute, or if an error occurs, -1 will be returned."
425 return (loc
>= 0) ? loc
: -1;
429 _mesa_program_resource_name(struct gl_program_resource
*res
)
431 const ir_variable
*var
;
433 case GL_UNIFORM_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 return RESOURCE_UNI(res
)->name
;
449 case GL_VERTEX_SUBROUTINE_UNIFORM
:
450 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
451 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
452 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
453 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
454 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
455 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
456 case GL_VERTEX_SUBROUTINE
:
457 case GL_GEOMETRY_SUBROUTINE
:
458 case GL_FRAGMENT_SUBROUTINE
:
459 case GL_COMPUTE_SUBROUTINE
:
460 case GL_TESS_CONTROL_SUBROUTINE
:
461 case GL_TESS_EVALUATION_SUBROUTINE
:
462 return RESOURCE_SUB(res
)->name
;
464 assert(!"support for resource type not implemented");
471 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
474 case GL_TRANSFORM_FEEDBACK_VARYING
:
475 return RESOURCE_XFB(res
)->Size
> 1 ?
476 RESOURCE_XFB(res
)->Size
: 0;
477 case GL_PROGRAM_INPUT
:
478 case GL_PROGRAM_OUTPUT
:
479 return RESOURCE_VAR(res
)->data
.max_array_access
;
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_VERTEX_SUBROUTINE
:
489 case GL_GEOMETRY_SUBROUTINE
:
490 case GL_FRAGMENT_SUBROUTINE
:
491 case GL_COMPUTE_SUBROUTINE
:
492 case GL_TESS_CONTROL_SUBROUTINE
:
493 case GL_TESS_EVALUATION_SUBROUTINE
:
494 case GL_ATOMIC_COUNTER_BUFFER
:
495 case GL_UNIFORM_BLOCK
:
498 assert(!"support for resource type not implemented");
504 * Checks if array subscript is valid and if so sets array_index.
507 valid_array_index(const GLchar
*name
, unsigned *array_index
)
510 const GLchar
*out_base_name_end
;
512 idx
= parse_program_resource_name(name
, &out_base_name_end
);
522 /* Find a program resource with specific name in given interface.
524 struct gl_program_resource
*
525 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
526 GLenum programInterface
, const char *name
,
527 unsigned *array_index
)
529 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
530 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
531 if (res
->Type
!= programInterface
)
534 /* Resource basename. */
535 const char *rname
= _mesa_program_resource_name(res
);
536 unsigned baselen
= strlen(rname
);
538 if (strncmp(rname
, name
, baselen
) == 0) {
539 switch (programInterface
) {
540 case GL_UNIFORM_BLOCK
:
541 /* Basename match, check if array or struct. */
542 if (name
[baselen
] == '\0' ||
543 name
[baselen
] == '[' ||
544 name
[baselen
] == '.') {
548 case GL_TRANSFORM_FEEDBACK_VARYING
:
550 case GL_VERTEX_SUBROUTINE_UNIFORM
:
551 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
552 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
553 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
554 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
555 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
556 case GL_VERTEX_SUBROUTINE
:
557 case GL_GEOMETRY_SUBROUTINE
:
558 case GL_FRAGMENT_SUBROUTINE
:
559 case GL_COMPUTE_SUBROUTINE
:
560 case GL_TESS_CONTROL_SUBROUTINE
:
561 case GL_TESS_EVALUATION_SUBROUTINE
:
562 if (name
[baselen
] == '.') {
566 case GL_PROGRAM_INPUT
:
567 case GL_PROGRAM_OUTPUT
:
568 if (name
[baselen
] == '\0') {
570 } else if (name
[baselen
] == '[' &&
571 valid_array_index(name
, array_index
)) {
576 assert(!"not implemented for given interface");
584 calc_resource_index(struct gl_shader_program
*shProg
,
585 struct gl_program_resource
*res
)
589 for (i
= 0; i
< shProg
->NumProgramResourceList
; i
++) {
590 if (&shProg
->ProgramResourceList
[i
] == res
)
592 if (shProg
->ProgramResourceList
[i
].Type
== res
->Type
)
595 return GL_INVALID_INDEX
;
599 * Calculate index for the given resource.
602 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
603 struct gl_program_resource
*res
)
606 return GL_INVALID_INDEX
;
609 case GL_UNIFORM_BLOCK
:
610 return RESOURCE_UBO(res
)- shProg
->UniformBlocks
;
611 case GL_ATOMIC_COUNTER_BUFFER
:
612 return RESOURCE_ATC(res
) - shProg
->AtomicBuffers
;
613 case GL_TRANSFORM_FEEDBACK_VARYING
:
615 return calc_resource_index(shProg
, res
);
619 /* Find a program resource with specific index in given interface.
621 struct gl_program_resource
*
622 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
623 GLenum programInterface
, GLuint index
)
625 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
628 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
629 if (res
->Type
!= programInterface
)
633 case GL_UNIFORM_BLOCK
:
634 case GL_ATOMIC_COUNTER_BUFFER
:
635 if (_mesa_program_resource_index(shProg
, res
) == index
)
638 case GL_TRANSFORM_FEEDBACK_VARYING
:
639 case GL_PROGRAM_INPUT
:
640 case GL_PROGRAM_OUTPUT
:
642 case GL_VERTEX_SUBROUTINE_UNIFORM
:
643 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
644 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
645 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
646 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
647 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
648 case GL_VERTEX_SUBROUTINE
:
649 case GL_GEOMETRY_SUBROUTINE
:
650 case GL_FRAGMENT_SUBROUTINE
:
651 case GL_COMPUTE_SUBROUTINE
:
652 case GL_TESS_CONTROL_SUBROUTINE
:
653 case GL_TESS_EVALUATION_SUBROUTINE
:
654 if (++idx
== (int) index
)
658 assert(!"not implemented for given interface");
664 /* Get full name of a program resource.
667 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
668 GLenum programInterface
, GLuint index
,
669 GLsizei bufSize
, GLsizei
*length
,
670 GLchar
*name
, const char *caller
)
672 GET_CURRENT_CONTEXT(ctx
);
674 /* Find resource with given interface and index. */
675 struct gl_program_resource
*res
=
676 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
678 /* The error INVALID_VALUE is generated if <index> is greater than
679 * or equal to the number of entries in the active resource list for
680 * <programInterface>.
683 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
688 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
695 length
= &localLength
;
697 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
699 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
702 * "If the active uniform is an array, the uniform name returned in
703 * name will always be the name of the uniform array appended with
706 * The same text also appears in the OpenGL 4.2 spec. It does not,
707 * however, appear in any previous spec. Previous specifications are
708 * ambiguous in this regard. However, either name can later be passed
709 * to glGetUniformLocation (and related APIs), so there shouldn't be any
710 * harm in always appending "[0]" to uniform array names.
712 * Geometry shader stage has different naming convention where the 'normal'
713 * condition is an array, therefore for variables referenced in geometry
714 * stage we do not add '[0]'.
716 * Note, that TCS outputs and TES inputs should not have index appended
719 bool add_index
= !(((programInterface
== GL_PROGRAM_INPUT
) &&
720 res
->StageReferences
& (1 << MESA_SHADER_GEOMETRY
)));
722 if (add_index
&& _mesa_program_resource_array_size(res
)) {
725 /* The comparison is strange because *length does *NOT* include the
726 * terminating NUL, but maxLength does.
728 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
729 name
[*length
+ i
] = "[0]"[i
];
731 name
[*length
+ i
] = '\0';
738 program_resource_location(struct gl_shader_program
*shProg
,
739 struct gl_program_resource
*res
, const char *name
,
740 unsigned array_index
)
742 /* Built-in locations should report GL_INVALID_INDEX. */
743 if (is_gl_identifier(name
))
744 return GL_INVALID_INDEX
;
746 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
747 * offsets are used internally to differentiate between built-in attributes
748 * and user-defined attributes.
751 case GL_PROGRAM_INPUT
:
752 /* If the input is an array, fail if the index is out of bounds. */
754 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
757 return RESOURCE_VAR(res
)->data
.location
+ array_index
- VERT_ATTRIB_GENERIC0
;
758 case GL_PROGRAM_OUTPUT
:
759 /* If the output is an array, fail if the index is out of bounds. */
761 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
764 return RESOURCE_VAR(res
)->data
.location
+ array_index
- FRAG_RESULT_DATA0
;
766 /* If the uniform is built-in, fail. */
767 if (RESOURCE_UNI(res
)->builtin
)
770 /* From the GL_ARB_uniform_buffer_object spec:
772 * "The value -1 will be returned if <name> does not correspond to an
773 * active uniform variable name in <program>, if <name> is associated
774 * with a named uniform block, or if <name> starts with the reserved
777 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
778 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
782 case GL_VERTEX_SUBROUTINE_UNIFORM
:
783 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
784 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
785 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
786 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
787 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
788 /* If the uniform is an array, fail if the index is out of bounds. */
790 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
794 /* location in remap table + array element offset */
795 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
802 * Function implements following location queries:
803 * glGetUniformLocation
806 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
807 GLenum programInterface
, const char *name
)
809 unsigned array_index
= 0;
810 struct gl_program_resource
*res
=
811 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
814 /* Resource not found. */
818 return program_resource_location(shProg
, res
, name
, array_index
);
822 * Function implements following index queries:
826 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
827 GLenum programInterface
, const char *name
)
829 struct gl_program_resource
*res
=
830 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
832 /* Non-existent variable or resource is not referenced by fragment stage. */
833 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
836 return RESOURCE_VAR(res
)->data
.index
;
840 stage_from_enum(GLenum ref
)
843 case GL_REFERENCED_BY_VERTEX_SHADER
:
844 return MESA_SHADER_VERTEX
;
845 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
846 return MESA_SHADER_TESS_CTRL
;
847 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
848 return MESA_SHADER_TESS_EVAL
;
849 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
850 return MESA_SHADER_GEOMETRY
;
851 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
852 return MESA_SHADER_FRAGMENT
;
853 case GL_REFERENCED_BY_COMPUTE_SHADER
:
854 return MESA_SHADER_COMPUTE
;
856 assert(!"shader stage not supported");
857 return MESA_SHADER_STAGES
;
862 * Check if resource is referenced by given 'referenced by' stage enum.
863 * ATC and UBO resources hold stage references of their own.
866 is_resource_referenced(struct gl_shader_program
*shProg
,
867 struct gl_program_resource
*res
,
868 GLuint index
, uint8_t stage
)
870 /* First, check if we even have such a stage active. */
871 if (!shProg
->_LinkedShaders
[stage
])
874 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
875 return RESOURCE_ATC(res
)->StageReferences
[stage
];
877 if (res
->Type
== GL_UNIFORM_BLOCK
)
878 return shProg
->UniformBlockStageIndex
[stage
][index
] != -1;
880 return res
->StageReferences
& (1 << stage
);
884 get_buffer_property(struct gl_shader_program
*shProg
,
885 struct gl_program_resource
*res
, const GLenum prop
,
886 GLint
*val
, const char *caller
)
888 GET_CURRENT_CONTEXT(ctx
);
889 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
890 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
)
891 goto invalid_operation
;
893 if (res
->Type
== GL_UNIFORM_BLOCK
) {
895 case GL_BUFFER_BINDING
:
896 *val
= RESOURCE_UBO(res
)->Binding
;
898 case GL_BUFFER_DATA_SIZE
:
899 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
901 case GL_NUM_ACTIVE_VARIABLES
:
903 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
904 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
905 struct gl_program_resource
*uni
=
906 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
913 case GL_ACTIVE_VARIABLES
:
914 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
915 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
916 struct gl_program_resource
*uni
=
917 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
922 _mesa_program_resource_index(shProg
, uni
);
924 return RESOURCE_UBO(res
)->NumUniforms
;
926 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
928 case GL_BUFFER_BINDING
:
929 *val
= RESOURCE_ATC(res
)->Binding
;
931 case GL_BUFFER_DATA_SIZE
:
932 *val
= RESOURCE_ATC(res
)->MinimumSize
;
934 case GL_NUM_ACTIVE_VARIABLES
:
935 *val
= RESOURCE_ATC(res
)->NumUniforms
;
937 case GL_ACTIVE_VARIABLES
:
938 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++)
939 *val
++ = RESOURCE_ATC(res
)->Uniforms
[i
];
940 return RESOURCE_ATC(res
)->NumUniforms
;
943 assert(!"support for property type not implemented");
946 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
947 _mesa_enum_to_string(res
->Type
),
948 _mesa_enum_to_string(prop
));
954 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
955 struct gl_program_resource
*res
, GLuint index
,
956 const GLenum prop
, GLint
*val
, const char *caller
)
958 GET_CURRENT_CONTEXT(ctx
);
960 #define VALIDATE_TYPE(type)\
961 if (res->Type != type)\
962 goto invalid_operation;
966 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
967 goto invalid_operation
;
968 /* Base name +3 if array '[0]' + terminator. */
969 *val
= strlen(_mesa_program_resource_name(res
)) +
970 (_mesa_program_resource_array_size(res
) > 0 ? 3 : 0) + 1;
975 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
977 case GL_PROGRAM_INPUT
:
978 case GL_PROGRAM_OUTPUT
:
979 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
981 case GL_TRANSFORM_FEEDBACK_VARYING
:
982 *val
= RESOURCE_XFB(res
)->Type
;
985 goto invalid_operation
;
990 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
992 case GL_PROGRAM_INPUT
:
993 case GL_PROGRAM_OUTPUT
:
994 *val
= MAX2(RESOURCE_VAR(res
)->type
->length
, 1);
996 case GL_TRANSFORM_FEEDBACK_VARYING
:
997 *val
= MAX2(RESOURCE_XFB(res
)->Size
, 1);
1000 goto invalid_operation
;
1003 VALIDATE_TYPE(GL_UNIFORM
);
1004 *val
= RESOURCE_UNI(res
)->offset
;
1006 case GL_BLOCK_INDEX
:
1007 VALIDATE_TYPE(GL_UNIFORM
);
1008 *val
= RESOURCE_UNI(res
)->block_index
;
1010 case GL_ARRAY_STRIDE
:
1011 VALIDATE_TYPE(GL_UNIFORM
);
1012 *val
= RESOURCE_UNI(res
)->array_stride
;
1014 case GL_MATRIX_STRIDE
:
1015 VALIDATE_TYPE(GL_UNIFORM
);
1016 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1018 case GL_IS_ROW_MAJOR
:
1019 VALIDATE_TYPE(GL_UNIFORM
);
1020 *val
= RESOURCE_UNI(res
)->row_major
;
1022 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1023 VALIDATE_TYPE(GL_UNIFORM
);
1024 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1026 case GL_BUFFER_BINDING
:
1027 case GL_BUFFER_DATA_SIZE
:
1028 case GL_NUM_ACTIVE_VARIABLES
:
1029 case GL_ACTIVE_VARIABLES
:
1030 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1031 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1032 if (!_mesa_has_compute_shaders(ctx
))
1035 case GL_REFERENCED_BY_VERTEX_SHADER
:
1036 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1037 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1038 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1039 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1040 switch (res
->Type
) {
1042 case GL_PROGRAM_INPUT
:
1043 case GL_PROGRAM_OUTPUT
:
1044 case GL_UNIFORM_BLOCK
:
1045 case GL_ATOMIC_COUNTER_BUFFER
:
1046 *val
= is_resource_referenced(shProg
, res
, index
,
1047 stage_from_enum(prop
));
1050 goto invalid_operation
;
1053 switch (res
->Type
) {
1055 case GL_PROGRAM_INPUT
:
1056 case GL_PROGRAM_OUTPUT
:
1057 *val
= program_resource_location(shProg
, res
,
1058 _mesa_program_resource_name(res
),
1062 goto invalid_operation
;
1064 case GL_LOCATION_INDEX
:
1065 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1066 goto invalid_operation
;
1067 *val
= RESOURCE_VAR(res
)->data
.index
;
1070 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1071 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1072 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1073 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1074 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1075 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1076 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1077 goto invalid_operation
;
1078 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1080 case GL_COMPATIBLE_SUBROUTINES
: {
1081 const struct gl_uniform_storage
*uni
;
1082 struct gl_shader
*sh
;
1086 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1087 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1088 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1089 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1090 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1091 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1092 goto invalid_operation
;
1093 uni
= RESOURCE_UNI(res
);
1095 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1097 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1098 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1099 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1100 if (fn
->types
[j
] == uni
->type
) {
1108 /* GL_ARB_tessellation_shader */
1109 case GL_IS_PER_PATCH
:
1110 switch (res
->Type
) {
1111 case GL_PROGRAM_INPUT
:
1112 case GL_PROGRAM_OUTPUT
:
1113 *val
= RESOURCE_VAR(res
)->data
.patch
;
1116 goto invalid_operation
;
1122 #undef VALIDATE_TYPE
1125 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1126 _mesa_enum_to_string(res
->Type
),
1127 _mesa_enum_to_string(prop
));
1131 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1132 _mesa_enum_to_string(res
->Type
),
1133 _mesa_enum_to_string(prop
));
1138 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1139 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1140 const GLenum
*props
, GLsizei bufSize
,
1141 GLsizei
*length
, GLint
*params
)
1143 GET_CURRENT_CONTEXT(ctx
);
1144 GLint
*val
= (GLint
*) params
;
1145 const GLenum
*prop
= props
;
1148 struct gl_program_resource
*res
=
1149 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1151 /* No such resource found or bufSize negative. */
1152 if (!res
|| bufSize
< 0) {
1153 _mesa_error(ctx
, GL_INVALID_VALUE
,
1154 "glGetProgramResourceiv(%s index %d bufSize %d)",
1155 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1159 /* Write propCount values until error occurs or bufSize reached. */
1160 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1162 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1163 "glGetProgramResourceiv");
1165 /* Error happened. */
1166 if (props_written
== 0)
1169 amount
+= props_written
;
1172 /* If <length> is not NULL, the actual number of integer values
1173 * written to <params> will be written to <length>.