16b43e840c7aa2cb3c00cc934cb3315b52f19692
[mesa.git] / src / mesa / main / shader_query.cpp
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file shader_query.cpp
26 * C-to-C++ bridge functions to query GLSL shader data
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
31 #include "main/context.h"
32 #include "main/core.h"
33 #include "glsl_symbol_table.h"
34 #include "ir.h"
35 #include "shaderapi.h"
36 #include "shaderobj.h"
37 #include "program/hash_table.h"
38 #include "../glsl/program.h"
39 #include "uniforms.h"
40 #include "main/enums.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_UNIFORM_BLOCK:
615 case GL_SHADER_STORAGE_BLOCK:
616 return RESOURCE_UBO(res)- shProg->UniformBlocks;
617 case GL_ATOMIC_COUNTER_BUFFER:
618 return RESOURCE_ATC(res) - shProg->AtomicBuffers;
619 case GL_TRANSFORM_FEEDBACK_VARYING:
620 default:
621 return calc_resource_index(shProg, res);
622 }
623 }
624
625 /* Find a program resource with specific index in given interface.
626 */
627 struct gl_program_resource *
628 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
629 GLenum programInterface, GLuint index)
630 {
631 struct gl_program_resource *res = shProg->ProgramResourceList;
632 int idx = -1;
633
634 for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
635 if (res->Type != programInterface)
636 continue;
637
638 switch (res->Type) {
639 case GL_UNIFORM_BLOCK:
640 case GL_ATOMIC_COUNTER_BUFFER:
641 case GL_SHADER_STORAGE_BLOCK:
642 if (_mesa_program_resource_index(shProg, res) == index)
643 return res;
644 break;
645 case GL_TRANSFORM_FEEDBACK_VARYING:
646 case GL_PROGRAM_INPUT:
647 case GL_PROGRAM_OUTPUT:
648 case GL_UNIFORM:
649 case GL_VERTEX_SUBROUTINE_UNIFORM:
650 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
651 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
652 case GL_COMPUTE_SUBROUTINE_UNIFORM:
653 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
654 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
655 case GL_VERTEX_SUBROUTINE:
656 case GL_GEOMETRY_SUBROUTINE:
657 case GL_FRAGMENT_SUBROUTINE:
658 case GL_COMPUTE_SUBROUTINE:
659 case GL_TESS_CONTROL_SUBROUTINE:
660 case GL_TESS_EVALUATION_SUBROUTINE:
661 case GL_BUFFER_VARIABLE:
662 if (++idx == (int) index)
663 return res;
664 break;
665 default:
666 assert(!"not implemented for given interface");
667 }
668 }
669 return NULL;
670 }
671
672 /* Function returns if resource name is expected to have index
673 * appended into it.
674 *
675 *
676 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
677 * spec says:
678 *
679 * "If the active uniform is an array, the uniform name returned in
680 * name will always be the name of the uniform array appended with
681 * "[0]"."
682 *
683 * The same text also appears in the OpenGL 4.2 spec. It does not,
684 * however, appear in any previous spec. Previous specifications are
685 * ambiguous in this regard. However, either name can later be passed
686 * to glGetUniformLocation (and related APIs), so there shouldn't be any
687 * harm in always appending "[0]" to uniform array names.
688 *
689 * Geometry shader stage has different naming convention where the 'normal'
690 * condition is an array, therefore for variables referenced in geometry
691 * stage we do not add '[0]'.
692 *
693 * Note, that TCS outputs and TES inputs should not have index appended
694 * either.
695 */
696 static bool
697 add_index_to_name(struct gl_program_resource *res)
698 {
699 bool add_index = !(((res->Type == GL_PROGRAM_INPUT) &&
700 res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
701
702 /* Transform feedback varyings have array index already appended
703 * in their names.
704 */
705 if (res->Type == GL_TRANSFORM_FEEDBACK_VARYING)
706 add_index = false;
707
708 return add_index;
709 }
710
711 /* Get name length of a program resource. This consists of
712 * base name + 3 for '[0]' if resource is an array.
713 */
714 extern unsigned
715 _mesa_program_resource_name_len(struct gl_program_resource *res)
716 {
717 unsigned length = strlen(_mesa_program_resource_name(res));
718 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
719 length += 3;
720 return length;
721 }
722
723 /* Get full name of a program resource.
724 */
725 bool
726 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
727 GLenum programInterface, GLuint index,
728 GLsizei bufSize, GLsizei *length,
729 GLchar *name, const char *caller)
730 {
731 GET_CURRENT_CONTEXT(ctx);
732
733 /* Find resource with given interface and index. */
734 struct gl_program_resource *res =
735 _mesa_program_resource_find_index(shProg, programInterface, index);
736
737 /* The error INVALID_VALUE is generated if <index> is greater than
738 * or equal to the number of entries in the active resource list for
739 * <programInterface>.
740 */
741 if (!res) {
742 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
743 return false;
744 }
745
746 if (bufSize < 0) {
747 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
748 return false;
749 }
750
751 GLsizei localLength;
752
753 if (length == NULL)
754 length = &localLength;
755
756 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
757
758 if (_mesa_program_resource_array_size(res) && add_index_to_name(res)) {
759 int i;
760
761 /* The comparison is strange because *length does *NOT* include the
762 * terminating NUL, but maxLength does.
763 */
764 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
765 name[*length + i] = "[0]"[i];
766
767 name[*length + i] = '\0';
768 *length += i;
769 }
770 return true;
771 }
772
773 static GLint
774 program_resource_location(struct gl_shader_program *shProg,
775 struct gl_program_resource *res, const char *name,
776 unsigned array_index)
777 {
778 /* Built-in locations should report GL_INVALID_INDEX. */
779 if (is_gl_identifier(name))
780 return GL_INVALID_INDEX;
781
782 /* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
783 * offsets are used internally to differentiate between built-in attributes
784 * and user-defined attributes.
785 */
786 switch (res->Type) {
787 case GL_PROGRAM_INPUT:
788 /* If the input is an array, fail if the index is out of bounds. */
789 if (array_index > 0
790 && array_index >= RESOURCE_VAR(res)->type->length) {
791 return -1;
792 }
793 return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
794 case GL_PROGRAM_OUTPUT:
795 /* If the output is an array, fail if the index is out of bounds. */
796 if (array_index > 0
797 && array_index >= RESOURCE_VAR(res)->type->length) {
798 return -1;
799 }
800 return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
801 case GL_UNIFORM:
802 /* If the uniform is built-in, fail. */
803 if (RESOURCE_UNI(res)->builtin)
804 return -1;
805
806 /* From the GL_ARB_uniform_buffer_object spec:
807 *
808 * "The value -1 will be returned if <name> does not correspond to an
809 * active uniform variable name in <program>, if <name> is associated
810 * with a named uniform block, or if <name> starts with the reserved
811 * prefix "gl_"."
812 */
813 if (RESOURCE_UNI(res)->block_index != -1 ||
814 RESOURCE_UNI(res)->atomic_buffer_index != -1)
815 return -1;
816
817 /* fallthrough */
818 case GL_VERTEX_SUBROUTINE_UNIFORM:
819 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
820 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
821 case GL_COMPUTE_SUBROUTINE_UNIFORM:
822 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
823 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
824 /* If the uniform is an array, fail if the index is out of bounds. */
825 if (array_index > 0
826 && array_index >= RESOURCE_UNI(res)->array_elements) {
827 return -1;
828 }
829
830 /* location in remap table + array element offset */
831 return RESOURCE_UNI(res)->remap_location + array_index;
832 default:
833 return -1;
834 }
835 }
836
837 static char*
838 get_top_level_name(const char *name)
839 {
840 const char *first_dot = strchr(name, '.');
841 const char *first_square_bracket = strchr(name, '[');
842 int name_size = 0;
843 /* From ARB_program_interface_query spec:
844 *
845 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer identifying the
846 * number of active array elements of the top-level shader storage block
847 * member containing to the active variable is written to <params>. If the
848 * top-level block member is not declared as an array, the value one is
849 * written to <params>. If the top-level block member is an array with no
850 * declared size, the value zero is written to <params>.
851 */
852
853 /* The buffer variable is on top level.*/
854 if (!first_square_bracket && !first_dot)
855 name_size = strlen(name);
856 else if ((!first_square_bracket ||
857 (first_dot && first_dot < first_square_bracket)))
858 name_size = first_dot - name;
859 else
860 name_size = first_square_bracket - name;
861
862 return strndup(name, name_size);
863 }
864
865 static char*
866 get_var_name(const char *name)
867 {
868 const char *first_dot = strchr(name, '.');
869
870 if (!first_dot)
871 return strdup(name);
872
873 return strndup(first_dot+1, strlen(first_dot) - 1);
874 }
875
876 static GLint
877 program_resource_top_level_array_size(struct gl_shader_program *shProg,
878 struct gl_program_resource *res,
879 const char *name)
880 {
881 int block_index = RESOURCE_UNI(res)->block_index;
882 int array_size = -1;
883 char *var_name = get_top_level_name(name);
884 char *interface_name =
885 get_top_level_name(shProg->UniformBlocks[block_index].Name);
886
887 if (strcmp(var_name, interface_name) == 0) {
888 /* Deal with instanced array of SSBOs */
889 char *temp_name = get_var_name(name);
890 free(var_name);
891 var_name = get_top_level_name(temp_name);
892 free(temp_name);
893 }
894
895 for (unsigned i = 0; i < shProg->NumShaders; i++) {
896 if (shProg->Shaders[i] == NULL)
897 continue;
898
899 const gl_shader *stage = shProg->Shaders[i];
900 foreach_in_list(ir_instruction, node, stage->ir) {
901 ir_variable *var = node->as_variable();
902 if (!var || !var->get_interface_type() ||
903 var->data.mode != ir_var_shader_storage)
904 continue;
905
906 const glsl_type *interface = var->get_interface_type();
907
908 if (strcmp(interface_name, interface->name) != 0)
909 continue;
910
911 for (unsigned i = 0; i < interface->length; i++) {
912 const glsl_struct_field *field = &interface->fields.structure[i];
913 if (strcmp(field->name, var_name) != 0)
914 continue;
915 /* From GL_ARB_program_interface_query spec:
916 *
917 * "For the property TOP_LEVEL_ARRAY_SIZE, a single integer
918 * identifying the number of active array elements of the top-level
919 * shader storage block member containing to the active variable is
920 * written to <params>. If the top-level block member is not
921 * declared as an array, the value one is written to <params>. If
922 * the top-level block member is an array with no declared size,
923 * the value zero is written to <params>.
924 */
925 if (field->type->is_unsized_array())
926 array_size = 0;
927 else if (field->type->is_array())
928 array_size = field->type->length;
929 else
930 array_size = 1;
931 goto found_top_level_array_size;
932 }
933 }
934 }
935 found_top_level_array_size:
936 free(interface_name);
937 free(var_name);
938 return array_size;
939 }
940
941 static GLint
942 program_resource_top_level_array_stride(struct gl_shader_program *shProg,
943 struct gl_program_resource *res,
944 const char *name)
945 {
946 int block_index = RESOURCE_UNI(res)->block_index;
947 int array_stride = -1;
948 char *var_name = get_top_level_name(name);
949 char *interface_name =
950 get_top_level_name(shProg->UniformBlocks[block_index].Name);
951
952 if (strcmp(var_name, interface_name) == 0) {
953 /* Deal with instanced array of SSBOs */
954 char *temp_name = get_var_name(name);
955 free(var_name);
956 var_name = get_top_level_name(temp_name);
957 free(temp_name);
958 }
959
960 for (unsigned i = 0; i < shProg->NumShaders; i++) {
961 if (shProg->Shaders[i] == NULL)
962 continue;
963
964 const gl_shader *stage = shProg->Shaders[i];
965 foreach_in_list(ir_instruction, node, stage->ir) {
966 ir_variable *var = node->as_variable();
967 if (!var || !var->get_interface_type() ||
968 var->data.mode != ir_var_shader_storage)
969 continue;
970
971 const glsl_type *interface = var->get_interface_type();
972
973 if (strcmp(interface_name, interface->name) != 0) {
974 continue;
975 }
976
977 for (unsigned i = 0; i < interface->length; i++) {
978 const glsl_struct_field *field = &interface->fields.structure[i];
979 if (strcmp(field->name, var_name) != 0)
980 continue;
981 /* From GL_ARB_program_interface_query:
982 *
983 * "For the property TOP_LEVEL_ARRAY_STRIDE, a single integer
984 * identifying the stride between array elements of the top-level
985 * shader storage block member containing the active variable is
986 * written to <params>. For top-level block members declared as
987 * arrays, the value written is the difference, in basic machine
988 * units, between the offsets of the active variable for
989 * consecutive elements in the top-level array. For top-level
990 * block members not declared as an array, zero is written to
991 * <params>."
992 */
993 if (field->type->is_array()) {
994 const enum glsl_matrix_layout matrix_layout =
995 glsl_matrix_layout(field->matrix_layout);
996 bool row_major = matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
997 const glsl_type *array_type = field->type->fields.array;
998
999 if (interface->interface_packing != GLSL_INTERFACE_PACKING_STD430) {
1000 if (array_type->is_record()) {
1001 array_stride = array_type->std140_size(row_major);
1002 array_stride = glsl_align(array_stride, 16);
1003 } else {
1004 unsigned element_base_align = 0;
1005 element_base_align = array_type->std140_base_alignment(row_major);
1006 array_stride = MAX2(element_base_align, 16);
1007 }
1008 } else {
1009 array_stride = array_type->std430_array_stride(row_major);
1010 }
1011 } else {
1012 array_stride = 0;
1013 }
1014 goto found_top_level_array_size;
1015 }
1016 }
1017 }
1018 found_top_level_array_size:
1019 free(interface_name);
1020 free(var_name);
1021 return array_stride;
1022 }
1023
1024 /**
1025 * Function implements following location queries:
1026 * glGetUniformLocation
1027 */
1028 GLint
1029 _mesa_program_resource_location(struct gl_shader_program *shProg,
1030 GLenum programInterface, const char *name)
1031 {
1032 unsigned array_index = 0;
1033 struct gl_program_resource *res =
1034 _mesa_program_resource_find_name(shProg, programInterface, name,
1035 &array_index);
1036
1037 /* Resource not found. */
1038 if (!res)
1039 return -1;
1040
1041 return program_resource_location(shProg, res, name, array_index);
1042 }
1043
1044 /**
1045 * Function implements following index queries:
1046 * glGetFragDataIndex
1047 */
1048 GLint
1049 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1050 GLenum programInterface, const char *name)
1051 {
1052 struct gl_program_resource *res =
1053 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1054
1055 /* Non-existent variable or resource is not referenced by fragment stage. */
1056 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1057 return -1;
1058
1059 return RESOURCE_VAR(res)->data.index;
1060 }
1061
1062 static uint8_t
1063 stage_from_enum(GLenum ref)
1064 {
1065 switch (ref) {
1066 case GL_REFERENCED_BY_VERTEX_SHADER:
1067 return MESA_SHADER_VERTEX;
1068 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1069 return MESA_SHADER_TESS_CTRL;
1070 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1071 return MESA_SHADER_TESS_EVAL;
1072 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1073 return MESA_SHADER_GEOMETRY;
1074 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1075 return MESA_SHADER_FRAGMENT;
1076 case GL_REFERENCED_BY_COMPUTE_SHADER:
1077 return MESA_SHADER_COMPUTE;
1078 default:
1079 assert(!"shader stage not supported");
1080 return MESA_SHADER_STAGES;
1081 }
1082 }
1083
1084 /**
1085 * Check if resource is referenced by given 'referenced by' stage enum.
1086 * ATC and UBO resources hold stage references of their own.
1087 */
1088 static bool
1089 is_resource_referenced(struct gl_shader_program *shProg,
1090 struct gl_program_resource *res,
1091 GLuint index, uint8_t stage)
1092 {
1093 /* First, check if we even have such a stage active. */
1094 if (!shProg->_LinkedShaders[stage])
1095 return false;
1096
1097 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1098 return RESOURCE_ATC(res)->StageReferences[stage];
1099
1100 if (res->Type == GL_UNIFORM_BLOCK || res->Type == GL_SHADER_STORAGE_BLOCK)
1101 return shProg->UniformBlockStageIndex[stage][index] != -1;
1102
1103 return res->StageReferences & (1 << stage);
1104 }
1105
1106 static unsigned
1107 get_buffer_property(struct gl_shader_program *shProg,
1108 struct gl_program_resource *res, const GLenum prop,
1109 GLint *val, const char *caller)
1110 {
1111 GET_CURRENT_CONTEXT(ctx);
1112 if (res->Type != GL_UNIFORM_BLOCK &&
1113 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1114 res->Type != GL_SHADER_STORAGE_BLOCK)
1115 goto invalid_operation;
1116
1117 if (res->Type == GL_UNIFORM_BLOCK) {
1118 switch (prop) {
1119 case GL_BUFFER_BINDING:
1120 *val = RESOURCE_UBO(res)->Binding;
1121 return 1;
1122 case GL_BUFFER_DATA_SIZE:
1123 *val = RESOURCE_UBO(res)->UniformBufferSize;
1124 return 1;
1125 case GL_NUM_ACTIVE_VARIABLES:
1126 *val = 0;
1127 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1128 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1129 struct gl_program_resource *uni =
1130 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1131 NULL);
1132 if (!uni)
1133 continue;
1134 (*val)++;
1135 }
1136 return 1;
1137 case GL_ACTIVE_VARIABLES:
1138 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1139 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1140 struct gl_program_resource *uni =
1141 _mesa_program_resource_find_name(shProg, GL_UNIFORM, iname,
1142 NULL);
1143 if (!uni)
1144 continue;
1145 *val++ =
1146 _mesa_program_resource_index(shProg, uni);
1147 }
1148 return RESOURCE_UBO(res)->NumUniforms;
1149 }
1150 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1151 switch (prop) {
1152 case GL_BUFFER_BINDING:
1153 *val = RESOURCE_UBO(res)->Binding;
1154 return 1;
1155 case GL_BUFFER_DATA_SIZE:
1156 *val = RESOURCE_UBO(res)->UniformBufferSize;
1157 return 1;
1158 case GL_NUM_ACTIVE_VARIABLES:
1159 *val = 0;
1160 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1161 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1162 struct gl_program_resource *uni =
1163 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1164 iname, NULL);
1165 if (!uni)
1166 continue;
1167 (*val)++;
1168 }
1169 return 1;
1170 case GL_ACTIVE_VARIABLES:
1171 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1172 const char *iname = RESOURCE_UBO(res)->Uniforms[i].IndexName;
1173 struct gl_program_resource *uni =
1174 _mesa_program_resource_find_name(shProg, GL_BUFFER_VARIABLE,
1175 iname, NULL);
1176 if (!uni)
1177 continue;
1178 *val++ =
1179 _mesa_program_resource_index(shProg, uni);
1180 }
1181 return RESOURCE_UBO(res)->NumUniforms;
1182 }
1183 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1184 switch (prop) {
1185 case GL_BUFFER_BINDING:
1186 *val = RESOURCE_ATC(res)->Binding;
1187 return 1;
1188 case GL_BUFFER_DATA_SIZE:
1189 *val = RESOURCE_ATC(res)->MinimumSize;
1190 return 1;
1191 case GL_NUM_ACTIVE_VARIABLES:
1192 *val = RESOURCE_ATC(res)->NumUniforms;
1193 return 1;
1194 case GL_ACTIVE_VARIABLES:
1195 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++)
1196 *val++ = RESOURCE_ATC(res)->Uniforms[i];
1197 return RESOURCE_ATC(res)->NumUniforms;
1198 }
1199 }
1200 assert(!"support for property type not implemented");
1201
1202 invalid_operation:
1203 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1204 _mesa_enum_to_string(res->Type),
1205 _mesa_enum_to_string(prop));
1206
1207 return 0;
1208 }
1209
1210 unsigned
1211 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1212 struct gl_program_resource *res, GLuint index,
1213 const GLenum prop, GLint *val, const char *caller)
1214 {
1215 GET_CURRENT_CONTEXT(ctx);
1216
1217 #define VALIDATE_TYPE(type)\
1218 if (res->Type != type)\
1219 goto invalid_operation;
1220
1221 #define VALIDATE_TYPE_2(type1, type2)\
1222 if (res->Type != type1 && res->Type != type2)\
1223 goto invalid_operation;
1224
1225 switch(prop) {
1226 case GL_NAME_LENGTH:
1227 switch (res->Type) {
1228 case GL_ATOMIC_COUNTER_BUFFER:
1229 goto invalid_operation;
1230 default:
1231 /* Resource name length + terminator. */
1232 *val = _mesa_program_resource_name_len(res) + 1;
1233 }
1234 return 1;
1235 case GL_TYPE:
1236 switch (res->Type) {
1237 case GL_UNIFORM:
1238 case GL_BUFFER_VARIABLE:
1239 *val = RESOURCE_UNI(res)->type->gl_type;
1240 return 1;
1241 case GL_PROGRAM_INPUT:
1242 case GL_PROGRAM_OUTPUT:
1243 *val = RESOURCE_VAR(res)->type->gl_type;
1244 return 1;
1245 case GL_TRANSFORM_FEEDBACK_VARYING:
1246 *val = RESOURCE_XFB(res)->Type;
1247 return 1;
1248 default:
1249 goto invalid_operation;
1250 }
1251 case GL_ARRAY_SIZE:
1252 switch (res->Type) {
1253 case GL_UNIFORM:
1254 case GL_BUFFER_VARIABLE:
1255 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1256 return 1;
1257 case GL_PROGRAM_INPUT:
1258 case GL_PROGRAM_OUTPUT:
1259 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1260 return 1;
1261 case GL_TRANSFORM_FEEDBACK_VARYING:
1262 *val = MAX2(RESOURCE_XFB(res)->Size, 1);
1263 return 1;
1264 default:
1265 goto invalid_operation;
1266 }
1267 case GL_OFFSET:
1268 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1269 *val = RESOURCE_UNI(res)->offset;
1270 return 1;
1271 case GL_BLOCK_INDEX:
1272 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1273 *val = RESOURCE_UNI(res)->block_index;
1274 return 1;
1275 case GL_ARRAY_STRIDE:
1276 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1277 *val = RESOURCE_UNI(res)->array_stride;
1278 return 1;
1279 case GL_MATRIX_STRIDE:
1280 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1281 *val = RESOURCE_UNI(res)->matrix_stride;
1282 return 1;
1283 case GL_IS_ROW_MAJOR:
1284 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1285 *val = RESOURCE_UNI(res)->row_major;
1286 return 1;
1287 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1288 VALIDATE_TYPE(GL_UNIFORM);
1289 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1290 return 1;
1291 case GL_BUFFER_BINDING:
1292 case GL_BUFFER_DATA_SIZE:
1293 case GL_NUM_ACTIVE_VARIABLES:
1294 case GL_ACTIVE_VARIABLES:
1295 return get_buffer_property(shProg, res, prop, val, caller);
1296 case GL_REFERENCED_BY_COMPUTE_SHADER:
1297 if (!_mesa_has_compute_shaders(ctx))
1298 goto invalid_enum;
1299 /* fallthrough */
1300 case GL_REFERENCED_BY_VERTEX_SHADER:
1301 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1302 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1303 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1304 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1305 switch (res->Type) {
1306 case GL_UNIFORM:
1307 case GL_PROGRAM_INPUT:
1308 case GL_PROGRAM_OUTPUT:
1309 case GL_UNIFORM_BLOCK:
1310 case GL_BUFFER_VARIABLE:
1311 case GL_SHADER_STORAGE_BLOCK:
1312 case GL_ATOMIC_COUNTER_BUFFER:
1313 *val = is_resource_referenced(shProg, res, index,
1314 stage_from_enum(prop));
1315 return 1;
1316 default:
1317 goto invalid_operation;
1318 }
1319 case GL_LOCATION:
1320 switch (res->Type) {
1321 case GL_UNIFORM:
1322 case GL_PROGRAM_INPUT:
1323 case GL_PROGRAM_OUTPUT:
1324 *val = program_resource_location(shProg, res,
1325 _mesa_program_resource_name(res),
1326 0);
1327 return 1;
1328 default:
1329 goto invalid_operation;
1330 }
1331 case GL_LOCATION_INDEX:
1332 if (res->Type != GL_PROGRAM_OUTPUT)
1333 goto invalid_operation;
1334 *val = RESOURCE_VAR(res)->data.index;
1335 return 1;
1336
1337 case GL_NUM_COMPATIBLE_SUBROUTINES:
1338 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1339 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1340 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1341 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1342 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1343 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1344 goto invalid_operation;
1345 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1346 return 1;
1347 case GL_COMPATIBLE_SUBROUTINES: {
1348 const struct gl_uniform_storage *uni;
1349 struct gl_shader *sh;
1350 unsigned count, i;
1351 int j;
1352
1353 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1354 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1355 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1356 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1357 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1358 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1359 goto invalid_operation;
1360 uni = RESOURCE_UNI(res);
1361
1362 sh = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)];
1363 count = 0;
1364 for (i = 0; i < sh->NumSubroutineFunctions; i++) {
1365 struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
1366 for (j = 0; j < fn->num_compat_types; j++) {
1367 if (fn->types[j] == uni->type) {
1368 val[count++] = i;
1369 break;
1370 }
1371 }
1372 }
1373 return count;
1374 }
1375
1376 case GL_TOP_LEVEL_ARRAY_SIZE:
1377 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1378 *val = program_resource_top_level_array_size(shProg, res,
1379 _mesa_program_resource_name(res));
1380 return 1;
1381
1382 case GL_TOP_LEVEL_ARRAY_STRIDE:
1383 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1384 *val = program_resource_top_level_array_stride(shProg, res,
1385 _mesa_program_resource_name(res));
1386 return 1;
1387
1388 /* GL_ARB_tessellation_shader */
1389 case GL_IS_PER_PATCH:
1390 switch (res->Type) {
1391 case GL_PROGRAM_INPUT:
1392 case GL_PROGRAM_OUTPUT:
1393 *val = RESOURCE_VAR(res)->data.patch;
1394 return 1;
1395 default:
1396 goto invalid_operation;
1397 }
1398 default:
1399 goto invalid_enum;
1400 }
1401
1402 #undef VALIDATE_TYPE
1403 #undef VALIDATE_TYPE_2
1404
1405 invalid_enum:
1406 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1407 _mesa_enum_to_string(res->Type),
1408 _mesa_enum_to_string(prop));
1409 return 0;
1410
1411 invalid_operation:
1412 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1413 _mesa_enum_to_string(res->Type),
1414 _mesa_enum_to_string(prop));
1415 return 0;
1416 }
1417
1418 extern void
1419 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1420 GLenum programInterface, GLuint index, GLsizei propCount,
1421 const GLenum *props, GLsizei bufSize,
1422 GLsizei *length, GLint *params)
1423 {
1424 GET_CURRENT_CONTEXT(ctx);
1425 GLint *val = (GLint *) params;
1426 const GLenum *prop = props;
1427 GLsizei amount = 0;
1428
1429 struct gl_program_resource *res =
1430 _mesa_program_resource_find_index(shProg, programInterface, index);
1431
1432 /* No such resource found or bufSize negative. */
1433 if (!res || bufSize < 0) {
1434 _mesa_error(ctx, GL_INVALID_VALUE,
1435 "glGetProgramResourceiv(%s index %d bufSize %d)",
1436 _mesa_enum_to_string(programInterface), index, bufSize);
1437 return;
1438 }
1439
1440 /* Write propCount values until error occurs or bufSize reached. */
1441 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1442 int props_written =
1443 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1444 "glGetProgramResourceiv");
1445
1446 /* Error happened. */
1447 if (props_written == 0)
1448 return;
1449
1450 amount += props_written;
1451 }
1452
1453 /* If <length> is not NULL, the actual number of integer values
1454 * written to <params> will be written to <length>.
1455 */
1456 if (length)
1457 *length = amount;
1458 }