Merge remote-tracking branch 'mesa-public/master' into vulkan
[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/core.h"
33 #include "glsl_symbol_table.h"
34 #include "ir.h"
35 #include "shaderobj.h"
36 #include "program/hash_table.h"
37 #include "../glsl/program.h"
38 #include "uniforms.h"
39 #include "main/enums.h"
40
41 extern "C" {
42 #include "shaderapi.h"
43 }
44
45 static GLint
46 program_resource_location(struct gl_shader_program *shProg,
47 struct gl_program_resource *res, const char *name,
48 unsigned array_index);
49
50 /**
51 * Declare convenience functions to return resource data in a given type.
52 * Warning! this is not type safe so be *very* careful when using these.
53 */
54 #define DECL_RESOURCE_FUNC(name, type) \
55 const type * RESOURCE_ ## name (gl_program_resource *res) { \
56 assert(res->Data); \
57 return (type *) res->Data; \
58 }
59
60 DECL_RESOURCE_FUNC(VAR, ir_variable);
61 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
62 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
63 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
64 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_varying_info);
65 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
66
67 void GLAPIENTRY
68 _mesa_BindAttribLocation(GLhandleARB program, GLuint index,
69 const GLcharARB *name)
70 {
71 GET_CURRENT_CONTEXT(ctx);
72
73 struct gl_shader_program *const shProg =
74 _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
75 if (!shProg)
76 return;
77
78 if (!name)
79 return;
80
81 if (strncmp(name, "gl_", 3) == 0) {
82 _mesa_error(ctx, GL_INVALID_OPERATION,
83 "glBindAttribLocation(illegal name)");
84 return;
85 }
86
87 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
88 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
89 return;
90 }
91
92 /* Replace the current value if it's already in the list. Add
93 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
94 * between built-in attributes and user-defined attributes.
95 */
96 shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
97
98 /*
99 * Note that this attribute binding won't go into effect until
100 * glLinkProgram is called again.
101 */
102 }
103
104 static bool
105 is_active_attrib(const ir_variable *var)
106 {
107 if (!var)
108 return false;
109
110 switch (var->data.mode) {
111 case ir_var_shader_in:
112 return var->data.location != -1;
113
114 case ir_var_system_value:
115 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
116 * "For GetActiveAttrib, all active vertex shader input variables
117 * are enumerated, including the special built-in inputs gl_VertexID
118 * and gl_InstanceID."
119 */
120 return var->data.location == SYSTEM_VALUE_VERTEX_ID ||
121 var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE ||
122 var->data.location == SYSTEM_VALUE_INSTANCE_ID;
123
124 default:
125 return false;
126 }
127 }
128
129 void GLAPIENTRY
130 _mesa_GetActiveAttrib(GLhandleARB program, GLuint desired_index,
131 GLsizei maxLength, GLsizei * length, GLint * size,
132 GLenum * type, GLcharARB * name)
133 {
134 GET_CURRENT_CONTEXT(ctx);
135 struct gl_shader_program *shProg;
136
137 if (maxLength < 0) {
138 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
139 return;
140 }
141
142 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
143 if (!shProg)
144 return;
145
146 if (!shProg->LinkStatus) {
147 _mesa_error(ctx, GL_INVALID_VALUE,
148 "glGetActiveAttrib(program not linked)");
149 return;
150 }
151
152 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
153 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
154 return;
155 }
156
157 struct gl_program_resource *res =
158 _mesa_program_resource_find_index(shProg, GL_PROGRAM_INPUT,
159 desired_index);
160
161 /* User asked for index that does not exist. */
162 if (!res) {
163 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
164 return;
165 }
166
167 const ir_variable *const var = RESOURCE_VAR(res);
168
169 if (!is_active_attrib(var))
170 return;
171
172 const char *var_name = var->name;
173
174 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
175 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
176 * active attributes.
177 */
178 if (var->data.mode == ir_var_system_value &&
179 var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
180 var_name = "gl_VertexID";
181 }
182
183 _mesa_copy_string(name, maxLength, length, var_name);
184
185 if (size)
186 _mesa_program_resource_prop(shProg, res, desired_index, GL_ARRAY_SIZE,
187 size, "glGetActiveAttrib");
188
189 if (type)
190 _mesa_program_resource_prop(shProg, res, desired_index, GL_TYPE,
191 (GLint *) type, "glGetActiveAttrib");
192 }
193
194 GLint GLAPIENTRY
195 _mesa_GetAttribLocation(GLhandleARB program, const GLcharARB * name)
196 {
197 GET_CURRENT_CONTEXT(ctx);
198 struct gl_shader_program *const shProg =
199 _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
200
201 if (!shProg) {
202 return -1;
203 }
204
205 if (!shProg->LinkStatus) {
206 _mesa_error(ctx, GL_INVALID_OPERATION,
207 "glGetAttribLocation(program not linked)");
208 return -1;
209 }
210
211 if (!name)
212 return -1;
213
214 /* Not having a vertex shader is not an error.
215 */
216 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
217 return -1;
218
219 unsigned array_index = 0;
220 struct gl_program_resource *res =
221 _mesa_program_resource_find_name(shProg, GL_PROGRAM_INPUT, name,
222 &array_index);
223
224 if (!res)
225 return -1;
226
227 GLint loc = program_resource_location(shProg, res, name, array_index);
228
229 /* The extra check against against 0 is made because of builtin-attribute
230 * locations that have offset applied. Function program_resource_location
231 * can return built-in attribute locations < 0 and glGetAttribLocation
232 * cannot be used on "conventional" attributes.
233 *
234 * From page 95 of the OpenGL 3.0 spec:
235 *
236 * "If name is not an active attribute, if name is a conventional
237 * attribute, or if an error occurs, -1 will be returned."
238 */
239 return (loc >= 0) ? loc : -1;
240 }
241
242 unsigned
243 _mesa_count_active_attribs(struct gl_shader_program *shProg)
244 {
245 if (!shProg->LinkStatus
246 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
247 return 0;
248 }
249
250 struct gl_program_resource *res = shProg->ProgramResourceList;
251 unsigned count = 0;
252 for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
253 if (res->Type == GL_PROGRAM_INPUT &&
254 res->StageReferences & (1 << MESA_SHADER_VERTEX) &&
255 is_active_attrib(RESOURCE_VAR(res)))
256 count++;
257 }
258 return count;
259 }
260
261
262 size_t
263 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
264 {
265 if (!shProg->LinkStatus
266 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
267 return 0;
268 }
269
270 struct gl_program_resource *res = shProg->ProgramResourceList;
271 size_t longest = 0;
272 for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
273 if (res->Type == GL_PROGRAM_INPUT &&
274 res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
275
276 const size_t length = strlen(RESOURCE_VAR(res)->name);
277 if (length >= longest)
278 longest = length + 1;
279 }
280 }
281
282 return longest;
283 }
284
285 void GLAPIENTRY
286 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
287 const GLchar *name)
288 {
289 _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
290 }
291
292 void GLAPIENTRY
293 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
294 GLuint index, const GLchar *name)
295 {
296 GET_CURRENT_CONTEXT(ctx);
297
298 struct gl_shader_program *const shProg =
299 _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
300 if (!shProg)
301 return;
302
303 if (!name)
304 return;
305
306 if (strncmp(name, "gl_", 3) == 0) {
307 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
308 return;
309 }
310
311 if (index > 1) {
312 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
313 return;
314 }
315
316 if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
317 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
318 return;
319 }
320
321 if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
322 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
323 return;
324 }
325
326 /* Replace the current value if it's already in the list. Add
327 * FRAG_RESULT_DATA0 because that's how the linker differentiates
328 * between built-in attributes and user-defined attributes.
329 */
330 shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
331 shProg->FragDataIndexBindings->put(index, name);
332 /*
333 * Note that this binding won't go into effect until
334 * glLinkProgram is called again.
335 */
336
337 }
338
339 GLint GLAPIENTRY
340 _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
341 {
342 GET_CURRENT_CONTEXT(ctx);
343 struct gl_shader_program *const shProg =
344 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
345
346 if (!shProg) {
347 return -1;
348 }
349
350 if (!shProg->LinkStatus) {
351 _mesa_error(ctx, GL_INVALID_OPERATION,
352 "glGetFragDataIndex(program not linked)");
353 return -1;
354 }
355
356 if (!name)
357 return -1;
358
359 if (strncmp(name, "gl_", 3) == 0) {
360 _mesa_error(ctx, GL_INVALID_OPERATION,
361 "glGetFragDataIndex(illegal name)");
362 return -1;
363 }
364
365 /* Not having a fragment shader is not an error.
366 */
367 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
368 return -1;
369
370 return _mesa_program_resource_location_index(shProg, GL_PROGRAM_OUTPUT,
371 name);
372 }
373
374 GLint GLAPIENTRY
375 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
376 {
377 GET_CURRENT_CONTEXT(ctx);
378 struct gl_shader_program *const shProg =
379 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
380
381 if (!shProg) {
382 return -1;
383 }
384
385 if (!shProg->LinkStatus) {
386 _mesa_error(ctx, GL_INVALID_OPERATION,
387 "glGetFragDataLocation(program not linked)");
388 return -1;
389 }
390
391 if (!name)
392 return -1;
393
394 if (strncmp(name, "gl_", 3) == 0) {
395 _mesa_error(ctx, GL_INVALID_OPERATION,
396 "glGetFragDataLocation(illegal name)");
397 return -1;
398 }
399
400 /* Not having a fragment shader is not an error.
401 */
402 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
403 return -1;
404
405 unsigned array_index = 0;
406 struct gl_program_resource *res =
407 _mesa_program_resource_find_name(shProg, GL_PROGRAM_OUTPUT, name,
408 &array_index);
409
410 if (!res)
411 return -1;
412
413 GLint loc = program_resource_location(shProg, res, name, array_index);
414
415 /* The extra check against against 0 is made because of builtin-attribute
416 * locations that have offset applied. Function program_resource_location
417 * can return built-in attribute locations < 0 and glGetFragDataLocation
418 * cannot be used on "conventional" attributes.
419 *
420 * From page 95 of the OpenGL 3.0 spec:
421 *
422 * "If name is not an active attribute, if name is a conventional
423 * attribute, or if an error occurs, -1 will be returned."
424 */
425 return (loc >= 0) ? loc : -1;
426 }
427
428 const char*
429 _mesa_program_resource_name(struct gl_program_resource *res)
430 {
431 const ir_variable *var;
432 switch (res->Type) {
433 case GL_UNIFORM_BLOCK:
434 return RESOURCE_UBO(res)->Name;
435 case GL_TRANSFORM_FEEDBACK_VARYING:
436 return RESOURCE_XFB(res)->Name;
437 case GL_PROGRAM_INPUT:
438 var = RESOURCE_VAR(res);
439 /* Special case gl_VertexIDMESA -> gl_VertexID. */
440 if (var->data.mode == ir_var_system_value &&
441 var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
442 return "gl_VertexID";
443 }
444 /* fallthrough */
445 case GL_PROGRAM_OUTPUT:
446 return RESOURCE_VAR(res)->name;
447 case GL_UNIFORM:
448 return RESOURCE_UNI(res)->name;
449 case GL_VERTEX_SUBROUTINE_UNIFORM:
450 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
451 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
452 case GL_COMPUTE_SUBROUTINE_UNIFORM:
453 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
454 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
455 return RESOURCE_UNI(res)->name + MESA_SUBROUTINE_PREFIX_LEN;
456 case GL_VERTEX_SUBROUTINE:
457 case GL_GEOMETRY_SUBROUTINE:
458 case GL_FRAGMENT_SUBROUTINE:
459 case GL_COMPUTE_SUBROUTINE:
460 case GL_TESS_CONTROL_SUBROUTINE:
461 case GL_TESS_EVALUATION_SUBROUTINE:
462 return RESOURCE_SUB(res)->name;
463 default:
464 assert(!"support for resource type not implemented");
465 }
466 return NULL;
467 }
468
469
470 unsigned
471 _mesa_program_resource_array_size(struct gl_program_resource *res)
472 {
473 switch (res->Type) {
474 case GL_TRANSFORM_FEEDBACK_VARYING:
475 return RESOURCE_XFB(res)->Size > 1 ?
476 RESOURCE_XFB(res)->Size : 0;
477 case GL_PROGRAM_INPUT:
478 case GL_PROGRAM_OUTPUT:
479 return RESOURCE_VAR(res)->data.max_array_access;
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_VERTEX_SUBROUTINE:
489 case GL_GEOMETRY_SUBROUTINE:
490 case GL_FRAGMENT_SUBROUTINE:
491 case GL_COMPUTE_SUBROUTINE:
492 case GL_TESS_CONTROL_SUBROUTINE:
493 case GL_TESS_EVALUATION_SUBROUTINE:
494 case GL_ATOMIC_COUNTER_BUFFER:
495 case GL_UNIFORM_BLOCK:
496 return 0;
497 default:
498 assert(!"support for resource type not implemented");
499 }
500 return 0;
501 }
502
503 /**
504 * Checks if array subscript is valid and if so sets array_index.
505 */
506 static bool
507 valid_array_index(const GLchar *name, unsigned *array_index)
508 {
509 long idx = 0;
510 const GLchar *out_base_name_end;
511
512 idx = parse_program_resource_name(name, &out_base_name_end);
513 if (idx < 0)
514 return false;
515
516 if (array_index)
517 *array_index = idx;
518
519 return true;
520 }
521
522 /* Find a program resource with specific name in given interface.
523 */
524 struct gl_program_resource *
525 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
526 GLenum programInterface, const char *name,
527 unsigned *array_index)
528 {
529 struct gl_program_resource *res = shProg->ProgramResourceList;
530 for (unsigned i = 0; i < shProg->NumProgramResourceList; 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
538 if (strncmp(rname, name, baselen) == 0) {
539 switch (programInterface) {
540 case GL_UNIFORM_BLOCK:
541 /* Basename match, check if array or struct. */
542 if (name[baselen] == '\0' ||
543 name[baselen] == '[' ||
544 name[baselen] == '.') {
545 return res;
546 }
547 break;
548 case GL_TRANSFORM_FEEDBACK_VARYING:
549 case GL_UNIFORM:
550 case GL_VERTEX_SUBROUTINE_UNIFORM:
551 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
552 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
553 case GL_COMPUTE_SUBROUTINE_UNIFORM:
554 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
555 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
556 case GL_VERTEX_SUBROUTINE:
557 case GL_GEOMETRY_SUBROUTINE:
558 case GL_FRAGMENT_SUBROUTINE:
559 case GL_COMPUTE_SUBROUTINE:
560 case GL_TESS_CONTROL_SUBROUTINE:
561 case GL_TESS_EVALUATION_SUBROUTINE:
562 if (name[baselen] == '.') {
563 return res;
564 }
565 /* fall-through */
566 case GL_PROGRAM_INPUT:
567 case GL_PROGRAM_OUTPUT:
568 if (name[baselen] == '\0') {
569 return res;
570 } else if (name[baselen] == '[' &&
571 valid_array_index(name, array_index)) {
572 return res;
573 }
574 break;
575 default:
576 assert(!"not implemented for given interface");
577 }
578 }
579 }
580 return NULL;
581 }
582
583 static GLuint
584 calc_resource_index(struct gl_shader_program *shProg,
585 struct gl_program_resource *res)
586 {
587 unsigned i;
588 GLuint index = 0;
589 for (i = 0; i < shProg->NumProgramResourceList; i++) {
590 if (&shProg->ProgramResourceList[i] == res)
591 return index;
592 if (shProg->ProgramResourceList[i].Type == res->Type)
593 index++;
594 }
595 return GL_INVALID_INDEX;
596 }
597
598 /**
599 * Calculate index for the given resource.
600 */
601 GLuint
602 _mesa_program_resource_index(struct gl_shader_program *shProg,
603 struct gl_program_resource *res)
604 {
605 if (!res)
606 return GL_INVALID_INDEX;
607
608 switch (res->Type) {
609 case GL_UNIFORM_BLOCK:
610 return RESOURCE_UBO(res)- shProg->UniformBlocks;
611 case GL_ATOMIC_COUNTER_BUFFER:
612 return RESOURCE_ATC(res) - shProg->AtomicBuffers;
613 case GL_TRANSFORM_FEEDBACK_VARYING:
614 default:
615 return calc_resource_index(shProg, res);
616 }
617 }
618
619 /* Find a program resource with specific index in given interface.
620 */
621 struct gl_program_resource *
622 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
623 GLenum programInterface, GLuint index)
624 {
625 struct gl_program_resource *res = shProg->ProgramResourceList;
626 int idx = -1;
627
628 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
629 if (res->Type != programInterface)
630 continue;
631
632 switch (res->Type) {
633 case GL_UNIFORM_BLOCK:
634 case GL_ATOMIC_COUNTER_BUFFER:
635 if (_mesa_program_resource_index(shProg, res) == index)
636 return res;
637 break;
638 case GL_TRANSFORM_FEEDBACK_VARYING:
639 case GL_PROGRAM_INPUT:
640 case GL_PROGRAM_OUTPUT:
641 case GL_UNIFORM:
642 case GL_VERTEX_SUBROUTINE_UNIFORM:
643 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
644 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
645 case GL_COMPUTE_SUBROUTINE_UNIFORM:
646 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
647 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
648 case GL_VERTEX_SUBROUTINE:
649 case GL_GEOMETRY_SUBROUTINE:
650 case GL_FRAGMENT_SUBROUTINE:
651 case GL_COMPUTE_SUBROUTINE:
652 case GL_TESS_CONTROL_SUBROUTINE:
653 case GL_TESS_EVALUATION_SUBROUTINE:
654 if (++idx == (int) index)
655 return res;
656 break;
657 default:
658 assert(!"not implemented for given interface");
659 }
660 }
661 return NULL;
662 }
663
664 /* Get full name of a program resource.
665 */
666 bool
667 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
668 GLenum programInterface, GLuint index,
669 GLsizei bufSize, GLsizei *length,
670 GLchar *name, const char *caller)
671 {
672 GET_CURRENT_CONTEXT(ctx);
673
674 /* Find resource with given interface and index. */
675 struct gl_program_resource *res =
676 _mesa_program_resource_find_index(shProg, programInterface, index);
677
678 /* The error INVALID_VALUE is generated if <index> is greater than
679 * or equal to the number of entries in the active resource list for
680 * <programInterface>.
681 */
682 if (!res) {
683 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
684 return false;
685 }
686
687 if (bufSize < 0) {
688 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
689 return false;
690 }
691
692 GLsizei localLength;
693
694 if (length == NULL)
695 length = &localLength;
696
697 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
698
699 /* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
700 * spec says:
701 *
702 * "If the active uniform is an array, the uniform name returned in
703 * name will always be the name of the uniform array appended with
704 * "[0]"."
705 *
706 * The same text also appears in the OpenGL 4.2 spec. It does not,
707 * however, appear in any previous spec. Previous specifications are
708 * ambiguous in this regard. However, either name can later be passed
709 * to glGetUniformLocation (and related APIs), so there shouldn't be any
710 * harm in always appending "[0]" to uniform array names.
711 *
712 * Geometry shader stage has different naming convention where the 'normal'
713 * condition is an array, therefore for variables referenced in geometry
714 * stage we do not add '[0]'.
715 *
716 * Note, that TCS outputs and TES inputs should not have index appended
717 * either.
718 */
719 bool add_index = !(((programInterface == GL_PROGRAM_INPUT) &&
720 res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
721
722 /* Transform feedback varyings have array index already appended
723 * in their names.
724 */
725 if (programInterface == GL_TRANSFORM_FEEDBACK_VARYING)
726 add_index = false;
727
728 if (add_index && _mesa_program_resource_array_size(res)) {
729 int i;
730
731 /* The comparison is strange because *length does *NOT* include the
732 * terminating NUL, but maxLength does.
733 */
734 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
735 name[*length + i] = "[0]"[i];
736
737 name[*length + i] = '\0';
738 *length += i;
739 }
740 return true;
741 }
742
743 static GLint
744 program_resource_location(struct gl_shader_program *shProg,
745 struct gl_program_resource *res, const char *name,
746 unsigned array_index)
747 {
748 /* Built-in locations should report GL_INVALID_INDEX. */
749 if (is_gl_identifier(name))
750 return GL_INVALID_INDEX;
751
752 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
753 * offsets are used internally to differentiate between built-in attributes
754 * and user-defined attributes.
755 */
756 switch (res->Type) {
757 case GL_PROGRAM_INPUT:
758 /* If the input is an array, fail if the index is out of bounds. */
759 if (array_index > 0
760 && array_index >= RESOURCE_VAR(res)->type->length) {
761 return -1;
762 }
763 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
764 case GL_PROGRAM_OUTPUT:
765 /* If the output is an array, fail if the index is out of bounds. */
766 if (array_index > 0
767 && array_index >= RESOURCE_VAR(res)->type->length) {
768 return -1;
769 }
770 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
771 case GL_UNIFORM:
772 /* If the uniform is built-in, fail. */
773 if (RESOURCE_UNI(res)->builtin)
774 return -1;
775
776 /* From the GL_ARB_uniform_buffer_object spec:
777 *
778 * "The value -1 will be returned if <name> does not correspond to an
779 * active uniform variable name in <program>, if <name> is associated
780 * with a named uniform block, or if <name> starts with the reserved
781 * prefix "gl_"."
782 */
783 if (RESOURCE_UNI(res)->block_index != -1 ||
784 RESOURCE_UNI(res)->atomic_buffer_index != -1)
785 return -1;
786
787 /* fallthrough */
788 case GL_VERTEX_SUBROUTINE_UNIFORM:
789 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
790 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
791 case GL_COMPUTE_SUBROUTINE_UNIFORM:
792 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
793 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
794 /* If the uniform is an array, fail if the index is out of bounds. */
795 if (array_index > 0
796 && array_index >= RESOURCE_UNI(res)->array_elements) {
797 return -1;
798 }
799
800 /* location in remap table + array element offset */
801 return RESOURCE_UNI(res)->remap_location + array_index;
802 default:
803 return -1;
804 }
805 }
806
807 /**
808 * Function implements following location queries:
809 * glGetUniformLocation
810 */
811 GLint
812 _mesa_program_resource_location(struct gl_shader_program *shProg,
813 GLenum programInterface, const char *name)
814 {
815 unsigned array_index = 0;
816 struct gl_program_resource *res =
817 _mesa_program_resource_find_name(shProg, programInterface, name,
818 &array_index);
819
820 /* Resource not found. */
821 if (!res)
822 return -1;
823
824 return program_resource_location(shProg, res, name, array_index);
825 }
826
827 /**
828 * Function implements following index queries:
829 * glGetFragDataIndex
830 */
831 GLint
832 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
833 GLenum programInterface, const char *name)
834 {
835 struct gl_program_resource *res =
836 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
837
838 /* Non-existent variable or resource is not referenced by fragment stage. */
839 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
840 return -1;
841
842 return RESOURCE_VAR(res)->data.index;
843 }
844
845 static uint8_t
846 stage_from_enum(GLenum ref)
847 {
848 switch (ref) {
849 case GL_REFERENCED_BY_VERTEX_SHADER:
850 return MESA_SHADER_VERTEX;
851 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
852 return MESA_SHADER_TESS_CTRL;
853 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
854 return MESA_SHADER_TESS_EVAL;
855 case GL_REFERENCED_BY_GEOMETRY_SHADER:
856 return MESA_SHADER_GEOMETRY;
857 case GL_REFERENCED_BY_FRAGMENT_SHADER:
858 return MESA_SHADER_FRAGMENT;
859 case GL_REFERENCED_BY_COMPUTE_SHADER:
860 return MESA_SHADER_COMPUTE;
861 default:
862 assert(!"shader stage not supported");
863 return MESA_SHADER_STAGES;
864 }
865 }
866
867 /**
868 * Check if resource is referenced by given 'referenced by' stage enum.
869 * ATC and UBO resources hold stage references of their own.
870 */
871 static bool
872 is_resource_referenced(struct gl_shader_program *shProg,
873 struct gl_program_resource *res,
874 GLuint index, uint8_t stage)
875 {
876 /* First, check if we even have such a stage active. */
877 if (!shProg->_LinkedShaders[stage])
878 return false;
879
880 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
881 return RESOURCE_ATC(res)->StageReferences[stage];
882
883 if (res->Type == GL_UNIFORM_BLOCK)
884 return shProg->UniformBlockStageIndex[stage][index] != -1;
885
886 return res->StageReferences & (1 << stage);
887 }
888
889 static unsigned
890 get_buffer_property(struct gl_shader_program *shProg,
891 struct gl_program_resource *res, const GLenum prop,
892 GLint *val, const char *caller)
893 {
894 GET_CURRENT_CONTEXT(ctx);
895 if (res->Type != GL_UNIFORM_BLOCK &&
896 res->Type != GL_ATOMIC_COUNTER_BUFFER)
897 goto invalid_operation;
898
899 if (res->Type == GL_UNIFORM_BLOCK) {
900 switch (prop) {
901 case GL_BUFFER_BINDING:
902 *val = RESOURCE_UBO(res)->Binding;
903 return 1;
904 case GL_BUFFER_DATA_SIZE:
905 *val = RESOURCE_UBO(res)->UniformBufferSize;
906 return 1;
907 case GL_NUM_ACTIVE_VARIABLES:
908 *val = 0;
909 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
910 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
911 struct gl_program_resource *uni =
912 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
913 NULL);
914 if (!uni)
915 continue;
916 (*val)++;
917 }
918 return 1;
919 case GL_ACTIVE_VARIABLES:
920 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
921 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
922 struct gl_program_resource *uni =
923 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
924 NULL);
925 if (!uni)
926 continue;
927 *val++ =
928 _mesa_program_resource_index(shProg, uni);
929 }
930 return RESOURCE_UBO(res)->NumUniforms;
931 }
932 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
933 switch (prop) {
934 case GL_BUFFER_BINDING:
935 *val = RESOURCE_ATC(res)->Binding;
936 return 1;
937 case GL_BUFFER_DATA_SIZE:
938 *val = RESOURCE_ATC(res)->MinimumSize;
939 return 1;
940 case GL_NUM_ACTIVE_VARIABLES:
941 *val = RESOURCE_ATC(res)->NumUniforms;
942 return 1;
943 case GL_ACTIVE_VARIABLES:
944 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
945 *val++ = RESOURCE_ATC(res)->Uniforms[i];
946 return RESOURCE_ATC(res)->NumUniforms;
947 }
948 }
949 assert(!"support for property type not implemented");
950
951 invalid_operation:
952 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
953 _mesa_enum_to_string(res->Type),
954 _mesa_enum_to_string(prop));
955
956 return 0;
957 }
958
959 unsigned
960 _mesa_program_resource_prop(struct gl_shader_program *shProg,
961 struct gl_program_resource *res, GLuint index,
962 const GLenum prop, GLint *val, const char *caller)
963 {
964 GET_CURRENT_CONTEXT(ctx);
965
966 #define VALIDATE_TYPE(type)\
967 if (res->Type != type)\
968 goto invalid_operation;
969
970 switch(prop) {
971 case GL_NAME_LENGTH:
972 switch (res->Type) {
973 case GL_ATOMIC_COUNTER_BUFFER:
974 goto invalid_operation;
975 case GL_TRANSFORM_FEEDBACK_VARYING:
976 *val = strlen(_mesa_program_resource_name(res)) + 1;
977 break;
978 default:
979 /* Base name +3 if array '[0]' + terminator. */
980 *val = strlen(_mesa_program_resource_name(res)) +
981 (_mesa_program_resource_array_size(res) > 0 ? 3 : 0) + 1;
982 }
983 return 1;
984 case GL_TYPE:
985 switch (res->Type) {
986 case GL_UNIFORM:
987 *val = RESOURCE_UNI(res)->type->gl_type;
988 return 1;
989 case GL_PROGRAM_INPUT:
990 case GL_PROGRAM_OUTPUT:
991 *val = RESOURCE_VAR(res)->type->gl_type;
992 return 1;
993 case GL_TRANSFORM_FEEDBACK_VARYING:
994 *val = RESOURCE_XFB(res)->Type;
995 return 1;
996 default:
997 goto invalid_operation;
998 }
999 case GL_ARRAY_SIZE:
1000 switch (res->Type) {
1001 case GL_UNIFORM:
1002 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1003 return 1;
1004 case GL_PROGRAM_INPUT:
1005 case GL_PROGRAM_OUTPUT:
1006 *val = MAX2(RESOURCE_VAR(res)->type->length, 1);
1007 return 1;
1008 case GL_TRANSFORM_FEEDBACK_VARYING:
1009 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
1010 return 1;
1011 default:
1012 goto invalid_operation;
1013 }
1014 case GL_OFFSET:
1015 VALIDATE_TYPE(GL_UNIFORM);
1016 *val = RESOURCE_UNI(res)->offset;
1017 return 1;
1018 case GL_BLOCK_INDEX:
1019 VALIDATE_TYPE(GL_UNIFORM);
1020 *val = RESOURCE_UNI(res)->block_index;
1021 return 1;
1022 case GL_ARRAY_STRIDE:
1023 VALIDATE_TYPE(GL_UNIFORM);
1024 *val = RESOURCE_UNI(res)->array_stride;
1025 return 1;
1026 case GL_MATRIX_STRIDE:
1027 VALIDATE_TYPE(GL_UNIFORM);
1028 *val = RESOURCE_UNI(res)->matrix_stride;
1029 return 1;
1030 case GL_IS_ROW_MAJOR:
1031 VALIDATE_TYPE(GL_UNIFORM);
1032 *val = RESOURCE_UNI(res)->row_major;
1033 return 1;
1034 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1035 VALIDATE_TYPE(GL_UNIFORM);
1036 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1037 return 1;
1038 case GL_BUFFER_BINDING:
1039 case GL_BUFFER_DATA_SIZE:
1040 case GL_NUM_ACTIVE_VARIABLES:
1041 case GL_ACTIVE_VARIABLES:
1042 return get_buffer_property(shProg, res, prop, val, caller);
1043 case GL_REFERENCED_BY_COMPUTE_SHADER:
1044 if (!_mesa_has_compute_shaders(ctx))
1045 goto invalid_enum;
1046 /* fallthrough */
1047 case GL_REFERENCED_BY_VERTEX_SHADER:
1048 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1049 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1050 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1051 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1052 switch (res->Type) {
1053 case GL_UNIFORM:
1054 case GL_PROGRAM_INPUT:
1055 case GL_PROGRAM_OUTPUT:
1056 case GL_UNIFORM_BLOCK:
1057 case GL_ATOMIC_COUNTER_BUFFER:
1058 *val = is_resource_referenced(shProg, res, index,
1059 stage_from_enum(prop));
1060 return 1;
1061 default:
1062 goto invalid_operation;
1063 }
1064 case GL_LOCATION:
1065 switch (res->Type) {
1066 case GL_UNIFORM:
1067 case GL_PROGRAM_INPUT:
1068 case GL_PROGRAM_OUTPUT:
1069 *val = program_resource_location(shProg, res,
1070 _mesa_program_resource_name(res),
1071 0);
1072 return 1;
1073 default:
1074 goto invalid_operation;
1075 }
1076 case GL_LOCATION_INDEX:
1077 if (res->Type != GL_PROGRAM_OUTPUT)
1078 goto invalid_operation;
1079 *val = RESOURCE_VAR(res)->data.index;
1080 return 1;
1081
1082 case GL_NUM_COMPATIBLE_SUBROUTINES:
1083 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1084 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1085 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1086 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1087 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1088 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1089 goto invalid_operation;
1090 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1091 return 1;
1092 case GL_COMPATIBLE_SUBROUTINES: {
1093 const struct gl_uniform_storage *uni;
1094 struct gl_shader *sh;
1095 unsigned count, i;
1096 int j;
1097
1098 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1099 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1100 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1101 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1102 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1103 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1104 goto invalid_operation;
1105 uni = RESOURCE_UNI(res);
1106
1107 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1108 count = 0;
1109 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1110 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1111 for (j = 0; j < fn->num_compat_types; j++) {
1112 if (fn->types[j] == uni->type) {
1113 val[count++] = i;
1114 break;
1115 }
1116 }
1117 }
1118 return count;
1119 }
1120 /* GL_ARB_tessellation_shader */
1121 case GL_IS_PER_PATCH:
1122 switch (res->Type) {
1123 case GL_PROGRAM_INPUT:
1124 case GL_PROGRAM_OUTPUT:
1125 *val = RESOURCE_VAR(res)->data.patch;
1126 return 1;
1127 default:
1128 goto invalid_operation;
1129 }
1130 default:
1131 goto invalid_enum;
1132 }
1133
1134 #undef VALIDATE_TYPE
1135
1136 invalid_enum:
1137 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1138 _mesa_enum_to_string(res->Type),
1139 _mesa_enum_to_string(prop));
1140 return 0;
1141
1142 invalid_operation:
1143 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1144 _mesa_enum_to_string(res->Type),
1145 _mesa_enum_to_string(prop));
1146 return 0;
1147 }
1148
1149 extern void
1150 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1151 GLenum programInterface, GLuint index, GLsizei propCount,
1152 const GLenum *props, GLsizei bufSize,
1153 GLsizei *length, GLint *params)
1154 {
1155 GET_CURRENT_CONTEXT(ctx);
1156 GLint *val = (GLint *) params;
1157 const GLenum *prop = props;
1158 GLsizei amount = 0;
1159
1160 struct gl_program_resource *res =
1161 _mesa_program_resource_find_index(shProg, programInterface, index);
1162
1163 /* No such resource found or bufSize negative. */
1164 if (!res || bufSize < 0) {
1165 _mesa_error(ctx, GL_INVALID_VALUE,
1166 "glGetProgramResourceiv(%s index %d bufSize %d)",
1167 _mesa_enum_to_string(programInterface), index, bufSize);
1168 return;
1169 }
1170
1171 /* Write propCount values until error occurs or bufSize reached. */
1172 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1173 int props_written =
1174 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1175 "glGetProgramResourceiv");
1176
1177 /* Error happened. */
1178 if (props_written == 0)
1179 return;
1180
1181 amount += props_written;
1182 }
1183
1184 /* If <length> is not NULL, the actual number of integer values
1185 * written to <params> will be written to <length>.
1186 */
1187 if (length)
1188 *length = amount;
1189 }