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