mesa: Trivial clean-ups in uniform_query.cpp
[mesa.git] / src / mesa / main / shader_query.cpp
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
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 "util/string_to_uint_map.h"
41 #include "util/strndup.h"
42
43
44 static GLint
45 program_resource_location(struct gl_program_resource *res,
46 unsigned array_index);
47
48 /**
49 * Declare convenience functions to return resource data in a given type.
50 * Warning! this is not type safe so be *very* careful when using these.
51 */
52 #define DECL_RESOURCE_FUNC(name, type) \
53 const type * RESOURCE_ ## name (gl_program_resource *res) { \
54 assert(res->Data); \
55 return (type *) res->Data; \
56 }
57
58 DECL_RESOURCE_FUNC(VAR, gl_shader_variable);
59 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
60 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
61 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
62 DECL_RESOURCE_FUNC(XFV, gl_transform_feedback_varying_info);
63 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
64 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
65
66 void GLAPIENTRY
67 _mesa_BindAttribLocation(GLuint program, GLuint index,
68 const GLchar *name)
69 {
70 GET_CURRENT_CONTEXT(ctx);
71
72 struct gl_shader_program *const shProg =
73 _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
74 if (!shProg)
75 return;
76
77 if (!name)
78 return;
79
80 if (strncmp(name, "gl_", 3) == 0) {
81 _mesa_error(ctx, GL_INVALID_OPERATION,
82 "glBindAttribLocation(illegal name)");
83 return;
84 }
85
86 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
87 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(%u >= %u)",
88 index, ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs);
89 return;
90 }
91
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.
95 */
96 shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
97
98 /*
99 * Note that this attribute binding won't go into effect until
100 * glLinkProgram is called again.
101 */
102 }
103
104 void GLAPIENTRY
105 _mesa_GetActiveAttrib(GLuint program, GLuint desired_index,
106 GLsizei maxLength, GLsizei * length, GLint * size,
107 GLenum * type, GLchar * name)
108 {
109 GET_CURRENT_CONTEXT(ctx);
110 struct gl_shader_program *shProg;
111
112 if (maxLength < 0) {
113 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
114 return;
115 }
116
117 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
118 if (!shProg)
119 return;
120
121 if (!shProg->data->LinkStatus) {
122 _mesa_error(ctx, GL_INVALID_VALUE,
123 "glGetActiveAttrib(program not linked)");
124 return;
125 }
126
127 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
128 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
129 return;
130 }
131
132 struct gl_program_resource *res =
133 _mesa_program_resource_find_index(shProg, GL_PROGRAM_INPUT,
134 desired_index);
135
136 /* User asked for index that does not exist. */
137 if (!res) {
138 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
139 return;
140 }
141
142 const gl_shader_variable *const var = RESOURCE_VAR(res);
143
144 const char *var_name = var->name;
145
146 _mesa_copy_string(name, maxLength, length, var_name);
147
148 if (size)
149 _mesa_program_resource_prop(shProg, res, desired_index, GL_ARRAY_SIZE,
150 size, "glGetActiveAttrib");
151
152 if (type)
153 _mesa_program_resource_prop(shProg, res, desired_index, GL_TYPE,
154 (GLint *) type, "glGetActiveAttrib");
155 }
156
157 GLint GLAPIENTRY
158 _mesa_GetAttribLocation(GLuint program, const GLchar * name)
159 {
160 GET_CURRENT_CONTEXT(ctx);
161 struct gl_shader_program *const shProg =
162 _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
163
164 if (!shProg) {
165 return -1;
166 }
167
168 if (!shProg->data->LinkStatus) {
169 _mesa_error(ctx, GL_INVALID_OPERATION,
170 "glGetAttribLocation(program not linked)");
171 return -1;
172 }
173
174 if (!name)
175 return -1;
176
177 /* Not having a vertex shader is not an error.
178 */
179 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
180 return -1;
181
182 unsigned array_index = 0;
183 struct gl_program_resource *res =
184 _mesa_program_resource_find_name(shProg, GL_PROGRAM_INPUT, name,
185 &array_index);
186
187 if (!res)
188 return -1;
189
190 return program_resource_location(res, array_index);
191 }
192
193 unsigned
194 _mesa_count_active_attribs(struct gl_shader_program *shProg)
195 {
196 if (!shProg->data->LinkStatus
197 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
198 return 0;
199 }
200
201 struct gl_program_resource *res = shProg->data->ProgramResourceList;
202 unsigned count = 0;
203 for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
204 j++, res++) {
205 if (res->Type == GL_PROGRAM_INPUT &&
206 res->StageReferences & (1 << MESA_SHADER_VERTEX))
207 count++;
208 }
209 return count;
210 }
211
212
213 size_t
214 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
215 {
216 if (!shProg->data->LinkStatus
217 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
218 return 0;
219 }
220
221 struct gl_program_resource *res = shProg->data->ProgramResourceList;
222 size_t longest = 0;
223 for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
224 j++, res++) {
225 if (res->Type == GL_PROGRAM_INPUT &&
226 res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
227
228 const size_t length = strlen(RESOURCE_VAR(res)->name);
229 if (length >= longest)
230 longest = length + 1;
231 }
232 }
233
234 return longest;
235 }
236
237 void GLAPIENTRY
238 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
239 const GLchar *name)
240 {
241 _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
242 }
243
244 void GLAPIENTRY
245 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
246 GLuint index, const GLchar *name)
247 {
248 GET_CURRENT_CONTEXT(ctx);
249
250 struct gl_shader_program *const shProg =
251 _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
252 if (!shProg)
253 return;
254
255 if (!name)
256 return;
257
258 if (strncmp(name, "gl_", 3) == 0) {
259 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
260 return;
261 }
262
263 if (index > 1) {
264 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
265 return;
266 }
267
268 if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
269 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
270 return;
271 }
272
273 if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
274 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
275 return;
276 }
277
278 /* Replace the current value if it's already in the list. Add
279 * FRAG_RESULT_DATA0 because that's how the linker differentiates
280 * between built-in attributes and user-defined attributes.
281 */
282 shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
283 shProg->FragDataIndexBindings->put(index, name);
284 /*
285 * Note that this binding won't go into effect until
286 * glLinkProgram is called again.
287 */
288
289 }
290
291 GLint GLAPIENTRY
292 _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
293 {
294 GET_CURRENT_CONTEXT(ctx);
295 struct gl_shader_program *const shProg =
296 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
297
298 if (!shProg) {
299 return -1;
300 }
301
302 if (!shProg->data->LinkStatus) {
303 _mesa_error(ctx, GL_INVALID_OPERATION,
304 "glGetFragDataIndex(program not linked)");
305 return -1;
306 }
307
308 if (!name)
309 return -1;
310
311 if (strncmp(name, "gl_", 3) == 0) {
312 _mesa_error(ctx, GL_INVALID_OPERATION,
313 "glGetFragDataIndex(illegal name)");
314 return -1;
315 }
316
317 /* Not having a fragment shader is not an error.
318 */
319 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
320 return -1;
321
322 return _mesa_program_resource_location_index(shProg, GL_PROGRAM_OUTPUT,
323 name);
324 }
325
326 GLint GLAPIENTRY
327 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
328 {
329 GET_CURRENT_CONTEXT(ctx);
330 struct gl_shader_program *const shProg =
331 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
332
333 if (!shProg) {
334 return -1;
335 }
336
337 if (!shProg->data->LinkStatus) {
338 _mesa_error(ctx, GL_INVALID_OPERATION,
339 "glGetFragDataLocation(program not linked)");
340 return -1;
341 }
342
343 if (!name)
344 return -1;
345
346 if (strncmp(name, "gl_", 3) == 0) {
347 _mesa_error(ctx, GL_INVALID_OPERATION,
348 "glGetFragDataLocation(illegal name)");
349 return -1;
350 }
351
352 /* Not having a fragment shader is not an error.
353 */
354 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
355 return -1;
356
357 unsigned array_index = 0;
358 struct gl_program_resource *res =
359 _mesa_program_resource_find_name(shProg, GL_PROGRAM_OUTPUT, name,
360 &array_index);
361
362 if (!res)
363 return -1;
364
365 return program_resource_location(res, array_index);
366 }
367
368 const char*
369 _mesa_program_resource_name(struct gl_program_resource *res)
370 {
371 switch (res->Type) {
372 case GL_UNIFORM_BLOCK:
373 case GL_SHADER_STORAGE_BLOCK:
374 return RESOURCE_UBO(res)->Name;
375 case GL_TRANSFORM_FEEDBACK_VARYING:
376 return RESOURCE_XFV(res)->Name;
377 case GL_PROGRAM_INPUT:
378 case GL_PROGRAM_OUTPUT:
379 return RESOURCE_VAR(res)->name;
380 case GL_UNIFORM:
381 case GL_BUFFER_VARIABLE:
382 return RESOURCE_UNI(res)->name;
383 case GL_VERTEX_SUBROUTINE_UNIFORM:
384 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
385 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
386 case GL_COMPUTE_SUBROUTINE_UNIFORM:
387 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
388 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
389 return RESOURCE_UNI(res)->name + MESA_SUBROUTINE_PREFIX_LEN;
390 case GL_VERTEX_SUBROUTINE:
391 case GL_GEOMETRY_SUBROUTINE:
392 case GL_FRAGMENT_SUBROUTINE:
393 case GL_COMPUTE_SUBROUTINE:
394 case GL_TESS_CONTROL_SUBROUTINE:
395 case GL_TESS_EVALUATION_SUBROUTINE:
396 return RESOURCE_SUB(res)->name;
397 default:
398 assert(!"support for resource type not implemented");
399 }
400 return NULL;
401 }
402
403
404 unsigned
405 _mesa_program_resource_array_size(struct gl_program_resource *res)
406 {
407 switch (res->Type) {
408 case GL_TRANSFORM_FEEDBACK_VARYING:
409 return RESOURCE_XFV(res)->Size > 1 ?
410 RESOURCE_XFV(res)->Size : 0;
411 case GL_PROGRAM_INPUT:
412 case GL_PROGRAM_OUTPUT:
413 return RESOURCE_VAR(res)->type->length;
414 case GL_UNIFORM:
415 case GL_VERTEX_SUBROUTINE_UNIFORM:
416 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
417 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
418 case GL_COMPUTE_SUBROUTINE_UNIFORM:
419 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
420 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
421 return RESOURCE_UNI(res)->array_elements;
422 case GL_BUFFER_VARIABLE:
423 /* Unsized arrays */
424 if (RESOURCE_UNI(res)->array_stride > 0 &&
425 RESOURCE_UNI(res)->array_elements == 0)
426 return 1;
427 else
428 return RESOURCE_UNI(res)->array_elements;
429 case GL_VERTEX_SUBROUTINE:
430 case GL_GEOMETRY_SUBROUTINE:
431 case GL_FRAGMENT_SUBROUTINE:
432 case GL_COMPUTE_SUBROUTINE:
433 case GL_TESS_CONTROL_SUBROUTINE:
434 case GL_TESS_EVALUATION_SUBROUTINE:
435 case GL_ATOMIC_COUNTER_BUFFER:
436 case GL_UNIFORM_BLOCK:
437 case GL_SHADER_STORAGE_BLOCK:
438 return 0;
439 default:
440 assert(!"support for resource type not implemented");
441 }
442 return 0;
443 }
444
445 /**
446 * Checks if array subscript is valid and if so sets array_index.
447 */
448 static bool
449 valid_array_index(const GLchar *name, unsigned *array_index)
450 {
451 long idx = 0;
452 const GLchar *out_base_name_end;
453
454 idx = parse_program_resource_name(name, &out_base_name_end);
455 if (idx < 0)
456 return false;
457
458 if (array_index)
459 *array_index = idx;
460
461 return true;
462 }
463
464 /* Find a program resource with specific name in given interface.
465 */
466 struct gl_program_resource *
467 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
468 GLenum programInterface, const char *name,
469 unsigned *array_index)
470 {
471 struct gl_program_resource *res = shProg->data->ProgramResourceList;
472 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
473 i++, res++) {
474 if (res->Type != programInterface)
475 continue;
476
477 /* Resource basename. */
478 const char *rname = _mesa_program_resource_name(res);
479 unsigned baselen = strlen(rname);
480 unsigned baselen_without_array_index = baselen;
481 const char *rname_last_square_bracket = strrchr(rname, '[');
482 bool found = false;
483 bool rname_has_array_index_zero = false;
484 /* From ARB_program_interface_query spec:
485 *
486 * "uint GetProgramResourceIndex(uint program, enum programInterface,
487 * const char *name);
488 * [...]
489 * If <name> exactly matches the name string of one of the active
490 * resources for <programInterface>, the index of the matched resource is
491 * returned. Additionally, if <name> would exactly match the name string
492 * of an active resource if "[0]" were appended to <name>, the index of
493 * the matched resource is returned. [...]"
494 *
495 * "A string provided to GetProgramResourceLocation or
496 * GetProgramResourceLocationIndex is considered to match an active variable
497 * if:
498 *
499 * * the string exactly matches the name of the active variable;
500 *
501 * * if the string identifies the base name of an active array, where the
502 * string would exactly match the name of the variable if the suffix
503 * "[0]" were appended to the string; [...]"
504 */
505 /* Remove array's index from interface block name comparison only if
506 * array's index is zero and the resulting string length is the same
507 * than the provided name's length.
508 */
509 if (rname_last_square_bracket) {
510 baselen_without_array_index -= strlen(rname_last_square_bracket);
511 rname_has_array_index_zero =
512 (strcmp(rname_last_square_bracket, "[0]") == 0) &&
513 (baselen_without_array_index == strlen(name));
514 }
515
516 if (strncmp(rname, name, baselen) == 0)
517 found = true;
518 else if (rname_has_array_index_zero &&
519 strncmp(rname, name, baselen_without_array_index) == 0)
520 found = true;
521
522 if (found) {
523 switch (programInterface) {
524 case GL_UNIFORM_BLOCK:
525 case GL_SHADER_STORAGE_BLOCK:
526 /* Basename match, check if array or struct. */
527 if (rname_has_array_index_zero ||
528 name[baselen] == '\0' ||
529 name[baselen] == '[' ||
530 name[baselen] == '.') {
531 return res;
532 }
533 break;
534 case GL_TRANSFORM_FEEDBACK_VARYING:
535 case GL_BUFFER_VARIABLE:
536 case GL_UNIFORM:
537 case GL_VERTEX_SUBROUTINE_UNIFORM:
538 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
539 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
540 case GL_COMPUTE_SUBROUTINE_UNIFORM:
541 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
542 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
543 case GL_VERTEX_SUBROUTINE:
544 case GL_GEOMETRY_SUBROUTINE:
545 case GL_FRAGMENT_SUBROUTINE:
546 case GL_COMPUTE_SUBROUTINE:
547 case GL_TESS_CONTROL_SUBROUTINE:
548 case GL_TESS_EVALUATION_SUBROUTINE:
549 if (name[baselen] == '.') {
550 return res;
551 }
552 /* fall-through */
553 case GL_PROGRAM_INPUT:
554 case GL_PROGRAM_OUTPUT:
555 if (name[baselen] == '\0') {
556 return res;
557 } else if (name[baselen] == '[' &&
558 valid_array_index(name, array_index)) {
559 return res;
560 }
561 break;
562 default:
563 assert(!"not implemented for given interface");
564 }
565 }
566 }
567 return NULL;
568 }
569
570 static GLuint
571 calc_resource_index(struct gl_shader_program *shProg,
572 struct gl_program_resource *res)
573 {
574 unsigned i;
575 GLuint index = 0;
576 for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
577 if (&shProg->data->ProgramResourceList[i] == res)
578 return index;
579 if (shProg->data->ProgramResourceList[i].Type == res->Type)
580 index++;
581 }
582 return GL_INVALID_INDEX;
583 }
584
585 /**
586 * Calculate index for the given resource.
587 */
588 GLuint
589 _mesa_program_resource_index(struct gl_shader_program *shProg,
590 struct gl_program_resource *res)
591 {
592 if (!res)
593 return GL_INVALID_INDEX;
594
595 switch (res->Type) {
596 case GL_ATOMIC_COUNTER_BUFFER:
597 return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
598 case GL_VERTEX_SUBROUTINE:
599 case GL_GEOMETRY_SUBROUTINE:
600 case GL_FRAGMENT_SUBROUTINE:
601 case GL_COMPUTE_SUBROUTINE:
602 case GL_TESS_CONTROL_SUBROUTINE:
603 case GL_TESS_EVALUATION_SUBROUTINE:
604 return RESOURCE_SUB(res)->index;
605 case GL_UNIFORM_BLOCK:
606 case GL_SHADER_STORAGE_BLOCK:
607 case GL_TRANSFORM_FEEDBACK_BUFFER:
608 case GL_TRANSFORM_FEEDBACK_VARYING:
609 default:
610 return calc_resource_index(shProg, res);
611 }
612 }
613
614 /**
615 * Find a program resource that points to given data.
616 */
617 static struct gl_program_resource*
618 program_resource_find_data(struct gl_shader_program *shProg, void *data)
619 {
620 struct gl_program_resource *res = shProg->data->ProgramResourceList;
621 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
622 i++, res++) {
623 if (res->Data == data)
624 return res;
625 }
626 return NULL;
627 }
628
629 /* Find a program resource with specific index in given interface.
630 */
631 struct gl_program_resource *
632 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
633 GLenum programInterface, GLuint index)
634 {
635 struct gl_program_resource *res = shProg->data->ProgramResourceList;
636 int idx = -1;
637
638 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
639 i++, res++) {
640 if (res->Type != programInterface)
641 continue;
642
643 switch (res->Type) {
644 case GL_UNIFORM_BLOCK:
645 case GL_ATOMIC_COUNTER_BUFFER:
646 case GL_SHADER_STORAGE_BLOCK:
647 case GL_TRANSFORM_FEEDBACK_BUFFER:
648 if (_mesa_program_resource_index(shProg, res) == index)
649 return res;
650 break;
651 case GL_TRANSFORM_FEEDBACK_VARYING:
652 case GL_PROGRAM_INPUT:
653 case GL_PROGRAM_OUTPUT:
654 case GL_UNIFORM:
655 case GL_VERTEX_SUBROUTINE_UNIFORM:
656 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
657 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
658 case GL_COMPUTE_SUBROUTINE_UNIFORM:
659 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
660 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
661 case GL_VERTEX_SUBROUTINE:
662 case GL_GEOMETRY_SUBROUTINE:
663 case GL_FRAGMENT_SUBROUTINE:
664 case GL_COMPUTE_SUBROUTINE:
665 case GL_TESS_CONTROL_SUBROUTINE:
666 case GL_TESS_EVALUATION_SUBROUTINE:
667 case GL_BUFFER_VARIABLE:
668 if (++idx == (int) index)
669 return res;
670 break;
671 default:
672 assert(!"not implemented for given interface");
673 }
674 }
675 return NULL;
676 }
677
678 /* Function returns if resource name is expected to have index
679 * appended into it.
680 *
681 *
682 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
683 * spec says:
684 *
685 * "If the active uniform is an array, the uniform name returned in
686 * name will always be the name of the uniform array appended with
687 * "[0]"."
688 *
689 * The same text also appears in the OpenGL 4.2 spec. It does not,
690 * however, appear in any previous spec. Previous specifications are
691 * ambiguous in this regard. However, either name can later be passed
692 * to glGetUniformLocation (and related APIs), so there shouldn't be any
693 * harm in always appending "[0]" to uniform array names.
694 */
695 static bool
696 add_index_to_name(struct gl_program_resource *res)
697 {
698 /* Transform feedback varyings have array index already appended
699 * in their names.
700 */
701 return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
702 }
703
704 /* Get name length of a program resource. This consists of
705 * base name + 3 for '[0]' if resource is an array.
706 */
707 extern unsigned
708 _mesa_program_resource_name_len(struct gl_program_resource *res)
709 {
710 unsigned length = strlen(_mesa_program_resource_name(res));
711 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
712 length += 3;
713 return length;
714 }
715
716 /* Get full name of a program resource.
717 */
718 bool
719 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
720 GLenum programInterface, GLuint index,
721 GLsizei bufSize, GLsizei *length,
722 GLchar *name, const char *caller)
723 {
724 GET_CURRENT_CONTEXT(ctx);
725
726 /* Find resource with given interface and index. */
727 struct gl_program_resource *res =
728 _mesa_program_resource_find_index(shProg, programInterface, index);
729
730 /* The error INVALID_VALUE is generated if <index> is greater than
731 * or equal to the number of entries in the active resource list for
732 * <programInterface>.
733 */
734 if (!res) {
735 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
736 return false;
737 }
738
739 if (bufSize < 0) {
740 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
741 return false;
742 }
743
744 GLsizei localLength;
745
746 if (length == NULL)
747 length = &localLength;
748
749 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
750
751 if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
752 int i;
753
754 /* The comparison is strange because *length does *NOT* include the
755 * terminating NUL, but maxLength does.
756 */
757 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
758 name[*length + i] = "[0]"[i];
759
760 name[*length + i] = '\0';
761 *length += i;
762 }
763 return true;
764 }
765
766 static GLint
767 program_resource_location(struct gl_program_resource *res, unsigned array_index)
768 {
769 switch (res->Type) {
770 case GL_PROGRAM_INPUT: {
771 const gl_shader_variable *var = RESOURCE_VAR(res);
772
773 if (var->location == -1)
774 return -1;
775
776 /* If the input is an array, fail if the index is out of bounds. */
777 if (array_index > 0
778 && array_index >= var->type->length) {
779 return -1;
780 }
781 return var->location +
782 (array_index * var->type->without_array()->matrix_columns);
783 }
784 case GL_PROGRAM_OUTPUT:
785 if (RESOURCE_VAR(res)->location == -1)
786 return -1;
787
788 /* If the output is an array, fail if the index is out of bounds. */
789 if (array_index > 0
790 && array_index >= RESOURCE_VAR(res)->type->length) {
791 return -1;
792 }
793 return RESOURCE_VAR(res)->location + array_index;
794 case GL_UNIFORM:
795 /* If the uniform is built-in, fail. */
796 if (RESOURCE_UNI(res)->builtin)
797 return -1;
798
799 /* From page 79 of the OpenGL 4.2 spec:
800 *
801 * "A valid name cannot be a structure, an array of structures, or any
802 * portion of a single vector or a matrix."
803 */
804 if (RESOURCE_UNI(res)->type->without_array()->is_record())
805 return -1;
806
807 /* From the GL_ARB_uniform_buffer_object spec:
808 *
809 * "The value -1 will be returned if <name> does not correspond to an
810 * active uniform variable name in <program>, if <name> is associated
811 * with a named uniform block, or if <name> starts with the reserved
812 * prefix "gl_"."
813 */
814 if (RESOURCE_UNI(res)->block_index != -1 ||
815 RESOURCE_UNI(res)->atomic_buffer_index != -1)
816 return -1;
817
818 /* fallthrough */
819 case GL_VERTEX_SUBROUTINE_UNIFORM:
820 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
821 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
822 case GL_COMPUTE_SUBROUTINE_UNIFORM:
823 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
824 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
825 /* If the uniform is an array, fail if the index is out of bounds. */
826 if (array_index > 0
827 && array_index >= RESOURCE_UNI(res)->array_elements) {
828 return -1;
829 }
830
831 /* location in remap table + array element offset */
832 return RESOURCE_UNI(res)->remap_location + array_index;
833 default:
834 return -1;
835 }
836 }
837
838 /**
839 * Function implements following location queries:
840 * glGetUniformLocation
841 */
842 GLint
843 _mesa_program_resource_location(struct gl_shader_program *shProg,
844 GLenum programInterface, const char *name)
845 {
846 unsigned array_index = 0;
847 struct gl_program_resource *res =
848 _mesa_program_resource_find_name(shProg, programInterface, name,
849 &array_index);
850
851 /* Resource not found. */
852 if (!res)
853 return -1;
854
855 return program_resource_location(res, array_index);
856 }
857
858 /**
859 * Function implements following index queries:
860 * glGetFragDataIndex
861 */
862 GLint
863 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
864 GLenum programInterface, const char *name)
865 {
866 struct gl_program_resource *res =
867 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
868
869 /* Non-existent variable or resource is not referenced by fragment stage. */
870 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
871 return -1;
872
873 /* From OpenGL 4.5 spec, 7.3 Program Objects
874 * "The value -1 will be returned by either command...
875 * ... or if name identifies an active variable that does not have a
876 * valid location assigned.
877 */
878 if (RESOURCE_VAR(res)->location == -1)
879 return -1;
880 return RESOURCE_VAR(res)->index;
881 }
882
883 static uint8_t
884 stage_from_enum(GLenum ref)
885 {
886 switch (ref) {
887 case GL_REFERENCED_BY_VERTEX_SHADER:
888 return MESA_SHADER_VERTEX;
889 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
890 return MESA_SHADER_TESS_CTRL;
891 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
892 return MESA_SHADER_TESS_EVAL;
893 case GL_REFERENCED_BY_GEOMETRY_SHADER:
894 return MESA_SHADER_GEOMETRY;
895 case GL_REFERENCED_BY_FRAGMENT_SHADER:
896 return MESA_SHADER_FRAGMENT;
897 case GL_REFERENCED_BY_COMPUTE_SHADER:
898 return MESA_SHADER_COMPUTE;
899 default:
900 assert(!"shader stage not supported");
901 return MESA_SHADER_STAGES;
902 }
903 }
904
905 /**
906 * Check if resource is referenced by given 'referenced by' stage enum.
907 * ATC and UBO resources hold stage references of their own.
908 */
909 static bool
910 is_resource_referenced(struct gl_shader_program *shProg,
911 struct gl_program_resource *res,
912 GLuint index, uint8_t stage)
913 {
914 /* First, check if we even have such a stage active. */
915 if (!shProg->_LinkedShaders[stage])
916 return false;
917
918 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
919 return RESOURCE_ATC(res)->StageReferences[stage];
920
921 if (res->Type == GL_UNIFORM_BLOCK)
922 return shProg->data->UniformBlocks[index].stageref & (1 << stage);
923
924 if (res->Type == GL_SHADER_STORAGE_BLOCK)
925 return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
926
927 return res->StageReferences & (1 << stage);
928 }
929
930 static unsigned
931 get_buffer_property(struct gl_shader_program *shProg,
932 struct gl_program_resource *res, const GLenum prop,
933 GLint *val, const char *caller)
934 {
935 GET_CURRENT_CONTEXT(ctx);
936 if (res->Type != GL_UNIFORM_BLOCK &&
937 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
938 res->Type != GL_SHADER_STORAGE_BLOCK &&
939 res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
940 goto invalid_operation;
941
942 if (res->Type == GL_UNIFORM_BLOCK) {
943 switch (prop) {
944 case GL_BUFFER_BINDING:
945 *val = RESOURCE_UBO(res)->Binding;
946 return 1;
947 case GL_BUFFER_DATA_SIZE:
948 *val = RESOURCE_UBO(res)->UniformBufferSize;
949 return 1;
950 case GL_NUM_ACTIVE_VARIABLES:
951 *val = 0;
952 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
953 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
954 struct gl_program_resource *uni =
955 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
956 NULL);
957 if (!uni)
958 continue;
959 (*val)++;
960 }
961 return 1;
962 case GL_ACTIVE_VARIABLES: {
963 unsigned num_values = 0;
964 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
965 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
966 struct gl_program_resource *uni =
967 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
968 NULL);
969 if (!uni)
970 continue;
971 *val++ =
972 _mesa_program_resource_index(shProg, uni);
973 num_values++;
974 }
975 return num_values;
976 }
977 }
978 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
979 switch (prop) {
980 case GL_BUFFER_BINDING:
981 *val = RESOURCE_UBO(res)->Binding;
982 return 1;
983 case GL_BUFFER_DATA_SIZE:
984 *val = RESOURCE_UBO(res)->UniformBufferSize;
985 return 1;
986 case GL_NUM_ACTIVE_VARIABLES:
987 *val = 0;
988 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
989 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
990 struct gl_program_resource *uni =
991 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
992 iname, NULL);
993 if (!uni)
994 continue;
995 (*val)++;
996 }
997 return 1;
998 case GL_ACTIVE_VARIABLES: {
999 unsigned num_values = 0;
1000 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1001 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1002 struct gl_program_resource *uni =
1003 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1004 iname, NULL);
1005 if (!uni)
1006 continue;
1007 *val++ =
1008 _mesa_program_resource_index(shProg, uni);
1009 num_values++;
1010 }
1011 return num_values;
1012 }
1013 }
1014 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1015 switch (prop) {
1016 case GL_BUFFER_BINDING:
1017 *val = RESOURCE_ATC(res)->Binding;
1018 return 1;
1019 case GL_BUFFER_DATA_SIZE:
1020 *val = RESOURCE_ATC(res)->MinimumSize;
1021 return 1;
1022 case GL_NUM_ACTIVE_VARIABLES:
1023 *val = RESOURCE_ATC(res)->NumUniforms;
1024 return 1;
1025 case GL_ACTIVE_VARIABLES:
1026 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1027 /* Active atomic buffer contains index to UniformStorage. Find
1028 * out gl_program_resource via data pointer and then calculate
1029 * index of that uniform.
1030 */
1031 unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1032 struct gl_program_resource *uni =
1033 program_resource_find_data(shProg,
1034 &shProg->data->UniformStorage[idx]);
1035 assert(uni);
1036 *val++ = _mesa_program_resource_index(shProg, uni);
1037 }
1038 return RESOURCE_ATC(res)->NumUniforms;
1039 }
1040 } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1041 switch (prop) {
1042 case GL_BUFFER_BINDING:
1043 *val = RESOURCE_XFB(res)->Binding;
1044 return 1;
1045 case GL_NUM_ACTIVE_VARIABLES:
1046 *val = RESOURCE_XFB(res)->NumVaryings;
1047 return 1;
1048 case GL_ACTIVE_VARIABLES:
1049 struct gl_transform_feedback_info *linked_xfb =
1050 shProg->last_vert_prog->sh.LinkedTransformFeedback;
1051 for (int i = 0; i < linked_xfb->NumVarying; i++) {
1052 unsigned index = linked_xfb->Varyings[i].BufferIndex;
1053 struct gl_program_resource *buf_res =
1054 _mesa_program_resource_find_index(shProg,
1055 GL_TRANSFORM_FEEDBACK_BUFFER,
1056 index);
1057 assert(buf_res);
1058 if (res == buf_res) {
1059 *val++ = i;
1060 }
1061 }
1062 return RESOURCE_XFB(res)->NumVaryings;
1063 }
1064 }
1065 assert(!"support for property type not implemented");
1066
1067 invalid_operation:
1068 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1069 _mesa_enum_to_string(res->Type),
1070 _mesa_enum_to_string(prop));
1071
1072 return 0;
1073 }
1074
1075 unsigned
1076 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1077 struct gl_program_resource *res, GLuint index,
1078 const GLenum prop, GLint *val, const char *caller)
1079 {
1080 GET_CURRENT_CONTEXT(ctx);
1081
1082 #define VALIDATE_TYPE(type)\
1083 if (res->Type != type)\
1084 goto invalid_operation;
1085
1086 #define VALIDATE_TYPE_2(type1, type2)\
1087 if (res->Type != type1 && res->Type != type2)\
1088 goto invalid_operation;
1089
1090 switch(prop) {
1091 case GL_NAME_LENGTH:
1092 switch (res->Type) {
1093 case GL_ATOMIC_COUNTER_BUFFER:
1094 case GL_TRANSFORM_FEEDBACK_BUFFER:
1095 goto invalid_operation;
1096 default:
1097 /* Resource name length + terminator. */
1098 *val = _mesa_program_resource_name_len(res) + 1;
1099 }
1100 return 1;
1101 case GL_TYPE:
1102 switch (res->Type) {
1103 case GL_UNIFORM:
1104 case GL_BUFFER_VARIABLE:
1105 *val = RESOURCE_UNI(res)->type->gl_type;
1106 return 1;
1107 case GL_PROGRAM_INPUT:
1108 case GL_PROGRAM_OUTPUT:
1109 *val = RESOURCE_VAR(res)->type->gl_type;
1110 return 1;
1111 case GL_TRANSFORM_FEEDBACK_VARYING:
1112 *val = RESOURCE_XFV(res)->Type;
1113 return 1;
1114 default:
1115 goto invalid_operation;
1116 }
1117 case GL_ARRAY_SIZE:
1118 switch (res->Type) {
1119 case GL_UNIFORM:
1120 case GL_BUFFER_VARIABLE:
1121 case GL_VERTEX_SUBROUTINE_UNIFORM:
1122 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1123 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1124 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1125 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1126 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1127
1128 /* Test if a buffer variable is an array or an unsized array.
1129 * Unsized arrays return zero as array size.
1130 */
1131 if (RESOURCE_UNI(res)->is_shader_storage &&
1132 RESOURCE_UNI(res)->array_stride > 0)
1133 *val = RESOURCE_UNI(res)->array_elements;
1134 else
1135 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1136 return 1;
1137 case GL_PROGRAM_INPUT:
1138 case GL_PROGRAM_OUTPUT:
1139 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1140 return 1;
1141 case GL_TRANSFORM_FEEDBACK_VARYING:
1142 *val = RESOURCE_XFV(res)->Size;
1143 return 1;
1144 default:
1145 goto invalid_operation;
1146 }
1147 case GL_OFFSET:
1148 switch (res->Type) {
1149 case GL_UNIFORM:
1150 case GL_BUFFER_VARIABLE:
1151 *val = RESOURCE_UNI(res)->offset;
1152 return 1;
1153 case GL_TRANSFORM_FEEDBACK_VARYING:
1154 *val = RESOURCE_XFV(res)->Offset;
1155 return 1;
1156 default:
1157 goto invalid_operation;
1158 }
1159 case GL_BLOCK_INDEX:
1160 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1161 *val = RESOURCE_UNI(res)->block_index;
1162 return 1;
1163 case GL_ARRAY_STRIDE:
1164 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1165 *val = RESOURCE_UNI(res)->array_stride;
1166 return 1;
1167 case GL_MATRIX_STRIDE:
1168 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1169 *val = RESOURCE_UNI(res)->matrix_stride;
1170 return 1;
1171 case GL_IS_ROW_MAJOR:
1172 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1173 *val = RESOURCE_UNI(res)->row_major;
1174 return 1;
1175 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1176 VALIDATE_TYPE(GL_UNIFORM);
1177 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1178 return 1;
1179 case GL_BUFFER_BINDING:
1180 case GL_BUFFER_DATA_SIZE:
1181 case GL_NUM_ACTIVE_VARIABLES:
1182 case GL_ACTIVE_VARIABLES:
1183 return get_buffer_property(shProg, res, prop, val, caller);
1184 case GL_REFERENCED_BY_COMPUTE_SHADER:
1185 if (!_mesa_has_compute_shaders(ctx))
1186 goto invalid_enum;
1187 /* fallthrough */
1188 case GL_REFERENCED_BY_VERTEX_SHADER:
1189 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1190 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1191 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1192 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1193 switch (res->Type) {
1194 case GL_UNIFORM:
1195 case GL_PROGRAM_INPUT:
1196 case GL_PROGRAM_OUTPUT:
1197 case GL_UNIFORM_BLOCK:
1198 case GL_BUFFER_VARIABLE:
1199 case GL_SHADER_STORAGE_BLOCK:
1200 case GL_ATOMIC_COUNTER_BUFFER:
1201 *val = is_resource_referenced(shProg, res, index,
1202 stage_from_enum(prop));
1203 return 1;
1204 default:
1205 goto invalid_operation;
1206 }
1207 case GL_LOCATION:
1208 switch (res->Type) {
1209 case GL_UNIFORM:
1210 case GL_VERTEX_SUBROUTINE_UNIFORM:
1211 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1212 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1213 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1214 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1215 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1216 case GL_PROGRAM_INPUT:
1217 case GL_PROGRAM_OUTPUT:
1218 *val = program_resource_location(res, 0);
1219 return 1;
1220 default:
1221 goto invalid_operation;
1222 }
1223 case GL_LOCATION_COMPONENT:
1224 switch (res->Type) {
1225 case GL_PROGRAM_INPUT:
1226 case GL_PROGRAM_OUTPUT:
1227 *val = RESOURCE_VAR(res)->component;
1228 return 1;
1229 default:
1230 goto invalid_operation;
1231 }
1232 case GL_LOCATION_INDEX: {
1233 int tmp;
1234 if (res->Type != GL_PROGRAM_OUTPUT)
1235 goto invalid_operation;
1236 tmp = program_resource_location(res, 0);
1237 if (tmp == -1)
1238 *val = -1;
1239 else
1240 *val = _mesa_program_resource_location_index(shProg, res->Type,
1241 RESOURCE_VAR(res)->name);
1242 return 1;
1243 }
1244 case GL_NUM_COMPATIBLE_SUBROUTINES:
1245 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1246 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1247 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1248 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1249 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1250 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1251 goto invalid_operation;
1252 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1253 return 1;
1254 case GL_COMPATIBLE_SUBROUTINES: {
1255 const struct gl_uniform_storage *uni;
1256 struct gl_program *p;
1257 unsigned count, i;
1258 int j;
1259
1260 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1261 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1262 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1263 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1264 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1265 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1266 goto invalid_operation;
1267 uni = RESOURCE_UNI(res);
1268
1269 p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
1270 count = 0;
1271 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
1272 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
1273 for (j = 0; j < fn->num_compat_types; j++) {
1274 if (fn->types[j] == uni->type) {
1275 val[count++] = i;
1276 break;
1277 }
1278 }
1279 }
1280 return count;
1281 }
1282
1283 case GL_TOP_LEVEL_ARRAY_SIZE:
1284 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1285 *val = RESOURCE_UNI(res)->top_level_array_size;
1286 return 1;
1287
1288 case GL_TOP_LEVEL_ARRAY_STRIDE:
1289 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1290 *val = RESOURCE_UNI(res)->top_level_array_stride;
1291 return 1;
1292
1293 /* GL_ARB_tessellation_shader */
1294 case GL_IS_PER_PATCH:
1295 switch (res->Type) {
1296 case GL_PROGRAM_INPUT:
1297 case GL_PROGRAM_OUTPUT:
1298 *val = RESOURCE_VAR(res)->patch;
1299 return 1;
1300 default:
1301 goto invalid_operation;
1302 }
1303
1304 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1305 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1306 *val = RESOURCE_XFV(res)->BufferIndex;
1307 return 1;
1308 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1309 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1310 *val = RESOURCE_XFB(res)->Stride * 4;
1311 return 1;
1312
1313 default:
1314 goto invalid_enum;
1315 }
1316
1317 #undef VALIDATE_TYPE
1318 #undef VALIDATE_TYPE_2
1319
1320 invalid_enum:
1321 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1322 _mesa_enum_to_string(res->Type),
1323 _mesa_enum_to_string(prop));
1324 return 0;
1325
1326 invalid_operation:
1327 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1328 _mesa_enum_to_string(res->Type),
1329 _mesa_enum_to_string(prop));
1330 return 0;
1331 }
1332
1333 extern void
1334 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1335 GLenum programInterface, GLuint index, GLsizei propCount,
1336 const GLenum *props, GLsizei bufSize,
1337 GLsizei *length, GLint *params)
1338 {
1339 GET_CURRENT_CONTEXT(ctx);
1340 GLint *val = (GLint *) params;
1341 const GLenum *prop = props;
1342 GLsizei amount = 0;
1343
1344 struct gl_program_resource *res =
1345 _mesa_program_resource_find_index(shProg, programInterface, index);
1346
1347 /* No such resource found or bufSize negative. */
1348 if (!res || bufSize < 0) {
1349 _mesa_error(ctx, GL_INVALID_VALUE,
1350 "glGetProgramResourceiv(%s index %d bufSize %d)",
1351 _mesa_enum_to_string(programInterface), index, bufSize);
1352 return;
1353 }
1354
1355 /* Write propCount values until error occurs or bufSize reached. */
1356 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1357 int props_written =
1358 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1359 "glGetProgramResourceiv");
1360
1361 /* Error happened. */
1362 if (props_written == 0)
1363 return;
1364
1365 amount += props_written;
1366 }
1367
1368 /* If <length> is not NULL, the actual number of integer values
1369 * written to <params> will be written to <length>.
1370 */
1371 if (length)
1372 *length = amount;
1373 }
1374
1375 static bool
1376 validate_io(struct gl_program *producer, struct gl_program *consumer)
1377 {
1378 if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
1379 return true;
1380
1381 const bool nonarray_stage_to_array_stage =
1382 producer->info.stage != MESA_SHADER_TESS_CTRL &&
1383 (consumer->info.stage == MESA_SHADER_GEOMETRY ||
1384 consumer->info.stage == MESA_SHADER_TESS_CTRL ||
1385 consumer->info.stage == MESA_SHADER_TESS_EVAL);
1386
1387 bool valid = true;
1388
1389 gl_shader_variable const **outputs =
1390 (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
1391 sizeof(gl_shader_variable *));
1392 if (outputs == NULL)
1393 return false;
1394
1395 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1396 * says:
1397 *
1398 * At an interface between program objects, the set of inputs and
1399 * outputs are considered to match exactly if and only if:
1400 *
1401 * - Every declared input variable has a matching output, as described
1402 * above.
1403 * - There are no user-defined output variables declared without a
1404 * matching input variable declaration.
1405 *
1406 * Every input has an output, and every output has an input. Scan the list
1407 * of producer resources once, and generate the list of outputs. As inputs
1408 * and outputs are matched, remove the matched outputs from the set. At
1409 * the end, the set must be empty. If the set is not empty, then there is
1410 * some output that did not have an input.
1411 */
1412 unsigned num_outputs = 0;
1413 for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
1414 struct gl_program_resource *res =
1415 &producer->sh.data->ProgramResourceList[i];
1416
1417 if (res->Type != GL_PROGRAM_OUTPUT)
1418 continue;
1419
1420 gl_shader_variable const *const var = RESOURCE_VAR(res);
1421
1422 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1423 * says:
1424 *
1425 * Built-in inputs or outputs do not affect interface matching.
1426 */
1427 if (is_gl_identifier(var->name))
1428 continue;
1429
1430 outputs[num_outputs++] = var;
1431 }
1432
1433 unsigned match_index = 0;
1434 for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
1435 struct gl_program_resource *res =
1436 &consumer->sh.data->ProgramResourceList[i];
1437
1438 if (res->Type != GL_PROGRAM_INPUT)
1439 continue;
1440
1441 gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
1442 gl_shader_variable const *producer_var = NULL;
1443
1444 if (is_gl_identifier(consumer_var->name))
1445 continue;
1446
1447 /* Inputs with explicit locations match other outputs with explicit
1448 * locations by location instead of by name.
1449 */
1450 if (consumer_var->explicit_location) {
1451 for (unsigned j = 0; j < num_outputs; j++) {
1452 const gl_shader_variable *const var = outputs[j];
1453
1454 if (var->explicit_location &&
1455 consumer_var->location == var->location) {
1456 producer_var = var;
1457 match_index = j;
1458 break;
1459 }
1460 }
1461 } else {
1462 for (unsigned j = 0; j < num_outputs; j++) {
1463 const gl_shader_variable *const var = outputs[j];
1464
1465 if (!var->explicit_location &&
1466 strcmp(consumer_var->name, var->name) == 0) {
1467 producer_var = var;
1468 match_index = j;
1469 break;
1470 }
1471 }
1472 }
1473
1474 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1475 * says:
1476 *
1477 * - An output variable is considered to match an input variable in
1478 * the subsequent shader if:
1479 *
1480 * - the two variables match in name, type, and qualification; or
1481 *
1482 * - the two variables are declared with the same location
1483 * qualifier and match in type and qualification.
1484 */
1485 if (producer_var == NULL) {
1486 valid = false;
1487 goto out;
1488 }
1489
1490 /* An output cannot match more than one input, so remove the output from
1491 * the set of possible outputs.
1492 */
1493 outputs[match_index] = NULL;
1494 num_outputs--;
1495 if (match_index < num_outputs)
1496 outputs[match_index] = outputs[num_outputs];
1497
1498 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1499 *
1500 * Qualifier Class| Qualifier |in/out
1501 * ---------------+-------------+------
1502 * Storage | in |
1503 * | out | N/A
1504 * | uniform |
1505 * ---------------+-------------+------
1506 * Auxiliary | centroid | No
1507 * ---------------+-------------+------
1508 * | location | Yes
1509 * | Block layout| N/A
1510 * | binding | N/A
1511 * | offset | N/A
1512 * | format | N/A
1513 * ---------------+-------------+------
1514 * Interpolation | smooth |
1515 * | flat | Yes
1516 * ---------------+-------------+------
1517 * | lowp |
1518 * Precision | mediump | Yes
1519 * | highp |
1520 * ---------------+-------------+------
1521 * Variance | invariant | No
1522 * ---------------+-------------+------
1523 * Memory | all | N/A
1524 *
1525 * Note that location mismatches are detected by the loops above that
1526 * find the producer variable that goes with the consumer variable.
1527 */
1528 if (nonarray_stage_to_array_stage) {
1529 if (consumer_var->interface_type != NULL) {
1530 /* the interface is the array; underlying types should match */
1531 if (producer_var->type != consumer_var->type) {
1532 valid = false;
1533 goto out;
1534 }
1535
1536 if (!consumer_var->interface_type->is_array() ||
1537 consumer_var->interface_type->fields.array != producer_var->interface_type) {
1538 valid = false;
1539 goto out;
1540 }
1541 } else {
1542 if (producer_var->interface_type != NULL) {
1543 valid = false;
1544 goto out;
1545 }
1546
1547 if (!consumer_var->type->is_array() ||
1548 consumer_var->type->fields.array != producer_var->type) {
1549 valid = false;
1550 goto out;
1551 }
1552 }
1553 } else {
1554 if (producer_var->type != consumer_var->type) {
1555 valid = false;
1556 goto out;
1557 }
1558
1559 if (producer_var->interface_type != consumer_var->interface_type) {
1560 valid = false;
1561 goto out;
1562 }
1563 }
1564
1565 if (producer_var->interpolation != consumer_var->interpolation) {
1566 valid = false;
1567 goto out;
1568 }
1569
1570 if (producer_var->precision != consumer_var->precision) {
1571 valid = false;
1572 goto out;
1573 }
1574
1575 if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
1576 valid = false;
1577 goto out;
1578 }
1579 }
1580
1581 out:
1582 free(outputs);
1583 return valid && num_outputs == 0;
1584 }
1585
1586 /**
1587 * Validate inputs against outputs in a program pipeline.
1588 */
1589 extern "C" bool
1590 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
1591 {
1592 struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
1593
1594 /* Find first active stage in pipeline. */
1595 unsigned idx, prev = 0;
1596 for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1597 if (prog[idx]) {
1598 prev = idx;
1599 break;
1600 }
1601 }
1602
1603 for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1604 if (prog[idx]) {
1605 /* Pipeline might include both non-compute and a compute program, do
1606 * not attempt to validate varyings between non-compute and compute
1607 * stage.
1608 */
1609 if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
1610 break;
1611
1612 if (!validate_io(prog[prev], prog[idx]))
1613 return false;
1614
1615 prev = idx;
1616 }
1617 }
1618 return true;
1619 }