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