mesa: fix type for array indexing validation
[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 if (add_index && _mesa_program_resource_array_size(res)) {
723 int i;
724
725 /* The comparison is strange because *length does *NOT* include the
726 * terminating NUL, but maxLength does.
727 */
728 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
729 name[*length + i] = "[0]"[i];
730
731 name[*length + i] = '\0';
732 *length += i;
733 }
734 return true;
735 }
736
737 static GLint
738 program_resource_location(struct gl_shader_program *shProg,
739 struct gl_program_resource *res, const char *name,
740 unsigned array_index)
741 {
742 /* Built-in locations should report GL_INVALID_INDEX. */
743 if (is_gl_identifier(name))
744 return GL_INVALID_INDEX;
745
746 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
747 * offsets are used internally to differentiate between built-in attributes
748 * and user-defined attributes.
749 */
750 switch (res->Type) {
751 case GL_PROGRAM_INPUT:
752 /* If the input is an array, fail if the index is out of bounds. */
753 if (array_index > 0
754 && array_index >= RESOURCE_VAR(res)->type->length) {
755 return -1;
756 }
757 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
758 case GL_PROGRAM_OUTPUT:
759 /* If the output is an array, fail if the index is out of bounds. */
760 if (array_index > 0
761 && array_index >= RESOURCE_VAR(res)->type->length) {
762 return -1;
763 }
764 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
765 case GL_UNIFORM:
766 /* If the uniform is built-in, fail. */
767 if (RESOURCE_UNI(res)->builtin)
768 return -1;
769
770 /* From the GL_ARB_uniform_buffer_object spec:
771 *
772 * "The value -1 will be returned if <name> does not correspond to an
773 * active uniform variable name in <program>, if <name> is associated
774 * with a named uniform block, or if <name> starts with the reserved
775 * prefix "gl_"."
776 */
777 if (RESOURCE_UNI(res)->block_index != -1 ||
778 RESOURCE_UNI(res)->atomic_buffer_index != -1)
779 return -1;
780
781 /* fallthrough */
782 case GL_VERTEX_SUBROUTINE_UNIFORM:
783 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
784 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
785 case GL_COMPUTE_SUBROUTINE_UNIFORM:
786 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
787 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
788 /* If the uniform is an array, fail if the index is out of bounds. */
789 if (array_index > 0
790 && array_index >= RESOURCE_UNI(res)->array_elements) {
791 return -1;
792 }
793
794 /* location in remap table + array element offset */
795 return RESOURCE_UNI(res)->remap_location + array_index;
796 default:
797 return -1;
798 }
799 }
800
801 /**
802 * Function implements following location queries:
803 * glGetUniformLocation
804 */
805 GLint
806 _mesa_program_resource_location(struct gl_shader_program *shProg,
807 GLenum programInterface, const char *name)
808 {
809 unsigned array_index = 0;
810 struct gl_program_resource *res =
811 _mesa_program_resource_find_name(shProg, programInterface, name,
812 &array_index);
813
814 /* Resource not found. */
815 if (!res)
816 return -1;
817
818 return program_resource_location(shProg, res, name, array_index);
819 }
820
821 /**
822 * Function implements following index queries:
823 * glGetFragDataIndex
824 */
825 GLint
826 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
827 GLenum programInterface, const char *name)
828 {
829 struct gl_program_resource *res =
830 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
831
832 /* Non-existent variable or resource is not referenced by fragment stage. */
833 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
834 return -1;
835
836 return RESOURCE_VAR(res)->data.index;
837 }
838
839 static uint8_t
840 stage_from_enum(GLenum ref)
841 {
842 switch (ref) {
843 case GL_REFERENCED_BY_VERTEX_SHADER:
844 return MESA_SHADER_VERTEX;
845 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
846 return MESA_SHADER_TESS_CTRL;
847 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
848 return MESA_SHADER_TESS_EVAL;
849 case GL_REFERENCED_BY_GEOMETRY_SHADER:
850 return MESA_SHADER_GEOMETRY;
851 case GL_REFERENCED_BY_FRAGMENT_SHADER:
852 return MESA_SHADER_FRAGMENT;
853 case GL_REFERENCED_BY_COMPUTE_SHADER:
854 return MESA_SHADER_COMPUTE;
855 default:
856 assert(!"shader stage not supported");
857 return MESA_SHADER_STAGES;
858 }
859 }
860
861 /**
862 * Check if resource is referenced by given 'referenced by' stage enum.
863 * ATC and UBO resources hold stage references of their own.
864 */
865 static bool
866 is_resource_referenced(struct gl_shader_program *shProg,
867 struct gl_program_resource *res,
868 GLuint index, uint8_t stage)
869 {
870 /* First, check if we even have such a stage active. */
871 if (!shProg->_LinkedShaders[stage])
872 return false;
873
874 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
875 return RESOURCE_ATC(res)->StageReferences[stage];
876
877 if (res->Type == GL_UNIFORM_BLOCK)
878 return shProg->UniformBlockStageIndex[stage][index] != -1;
879
880 return res->StageReferences & (1 << stage);
881 }
882
883 static unsigned
884 get_buffer_property(struct gl_shader_program *shProg,
885 struct gl_program_resource *res, const GLenum prop,
886 GLint *val, const char *caller)
887 {
888 GET_CURRENT_CONTEXT(ctx);
889 if (res->Type != GL_UNIFORM_BLOCK &&
890 res->Type != GL_ATOMIC_COUNTER_BUFFER)
891 goto invalid_operation;
892
893 if (res->Type == GL_UNIFORM_BLOCK) {
894 switch (prop) {
895 case GL_BUFFER_BINDING:
896 *val = RESOURCE_UBO(res)->Binding;
897 return 1;
898 case GL_BUFFER_DATA_SIZE:
899 *val = RESOURCE_UBO(res)->UniformBufferSize;
900 return 1;
901 case GL_NUM_ACTIVE_VARIABLES:
902 *val = 0;
903 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
904 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
905 struct gl_program_resource *uni =
906 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
907 NULL);
908 if (!uni)
909 continue;
910 (*val)++;
911 }
912 return 1;
913 case GL_ACTIVE_VARIABLES:
914 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
915 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
916 struct gl_program_resource *uni =
917 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
918 NULL);
919 if (!uni)
920 continue;
921 *val++ =
922 _mesa_program_resource_index(shProg, uni);
923 }
924 return RESOURCE_UBO(res)->NumUniforms;
925 }
926 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
927 switch (prop) {
928 case GL_BUFFER_BINDING:
929 *val = RESOURCE_ATC(res)->Binding;
930 return 1;
931 case GL_BUFFER_DATA_SIZE:
932 *val = RESOURCE_ATC(res)->MinimumSize;
933 return 1;
934 case GL_NUM_ACTIVE_VARIABLES:
935 *val = RESOURCE_ATC(res)->NumUniforms;
936 return 1;
937 case GL_ACTIVE_VARIABLES:
938 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
939 *val++ = RESOURCE_ATC(res)->Uniforms[i];
940 return RESOURCE_ATC(res)->NumUniforms;
941 }
942 }
943 assert(!"support for property type not implemented");
944
945 invalid_operation:
946 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
947 _mesa_enum_to_string(res->Type),
948 _mesa_enum_to_string(prop));
949
950 return 0;
951 }
952
953 unsigned
954 _mesa_program_resource_prop(struct gl_shader_program *shProg,
955 struct gl_program_resource *res, GLuint index,
956 const GLenum prop, GLint *val, const char *caller)
957 {
958 GET_CURRENT_CONTEXT(ctx);
959
960 #define VALIDATE_TYPE(type)\
961 if (res->Type != type)\
962 goto invalid_operation;
963
964 switch(prop) {
965 case GL_NAME_LENGTH:
966 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
967 goto invalid_operation;
968 /* Base name +3 if array '[0]' + terminator. */
969 *val = strlen(_mesa_program_resource_name(res)) +
970 (_mesa_program_resource_array_size(res) > 0 ? 3 : 0) + 1;
971 return 1;
972 case GL_TYPE:
973 switch (res->Type) {
974 case GL_UNIFORM:
975 *val = RESOURCE_UNI(res)->type->gl_type;
976 return 1;
977 case GL_PROGRAM_INPUT:
978 case GL_PROGRAM_OUTPUT:
979 *val = RESOURCE_VAR(res)->type->gl_type;
980 return 1;
981 case GL_TRANSFORM_FEEDBACK_VARYING:
982 *val = RESOURCE_XFB(res)->Type;
983 return 1;
984 default:
985 goto invalid_operation;
986 }
987 case GL_ARRAY_SIZE:
988 switch (res->Type) {
989 case GL_UNIFORM:
990 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
991 return 1;
992 case GL_PROGRAM_INPUT:
993 case GL_PROGRAM_OUTPUT:
994 *val = MAX2(RESOURCE_VAR(res)->type->length, 1);
995 return 1;
996 case GL_TRANSFORM_FEEDBACK_VARYING:
997 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
998 return 1;
999 default:
1000 goto invalid_operation;
1001 }
1002 case GL_OFFSET:
1003 VALIDATE_TYPE(GL_UNIFORM);
1004 *val = RESOURCE_UNI(res)->offset;
1005 return 1;
1006 case GL_BLOCK_INDEX:
1007 VALIDATE_TYPE(GL_UNIFORM);
1008 *val = RESOURCE_UNI(res)->block_index;
1009 return 1;
1010 case GL_ARRAY_STRIDE:
1011 VALIDATE_TYPE(GL_UNIFORM);
1012 *val = RESOURCE_UNI(res)->array_stride;
1013 return 1;
1014 case GL_MATRIX_STRIDE:
1015 VALIDATE_TYPE(GL_UNIFORM);
1016 *val = RESOURCE_UNI(res)->matrix_stride;
1017 return 1;
1018 case GL_IS_ROW_MAJOR:
1019 VALIDATE_TYPE(GL_UNIFORM);
1020 *val = RESOURCE_UNI(res)->row_major;
1021 return 1;
1022 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1023 VALIDATE_TYPE(GL_UNIFORM);
1024 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1025 return 1;
1026 case GL_BUFFER_BINDING:
1027 case GL_BUFFER_DATA_SIZE:
1028 case GL_NUM_ACTIVE_VARIABLES:
1029 case GL_ACTIVE_VARIABLES:
1030 return get_buffer_property(shProg, res, prop, val, caller);
1031 case GL_REFERENCED_BY_COMPUTE_SHADER:
1032 if (!_mesa_has_compute_shaders(ctx))
1033 goto invalid_enum;
1034 /* fallthrough */
1035 case GL_REFERENCED_BY_VERTEX_SHADER:
1036 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1037 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1038 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1039 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1040 switch (res->Type) {
1041 case GL_UNIFORM:
1042 case GL_PROGRAM_INPUT:
1043 case GL_PROGRAM_OUTPUT:
1044 case GL_UNIFORM_BLOCK:
1045 case GL_ATOMIC_COUNTER_BUFFER:
1046 *val = is_resource_referenced(shProg, res, index,
1047 stage_from_enum(prop));
1048 return 1;
1049 default:
1050 goto invalid_operation;
1051 }
1052 case GL_LOCATION:
1053 switch (res->Type) {
1054 case GL_UNIFORM:
1055 case GL_PROGRAM_INPUT:
1056 case GL_PROGRAM_OUTPUT:
1057 *val = program_resource_location(shProg, res,
1058 _mesa_program_resource_name(res),
1059 0);
1060 return 1;
1061 default:
1062 goto invalid_operation;
1063 }
1064 case GL_LOCATION_INDEX:
1065 if (res->Type != GL_PROGRAM_OUTPUT)
1066 goto invalid_operation;
1067 *val = RESOURCE_VAR(res)->data.index;
1068 return 1;
1069
1070 case GL_NUM_COMPATIBLE_SUBROUTINES:
1071 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1072 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1073 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1074 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1075 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1076 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1077 goto invalid_operation;
1078 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1079 return 1;
1080 case GL_COMPATIBLE_SUBROUTINES: {
1081 const struct gl_uniform_storage *uni;
1082 struct gl_shader *sh;
1083 unsigned count, i;
1084 int j;
1085
1086 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1087 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1088 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1089 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1090 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1091 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1092 goto invalid_operation;
1093 uni = RESOURCE_UNI(res);
1094
1095 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1096 count = 0;
1097 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1098 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1099 for (j = 0; j < fn->num_compat_types; j++) {
1100 if (fn->types[j] == uni->type) {
1101 val[count++] = i;
1102 break;
1103 }
1104 }
1105 }
1106 return count;
1107 }
1108 /* GL_ARB_tessellation_shader */
1109 case GL_IS_PER_PATCH:
1110 switch (res->Type) {
1111 case GL_PROGRAM_INPUT:
1112 case GL_PROGRAM_OUTPUT:
1113 *val = RESOURCE_VAR(res)->data.patch;
1114 return 1;
1115 default:
1116 goto invalid_operation;
1117 }
1118 default:
1119 goto invalid_enum;
1120 }
1121
1122 #undef VALIDATE_TYPE
1123
1124 invalid_enum:
1125 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1126 _mesa_enum_to_string(res->Type),
1127 _mesa_enum_to_string(prop));
1128 return 0;
1129
1130 invalid_operation:
1131 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1132 _mesa_enum_to_string(res->Type),
1133 _mesa_enum_to_string(prop));
1134 return 0;
1135 }
1136
1137 extern void
1138 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1139 GLenum programInterface, GLuint index, GLsizei propCount,
1140 const GLenum *props, GLsizei bufSize,
1141 GLsizei *length, GLint *params)
1142 {
1143 GET_CURRENT_CONTEXT(ctx);
1144 GLint *val = (GLint *) params;
1145 const GLenum *prop = props;
1146 GLsizei amount = 0;
1147
1148 struct gl_program_resource *res =
1149 _mesa_program_resource_find_index(shProg, programInterface, index);
1150
1151 /* No such resource found or bufSize negative. */
1152 if (!res || bufSize < 0) {
1153 _mesa_error(ctx, GL_INVALID_VALUE,
1154 "glGetProgramResourceiv(%s index %d bufSize %d)",
1155 _mesa_enum_to_string(programInterface), index, bufSize);
1156 return;
1157 }
1158
1159 /* Write propCount values until error occurs or bufSize reached. */
1160 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1161 int props_written =
1162 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1163 "glGetProgramResourceiv");
1164
1165 /* Error happened. */
1166 if (props_written == 0)
1167 return;
1168
1169 amount += props_written;
1170 }
1171
1172 /* If <length> is not NULL, the actual number of integer values
1173 * written to <params> will be written to <length>.
1174 */
1175 if (length)
1176 *length = amount;
1177 }