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