main: Remove interface block array index for doing the name comparison
[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 unsigned baselen_without_array_index = baselen;
547 const char *rname_last_square_bracket = strrchr(rname, '[');
548 bool found = false;
549 bool rname_has_array_index_zero = false;
550 /* From ARB_program_interface_query spec:
551 *
552 * "uint GetProgramResourceIndex(uint program, enum programInterface,
553 * const char *name);
554 * [...]
555 * If <name> exactly matches the name string of one of the active
556 * resources for <programInterface>, the index of the matched resource is
557 * returned. Additionally, if <name> would exactly match the name string
558 * of an active resource if "[0]" were appended to <name>, the index of
559 * the matched resource is returned. [...]"
560 *
561 * "A string provided to GetProgramResourceLocation or
562 * GetProgramResourceLocationIndex is considered to match an active variable
563 * if:
564 *
565 * * the string exactly matches the name of the active variable;
566 *
567 * * if the string identifies the base name of an active array, where the
568 * string would exactly match the name of the variable if the suffix
569 * "[0]" were appended to the string; [...]"
570 */
571 /* Remove array's index from interface block name comparison only if
572 * array's index is zero and the resulting string length is the same
573 * than the provided name's length.
574 */
575 if (rname_last_square_bracket) {
576 baselen_without_array_index -= strlen(rname_last_square_bracket);
577 rname_has_array_index_zero =
578 (strncmp(rname_last_square_bracket, "[0]\0", 4) == 0) &&
579 (baselen_without_array_index == strlen(name));
580 }
581
582 if (strncmp(rname, name, baselen) == 0)
583 found = true;
584 else if (rname_has_array_index_zero &&
585 strncmp(rname, name, baselen_without_array_index) == 0)
586 found = true;
587
588 if (found) {
589 switch (programInterface) {
590 case GL_UNIFORM_BLOCK:
591 case GL_SHADER_STORAGE_BLOCK:
592 /* Basename match, check if array or struct. */
593 if (name[baselen] == '\0' ||
594 name[baselen] == '[' ||
595 name[baselen] == '.') {
596 return res;
597 }
598 break;
599 case GL_TRANSFORM_FEEDBACK_VARYING:
600 case GL_BUFFER_VARIABLE:
601 case GL_UNIFORM:
602 case GL_VERTEX_SUBROUTINE_UNIFORM:
603 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
604 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
605 case GL_COMPUTE_SUBROUTINE_UNIFORM:
606 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
607 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
608 case GL_VERTEX_SUBROUTINE:
609 case GL_GEOMETRY_SUBROUTINE:
610 case GL_FRAGMENT_SUBROUTINE:
611 case GL_COMPUTE_SUBROUTINE:
612 case GL_TESS_CONTROL_SUBROUTINE:
613 case GL_TESS_EVALUATION_SUBROUTINE:
614 if (name[baselen] == '.') {
615 return res;
616 }
617 /* fall-through */
618 case GL_PROGRAM_INPUT:
619 case GL_PROGRAM_OUTPUT:
620 if (name[baselen] == '\0') {
621 return res;
622 } else if (name[baselen] == '[' &&
623 valid_array_index(name, array_index)) {
624 return res;
625 }
626 break;
627 default:
628 assert(!"not implemented for given interface");
629 }
630 }
631 }
632 return NULL;
633 }
634
635 static GLuint
636 calc_resource_index(struct gl_shader_program *shProg,
637 struct gl_program_resource *res)
638 {
639 unsigned i;
640 GLuint index = 0;
641 for (i = 0; i < shProg->NumProgramResourceList; i++) {
642 if (&shProg->ProgramResourceList[i] == res)
643 return index;
644 if (shProg->ProgramResourceList[i].Type == res->Type)
645 index++;
646 }
647 return GL_INVALID_INDEX;
648 }
649
650 /**
651 * Calculate index for the given resource.
652 */
653 GLuint
654 _mesa_program_resource_index(struct gl_shader_program *shProg,
655 struct gl_program_resource *res)
656 {
657 if (!res)
658 return GL_INVALID_INDEX;
659
660 switch (res->Type) {
661 case GL_ATOMIC_COUNTER_BUFFER:
662 return RESOURCE_ATC(res) - shProg->AtomicBuffers;
663 case GL_UNIFORM_BLOCK:
664 case GL_SHADER_STORAGE_BLOCK:
665 case GL_TRANSFORM_FEEDBACK_VARYING:
666 default:
667 return calc_resource_index(shProg, res);
668 }
669 }
670
671 /* Find a program resource with specific index in given interface.
672 */
673 struct gl_program_resource *
674 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
675 GLenum programInterface, GLuint index)
676 {
677 struct gl_program_resource *res = shProg->ProgramResourceList;
678 int idx = -1;
679
680 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
681 if (res->Type != programInterface)
682 continue;
683
684 switch (res->Type) {
685 case GL_UNIFORM_BLOCK:
686 case GL_ATOMIC_COUNTER_BUFFER:
687 case GL_SHADER_STORAGE_BLOCK:
688 if (_mesa_program_resource_index(shProg, res) == index)
689 return res;
690 break;
691 case GL_TRANSFORM_FEEDBACK_VARYING:
692 case GL_PROGRAM_INPUT:
693 case GL_PROGRAM_OUTPUT:
694 case GL_UNIFORM:
695 case GL_VERTEX_SUBROUTINE_UNIFORM:
696 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
697 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
698 case GL_COMPUTE_SUBROUTINE_UNIFORM:
699 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
700 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
701 case GL_VERTEX_SUBROUTINE:
702 case GL_GEOMETRY_SUBROUTINE:
703 case GL_FRAGMENT_SUBROUTINE:
704 case GL_COMPUTE_SUBROUTINE:
705 case GL_TESS_CONTROL_SUBROUTINE:
706 case GL_TESS_EVALUATION_SUBROUTINE:
707 case GL_BUFFER_VARIABLE:
708 if (++idx == (int) index)
709 return res;
710 break;
711 default:
712 assert(!"not implemented for given interface");
713 }
714 }
715 return NULL;
716 }
717
718 /* Function returns if resource name is expected to have index
719 * appended into it.
720 *
721 *
722 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
723 * spec says:
724 *
725 * "If the active uniform is an array, the uniform name returned in
726 * name will always be the name of the uniform array appended with
727 * "[0]"."
728 *
729 * The same text also appears in the OpenGL 4.2 spec. It does not,
730 * however, appear in any previous spec. Previous specifications are
731 * ambiguous in this regard. However, either name can later be passed
732 * to glGetUniformLocation (and related APIs), so there shouldn't be any
733 * harm in always appending "[0]" to uniform array names.
734 *
735 * Geometry shader stage has different naming convention where the 'normal'
736 * condition is an array, therefore for variables referenced in geometry
737 * stage we do not add '[0]'.
738 *
739 * Note, that TCS outputs and TES inputs should not have index appended
740 * either.
741 */
742 static bool
743 add_index_to_name(struct gl_program_resource *res)
744 {
745 bool add_index = !(((res->Type == GL_PROGRAM_INPUT) &&
746 res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
747
748 /* Transform feedback varyings have array index already appended
749 * in their names.
750 */
751 if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
752 add_index = false;
753
754 return add_index;
755 }
756
757 /* Get name length of a program resource. This consists of
758 * base name + 3 for '[0]' if resource is an array.
759 */
760 extern unsigned
761 _mesa_program_resource_name_len(struct gl_program_resource *res)
762 {
763 unsigned length = strlen(_mesa_program_resource_name(res));
764 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
765 length += 3;
766 return length;
767 }
768
769 /* Get full name of a program resource.
770 */
771 bool
772 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
773 GLenum programInterface, GLuint index,
774 GLsizei bufSize, GLsizei *length,
775 GLchar *name, const char *caller)
776 {
777 GET_CURRENT_CONTEXT(ctx);
778
779 /* Find resource with given interface and index. */
780 struct gl_program_resource *res =
781 _mesa_program_resource_find_index(shProg, programInterface, index);
782
783 /* The error INVALID_VALUE is generated if <index> is greater than
784 * or equal to the number of entries in the active resource list for
785 * <programInterface>.
786 */
787 if (!res) {
788 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
789 return false;
790 }
791
792 if (bufSize < 0) {
793 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
794 return false;
795 }
796
797 GLsizei localLength;
798
799 if (length == NULL)
800 length = &localLength;
801
802 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
803
804 if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
805 int i;
806
807 /* The comparison is strange because *length does *NOT* include the
808 * terminating NUL, but maxLength does.
809 */
810 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
811 name[*length + i] = "[0]"[i];
812
813 name[*length + i] = '\0';
814 *length += i;
815 }
816 return true;
817 }
818
819 static GLint
820 program_resource_location(struct gl_shader_program *shProg,
821 struct gl_program_resource *res, const char *name,
822 unsigned array_index)
823 {
824 /* Built-in locations should report GL_INVALID_INDEX. */
825 if (is_gl_identifier(name))
826 return GL_INVALID_INDEX;
827
828 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
829 * offsets are used internally to differentiate between built-in attributes
830 * and user-defined attributes.
831 */
832 switch (res->Type) {
833 case GL_PROGRAM_INPUT:
834 /* If the input is an array, fail if the index is out of bounds. */
835 if (array_index > 0
836 && array_index >= RESOURCE_VAR(res)->type->length) {
837 return -1;
838 }
839 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
840 case GL_PROGRAM_OUTPUT:
841 /* If the output is an array, fail if the index is out of bounds. */
842 if (array_index > 0
843 && array_index >= RESOURCE_VAR(res)->type->length) {
844 return -1;
845 }
846 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
847 case GL_UNIFORM:
848 /* If the uniform is built-in, fail. */
849 if (RESOURCE_UNI(res)->builtin)
850 return -1;
851
852 /* From page 79 of the OpenGL 4.2 spec:
853 *
854 * "A valid name cannot be a structure, an array of structures, or any
855 * portion of a single vector or a matrix."
856 */
857 if (RESOURCE_UNI(res)->type->without_array()->is_record())
858 return -1;
859
860 /* From the GL_ARB_uniform_buffer_object spec:
861 *
862 * "The value -1 will be returned if <name> does not correspond to an
863 * active uniform variable name in <program>, if <name> is associated
864 * with a named uniform block, or if <name> starts with the reserved
865 * prefix "gl_"."
866 */
867 if (RESOURCE_UNI(res)->block_index != -1 ||
868 RESOURCE_UNI(res)->atomic_buffer_index != -1)
869 return -1;
870
871 /* fallthrough */
872 case GL_VERTEX_SUBROUTINE_UNIFORM:
873 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
874 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
875 case GL_COMPUTE_SUBROUTINE_UNIFORM:
876 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
877 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
878 /* If the uniform is an array, fail if the index is out of bounds. */
879 if (array_index > 0
880 && array_index >= RESOURCE_UNI(res)->array_elements) {
881 return -1;
882 }
883
884 /* location in remap table + array element offset */
885 return RESOURCE_UNI(res)->remap_location + array_index;
886 default:
887 return -1;
888 }
889 }
890
891 /**
892 * Function implements following location queries:
893 * glGetUniformLocation
894 */
895 GLint
896 _mesa_program_resource_location(struct gl_shader_program *shProg,
897 GLenum programInterface, const char *name)
898 {
899 unsigned array_index = 0;
900 struct gl_program_resource *res =
901 _mesa_program_resource_find_name(shProg, programInterface, name,
902 &array_index);
903
904 /* Resource not found. */
905 if (!res)
906 return -1;
907
908 return program_resource_location(shProg, res, name, array_index);
909 }
910
911 /**
912 * Function implements following index queries:
913 * glGetFragDataIndex
914 */
915 GLint
916 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
917 GLenum programInterface, const char *name)
918 {
919 struct gl_program_resource *res =
920 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
921
922 /* Non-existent variable or resource is not referenced by fragment stage. */
923 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
924 return -1;
925
926 return RESOURCE_VAR(res)->data.index;
927 }
928
929 static uint8_t
930 stage_from_enum(GLenum ref)
931 {
932 switch (ref) {
933 case GL_REFERENCED_BY_VERTEX_SHADER:
934 return MESA_SHADER_VERTEX;
935 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
936 return MESA_SHADER_TESS_CTRL;
937 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
938 return MESA_SHADER_TESS_EVAL;
939 case GL_REFERENCED_BY_GEOMETRY_SHADER:
940 return MESA_SHADER_GEOMETRY;
941 case GL_REFERENCED_BY_FRAGMENT_SHADER:
942 return MESA_SHADER_FRAGMENT;
943 case GL_REFERENCED_BY_COMPUTE_SHADER:
944 return MESA_SHADER_COMPUTE;
945 default:
946 assert(!"shader stage not supported");
947 return MESA_SHADER_STAGES;
948 }
949 }
950
951 /**
952 * Check if resource is referenced by given 'referenced by' stage enum.
953 * ATC and UBO resources hold stage references of their own.
954 */
955 static bool
956 is_resource_referenced(struct gl_shader_program *shProg,
957 struct gl_program_resource *res,
958 GLuint index, uint8_t stage)
959 {
960 /* First, check if we even have such a stage active. */
961 if (!shProg->_LinkedShaders[stage])
962 return false;
963
964 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
965 return RESOURCE_ATC(res)->StageReferences[stage];
966
967 if (res->Type == GL_UNIFORM_BLOCK || res->Type == GL_SHADER_STORAGE_BLOCK)
968 return shProg->UniformBlockStageIndex[stage][index] != -1;
969
970 return res->StageReferences & (1 << stage);
971 }
972
973 static unsigned
974 get_buffer_property(struct gl_shader_program *shProg,
975 struct gl_program_resource *res, const GLenum prop,
976 GLint *val, const char *caller)
977 {
978 GET_CURRENT_CONTEXT(ctx);
979 if (res->Type != GL_UNIFORM_BLOCK &&
980 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
981 res->Type != GL_SHADER_STORAGE_BLOCK)
982 goto invalid_operation;
983
984 if (res->Type == GL_UNIFORM_BLOCK) {
985 switch (prop) {
986 case GL_BUFFER_BINDING:
987 *val = RESOURCE_UBO(res)->Binding;
988 return 1;
989 case GL_BUFFER_DATA_SIZE:
990 *val = RESOURCE_UBO(res)->UniformBufferSize;
991 return 1;
992 case GL_NUM_ACTIVE_VARIABLES:
993 *val = 0;
994 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
995 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
996 struct gl_program_resource *uni =
997 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
998 NULL);
999 if (!uni)
1000 continue;
1001 (*val)++;
1002 }
1003 return 1;
1004 case GL_ACTIVE_VARIABLES: {
1005 unsigned num_values = 0;
1006 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1007 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1008 struct gl_program_resource *uni =
1009 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1010 NULL);
1011 if (!uni)
1012 continue;
1013 *val++ =
1014 _mesa_program_resource_index(shProg, uni);
1015 num_values++;
1016 }
1017 return num_values;
1018 }
1019 }
1020 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1021 switch (prop) {
1022 case GL_BUFFER_BINDING:
1023 *val = RESOURCE_UBO(res)->Binding;
1024 return 1;
1025 case GL_BUFFER_DATA_SIZE:
1026 *val = RESOURCE_UBO(res)->UniformBufferSize;
1027 return 1;
1028 case GL_NUM_ACTIVE_VARIABLES:
1029 *val = 0;
1030 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1031 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1032 struct gl_program_resource *uni =
1033 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1034 iname, NULL);
1035 if (!uni)
1036 continue;
1037 (*val)++;
1038 }
1039 return 1;
1040 case GL_ACTIVE_VARIABLES: {
1041 unsigned num_values = 0;
1042 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1043 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1044 struct gl_program_resource *uni =
1045 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1046 iname, NULL);
1047 if (!uni)
1048 continue;
1049 *val++ =
1050 _mesa_program_resource_index(shProg, uni);
1051 num_values++;
1052 }
1053 return num_values;
1054 }
1055 }
1056 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1057 switch (prop) {
1058 case GL_BUFFER_BINDING:
1059 *val = RESOURCE_ATC(res)->Binding;
1060 return 1;
1061 case GL_BUFFER_DATA_SIZE:
1062 *val = RESOURCE_ATC(res)->MinimumSize;
1063 return 1;
1064 case GL_NUM_ACTIVE_VARIABLES:
1065 *val = RESOURCE_ATC(res)->NumUniforms;
1066 return 1;
1067 case GL_ACTIVE_VARIABLES:
1068 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
1069 *val++ = RESOURCE_ATC(res)->Uniforms[i];
1070 return RESOURCE_ATC(res)->NumUniforms;
1071 }
1072 }
1073 assert(!"support for property type not implemented");
1074
1075 invalid_operation:
1076 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1077 _mesa_enum_to_string(res->Type),
1078 _mesa_enum_to_string(prop));
1079
1080 return 0;
1081 }
1082
1083 unsigned
1084 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1085 struct gl_program_resource *res, GLuint index,
1086 const GLenum prop, GLint *val, const char *caller)
1087 {
1088 GET_CURRENT_CONTEXT(ctx);
1089
1090 #define VALIDATE_TYPE(type)\
1091 if (res->Type != type)\
1092 goto invalid_operation;
1093
1094 #define VALIDATE_TYPE_2(type1, type2)\
1095 if (res->Type != type1 && res->Type != type2)\
1096 goto invalid_operation;
1097
1098 switch(prop) {
1099 case GL_NAME_LENGTH:
1100 switch (res->Type) {
1101 case GL_ATOMIC_COUNTER_BUFFER:
1102 goto invalid_operation;
1103 default:
1104 /* Resource name length + terminator. */
1105 *val = _mesa_program_resource_name_len(res) + 1;
1106 }
1107 return 1;
1108 case GL_TYPE:
1109 switch (res->Type) {
1110 case GL_UNIFORM:
1111 case GL_BUFFER_VARIABLE:
1112 *val = RESOURCE_UNI(res)->type->gl_type;
1113 return 1;
1114 case GL_PROGRAM_INPUT:
1115 case GL_PROGRAM_OUTPUT:
1116 *val = RESOURCE_VAR(res)->type->gl_type;
1117 return 1;
1118 case GL_TRANSFORM_FEEDBACK_VARYING:
1119 *val = RESOURCE_XFB(res)->Type;
1120 return 1;
1121 default:
1122 goto invalid_operation;
1123 }
1124 case GL_ARRAY_SIZE:
1125 switch (res->Type) {
1126 case GL_UNIFORM:
1127 case GL_BUFFER_VARIABLE:
1128 /* Test if a buffer variable is an array or an unsized array.
1129 * Unsized arrays return zero as array size.
1130 */
1131 if (RESOURCE_UNI(res)->is_shader_storage &&
1132 RESOURCE_UNI(res)->array_stride > 0)
1133 *val = RESOURCE_UNI(res)->array_elements;
1134 else
1135 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1136 return 1;
1137 case GL_PROGRAM_INPUT:
1138 case GL_PROGRAM_OUTPUT:
1139 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1140 return 1;
1141 case GL_TRANSFORM_FEEDBACK_VARYING:
1142 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
1143 return 1;
1144 default:
1145 goto invalid_operation;
1146 }
1147 case GL_OFFSET:
1148 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1149 *val = RESOURCE_UNI(res)->offset;
1150 return 1;
1151 case GL_BLOCK_INDEX:
1152 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1153 *val = RESOURCE_UNI(res)->block_index;
1154 return 1;
1155 case GL_ARRAY_STRIDE:
1156 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1157 *val = RESOURCE_UNI(res)->array_stride;
1158 return 1;
1159 case GL_MATRIX_STRIDE:
1160 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1161 *val = RESOURCE_UNI(res)->matrix_stride;
1162 return 1;
1163 case GL_IS_ROW_MAJOR:
1164 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1165 *val = RESOURCE_UNI(res)->row_major;
1166 return 1;
1167 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1168 VALIDATE_TYPE(GL_UNIFORM);
1169 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1170 return 1;
1171 case GL_BUFFER_BINDING:
1172 case GL_BUFFER_DATA_SIZE:
1173 case GL_NUM_ACTIVE_VARIABLES:
1174 case GL_ACTIVE_VARIABLES:
1175 return get_buffer_property(shProg, res, prop, val, caller);
1176 case GL_REFERENCED_BY_COMPUTE_SHADER:
1177 if (!_mesa_has_compute_shaders(ctx))
1178 goto invalid_enum;
1179 /* fallthrough */
1180 case GL_REFERENCED_BY_VERTEX_SHADER:
1181 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1182 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1183 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1184 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1185 switch (res->Type) {
1186 case GL_UNIFORM:
1187 case GL_PROGRAM_INPUT:
1188 case GL_PROGRAM_OUTPUT:
1189 case GL_UNIFORM_BLOCK:
1190 case GL_BUFFER_VARIABLE:
1191 case GL_SHADER_STORAGE_BLOCK:
1192 case GL_ATOMIC_COUNTER_BUFFER:
1193 *val = is_resource_referenced(shProg, res, index,
1194 stage_from_enum(prop));
1195 return 1;
1196 default:
1197 goto invalid_operation;
1198 }
1199 case GL_LOCATION:
1200 switch (res->Type) {
1201 case GL_UNIFORM:
1202 case GL_PROGRAM_INPUT:
1203 case GL_PROGRAM_OUTPUT:
1204 *val = program_resource_location(shProg, res,
1205 _mesa_program_resource_name(res),
1206 0);
1207 return 1;
1208 default:
1209 goto invalid_operation;
1210 }
1211 case GL_LOCATION_INDEX:
1212 if (res->Type != GL_PROGRAM_OUTPUT)
1213 goto invalid_operation;
1214 *val = RESOURCE_VAR(res)->data.index;
1215 return 1;
1216
1217 case GL_NUM_COMPATIBLE_SUBROUTINES:
1218 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1219 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1220 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1221 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1222 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1223 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1224 goto invalid_operation;
1225 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1226 return 1;
1227 case GL_COMPATIBLE_SUBROUTINES: {
1228 const struct gl_uniform_storage *uni;
1229 struct gl_shader *sh;
1230 unsigned count, i;
1231 int j;
1232
1233 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1234 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1235 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1236 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1237 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1238 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1239 goto invalid_operation;
1240 uni = RESOURCE_UNI(res);
1241
1242 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1243 count = 0;
1244 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1245 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1246 for (j = 0; j < fn->num_compat_types; j++) {
1247 if (fn->types[j] == uni->type) {
1248 val[count++] = i;
1249 break;
1250 }
1251 }
1252 }
1253 return count;
1254 }
1255
1256 case GL_TOP_LEVEL_ARRAY_SIZE:
1257 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1258 *val = RESOURCE_UNI(res)->top_level_array_size;
1259 return 1;
1260
1261 case GL_TOP_LEVEL_ARRAY_STRIDE:
1262 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1263 *val = RESOURCE_UNI(res)->top_level_array_stride;
1264 return 1;
1265
1266 /* GL_ARB_tessellation_shader */
1267 case GL_IS_PER_PATCH:
1268 switch (res->Type) {
1269 case GL_PROGRAM_INPUT:
1270 case GL_PROGRAM_OUTPUT:
1271 *val = RESOURCE_VAR(res)->data.patch;
1272 return 1;
1273 default:
1274 goto invalid_operation;
1275 }
1276 default:
1277 goto invalid_enum;
1278 }
1279
1280 #undef VALIDATE_TYPE
1281 #undef VALIDATE_TYPE_2
1282
1283 invalid_enum:
1284 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1285 _mesa_enum_to_string(res->Type),
1286 _mesa_enum_to_string(prop));
1287 return 0;
1288
1289 invalid_operation:
1290 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1291 _mesa_enum_to_string(res->Type),
1292 _mesa_enum_to_string(prop));
1293 return 0;
1294 }
1295
1296 extern void
1297 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1298 GLenum programInterface, GLuint index, GLsizei propCount,
1299 const GLenum *props, GLsizei bufSize,
1300 GLsizei *length, GLint *params)
1301 {
1302 GET_CURRENT_CONTEXT(ctx);
1303 GLint *val = (GLint *) params;
1304 const GLenum *prop = props;
1305 GLsizei amount = 0;
1306
1307 struct gl_program_resource *res =
1308 _mesa_program_resource_find_index(shProg, programInterface, index);
1309
1310 /* No such resource found or bufSize negative. */
1311 if (!res || bufSize < 0) {
1312 _mesa_error(ctx, GL_INVALID_VALUE,
1313 "glGetProgramResourceiv(%s index %d bufSize %d)",
1314 _mesa_enum_to_string(programInterface), index, bufSize);
1315 return;
1316 }
1317
1318 /* Write propCount values until error occurs or bufSize reached. */
1319 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1320 int props_written =
1321 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1322 "glGetProgramResourceiv");
1323
1324 /* Error happened. */
1325 if (props_written == 0)
1326 return;
1327
1328 amount += props_written;
1329 }
1330
1331 /* If <length> is not NULL, the actual number of integer values
1332 * written to <params> will be written to <length>.
1333 */
1334 if (length)
1335 *length = amount;
1336 }