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