Merge remote-tracking branch 'public/master' into vulkan
[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_shader_program *shProg,
46 struct gl_program_resource *res, const char *name,
47 unsigned array_index);
48
49 /**
50 * Declare convenience functions to return resource data in a given type.
51 * Warning! this is not type safe so be *very* careful when using these.
52 */
53 #define DECL_RESOURCE_FUNC(name, type) \
54 const type * RESOURCE_ ## name (gl_program_resource *res) { \
55 assert(res->Data); \
56 return (type *) res->Data; \
57 }
58
59 DECL_RESOURCE_FUNC(VAR, gl_shader_variable);
60 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
61 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
62 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
63 DECL_RESOURCE_FUNC(XFV, gl_transform_feedback_varying_info);
64 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
65 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
66
67 void GLAPIENTRY
68 _mesa_BindAttribLocation(GLuint program, GLuint index,
69 const GLchar *name)
70 {
71 GET_CURRENT_CONTEXT(ctx);
72
73 struct gl_shader_program *const shProg =
74 _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
75 if (!shProg)
76 return;
77
78 if (!name)
79 return;
80
81 if (strncmp(name, "gl_", 3) == 0) {
82 _mesa_error(ctx, GL_INVALID_OPERATION,
83 "glBindAttribLocation(illegal name)");
84 return;
85 }
86
87 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
88 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
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(shProg, res, name, 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(shProg, res, name, 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 (strncmp(rname_last_square_bracket, "[0]\0", 4) == 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_shader_program *shProg,
780 struct gl_program_resource *res, const char *name,
781 unsigned array_index)
782 {
783 switch (res->Type) {
784 case GL_PROGRAM_INPUT: {
785 const gl_shader_variable *var = RESOURCE_VAR(res);
786
787 if (var->location == -1)
788 return -1;
789
790 /* If the input is an array, fail if the index is out of bounds. */
791 if (array_index > 0
792 && array_index >= var->type->length) {
793 return -1;
794 }
795 return var->location +
796 (array_index * var->type->without_array()->matrix_columns);
797 }
798 case GL_PROGRAM_OUTPUT:
799 if (RESOURCE_VAR(res)->location == -1)
800 return -1;
801
802 /* If the output is an array, fail if the index is out of bounds. */
803 if (array_index > 0
804 && array_index >= RESOURCE_VAR(res)->type->length) {
805 return -1;
806 }
807 return RESOURCE_VAR(res)->location + array_index;
808 case GL_UNIFORM:
809 /* If the uniform is built-in, fail. */
810 if (RESOURCE_UNI(res)->builtin)
811 return -1;
812
813 /* From page 79 of the OpenGL 4.2 spec:
814 *
815 * "A valid name cannot be a structure, an array of structures, or any
816 * portion of a single vector or a matrix."
817 */
818 if (RESOURCE_UNI(res)->type->without_array()->is_record())
819 return -1;
820
821 /* From the GL_ARB_uniform_buffer_object spec:
822 *
823 * "The value -1 will be returned if <name> does not correspond to an
824 * active uniform variable name in <program>, if <name> is associated
825 * with a named uniform block, or if <name> starts with the reserved
826 * prefix "gl_"."
827 */
828 if (RESOURCE_UNI(res)->block_index != -1 ||
829 RESOURCE_UNI(res)->atomic_buffer_index != -1)
830 return -1;
831
832 /* fallthrough */
833 case GL_VERTEX_SUBROUTINE_UNIFORM:
834 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
835 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
836 case GL_COMPUTE_SUBROUTINE_UNIFORM:
837 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
838 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
839 /* If the uniform is an array, fail if the index is out of bounds. */
840 if (array_index > 0
841 && array_index >= RESOURCE_UNI(res)->array_elements) {
842 return -1;
843 }
844
845 /* location in remap table + array element offset */
846 return RESOURCE_UNI(res)->remap_location + array_index;
847 default:
848 return -1;
849 }
850 }
851
852 /**
853 * Function implements following location queries:
854 * glGetUniformLocation
855 */
856 GLint
857 _mesa_program_resource_location(struct gl_shader_program *shProg,
858 GLenum programInterface, const char *name)
859 {
860 unsigned array_index = 0;
861 struct gl_program_resource *res =
862 _mesa_program_resource_find_name(shProg, programInterface, name,
863 &array_index);
864
865 /* Resource not found. */
866 if (!res)
867 return -1;
868
869 return program_resource_location(shProg, res, name, array_index);
870 }
871
872 /**
873 * Function implements following index queries:
874 * glGetFragDataIndex
875 */
876 GLint
877 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
878 GLenum programInterface, const char *name)
879 {
880 struct gl_program_resource *res =
881 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
882
883 /* Non-existent variable or resource is not referenced by fragment stage. */
884 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
885 return -1;
886
887 return RESOURCE_VAR(res)->index;
888 }
889
890 static uint8_t
891 stage_from_enum(GLenum ref)
892 {
893 switch (ref) {
894 case GL_REFERENCED_BY_VERTEX_SHADER:
895 return MESA_SHADER_VERTEX;
896 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
897 return MESA_SHADER_TESS_CTRL;
898 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
899 return MESA_SHADER_TESS_EVAL;
900 case GL_REFERENCED_BY_GEOMETRY_SHADER:
901 return MESA_SHADER_GEOMETRY;
902 case GL_REFERENCED_BY_FRAGMENT_SHADER:
903 return MESA_SHADER_FRAGMENT;
904 case GL_REFERENCED_BY_COMPUTE_SHADER:
905 return MESA_SHADER_COMPUTE;
906 default:
907 assert(!"shader stage not supported");
908 return MESA_SHADER_STAGES;
909 }
910 }
911
912 /**
913 * Check if resource is referenced by given 'referenced by' stage enum.
914 * ATC and UBO resources hold stage references of their own.
915 */
916 static bool
917 is_resource_referenced(struct gl_shader_program *shProg,
918 struct gl_program_resource *res,
919 GLuint index, uint8_t stage)
920 {
921 /* First, check if we even have such a stage active. */
922 if (!shProg->_LinkedShaders[stage])
923 return false;
924
925 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
926 return RESOURCE_ATC(res)->StageReferences[stage];
927
928 if (res->Type == GL_UNIFORM_BLOCK)
929 return shProg->UniformBlocks[index].stageref & (1 << stage);
930
931 if (res->Type == GL_SHADER_STORAGE_BLOCK)
932 return shProg->ShaderStorageBlocks[index].stageref & (1 << stage);
933
934 return res->StageReferences & (1 << stage);
935 }
936
937 static unsigned
938 get_buffer_property(struct gl_shader_program *shProg,
939 struct gl_program_resource *res, const GLenum prop,
940 GLint *val, const char *caller)
941 {
942 GET_CURRENT_CONTEXT(ctx);
943 if (res->Type != GL_UNIFORM_BLOCK &&
944 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
945 res->Type != GL_SHADER_STORAGE_BLOCK &&
946 res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
947 goto invalid_operation;
948
949 if (res->Type == GL_UNIFORM_BLOCK) {
950 switch (prop) {
951 case GL_BUFFER_BINDING:
952 *val = RESOURCE_UBO(res)->Binding;
953 return 1;
954 case GL_BUFFER_DATA_SIZE:
955 *val = RESOURCE_UBO(res)->UniformBufferSize;
956 return 1;
957 case GL_NUM_ACTIVE_VARIABLES:
958 *val = 0;
959 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
960 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
961 struct gl_program_resource *uni =
962 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
963 NULL);
964 if (!uni)
965 continue;
966 (*val)++;
967 }
968 return 1;
969 case GL_ACTIVE_VARIABLES: {
970 unsigned num_values = 0;
971 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
972 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
973 struct gl_program_resource *uni =
974 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
975 NULL);
976 if (!uni)
977 continue;
978 *val++ =
979 _mesa_program_resource_index(shProg, uni);
980 num_values++;
981 }
982 return num_values;
983 }
984 }
985 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
986 switch (prop) {
987 case GL_BUFFER_BINDING:
988 *val = RESOURCE_UBO(res)->Binding;
989 return 1;
990 case GL_BUFFER_DATA_SIZE:
991 *val = RESOURCE_UBO(res)->UniformBufferSize;
992 return 1;
993 case GL_NUM_ACTIVE_VARIABLES:
994 *val = 0;
995 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
996 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
997 struct gl_program_resource *uni =
998 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
999 iname, NULL);
1000 if (!uni)
1001 continue;
1002 (*val)++;
1003 }
1004 return 1;
1005 case GL_ACTIVE_VARIABLES: {
1006 unsigned num_values = 0;
1007 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1008 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1009 struct gl_program_resource *uni =
1010 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1011 iname, NULL);
1012 if (!uni)
1013 continue;
1014 *val++ =
1015 _mesa_program_resource_index(shProg, uni);
1016 num_values++;
1017 }
1018 return num_values;
1019 }
1020 }
1021 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1022 switch (prop) {
1023 case GL_BUFFER_BINDING:
1024 *val = RESOURCE_ATC(res)->Binding;
1025 return 1;
1026 case GL_BUFFER_DATA_SIZE:
1027 *val = RESOURCE_ATC(res)->MinimumSize;
1028 return 1;
1029 case GL_NUM_ACTIVE_VARIABLES:
1030 *val = RESOURCE_ATC(res)->NumUniforms;
1031 return 1;
1032 case GL_ACTIVE_VARIABLES:
1033 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1034 /* Active atomic buffer contains index to UniformStorage. Find
1035 * out gl_program_resource via data pointer and then calculate
1036 * index of that uniform.
1037 */
1038 unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1039 struct gl_program_resource *uni =
1040 program_resource_find_data(shProg,
1041 &shProg->UniformStorage[idx]);
1042 assert(uni);
1043 *val++ = _mesa_program_resource_index(shProg, uni);
1044 }
1045 return RESOURCE_ATC(res)->NumUniforms;
1046 }
1047 } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1048 switch (prop) {
1049 case GL_BUFFER_BINDING:
1050 *val = RESOURCE_XFB(res)->Binding;
1051 return 1;
1052 case GL_NUM_ACTIVE_VARIABLES:
1053 *val = RESOURCE_XFB(res)->NumVaryings;
1054 return 1;
1055 case GL_ACTIVE_VARIABLES:
1056 int i = 0;
1057 for ( ; i < shProg->LinkedTransformFeedback.NumVarying; i++) {
1058 unsigned index =
1059 shProg->LinkedTransformFeedback.Varyings[i].BufferIndex;
1060 struct gl_program_resource *buf_res =
1061 _mesa_program_resource_find_index(shProg,
1062 GL_TRANSFORM_FEEDBACK_BUFFER,
1063 index);
1064 assert(buf_res);
1065 if (res == buf_res) {
1066 *val++ = i;
1067 }
1068 }
1069 return RESOURCE_XFB(res)->NumVaryings;
1070 }
1071 }
1072 assert(!"support for property type not implemented");
1073
1074 invalid_operation:
1075 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1076 _mesa_enum_to_string(res->Type),
1077 _mesa_enum_to_string(prop));
1078
1079 return 0;
1080 }
1081
1082 unsigned
1083 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1084 struct gl_program_resource *res, GLuint index,
1085 const GLenum prop, GLint *val, const char *caller)
1086 {
1087 GET_CURRENT_CONTEXT(ctx);
1088
1089 #define VALIDATE_TYPE(type)\
1090 if (res->Type != type)\
1091 goto invalid_operation;
1092
1093 #define VALIDATE_TYPE_2(type1, type2)\
1094 if (res->Type != type1 && res->Type != type2)\
1095 goto invalid_operation;
1096
1097 switch(prop) {
1098 case GL_NAME_LENGTH:
1099 switch (res->Type) {
1100 case GL_ATOMIC_COUNTER_BUFFER:
1101 case GL_TRANSFORM_FEEDBACK_BUFFER:
1102 goto invalid_operation;
1103 default:
1104 /* Resource name length + terminator. */
1105 *val = _mesa_program_resource_name_len(res) + 1;
1106 }
1107 return 1;
1108 case GL_TYPE:
1109 switch (res->Type) {
1110 case GL_UNIFORM:
1111 case GL_BUFFER_VARIABLE:
1112 *val = RESOURCE_UNI(res)->type->gl_type;
1113 return 1;
1114 case GL_PROGRAM_INPUT:
1115 case GL_PROGRAM_OUTPUT:
1116 *val = RESOURCE_VAR(res)->type->gl_type;
1117 return 1;
1118 case GL_TRANSFORM_FEEDBACK_VARYING:
1119 *val = RESOURCE_XFV(res)->Type;
1120 return 1;
1121 default:
1122 goto invalid_operation;
1123 }
1124 case GL_ARRAY_SIZE:
1125 switch (res->Type) {
1126 case GL_UNIFORM:
1127 case GL_BUFFER_VARIABLE:
1128 /* Test if a buffer variable is an array or an unsized array.
1129 * Unsized arrays return zero as array size.
1130 */
1131 if (RESOURCE_UNI(res)->is_shader_storage &&
1132 RESOURCE_UNI(res)->array_stride > 0)
1133 *val = RESOURCE_UNI(res)->array_elements;
1134 else
1135 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1136 return 1;
1137 case GL_PROGRAM_INPUT:
1138 case GL_PROGRAM_OUTPUT:
1139 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1140 return 1;
1141 case GL_TRANSFORM_FEEDBACK_VARYING:
1142 *val = MAX2(RESOURCE_XFV(res)->Size, 1);
1143 return 1;
1144 default:
1145 goto invalid_operation;
1146 }
1147 case GL_OFFSET:
1148 switch (res->Type) {
1149 case GL_UNIFORM:
1150 case GL_BUFFER_VARIABLE:
1151 *val = RESOURCE_UNI(res)->offset;
1152 return 1;
1153 case GL_TRANSFORM_FEEDBACK_VARYING:
1154 *val = RESOURCE_XFV(res)->Offset;
1155 return 1;
1156 default:
1157 goto invalid_operation;
1158 }
1159 case GL_BLOCK_INDEX:
1160 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1161 *val = RESOURCE_UNI(res)->block_index;
1162 return 1;
1163 case GL_ARRAY_STRIDE:
1164 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1165 *val = RESOURCE_UNI(res)->array_stride;
1166 return 1;
1167 case GL_MATRIX_STRIDE:
1168 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1169 *val = RESOURCE_UNI(res)->matrix_stride;
1170 return 1;
1171 case GL_IS_ROW_MAJOR:
1172 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1173 *val = RESOURCE_UNI(res)->row_major;
1174 return 1;
1175 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1176 VALIDATE_TYPE(GL_UNIFORM);
1177 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1178 return 1;
1179 case GL_BUFFER_BINDING:
1180 case GL_BUFFER_DATA_SIZE:
1181 case GL_NUM_ACTIVE_VARIABLES:
1182 case GL_ACTIVE_VARIABLES:
1183 return get_buffer_property(shProg, res, prop, val, caller);
1184 case GL_REFERENCED_BY_COMPUTE_SHADER:
1185 if (!_mesa_has_compute_shaders(ctx))
1186 goto invalid_enum;
1187 /* fallthrough */
1188 case GL_REFERENCED_BY_VERTEX_SHADER:
1189 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1190 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1191 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1192 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1193 switch (res->Type) {
1194 case GL_UNIFORM:
1195 case GL_PROGRAM_INPUT:
1196 case GL_PROGRAM_OUTPUT:
1197 case GL_UNIFORM_BLOCK:
1198 case GL_BUFFER_VARIABLE:
1199 case GL_SHADER_STORAGE_BLOCK:
1200 case GL_ATOMIC_COUNTER_BUFFER:
1201 *val = is_resource_referenced(shProg, res, index,
1202 stage_from_enum(prop));
1203 return 1;
1204 default:
1205 goto invalid_operation;
1206 }
1207 case GL_LOCATION:
1208 switch (res->Type) {
1209 case GL_UNIFORM:
1210 case GL_PROGRAM_INPUT:
1211 case GL_PROGRAM_OUTPUT:
1212 *val = program_resource_location(shProg, res,
1213 _mesa_program_resource_name(res),
1214 0);
1215 return 1;
1216 default:
1217 goto invalid_operation;
1218 }
1219 case GL_LOCATION_INDEX:
1220 if (res->Type != GL_PROGRAM_OUTPUT)
1221 goto invalid_operation;
1222 *val = RESOURCE_VAR(res)->index;
1223 return 1;
1224
1225 case GL_NUM_COMPATIBLE_SUBROUTINES:
1226 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1227 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1228 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1229 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1230 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1231 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1232 goto invalid_operation;
1233 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1234 return 1;
1235 case GL_COMPATIBLE_SUBROUTINES: {
1236 const struct gl_uniform_storage *uni;
1237 struct gl_shader *sh;
1238 unsigned count, i;
1239 int j;
1240
1241 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1242 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1243 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1244 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1245 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1246 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1247 goto invalid_operation;
1248 uni = RESOURCE_UNI(res);
1249
1250 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1251 count = 0;
1252 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1253 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1254 for (j = 0; j < fn->num_compat_types; j++) {
1255 if (fn->types[j] == uni->type) {
1256 val[count++] = i;
1257 break;
1258 }
1259 }
1260 }
1261 return count;
1262 }
1263
1264 case GL_TOP_LEVEL_ARRAY_SIZE:
1265 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1266 *val = RESOURCE_UNI(res)->top_level_array_size;
1267 return 1;
1268
1269 case GL_TOP_LEVEL_ARRAY_STRIDE:
1270 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1271 *val = RESOURCE_UNI(res)->top_level_array_stride;
1272 return 1;
1273
1274 /* GL_ARB_tessellation_shader */
1275 case GL_IS_PER_PATCH:
1276 switch (res->Type) {
1277 case GL_PROGRAM_INPUT:
1278 case GL_PROGRAM_OUTPUT:
1279 *val = RESOURCE_VAR(res)->patch;
1280 return 1;
1281 default:
1282 goto invalid_operation;
1283 }
1284
1285 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1286 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1287 *val = RESOURCE_XFV(res)->BufferIndex;
1288 return 1;
1289 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1290 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1291 *val = RESOURCE_XFB(res)->Stride * 4;
1292 return 1;
1293
1294 default:
1295 goto invalid_enum;
1296 }
1297
1298 #undef VALIDATE_TYPE
1299 #undef VALIDATE_TYPE_2
1300
1301 invalid_enum:
1302 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1303 _mesa_enum_to_string(res->Type),
1304 _mesa_enum_to_string(prop));
1305 return 0;
1306
1307 invalid_operation:
1308 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1309 _mesa_enum_to_string(res->Type),
1310 _mesa_enum_to_string(prop));
1311 return 0;
1312 }
1313
1314 extern void
1315 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1316 GLenum programInterface, GLuint index, GLsizei propCount,
1317 const GLenum *props, GLsizei bufSize,
1318 GLsizei *length, GLint *params)
1319 {
1320 GET_CURRENT_CONTEXT(ctx);
1321 GLint *val = (GLint *) params;
1322 const GLenum *prop = props;
1323 GLsizei amount = 0;
1324
1325 struct gl_program_resource *res =
1326 _mesa_program_resource_find_index(shProg, programInterface, index);
1327
1328 /* No such resource found or bufSize negative. */
1329 if (!res || bufSize < 0) {
1330 _mesa_error(ctx, GL_INVALID_VALUE,
1331 "glGetProgramResourceiv(%s index %d bufSize %d)",
1332 _mesa_enum_to_string(programInterface), index, bufSize);
1333 return;
1334 }
1335
1336 /* Write propCount values until error occurs or bufSize reached. */
1337 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1338 int props_written =
1339 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1340 "glGetProgramResourceiv");
1341
1342 /* Error happened. */
1343 if (props_written == 0)
1344 return;
1345
1346 amount += props_written;
1347 }
1348
1349 /* If <length> is not NULL, the actual number of integer values
1350 * written to <params> will be written to <length>.
1351 */
1352 if (length)
1353 *length = amount;
1354 }
1355
1356 static bool
1357 validate_io(const struct gl_shader *producer,
1358 const struct gl_shader *consumer, bool isES)
1359 {
1360 assert(producer && consumer);
1361 unsigned inputs = 0, outputs = 0;
1362
1363 /* From OpenGL ES 3.1 spec (Interface matching):
1364 *
1365 * "An output variable is considered to match an input variable in the
1366 * subsequent shader if:
1367 *
1368 * - the two variables match in name, type, and qualification; or
1369 * - the two variables are declared with the same location qualifier and
1370 * match in type and qualification.
1371 *
1372 * ...
1373 *
1374 * At an interface between program objects, the set of inputs and outputs
1375 * are considered to match exactly if and only if:
1376 *
1377 * - Every declared input variable has a matching output, as described
1378 * above.
1379 *
1380 * - There are no user-defined output variables declared without a
1381 * matching input variable declaration.
1382 *
1383 * - All matched input and output variables have identical precision
1384 * qualification.
1385 *
1386 * When the set of inputs and outputs on an interface between programs
1387 * matches exactly, all inputs are well-defined except when the
1388 * corresponding outputs were not written in the previous shader. However,
1389 * any mismatch between inputs and outputs will result in a validation
1390 * failure."
1391 *
1392 * OpenGL Core 4.5 spec includes same paragraph as above but without check
1393 * for precision and the last 'validation failure' clause. Therefore
1394 * behaviour is more relaxed, input and output amount is not required by the
1395 * spec to be validated.
1396 *
1397 * FIXME: Update once Khronos spec bug #15331 is resolved.
1398 * FIXME: Add validation by type, currently information loss during varying
1399 * packing makes this challenging.
1400 */
1401
1402 /* Currently no matching done for desktop. */
1403 if (!isES)
1404 return true;
1405
1406 /* For each output in a, find input in b and do any required checks. */
1407 foreach_in_list(ir_instruction, out, producer->ir) {
1408 ir_variable *out_var = out->as_variable();
1409 if (!out_var || out_var->data.mode != ir_var_shader_out ||
1410 is_gl_identifier(out_var->name))
1411 continue;
1412
1413 outputs++;
1414
1415 inputs = 0;
1416 foreach_in_list(ir_instruction, in, consumer->ir) {
1417 ir_variable *in_var = in->as_variable();
1418 if (!in_var || in_var->data.mode != ir_var_shader_in ||
1419 is_gl_identifier(in_var->name))
1420 continue;
1421
1422 inputs++;
1423
1424 /* Match by location qualifier and precision.
1425 *
1426 * FIXME: Add explicit location matching validation here. Be careful
1427 * not to match varyings with explicit locations to varyings without
1428 * explicit locations.
1429 */
1430 if ((in_var->data.explicit_location &&
1431 out_var->data.explicit_location) &&
1432 in_var->data.location == out_var->data.location &&
1433 in_var->data.precision == out_var->data.precision)
1434 continue;
1435
1436 unsigned len = strlen(in_var->name);
1437
1438 /* Handle input swizzle in variable name. */
1439 const char *dot = strchr(in_var->name, '.');
1440 if (dot)
1441 len = dot - in_var->name;
1442
1443 /* Match by name and precision. */
1444 if (strncmp(in_var->name, out_var->name, len) == 0) {
1445 /* From OpenGL ES 3.1 spec:
1446 * "When both shaders are in separate programs, mismatched
1447 * precision qualifiers will result in a program interface
1448 * mismatch that will result in program pipeline validation
1449 * failures, as described in section 7.4.1 (“Shader Interface
1450 * Matching”) of the OpenGL ES 3.1 Specification."
1451 */
1452 if (in_var->data.precision != out_var->data.precision)
1453 return false;
1454 }
1455 }
1456 }
1457 return inputs == outputs;
1458 }
1459
1460 /**
1461 * Validate inputs against outputs in a program pipeline.
1462 */
1463 extern "C" bool
1464 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
1465 {
1466 struct gl_shader_program **shProg =
1467 (struct gl_shader_program **) pipeline->CurrentProgram;
1468
1469 /* Find first active stage in pipeline. */
1470 unsigned idx, prev = 0;
1471 for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1472 if (shProg[idx]) {
1473 prev = idx;
1474 break;
1475 }
1476 }
1477
1478 for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1479 if (shProg[idx]) {
1480 /* Pipeline might include both non-compute and a compute program, do
1481 * not attempt to validate varyings between non-compute and compute
1482 * stage.
1483 */
1484 if (shProg[idx]->_LinkedShaders[idx]->Stage == MESA_SHADER_COMPUTE)
1485 break;
1486
1487 if (!validate_io(shProg[prev]->_LinkedShaders[prev],
1488 shProg[idx]->_LinkedShaders[idx],
1489 shProg[prev]->IsES || shProg[idx]->IsES))
1490 return false;
1491 prev = idx;
1492 }
1493 }
1494 return true;
1495 }