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