mesa: Fix ReadBuffers with pbuffers
[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 /* Find an uniform or buffer variable program resource with an specific offset
642 * inside a block with an specific binding.
643 *
644 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
645 */
646 static struct gl_program_resource *
647 program_resource_find_binding_offset(struct gl_shader_program *shProg,
648 GLenum programInterface,
649 const GLuint binding,
650 const GLint offset)
651 {
652
653 /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
654 GLenum blockInterface;
655
656 switch (programInterface) {
657 case GL_BUFFER_VARIABLE:
658 blockInterface = GL_SHADER_STORAGE_BLOCK;
659 break;
660 case GL_UNIFORM:
661 blockInterface = GL_UNIFORM_BLOCK;
662 break;
663 default:
664 assert("Invalid program interface");
665 return NULL;
666 }
667
668 int block_index = -1;
669 int starting_index = -1;
670 struct gl_program_resource *res = shProg->data->ProgramResourceList;
671
672 /* Blocks are added to the resource list in the same order that they are
673 * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
674 * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
675 * the resource list.
676 */
677 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
678 if (res->Type != blockInterface)
679 continue;
680
681 /* Store the first index where a resource of the specific interface is. */
682 if (starting_index == -1)
683 starting_index = i;
684
685 const struct gl_uniform_block *block = RESOURCE_UBO(res);
686
687 if (block->Binding == binding) {
688 /* For arrays, or arrays of arrays of blocks, we want the resource
689 * for the block with base index. Most properties for members of each
690 * block are inherited from the block with the base index, including
691 * a uniform being active or not.
692 */
693 block_index = i - starting_index - block->linearized_array_index;
694 break;
695 }
696 }
697
698 if (block_index == -1)
699 return NULL;
700
701 /* We now look for the resource corresponding to the uniform or buffer
702 * variable using the BLOCK_INDEX and OFFSET.
703 */
704 res = shProg->data->ProgramResourceList;
705 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
706 if (res->Type != programInterface)
707 continue;
708
709 const struct gl_uniform_storage *uniform = RESOURCE_UNI(res);
710
711 if (uniform->block_index == block_index && uniform->offset == offset) {
712 return res;
713 }
714 }
715
716 return NULL;
717 }
718
719 /* Checks if an uniform or buffer variable is in the active program resource
720 * list.
721 *
722 * It takes into accout that for variables coming from SPIR-V binaries their
723 * names could not be available (ARB_gl_spirv). In that case, it will use the
724 * the offset and the block binding to locate the resource.
725 *
726 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
727 */
728 struct gl_program_resource *
729 _mesa_program_resource_find_active_variable(struct gl_shader_program *shProg,
730 GLenum programInterface,
731 const gl_uniform_block *block,
732 unsigned index)
733 {
734 struct gl_program_resource *res;
735 struct gl_uniform_buffer_variable uni = block->Uniforms[index];
736
737 assert(programInterface == GL_UNIFORM ||
738 programInterface == GL_BUFFER_VARIABLE);
739
740 if (uni.IndexName) {
741 res = _mesa_program_resource_find_name(shProg, programInterface, uni.IndexName,
742 NULL);
743 } else {
744 /* As the resource has no associated name (ARB_gl_spirv),
745 * we can use the UBO/SSBO binding and offset to find it.
746 */
747 res = program_resource_find_binding_offset(shProg, programInterface,
748 block->Binding, uni.Offset);
749 }
750
751 return res;
752 }
753
754 static GLuint
755 calc_resource_index(struct gl_shader_program *shProg,
756 struct gl_program_resource *res)
757 {
758 unsigned i;
759 GLuint index = 0;
760 for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
761 if (&shProg->data->ProgramResourceList[i] == res)
762 return index;
763 if (shProg->data->ProgramResourceList[i].Type == res->Type)
764 index++;
765 }
766 return GL_INVALID_INDEX;
767 }
768
769 /**
770 * Calculate index for the given resource.
771 */
772 GLuint
773 _mesa_program_resource_index(struct gl_shader_program *shProg,
774 struct gl_program_resource *res)
775 {
776 if (!res)
777 return GL_INVALID_INDEX;
778
779 switch (res->Type) {
780 case GL_ATOMIC_COUNTER_BUFFER:
781 return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
782 case GL_VERTEX_SUBROUTINE:
783 case GL_GEOMETRY_SUBROUTINE:
784 case GL_FRAGMENT_SUBROUTINE:
785 case GL_COMPUTE_SUBROUTINE:
786 case GL_TESS_CONTROL_SUBROUTINE:
787 case GL_TESS_EVALUATION_SUBROUTINE:
788 return RESOURCE_SUB(res)->index;
789 case GL_UNIFORM_BLOCK:
790 case GL_SHADER_STORAGE_BLOCK:
791 case GL_TRANSFORM_FEEDBACK_BUFFER:
792 case GL_TRANSFORM_FEEDBACK_VARYING:
793 default:
794 return calc_resource_index(shProg, res);
795 }
796 }
797
798 /**
799 * Find a program resource that points to given data.
800 */
801 static struct gl_program_resource*
802 program_resource_find_data(struct gl_shader_program *shProg, void *data)
803 {
804 struct gl_program_resource *res = shProg->data->ProgramResourceList;
805 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
806 i++, res++) {
807 if (res->Data == data)
808 return res;
809 }
810 return NULL;
811 }
812
813 /* Find a program resource with specific index in given interface.
814 */
815 struct gl_program_resource *
816 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
817 GLenum programInterface, GLuint index)
818 {
819 struct gl_program_resource *res = shProg->data->ProgramResourceList;
820 int idx = -1;
821
822 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
823 i++, res++) {
824 if (res->Type != programInterface)
825 continue;
826
827 switch (res->Type) {
828 case GL_UNIFORM_BLOCK:
829 case GL_ATOMIC_COUNTER_BUFFER:
830 case GL_SHADER_STORAGE_BLOCK:
831 case GL_TRANSFORM_FEEDBACK_BUFFER:
832 if (_mesa_program_resource_index(shProg, res) == index)
833 return res;
834 break;
835 case GL_TRANSFORM_FEEDBACK_VARYING:
836 case GL_PROGRAM_INPUT:
837 case GL_PROGRAM_OUTPUT:
838 case GL_UNIFORM:
839 case GL_VERTEX_SUBROUTINE_UNIFORM:
840 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
841 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
842 case GL_COMPUTE_SUBROUTINE_UNIFORM:
843 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
844 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
845 case GL_VERTEX_SUBROUTINE:
846 case GL_GEOMETRY_SUBROUTINE:
847 case GL_FRAGMENT_SUBROUTINE:
848 case GL_COMPUTE_SUBROUTINE:
849 case GL_TESS_CONTROL_SUBROUTINE:
850 case GL_TESS_EVALUATION_SUBROUTINE:
851 case GL_BUFFER_VARIABLE:
852 if (++idx == (int) index)
853 return res;
854 break;
855 default:
856 assert(!"not implemented for given interface");
857 }
858 }
859 return NULL;
860 }
861
862 /* Function returns if resource name is expected to have index
863 * appended into it.
864 *
865 *
866 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
867 * spec says:
868 *
869 * "If the active uniform is an array, the uniform name returned in
870 * name will always be the name of the uniform array appended with
871 * "[0]"."
872 *
873 * The same text also appears in the OpenGL 4.2 spec. It does not,
874 * however, appear in any previous spec. Previous specifications are
875 * ambiguous in this regard. However, either name can later be passed
876 * to glGetUniformLocation (and related APIs), so there shouldn't be any
877 * harm in always appending "[0]" to uniform array names.
878 */
879 static bool
880 add_index_to_name(struct gl_program_resource *res)
881 {
882 /* Transform feedback varyings have array index already appended
883 * in their names.
884 */
885 return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
886 }
887
888 /* Get name length of a program resource. This consists of
889 * base name + 3 for '[0]' if resource is an array.
890 */
891 extern unsigned
892 _mesa_program_resource_name_len(struct gl_program_resource *res)
893 {
894 const char* name = _mesa_program_resource_name(res);
895
896 /* For shaders constructed from SPIR-V binaries, variables may not
897 * have names associated with them.
898 */
899 if (!name)
900 return 0;
901
902 unsigned length = strlen(name);
903 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
904 length += 3;
905 return length;
906 }
907
908 /* Get full name of a program resource.
909 */
910 bool
911 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
912 GLenum programInterface, GLuint index,
913 GLsizei bufSize, GLsizei *length,
914 GLchar *name, const char *caller)
915 {
916 GET_CURRENT_CONTEXT(ctx);
917
918 /* Find resource with given interface and index. */
919 struct gl_program_resource *res =
920 _mesa_program_resource_find_index(shProg, programInterface, index);
921
922 /* The error INVALID_VALUE is generated if <index> is greater than
923 * or equal to the number of entries in the active resource list for
924 * <programInterface>.
925 */
926 if (!res) {
927 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
928 return false;
929 }
930
931 if (bufSize < 0) {
932 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
933 return false;
934 }
935
936 GLsizei localLength;
937
938 if (length == NULL)
939 length = &localLength;
940
941 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
942
943 /* The resource name can be NULL for shaders constructed from SPIR-V
944 * binaries. In that case, we do not add the '[0]'.
945 */
946 if (name && name[0] != '\0' &&
947 _mesa_program_resource_array_size(res) && add_index_to_name(res)) {
948 int i;
949
950 /* The comparison is strange because *length does *NOT* include the
951 * terminating NUL, but maxLength does.
952 */
953 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
954 name[*length + i] = "[0]"[i];
955
956 name[*length + i] = '\0';
957 *length += i;
958 }
959 return true;
960 }
961
962 static GLint
963 program_resource_location(struct gl_program_resource *res, unsigned array_index)
964 {
965 switch (res->Type) {
966 case GL_PROGRAM_INPUT: {
967 const gl_shader_variable *var = RESOURCE_VAR(res);
968
969 if (var->location == -1)
970 return -1;
971
972 /* If the input is an array, fail if the index is out of bounds. */
973 if (array_index > 0
974 && array_index >= var->type->length) {
975 return -1;
976 }
977 return var->location +
978 (array_index * var->type->without_array()->matrix_columns);
979 }
980 case GL_PROGRAM_OUTPUT:
981 if (RESOURCE_VAR(res)->location == -1)
982 return -1;
983
984 /* If the output is an array, fail if the index is out of bounds. */
985 if (array_index > 0
986 && array_index >= RESOURCE_VAR(res)->type->length) {
987 return -1;
988 }
989 return RESOURCE_VAR(res)->location + array_index;
990 case GL_UNIFORM:
991 /* If the uniform is built-in, fail. */
992 if (RESOURCE_UNI(res)->builtin)
993 return -1;
994
995 /* From page 79 of the OpenGL 4.2 spec:
996 *
997 * "A valid name cannot be a structure, an array of structures, or any
998 * portion of a single vector or a matrix."
999 */
1000 if (RESOURCE_UNI(res)->type->without_array()->is_struct())
1001 return -1;
1002
1003 /* From the GL_ARB_uniform_buffer_object spec:
1004 *
1005 * "The value -1 will be returned if <name> does not correspond to an
1006 * active uniform variable name in <program>, if <name> is associated
1007 * with a named uniform block, or if <name> starts with the reserved
1008 * prefix "gl_"."
1009 */
1010 if (RESOURCE_UNI(res)->block_index != -1 ||
1011 RESOURCE_UNI(res)->atomic_buffer_index != -1)
1012 return -1;
1013
1014 /* fallthrough */
1015 case GL_VERTEX_SUBROUTINE_UNIFORM:
1016 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1017 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1018 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1019 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1020 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1021 /* If the uniform is an array, fail if the index is out of bounds. */
1022 if (array_index > 0
1023 && array_index >= RESOURCE_UNI(res)->array_elements) {
1024 return -1;
1025 }
1026
1027 /* location in remap table + array element offset */
1028 return RESOURCE_UNI(res)->remap_location + array_index;
1029 default:
1030 return -1;
1031 }
1032 }
1033
1034 /**
1035 * Function implements following location queries:
1036 * glGetUniformLocation
1037 */
1038 GLint
1039 _mesa_program_resource_location(struct gl_shader_program *shProg,
1040 GLenum programInterface, const char *name)
1041 {
1042 unsigned array_index = 0;
1043 struct gl_program_resource *res =
1044 _mesa_program_resource_find_name(shProg, programInterface, name,
1045 &array_index);
1046
1047 /* Resource not found. */
1048 if (!res)
1049 return -1;
1050
1051 return program_resource_location(res, array_index);
1052 }
1053
1054 static GLint
1055 _get_resource_location_index(struct gl_program_resource *res)
1056 {
1057 /* Non-existent variable or resource is not referenced by fragment stage. */
1058 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1059 return -1;
1060
1061 /* From OpenGL 4.5 spec, 7.3 Program Objects
1062 * "The value -1 will be returned by either command...
1063 * ... or if name identifies an active variable that does not have a
1064 * valid location assigned.
1065 */
1066 if (RESOURCE_VAR(res)->location == -1)
1067 return -1;
1068 return RESOURCE_VAR(res)->index;
1069 }
1070
1071 /**
1072 * Function implements following index queries:
1073 * glGetFragDataIndex
1074 */
1075 GLint
1076 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1077 GLenum programInterface, const char *name)
1078 {
1079 struct gl_program_resource *res =
1080 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1081
1082 return _get_resource_location_index(res);
1083 }
1084
1085 static uint8_t
1086 stage_from_enum(GLenum ref)
1087 {
1088 switch (ref) {
1089 case GL_REFERENCED_BY_VERTEX_SHADER:
1090 return MESA_SHADER_VERTEX;
1091 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1092 return MESA_SHADER_TESS_CTRL;
1093 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1094 return MESA_SHADER_TESS_EVAL;
1095 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1096 return MESA_SHADER_GEOMETRY;
1097 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1098 return MESA_SHADER_FRAGMENT;
1099 case GL_REFERENCED_BY_COMPUTE_SHADER:
1100 return MESA_SHADER_COMPUTE;
1101 default:
1102 assert(!"shader stage not supported");
1103 return MESA_SHADER_STAGES;
1104 }
1105 }
1106
1107 /**
1108 * Check if resource is referenced by given 'referenced by' stage enum.
1109 * ATC and UBO resources hold stage references of their own.
1110 */
1111 static bool
1112 is_resource_referenced(struct gl_shader_program *shProg,
1113 struct gl_program_resource *res,
1114 GLuint index, uint8_t stage)
1115 {
1116 /* First, check if we even have such a stage active. */
1117 if (!shProg->_LinkedShaders[stage])
1118 return false;
1119
1120 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1121 return RESOURCE_ATC(res)->StageReferences[stage];
1122
1123 if (res->Type == GL_UNIFORM_BLOCK)
1124 return shProg->data->UniformBlocks[index].stageref & (1 << stage);
1125
1126 if (res->Type == GL_SHADER_STORAGE_BLOCK)
1127 return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
1128
1129 return res->StageReferences & (1 << stage);
1130 }
1131
1132 static unsigned
1133 get_buffer_property(struct gl_shader_program *shProg,
1134 struct gl_program_resource *res, const GLenum prop,
1135 GLint *val, const char *caller)
1136 {
1137 GET_CURRENT_CONTEXT(ctx);
1138 if (res->Type != GL_UNIFORM_BLOCK &&
1139 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1140 res->Type != GL_SHADER_STORAGE_BLOCK &&
1141 res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
1142 goto invalid_operation;
1143
1144 if (res->Type == GL_UNIFORM_BLOCK) {
1145 switch (prop) {
1146 case GL_BUFFER_BINDING:
1147 *val = RESOURCE_UBO(res)->Binding;
1148 return 1;
1149 case GL_BUFFER_DATA_SIZE:
1150 *val = RESOURCE_UBO(res)->UniformBufferSize;
1151 return 1;
1152 case GL_NUM_ACTIVE_VARIABLES:
1153 *val = 0;
1154 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1155 struct gl_program_resource *uni =
1156 _mesa_program_resource_find_active_variable(
1157 shProg,
1158 GL_UNIFORM,
1159 RESOURCE_UBO(res),
1160 i);
1161
1162 if (!uni)
1163 continue;
1164 (*val)++;
1165 }
1166 return 1;
1167 case GL_ACTIVE_VARIABLES: {
1168 unsigned num_values = 0;
1169 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1170 struct gl_program_resource *uni =
1171 _mesa_program_resource_find_active_variable(
1172 shProg,
1173 GL_UNIFORM,
1174 RESOURCE_UBO(res),
1175 i);
1176
1177 if (!uni)
1178 continue;
1179 *val++ =
1180 _mesa_program_resource_index(shProg, uni);
1181 num_values++;
1182 }
1183 return num_values;
1184 }
1185 }
1186 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1187 switch (prop) {
1188 case GL_BUFFER_BINDING:
1189 *val = RESOURCE_UBO(res)->Binding;
1190 return 1;
1191 case GL_BUFFER_DATA_SIZE:
1192 *val = RESOURCE_UBO(res)->UniformBufferSize;
1193 return 1;
1194 case GL_NUM_ACTIVE_VARIABLES:
1195 *val = 0;
1196 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1197 struct gl_program_resource *uni =
1198 _mesa_program_resource_find_active_variable(
1199 shProg,
1200 GL_BUFFER_VARIABLE,
1201 RESOURCE_UBO(res),
1202 i);
1203
1204 if (!uni)
1205 continue;
1206 (*val)++;
1207 }
1208 return 1;
1209 case GL_ACTIVE_VARIABLES: {
1210 unsigned num_values = 0;
1211 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1212 struct gl_program_resource *uni =
1213 _mesa_program_resource_find_active_variable(
1214 shProg,
1215 GL_BUFFER_VARIABLE,
1216 RESOURCE_UBO(res),
1217 i);
1218
1219 if (!uni)
1220 continue;
1221 *val++ =
1222 _mesa_program_resource_index(shProg, uni);
1223 num_values++;
1224 }
1225 return num_values;
1226 }
1227 }
1228 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1229 switch (prop) {
1230 case GL_BUFFER_BINDING:
1231 *val = RESOURCE_ATC(res)->Binding;
1232 return 1;
1233 case GL_BUFFER_DATA_SIZE:
1234 *val = RESOURCE_ATC(res)->MinimumSize;
1235 return 1;
1236 case GL_NUM_ACTIVE_VARIABLES:
1237 *val = RESOURCE_ATC(res)->NumUniforms;
1238 return 1;
1239 case GL_ACTIVE_VARIABLES:
1240 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1241 /* Active atomic buffer contains index to UniformStorage. Find
1242 * out gl_program_resource via data pointer and then calculate
1243 * index of that uniform.
1244 */
1245 unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1246 struct gl_program_resource *uni =
1247 program_resource_find_data(shProg,
1248 &shProg->data->UniformStorage[idx]);
1249 assert(uni);
1250 *val++ = _mesa_program_resource_index(shProg, uni);
1251 }
1252 return RESOURCE_ATC(res)->NumUniforms;
1253 }
1254 } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1255 switch (prop) {
1256 case GL_BUFFER_BINDING:
1257 *val = RESOURCE_XFB(res)->Binding;
1258 return 1;
1259 case GL_NUM_ACTIVE_VARIABLES:
1260 *val = RESOURCE_XFB(res)->NumVaryings;
1261 return 1;
1262 case GL_ACTIVE_VARIABLES:
1263 struct gl_transform_feedback_info *linked_xfb =
1264 shProg->last_vert_prog->sh.LinkedTransformFeedback;
1265 for (int i = 0; i < linked_xfb->NumVarying; i++) {
1266 unsigned index = linked_xfb->Varyings[i].BufferIndex;
1267 struct gl_program_resource *buf_res =
1268 _mesa_program_resource_find_index(shProg,
1269 GL_TRANSFORM_FEEDBACK_BUFFER,
1270 index);
1271 assert(buf_res);
1272 if (res == buf_res) {
1273 *val++ = i;
1274 }
1275 }
1276 return RESOURCE_XFB(res)->NumVaryings;
1277 }
1278 }
1279 assert(!"support for property type not implemented");
1280
1281 invalid_operation:
1282 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1283 _mesa_enum_to_string(res->Type),
1284 _mesa_enum_to_string(prop));
1285
1286 return 0;
1287 }
1288
1289 unsigned
1290 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1291 struct gl_program_resource *res, GLuint index,
1292 const GLenum prop, GLint *val, const char *caller)
1293 {
1294 GET_CURRENT_CONTEXT(ctx);
1295
1296 #define VALIDATE_TYPE(type)\
1297 if (res->Type != type)\
1298 goto invalid_operation;
1299
1300 #define VALIDATE_TYPE_2(type1, type2)\
1301 if (res->Type != type1 && res->Type != type2)\
1302 goto invalid_operation;
1303
1304 switch(prop) {
1305 case GL_NAME_LENGTH:
1306 switch (res->Type) {
1307 case GL_ATOMIC_COUNTER_BUFFER:
1308 case GL_TRANSFORM_FEEDBACK_BUFFER:
1309 goto invalid_operation;
1310 default:
1311 /* Resource name length + terminator. */
1312 *val = _mesa_program_resource_name_len(res) + 1;
1313 }
1314 return 1;
1315 case GL_TYPE:
1316 switch (res->Type) {
1317 case GL_UNIFORM:
1318 case GL_BUFFER_VARIABLE:
1319 *val = RESOURCE_UNI(res)->type->gl_type;
1320 return 1;
1321 case GL_PROGRAM_INPUT:
1322 case GL_PROGRAM_OUTPUT:
1323 *val = RESOURCE_VAR(res)->type->gl_type;
1324 return 1;
1325 case GL_TRANSFORM_FEEDBACK_VARYING:
1326 *val = RESOURCE_XFV(res)->Type;
1327 return 1;
1328 default:
1329 goto invalid_operation;
1330 }
1331 case GL_ARRAY_SIZE:
1332 switch (res->Type) {
1333 case GL_UNIFORM:
1334 case GL_BUFFER_VARIABLE:
1335 case GL_VERTEX_SUBROUTINE_UNIFORM:
1336 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1337 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1338 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1339 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1340 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1341
1342 /* Test if a buffer variable is an array or an unsized array.
1343 * Unsized arrays return zero as array size.
1344 */
1345 if (RESOURCE_UNI(res)->is_shader_storage &&
1346 RESOURCE_UNI(res)->array_stride > 0)
1347 *val = RESOURCE_UNI(res)->array_elements;
1348 else
1349 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1350 return 1;
1351 case GL_PROGRAM_INPUT:
1352 case GL_PROGRAM_OUTPUT:
1353 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1354 return 1;
1355 case GL_TRANSFORM_FEEDBACK_VARYING:
1356 *val = RESOURCE_XFV(res)->Size;
1357 return 1;
1358 default:
1359 goto invalid_operation;
1360 }
1361 case GL_OFFSET:
1362 switch (res->Type) {
1363 case GL_UNIFORM:
1364 case GL_BUFFER_VARIABLE:
1365 *val = RESOURCE_UNI(res)->offset;
1366 return 1;
1367 case GL_TRANSFORM_FEEDBACK_VARYING:
1368 *val = RESOURCE_XFV(res)->Offset;
1369 return 1;
1370 default:
1371 goto invalid_operation;
1372 }
1373 case GL_BLOCK_INDEX:
1374 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1375 *val = RESOURCE_UNI(res)->block_index;
1376 return 1;
1377 case GL_ARRAY_STRIDE:
1378 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1379 *val = RESOURCE_UNI(res)->array_stride;
1380 return 1;
1381 case GL_MATRIX_STRIDE:
1382 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1383 *val = RESOURCE_UNI(res)->matrix_stride;
1384 return 1;
1385 case GL_IS_ROW_MAJOR:
1386 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1387 *val = RESOURCE_UNI(res)->row_major;
1388 return 1;
1389 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1390 VALIDATE_TYPE(GL_UNIFORM);
1391 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1392 return 1;
1393 case GL_BUFFER_BINDING:
1394 case GL_BUFFER_DATA_SIZE:
1395 case GL_NUM_ACTIVE_VARIABLES:
1396 case GL_ACTIVE_VARIABLES:
1397 return get_buffer_property(shProg, res, prop, val, caller);
1398 case GL_REFERENCED_BY_COMPUTE_SHADER:
1399 if (!_mesa_has_compute_shaders(ctx))
1400 goto invalid_enum;
1401 /* fallthrough */
1402 case GL_REFERENCED_BY_VERTEX_SHADER:
1403 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1404 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1405 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1406 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1407 switch (res->Type) {
1408 case GL_UNIFORM:
1409 case GL_PROGRAM_INPUT:
1410 case GL_PROGRAM_OUTPUT:
1411 case GL_UNIFORM_BLOCK:
1412 case GL_BUFFER_VARIABLE:
1413 case GL_SHADER_STORAGE_BLOCK:
1414 case GL_ATOMIC_COUNTER_BUFFER:
1415 *val = is_resource_referenced(shProg, res, index,
1416 stage_from_enum(prop));
1417 return 1;
1418 default:
1419 goto invalid_operation;
1420 }
1421 case GL_LOCATION:
1422 switch (res->Type) {
1423 case GL_UNIFORM:
1424 case GL_VERTEX_SUBROUTINE_UNIFORM:
1425 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1426 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1427 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1428 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1429 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1430 case GL_PROGRAM_INPUT:
1431 case GL_PROGRAM_OUTPUT:
1432 *val = program_resource_location(res, 0);
1433 return 1;
1434 default:
1435 goto invalid_operation;
1436 }
1437 case GL_LOCATION_COMPONENT:
1438 switch (res->Type) {
1439 case GL_PROGRAM_INPUT:
1440 case GL_PROGRAM_OUTPUT:
1441 *val = RESOURCE_VAR(res)->component;
1442 return 1;
1443 default:
1444 goto invalid_operation;
1445 }
1446 case GL_LOCATION_INDEX: {
1447 int tmp;
1448 if (res->Type != GL_PROGRAM_OUTPUT)
1449 goto invalid_operation;
1450 tmp = program_resource_location(res, 0);
1451 if (tmp == -1)
1452 *val = -1;
1453 else
1454 *val = _get_resource_location_index(res);
1455 return 1;
1456 }
1457 case GL_NUM_COMPATIBLE_SUBROUTINES:
1458 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1459 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1460 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1461 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1462 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1463 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1464 goto invalid_operation;
1465 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1466 return 1;
1467 case GL_COMPATIBLE_SUBROUTINES: {
1468 const struct gl_uniform_storage *uni;
1469 struct gl_program *p;
1470 unsigned count, i;
1471 int j;
1472
1473 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1474 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1475 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1476 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1477 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1478 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1479 goto invalid_operation;
1480 uni = RESOURCE_UNI(res);
1481
1482 p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
1483 count = 0;
1484 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
1485 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
1486 for (j = 0; j < fn->num_compat_types; j++) {
1487 if (fn->types[j] == uni->type) {
1488 val[count++] = i;
1489 break;
1490 }
1491 }
1492 }
1493 return count;
1494 }
1495
1496 case GL_TOP_LEVEL_ARRAY_SIZE:
1497 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1498 *val = RESOURCE_UNI(res)->top_level_array_size;
1499 return 1;
1500
1501 case GL_TOP_LEVEL_ARRAY_STRIDE:
1502 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1503 *val = RESOURCE_UNI(res)->top_level_array_stride;
1504 return 1;
1505
1506 /* GL_ARB_tessellation_shader */
1507 case GL_IS_PER_PATCH:
1508 switch (res->Type) {
1509 case GL_PROGRAM_INPUT:
1510 case GL_PROGRAM_OUTPUT:
1511 *val = RESOURCE_VAR(res)->patch;
1512 return 1;
1513 default:
1514 goto invalid_operation;
1515 }
1516
1517 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1518 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1519 *val = RESOURCE_XFV(res)->BufferIndex;
1520 return 1;
1521 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1522 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1523 *val = RESOURCE_XFB(res)->Stride * 4;
1524 return 1;
1525
1526 default:
1527 goto invalid_enum;
1528 }
1529
1530 #undef VALIDATE_TYPE
1531 #undef VALIDATE_TYPE_2
1532
1533 invalid_enum:
1534 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1535 _mesa_enum_to_string(res->Type),
1536 _mesa_enum_to_string(prop));
1537 return 0;
1538
1539 invalid_operation:
1540 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1541 _mesa_enum_to_string(res->Type),
1542 _mesa_enum_to_string(prop));
1543 return 0;
1544 }
1545
1546 extern void
1547 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1548 GLenum programInterface, GLuint index, GLsizei propCount,
1549 const GLenum *props, GLsizei bufSize,
1550 GLsizei *length, GLint *params)
1551 {
1552 GET_CURRENT_CONTEXT(ctx);
1553 GLint *val = (GLint *) params;
1554 const GLenum *prop = props;
1555 GLsizei amount = 0;
1556
1557 struct gl_program_resource *res =
1558 _mesa_program_resource_find_index(shProg, programInterface, index);
1559
1560 /* No such resource found or bufSize negative. */
1561 if (!res || bufSize < 0) {
1562 _mesa_error(ctx, GL_INVALID_VALUE,
1563 "glGetProgramResourceiv(%s index %d bufSize %d)",
1564 _mesa_enum_to_string(programInterface), index, bufSize);
1565 return;
1566 }
1567
1568 /* Write propCount values until error occurs or bufSize reached. */
1569 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1570 int props_written =
1571 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1572 "glGetProgramResourceiv");
1573
1574 /* Error happened. */
1575 if (props_written == 0)
1576 return;
1577
1578 amount += props_written;
1579 }
1580
1581 /* If <length> is not NULL, the actual number of integer values
1582 * written to <params> will be written to <length>.
1583 */
1584 if (length)
1585 *length = amount;
1586 }
1587
1588 static bool
1589 validate_io(struct gl_program *producer, struct gl_program *consumer)
1590 {
1591 if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
1592 return true;
1593
1594 const bool producer_is_array_stage =
1595 producer->info.stage == MESA_SHADER_TESS_CTRL;
1596 const bool consumer_is_array_stage =
1597 consumer->info.stage == MESA_SHADER_GEOMETRY ||
1598 consumer->info.stage == MESA_SHADER_TESS_CTRL ||
1599 consumer->info.stage == MESA_SHADER_TESS_EVAL;
1600
1601 bool valid = true;
1602
1603 gl_shader_variable const **outputs =
1604 (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
1605 sizeof(gl_shader_variable *));
1606 if (outputs == NULL)
1607 return false;
1608
1609 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1610 * says:
1611 *
1612 * At an interface between program objects, the set of inputs and
1613 * outputs are considered to match exactly if and only if:
1614 *
1615 * - Every declared input variable has a matching output, as described
1616 * above.
1617 * - There are no user-defined output variables declared without a
1618 * matching input variable declaration.
1619 *
1620 * Every input has an output, and every output has an input. Scan the list
1621 * of producer resources once, and generate the list of outputs. As inputs
1622 * and outputs are matched, remove the matched outputs from the set. At
1623 * the end, the set must be empty. If the set is not empty, then there is
1624 * some output that did not have an input.
1625 */
1626 unsigned num_outputs = 0;
1627 for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
1628 struct gl_program_resource *res =
1629 &producer->sh.data->ProgramResourceList[i];
1630
1631 if (res->Type != GL_PROGRAM_OUTPUT)
1632 continue;
1633
1634 gl_shader_variable const *const var = RESOURCE_VAR(res);
1635
1636 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1637 * says:
1638 *
1639 * Built-in inputs or outputs do not affect interface matching.
1640 */
1641 if (is_gl_identifier(var->name))
1642 continue;
1643
1644 outputs[num_outputs++] = var;
1645 }
1646
1647 unsigned match_index = 0;
1648 for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
1649 struct gl_program_resource *res =
1650 &consumer->sh.data->ProgramResourceList[i];
1651
1652 if (res->Type != GL_PROGRAM_INPUT)
1653 continue;
1654
1655 gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
1656 gl_shader_variable const *producer_var = NULL;
1657
1658 if (is_gl_identifier(consumer_var->name))
1659 continue;
1660
1661 /* Inputs with explicit locations match other outputs with explicit
1662 * locations by location instead of by name.
1663 */
1664 if (consumer_var->explicit_location) {
1665 for (unsigned j = 0; j < num_outputs; j++) {
1666 const gl_shader_variable *const var = outputs[j];
1667
1668 if (var->explicit_location &&
1669 consumer_var->location == var->location) {
1670 producer_var = var;
1671 match_index = j;
1672 break;
1673 }
1674 }
1675 } else {
1676 for (unsigned j = 0; j < num_outputs; j++) {
1677 const gl_shader_variable *const var = outputs[j];
1678
1679 if (!var->explicit_location &&
1680 strcmp(consumer_var->name, var->name) == 0) {
1681 producer_var = var;
1682 match_index = j;
1683 break;
1684 }
1685 }
1686 }
1687
1688 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1689 * says:
1690 *
1691 * - An output variable is considered to match an input variable in
1692 * the subsequent shader if:
1693 *
1694 * - the two variables match in name, type, and qualification; or
1695 *
1696 * - the two variables are declared with the same location
1697 * qualifier and match in type and qualification.
1698 */
1699 if (producer_var == NULL) {
1700 valid = false;
1701 goto out;
1702 }
1703
1704 /* An output cannot match more than one input, so remove the output from
1705 * the set of possible outputs.
1706 */
1707 outputs[match_index] = NULL;
1708 num_outputs--;
1709 if (match_index < num_outputs)
1710 outputs[match_index] = outputs[num_outputs];
1711
1712 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1713 *
1714 * "Tessellation control shader per-vertex output variables and
1715 * blocks and tessellation control, tessellation evaluation, and
1716 * geometry shader per-vertex input variables and blocks are
1717 * required to be declared as arrays, with each element representing
1718 * input or output values for a single vertex of a multi-vertex
1719 * primitive. For the purposes of interface matching, such variables
1720 * and blocks are treated as though they were not declared as
1721 * arrays."
1722 *
1723 * So we unwrap those types before matching.
1724 */
1725 const glsl_type *consumer_type = consumer_var->type;
1726 const glsl_type *consumer_interface_type = consumer_var->interface_type;
1727 const glsl_type *producer_type = producer_var->type;
1728 const glsl_type *producer_interface_type = producer_var->interface_type;
1729
1730 if (consumer_is_array_stage) {
1731 if (consumer_interface_type) {
1732 /* the interface is the array; the underlying types should match */
1733 if (consumer_interface_type->is_array() && !consumer_var->patch)
1734 consumer_interface_type = consumer_interface_type->fields.array;
1735 } else {
1736 if (consumer_type->is_array() && !consumer_var->patch)
1737 consumer_type = consumer_type->fields.array;
1738 }
1739 }
1740
1741 if (producer_is_array_stage) {
1742 if (producer_interface_type) {
1743 /* the interface is the array; the underlying types should match */
1744 if (producer_interface_type->is_array() && !producer_var->patch)
1745 producer_interface_type = producer_interface_type->fields.array;
1746 } else {
1747 if (producer_type->is_array() && !producer_var->patch)
1748 producer_type = producer_type->fields.array;
1749 }
1750 }
1751
1752 if (producer_type != consumer_type) {
1753 valid = false;
1754 goto out;
1755 }
1756
1757 if (producer_interface_type != consumer_interface_type) {
1758 valid = false;
1759 goto out;
1760 }
1761
1762 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1763 *
1764 * Qualifier Class| Qualifier |in/out
1765 * ---------------+-------------+------
1766 * Storage | in |
1767 * | out | N/A
1768 * | uniform |
1769 * ---------------+-------------+------
1770 * Auxiliary | centroid | No
1771 * ---------------+-------------+------
1772 * | location | Yes
1773 * | Block layout| N/A
1774 * | binding | N/A
1775 * | offset | N/A
1776 * | format | N/A
1777 * ---------------+-------------+------
1778 * Interpolation | smooth |
1779 * | flat | Yes
1780 * ---------------+-------------+------
1781 * | lowp |
1782 * Precision | mediump | Yes
1783 * | highp |
1784 * ---------------+-------------+------
1785 * Variance | invariant | No
1786 * ---------------+-------------+------
1787 * Memory | all | N/A
1788 *
1789 * Note that location mismatches are detected by the loops above that
1790 * find the producer variable that goes with the consumer variable.
1791 */
1792 unsigned producer_interpolation = producer_var->interpolation;
1793 unsigned consumer_interpolation = consumer_var->interpolation;
1794 if (producer_interpolation == INTERP_MODE_NONE)
1795 producer_interpolation = INTERP_MODE_SMOOTH;
1796 if (consumer_interpolation == INTERP_MODE_NONE)
1797 consumer_interpolation = INTERP_MODE_SMOOTH;
1798 if (producer_interpolation != consumer_interpolation) {
1799 valid = false;
1800 goto out;
1801 }
1802
1803 if (producer_var->precision != consumer_var->precision) {
1804 valid = false;
1805 goto out;
1806 }
1807
1808 if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
1809 valid = false;
1810 goto out;
1811 }
1812 }
1813
1814 out:
1815 free(outputs);
1816 return valid && num_outputs == 0;
1817 }
1818
1819 /**
1820 * Validate inputs against outputs in a program pipeline.
1821 */
1822 extern "C" bool
1823 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
1824 {
1825 struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
1826
1827 /* Find first active stage in pipeline. */
1828 unsigned idx, prev = 0;
1829 for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1830 if (prog[idx]) {
1831 prev = idx;
1832 break;
1833 }
1834 }
1835
1836 for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1837 if (prog[idx]) {
1838 /* Pipeline might include both non-compute and a compute program, do
1839 * not attempt to validate varyings between non-compute and compute
1840 * stage.
1841 */
1842 if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
1843 break;
1844
1845 if (!validate_io(prog[prev], prog[idx]))
1846 return false;
1847
1848 prev = idx;
1849 }
1850 }
1851 return true;
1852 }