main: consider that unsized arrays have at least one active element
[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 return RESOURCE_UNI(res)->array_elements;
489 case GL_BUFFER_VARIABLE:
490 /* Unsized arrays */
491 if (RESOURCE_UNI(res)->array_stride > 0 &&
492 RESOURCE_UNI(res)->array_elements == 0)
493 return 1;
494 else
495 return RESOURCE_UNI(res)->array_elements;
496 case GL_VERTEX_SUBROUTINE:
497 case GL_GEOMETRY_SUBROUTINE:
498 case GL_FRAGMENT_SUBROUTINE:
499 case GL_COMPUTE_SUBROUTINE:
500 case GL_TESS_CONTROL_SUBROUTINE:
501 case GL_TESS_EVALUATION_SUBROUTINE:
502 case GL_ATOMIC_COUNTER_BUFFER:
503 case GL_UNIFORM_BLOCK:
504 case GL_SHADER_STORAGE_BLOCK:
505 return 0;
506 default:
507 assert(!"support for resource type not implemented");
508 }
509 return 0;
510 }
511
512 /**
513 * Checks if array subscript is valid and if so sets array_index.
514 */
515 static bool
516 valid_array_index(const GLchar *name, unsigned *array_index)
517 {
518 long idx = 0;
519 const GLchar *out_base_name_end;
520
521 idx = parse_program_resource_name(name, &out_base_name_end);
522 if (idx < 0)
523 return false;
524
525 if (array_index)
526 *array_index = idx;
527
528 return true;
529 }
530
531 /* Find a program resource with specific name in given interface.
532 */
533 struct gl_program_resource *
534 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
535 GLenum programInterface, const char *name,
536 unsigned *array_index)
537 {
538 struct gl_program_resource *res = shProg->ProgramResourceList;
539 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
540 if (res->Type != programInterface)
541 continue;
542
543 /* Resource basename. */
544 const char *rname = _mesa_program_resource_name(res);
545 unsigned baselen = strlen(rname);
546
547 if (strncmp(rname, name, baselen) == 0) {
548 switch (programInterface) {
549 case GL_UNIFORM_BLOCK:
550 case GL_SHADER_STORAGE_BLOCK:
551 /* Basename match, check if array or struct. */
552 if (name[baselen] == '\0' ||
553 name[baselen] == '[' ||
554 name[baselen] == '.') {
555 return res;
556 }
557 break;
558 case GL_TRANSFORM_FEEDBACK_VARYING:
559 case GL_BUFFER_VARIABLE:
560 case GL_UNIFORM:
561 case GL_VERTEX_SUBROUTINE_UNIFORM:
562 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
563 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
564 case GL_COMPUTE_SUBROUTINE_UNIFORM:
565 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
566 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
567 case GL_VERTEX_SUBROUTINE:
568 case GL_GEOMETRY_SUBROUTINE:
569 case GL_FRAGMENT_SUBROUTINE:
570 case GL_COMPUTE_SUBROUTINE:
571 case GL_TESS_CONTROL_SUBROUTINE:
572 case GL_TESS_EVALUATION_SUBROUTINE:
573 if (name[baselen] == '.') {
574 return res;
575 }
576 /* fall-through */
577 case GL_PROGRAM_INPUT:
578 case GL_PROGRAM_OUTPUT:
579 if (name[baselen] == '\0') {
580 return res;
581 } else if (name[baselen] == '[' &&
582 valid_array_index(name, array_index)) {
583 return res;
584 }
585 break;
586 default:
587 assert(!"not implemented for given interface");
588 }
589 }
590 }
591 return NULL;
592 }
593
594 static GLuint
595 calc_resource_index(struct gl_shader_program *shProg,
596 struct gl_program_resource *res)
597 {
598 unsigned i;
599 GLuint index = 0;
600 for (i = 0; i < shProg->NumProgramResourceList; i++) {
601 if (&shProg->ProgramResourceList[i] == res)
602 return index;
603 if (shProg->ProgramResourceList[i].Type == res->Type)
604 index++;
605 }
606 return GL_INVALID_INDEX;
607 }
608
609 /**
610 * Calculate index for the given resource.
611 */
612 GLuint
613 _mesa_program_resource_index(struct gl_shader_program *shProg,
614 struct gl_program_resource *res)
615 {
616 if (!res)
617 return GL_INVALID_INDEX;
618
619 switch (res->Type) {
620 case GL_ATOMIC_COUNTER_BUFFER:
621 return RESOURCE_ATC(res) - shProg->AtomicBuffers;
622 case GL_UNIFORM_BLOCK:
623 case GL_SHADER_STORAGE_BLOCK:
624 case GL_TRANSFORM_FEEDBACK_VARYING:
625 default:
626 return calc_resource_index(shProg, res);
627 }
628 }
629
630 /* Find a program resource with specific index in given interface.
631 */
632 struct gl_program_resource *
633 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
634 GLenum programInterface, GLuint index)
635 {
636 struct gl_program_resource *res = shProg->ProgramResourceList;
637 int idx = -1;
638
639 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
640 if (res->Type != programInterface)
641 continue;
642
643 switch (res->Type) {
644 case GL_UNIFORM_BLOCK:
645 case GL_ATOMIC_COUNTER_BUFFER:
646 case GL_SHADER_STORAGE_BLOCK:
647 if (_mesa_program_resource_index(shProg, res) == index)
648 return res;
649 break;
650 case GL_TRANSFORM_FEEDBACK_VARYING:
651 case GL_PROGRAM_INPUT:
652 case GL_PROGRAM_OUTPUT:
653 case GL_UNIFORM:
654 case GL_VERTEX_SUBROUTINE_UNIFORM:
655 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
656 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
657 case GL_COMPUTE_SUBROUTINE_UNIFORM:
658 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
659 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
660 case GL_VERTEX_SUBROUTINE:
661 case GL_GEOMETRY_SUBROUTINE:
662 case GL_FRAGMENT_SUBROUTINE:
663 case GL_COMPUTE_SUBROUTINE:
664 case GL_TESS_CONTROL_SUBROUTINE:
665 case GL_TESS_EVALUATION_SUBROUTINE:
666 case GL_BUFFER_VARIABLE:
667 if (++idx == (int) index)
668 return res;
669 break;
670 default:
671 assert(!"not implemented for given interface");
672 }
673 }
674 return NULL;
675 }
676
677 /* Function returns if resource name is expected to have index
678 * appended into it.
679 *
680 *
681 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
682 * spec says:
683 *
684 * "If the active uniform is an array, the uniform name returned in
685 * name will always be the name of the uniform array appended with
686 * "[0]"."
687 *
688 * The same text also appears in the OpenGL 4.2 spec. It does not,
689 * however, appear in any previous spec. Previous specifications are
690 * ambiguous in this regard. However, either name can later be passed
691 * to glGetUniformLocation (and related APIs), so there shouldn't be any
692 * harm in always appending "[0]" to uniform array names.
693 *
694 * Geometry shader stage has different naming convention where the 'normal'
695 * condition is an array, therefore for variables referenced in geometry
696 * stage we do not add '[0]'.
697 *
698 * Note, that TCS outputs and TES inputs should not have index appended
699 * either.
700 */
701 static bool
702 add_index_to_name(struct gl_program_resource *res)
703 {
704 bool add_index = !(((res->Type == GL_PROGRAM_INPUT) &&
705 res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
706
707 /* Transform feedback varyings have array index already appended
708 * in their names.
709 */
710 if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
711 add_index = false;
712
713 return add_index;
714 }
715
716 /* Get name length of a program resource. This consists of
717 * base name + 3 for '[0]' if resource is an array.
718 */
719 extern unsigned
720 _mesa_program_resource_name_len(struct gl_program_resource *res)
721 {
722 unsigned length = strlen(_mesa_program_resource_name(res));
723 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
724 length += 3;
725 return length;
726 }
727
728 /* Get full name of a program resource.
729 */
730 bool
731 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
732 GLenum programInterface, GLuint index,
733 GLsizei bufSize, GLsizei *length,
734 GLchar *name, const char *caller)
735 {
736 GET_CURRENT_CONTEXT(ctx);
737
738 /* Find resource with given interface and index. */
739 struct gl_program_resource *res =
740 _mesa_program_resource_find_index(shProg, programInterface, index);
741
742 /* The error INVALID_VALUE is generated if <index> is greater than
743 * or equal to the number of entries in the active resource list for
744 * <programInterface>.
745 */
746 if (!res) {
747 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
748 return false;
749 }
750
751 if (bufSize < 0) {
752 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
753 return false;
754 }
755
756 GLsizei localLength;
757
758 if (length == NULL)
759 length = &localLength;
760
761 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
762
763 if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
764 int i;
765
766 /* The comparison is strange because *length does *NOT* include the
767 * terminating NUL, but maxLength does.
768 */
769 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
770 name[*length + i] = "[0]"[i];
771
772 name[*length + i] = '\0';
773 *length += i;
774 }
775 return true;
776 }
777
778 static GLint
779 program_resource_location(struct gl_shader_program *shProg,
780 struct gl_program_resource *res, const char *name,
781 unsigned array_index)
782 {
783 /* Built-in locations should report GL_INVALID_INDEX. */
784 if (is_gl_identifier(name))
785 return GL_INVALID_INDEX;
786
787 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
788 * offsets are used internally to differentiate between built-in attributes
789 * and user-defined attributes.
790 */
791 switch (res->Type) {
792 case GL_PROGRAM_INPUT:
793 /* If the input is an array, fail if the index is out of bounds. */
794 if (array_index > 0
795 && array_index >= RESOURCE_VAR(res)->type->length) {
796 return -1;
797 }
798 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
799 case GL_PROGRAM_OUTPUT:
800 /* If the output is an array, fail if the index is out of bounds. */
801 if (array_index > 0
802 && array_index >= RESOURCE_VAR(res)->type->length) {
803 return -1;
804 }
805 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
806 case GL_UNIFORM:
807 /* If the uniform is built-in, fail. */
808 if (RESOURCE_UNI(res)->builtin)
809 return -1;
810
811 /* From the GL_ARB_uniform_buffer_object spec:
812 *
813 * "The value -1 will be returned if <name> does not correspond to an
814 * active uniform variable name in <program>, if <name> is associated
815 * with a named uniform block, or if <name> starts with the reserved
816 * prefix "gl_"."
817 */
818 if (RESOURCE_UNI(res)->block_index != -1 ||
819 RESOURCE_UNI(res)->atomic_buffer_index != -1)
820 return -1;
821
822 /* fallthrough */
823 case GL_VERTEX_SUBROUTINE_UNIFORM:
824 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
825 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
826 case GL_COMPUTE_SUBROUTINE_UNIFORM:
827 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
828 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
829 /* If the uniform is an array, fail if the index is out of bounds. */
830 if (array_index > 0
831 && array_index >= RESOURCE_UNI(res)->array_elements) {
832 return -1;
833 }
834
835 /* location in remap table + array element offset */
836 return RESOURCE_UNI(res)->remap_location + array_index;
837 default:
838 return -1;
839 }
840 }
841
842 static char*
843 get_top_level_name(const char *name)
844 {
845 const char *first_dot = strchr(name, '.');
846 const char *first_square_bracket = strchr(name, '[');
847 int name_size = 0;
848 /* From ARB_program_interface_query spec:
849 *
850 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying the
851 * number of active array elements of the top-level shader storage block
852 * member containing to the active variable is written to <params>. If the
853 * top-level block member is not declared as an array, the value one is
854 * written to <params>. If the top-level block member is an array with no
855 * declared size, the value zero is written to <params>.
856 */
857
858 /* The buffer variable is on top level.*/
859 if (!first_square_bracket && !first_dot)
860 name_size = strlen(name);
861 else if ((!first_square_bracket ||
862 (first_dot && first_dot < first_square_bracket)))
863 name_size = first_dot - name;
864 else
865 name_size = first_square_bracket - name;
866
867 return strndup(name, name_size);
868 }
869
870 static char*
871 get_var_name(const char *name)
872 {
873 const char *first_dot = strchr(name, '.');
874
875 if (!first_dot)
876 return strdup(name);
877
878 return strndup(first_dot+1, strlen(first_dot) - 1);
879 }
880
881 static bool
882 is_top_level_shader_storage_block_member(const char* name,
883 const char* interface_name,
884 const char* field_name)
885 {
886 bool result = false;
887
888 /* If the given variable is already a top-level shader storage
889 * block member, then return array_size = 1.
890 * We could have two possibilities: if we have an instanced
891 * shader storage block or not instanced.
892 *
893 * For the first, we check create a name as it was in top level and
894 * compare it with the real name. If they are the same, then
895 * the variable is already at top-level.
896 *
897 * Full instanced name is: interface name + '.' + var name +
898 * NULL character
899 */
900 int name_length = strlen(interface_name) + 1 + strlen(field_name) + 1;
901 char *full_instanced_name = (char *) calloc(name_length, sizeof(char));
902 if (!full_instanced_name) {
903 fprintf(stderr, "%s: Cannot allocate space for name\n", __func__);
904 return false;
905 }
906
907 snprintf(full_instanced_name, name_length, "%s.%s",
908 interface_name, field_name);
909
910 /* Check if its top-level shader storage block member of an
911 * instanced interface block, or of a unnamed interface block.
912 */
913 if (strcmp(name, full_instanced_name) == 0 ||
914 strcmp(name, field_name) == 0)
915 result = true;
916
917 free(full_instanced_name);
918 return result;
919 }
920
921 static GLint
922 program_resource_top_level_array_size(struct gl_shader_program *shProg,
923 struct gl_program_resource *res,
924 const char *name)
925 {
926 int block_index = RESOURCE_UNI(res)->block_index;
927 int array_size = -1;
928 char *var_name = get_top_level_name(name);
929 char *interface_name =
930 get_top_level_name(shProg->UniformBlocks[block_index].Name);
931
932 if (strcmp(var_name, interface_name) == 0) {
933 /* Deal with instanced array of SSBOs */
934 char *temp_name = get_var_name(name);
935 free(var_name);
936 var_name = get_top_level_name(temp_name);
937 free(temp_name);
938 }
939
940 for (unsigned i = 0; i < shProg->NumShaders; i++) {
941 if (shProg->Shaders[i] == NULL)
942 continue;
943
944 const gl_shader *stage = shProg->Shaders[i];
945 foreach_in_list(ir_instruction, node, stage->ir) {
946 ir_variable *var = node->as_variable();
947 if (!var || !var->get_interface_type() ||
948 var->data.mode != ir_var_shader_storage)
949 continue;
950
951 const glsl_type *interface = var->get_interface_type();
952
953 if (strcmp(interface_name, interface->name) != 0)
954 continue;
955
956 for (unsigned i = 0; i < interface->length; i++) {
957 const glsl_struct_field *field = &interface->fields.structure[i];
958 if (strcmp(field->name, var_name) != 0)
959 continue;
960 /* From GL_ARB_program_interface_query spec:
961 *
962 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer
963 * identifying the number of active array elements of the top-level
964 * shader storage block member containing to the active variable is
965 * written to <params>. If the top-level block member is not
966 * declared as an array, the value one is written to <params>. If
967 * the top-level block member is an array with no declared size,
968 * the value zero is written to <params>.
969 */
970 if (is_top_level_shader_storage_block_member(name,
971 interface_name,
972 var_name))
973 array_size = 1;
974 else if (field->type->is_unsized_array())
975 array_size = 0;
976 else if (field->type->is_array())
977 array_size = field->type->length;
978 else
979 array_size = 1;
980
981 goto found_top_level_array_size;
982 }
983 }
984 }
985 found_top_level_array_size:
986 free(interface_name);
987 free(var_name);
988 return array_size;
989 }
990
991 static GLint
992 program_resource_top_level_array_stride(struct gl_shader_program *shProg,
993 struct gl_program_resource *res,
994 const char *name)
995 {
996 int block_index = RESOURCE_UNI(res)->block_index;
997 int array_stride = -1;
998 char *var_name = get_top_level_name(name);
999 char *interface_name =
1000 get_top_level_name(shProg->UniformBlocks[block_index].Name);
1001
1002 if (strcmp(var_name, interface_name) == 0) {
1003 /* Deal with instanced array of SSBOs */
1004 char *temp_name = get_var_name(name);
1005 free(var_name);
1006 var_name = get_top_level_name(temp_name);
1007 free(temp_name);
1008 }
1009
1010 for (unsigned i = 0; i < shProg->NumShaders; i++) {
1011 if (shProg->Shaders[i] == NULL)
1012 continue;
1013
1014 const gl_shader *stage = shProg->Shaders[i];
1015 foreach_in_list(ir_instruction, node, stage->ir) {
1016 ir_variable *var = node->as_variable();
1017 if (!var || !var->get_interface_type() ||
1018 var->data.mode != ir_var_shader_storage)
1019 continue;
1020
1021 const glsl_type *interface = var->get_interface_type();
1022
1023 if (strcmp(interface_name, interface->name) != 0) {
1024 continue;
1025 }
1026
1027 for (unsigned i = 0; i < interface->length; i++) {
1028 const glsl_struct_field *field = &interface->fields.structure[i];
1029 if (strcmp(field->name, var_name) != 0)
1030 continue;
1031 /* From GL_ARB_program_interface_query:
1032 *
1033 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
1034 * identifying the stride between array elements of the top-level
1035 * shader storage block member containing the active variable is
1036 * written to <params>. For top-level block members declared as
1037 * arrays, the value written is the difference, in basic machine
1038 * units, between the offsets of the active variable for
1039 * consecutive elements in the top-level array. For top-level
1040 * block members not declared as an array, zero is written to
1041 * <params>."
1042 */
1043 if (field->type->is_array()) {
1044 const enum glsl_matrix_layout matrix_layout =
1045 glsl_matrix_layout(field->matrix_layout);
1046 bool row_major = matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
1047 const glsl_type *array_type = field->type->fields.array;
1048
1049 if (is_top_level_shader_storage_block_member(name,
1050 interface_name,
1051 var_name)) {
1052 array_stride = 0;
1053 goto found_top_level_array_stride;
1054 }
1055 if (interface->interface_packing != GLSL_INTERFACE_PACKING_STD430) {
1056 if (array_type->is_record() || array_type->is_array()) {
1057 array_stride = array_type->std140_size(row_major);
1058 array_stride = glsl_align(array_stride, 16);
1059 } else {
1060 unsigned element_base_align = 0;
1061 element_base_align = array_type->std140_base_alignment(row_major);
1062 array_stride = MAX2(element_base_align, 16);
1063 }
1064 } else {
1065 array_stride = array_type->std430_array_stride(row_major);
1066 }
1067 } else {
1068 array_stride = 0;
1069 }
1070 goto found_top_level_array_stride;
1071 }
1072 }
1073 }
1074 found_top_level_array_stride:
1075 free(interface_name);
1076 free(var_name);
1077 return array_stride;
1078 }
1079
1080 /**
1081 * Function implements following location queries:
1082 * glGetUniformLocation
1083 */
1084 GLint
1085 _mesa_program_resource_location(struct gl_shader_program *shProg,
1086 GLenum programInterface, const char *name)
1087 {
1088 unsigned array_index = 0;
1089 struct gl_program_resource *res =
1090 _mesa_program_resource_find_name(shProg, programInterface, name,
1091 &array_index);
1092
1093 /* Resource not found. */
1094 if (!res)
1095 return -1;
1096
1097 return program_resource_location(shProg, res, name, array_index);
1098 }
1099
1100 /**
1101 * Function implements following index queries:
1102 * glGetFragDataIndex
1103 */
1104 GLint
1105 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1106 GLenum programInterface, const char *name)
1107 {
1108 struct gl_program_resource *res =
1109 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1110
1111 /* Non-existent variable or resource is not referenced by fragment stage. */
1112 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1113 return -1;
1114
1115 return RESOURCE_VAR(res)->data.index;
1116 }
1117
1118 static uint8_t
1119 stage_from_enum(GLenum ref)
1120 {
1121 switch (ref) {
1122 case GL_REFERENCED_BY_VERTEX_SHADER:
1123 return MESA_SHADER_VERTEX;
1124 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1125 return MESA_SHADER_TESS_CTRL;
1126 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1127 return MESA_SHADER_TESS_EVAL;
1128 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1129 return MESA_SHADER_GEOMETRY;
1130 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1131 return MESA_SHADER_FRAGMENT;
1132 case GL_REFERENCED_BY_COMPUTE_SHADER:
1133 return MESA_SHADER_COMPUTE;
1134 default:
1135 assert(!"shader stage not supported");
1136 return MESA_SHADER_STAGES;
1137 }
1138 }
1139
1140 /**
1141 * Check if resource is referenced by given 'referenced by' stage enum.
1142 * ATC and UBO resources hold stage references of their own.
1143 */
1144 static bool
1145 is_resource_referenced(struct gl_shader_program *shProg,
1146 struct gl_program_resource *res,
1147 GLuint index, uint8_t stage)
1148 {
1149 /* First, check if we even have such a stage active. */
1150 if (!shProg->_LinkedShaders[stage])
1151 return false;
1152
1153 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1154 return RESOURCE_ATC(res)->StageReferences[stage];
1155
1156 if (res->Type == GL_UNIFORM_BLOCK || res->Type == GL_SHADER_STORAGE_BLOCK)
1157 return shProg->UniformBlockStageIndex[stage][index] != -1;
1158
1159 return res->StageReferences & (1 << stage);
1160 }
1161
1162 static unsigned
1163 get_buffer_property(struct gl_shader_program *shProg,
1164 struct gl_program_resource *res, const GLenum prop,
1165 GLint *val, const char *caller)
1166 {
1167 GET_CURRENT_CONTEXT(ctx);
1168 if (res->Type != GL_UNIFORM_BLOCK &&
1169 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1170 res->Type != GL_SHADER_STORAGE_BLOCK)
1171 goto invalid_operation;
1172
1173 if (res->Type == GL_UNIFORM_BLOCK) {
1174 switch (prop) {
1175 case GL_BUFFER_BINDING:
1176 *val = RESOURCE_UBO(res)->Binding;
1177 return 1;
1178 case GL_BUFFER_DATA_SIZE:
1179 *val = RESOURCE_UBO(res)->UniformBufferSize;
1180 return 1;
1181 case GL_NUM_ACTIVE_VARIABLES:
1182 *val = 0;
1183 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1184 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1185 struct gl_program_resource *uni =
1186 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1187 NULL);
1188 if (!uni)
1189 continue;
1190 (*val)++;
1191 }
1192 return 1;
1193 case GL_ACTIVE_VARIABLES:
1194 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1195 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1196 struct gl_program_resource *uni =
1197 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1198 NULL);
1199 if (!uni)
1200 continue;
1201 *val++ =
1202 _mesa_program_resource_index(shProg, uni);
1203 }
1204 return RESOURCE_UBO(res)->NumUniforms;
1205 }
1206 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1207 switch (prop) {
1208 case GL_BUFFER_BINDING:
1209 *val = RESOURCE_UBO(res)->Binding;
1210 return 1;
1211 case GL_BUFFER_DATA_SIZE:
1212 *val = RESOURCE_UBO(res)->UniformBufferSize;
1213 return 1;
1214 case GL_NUM_ACTIVE_VARIABLES:
1215 *val = 0;
1216 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1217 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1218 struct gl_program_resource *uni =
1219 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1220 iname, NULL);
1221 if (!uni)
1222 continue;
1223 (*val)++;
1224 }
1225 return 1;
1226 case GL_ACTIVE_VARIABLES:
1227 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1228 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1229 struct gl_program_resource *uni =
1230 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1231 iname, NULL);
1232 if (!uni)
1233 continue;
1234 *val++ =
1235 _mesa_program_resource_index(shProg, uni);
1236 }
1237 return RESOURCE_UBO(res)->NumUniforms;
1238 }
1239 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1240 switch (prop) {
1241 case GL_BUFFER_BINDING:
1242 *val = RESOURCE_ATC(res)->Binding;
1243 return 1;
1244 case GL_BUFFER_DATA_SIZE:
1245 *val = RESOURCE_ATC(res)->MinimumSize;
1246 return 1;
1247 case GL_NUM_ACTIVE_VARIABLES:
1248 *val = RESOURCE_ATC(res)->NumUniforms;
1249 return 1;
1250 case GL_ACTIVE_VARIABLES:
1251 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
1252 *val++ = RESOURCE_ATC(res)->Uniforms[i];
1253 return RESOURCE_ATC(res)->NumUniforms;
1254 }
1255 }
1256 assert(!"support for property type not implemented");
1257
1258 invalid_operation:
1259 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1260 _mesa_enum_to_string(res->Type),
1261 _mesa_enum_to_string(prop));
1262
1263 return 0;
1264 }
1265
1266 unsigned
1267 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1268 struct gl_program_resource *res, GLuint index,
1269 const GLenum prop, GLint *val, const char *caller)
1270 {
1271 GET_CURRENT_CONTEXT(ctx);
1272
1273 #define VALIDATE_TYPE(type)\
1274 if (res->Type != type)\
1275 goto invalid_operation;
1276
1277 #define VALIDATE_TYPE_2(type1, type2)\
1278 if (res->Type != type1 && res->Type != type2)\
1279 goto invalid_operation;
1280
1281 switch(prop) {
1282 case GL_NAME_LENGTH:
1283 switch (res->Type) {
1284 case GL_ATOMIC_COUNTER_BUFFER:
1285 goto invalid_operation;
1286 default:
1287 /* Resource name length + terminator. */
1288 *val = _mesa_program_resource_name_len(res) + 1;
1289 }
1290 return 1;
1291 case GL_TYPE:
1292 switch (res->Type) {
1293 case GL_UNIFORM:
1294 case GL_BUFFER_VARIABLE:
1295 *val = RESOURCE_UNI(res)->type->gl_type;
1296 return 1;
1297 case GL_PROGRAM_INPUT:
1298 case GL_PROGRAM_OUTPUT:
1299 *val = RESOURCE_VAR(res)->type->gl_type;
1300 return 1;
1301 case GL_TRANSFORM_FEEDBACK_VARYING:
1302 *val = RESOURCE_XFB(res)->Type;
1303 return 1;
1304 default:
1305 goto invalid_operation;
1306 }
1307 case GL_ARRAY_SIZE:
1308 switch (res->Type) {
1309 case GL_UNIFORM:
1310 case GL_BUFFER_VARIABLE:
1311 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1312 return 1;
1313 case GL_PROGRAM_INPUT:
1314 case GL_PROGRAM_OUTPUT:
1315 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1316 return 1;
1317 case GL_TRANSFORM_FEEDBACK_VARYING:
1318 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
1319 return 1;
1320 default:
1321 goto invalid_operation;
1322 }
1323 case GL_OFFSET:
1324 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1325 *val = RESOURCE_UNI(res)->offset;
1326 return 1;
1327 case GL_BLOCK_INDEX:
1328 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1329 *val = RESOURCE_UNI(res)->block_index;
1330 return 1;
1331 case GL_ARRAY_STRIDE:
1332 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1333 *val = RESOURCE_UNI(res)->array_stride;
1334 return 1;
1335 case GL_MATRIX_STRIDE:
1336 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1337 *val = RESOURCE_UNI(res)->matrix_stride;
1338 return 1;
1339 case GL_IS_ROW_MAJOR:
1340 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1341 *val = RESOURCE_UNI(res)->row_major;
1342 return 1;
1343 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1344 VALIDATE_TYPE(GL_UNIFORM);
1345 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1346 return 1;
1347 case GL_BUFFER_BINDING:
1348 case GL_BUFFER_DATA_SIZE:
1349 case GL_NUM_ACTIVE_VARIABLES:
1350 case GL_ACTIVE_VARIABLES:
1351 return get_buffer_property(shProg, res, prop, val, caller);
1352 case GL_REFERENCED_BY_COMPUTE_SHADER:
1353 if (!_mesa_has_compute_shaders(ctx))
1354 goto invalid_enum;
1355 /* fallthrough */
1356 case GL_REFERENCED_BY_VERTEX_SHADER:
1357 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1358 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1359 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1360 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1361 switch (res->Type) {
1362 case GL_UNIFORM:
1363 case GL_PROGRAM_INPUT:
1364 case GL_PROGRAM_OUTPUT:
1365 case GL_UNIFORM_BLOCK:
1366 case GL_BUFFER_VARIABLE:
1367 case GL_SHADER_STORAGE_BLOCK:
1368 case GL_ATOMIC_COUNTER_BUFFER:
1369 *val = is_resource_referenced(shProg, res, index,
1370 stage_from_enum(prop));
1371 return 1;
1372 default:
1373 goto invalid_operation;
1374 }
1375 case GL_LOCATION:
1376 switch (res->Type) {
1377 case GL_UNIFORM:
1378 case GL_PROGRAM_INPUT:
1379 case GL_PROGRAM_OUTPUT:
1380 *val = program_resource_location(shProg, res,
1381 _mesa_program_resource_name(res),
1382 0);
1383 return 1;
1384 default:
1385 goto invalid_operation;
1386 }
1387 case GL_LOCATION_INDEX:
1388 if (res->Type != GL_PROGRAM_OUTPUT)
1389 goto invalid_operation;
1390 *val = RESOURCE_VAR(res)->data.index;
1391 return 1;
1392
1393 case GL_NUM_COMPATIBLE_SUBROUTINES:
1394 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1395 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1396 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1397 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1398 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1399 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1400 goto invalid_operation;
1401 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1402 return 1;
1403 case GL_COMPATIBLE_SUBROUTINES: {
1404 const struct gl_uniform_storage *uni;
1405 struct gl_shader *sh;
1406 unsigned count, i;
1407 int j;
1408
1409 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1410 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1411 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1412 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1413 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1414 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1415 goto invalid_operation;
1416 uni = RESOURCE_UNI(res);
1417
1418 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1419 count = 0;
1420 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1421 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1422 for (j = 0; j < fn->num_compat_types; j++) {
1423 if (fn->types[j] == uni->type) {
1424 val[count++] = i;
1425 break;
1426 }
1427 }
1428 }
1429 return count;
1430 }
1431
1432 case GL_TOP_LEVEL_ARRAY_SIZE:
1433 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1434 *val = program_resource_top_level_array_size(shProg, res,
1435 _mesa_program_resource_name(res));
1436 return 1;
1437
1438 case GL_TOP_LEVEL_ARRAY_STRIDE:
1439 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1440 *val = program_resource_top_level_array_stride(shProg, res,
1441 _mesa_program_resource_name(res));
1442 return 1;
1443
1444 /* GL_ARB_tessellation_shader */
1445 case GL_IS_PER_PATCH:
1446 switch (res->Type) {
1447 case GL_PROGRAM_INPUT:
1448 case GL_PROGRAM_OUTPUT:
1449 *val = RESOURCE_VAR(res)->data.patch;
1450 return 1;
1451 default:
1452 goto invalid_operation;
1453 }
1454 default:
1455 goto invalid_enum;
1456 }
1457
1458 #undef VALIDATE_TYPE
1459 #undef VALIDATE_TYPE_2
1460
1461 invalid_enum:
1462 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1463 _mesa_enum_to_string(res->Type),
1464 _mesa_enum_to_string(prop));
1465 return 0;
1466
1467 invalid_operation:
1468 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1469 _mesa_enum_to_string(res->Type),
1470 _mesa_enum_to_string(prop));
1471 return 0;
1472 }
1473
1474 extern void
1475 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1476 GLenum programInterface, GLuint index, GLsizei propCount,
1477 const GLenum *props, GLsizei bufSize,
1478 GLsizei *length, GLint *params)
1479 {
1480 GET_CURRENT_CONTEXT(ctx);
1481 GLint *val = (GLint *) params;
1482 const GLenum *prop = props;
1483 GLsizei amount = 0;
1484
1485 struct gl_program_resource *res =
1486 _mesa_program_resource_find_index(shProg, programInterface, index);
1487
1488 /* No such resource found or bufSize negative. */
1489 if (!res || bufSize < 0) {
1490 _mesa_error(ctx, GL_INVALID_VALUE,
1491 "glGetProgramResourceiv(%s index %d bufSize %d)",
1492 _mesa_enum_to_string(programInterface), index, bufSize);
1493 return;
1494 }
1495
1496 /* Write propCount values until error occurs or bufSize reached. */
1497 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1498 int props_written =
1499 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1500 "glGetProgramResourceiv");
1501
1502 /* Error happened. */
1503 if (props_written == 0)
1504 return;
1505
1506 amount += props_written;
1507 }
1508
1509 /* If <length> is not NULL, the actual number of integer values
1510 * written to <params> will be written to <length>.
1511 */
1512 if (length)
1513 *length = amount;
1514 }