e9b39b7ff8f496425741caaf11f2f663e5c11225
[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 const char* name = _mesa_program_resource_name(res);
782
783 /* For shaders constructed from SPIR-V binaries, variables may not
784 * have names associated with them.
785 */
786 if (!name)
787 return 0;
788
789 unsigned length = strlen(name);
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 /* The resource name can be NULL for shaders constructed from SPIR-V
831 * binaries. In that case, we do not add the '[0]'.
832 */
833 if (name && name[0] != '\0' &&
834 _mesa_program_resource_array_size(res) && add_index_to_name(res)) {
835 int i;
836
837 /* The comparison is strange because *length does *NOT* include the
838 * terminating NUL, but maxLength does.
839 */
840 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
841 name[*length + i] = "[0]"[i];
842
843 name[*length + i] = '\0';
844 *length += i;
845 }
846 return true;
847 }
848
849 static GLint
850 program_resource_location(struct gl_program_resource *res, unsigned array_index)
851 {
852 switch (res->Type) {
853 case GL_PROGRAM_INPUT: {
854 const gl_shader_variable *var = RESOURCE_VAR(res);
855
856 if (var->location == -1)
857 return -1;
858
859 /* If the input is an array, fail if the index is out of bounds. */
860 if (array_index > 0
861 && array_index >= var->type->length) {
862 return -1;
863 }
864 return var->location +
865 (array_index * var->type->without_array()->matrix_columns);
866 }
867 case GL_PROGRAM_OUTPUT:
868 if (RESOURCE_VAR(res)->location == -1)
869 return -1;
870
871 /* If the output is an array, fail if the index is out of bounds. */
872 if (array_index > 0
873 && array_index >= RESOURCE_VAR(res)->type->length) {
874 return -1;
875 }
876 return RESOURCE_VAR(res)->location + array_index;
877 case GL_UNIFORM:
878 /* If the uniform is built-in, fail. */
879 if (RESOURCE_UNI(res)->builtin)
880 return -1;
881
882 /* From page 79 of the OpenGL 4.2 spec:
883 *
884 * "A valid name cannot be a structure, an array of structures, or any
885 * portion of a single vector or a matrix."
886 */
887 if (RESOURCE_UNI(res)->type->without_array()->is_struct())
888 return -1;
889
890 /* From the GL_ARB_uniform_buffer_object spec:
891 *
892 * "The value -1 will be returned if <name> does not correspond to an
893 * active uniform variable name in <program>, if <name> is associated
894 * with a named uniform block, or if <name> starts with the reserved
895 * prefix "gl_"."
896 */
897 if (RESOURCE_UNI(res)->block_index != -1 ||
898 RESOURCE_UNI(res)->atomic_buffer_index != -1)
899 return -1;
900
901 /* fallthrough */
902 case GL_VERTEX_SUBROUTINE_UNIFORM:
903 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
904 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
905 case GL_COMPUTE_SUBROUTINE_UNIFORM:
906 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
907 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
908 /* If the uniform is an array, fail if the index is out of bounds. */
909 if (array_index > 0
910 && array_index >= RESOURCE_UNI(res)->array_elements) {
911 return -1;
912 }
913
914 /* location in remap table + array element offset */
915 return RESOURCE_UNI(res)->remap_location + array_index;
916 default:
917 return -1;
918 }
919 }
920
921 /**
922 * Function implements following location queries:
923 * glGetUniformLocation
924 */
925 GLint
926 _mesa_program_resource_location(struct gl_shader_program *shProg,
927 GLenum programInterface, const char *name)
928 {
929 unsigned array_index = 0;
930 struct gl_program_resource *res =
931 _mesa_program_resource_find_name(shProg, programInterface, name,
932 &array_index);
933
934 /* Resource not found. */
935 if (!res)
936 return -1;
937
938 return program_resource_location(res, array_index);
939 }
940
941 static GLint
942 _get_resource_location_index(struct gl_program_resource *res)
943 {
944 /* Non-existent variable or resource is not referenced by fragment stage. */
945 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
946 return -1;
947
948 /* From OpenGL 4.5 spec, 7.3 Program Objects
949 * "The value -1 will be returned by either command...
950 * ... or if name identifies an active variable that does not have a
951 * valid location assigned.
952 */
953 if (RESOURCE_VAR(res)->location == -1)
954 return -1;
955 return RESOURCE_VAR(res)->index;
956 }
957
958 /**
959 * Function implements following index queries:
960 * glGetFragDataIndex
961 */
962 GLint
963 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
964 GLenum programInterface, const char *name)
965 {
966 struct gl_program_resource *res =
967 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
968
969 return _get_resource_location_index(res);
970 }
971
972 static uint8_t
973 stage_from_enum(GLenum ref)
974 {
975 switch (ref) {
976 case GL_REFERENCED_BY_VERTEX_SHADER:
977 return MESA_SHADER_VERTEX;
978 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
979 return MESA_SHADER_TESS_CTRL;
980 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
981 return MESA_SHADER_TESS_EVAL;
982 case GL_REFERENCED_BY_GEOMETRY_SHADER:
983 return MESA_SHADER_GEOMETRY;
984 case GL_REFERENCED_BY_FRAGMENT_SHADER:
985 return MESA_SHADER_FRAGMENT;
986 case GL_REFERENCED_BY_COMPUTE_SHADER:
987 return MESA_SHADER_COMPUTE;
988 default:
989 assert(!"shader stage not supported");
990 return MESA_SHADER_STAGES;
991 }
992 }
993
994 /**
995 * Check if resource is referenced by given 'referenced by' stage enum.
996 * ATC and UBO resources hold stage references of their own.
997 */
998 static bool
999 is_resource_referenced(struct gl_shader_program *shProg,
1000 struct gl_program_resource *res,
1001 GLuint index, uint8_t stage)
1002 {
1003 /* First, check if we even have such a stage active. */
1004 if (!shProg->_LinkedShaders[stage])
1005 return false;
1006
1007 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1008 return RESOURCE_ATC(res)->StageReferences[stage];
1009
1010 if (res->Type == GL_UNIFORM_BLOCK)
1011 return shProg->data->UniformBlocks[index].stageref & (1 << stage);
1012
1013 if (res->Type == GL_SHADER_STORAGE_BLOCK)
1014 return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
1015
1016 return res->StageReferences & (1 << stage);
1017 }
1018
1019 static unsigned
1020 get_buffer_property(struct gl_shader_program *shProg,
1021 struct gl_program_resource *res, const GLenum prop,
1022 GLint *val, const char *caller)
1023 {
1024 GET_CURRENT_CONTEXT(ctx);
1025 if (res->Type != GL_UNIFORM_BLOCK &&
1026 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1027 res->Type != GL_SHADER_STORAGE_BLOCK &&
1028 res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
1029 goto invalid_operation;
1030
1031 if (res->Type == GL_UNIFORM_BLOCK) {
1032 switch (prop) {
1033 case GL_BUFFER_BINDING:
1034 *val = RESOURCE_UBO(res)->Binding;
1035 return 1;
1036 case GL_BUFFER_DATA_SIZE:
1037 *val = RESOURCE_UBO(res)->UniformBufferSize;
1038 return 1;
1039 case GL_NUM_ACTIVE_VARIABLES:
1040 *val = 0;
1041 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1042 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1043 struct gl_program_resource *uni =
1044 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1045 NULL);
1046 if (!uni)
1047 continue;
1048 (*val)++;
1049 }
1050 return 1;
1051 case GL_ACTIVE_VARIABLES: {
1052 unsigned num_values = 0;
1053 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1054 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1055 struct gl_program_resource *uni =
1056 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1057 NULL);
1058 if (!uni)
1059 continue;
1060 *val++ =
1061 _mesa_program_resource_index(shProg, uni);
1062 num_values++;
1063 }
1064 return num_values;
1065 }
1066 }
1067 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1068 switch (prop) {
1069 case GL_BUFFER_BINDING:
1070 *val = RESOURCE_UBO(res)->Binding;
1071 return 1;
1072 case GL_BUFFER_DATA_SIZE:
1073 *val = RESOURCE_UBO(res)->UniformBufferSize;
1074 return 1;
1075 case GL_NUM_ACTIVE_VARIABLES:
1076 *val = 0;
1077 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1078 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1079 struct gl_program_resource *uni =
1080 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1081 iname, NULL);
1082 if (!uni)
1083 continue;
1084 (*val)++;
1085 }
1086 return 1;
1087 case GL_ACTIVE_VARIABLES: {
1088 unsigned num_values = 0;
1089 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1090 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1091 struct gl_program_resource *uni =
1092 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1093 iname, NULL);
1094 if (!uni)
1095 continue;
1096 *val++ =
1097 _mesa_program_resource_index(shProg, uni);
1098 num_values++;
1099 }
1100 return num_values;
1101 }
1102 }
1103 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1104 switch (prop) {
1105 case GL_BUFFER_BINDING:
1106 *val = RESOURCE_ATC(res)->Binding;
1107 return 1;
1108 case GL_BUFFER_DATA_SIZE:
1109 *val = RESOURCE_ATC(res)->MinimumSize;
1110 return 1;
1111 case GL_NUM_ACTIVE_VARIABLES:
1112 *val = RESOURCE_ATC(res)->NumUniforms;
1113 return 1;
1114 case GL_ACTIVE_VARIABLES:
1115 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1116 /* Active atomic buffer contains index to UniformStorage. Find
1117 * out gl_program_resource via data pointer and then calculate
1118 * index of that uniform.
1119 */
1120 unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1121 struct gl_program_resource *uni =
1122 program_resource_find_data(shProg,
1123 &shProg->data->UniformStorage[idx]);
1124 assert(uni);
1125 *val++ = _mesa_program_resource_index(shProg, uni);
1126 }
1127 return RESOURCE_ATC(res)->NumUniforms;
1128 }
1129 } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1130 switch (prop) {
1131 case GL_BUFFER_BINDING:
1132 *val = RESOURCE_XFB(res)->Binding;
1133 return 1;
1134 case GL_NUM_ACTIVE_VARIABLES:
1135 *val = RESOURCE_XFB(res)->NumVaryings;
1136 return 1;
1137 case GL_ACTIVE_VARIABLES:
1138 struct gl_transform_feedback_info *linked_xfb =
1139 shProg->last_vert_prog->sh.LinkedTransformFeedback;
1140 for (int i = 0; i < linked_xfb->NumVarying; i++) {
1141 unsigned index = linked_xfb->Varyings[i].BufferIndex;
1142 struct gl_program_resource *buf_res =
1143 _mesa_program_resource_find_index(shProg,
1144 GL_TRANSFORM_FEEDBACK_BUFFER,
1145 index);
1146 assert(buf_res);
1147 if (res == buf_res) {
1148 *val++ = i;
1149 }
1150 }
1151 return RESOURCE_XFB(res)->NumVaryings;
1152 }
1153 }
1154 assert(!"support for property type not implemented");
1155
1156 invalid_operation:
1157 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1158 _mesa_enum_to_string(res->Type),
1159 _mesa_enum_to_string(prop));
1160
1161 return 0;
1162 }
1163
1164 unsigned
1165 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1166 struct gl_program_resource *res, GLuint index,
1167 const GLenum prop, GLint *val, const char *caller)
1168 {
1169 GET_CURRENT_CONTEXT(ctx);
1170
1171 #define VALIDATE_TYPE(type)\
1172 if (res->Type != type)\
1173 goto invalid_operation;
1174
1175 #define VALIDATE_TYPE_2(type1, type2)\
1176 if (res->Type != type1 && res->Type != type2)\
1177 goto invalid_operation;
1178
1179 switch(prop) {
1180 case GL_NAME_LENGTH:
1181 switch (res->Type) {
1182 case GL_ATOMIC_COUNTER_BUFFER:
1183 case GL_TRANSFORM_FEEDBACK_BUFFER:
1184 goto invalid_operation;
1185 default:
1186 /* Resource name length + terminator. */
1187 *val = _mesa_program_resource_name_len(res) + 1;
1188 }
1189 return 1;
1190 case GL_TYPE:
1191 switch (res->Type) {
1192 case GL_UNIFORM:
1193 case GL_BUFFER_VARIABLE:
1194 *val = RESOURCE_UNI(res)->type->gl_type;
1195 return 1;
1196 case GL_PROGRAM_INPUT:
1197 case GL_PROGRAM_OUTPUT:
1198 *val = RESOURCE_VAR(res)->type->gl_type;
1199 return 1;
1200 case GL_TRANSFORM_FEEDBACK_VARYING:
1201 *val = RESOURCE_XFV(res)->Type;
1202 return 1;
1203 default:
1204 goto invalid_operation;
1205 }
1206 case GL_ARRAY_SIZE:
1207 switch (res->Type) {
1208 case GL_UNIFORM:
1209 case GL_BUFFER_VARIABLE:
1210 case GL_VERTEX_SUBROUTINE_UNIFORM:
1211 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1212 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1213 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1214 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1215 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1216
1217 /* Test if a buffer variable is an array or an unsized array.
1218 * Unsized arrays return zero as array size.
1219 */
1220 if (RESOURCE_UNI(res)->is_shader_storage &&
1221 RESOURCE_UNI(res)->array_stride > 0)
1222 *val = RESOURCE_UNI(res)->array_elements;
1223 else
1224 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1225 return 1;
1226 case GL_PROGRAM_INPUT:
1227 case GL_PROGRAM_OUTPUT:
1228 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1229 return 1;
1230 case GL_TRANSFORM_FEEDBACK_VARYING:
1231 *val = RESOURCE_XFV(res)->Size;
1232 return 1;
1233 default:
1234 goto invalid_operation;
1235 }
1236 case GL_OFFSET:
1237 switch (res->Type) {
1238 case GL_UNIFORM:
1239 case GL_BUFFER_VARIABLE:
1240 *val = RESOURCE_UNI(res)->offset;
1241 return 1;
1242 case GL_TRANSFORM_FEEDBACK_VARYING:
1243 *val = RESOURCE_XFV(res)->Offset;
1244 return 1;
1245 default:
1246 goto invalid_operation;
1247 }
1248 case GL_BLOCK_INDEX:
1249 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1250 *val = RESOURCE_UNI(res)->block_index;
1251 return 1;
1252 case GL_ARRAY_STRIDE:
1253 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1254 *val = RESOURCE_UNI(res)->array_stride;
1255 return 1;
1256 case GL_MATRIX_STRIDE:
1257 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1258 *val = RESOURCE_UNI(res)->matrix_stride;
1259 return 1;
1260 case GL_IS_ROW_MAJOR:
1261 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1262 *val = RESOURCE_UNI(res)->row_major;
1263 return 1;
1264 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1265 VALIDATE_TYPE(GL_UNIFORM);
1266 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1267 return 1;
1268 case GL_BUFFER_BINDING:
1269 case GL_BUFFER_DATA_SIZE:
1270 case GL_NUM_ACTIVE_VARIABLES:
1271 case GL_ACTIVE_VARIABLES:
1272 return get_buffer_property(shProg, res, prop, val, caller);
1273 case GL_REFERENCED_BY_COMPUTE_SHADER:
1274 if (!_mesa_has_compute_shaders(ctx))
1275 goto invalid_enum;
1276 /* fallthrough */
1277 case GL_REFERENCED_BY_VERTEX_SHADER:
1278 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1279 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1280 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1281 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1282 switch (res->Type) {
1283 case GL_UNIFORM:
1284 case GL_PROGRAM_INPUT:
1285 case GL_PROGRAM_OUTPUT:
1286 case GL_UNIFORM_BLOCK:
1287 case GL_BUFFER_VARIABLE:
1288 case GL_SHADER_STORAGE_BLOCK:
1289 case GL_ATOMIC_COUNTER_BUFFER:
1290 *val = is_resource_referenced(shProg, res, index,
1291 stage_from_enum(prop));
1292 return 1;
1293 default:
1294 goto invalid_operation;
1295 }
1296 case GL_LOCATION:
1297 switch (res->Type) {
1298 case GL_UNIFORM:
1299 case GL_VERTEX_SUBROUTINE_UNIFORM:
1300 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1301 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1302 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1303 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1304 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1305 case GL_PROGRAM_INPUT:
1306 case GL_PROGRAM_OUTPUT:
1307 *val = program_resource_location(res, 0);
1308 return 1;
1309 default:
1310 goto invalid_operation;
1311 }
1312 case GL_LOCATION_COMPONENT:
1313 switch (res->Type) {
1314 case GL_PROGRAM_INPUT:
1315 case GL_PROGRAM_OUTPUT:
1316 *val = RESOURCE_VAR(res)->component;
1317 return 1;
1318 default:
1319 goto invalid_operation;
1320 }
1321 case GL_LOCATION_INDEX: {
1322 int tmp;
1323 if (res->Type != GL_PROGRAM_OUTPUT)
1324 goto invalid_operation;
1325 tmp = program_resource_location(res, 0);
1326 if (tmp == -1)
1327 *val = -1;
1328 else
1329 *val = _get_resource_location_index(res);
1330 return 1;
1331 }
1332 case GL_NUM_COMPATIBLE_SUBROUTINES:
1333 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1334 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1335 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1336 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1337 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1338 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1339 goto invalid_operation;
1340 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1341 return 1;
1342 case GL_COMPATIBLE_SUBROUTINES: {
1343 const struct gl_uniform_storage *uni;
1344 struct gl_program *p;
1345 unsigned count, i;
1346 int j;
1347
1348 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1349 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1350 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1351 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1352 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1353 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1354 goto invalid_operation;
1355 uni = RESOURCE_UNI(res);
1356
1357 p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
1358 count = 0;
1359 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
1360 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
1361 for (j = 0; j < fn->num_compat_types; j++) {
1362 if (fn->types[j] == uni->type) {
1363 val[count++] = i;
1364 break;
1365 }
1366 }
1367 }
1368 return count;
1369 }
1370
1371 case GL_TOP_LEVEL_ARRAY_SIZE:
1372 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1373 *val = RESOURCE_UNI(res)->top_level_array_size;
1374 return 1;
1375
1376 case GL_TOP_LEVEL_ARRAY_STRIDE:
1377 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1378 *val = RESOURCE_UNI(res)->top_level_array_stride;
1379 return 1;
1380
1381 /* GL_ARB_tessellation_shader */
1382 case GL_IS_PER_PATCH:
1383 switch (res->Type) {
1384 case GL_PROGRAM_INPUT:
1385 case GL_PROGRAM_OUTPUT:
1386 *val = RESOURCE_VAR(res)->patch;
1387 return 1;
1388 default:
1389 goto invalid_operation;
1390 }
1391
1392 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1393 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1394 *val = RESOURCE_XFV(res)->BufferIndex;
1395 return 1;
1396 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1397 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1398 *val = RESOURCE_XFB(res)->Stride * 4;
1399 return 1;
1400
1401 default:
1402 goto invalid_enum;
1403 }
1404
1405 #undef VALIDATE_TYPE
1406 #undef VALIDATE_TYPE_2
1407
1408 invalid_enum:
1409 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1410 _mesa_enum_to_string(res->Type),
1411 _mesa_enum_to_string(prop));
1412 return 0;
1413
1414 invalid_operation:
1415 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1416 _mesa_enum_to_string(res->Type),
1417 _mesa_enum_to_string(prop));
1418 return 0;
1419 }
1420
1421 extern void
1422 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1423 GLenum programInterface, GLuint index, GLsizei propCount,
1424 const GLenum *props, GLsizei bufSize,
1425 GLsizei *length, GLint *params)
1426 {
1427 GET_CURRENT_CONTEXT(ctx);
1428 GLint *val = (GLint *) params;
1429 const GLenum *prop = props;
1430 GLsizei amount = 0;
1431
1432 struct gl_program_resource *res =
1433 _mesa_program_resource_find_index(shProg, programInterface, index);
1434
1435 /* No such resource found or bufSize negative. */
1436 if (!res || bufSize < 0) {
1437 _mesa_error(ctx, GL_INVALID_VALUE,
1438 "glGetProgramResourceiv(%s index %d bufSize %d)",
1439 _mesa_enum_to_string(programInterface), index, bufSize);
1440 return;
1441 }
1442
1443 /* Write propCount values until error occurs or bufSize reached. */
1444 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1445 int props_written =
1446 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1447 "glGetProgramResourceiv");
1448
1449 /* Error happened. */
1450 if (props_written == 0)
1451 return;
1452
1453 amount += props_written;
1454 }
1455
1456 /* If <length> is not NULL, the actual number of integer values
1457 * written to <params> will be written to <length>.
1458 */
1459 if (length)
1460 *length = amount;
1461 }
1462
1463 static bool
1464 validate_io(struct gl_program *producer, struct gl_program *consumer)
1465 {
1466 if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
1467 return true;
1468
1469 const bool producer_is_array_stage =
1470 producer->info.stage == MESA_SHADER_TESS_CTRL;
1471 const bool consumer_is_array_stage =
1472 consumer->info.stage == MESA_SHADER_GEOMETRY ||
1473 consumer->info.stage == MESA_SHADER_TESS_CTRL ||
1474 consumer->info.stage == MESA_SHADER_TESS_EVAL;
1475
1476 bool valid = true;
1477
1478 gl_shader_variable const **outputs =
1479 (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
1480 sizeof(gl_shader_variable *));
1481 if (outputs == NULL)
1482 return false;
1483
1484 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1485 * says:
1486 *
1487 * At an interface between program objects, the set of inputs and
1488 * outputs are considered to match exactly if and only if:
1489 *
1490 * - Every declared input variable has a matching output, as described
1491 * above.
1492 * - There are no user-defined output variables declared without a
1493 * matching input variable declaration.
1494 *
1495 * Every input has an output, and every output has an input. Scan the list
1496 * of producer resources once, and generate the list of outputs. As inputs
1497 * and outputs are matched, remove the matched outputs from the set. At
1498 * the end, the set must be empty. If the set is not empty, then there is
1499 * some output that did not have an input.
1500 */
1501 unsigned num_outputs = 0;
1502 for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
1503 struct gl_program_resource *res =
1504 &producer->sh.data->ProgramResourceList[i];
1505
1506 if (res->Type != GL_PROGRAM_OUTPUT)
1507 continue;
1508
1509 gl_shader_variable const *const var = RESOURCE_VAR(res);
1510
1511 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1512 * says:
1513 *
1514 * Built-in inputs or outputs do not affect interface matching.
1515 */
1516 if (is_gl_identifier(var->name))
1517 continue;
1518
1519 outputs[num_outputs++] = var;
1520 }
1521
1522 unsigned match_index = 0;
1523 for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
1524 struct gl_program_resource *res =
1525 &consumer->sh.data->ProgramResourceList[i];
1526
1527 if (res->Type != GL_PROGRAM_INPUT)
1528 continue;
1529
1530 gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
1531 gl_shader_variable const *producer_var = NULL;
1532
1533 if (is_gl_identifier(consumer_var->name))
1534 continue;
1535
1536 /* Inputs with explicit locations match other outputs with explicit
1537 * locations by location instead of by name.
1538 */
1539 if (consumer_var->explicit_location) {
1540 for (unsigned j = 0; j < num_outputs; j++) {
1541 const gl_shader_variable *const var = outputs[j];
1542
1543 if (var->explicit_location &&
1544 consumer_var->location == var->location) {
1545 producer_var = var;
1546 match_index = j;
1547 break;
1548 }
1549 }
1550 } else {
1551 for (unsigned j = 0; j < num_outputs; j++) {
1552 const gl_shader_variable *const var = outputs[j];
1553
1554 if (!var->explicit_location &&
1555 strcmp(consumer_var->name, var->name) == 0) {
1556 producer_var = var;
1557 match_index = j;
1558 break;
1559 }
1560 }
1561 }
1562
1563 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1564 * says:
1565 *
1566 * - An output variable is considered to match an input variable in
1567 * the subsequent shader if:
1568 *
1569 * - the two variables match in name, type, and qualification; or
1570 *
1571 * - the two variables are declared with the same location
1572 * qualifier and match in type and qualification.
1573 */
1574 if (producer_var == NULL) {
1575 valid = false;
1576 goto out;
1577 }
1578
1579 /* An output cannot match more than one input, so remove the output from
1580 * the set of possible outputs.
1581 */
1582 outputs[match_index] = NULL;
1583 num_outputs--;
1584 if (match_index < num_outputs)
1585 outputs[match_index] = outputs[num_outputs];
1586
1587 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1588 *
1589 * "Tessellation control shader per-vertex output variables and
1590 * blocks and tessellation control, tessellation evaluation, and
1591 * geometry shader per-vertex input variables and blocks are
1592 * required to be declared as arrays, with each element representing
1593 * input or output values for a single vertex of a multi-vertex
1594 * primitive. For the purposes of interface matching, such variables
1595 * and blocks are treated as though they were not declared as
1596 * arrays."
1597 *
1598 * So we unwrap those types before matching.
1599 */
1600 const glsl_type *consumer_type = consumer_var->type;
1601 const glsl_type *consumer_interface_type = consumer_var->interface_type;
1602 const glsl_type *producer_type = producer_var->type;
1603 const glsl_type *producer_interface_type = producer_var->interface_type;
1604
1605 if (consumer_is_array_stage) {
1606 if (consumer_interface_type) {
1607 /* the interface is the array; the underlying types should match */
1608 if (consumer_interface_type->is_array() && !consumer_var->patch)
1609 consumer_interface_type = consumer_interface_type->fields.array;
1610 } else {
1611 if (consumer_type->is_array() && !consumer_var->patch)
1612 consumer_type = consumer_type->fields.array;
1613 }
1614 }
1615
1616 if (producer_is_array_stage) {
1617 if (producer_interface_type) {
1618 /* the interface is the array; the underlying types should match */
1619 if (producer_interface_type->is_array() && !producer_var->patch)
1620 producer_interface_type = producer_interface_type->fields.array;
1621 } else {
1622 if (producer_type->is_array() && !producer_var->patch)
1623 producer_type = producer_type->fields.array;
1624 }
1625 }
1626
1627 if (producer_type != consumer_type) {
1628 valid = false;
1629 goto out;
1630 }
1631
1632 if (producer_interface_type != consumer_interface_type) {
1633 valid = false;
1634 goto out;
1635 }
1636
1637 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1638 *
1639 * Qualifier Class| Qualifier |in/out
1640 * ---------------+-------------+------
1641 * Storage | in |
1642 * | out | N/A
1643 * | uniform |
1644 * ---------------+-------------+------
1645 * Auxiliary | centroid | No
1646 * ---------------+-------------+------
1647 * | location | Yes
1648 * | Block layout| N/A
1649 * | binding | N/A
1650 * | offset | N/A
1651 * | format | N/A
1652 * ---------------+-------------+------
1653 * Interpolation | smooth |
1654 * | flat | Yes
1655 * ---------------+-------------+------
1656 * | lowp |
1657 * Precision | mediump | Yes
1658 * | highp |
1659 * ---------------+-------------+------
1660 * Variance | invariant | No
1661 * ---------------+-------------+------
1662 * Memory | all | N/A
1663 *
1664 * Note that location mismatches are detected by the loops above that
1665 * find the producer variable that goes with the consumer variable.
1666 */
1667 unsigned producer_interpolation = producer_var->interpolation;
1668 unsigned consumer_interpolation = consumer_var->interpolation;
1669 if (producer_interpolation == INTERP_MODE_NONE)
1670 producer_interpolation = INTERP_MODE_SMOOTH;
1671 if (consumer_interpolation == INTERP_MODE_NONE)
1672 consumer_interpolation = INTERP_MODE_SMOOTH;
1673 if (producer_interpolation != consumer_interpolation) {
1674 valid = false;
1675 goto out;
1676 }
1677
1678 if (producer_var->precision != consumer_var->precision) {
1679 valid = false;
1680 goto out;
1681 }
1682
1683 if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
1684 valid = false;
1685 goto out;
1686 }
1687 }
1688
1689 out:
1690 free(outputs);
1691 return valid && num_outputs == 0;
1692 }
1693
1694 /**
1695 * Validate inputs against outputs in a program pipeline.
1696 */
1697 extern "C" bool
1698 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
1699 {
1700 struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
1701
1702 /* Find first active stage in pipeline. */
1703 unsigned idx, prev = 0;
1704 for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1705 if (prog[idx]) {
1706 prev = idx;
1707 break;
1708 }
1709 }
1710
1711 for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1712 if (prog[idx]) {
1713 /* Pipeline might include both non-compute and a compute program, do
1714 * not attempt to validate varyings between non-compute and compute
1715 * stage.
1716 */
1717 if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
1718 break;
1719
1720 if (!validate_io(prog[prev], prog[idx]))
1721 return false;
1722
1723 prev = idx;
1724 }
1725 }
1726 return true;
1727 }