1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
|
/*
* Copyright © 2011 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file shader_query.cpp
* C-to-C++ bridge functions to query GLSL shader data
*
* \author Ian Romanick <ian.d.romanick@intel.com>
*/
#include "main/core.h"
#include "glsl_symbol_table.h"
#include "ir.h"
#include "shaderobj.h"
#include "program/hash_table.h"
#include "../glsl/program.h"
#include "uniforms.h"
#include "main/enums.h"
extern "C" {
#include "shaderapi.h"
}
/**
* Declare convenience functions to return resource data in a given type.
* Warning! this is not type safe so be *very* careful when using these.
*/
#define DECL_RESOURCE_FUNC(name, type) \
const type * RESOURCE_ ## name (gl_program_resource *res) { \
assert(res->Data); \
return (type *) res->Data; \
}
DECL_RESOURCE_FUNC(VAR, ir_variable);
DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_varying_info);
void GLAPIENTRY
_mesa_BindAttribLocation(GLhandleARB program, GLuint index,
const GLcharARB *name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *const shProg =
_mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
if (!shProg)
return;
if (!name)
return;
if (strncmp(name, "gl_", 3) == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glBindAttribLocation(illegal name)");
return;
}
if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
_mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
return;
}
/* Replace the current value if it's already in the list. Add
* VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
* between built-in attributes and user-defined attributes.
*/
shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
/*
* Note that this attribute binding won't go into effect until
* glLinkProgram is called again.
*/
}
static bool
is_active_attrib(const ir_variable *var)
{
if (!var)
return false;
switch (var->data.mode) {
case ir_var_shader_in:
return var->data.location != -1;
case ir_var_system_value:
/* From GL 4.3 core spec, section 11.1.1 (Vertex Attributes):
* "For GetActiveAttrib, all active vertex shader input variables
* are enumerated, including the special built-in inputs gl_VertexID
* and gl_InstanceID."
*/
return var->data.location == SYSTEM_VALUE_VERTEX_ID ||
var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE ||
var->data.location == SYSTEM_VALUE_INSTANCE_ID;
default:
return false;
}
}
void GLAPIENTRY
_mesa_GetActiveAttrib(GLhandleARB program, GLuint desired_index,
GLsizei maxLength, GLsizei * length, GLint * size,
GLenum * type, GLcharARB * name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *shProg;
if (maxLength < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
if (!shProg)
return;
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetActiveAttrib(program not linked)");
return;
}
if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
return;
}
exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
unsigned current_index = 0;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
if (!is_active_attrib(var))
continue;
if (current_index == desired_index) {
const char *var_name = var->name;
/* Since gl_VertexID may be lowered to gl_VertexIDMESA, we need to
* consider gl_VertexIDMESA as gl_VertexID for purposes of checking
* active attributes.
*/
if (var->data.mode == ir_var_system_value &&
var->data.location == SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) {
var_name = "gl_VertexID";
}
_mesa_copy_string(name, maxLength, length, var_name);
if (size)
*size = (var->type->is_array()) ? var->type->length : 1;
if (type)
*type = var->type->gl_type;
return;
}
current_index++;
}
/* If the loop did not return early, the caller must have asked for
* an index that did not exit. Set an error.
*/
_mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
}
/* Locations associated with shader variables (array or non-array) can be
* queried using its base name or using the base name appended with the
* valid array index. For example, in case of below vertex shader, valid
* queries can be made to know the location of "xyz", "array", "array[0]",
* "array[1]", "array[2]" and "array[3]". In this example index reurned
* will be 0, 0, 0, 1, 2, 3 respectively.
*
* [Vertex Shader]
* layout(location=0) in vec4 xyz;
* layout(location=1) in vec4[4] array;
* void main()
* { }
*
* This requirement came up with the addition of ARB_program_interface_query
* to OpenGL 4.3 specification. See page 101 (page 122 of the PDF) for details.
*
* This utility function is used by:
* _mesa_GetAttribLocation
* _mesa_GetFragDataLocation
* _mesa_GetFragDataIndex
*
* Returns 0:
* if the 'name' string matches var->name.
* Returns 'matched index':
* if the 'name' string matches var->name appended with valid array index.
*/
int static inline
get_matching_index(const ir_variable *const var, const char *name) {
unsigned idx = 0;
const char *const paren = strchr(name, '[');
const unsigned len = (paren != NULL) ? paren - name : strlen(name);
if (paren != NULL) {
if (!var->type->is_array())
return -1;
char *endptr;
idx = (unsigned) strtol(paren + 1, &endptr, 10);
const unsigned idx_len = endptr != (paren + 1) ? endptr - paren - 1 : 0;
/* Validate the sub string representing index in 'name' string */
if ((idx > 0 && paren[1] == '0') /* leading zeroes */
|| (idx == 0 && idx_len > 1) /* all zeroes */
|| paren[1] == ' ' /* whitespace */
|| endptr[0] != ']' /* closing brace */
|| endptr[1] != '\0' /* null char */
|| idx_len == 0 /* missing index */
|| idx >= var->type->length) /* exceeding array bound */
return -1;
}
if (strncmp(var->name, name, len) == 0 && var->name[len] == '\0')
return idx;
return -1;
}
GLint GLAPIENTRY
_mesa_GetAttribLocation(GLhandleARB program, const GLcharARB * name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *const shProg =
_mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
if (!shProg) {
return -1;
}
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetAttribLocation(program not linked)");
return -1;
}
if (!name)
return -1;
/* Not having a vertex shader is not an error.
*/
if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
return -1;
exec_list *ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
/* The extra check against VERT_ATTRIB_GENERIC0 is because
* glGetAttribLocation cannot be used on "conventional" attributes.
*
* From page 95 of the OpenGL 3.0 spec:
*
* "If name is not an active attribute, if name is a conventional
* attribute, or if an error occurs, -1 will be returned."
*/
if (var == NULL
|| var->data.mode != ir_var_shader_in
|| var->data.location == -1
|| var->data.location < VERT_ATTRIB_GENERIC0)
continue;
int index = get_matching_index(var, (const char *) name);
if (index >= 0)
return var->data.location + index - VERT_ATTRIB_GENERIC0;
}
return -1;
}
unsigned
_mesa_count_active_attribs(struct gl_shader_program *shProg)
{
if (!shProg->LinkStatus
|| shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
return 0;
}
exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
unsigned i = 0;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
if (!is_active_attrib(var))
continue;
i++;
}
return i;
}
size_t
_mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
{
if (!shProg->LinkStatus
|| shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
return 0;
}
exec_list *const ir = shProg->_LinkedShaders[MESA_SHADER_VERTEX]->ir;
size_t longest = 0;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
if (var == NULL
|| var->data.mode != ir_var_shader_in
|| var->data.location == -1)
continue;
const size_t len = strlen(var->name);
if (len >= longest)
longest = len + 1;
}
return longest;
}
void GLAPIENTRY
_mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
const GLchar *name)
{
_mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
}
void GLAPIENTRY
_mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
GLuint index, const GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *const shProg =
_mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
if (!shProg)
return;
if (!name)
return;
if (strncmp(name, "gl_", 3) == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
return;
}
if (index > 1) {
_mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
return;
}
if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
_mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
return;
}
if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
_mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
return;
}
/* Replace the current value if it's already in the list. Add
* FRAG_RESULT_DATA0 because that's how the linker differentiates
* between built-in attributes and user-defined attributes.
*/
shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
shProg->FragDataIndexBindings->put(index, name);
/*
* Note that this binding won't go into effect until
* glLinkProgram is called again.
*/
}
GLint GLAPIENTRY
_mesa_GetFragDataIndex(GLuint program, const GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *const shProg =
_mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
if (!shProg) {
return -1;
}
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataIndex(program not linked)");
return -1;
}
if (!name)
return -1;
if (strncmp(name, "gl_", 3) == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataIndex(illegal name)");
return -1;
}
/* Not having a fragment shader is not an error.
*/
if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
return -1;
exec_list *ir = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
/* The extra check against FRAG_RESULT_DATA0 is because
* glGetFragDataLocation cannot be used on "conventional" attributes.
*
* From page 95 of the OpenGL 3.0 spec:
*
* "If name is not an active attribute, if name is a conventional
* attribute, or if an error occurs, -1 will be returned."
*/
if (var == NULL
|| var->data.mode != ir_var_shader_out
|| var->data.location == -1
|| var->data.location < FRAG_RESULT_DATA0)
continue;
if (get_matching_index(var, (const char *) name) >= 0)
return var->data.index;
}
return -1;
}
GLint GLAPIENTRY
_mesa_GetFragDataLocation(GLuint program, const GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *const shProg =
_mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
if (!shProg) {
return -1;
}
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataLocation(program not linked)");
return -1;
}
if (!name)
return -1;
if (strncmp(name, "gl_", 3) == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetFragDataLocation(illegal name)");
return -1;
}
/* Not having a fragment shader is not an error.
*/
if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
return -1;
exec_list *ir = shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir;
foreach_in_list(ir_instruction, node, ir) {
const ir_variable *const var = node->as_variable();
/* The extra check against FRAG_RESULT_DATA0 is because
* glGetFragDataLocation cannot be used on "conventional" attributes.
*
* From page 95 of the OpenGL 3.0 spec:
*
* "If name is not an active attribute, if name is a conventional
* attribute, or if an error occurs, -1 will be returned."
*/
if (var == NULL
|| var->data.mode != ir_var_shader_out
|| var->data.location == -1
|| var->data.location < FRAG_RESULT_DATA0)
continue;
int index = get_matching_index(var, (const char *) name);
if (index >= 0)
return var->data.location + index - FRAG_RESULT_DATA0;
}
return -1;
}
const char*
_mesa_program_resource_name(struct gl_program_resource *res)
{
switch (res->Type) {
case GL_UNIFORM_BLOCK:
return RESOURCE_UBO(res)->Name;
case GL_TRANSFORM_FEEDBACK_VARYING:
return RESOURCE_XFB(res)->Name;
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
return RESOURCE_VAR(res)->name;
case GL_UNIFORM:
return RESOURCE_UNI(res)->name;
default:
assert(!"support for resource type not implemented");
}
return NULL;
}
unsigned
_mesa_program_resource_array_size(struct gl_program_resource *res)
{
switch (res->Type) {
case GL_TRANSFORM_FEEDBACK_VARYING:
return RESOURCE_XFB(res)->Size > 1 ?
RESOURCE_XFB(res)->Size : 0;
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
return RESOURCE_VAR(res)->data.max_array_access;
case GL_UNIFORM:
return RESOURCE_UNI(res)->array_elements;
case GL_ATOMIC_COUNTER_BUFFER:
case GL_UNIFORM_BLOCK:
return 0;
default:
assert(!"support for resource type not implemented");
}
return 0;
}
static int
array_index_of_resource(struct gl_program_resource *res,
const char *name)
{
assert(res->Data);
switch (res->Type) {
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
return get_matching_index(RESOURCE_VAR(res), name);
default:
assert(!"support for resource type not implemented");
}
}
/* Find a program resource with specific name in given interface.
*/
struct gl_program_resource *
_mesa_program_resource_find_name(struct gl_shader_program *shProg,
GLenum interface, const char *name)
{
struct gl_program_resource *res = shProg->ProgramResourceList;
for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
if (res->Type != interface)
continue;
/* Resource basename. */
const char *rname = _mesa_program_resource_name(res);
unsigned baselen = strlen(rname);
switch (interface) {
case GL_TRANSFORM_FEEDBACK_VARYING:
case GL_UNIFORM_BLOCK:
case GL_UNIFORM:
if (strncmp(rname, name, baselen) == 0) {
/* Basename match, check if array or struct. */
if (name[baselen] == '\0' ||
name[baselen] == '[' ||
name[baselen] == '.') {
return res;
}
}
break;
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
if (array_index_of_resource(res, name) >= 0)
return res;
break;
default:
assert(!"not implemented for given interface");
}
}
return NULL;
}
static GLuint
calc_resource_index(struct gl_shader_program *shProg,
struct gl_program_resource *res)
{
unsigned i;
GLuint index = 0;
for (i = 0; i < shProg->NumProgramResourceList; i++) {
if (&shProg->ProgramResourceList[i] == res)
return index;
if (shProg->ProgramResourceList[i].Type == res->Type)
index++;
}
return GL_INVALID_INDEX;
}
/**
* Calculate index for the given resource.
*/
GLuint
_mesa_program_resource_index(struct gl_shader_program *shProg,
struct gl_program_resource *res)
{
if (!res)
return GL_INVALID_INDEX;
switch (res->Type) {
case GL_UNIFORM_BLOCK:
return RESOURCE_UBO(res)- shProg->UniformBlocks;
case GL_ATOMIC_COUNTER_BUFFER:
return RESOURCE_ATC(res) - shProg->AtomicBuffers;
case GL_TRANSFORM_FEEDBACK_VARYING:
default:
return calc_resource_index(shProg, res);
}
}
/* Find a program resource with specific index in given interface.
*/
struct gl_program_resource *
_mesa_program_resource_find_index(struct gl_shader_program *shProg,
GLenum interface, GLuint index)
{
struct gl_program_resource *res = shProg->ProgramResourceList;
int idx = -1;
for (unsigned i = 0; i < shProg->NumProgramResourceList; i++, res++) {
if (res->Type != interface)
continue;
switch (res->Type) {
case GL_UNIFORM_BLOCK:
case GL_ATOMIC_COUNTER_BUFFER:
if (_mesa_program_resource_index(shProg, res) == index)
return res;
case GL_TRANSFORM_FEEDBACK_VARYING:
case GL_PROGRAM_INPUT:
case GL_PROGRAM_OUTPUT:
case GL_UNIFORM:
if (++idx == (int) index)
return res;
break;
default:
assert(!"not implemented for given interface");
}
}
return NULL;
}
/* Get full name of a program resource.
*/
bool
_mesa_get_program_resource_name(struct gl_shader_program *shProg,
GLenum interface, GLuint index,
GLsizei bufSize, GLsizei *length,
GLchar *name, const char *caller)
{
GET_CURRENT_CONTEXT(ctx);
/* Find resource with given interface and index. */
struct gl_program_resource *res =
_mesa_program_resource_find_index(shProg, interface, index);
/* The error INVALID_VALUE is generated if <index> is greater than
* or equal to the number of entries in the active resource list for
* <programInterface>.
*/
if (!res) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
return false;
}
if (bufSize < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
return false;
}
GLsizei localLength;
if (length == NULL)
length = &localLength;
_mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
/* Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
* spec says:
*
* "If the active uniform is an array, the uniform name returned in
* name will always be the name of the uniform array appended with
* "[0]"."
*
* The same text also appears in the OpenGL 4.2 spec. It does not,
* however, appear in any previous spec. Previous specifications are
* ambiguous in this regard. However, either name can later be passed
* to glGetUniformLocation (and related APIs), so there shouldn't be any
* harm in always appending "[0]" to uniform array names.
*
* Geometry shader stage has different naming convention where the 'normal'
* condition is an array, therefore for variables referenced in geometry
* stage we do not add '[0]'.
*
* Note, that TCS outputs and TES inputs should not have index appended
* either.
*/
bool add_index = !(((interface == GL_PROGRAM_INPUT) &&
res->StageReferences & (1 << MESA_SHADER_GEOMETRY)));
if (add_index && _mesa_program_resource_array_size(res)) {
int i;
/* The comparison is strange because *length does *NOT* include the
* terminating NUL, but maxLength does.
*/
for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
name[*length + i] = "[0]"[i];
name[*length + i] = '\0';
*length += i;
}
return true;
}
static GLint
program_resource_location(struct gl_shader_program *shProg,
struct gl_program_resource *res, const char *name)
{
unsigned index, offset;
int array_index = -1;
if (res->Type == GL_PROGRAM_INPUT || res->Type == GL_PROGRAM_OUTPUT) {
array_index = array_index_of_resource(res, name);
if (array_index < 0)
return -1;
}
/* VERT_ATTRIB_GENERIC0 and FRAG_RESULT_DATA0 are decremented as these
* offsets are used internally to differentiate between built-in attributes
* and user-defined attributes.
*/
switch (res->Type) {
case GL_PROGRAM_INPUT:
return RESOURCE_VAR(res)->data.location + array_index - VERT_ATTRIB_GENERIC0;
case GL_PROGRAM_OUTPUT:
return RESOURCE_VAR(res)->data.location + array_index - FRAG_RESULT_DATA0;
case GL_UNIFORM:
index = _mesa_get_uniform_location(shProg, name, &offset);
if (index == GL_INVALID_INDEX)
return -1;
/* From the GL_ARB_uniform_buffer_object spec:
*
* "The value -1 will be returned if <name> does not correspond to an
* active uniform variable name in <program>, if <name> is associated
* with a named uniform block, or if <name> starts with the reserved
* prefix "gl_"."
*/
if (RESOURCE_UNI(res)->block_index != -1 ||
RESOURCE_UNI(res)->atomic_buffer_index != -1)
return -1;
/* location in remap table + array element offset */
return RESOURCE_UNI(res)->remap_location + offset;
default:
return -1;
}
}
/**
* Function implements following location queries:
* glGetAttribLocation
* glGetFragDataLocation
* glGetUniformLocation
*/
GLint
_mesa_program_resource_location(struct gl_shader_program *shProg,
GLenum interface, const char *name)
{
struct gl_program_resource *res =
_mesa_program_resource_find_name(shProg, interface, name);
/* Resource not found. */
if (!res)
return -1;
return program_resource_location(shProg, res, name);
}
/**
* Function implements following index queries:
* glGetFragDataIndex
*/
GLint
_mesa_program_resource_location_index(struct gl_shader_program *shProg,
GLenum interface, const char *name)
{
struct gl_program_resource *res =
_mesa_program_resource_find_name(shProg, interface, name);
/* Non-existent variable or resource is not referenced by fragment stage. */
if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
return -1;
return RESOURCE_VAR(res)->data.index;
}
|