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