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 "compiler/glsl/glsl_symbol_table.h"
38 #include "compiler/glsl/ir.h"
39 #include "compiler/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
, gl_shader_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(XFV
, gl_transform_feedback_varying_info
);
64 DECL_RESOURCE_FUNC(XFB
, gl_transform_feedback_buffer
);
65 DECL_RESOURCE_FUNC(SUB
, gl_subroutine_function
);
68 _mesa_BindAttribLocation(GLuint program
, GLuint index
,
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 gl_shader_variable
*var
)
111 case ir_var_shader_in
:
112 return var
->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
->location
== SYSTEM_VALUE_VERTEX_ID
||
121 var
->location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
||
122 var
->location
== SYSTEM_VALUE_INSTANCE_ID
;
130 _mesa_GetActiveAttrib(GLuint program
, GLuint desired_index
,
131 GLsizei maxLength
, GLsizei
* length
, GLint
* size
,
132 GLenum
* type
, GLchar
* 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 gl_shader_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
->mode
== ir_var_system_value
&&
179 var
->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(GLuint program
, const GLchar
* 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 gl_shader_variable
*var
;
433 case GL_UNIFORM_BLOCK
:
434 case GL_SHADER_STORAGE_BLOCK
:
435 return RESOURCE_UBO(res
)->Name
;
436 case GL_TRANSFORM_FEEDBACK_VARYING
:
437 return RESOURCE_XFV(res
)->Name
;
438 case GL_PROGRAM_INPUT
:
439 var
= RESOURCE_VAR(res
);
440 /* Special case gl_VertexIDMESA -> gl_VertexID. */
441 if (var
->mode
== ir_var_system_value
&&
442 var
->location
== SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
) {
443 return "gl_VertexID";
446 case GL_PROGRAM_OUTPUT
:
447 return RESOURCE_VAR(res
)->name
;
449 case GL_BUFFER_VARIABLE
:
450 return RESOURCE_UNI(res
)->name
;
451 case GL_VERTEX_SUBROUTINE_UNIFORM
:
452 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
453 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
454 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
455 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
456 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
457 return RESOURCE_UNI(res
)->name
+ MESA_SUBROUTINE_PREFIX_LEN
;
458 case GL_VERTEX_SUBROUTINE
:
459 case GL_GEOMETRY_SUBROUTINE
:
460 case GL_FRAGMENT_SUBROUTINE
:
461 case GL_COMPUTE_SUBROUTINE
:
462 case GL_TESS_CONTROL_SUBROUTINE
:
463 case GL_TESS_EVALUATION_SUBROUTINE
:
464 return RESOURCE_SUB(res
)->name
;
466 assert(!"support for resource type not implemented");
473 _mesa_program_resource_array_size(struct gl_program_resource
*res
)
476 case GL_TRANSFORM_FEEDBACK_VARYING
:
477 return RESOURCE_XFV(res
)->Size
> 1 ?
478 RESOURCE_XFV(res
)->Size
: 0;
479 case GL_PROGRAM_INPUT
:
480 case GL_PROGRAM_OUTPUT
:
481 return RESOURCE_VAR(res
)->type
->length
;
483 case GL_VERTEX_SUBROUTINE_UNIFORM
:
484 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
485 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
486 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
487 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
488 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
489 return RESOURCE_UNI(res
)->array_elements
;
490 case GL_BUFFER_VARIABLE
:
492 if (RESOURCE_UNI(res
)->array_stride
> 0 &&
493 RESOURCE_UNI(res
)->array_elements
== 0)
496 return RESOURCE_UNI(res
)->array_elements
;
497 case GL_VERTEX_SUBROUTINE
:
498 case GL_GEOMETRY_SUBROUTINE
:
499 case GL_FRAGMENT_SUBROUTINE
:
500 case GL_COMPUTE_SUBROUTINE
:
501 case GL_TESS_CONTROL_SUBROUTINE
:
502 case GL_TESS_EVALUATION_SUBROUTINE
:
503 case GL_ATOMIC_COUNTER_BUFFER
:
504 case GL_UNIFORM_BLOCK
:
505 case GL_SHADER_STORAGE_BLOCK
:
508 assert(!"support for resource type not implemented");
514 * Checks if array subscript is valid and if so sets array_index.
517 valid_array_index(const GLchar
*name
, unsigned *array_index
)
520 const GLchar
*out_base_name_end
;
522 idx
= parse_program_resource_name(name
, &out_base_name_end
);
532 /* Find a program resource with specific name in given interface.
534 struct gl_program_resource
*
535 _mesa_program_resource_find_name(struct gl_shader_program
*shProg
,
536 GLenum programInterface
, const char *name
,
537 unsigned *array_index
)
539 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
540 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
541 if (res
->Type
!= programInterface
)
544 /* Resource basename. */
545 const char *rname
= _mesa_program_resource_name(res
);
546 unsigned baselen
= strlen(rname
);
547 unsigned baselen_without_array_index
= baselen
;
548 const char *rname_last_square_bracket
= strrchr(rname
, '[');
550 bool rname_has_array_index_zero
= false;
551 /* From ARB_program_interface_query spec:
553 * "uint GetProgramResourceIndex(uint program, enum programInterface,
556 * If <name> exactly matches the name string of one of the active
557 * resources for <programInterface>, the index of the matched resource is
558 * returned. Additionally, if <name> would exactly match the name string
559 * of an active resource if "[0]" were appended to <name>, the index of
560 * the matched resource is returned. [...]"
562 * "A string provided to GetProgramResourceLocation or
563 * GetProgramResourceLocationIndex is considered to match an active variable
566 * * the string exactly matches the name of the active variable;
568 * * if the string identifies the base name of an active array, where the
569 * string would exactly match the name of the variable if the suffix
570 * "[0]" were appended to the string; [...]"
572 /* Remove array's index from interface block name comparison only if
573 * array's index is zero and the resulting string length is the same
574 * than the provided name's length.
576 if (rname_last_square_bracket
) {
577 baselen_without_array_index
-= strlen(rname_last_square_bracket
);
578 rname_has_array_index_zero
=
579 (strncmp(rname_last_square_bracket
, "[0]\0", 4) == 0) &&
580 (baselen_without_array_index
== strlen(name
));
583 if (strncmp(rname
, name
, baselen
) == 0)
585 else if (rname_has_array_index_zero
&&
586 strncmp(rname
, name
, baselen_without_array_index
) == 0)
590 switch (programInterface
) {
591 case GL_UNIFORM_BLOCK
:
592 case GL_SHADER_STORAGE_BLOCK
:
593 /* Basename match, check if array or struct. */
594 if (rname_has_array_index_zero
||
595 name
[baselen
] == '\0' ||
596 name
[baselen
] == '[' ||
597 name
[baselen
] == '.') {
601 case GL_TRANSFORM_FEEDBACK_VARYING
:
602 case GL_BUFFER_VARIABLE
:
604 case GL_VERTEX_SUBROUTINE_UNIFORM
:
605 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
606 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
607 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
608 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
609 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
610 case GL_VERTEX_SUBROUTINE
:
611 case GL_GEOMETRY_SUBROUTINE
:
612 case GL_FRAGMENT_SUBROUTINE
:
613 case GL_COMPUTE_SUBROUTINE
:
614 case GL_TESS_CONTROL_SUBROUTINE
:
615 case GL_TESS_EVALUATION_SUBROUTINE
:
616 if (name
[baselen
] == '.') {
620 case GL_PROGRAM_INPUT
:
621 case GL_PROGRAM_OUTPUT
:
622 if (name
[baselen
] == '\0') {
624 } else if (name
[baselen
] == '[' &&
625 valid_array_index(name
, array_index
)) {
630 assert(!"not implemented for given interface");
638 calc_resource_index(struct gl_shader_program
*shProg
,
639 struct gl_program_resource
*res
)
643 for (i
= 0; i
< shProg
->NumProgramResourceList
; i
++) {
644 if (&shProg
->ProgramResourceList
[i
] == res
)
646 if (shProg
->ProgramResourceList
[i
].Type
== res
->Type
)
649 return GL_INVALID_INDEX
;
653 * Calculate index for the given resource.
656 _mesa_program_resource_index(struct gl_shader_program
*shProg
,
657 struct gl_program_resource
*res
)
660 return GL_INVALID_INDEX
;
663 case GL_ATOMIC_COUNTER_BUFFER
:
664 return RESOURCE_ATC(res
) - shProg
->AtomicBuffers
;
665 case GL_VERTEX_SUBROUTINE
:
666 case GL_GEOMETRY_SUBROUTINE
:
667 case GL_FRAGMENT_SUBROUTINE
:
668 case GL_COMPUTE_SUBROUTINE
:
669 case GL_TESS_CONTROL_SUBROUTINE
:
670 case GL_TESS_EVALUATION_SUBROUTINE
:
671 return RESOURCE_SUB(res
)->index
;
672 case GL_UNIFORM_BLOCK
:
673 case GL_SHADER_STORAGE_BLOCK
:
674 case GL_TRANSFORM_FEEDBACK_BUFFER
:
675 case GL_TRANSFORM_FEEDBACK_VARYING
:
677 return calc_resource_index(shProg
, res
);
682 * Find a program resource that points to given data.
684 static struct gl_program_resource
*
685 program_resource_find_data(struct gl_shader_program
*shProg
, void *data
)
687 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
688 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
689 if (res
->Data
== data
)
695 /* Find a program resource with specific index in given interface.
697 struct gl_program_resource
*
698 _mesa_program_resource_find_index(struct gl_shader_program
*shProg
,
699 GLenum programInterface
, GLuint index
)
701 struct gl_program_resource
*res
= shProg
->ProgramResourceList
;
704 for (unsigned i
= 0; i
< shProg
->NumProgramResourceList
; i
++, res
++) {
705 if (res
->Type
!= programInterface
)
709 case GL_UNIFORM_BLOCK
:
710 case GL_ATOMIC_COUNTER_BUFFER
:
711 case GL_SHADER_STORAGE_BLOCK
:
712 case GL_TRANSFORM_FEEDBACK_BUFFER
:
713 if (_mesa_program_resource_index(shProg
, res
) == index
)
716 case GL_TRANSFORM_FEEDBACK_VARYING
:
717 case GL_PROGRAM_INPUT
:
718 case GL_PROGRAM_OUTPUT
:
720 case GL_VERTEX_SUBROUTINE_UNIFORM
:
721 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
722 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
723 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
724 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
725 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
726 case GL_VERTEX_SUBROUTINE
:
727 case GL_GEOMETRY_SUBROUTINE
:
728 case GL_FRAGMENT_SUBROUTINE
:
729 case GL_COMPUTE_SUBROUTINE
:
730 case GL_TESS_CONTROL_SUBROUTINE
:
731 case GL_TESS_EVALUATION_SUBROUTINE
:
732 case GL_BUFFER_VARIABLE
:
733 if (++idx
== (int) index
)
737 assert(!"not implemented for given interface");
743 /* Function returns if resource name is expected to have index
747 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
750 * "If the active uniform is an array, the uniform name returned in
751 * name will always be the name of the uniform array appended with
754 * The same text also appears in the OpenGL 4.2 spec. It does not,
755 * however, appear in any previous spec. Previous specifications are
756 * ambiguous in this regard. However, either name can later be passed
757 * to glGetUniformLocation (and related APIs), so there shouldn't be any
758 * harm in always appending "[0]" to uniform array names.
760 * Geometry shader stage has different naming convention where the 'normal'
761 * condition is an array, therefore for variables referenced in geometry
762 * stage we do not add '[0]'.
764 * Note, that TCS outputs and TES inputs should not have index appended
768 add_index_to_name(struct gl_program_resource
*res
)
770 bool add_index
= !((res
->Type
== GL_PROGRAM_INPUT
&&
771 res
->StageReferences
& (1 << MESA_SHADER_GEOMETRY
|
772 1 << MESA_SHADER_TESS_CTRL
|
773 1 << MESA_SHADER_TESS_EVAL
)) ||
774 (res
->Type
== GL_PROGRAM_OUTPUT
&&
775 res
->StageReferences
& 1 << MESA_SHADER_TESS_CTRL
));
777 /* Transform feedback varyings have array index already appended
780 if (res
->Type
== GL_TRANSFORM_FEEDBACK_VARYING
)
786 /* Get name length of a program resource. This consists of
787 * base name + 3 for '[0]' if resource is an array.
790 _mesa_program_resource_name_len(struct gl_program_resource
*res
)
792 unsigned length
= strlen(_mesa_program_resource_name(res
));
793 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
))
798 /* Get full name of a program resource.
801 _mesa_get_program_resource_name(struct gl_shader_program
*shProg
,
802 GLenum programInterface
, GLuint index
,
803 GLsizei bufSize
, GLsizei
*length
,
804 GLchar
*name
, const char *caller
)
806 GET_CURRENT_CONTEXT(ctx
);
808 /* Find resource with given interface and index. */
809 struct gl_program_resource
*res
=
810 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
812 /* The error INVALID_VALUE is generated if <index> is greater than
813 * or equal to the number of entries in the active resource list for
814 * <programInterface>.
817 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index %u)", caller
, index
);
822 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(bufSize %d)", caller
, bufSize
);
829 length
= &localLength
;
831 _mesa_copy_string(name
, bufSize
, length
, _mesa_program_resource_name(res
));
833 if (_mesa_program_resource_array_size(res
) && add_index_to_name(res
)) {
836 /* The comparison is strange because *length does *NOT* include the
837 * terminating NUL, but maxLength does.
839 for (i
= 0; i
< 3 && (*length
+ i
+ 1) < bufSize
; i
++)
840 name
[*length
+ i
] = "[0]"[i
];
842 name
[*length
+ i
] = '\0';
849 program_resource_location(struct gl_shader_program
*shProg
,
850 struct gl_program_resource
*res
, const char *name
,
851 unsigned array_index
)
853 /* Built-in locations should report GL_INVALID_INDEX. */
854 if (is_gl_identifier(name
))
855 return GL_INVALID_INDEX
;
857 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
858 * offsets are used internally to differentiate between built-in attributes
859 * and user-defined attributes.
862 case GL_PROGRAM_INPUT
: {
863 const gl_shader_variable
*var
= RESOURCE_VAR(res
);
865 /* If the input is an array, fail if the index is out of bounds. */
867 && array_index
>= var
->type
->length
) {
870 return (var
->location
+
871 (array_index
* var
->type
->without_array()->matrix_columns
) -
872 VERT_ATTRIB_GENERIC0
);
874 case GL_PROGRAM_OUTPUT
:
875 /* If the output is an array, fail if the index is out of bounds. */
877 && array_index
>= RESOURCE_VAR(res
)->type
->length
) {
880 return RESOURCE_VAR(res
)->location
+ array_index
- FRAG_RESULT_DATA0
;
882 /* If the uniform is built-in, fail. */
883 if (RESOURCE_UNI(res
)->builtin
)
886 /* From page 79 of the OpenGL 4.2 spec:
888 * "A valid name cannot be a structure, an array of structures, or any
889 * portion of a single vector or a matrix."
891 if (RESOURCE_UNI(res
)->type
->without_array()->is_record())
894 /* From the GL_ARB_uniform_buffer_object spec:
896 * "The value -1 will be returned if <name> does not correspond to an
897 * active uniform variable name in <program>, if <name> is associated
898 * with a named uniform block, or if <name> starts with the reserved
901 if (RESOURCE_UNI(res
)->block_index
!= -1 ||
902 RESOURCE_UNI(res
)->atomic_buffer_index
!= -1)
906 case GL_VERTEX_SUBROUTINE_UNIFORM
:
907 case GL_GEOMETRY_SUBROUTINE_UNIFORM
:
908 case GL_FRAGMENT_SUBROUTINE_UNIFORM
:
909 case GL_COMPUTE_SUBROUTINE_UNIFORM
:
910 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM
:
911 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
:
912 /* If the uniform is an array, fail if the index is out of bounds. */
914 && array_index
>= RESOURCE_UNI(res
)->array_elements
) {
918 /* location in remap table + array element offset */
919 return RESOURCE_UNI(res
)->remap_location
+ array_index
;
926 * Function implements following location queries:
927 * glGetUniformLocation
930 _mesa_program_resource_location(struct gl_shader_program
*shProg
,
931 GLenum programInterface
, const char *name
)
933 unsigned array_index
= 0;
934 struct gl_program_resource
*res
=
935 _mesa_program_resource_find_name(shProg
, programInterface
, name
,
938 /* Resource not found. */
942 return program_resource_location(shProg
, res
, name
, array_index
);
946 * Function implements following index queries:
950 _mesa_program_resource_location_index(struct gl_shader_program
*shProg
,
951 GLenum programInterface
, const char *name
)
953 struct gl_program_resource
*res
=
954 _mesa_program_resource_find_name(shProg
, programInterface
, name
, NULL
);
956 /* Non-existent variable or resource is not referenced by fragment stage. */
957 if (!res
|| !(res
->StageReferences
& (1 << MESA_SHADER_FRAGMENT
)))
960 return RESOURCE_VAR(res
)->index
;
964 stage_from_enum(GLenum ref
)
967 case GL_REFERENCED_BY_VERTEX_SHADER
:
968 return MESA_SHADER_VERTEX
;
969 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
970 return MESA_SHADER_TESS_CTRL
;
971 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
972 return MESA_SHADER_TESS_EVAL
;
973 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
974 return MESA_SHADER_GEOMETRY
;
975 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
976 return MESA_SHADER_FRAGMENT
;
977 case GL_REFERENCED_BY_COMPUTE_SHADER
:
978 return MESA_SHADER_COMPUTE
;
980 assert(!"shader stage not supported");
981 return MESA_SHADER_STAGES
;
986 * Check if resource is referenced by given 'referenced by' stage enum.
987 * ATC and UBO resources hold stage references of their own.
990 is_resource_referenced(struct gl_shader_program
*shProg
,
991 struct gl_program_resource
*res
,
992 GLuint index
, uint8_t stage
)
994 /* First, check if we even have such a stage active. */
995 if (!shProg
->_LinkedShaders
[stage
])
998 if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
)
999 return RESOURCE_ATC(res
)->StageReferences
[stage
];
1001 if (res
->Type
== GL_UNIFORM_BLOCK
|| res
->Type
== GL_SHADER_STORAGE_BLOCK
)
1002 return shProg
->InterfaceBlockStageIndex
[stage
][index
] != -1;
1004 return res
->StageReferences
& (1 << stage
);
1008 get_buffer_property(struct gl_shader_program
*shProg
,
1009 struct gl_program_resource
*res
, const GLenum prop
,
1010 GLint
*val
, const char *caller
)
1012 GET_CURRENT_CONTEXT(ctx
);
1013 if (res
->Type
!= GL_UNIFORM_BLOCK
&&
1014 res
->Type
!= GL_ATOMIC_COUNTER_BUFFER
&&
1015 res
->Type
!= GL_SHADER_STORAGE_BLOCK
&&
1016 res
->Type
!= GL_TRANSFORM_FEEDBACK_BUFFER
)
1017 goto invalid_operation
;
1019 if (res
->Type
== GL_UNIFORM_BLOCK
) {
1021 case GL_BUFFER_BINDING
:
1022 *val
= RESOURCE_UBO(res
)->Binding
;
1024 case GL_BUFFER_DATA_SIZE
:
1025 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1027 case GL_NUM_ACTIVE_VARIABLES
:
1029 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1030 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1031 struct gl_program_resource
*uni
=
1032 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1039 case GL_ACTIVE_VARIABLES
: {
1040 unsigned num_values
= 0;
1041 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1042 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1043 struct gl_program_resource
*uni
=
1044 _mesa_program_resource_find_name(shProg
, GL_UNIFORM
, iname
,
1049 _mesa_program_resource_index(shProg
, uni
);
1055 } else if (res
->Type
== GL_SHADER_STORAGE_BLOCK
) {
1057 case GL_BUFFER_BINDING
:
1058 *val
= RESOURCE_UBO(res
)->Binding
;
1060 case GL_BUFFER_DATA_SIZE
:
1061 *val
= RESOURCE_UBO(res
)->UniformBufferSize
;
1063 case GL_NUM_ACTIVE_VARIABLES
:
1065 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1066 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1067 struct gl_program_resource
*uni
=
1068 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1075 case GL_ACTIVE_VARIABLES
: {
1076 unsigned num_values
= 0;
1077 for (unsigned i
= 0; i
< RESOURCE_UBO(res
)->NumUniforms
; i
++) {
1078 const char *iname
= RESOURCE_UBO(res
)->Uniforms
[i
].IndexName
;
1079 struct gl_program_resource
*uni
=
1080 _mesa_program_resource_find_name(shProg
, GL_BUFFER_VARIABLE
,
1085 _mesa_program_resource_index(shProg
, uni
);
1091 } else if (res
->Type
== GL_ATOMIC_COUNTER_BUFFER
) {
1093 case GL_BUFFER_BINDING
:
1094 *val
= RESOURCE_ATC(res
)->Binding
;
1096 case GL_BUFFER_DATA_SIZE
:
1097 *val
= RESOURCE_ATC(res
)->MinimumSize
;
1099 case GL_NUM_ACTIVE_VARIABLES
:
1100 *val
= RESOURCE_ATC(res
)->NumUniforms
;
1102 case GL_ACTIVE_VARIABLES
:
1103 for (unsigned i
= 0; i
< RESOURCE_ATC(res
)->NumUniforms
; i
++) {
1104 /* Active atomic buffer contains index to UniformStorage. Find
1105 * out gl_program_resource via data pointer and then calculate
1106 * index of that uniform.
1108 unsigned idx
= RESOURCE_ATC(res
)->Uniforms
[i
];
1109 struct gl_program_resource
*uni
=
1110 program_resource_find_data(shProg
,
1111 &shProg
->UniformStorage
[idx
]);
1113 *val
++ = _mesa_program_resource_index(shProg
, uni
);
1115 return RESOURCE_ATC(res
)->NumUniforms
;
1117 } else if (res
->Type
== GL_TRANSFORM_FEEDBACK_BUFFER
) {
1119 case GL_BUFFER_BINDING
:
1120 *val
= RESOURCE_XFB(res
)->Binding
;
1122 case GL_NUM_ACTIVE_VARIABLES
:
1123 *val
= RESOURCE_XFB(res
)->NumVaryings
;
1125 case GL_ACTIVE_VARIABLES
:
1127 for ( ; i
< shProg
->LinkedTransformFeedback
.NumVarying
; i
++) {
1129 shProg
->LinkedTransformFeedback
.Varyings
[i
].BufferIndex
;
1130 struct gl_program_resource
*buf_res
=
1131 _mesa_program_resource_find_index(shProg
,
1132 GL_TRANSFORM_FEEDBACK_BUFFER
,
1135 if (res
== buf_res
) {
1139 return RESOURCE_XFB(res
)->NumVaryings
;
1142 assert(!"support for property type not implemented");
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1146 _mesa_enum_to_string(res
->Type
),
1147 _mesa_enum_to_string(prop
));
1153 _mesa_program_resource_prop(struct gl_shader_program
*shProg
,
1154 struct gl_program_resource
*res
, GLuint index
,
1155 const GLenum prop
, GLint
*val
, const char *caller
)
1157 GET_CURRENT_CONTEXT(ctx
);
1159 #define VALIDATE_TYPE(type)\
1160 if (res->Type != type)\
1161 goto invalid_operation;
1163 #define VALIDATE_TYPE_2(type1, type2)\
1164 if (res->Type != type1 && res->Type != type2)\
1165 goto invalid_operation;
1168 case GL_NAME_LENGTH
:
1169 switch (res
->Type
) {
1170 case GL_ATOMIC_COUNTER_BUFFER
:
1171 case GL_TRANSFORM_FEEDBACK_BUFFER
:
1172 goto invalid_operation
;
1174 /* Resource name length + terminator. */
1175 *val
= _mesa_program_resource_name_len(res
) + 1;
1179 switch (res
->Type
) {
1181 case GL_BUFFER_VARIABLE
:
1182 *val
= RESOURCE_UNI(res
)->type
->gl_type
;
1184 case GL_PROGRAM_INPUT
:
1185 case GL_PROGRAM_OUTPUT
:
1186 *val
= RESOURCE_VAR(res
)->type
->gl_type
;
1188 case GL_TRANSFORM_FEEDBACK_VARYING
:
1189 *val
= RESOURCE_XFV(res
)->Type
;
1192 goto invalid_operation
;
1195 switch (res
->Type
) {
1197 case GL_BUFFER_VARIABLE
:
1198 /* Test if a buffer variable is an array or an unsized array.
1199 * Unsized arrays return zero as array size.
1201 if (RESOURCE_UNI(res
)->is_shader_storage
&&
1202 RESOURCE_UNI(res
)->array_stride
> 0)
1203 *val
= RESOURCE_UNI(res
)->array_elements
;
1205 *val
= MAX2(RESOURCE_UNI(res
)->array_elements
, 1);
1207 case GL_PROGRAM_INPUT
:
1208 case GL_PROGRAM_OUTPUT
:
1209 *val
= MAX2(_mesa_program_resource_array_size(res
), 1);
1211 case GL_TRANSFORM_FEEDBACK_VARYING
:
1212 *val
= MAX2(RESOURCE_XFV(res
)->Size
, 1);
1215 goto invalid_operation
;
1218 switch (res
->Type
) {
1220 case GL_BUFFER_VARIABLE
:
1221 *val
= RESOURCE_UNI(res
)->offset
;
1223 case GL_TRANSFORM_FEEDBACK_VARYING
:
1224 *val
= RESOURCE_XFV(res
)->Offset
;
1227 goto invalid_operation
;
1229 case GL_BLOCK_INDEX
:
1230 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1231 *val
= RESOURCE_UNI(res
)->block_index
;
1233 case GL_ARRAY_STRIDE
:
1234 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1235 *val
= RESOURCE_UNI(res
)->array_stride
;
1237 case GL_MATRIX_STRIDE
:
1238 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1239 *val
= RESOURCE_UNI(res
)->matrix_stride
;
1241 case GL_IS_ROW_MAJOR
:
1242 VALIDATE_TYPE_2(GL_UNIFORM
, GL_BUFFER_VARIABLE
);
1243 *val
= RESOURCE_UNI(res
)->row_major
;
1245 case GL_ATOMIC_COUNTER_BUFFER_INDEX
:
1246 VALIDATE_TYPE(GL_UNIFORM
);
1247 *val
= RESOURCE_UNI(res
)->atomic_buffer_index
;
1249 case GL_BUFFER_BINDING
:
1250 case GL_BUFFER_DATA_SIZE
:
1251 case GL_NUM_ACTIVE_VARIABLES
:
1252 case GL_ACTIVE_VARIABLES
:
1253 return get_buffer_property(shProg
, res
, prop
, val
, caller
);
1254 case GL_REFERENCED_BY_COMPUTE_SHADER
:
1255 if (!_mesa_has_compute_shaders(ctx
))
1258 case GL_REFERENCED_BY_VERTEX_SHADER
:
1259 case GL_REFERENCED_BY_TESS_CONTROL_SHADER
:
1260 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER
:
1261 case GL_REFERENCED_BY_GEOMETRY_SHADER
:
1262 case GL_REFERENCED_BY_FRAGMENT_SHADER
:
1263 switch (res
->Type
) {
1265 case GL_PROGRAM_INPUT
:
1266 case GL_PROGRAM_OUTPUT
:
1267 case GL_UNIFORM_BLOCK
:
1268 case GL_BUFFER_VARIABLE
:
1269 case GL_SHADER_STORAGE_BLOCK
:
1270 case GL_ATOMIC_COUNTER_BUFFER
:
1271 *val
= is_resource_referenced(shProg
, res
, index
,
1272 stage_from_enum(prop
));
1275 goto invalid_operation
;
1278 switch (res
->Type
) {
1280 case GL_PROGRAM_INPUT
:
1281 case GL_PROGRAM_OUTPUT
:
1282 *val
= program_resource_location(shProg
, res
,
1283 _mesa_program_resource_name(res
),
1287 goto invalid_operation
;
1289 case GL_LOCATION_INDEX
:
1290 if (res
->Type
!= GL_PROGRAM_OUTPUT
)
1291 goto invalid_operation
;
1292 *val
= RESOURCE_VAR(res
)->index
;
1295 case GL_NUM_COMPATIBLE_SUBROUTINES
:
1296 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1297 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1298 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1299 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1300 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1301 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1302 goto invalid_operation
;
1303 *val
= RESOURCE_UNI(res
)->num_compatible_subroutines
;
1305 case GL_COMPATIBLE_SUBROUTINES
: {
1306 const struct gl_uniform_storage
*uni
;
1307 struct gl_shader
*sh
;
1311 if (res
->Type
!= GL_VERTEX_SUBROUTINE_UNIFORM
&&
1312 res
->Type
!= GL_FRAGMENT_SUBROUTINE_UNIFORM
&&
1313 res
->Type
!= GL_GEOMETRY_SUBROUTINE_UNIFORM
&&
1314 res
->Type
!= GL_COMPUTE_SUBROUTINE_UNIFORM
&&
1315 res
->Type
!= GL_TESS_CONTROL_SUBROUTINE_UNIFORM
&&
1316 res
->Type
!= GL_TESS_EVALUATION_SUBROUTINE_UNIFORM
)
1317 goto invalid_operation
;
1318 uni
= RESOURCE_UNI(res
);
1320 sh
= shProg
->_LinkedShaders
[_mesa_shader_stage_from_subroutine_uniform(res
->Type
)];
1322 for (i
= 0; i
< sh
->NumSubroutineFunctions
; i
++) {
1323 struct gl_subroutine_function
*fn
= &sh
->SubroutineFunctions
[i
];
1324 for (j
= 0; j
< fn
->num_compat_types
; j
++) {
1325 if (fn
->types
[j
] == uni
->type
) {
1334 case GL_TOP_LEVEL_ARRAY_SIZE
:
1335 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1336 *val
= RESOURCE_UNI(res
)->top_level_array_size
;
1339 case GL_TOP_LEVEL_ARRAY_STRIDE
:
1340 VALIDATE_TYPE(GL_BUFFER_VARIABLE
);
1341 *val
= RESOURCE_UNI(res
)->top_level_array_stride
;
1344 /* GL_ARB_tessellation_shader */
1345 case GL_IS_PER_PATCH
:
1346 switch (res
->Type
) {
1347 case GL_PROGRAM_INPUT
:
1348 case GL_PROGRAM_OUTPUT
:
1349 *val
= RESOURCE_VAR(res
)->patch
;
1352 goto invalid_operation
;
1355 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX
:
1356 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING
);
1357 *val
= RESOURCE_XFV(res
)->BufferIndex
;
1359 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE
:
1360 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER
);
1361 *val
= RESOURCE_XFB(res
)->Stride
* 4;
1368 #undef VALIDATE_TYPE
1369 #undef VALIDATE_TYPE_2
1372 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(%s prop %s)", caller
,
1373 _mesa_enum_to_string(res
->Type
),
1374 _mesa_enum_to_string(prop
));
1378 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(%s prop %s)", caller
,
1379 _mesa_enum_to_string(res
->Type
),
1380 _mesa_enum_to_string(prop
));
1385 _mesa_get_program_resourceiv(struct gl_shader_program
*shProg
,
1386 GLenum programInterface
, GLuint index
, GLsizei propCount
,
1387 const GLenum
*props
, GLsizei bufSize
,
1388 GLsizei
*length
, GLint
*params
)
1390 GET_CURRENT_CONTEXT(ctx
);
1391 GLint
*val
= (GLint
*) params
;
1392 const GLenum
*prop
= props
;
1395 struct gl_program_resource
*res
=
1396 _mesa_program_resource_find_index(shProg
, programInterface
, index
);
1398 /* No such resource found or bufSize negative. */
1399 if (!res
|| bufSize
< 0) {
1400 _mesa_error(ctx
, GL_INVALID_VALUE
,
1401 "glGetProgramResourceiv(%s index %d bufSize %d)",
1402 _mesa_enum_to_string(programInterface
), index
, bufSize
);
1406 /* Write propCount values until error occurs or bufSize reached. */
1407 for (int i
= 0; i
< propCount
&& i
< bufSize
; i
++, val
++, prop
++) {
1409 _mesa_program_resource_prop(shProg
, res
, index
, *prop
, val
,
1410 "glGetProgramResourceiv");
1412 /* Error happened. */
1413 if (props_written
== 0)
1416 amount
+= props_written
;
1419 /* If <length> is not NULL, the actual number of integer values
1420 * written to <params> will be written to <length>.
1427 validate_io(const struct gl_shader
*producer
,
1428 const struct gl_shader
*consumer
, bool isES
)
1430 assert(producer
&& consumer
);
1431 unsigned inputs
= 0, outputs
= 0;
1433 /* From OpenGL ES 3.1 spec (Interface matching):
1435 * "An output variable is considered to match an input variable in the
1436 * subsequent shader if:
1438 * - the two variables match in name, type, and qualification; or
1439 * - the two variables are declared with the same location qualifier and
1440 * match in type and qualification.
1444 * At an interface between program objects, the set of inputs and outputs
1445 * are considered to match exactly if and only if:
1447 * - Every declared input variable has a matching output, as described
1450 * - There are no user-defined output variables declared without a
1451 * matching input variable declaration.
1453 * - All matched input and output variables have identical precision
1456 * When the set of inputs and outputs on an interface between programs
1457 * matches exactly, all inputs are well-defined except when the
1458 * corresponding outputs were not written in the previous shader. However,
1459 * any mismatch between inputs and outputs will result in a validation
1462 * OpenGL Core 4.5 spec includes same paragraph as above but without check
1463 * for precision and the last 'validation failure' clause. Therefore
1464 * behaviour is more relaxed, input and output amount is not required by the
1465 * spec to be validated.
1467 * FIXME: Update once Khronos spec bug #15331 is resolved.
1468 * FIXME: Add validation by type, currently information loss during varying
1469 * packing makes this challenging.
1472 /* Currently no matching done for desktop. */
1476 /* For each output in a, find input in b and do any required checks. */
1477 foreach_in_list(ir_instruction
, out
, producer
->ir
) {
1478 ir_variable
*out_var
= out
->as_variable();
1479 if (!out_var
|| out_var
->data
.mode
!= ir_var_shader_out
||
1480 is_gl_identifier(out_var
->name
))
1486 foreach_in_list(ir_instruction
, in
, consumer
->ir
) {
1487 ir_variable
*in_var
= in
->as_variable();
1488 if (!in_var
|| in_var
->data
.mode
!= ir_var_shader_in
||
1489 is_gl_identifier(in_var
->name
))
1494 /* Match by location qualifier and precision.
1496 * FIXME: Add explicit location matching validation here. Be careful
1497 * not to match varyings with explicit locations to varyings without
1498 * explicit locations.
1500 if ((in_var
->data
.explicit_location
&&
1501 out_var
->data
.explicit_location
) &&
1502 in_var
->data
.location
== out_var
->data
.location
&&
1503 in_var
->data
.precision
== out_var
->data
.precision
)
1506 unsigned len
= strlen(in_var
->name
);
1508 /* Handle input swizzle in variable name. */
1509 const char *dot
= strchr(in_var
->name
, '.');
1511 len
= dot
- in_var
->name
;
1513 /* Match by name and precision. */
1514 if (strncmp(in_var
->name
, out_var
->name
, len
) == 0) {
1515 /* From OpenGL ES 3.1 spec:
1516 * "When both shaders are in separate programs, mismatched
1517 * precision qualifiers will result in a program interface
1518 * mismatch that will result in program pipeline validation
1519 * failures, as described in section 7.4.1 (“Shader Interface
1520 * Matching”) of the OpenGL ES 3.1 Specification."
1522 if (in_var
->data
.precision
!= out_var
->data
.precision
)
1527 return inputs
== outputs
;
1531 * Validate inputs against outputs in a program pipeline.
1534 _mesa_validate_pipeline_io(struct gl_pipeline_object
*pipeline
)
1536 struct gl_shader_program
**shProg
=
1537 (struct gl_shader_program
**) pipeline
->CurrentProgram
;
1539 /* Find first active stage in pipeline. */
1540 unsigned idx
, prev
= 0;
1541 for (idx
= 0; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1548 for (idx
= prev
+ 1; idx
< ARRAY_SIZE(pipeline
->CurrentProgram
); idx
++) {
1550 /* Pipeline might include both non-compute and a compute program, do
1551 * not attempt to validate varyings between non-compute and compute
1554 if (shProg
[idx
]->_LinkedShaders
[idx
]->Stage
== MESA_SHADER_COMPUTE
)
1557 if (!validate_io(shProg
[prev
]->_LinkedShaders
[prev
],
1558 shProg
[idx
]->_LinkedShaders
[idx
],
1559 shProg
[prev
]->IsES
|| shProg
[idx
]->IsES
))