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 /* Transform feedback varyings have array index already appended
725 if (programInterface
== GL_TRANSFORM_FEEDBACK_VARYING
)
728 if (add_index
&& _mesa_program_resource_array_size(res
)) {
731 /* The comparison is strange because *length does *NOT* include the
732 * terminating NUL, but maxLength does.
734 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
735 name
[*length
+ i
] = "[0]"[i
];
737 name
[*length
+ i
] = '\0';
744 program_resource_location(struct gl_shader_program
*shProg
,
745 struct gl_program_resource
*res
, const char *name
,
746 unsigned array_index
)
748 /* Built-in locations should report GL_INVALID_INDEX. */
749 if (is_gl_identifier(name
))
750 return GL_INVALID_INDEX
;
752 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
753 * offsets are used internally to differentiate between built-in attributes
754 * and user-defined attributes.
757 case GL_PROGRAM_INPUT
:
758 /* If the input is an array, fail if the index is out of bounds. */
760 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
763 return RESOURCE_VAR(res
)->data
.location
+ array_index
- VERT_ATTRIB_GENERIC0
;
764 case GL_PROGRAM_OUTPUT
:
765 /* If the output is an array, fail if the index is out of bounds. */
767 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
770 return RESOURCE_VAR(res
)->data
.location
+ array_index
- FRAG_RESULT_DATA0
;
772 /* If the uniform is built-in, fail. */
773 if (RESOURCE_UNI(res
)->builtin
)
776 /* From the GL_ARB_uniform_buffer_object spec:
778 * "The value -1 will be returned if <name> does not correspond to an
779 * active uniform variable name in <program>, if <name> is associated
780 * with a named uniform block, or if <name> starts with the reserved
783 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
784 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
788 case GL_VERTEX_SUBROUTINE_UNIFORM
:
789 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
790 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
791 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
792 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
793 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
794 /* If the uniform is an array, fail if the index is out of bounds. */
796 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
800 /* location in remap table + array element offset */
801 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
808 * Function implements following location queries:
809 * glGetUniformLocation
812 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
813 GLenum programInterface
, const char *name
)
815 unsigned array_index
= 0;
816 struct gl_program_resource
*res
=
817 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
820 /* Resource not found. */
824 return program_resource_location(shProg
, res
, name
, array_index
);
828 * Function implements following index queries:
832 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
833 GLenum programInterface
, const char *name
)
835 struct gl_program_resource
*res
=
836 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
838 /* Non-existent variable or resource is not referenced by fragment stage. */
839 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
842 return RESOURCE_VAR(res
)->data
.index
;
846 stage_from_enum(GLenum ref
)
849 case GL_REFERENCED_BY_VERTEX_SHADER
:
850 return MESA_SHADER_VERTEX
;
851 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
852 return MESA_SHADER_TESS_CTRL
;
853 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
854 return MESA_SHADER_TESS_EVAL
;
855 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
856 return MESA_SHADER_GEOMETRY
;
857 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
858 return MESA_SHADER_FRAGMENT
;
859 case GL_REFERENCED_BY_COMPUTE_SHADER
:
860 return MESA_SHADER_COMPUTE
;
862 assert(!"shader stage not supported");
863 return MESA_SHADER_STAGES
;
868 * Check if resource is referenced by given 'referenced by' stage enum.
869 * ATC and UBO resources hold stage references of their own.
872 is_resource_referenced(struct gl_shader_program
*shProg
,
873 struct gl_program_resource
*res
,
874 GLuint index
, uint8_t stage
)
876 /* First, check if we even have such a stage active. */
877 if (!shProg
->_LinkedShaders
[stage
])
880 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
881 return RESOURCE_ATC(res
)->StageReferences
[stage
];
883 if (res
->Type
== GL_UNIFORM_BLOCK
)
884 return shProg
->UniformBlockStageIndex
[stage
][index
] != -1;
886 return res
->StageReferences
& (1 << stage
);
890 get_buffer_property(struct gl_shader_program
*shProg
,
891 struct gl_program_resource
*res
, const GLenum prop
,
892 GLint
*val
, const char *caller
)
894 GET_CURRENT_CONTEXT(ctx
);
895 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
896 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
)
897 goto invalid_operation
;
899 if (res
->Type
== GL_UNIFORM_BLOCK
) {
901 case GL_BUFFER_BINDING
:
902 *val
= RESOURCE_UBO(res
)->Binding
;
904 case GL_BUFFER_DATA_SIZE
:
905 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
907 case GL_NUM_ACTIVE_VARIABLES
:
909 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
910 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
911 struct gl_program_resource
*uni
=
912 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
919 case GL_ACTIVE_VARIABLES
:
920 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
921 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
922 struct gl_program_resource
*uni
=
923 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
928 _mesa_program_resource_index(shProg
, uni
);
930 return RESOURCE_UBO(res
)->NumUniforms
;
932 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
934 case GL_BUFFER_BINDING
:
935 *val
= RESOURCE_ATC(res
)->Binding
;
937 case GL_BUFFER_DATA_SIZE
:
938 *val
= RESOURCE_ATC(res
)->MinimumSize
;
940 case GL_NUM_ACTIVE_VARIABLES
:
941 *val
= RESOURCE_ATC(res
)->NumUniforms
;
943 case GL_ACTIVE_VARIABLES
:
944 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++)
945 *val
++ = RESOURCE_ATC(res
)->Uniforms
[i
];
946 return RESOURCE_ATC(res
)->NumUniforms
;
949 assert(!"support for property type not implemented");
952 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
953 _mesa_enum_to_string(res
->Type
),
954 _mesa_enum_to_string(prop
));
960 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
961 struct gl_program_resource
*res
, GLuint index
,
962 const GLenum prop
, GLint
*val
, const char *caller
)
964 GET_CURRENT_CONTEXT(ctx
);
966 #define VALIDATE_TYPE(type)\
967 if (res->Type != type)\
968 goto invalid_operation;
973 case GL_ATOMIC_COUNTER_BUFFER
:
974 goto invalid_operation
;
975 case GL_TRANSFORM_FEEDBACK_VARYING
:
976 *val
= strlen(_mesa_program_resource_name(res
)) + 1;
979 /* Base name +3 if array '[0]' + terminator. */
980 *val
= strlen(_mesa_program_resource_name(res
)) +
981 (_mesa_program_resource_array_size(res
) > 0 ? 3 : 0) + 1;
987 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
989 case GL_PROGRAM_INPUT
:
990 case GL_PROGRAM_OUTPUT
:
991 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
993 case GL_TRANSFORM_FEEDBACK_VARYING
:
994 *val
= RESOURCE_XFB(res
)->Type
;
997 goto invalid_operation
;
1000 switch (res
->Type
) {
1002 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1004 case GL_PROGRAM_INPUT
:
1005 case GL_PROGRAM_OUTPUT
:
1006 *val
= MAX2(RESOURCE_VAR(res
)->type
->length
, 1);
1008 case GL_TRANSFORM_FEEDBACK_VARYING
:
1009 *val
= MAX2(RESOURCE_XFB(res
)->Size
, 1);
1012 goto invalid_operation
;
1015 VALIDATE_TYPE(GL_UNIFORM
);
1016 *val
= RESOURCE_UNI(res
)->offset
;
1018 case GL_BLOCK_INDEX
:
1019 VALIDATE_TYPE(GL_UNIFORM
);
1020 *val
= RESOURCE_UNI(res
)->block_index
;
1022 case GL_ARRAY_STRIDE
:
1023 VALIDATE_TYPE(GL_UNIFORM
);
1024 *val
= RESOURCE_UNI(res
)->array_stride
;
1026 case GL_MATRIX_STRIDE
:
1027 VALIDATE_TYPE(GL_UNIFORM
);
1028 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1030 case GL_IS_ROW_MAJOR
:
1031 VALIDATE_TYPE(GL_UNIFORM
);
1032 *val
= RESOURCE_UNI(res
)->row_major
;
1034 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1035 VALIDATE_TYPE(GL_UNIFORM
);
1036 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1038 case GL_BUFFER_BINDING
:
1039 case GL_BUFFER_DATA_SIZE
:
1040 case GL_NUM_ACTIVE_VARIABLES
:
1041 case GL_ACTIVE_VARIABLES
:
1042 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1043 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1044 if (!_mesa_has_compute_shaders(ctx
))
1047 case GL_REFERENCED_BY_VERTEX_SHADER
:
1048 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1049 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1050 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1051 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1052 switch (res
->Type
) {
1054 case GL_PROGRAM_INPUT
:
1055 case GL_PROGRAM_OUTPUT
:
1056 case GL_UNIFORM_BLOCK
:
1057 case GL_ATOMIC_COUNTER_BUFFER
:
1058 *val
= is_resource_referenced(shProg
, res
, index
,
1059 stage_from_enum(prop
));
1062 goto invalid_operation
;
1065 switch (res
->Type
) {
1067 case GL_PROGRAM_INPUT
:
1068 case GL_PROGRAM_OUTPUT
:
1069 *val
= program_resource_location(shProg
, res
,
1070 _mesa_program_resource_name(res
),
1074 goto invalid_operation
;
1076 case GL_LOCATION_INDEX
:
1077 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1078 goto invalid_operation
;
1079 *val
= RESOURCE_VAR(res
)->data
.index
;
1082 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1083 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1084 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1085 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1086 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1087 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1088 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1089 goto invalid_operation
;
1090 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1092 case GL_COMPATIBLE_SUBROUTINES
: {
1093 const struct gl_uniform_storage
*uni
;
1094 struct gl_shader
*sh
;
1098 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1099 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1100 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1101 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1102 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1103 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1104 goto invalid_operation
;
1105 uni
= RESOURCE_UNI(res
);
1107 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1109 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1110 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1111 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1112 if (fn
->types
[j
] == uni
->type
) {
1120 /* GL_ARB_tessellation_shader */
1121 case GL_IS_PER_PATCH
:
1122 switch (res
->Type
) {
1123 case GL_PROGRAM_INPUT
:
1124 case GL_PROGRAM_OUTPUT
:
1125 *val
= RESOURCE_VAR(res
)->data
.patch
;
1128 goto invalid_operation
;
1134 #undef VALIDATE_TYPE
1137 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1138 _mesa_enum_to_string(res
->Type
),
1139 _mesa_enum_to_string(prop
));
1143 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1144 _mesa_enum_to_string(res
->Type
),
1145 _mesa_enum_to_string(prop
));
1150 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1151 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1152 const GLenum
*props
, GLsizei bufSize
,
1153 GLsizei
*length
, GLint
*params
)
1155 GET_CURRENT_CONTEXT(ctx
);
1156 GLint
*val
= (GLint
*) params
;
1157 const GLenum
*prop
= props
;
1160 struct gl_program_resource
*res
=
1161 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1163 /* No such resource found or bufSize negative. */
1164 if (!res
|| bufSize
< 0) {
1165 _mesa_error(ctx
, GL_INVALID_VALUE
,
1166 "glGetProgramResourceiv(%s index %d bufSize %d)",
1167 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1171 /* Write propCount values until error occurs or bufSize reached. */
1172 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1174 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1175 "glGetProgramResourceiv");
1177 /* Error happened. */
1178 if (props_written
== 0)
1181 amount
+= props_written
;
1184 /* If <length> is not NULL, the actual number of integer values
1185 * written to <params> will be written to <length>.