main: fix TOP_LEVEL_ARRAY_SIZE and TOP_LEVEL_ARRAY_STRIDE
[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 "main/enums.h"
34 #include "main/shaderapi.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "glsl/glsl_symbol_table.h"
38 #include "glsl/ir.h"
39 #include "glsl/program.h"
40 #include "program/hash_table.h"
41 #include "util/strndup.h"
42
43
44 static GLint
45 program_resource_location(struct gl_shader_program *shProg,
46 struct gl_program_resource *res, const char *name,
47 unsigned array_index);
48
49 /**
50 * Declare convenience functions to return resource data in a given type.
51 * Warning! this is not type safe so be *very* careful when using these.
52 */
53 #define DECL_RESOURCE_FUNC(name, type) \
54 const type * RESOURCE_ ## name (gl_program_resource *res) { \
55 assert(res->Data); \
56 return (type *) res->Data; \
57 }
58
59 DECL_RESOURCE_FUNC(VAR, ir_variable);
60 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
61 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
62 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
63 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_varying_info);
64 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
65
66 void GLAPIENTRY
67 _mesa_BindAttribLocation(GLhandleARB program, GLuint index,
68 const GLcharARB *name)
69 {
70 GET_CURRENT_CONTEXT(ctx);
71
72 struct gl_shader_program *const shProg =
73 _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
74 if (!shProg)
75 return;
76
77 if (!name)
78 return;
79
80 if (strncmp(name, "gl_", 3) == 0) {
81 _mesa_error(ctx, GL_INVALID_OPERATION,
82 "glBindAttribLocation(illegal name)");
83 return;
84 }
85
86 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
87 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
88 return;
89 }
90
91 /* Replace the current value if it's already in the list. Add
92 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
93 * between built-in attributes and user-defined attributes.
94 */
95 shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
96
97 /*
98 * Note that this attribute binding won't go into effect until
99 * glLinkProgram is called again.
100 */
101 }
102
103 static bool
104 is_active_attrib(const ir_variable *var)
105 {
106 if (!var)
107 return false;
108
109 switch (var->data.mode) {
110 case ir_var_shader_in:
111 return var->data.location != -1;
112
113 case ir_var_system_value:
114 /* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
115 * "For GetActiveAttrib, all active vertex shader input variables
116 * are enumerated, including the special built-in inputs gl_VertexID
117 * and gl_InstanceID."
118 */
119 return var->data.location == SYSTEM_VALUE_VERTEX_ID ||
120 var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE ||
121 var->data.location == SYSTEM_VALUE_INSTANCE_ID;
122
123 default:
124 return false;
125 }
126 }
127
128 void GLAPIENTRY
129 _mesa_GetActiveAttrib(GLhandleARB program, GLuint desired_index,
130 GLsizei maxLength, GLsizei * length, GLint * size,
131 GLenum * type, GLcharARB * name)
132 {
133 GET_CURRENT_CONTEXT(ctx);
134 struct gl_shader_program *shProg;
135
136 if (maxLength < 0) {
137 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
138 return;
139 }
140
141 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
142 if (!shProg)
143 return;
144
145 if (!shProg->LinkStatus) {
146 _mesa_error(ctx, GL_INVALID_VALUE,
147 "glGetActiveAttrib(program not linked)");
148 return;
149 }
150
151 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
152 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
153 return;
154 }
155
156 struct gl_program_resource *res =
157 _mesa_program_resource_find_index(shProg, GL_PROGRAM_INPUT,
158 desired_index);
159
160 /* User asked for index that does not exist. */
161 if (!res) {
162 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
163 return;
164 }
165
166 const ir_variable *const var = RESOURCE_VAR(res);
167
168 if (!is_active_attrib(var))
169 return;
170
171 const char *var_name = var->name;
172
173 /* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
174 * consider gl_VertexIDMESA as gl_VertexID for purposes of checking
175 * active attributes.
176 */
177 if (var->data.mode == ir_var_system_value &&
178 var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
179 var_name = "gl_VertexID";
180 }
181
182 _mesa_copy_string(name, maxLength, length, var_name);
183
184 if (size)
185 _mesa_program_resource_prop(shProg, res, desired_index, GL_ARRAY_SIZE,
186 size, "glGetActiveAttrib");
187
188 if (type)
189 _mesa_program_resource_prop(shProg, res, desired_index, GL_TYPE,
190 (GLint *) type, "glGetActiveAttrib");
191 }
192
193 GLint GLAPIENTRY
194 _mesa_GetAttribLocation(GLhandleARB program, const GLcharARB * name)
195 {
196 GET_CURRENT_CONTEXT(ctx);
197 struct gl_shader_program *const shProg =
198 _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
199
200 if (!shProg) {
201 return -1;
202 }
203
204 if (!shProg->LinkStatus) {
205 _mesa_error(ctx, GL_INVALID_OPERATION,
206 "glGetAttribLocation(program not linked)");
207 return -1;
208 }
209
210 if (!name)
211 return -1;
212
213 /* Not having a vertex shader is not an error.
214 */
215 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
216 return -1;
217
218 unsigned array_index = 0;
219 struct gl_program_resource *res =
220 _mesa_program_resource_find_name(shProg, GL_PROGRAM_INPUT, name,
221 &array_index);
222
223 if (!res)
224 return -1;
225
226 GLint loc = program_resource_location(shProg, res, name, array_index);
227
228 /* The extra check against against 0 is made because of builtin-attribute
229 * locations that have offset applied. Function program_resource_location
230 * can return built-in attribute locations < 0 and glGetAttribLocation
231 * cannot be used on "conventional" attributes.
232 *
233 * From page 95 of the OpenGL 3.0 spec:
234 *
235 * "If name is not an active attribute, if name is a conventional
236 * attribute, or if an error occurs, -1 will be returned."
237 */
238 return (loc >= 0) ? loc : -1;
239 }
240
241 unsigned
242 _mesa_count_active_attribs(struct gl_shader_program *shProg)
243 {
244 if (!shProg->LinkStatus
245 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
246 return 0;
247 }
248
249 struct gl_program_resource *res = shProg->ProgramResourceList;
250 unsigned count = 0;
251 for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
252 if (res->Type == GL_PROGRAM_INPUT &&
253 res->StageReferences & (1 << MESA_SHADER_VERTEX) &&
254 is_active_attrib(RESOURCE_VAR(res)))
255 count++;
256 }
257 return count;
258 }
259
260
261 size_t
262 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
263 {
264 if (!shProg->LinkStatus
265 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
266 return 0;
267 }
268
269 struct gl_program_resource *res = shProg->ProgramResourceList;
270 size_t longest = 0;
271 for (unsigned j = 0; j < shProg->NumProgramResourceList; j++, res++) {
272 if (res->Type == GL_PROGRAM_INPUT &&
273 res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
274
275 const size_t length = strlen(RESOURCE_VAR(res)->name);
276 if (length >= longest)
277 longest = length + 1;
278 }
279 }
280
281 return longest;
282 }
283
284 void GLAPIENTRY
285 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
286 const GLchar *name)
287 {
288 _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
289 }
290
291 void GLAPIENTRY
292 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
293 GLuint index, const GLchar *name)
294 {
295 GET_CURRENT_CONTEXT(ctx);
296
297 struct gl_shader_program *const shProg =
298 _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
299 if (!shProg)
300 return;
301
302 if (!name)
303 return;
304
305 if (strncmp(name, "gl_", 3) == 0) {
306 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
307 return;
308 }
309
310 if (index > 1) {
311 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
312 return;
313 }
314
315 if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
316 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
317 return;
318 }
319
320 if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
321 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
322 return;
323 }
324
325 /* Replace the current value if it's already in the list. Add
326 * FRAG_RESULT_DATA0 because that's how the linker differentiates
327 * between built-in attributes and user-defined attributes.
328 */
329 shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
330 shProg->FragDataIndexBindings->put(index, name);
331 /*
332 * Note that this binding won't go into effect until
333 * glLinkProgram is called again.
334 */
335
336 }
337
338 GLint GLAPIENTRY
339 _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
340 {
341 GET_CURRENT_CONTEXT(ctx);
342 struct gl_shader_program *const shProg =
343 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
344
345 if (!shProg) {
346 return -1;
347 }
348
349 if (!shProg->LinkStatus) {
350 _mesa_error(ctx, GL_INVALID_OPERATION,
351 "glGetFragDataIndex(program not linked)");
352 return -1;
353 }
354
355 if (!name)
356 return -1;
357
358 if (strncmp(name, "gl_", 3) == 0) {
359 _mesa_error(ctx, GL_INVALID_OPERATION,
360 "glGetFragDataIndex(illegal name)");
361 return -1;
362 }
363
364 /* Not having a fragment shader is not an error.
365 */
366 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
367 return -1;
368
369 return _mesa_program_resource_location_index(shProg, GL_PROGRAM_OUTPUT,
370 name);
371 }
372
373 GLint GLAPIENTRY
374 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
375 {
376 GET_CURRENT_CONTEXT(ctx);
377 struct gl_shader_program *const shProg =
378 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
379
380 if (!shProg) {
381 return -1;
382 }
383
384 if (!shProg->LinkStatus) {
385 _mesa_error(ctx, GL_INVALID_OPERATION,
386 "glGetFragDataLocation(program not linked)");
387 return -1;
388 }
389
390 if (!name)
391 return -1;
392
393 if (strncmp(name, "gl_", 3) == 0) {
394 _mesa_error(ctx, GL_INVALID_OPERATION,
395 "glGetFragDataLocation(illegal name)");
396 return -1;
397 }
398
399 /* Not having a fragment shader is not an error.
400 */
401 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
402 return -1;
403
404 unsigned array_index = 0;
405 struct gl_program_resource *res =
406 _mesa_program_resource_find_name(shProg, GL_PROGRAM_OUTPUT, name,
407 &array_index);
408
409 if (!res)
410 return -1;
411
412 GLint loc = program_resource_location(shProg, res, name, array_index);
413
414 /* The extra check against against 0 is made because of builtin-attribute
415 * locations that have offset applied. Function program_resource_location
416 * can return built-in attribute locations < 0 and glGetFragDataLocation
417 * cannot be used on "conventional" attributes.
418 *
419 * From page 95 of the OpenGL 3.0 spec:
420 *
421 * "If name is not an active attribute, if name is a conventional
422 * attribute, or if an error occurs, -1 will be returned."
423 */
424 return (loc >= 0) ? loc : -1;
425 }
426
427 const char*
428 _mesa_program_resource_name(struct gl_program_resource *res)
429 {
430 const ir_variable *var;
431 switch (res->Type) {
432 case GL_UNIFORM_BLOCK:
433 case GL_SHADER_STORAGE_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 case GL_BUFFER_VARIABLE:
449 return RESOURCE_UNI(res)->name;
450 case GL_VERTEX_SUBROUTINE_UNIFORM:
451 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
452 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
453 case GL_COMPUTE_SUBROUTINE_UNIFORM:
454 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
455 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
456 return RESOURCE_UNI(res)->name + MESA_SUBROUTINE_PREFIX_LEN;
457 case GL_VERTEX_SUBROUTINE:
458 case GL_GEOMETRY_SUBROUTINE:
459 case GL_FRAGMENT_SUBROUTINE:
460 case GL_COMPUTE_SUBROUTINE:
461 case GL_TESS_CONTROL_SUBROUTINE:
462 case GL_TESS_EVALUATION_SUBROUTINE:
463 return RESOURCE_SUB(res)->name;
464 default:
465 assert(!"support for resource type not implemented");
466 }
467 return NULL;
468 }
469
470
471 unsigned
472 _mesa_program_resource_array_size(struct gl_program_resource *res)
473 {
474 switch (res->Type) {
475 case GL_TRANSFORM_FEEDBACK_VARYING:
476 return RESOURCE_XFB(res)->Size > 1 ?
477 RESOURCE_XFB(res)->Size : 0;
478 case GL_PROGRAM_INPUT:
479 case GL_PROGRAM_OUTPUT:
480 return RESOURCE_VAR(res)->type->length;
481 case GL_UNIFORM:
482 case GL_VERTEX_SUBROUTINE_UNIFORM:
483 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
484 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
485 case GL_COMPUTE_SUBROUTINE_UNIFORM:
486 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
487 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
488 case GL_BUFFER_VARIABLE:
489 return RESOURCE_UNI(res)->array_elements;
490 case GL_VERTEX_SUBROUTINE:
491 case GL_GEOMETRY_SUBROUTINE:
492 case GL_FRAGMENT_SUBROUTINE:
493 case GL_COMPUTE_SUBROUTINE:
494 case GL_TESS_CONTROL_SUBROUTINE:
495 case GL_TESS_EVALUATION_SUBROUTINE:
496 case GL_ATOMIC_COUNTER_BUFFER:
497 case GL_UNIFORM_BLOCK:
498 case GL_SHADER_STORAGE_BLOCK:
499 return 0;
500 default:
501 assert(!"support for resource type not implemented");
502 }
503 return 0;
504 }
505
506 /**
507 * Checks if array subscript is valid and if so sets array_index.
508 */
509 static bool
510 valid_array_index(const GLchar *name, unsigned *array_index)
511 {
512 long idx = 0;
513 const GLchar *out_base_name_end;
514
515 idx = parse_program_resource_name(name, &out_base_name_end);
516 if (idx < 0)
517 return false;
518
519 if (array_index)
520 *array_index = idx;
521
522 return true;
523 }
524
525 /* Find a program resource with specific name in given interface.
526 */
527 struct gl_program_resource *
528 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
529 GLenum programInterface, const char *name,
530 unsigned *array_index)
531 {
532 struct gl_program_resource *res = shProg->ProgramResourceList;
533 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
534 if (res->Type != programInterface)
535 continue;
536
537 /* Resource basename. */
538 const char *rname = _mesa_program_resource_name(res);
539 unsigned baselen = strlen(rname);
540
541 if (strncmp(rname, name, baselen) == 0) {
542 switch (programInterface) {
543 case GL_UNIFORM_BLOCK:
544 case GL_SHADER_STORAGE_BLOCK:
545 /* Basename match, check if array or struct. */
546 if (name[baselen] == '\0' ||
547 name[baselen] == '[' ||
548 name[baselen] == '.') {
549 return res;
550 }
551 break;
552 case GL_TRANSFORM_FEEDBACK_VARYING:
553 case GL_BUFFER_VARIABLE:
554 case GL_UNIFORM:
555 case GL_VERTEX_SUBROUTINE_UNIFORM:
556 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
557 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
558 case GL_COMPUTE_SUBROUTINE_UNIFORM:
559 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
560 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
561 case GL_VERTEX_SUBROUTINE:
562 case GL_GEOMETRY_SUBROUTINE:
563 case GL_FRAGMENT_SUBROUTINE:
564 case GL_COMPUTE_SUBROUTINE:
565 case GL_TESS_CONTROL_SUBROUTINE:
566 case GL_TESS_EVALUATION_SUBROUTINE:
567 if (name[baselen] == '.') {
568 return res;
569 }
570 /* fall-through */
571 case GL_PROGRAM_INPUT:
572 case GL_PROGRAM_OUTPUT:
573 if (name[baselen] == '\0') {
574 return res;
575 } else if (name[baselen] == '[' &&
576 valid_array_index(name, array_index)) {
577 return res;
578 }
579 break;
580 default:
581 assert(!"not implemented for given interface");
582 }
583 }
584 }
585 return NULL;
586 }
587
588 static GLuint
589 calc_resource_index(struct gl_shader_program *shProg,
590 struct gl_program_resource *res)
591 {
592 unsigned i;
593 GLuint index = 0;
594 for (i = 0; i < shProg->NumProgramResourceList; i++) {
595 if (&shProg->ProgramResourceList[i] == res)
596 return index;
597 if (shProg->ProgramResourceList[i].Type == res->Type)
598 index++;
599 }
600 return GL_INVALID_INDEX;
601 }
602
603 /**
604 * Calculate index for the given resource.
605 */
606 GLuint
607 _mesa_program_resource_index(struct gl_shader_program *shProg,
608 struct gl_program_resource *res)
609 {
610 if (!res)
611 return GL_INVALID_INDEX;
612
613 switch (res->Type) {
614 case GL_ATOMIC_COUNTER_BUFFER:
615 return RESOURCE_ATC(res) - shProg->AtomicBuffers;
616 case GL_UNIFORM_BLOCK:
617 case GL_SHADER_STORAGE_BLOCK:
618 case GL_TRANSFORM_FEEDBACK_VARYING:
619 default:
620 return calc_resource_index(shProg, res);
621 }
622 }
623
624 /* Find a program resource with specific index in given interface.
625 */
626 struct gl_program_resource *
627 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
628 GLenum programInterface, GLuint index)
629 {
630 struct gl_program_resource *res = shProg->ProgramResourceList;
631 int idx = -1;
632
633 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
634 if (res->Type != programInterface)
635 continue;
636
637 switch (res->Type) {
638 case GL_UNIFORM_BLOCK:
639 case GL_ATOMIC_COUNTER_BUFFER:
640 case GL_SHADER_STORAGE_BLOCK:
641 if (_mesa_program_resource_index(shProg, res) == index)
642 return res;
643 break;
644 case GL_TRANSFORM_FEEDBACK_VARYING:
645 case GL_PROGRAM_INPUT:
646 case GL_PROGRAM_OUTPUT:
647 case GL_UNIFORM:
648 case GL_VERTEX_SUBROUTINE_UNIFORM:
649 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
650 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
651 case GL_COMPUTE_SUBROUTINE_UNIFORM:
652 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
653 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
654 case GL_VERTEX_SUBROUTINE:
655 case GL_GEOMETRY_SUBROUTINE:
656 case GL_FRAGMENT_SUBROUTINE:
657 case GL_COMPUTE_SUBROUTINE:
658 case GL_TESS_CONTROL_SUBROUTINE:
659 case GL_TESS_EVALUATION_SUBROUTINE:
660 case GL_BUFFER_VARIABLE:
661 if (++idx == (int) index)
662 return res;
663 break;
664 default:
665 assert(!"not implemented for given interface");
666 }
667 }
668 return NULL;
669 }
670
671 /* Function returns if resource name is expected to have index
672 * appended into it.
673 *
674 *
675 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
676 * spec says:
677 *
678 * "If the active uniform is an array, the uniform name returned in
679 * name will always be the name of the uniform array appended with
680 * "[0]"."
681 *
682 * The same text also appears in the OpenGL 4.2 spec. It does not,
683 * however, appear in any previous spec. Previous specifications are
684 * ambiguous in this regard. However, either name can later be passed
685 * to glGetUniformLocation (and related APIs), so there shouldn't be any
686 * harm in always appending "[0]" to uniform array names.
687 *
688 * Geometry shader stage has different naming convention where the 'normal'
689 * condition is an array, therefore for variables referenced in geometry
690 * stage we do not add '[0]'.
691 *
692 * Note, that TCS outputs and TES inputs should not have index appended
693 * either.
694 */
695 static bool
696 add_index_to_name(struct gl_program_resource *res)
697 {
698 bool add_index = !(((res->Type == GL_PROGRAM_INPUT) &&
699 res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
700
701 /* Transform feedback varyings have array index already appended
702 * in their names.
703 */
704 if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
705 add_index = false;
706
707 return add_index;
708 }
709
710 /* Get name length of a program resource. This consists of
711 * base name + 3 for '[0]' if resource is an array.
712 */
713 extern unsigned
714 _mesa_program_resource_name_len(struct gl_program_resource *res)
715 {
716 unsigned length = strlen(_mesa_program_resource_name(res));
717 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
718 length += 3;
719 return length;
720 }
721
722 /* Get full name of a program resource.
723 */
724 bool
725 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
726 GLenum programInterface, GLuint index,
727 GLsizei bufSize, GLsizei *length,
728 GLchar *name, const char *caller)
729 {
730 GET_CURRENT_CONTEXT(ctx);
731
732 /* Find resource with given interface and index. */
733 struct gl_program_resource *res =
734 _mesa_program_resource_find_index(shProg, programInterface, index);
735
736 /* The error INVALID_VALUE is generated if <index> is greater than
737 * or equal to the number of entries in the active resource list for
738 * <programInterface>.
739 */
740 if (!res) {
741 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
742 return false;
743 }
744
745 if (bufSize < 0) {
746 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
747 return false;
748 }
749
750 GLsizei localLength;
751
752 if (length == NULL)
753 length = &localLength;
754
755 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
756
757 if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
758 int i;
759
760 /* The comparison is strange because *length does *NOT* include the
761 * terminating NUL, but maxLength does.
762 */
763 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
764 name[*length + i] = "[0]"[i];
765
766 name[*length + i] = '\0';
767 *length += i;
768 }
769 return true;
770 }
771
772 static GLint
773 program_resource_location(struct gl_shader_program *shProg,
774 struct gl_program_resource *res, const char *name,
775 unsigned array_index)
776 {
777 /* Built-in locations should report GL_INVALID_INDEX. */
778 if (is_gl_identifier(name))
779 return GL_INVALID_INDEX;
780
781 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
782 * offsets are used internally to differentiate between built-in attributes
783 * and user-defined attributes.
784 */
785 switch (res->Type) {
786 case GL_PROGRAM_INPUT:
787 /* If the input is an array, fail if the index is out of bounds. */
788 if (array_index > 0
789 && array_index >= RESOURCE_VAR(res)->type->length) {
790 return -1;
791 }
792 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
793 case GL_PROGRAM_OUTPUT:
794 /* If the output is an array, fail if the index is out of bounds. */
795 if (array_index > 0
796 && array_index >= RESOURCE_VAR(res)->type->length) {
797 return -1;
798 }
799 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
800 case GL_UNIFORM:
801 /* If the uniform is built-in, fail. */
802 if (RESOURCE_UNI(res)->builtin)
803 return -1;
804
805 /* From the GL_ARB_uniform_buffer_object spec:
806 *
807 * "The value -1 will be returned if <name> does not correspond to an
808 * active uniform variable name in <program>, if <name> is associated
809 * with a named uniform block, or if <name> starts with the reserved
810 * prefix "gl_"."
811 */
812 if (RESOURCE_UNI(res)->block_index != -1 ||
813 RESOURCE_UNI(res)->atomic_buffer_index != -1)
814 return -1;
815
816 /* fallthrough */
817 case GL_VERTEX_SUBROUTINE_UNIFORM:
818 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
819 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
820 case GL_COMPUTE_SUBROUTINE_UNIFORM:
821 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
822 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
823 /* If the uniform is an array, fail if the index is out of bounds. */
824 if (array_index > 0
825 && array_index >= RESOURCE_UNI(res)->array_elements) {
826 return -1;
827 }
828
829 /* location in remap table + array element offset */
830 return RESOURCE_UNI(res)->remap_location + array_index;
831 default:
832 return -1;
833 }
834 }
835
836 static char*
837 get_top_level_name(const char *name)
838 {
839 const char *first_dot = strchr(name, '.');
840 const char *first_square_bracket = strchr(name, '[');
841 int name_size = 0;
842 /* From ARB_program_interface_query spec:
843 *
844 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying the
845 * number of active array elements of the top-level shader storage block
846 * member containing to the active variable is written to <params>. If the
847 * top-level block member is not declared as an array, the value one is
848 * written to <params>. If the top-level block member is an array with no
849 * declared size, the value zero is written to <params>.
850 */
851
852 /* The buffer variable is on top level.*/
853 if (!first_square_bracket && !first_dot)
854 name_size = strlen(name);
855 else if ((!first_square_bracket ||
856 (first_dot && first_dot < first_square_bracket)))
857 name_size = first_dot - name;
858 else
859 name_size = first_square_bracket - name;
860
861 return strndup(name, name_size);
862 }
863
864 static char*
865 get_var_name(const char *name)
866 {
867 const char *first_dot = strchr(name, '.');
868
869 if (!first_dot)
870 return strdup(name);
871
872 return strndup(first_dot+1, strlen(first_dot) - 1);
873 }
874
875 static bool
876 is_top_level_shader_storage_block_member(const char* name,
877 const char* interface_name,
878 const char* field_name)
879 {
880 bool result = false;
881
882 /* If the given variable is already a top-level shader storage
883 * block member, then return array_size = 1.
884 * We could have two possibilities: if we have an instanced
885 * shader storage block or not instanced.
886 *
887 * For the first, we check create a name as it was in top level and
888 * compare it with the real name. If they are the same, then
889 * the variable is already at top-level.
890 *
891 * Full instanced name is: interface name + '.' + var name +
892 * NULL character
893 */
894 int name_length = strlen(interface_name) + 1 + strlen(field_name) + 1;
895 char *full_instanced_name = (char *) calloc(name_length, sizeof(char));
896 if (!full_instanced_name) {
897 fprintf(stderr, "%s: Cannot allocate space for name\n", __func__);
898 return false;
899 }
900
901 snprintf(full_instanced_name, name_length, "%s.%s",
902 interface_name, field_name);
903
904 /* Check if its top-level shader storage block member of an
905 * instanced interface block, or of a unnamed interface block.
906 */
907 if (strcmp(name, full_instanced_name) == 0 ||
908 strcmp(name, field_name) == 0)
909 result = true;
910
911 free(full_instanced_name);
912 return result;
913 }
914
915 static GLint
916 program_resource_top_level_array_size(struct gl_shader_program *shProg,
917 struct gl_program_resource *res,
918 const char *name)
919 {
920 int block_index = RESOURCE_UNI(res)->block_index;
921 int array_size = -1;
922 char *var_name = get_top_level_name(name);
923 char *interface_name =
924 get_top_level_name(shProg->UniformBlocks[block_index].Name);
925
926 if (strcmp(var_name, interface_name) == 0) {
927 /* Deal with instanced array of SSBOs */
928 char *temp_name = get_var_name(name);
929 free(var_name);
930 var_name = get_top_level_name(temp_name);
931 free(temp_name);
932 }
933
934 for (unsigned i = 0; i < shProg->NumShaders; i++) {
935 if (shProg->Shaders[i] == NULL)
936 continue;
937
938 const gl_shader *stage = shProg->Shaders[i];
939 foreach_in_list(ir_instruction, node, stage->ir) {
940 ir_variable *var = node->as_variable();
941 if (!var || !var->get_interface_type() ||
942 var->data.mode != ir_var_shader_storage)
943 continue;
944
945 const glsl_type *interface = var->get_interface_type();
946
947 if (strcmp(interface_name, interface->name) != 0)
948 continue;
949
950 for (unsigned i = 0; i < interface->length; i++) {
951 const glsl_struct_field *field = &interface->fields.structure[i];
952 if (strcmp(field->name, var_name) != 0)
953 continue;
954 /* From GL_ARB_program_interface_query spec:
955 *
956 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer
957 * identifying the number of active array elements of the top-level
958 * shader storage block member containing to the active variable is
959 * written to <params>. If the top-level block member is not
960 * declared as an array, the value one is written to <params>. If
961 * the top-level block member is an array with no declared size,
962 * the value zero is written to <params>.
963 */
964 if (is_top_level_shader_storage_block_member(name,
965 interface_name,
966 var_name))
967 array_size = 1;
968 else if (field->type->is_unsized_array())
969 array_size = 0;
970 else if (field->type->is_array())
971 array_size = field->type->length;
972 else
973 array_size = 1;
974
975 goto found_top_level_array_size;
976 }
977 }
978 }
979 found_top_level_array_size:
980 free(interface_name);
981 free(var_name);
982 return array_size;
983 }
984
985 static GLint
986 program_resource_top_level_array_stride(struct gl_shader_program *shProg,
987 struct gl_program_resource *res,
988 const char *name)
989 {
990 int block_index = RESOURCE_UNI(res)->block_index;
991 int array_stride = -1;
992 char *var_name = get_top_level_name(name);
993 char *interface_name =
994 get_top_level_name(shProg->UniformBlocks[block_index].Name);
995
996 if (strcmp(var_name, interface_name) == 0) {
997 /* Deal with instanced array of SSBOs */
998 char *temp_name = get_var_name(name);
999 free(var_name);
1000 var_name = get_top_level_name(temp_name);
1001 free(temp_name);
1002 }
1003
1004 for (unsigned i = 0; i < shProg->NumShaders; i++) {
1005 if (shProg->Shaders[i] == NULL)
1006 continue;
1007
1008 const gl_shader *stage = shProg->Shaders[i];
1009 foreach_in_list(ir_instruction, node, stage->ir) {
1010 ir_variable *var = node->as_variable();
1011 if (!var || !var->get_interface_type() ||
1012 var->data.mode != ir_var_shader_storage)
1013 continue;
1014
1015 const glsl_type *interface = var->get_interface_type();
1016
1017 if (strcmp(interface_name, interface->name) != 0) {
1018 continue;
1019 }
1020
1021 for (unsigned i = 0; i < interface->length; i++) {
1022 const glsl_struct_field *field = &interface->fields.structure[i];
1023 if (strcmp(field->name, var_name) != 0)
1024 continue;
1025 /* From GL_ARB_program_interface_query:
1026 *
1027 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
1028 * identifying the stride between array elements of the top-level
1029 * shader storage block member containing the active variable is
1030 * written to <params>. For top-level block members declared as
1031 * arrays, the value written is the difference, in basic machine
1032 * units, between the offsets of the active variable for
1033 * consecutive elements in the top-level array. For top-level
1034 * block members not declared as an array, zero is written to
1035 * <params>."
1036 */
1037 if (field->type->is_array()) {
1038 const enum glsl_matrix_layout matrix_layout =
1039 glsl_matrix_layout(field->matrix_layout);
1040 bool row_major = matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
1041 const glsl_type *array_type = field->type->fields.array;
1042
1043 if (is_top_level_shader_storage_block_member(name,
1044 interface_name,
1045 var_name)) {
1046 array_stride = 0;
1047 goto found_top_level_array_stride;
1048 }
1049 if (interface->interface_packing != GLSL_INTERFACE_PACKING_STD430) {
1050 if (array_type->is_record() || array_type->is_array()) {
1051 array_stride = array_type->std140_size(row_major);
1052 array_stride = glsl_align(array_stride, 16);
1053 } else {
1054 unsigned element_base_align = 0;
1055 element_base_align = array_type->std140_base_alignment(row_major);
1056 array_stride = MAX2(element_base_align, 16);
1057 }
1058 } else {
1059 array_stride = array_type->std430_array_stride(row_major);
1060 }
1061 } else {
1062 array_stride = 0;
1063 }
1064 goto found_top_level_array_stride;
1065 }
1066 }
1067 }
1068 found_top_level_array_stride:
1069 free(interface_name);
1070 free(var_name);
1071 return array_stride;
1072 }
1073
1074 /**
1075 * Function implements following location queries:
1076 * glGetUniformLocation
1077 */
1078 GLint
1079 _mesa_program_resource_location(struct gl_shader_program *shProg,
1080 GLenum programInterface, const char *name)
1081 {
1082 unsigned array_index = 0;
1083 struct gl_program_resource *res =
1084 _mesa_program_resource_find_name(shProg, programInterface, name,
1085 &array_index);
1086
1087 /* Resource not found. */
1088 if (!res)
1089 return -1;
1090
1091 return program_resource_location(shProg, res, name, array_index);
1092 }
1093
1094 /**
1095 * Function implements following index queries:
1096 * glGetFragDataIndex
1097 */
1098 GLint
1099 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1100 GLenum programInterface, const char *name)
1101 {
1102 struct gl_program_resource *res =
1103 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1104
1105 /* Non-existent variable or resource is not referenced by fragment stage. */
1106 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1107 return -1;
1108
1109 return RESOURCE_VAR(res)->data.index;
1110 }
1111
1112 static uint8_t
1113 stage_from_enum(GLenum ref)
1114 {
1115 switch (ref) {
1116 case GL_REFERENCED_BY_VERTEX_SHADER:
1117 return MESA_SHADER_VERTEX;
1118 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1119 return MESA_SHADER_TESS_CTRL;
1120 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1121 return MESA_SHADER_TESS_EVAL;
1122 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1123 return MESA_SHADER_GEOMETRY;
1124 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1125 return MESA_SHADER_FRAGMENT;
1126 case GL_REFERENCED_BY_COMPUTE_SHADER:
1127 return MESA_SHADER_COMPUTE;
1128 default:
1129 assert(!"shader stage not supported");
1130 return MESA_SHADER_STAGES;
1131 }
1132 }
1133
1134 /**
1135 * Check if resource is referenced by given 'referenced by' stage enum.
1136 * ATC and UBO resources hold stage references of their own.
1137 */
1138 static bool
1139 is_resource_referenced(struct gl_shader_program *shProg,
1140 struct gl_program_resource *res,
1141 GLuint index, uint8_t stage)
1142 {
1143 /* First, check if we even have such a stage active. */
1144 if (!shProg->_LinkedShaders[stage])
1145 return false;
1146
1147 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1148 return RESOURCE_ATC(res)->StageReferences[stage];
1149
1150 if (res->Type == GL_UNIFORM_BLOCK || res->Type == GL_SHADER_STORAGE_BLOCK)
1151 return shProg->UniformBlockStageIndex[stage][index] != -1;
1152
1153 return res->StageReferences & (1 << stage);
1154 }
1155
1156 static unsigned
1157 get_buffer_property(struct gl_shader_program *shProg,
1158 struct gl_program_resource *res, const GLenum prop,
1159 GLint *val, const char *caller)
1160 {
1161 GET_CURRENT_CONTEXT(ctx);
1162 if (res->Type != GL_UNIFORM_BLOCK &&
1163 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1164 res->Type != GL_SHADER_STORAGE_BLOCK)
1165 goto invalid_operation;
1166
1167 if (res->Type == GL_UNIFORM_BLOCK) {
1168 switch (prop) {
1169 case GL_BUFFER_BINDING:
1170 *val = RESOURCE_UBO(res)->Binding;
1171 return 1;
1172 case GL_BUFFER_DATA_SIZE:
1173 *val = RESOURCE_UBO(res)->UniformBufferSize;
1174 return 1;
1175 case GL_NUM_ACTIVE_VARIABLES:
1176 *val = 0;
1177 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1178 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1179 struct gl_program_resource *uni =
1180 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1181 NULL);
1182 if (!uni)
1183 continue;
1184 (*val)++;
1185 }
1186 return 1;
1187 case GL_ACTIVE_VARIABLES:
1188 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1189 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1190 struct gl_program_resource *uni =
1191 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1192 NULL);
1193 if (!uni)
1194 continue;
1195 *val++ =
1196 _mesa_program_resource_index(shProg, uni);
1197 }
1198 return RESOURCE_UBO(res)->NumUniforms;
1199 }
1200 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1201 switch (prop) {
1202 case GL_BUFFER_BINDING:
1203 *val = RESOURCE_UBO(res)->Binding;
1204 return 1;
1205 case GL_BUFFER_DATA_SIZE:
1206 *val = RESOURCE_UBO(res)->UniformBufferSize;
1207 return 1;
1208 case GL_NUM_ACTIVE_VARIABLES:
1209 *val = 0;
1210 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1211 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1212 struct gl_program_resource *uni =
1213 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1214 iname, NULL);
1215 if (!uni)
1216 continue;
1217 (*val)++;
1218 }
1219 return 1;
1220 case GL_ACTIVE_VARIABLES:
1221 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1222 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1223 struct gl_program_resource *uni =
1224 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1225 iname, NULL);
1226 if (!uni)
1227 continue;
1228 *val++ =
1229 _mesa_program_resource_index(shProg, uni);
1230 }
1231 return RESOURCE_UBO(res)->NumUniforms;
1232 }
1233 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1234 switch (prop) {
1235 case GL_BUFFER_BINDING:
1236 *val = RESOURCE_ATC(res)->Binding;
1237 return 1;
1238 case GL_BUFFER_DATA_SIZE:
1239 *val = RESOURCE_ATC(res)->MinimumSize;
1240 return 1;
1241 case GL_NUM_ACTIVE_VARIABLES:
1242 *val = RESOURCE_ATC(res)->NumUniforms;
1243 return 1;
1244 case GL_ACTIVE_VARIABLES:
1245 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
1246 *val++ = RESOURCE_ATC(res)->Uniforms[i];
1247 return RESOURCE_ATC(res)->NumUniforms;
1248 }
1249 }
1250 assert(!"support for property type not implemented");
1251
1252 invalid_operation:
1253 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1254 _mesa_enum_to_string(res->Type),
1255 _mesa_enum_to_string(prop));
1256
1257 return 0;
1258 }
1259
1260 unsigned
1261 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1262 struct gl_program_resource *res, GLuint index,
1263 const GLenum prop, GLint *val, const char *caller)
1264 {
1265 GET_CURRENT_CONTEXT(ctx);
1266
1267 #define VALIDATE_TYPE(type)\
1268 if (res->Type != type)\
1269 goto invalid_operation;
1270
1271 #define VALIDATE_TYPE_2(type1, type2)\
1272 if (res->Type != type1 && res->Type != type2)\
1273 goto invalid_operation;
1274
1275 switch(prop) {
1276 case GL_NAME_LENGTH:
1277 switch (res->Type) {
1278 case GL_ATOMIC_COUNTER_BUFFER:
1279 goto invalid_operation;
1280 default:
1281 /* Resource name length + terminator. */
1282 *val = _mesa_program_resource_name_len(res) + 1;
1283 }
1284 return 1;
1285 case GL_TYPE:
1286 switch (res->Type) {
1287 case GL_UNIFORM:
1288 case GL_BUFFER_VARIABLE:
1289 *val = RESOURCE_UNI(res)->type->gl_type;
1290 return 1;
1291 case GL_PROGRAM_INPUT:
1292 case GL_PROGRAM_OUTPUT:
1293 *val = RESOURCE_VAR(res)->type->gl_type;
1294 return 1;
1295 case GL_TRANSFORM_FEEDBACK_VARYING:
1296 *val = RESOURCE_XFB(res)->Type;
1297 return 1;
1298 default:
1299 goto invalid_operation;
1300 }
1301 case GL_ARRAY_SIZE:
1302 switch (res->Type) {
1303 case GL_UNIFORM:
1304 case GL_BUFFER_VARIABLE:
1305 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1306 return 1;
1307 case GL_PROGRAM_INPUT:
1308 case GL_PROGRAM_OUTPUT:
1309 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1310 return 1;
1311 case GL_TRANSFORM_FEEDBACK_VARYING:
1312 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
1313 return 1;
1314 default:
1315 goto invalid_operation;
1316 }
1317 case GL_OFFSET:
1318 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1319 *val = RESOURCE_UNI(res)->offset;
1320 return 1;
1321 case GL_BLOCK_INDEX:
1322 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1323 *val = RESOURCE_UNI(res)->block_index;
1324 return 1;
1325 case GL_ARRAY_STRIDE:
1326 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1327 *val = RESOURCE_UNI(res)->array_stride;
1328 return 1;
1329 case GL_MATRIX_STRIDE:
1330 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1331 *val = RESOURCE_UNI(res)->matrix_stride;
1332 return 1;
1333 case GL_IS_ROW_MAJOR:
1334 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1335 *val = RESOURCE_UNI(res)->row_major;
1336 return 1;
1337 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1338 VALIDATE_TYPE(GL_UNIFORM);
1339 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1340 return 1;
1341 case GL_BUFFER_BINDING:
1342 case GL_BUFFER_DATA_SIZE:
1343 case GL_NUM_ACTIVE_VARIABLES:
1344 case GL_ACTIVE_VARIABLES:
1345 return get_buffer_property(shProg, res, prop, val, caller);
1346 case GL_REFERENCED_BY_COMPUTE_SHADER:
1347 if (!_mesa_has_compute_shaders(ctx))
1348 goto invalid_enum;
1349 /* fallthrough */
1350 case GL_REFERENCED_BY_VERTEX_SHADER:
1351 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1352 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1353 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1354 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1355 switch (res->Type) {
1356 case GL_UNIFORM:
1357 case GL_PROGRAM_INPUT:
1358 case GL_PROGRAM_OUTPUT:
1359 case GL_UNIFORM_BLOCK:
1360 case GL_BUFFER_VARIABLE:
1361 case GL_SHADER_STORAGE_BLOCK:
1362 case GL_ATOMIC_COUNTER_BUFFER:
1363 *val = is_resource_referenced(shProg, res, index,
1364 stage_from_enum(prop));
1365 return 1;
1366 default:
1367 goto invalid_operation;
1368 }
1369 case GL_LOCATION:
1370 switch (res->Type) {
1371 case GL_UNIFORM:
1372 case GL_PROGRAM_INPUT:
1373 case GL_PROGRAM_OUTPUT:
1374 *val = program_resource_location(shProg, res,
1375 _mesa_program_resource_name(res),
1376 0);
1377 return 1;
1378 default:
1379 goto invalid_operation;
1380 }
1381 case GL_LOCATION_INDEX:
1382 if (res->Type != GL_PROGRAM_OUTPUT)
1383 goto invalid_operation;
1384 *val = RESOURCE_VAR(res)->data.index;
1385 return 1;
1386
1387 case GL_NUM_COMPATIBLE_SUBROUTINES:
1388 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1389 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1390 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1391 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1392 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1393 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1394 goto invalid_operation;
1395 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1396 return 1;
1397 case GL_COMPATIBLE_SUBROUTINES: {
1398 const struct gl_uniform_storage *uni;
1399 struct gl_shader *sh;
1400 unsigned count, i;
1401 int j;
1402
1403 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1404 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1405 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1406 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1407 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1408 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1409 goto invalid_operation;
1410 uni = RESOURCE_UNI(res);
1411
1412 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1413 count = 0;
1414 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1415 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1416 for (j = 0; j < fn->num_compat_types; j++) {
1417 if (fn->types[j] == uni->type) {
1418 val[count++] = i;
1419 break;
1420 }
1421 }
1422 }
1423 return count;
1424 }
1425
1426 case GL_TOP_LEVEL_ARRAY_SIZE:
1427 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1428 *val = program_resource_top_level_array_size(shProg, res,
1429 _mesa_program_resource_name(res));
1430 return 1;
1431
1432 case GL_TOP_LEVEL_ARRAY_STRIDE:
1433 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1434 *val = program_resource_top_level_array_stride(shProg, res,
1435 _mesa_program_resource_name(res));
1436 return 1;
1437
1438 /* GL_ARB_tessellation_shader */
1439 case GL_IS_PER_PATCH:
1440 switch (res->Type) {
1441 case GL_PROGRAM_INPUT:
1442 case GL_PROGRAM_OUTPUT:
1443 *val = RESOURCE_VAR(res)->data.patch;
1444 return 1;
1445 default:
1446 goto invalid_operation;
1447 }
1448 default:
1449 goto invalid_enum;
1450 }
1451
1452 #undef VALIDATE_TYPE
1453 #undef VALIDATE_TYPE_2
1454
1455 invalid_enum:
1456 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1457 _mesa_enum_to_string(res->Type),
1458 _mesa_enum_to_string(prop));
1459 return 0;
1460
1461 invalid_operation:
1462 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1463 _mesa_enum_to_string(res->Type),
1464 _mesa_enum_to_string(prop));
1465 return 0;
1466 }
1467
1468 extern void
1469 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1470 GLenum programInterface, GLuint index, GLsizei propCount,
1471 const GLenum *props, GLsizei bufSize,
1472 GLsizei *length, GLint *params)
1473 {
1474 GET_CURRENT_CONTEXT(ctx);
1475 GLint *val = (GLint *) params;
1476 const GLenum *prop = props;
1477 GLsizei amount = 0;
1478
1479 struct gl_program_resource *res =
1480 _mesa_program_resource_find_index(shProg, programInterface, index);
1481
1482 /* No such resource found or bufSize negative. */
1483 if (!res || bufSize < 0) {
1484 _mesa_error(ctx, GL_INVALID_VALUE,
1485 "glGetProgramResourceiv(%s index %d bufSize %d)",
1486 _mesa_enum_to_string(programInterface), index, bufSize);
1487 return;
1488 }
1489
1490 /* Write propCount values until error occurs or bufSize reached. */
1491 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1492 int props_written =
1493 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1494 "glGetProgramResourceiv");
1495
1496 /* Error happened. */
1497 if (props_written == 0)
1498 return;
1499
1500 amount += props_written;
1501 }
1502
1503 /* If <length> is not NULL, the actual number of integer values
1504 * written to <params> will be written to <length>.
1505 */
1506 if (length)
1507 *length = amount;
1508 }