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