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