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