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/enums.h"
33 #include "main/shaderapi.h"
34 #include "main/shaderobj.h"
35 #include "main/uniforms.h"
36 #include "compiler/glsl/glsl_symbol_table.h"
37 #include "compiler/glsl/ir.h"
38 #include "compiler/glsl/program.h"
39 #include "compiler/glsl/string_to_uint_map.h"
40 #include "util/mesa-sha1.h"
41
42 static GLint
43 program_resource_location(struct gl_program_resource *res,
44 unsigned array_index);
45
46 /**
47 * Declare convenience functions to return resource data in a given type.
48 * Warning! this is not type safe so be *very* careful when using these.
49 */
50 #define DECL_RESOURCE_FUNC(name, type) \
51 const type * RESOURCE_ ## name (gl_program_resource *res) { \
52 assert(res->Data); \
53 return (type *) res->Data; \
54 }
55
56 DECL_RESOURCE_FUNC(VAR, gl_shader_variable);
57 DECL_RESOURCE_FUNC(UBO, gl_uniform_block);
58 DECL_RESOURCE_FUNC(UNI, gl_uniform_storage);
59 DECL_RESOURCE_FUNC(ATC, gl_active_atomic_buffer);
60 DECL_RESOURCE_FUNC(XFV, gl_transform_feedback_varying_info);
61 DECL_RESOURCE_FUNC(XFB, gl_transform_feedback_buffer);
62 DECL_RESOURCE_FUNC(SUB, gl_subroutine_function);
63
64 static void
bind_attrib_location(struct gl_context * ctx,struct gl_shader_program * const shProg,GLuint index,const GLchar * name,bool no_error)65 bind_attrib_location(struct gl_context *ctx,
66 struct gl_shader_program *const shProg, GLuint index,
67 const GLchar *name, bool no_error)
68 {
69 if (!name)
70 return;
71
72 if (!no_error) {
73 if (strncmp(name, "gl_", 3) == 0) {
74 _mesa_error(ctx, GL_INVALID_OPERATION,
75 "glBindAttribLocation(illegal name)");
76 return;
77 }
78
79 if (index >= ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs) {
80 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(%u >= %u)",
81 index, ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs);
82 return;
83 }
84 }
85
86 /* Replace the current value if it's already in the list. Add
87 * VERT_ATTRIB_GENERIC0 because that's how the linker differentiates
88 * between built-in attributes and user-defined attributes.
89 */
90 shProg->AttributeBindings->put(index + VERT_ATTRIB_GENERIC0, name);
91
92 /*
93 * Note that this attribute binding won't go into effect until
94 * glLinkProgram is called again.
95 */
96 }
97
98 void GLAPIENTRY
_mesa_BindAttribLocation_no_error(GLuint program,GLuint index,const GLchar * name)99 _mesa_BindAttribLocation_no_error(GLuint program, GLuint index,
100 const GLchar *name)
101 {
102 GET_CURRENT_CONTEXT(ctx);
103
104 struct gl_shader_program *const shProg =
105 _mesa_lookup_shader_program(ctx, program);
106 bind_attrib_location(ctx, shProg, index, name, true);
107 }
108
109 void GLAPIENTRY
_mesa_BindAttribLocation(GLuint program,GLuint index,const GLchar * name)110 _mesa_BindAttribLocation(GLuint program, GLuint index,
111 const GLchar *name)
112 {
113 GET_CURRENT_CONTEXT(ctx);
114
115 struct gl_shader_program *const shProg =
116 _mesa_lookup_shader_program_err(ctx, program, "glBindAttribLocation");
117 if (!shProg)
118 return;
119
120 bind_attrib_location(ctx, shProg, index, name, false);
121 }
122
123 void GLAPIENTRY
_mesa_GetActiveAttrib(GLuint program,GLuint desired_index,GLsizei maxLength,GLsizei * length,GLint * size,GLenum * type,GLchar * name)124 _mesa_GetActiveAttrib(GLuint program, GLuint desired_index,
125 GLsizei maxLength, GLsizei * length, GLint * size,
126 GLenum * type, GLchar * name)
127 {
128 GET_CURRENT_CONTEXT(ctx);
129 struct gl_shader_program *shProg;
130
131 if (maxLength < 0) {
132 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(maxLength < 0)");
133 return;
134 }
135
136 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
137 if (!shProg)
138 return;
139
140 if (!shProg->data->LinkStatus) {
141 _mesa_error(ctx, GL_INVALID_VALUE,
142 "glGetActiveAttrib(program not linked)");
143 return;
144 }
145
146 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
147 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(no vertex shader)");
148 return;
149 }
150
151 struct gl_program_resource *res =
152 _mesa_program_resource_find_index(shProg, GL_PROGRAM_INPUT,
153 desired_index);
154
155 /* User asked for index that does not exist. */
156 if (!res) {
157 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
158 return;
159 }
160
161 const gl_shader_variable *const var = RESOURCE_VAR(res);
162
163 const char *var_name = var->name;
164
165 _mesa_copy_string(name, maxLength, length, var_name);
166
167 if (size)
168 _mesa_program_resource_prop(shProg, res, desired_index, GL_ARRAY_SIZE,
169 size, "glGetActiveAttrib");
170
171 if (type)
172 _mesa_program_resource_prop(shProg, res, desired_index, GL_TYPE,
173 (GLint *) type, "glGetActiveAttrib");
174 }
175
176 GLint GLAPIENTRY
_mesa_GetAttribLocation(GLuint program,const GLchar * name)177 _mesa_GetAttribLocation(GLuint program, const GLchar * name)
178 {
179 GET_CURRENT_CONTEXT(ctx);
180 struct gl_shader_program *const shProg =
181 _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
182
183 if (!shProg) {
184 return -1;
185 }
186
187 if (!shProg->data->LinkStatus) {
188 _mesa_error(ctx, GL_INVALID_OPERATION,
189 "glGetAttribLocation(program not linked)");
190 return -1;
191 }
192
193 if (!name)
194 return -1;
195
196 /* Not having a vertex shader is not an error.
197 */
198 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL)
199 return -1;
200
201 unsigned array_index = 0;
202 struct gl_program_resource *res =
203 _mesa_program_resource_find_name(shProg, GL_PROGRAM_INPUT, name,
204 &array_index);
205
206 if (!res)
207 return -1;
208
209 return program_resource_location(res, array_index);
210 }
211
212 unsigned
_mesa_count_active_attribs(struct gl_shader_program * shProg)213 _mesa_count_active_attribs(struct gl_shader_program *shProg)
214 {
215 if (!shProg->data->LinkStatus
216 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
217 return 0;
218 }
219
220 struct gl_program_resource *res = shProg->data->ProgramResourceList;
221 unsigned count = 0;
222 for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
223 j++, res++) {
224 if (res->Type == GL_PROGRAM_INPUT &&
225 res->StageReferences & (1 << MESA_SHADER_VERTEX))
226 count++;
227 }
228 return count;
229 }
230
231
232 size_t
_mesa_longest_attribute_name_length(struct gl_shader_program * shProg)233 _mesa_longest_attribute_name_length(struct gl_shader_program *shProg)
234 {
235 if (!shProg->data->LinkStatus
236 || shProg->_LinkedShaders[MESA_SHADER_VERTEX] == NULL) {
237 return 0;
238 }
239
240 struct gl_program_resource *res = shProg->data->ProgramResourceList;
241 size_t longest = 0;
242 for (unsigned j = 0; j < shProg->data->NumProgramResourceList;
243 j++, res++) {
244 if (res->Type == GL_PROGRAM_INPUT &&
245 res->StageReferences & (1 << MESA_SHADER_VERTEX)) {
246
247 /* From the ARB_gl_spirv spec:
248 *
249 * "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the
250 * longest active attribute name, including a null terminator, is
251 * returned. If no active attributes exist, zero is returned. If
252 * no name reflection information is available, one is returned."
253 */
254 const size_t length = RESOURCE_VAR(res)->name != NULL ?
255 strlen(RESOURCE_VAR(res)->name) : 0;
256
257 if (length >= longest)
258 longest = length + 1;
259 }
260 }
261
262 return longest;
263 }
264
265 void static
bind_frag_data_location(struct gl_shader_program * const shProg,const char * name,unsigned colorNumber,unsigned index)266 bind_frag_data_location(struct gl_shader_program *const shProg,
267 const char *name, unsigned colorNumber,
268 unsigned index)
269 {
270 /* Replace the current value if it's already in the list. Add
271 * FRAG_RESULT_DATA0 because that's how the linker differentiates
272 * between built-in attributes and user-defined attributes.
273 */
274 shProg->FragDataBindings->put(colorNumber + FRAG_RESULT_DATA0, name);
275 shProg->FragDataIndexBindings->put(index, name);
276
277 /*
278 * Note that this binding won't go into effect until
279 * glLinkProgram is called again.
280 */
281 }
282
283 void GLAPIENTRY
_mesa_BindFragDataLocation(GLuint program,GLuint colorNumber,const GLchar * name)284 _mesa_BindFragDataLocation(GLuint program, GLuint colorNumber,
285 const GLchar *name)
286 {
287 _mesa_BindFragDataLocationIndexed(program, colorNumber, 0, name);
288 }
289
290 void GLAPIENTRY
_mesa_BindFragDataLocation_no_error(GLuint program,GLuint colorNumber,const GLchar * name)291 _mesa_BindFragDataLocation_no_error(GLuint program, GLuint colorNumber,
292 const GLchar *name)
293 {
294 GET_CURRENT_CONTEXT(ctx);
295
296 if (!name)
297 return;
298
299 struct gl_shader_program *const shProg =
300 _mesa_lookup_shader_program(ctx, program);
301
302 bind_frag_data_location(shProg, name, colorNumber, 0);
303 }
304
305 void GLAPIENTRY
_mesa_BindFragDataLocationIndexed(GLuint program,GLuint colorNumber,GLuint index,const GLchar * name)306 _mesa_BindFragDataLocationIndexed(GLuint program, GLuint colorNumber,
307 GLuint index, const GLchar *name)
308 {
309 GET_CURRENT_CONTEXT(ctx);
310
311 struct gl_shader_program *const shProg =
312 _mesa_lookup_shader_program_err(ctx, program, "glBindFragDataLocationIndexed");
313 if (!shProg)
314 return;
315
316 if (!name)
317 return;
318
319 if (strncmp(name, "gl_", 3) == 0) {
320 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFragDataLocationIndexed(illegal name)");
321 return;
322 }
323
324 if (index > 1) {
325 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(index)");
326 return;
327 }
328
329 if (index == 0 && colorNumber >= ctx->Const.MaxDrawBuffers) {
330 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
331 return;
332 }
333
334 if (index == 1 && colorNumber >= ctx->Const.MaxDualSourceDrawBuffers) {
335 _mesa_error(ctx, GL_INVALID_VALUE, "glBindFragDataLocationIndexed(colorNumber)");
336 return;
337 }
338
339 bind_frag_data_location(shProg, name, colorNumber, index);
340 }
341
342 void GLAPIENTRY
_mesa_BindFragDataLocationIndexed_no_error(GLuint program,GLuint colorNumber,GLuint index,const GLchar * name)343 _mesa_BindFragDataLocationIndexed_no_error(GLuint program, GLuint colorNumber,
344 GLuint index, const GLchar *name)
345 {
346 GET_CURRENT_CONTEXT(ctx);
347
348 if (!name)
349 return;
350
351 struct gl_shader_program *const shProg =
352 _mesa_lookup_shader_program(ctx, program);
353
354 bind_frag_data_location(shProg, name, colorNumber, index);
355 }
356
357 GLint GLAPIENTRY
_mesa_GetFragDataIndex(GLuint program,const GLchar * name)358 _mesa_GetFragDataIndex(GLuint program, const GLchar *name)
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 struct gl_shader_program *const shProg =
362 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataIndex");
363
364 if (!shProg) {
365 return -1;
366 }
367
368 if (!shProg->data->LinkStatus) {
369 _mesa_error(ctx, GL_INVALID_OPERATION,
370 "glGetFragDataIndex(program not linked)");
371 return -1;
372 }
373
374 if (!name)
375 return -1;
376
377 if (strncmp(name, "gl_", 3) == 0) {
378 _mesa_error(ctx, GL_INVALID_OPERATION,
379 "glGetFragDataIndex(illegal name)");
380 return -1;
381 }
382
383 /* Not having a fragment shader is not an error.
384 */
385 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
386 return -1;
387
388 return _mesa_program_resource_location_index(shProg, GL_PROGRAM_OUTPUT,
389 name);
390 }
391
392 GLint GLAPIENTRY
_mesa_GetFragDataLocation(GLuint program,const GLchar * name)393 _mesa_GetFragDataLocation(GLuint program, const GLchar *name)
394 {
395 GET_CURRENT_CONTEXT(ctx);
396 struct gl_shader_program *const shProg =
397 _mesa_lookup_shader_program_err(ctx, program, "glGetFragDataLocation");
398
399 if (!shProg) {
400 return -1;
401 }
402
403 if (!shProg->data->LinkStatus) {
404 _mesa_error(ctx, GL_INVALID_OPERATION,
405 "glGetFragDataLocation(program not linked)");
406 return -1;
407 }
408
409 if (!name)
410 return -1;
411
412 if (strncmp(name, "gl_", 3) == 0) {
413 _mesa_error(ctx, GL_INVALID_OPERATION,
414 "glGetFragDataLocation(illegal name)");
415 return -1;
416 }
417
418 /* Not having a fragment shader is not an error.
419 */
420 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT] == NULL)
421 return -1;
422
423 unsigned array_index = 0;
424 struct gl_program_resource *res =
425 _mesa_program_resource_find_name(shProg, GL_PROGRAM_OUTPUT, name,
426 &array_index);
427
428 if (!res)
429 return -1;
430
431 return program_resource_location(res, array_index);
432 }
433
434 const char*
_mesa_program_resource_name(struct gl_program_resource * res)435 _mesa_program_resource_name(struct gl_program_resource *res)
436 {
437 switch (res->Type) {
438 case GL_UNIFORM_BLOCK:
439 case GL_SHADER_STORAGE_BLOCK:
440 return RESOURCE_UBO(res)->Name;
441 case GL_TRANSFORM_FEEDBACK_VARYING:
442 return RESOURCE_XFV(res)->Name;
443 case GL_PROGRAM_INPUT:
444 case GL_PROGRAM_OUTPUT:
445 return RESOURCE_VAR(res)->name;
446 case GL_UNIFORM:
447 case GL_BUFFER_VARIABLE:
448 return RESOURCE_UNI(res)->name;
449 case GL_VERTEX_SUBROUTINE_UNIFORM:
450 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
451 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
452 case GL_COMPUTE_SUBROUTINE_UNIFORM:
453 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
454 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
455 return RESOURCE_UNI(res)->name + MESA_SUBROUTINE_PREFIX_LEN;
456 case GL_VERTEX_SUBROUTINE:
457 case GL_GEOMETRY_SUBROUTINE:
458 case GL_FRAGMENT_SUBROUTINE:
459 case GL_COMPUTE_SUBROUTINE:
460 case GL_TESS_CONTROL_SUBROUTINE:
461 case GL_TESS_EVALUATION_SUBROUTINE:
462 return RESOURCE_SUB(res)->name;
463 default:
464 break;
465 }
466 return NULL;
467 }
468
469
470 unsigned
_mesa_program_resource_array_size(struct gl_program_resource * res)471 _mesa_program_resource_array_size(struct gl_program_resource *res)
472 {
473 switch (res->Type) {
474 case GL_TRANSFORM_FEEDBACK_VARYING:
475 return RESOURCE_XFV(res)->Size > 1 ?
476 RESOURCE_XFV(res)->Size : 0;
477 case GL_PROGRAM_INPUT:
478 case GL_PROGRAM_OUTPUT:
479 return RESOURCE_VAR(res)->type->length;
480 case GL_UNIFORM:
481 case GL_VERTEX_SUBROUTINE_UNIFORM:
482 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
483 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
484 case GL_COMPUTE_SUBROUTINE_UNIFORM:
485 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
486 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
487 return RESOURCE_UNI(res)->array_elements;
488 case GL_BUFFER_VARIABLE:
489 /* Unsized arrays */
490 if (RESOURCE_UNI(res)->array_stride > 0 &&
491 RESOURCE_UNI(res)->array_elements == 0)
492 return 1;
493 else
494 return RESOURCE_UNI(res)->array_elements;
495 case GL_VERTEX_SUBROUTINE:
496 case GL_GEOMETRY_SUBROUTINE:
497 case GL_FRAGMENT_SUBROUTINE:
498 case GL_COMPUTE_SUBROUTINE:
499 case GL_TESS_CONTROL_SUBROUTINE:
500 case GL_TESS_EVALUATION_SUBROUTINE:
501 case GL_ATOMIC_COUNTER_BUFFER:
502 case GL_UNIFORM_BLOCK:
503 case GL_SHADER_STORAGE_BLOCK:
504 return 0;
505 default:
506 assert(!"support for resource type not implemented");
507 }
508 return 0;
509 }
510
511 /**
512 * Checks if array subscript is valid and if so sets array_index.
513 */
514 static bool
valid_array_index(const GLchar * name,unsigned * array_index)515 valid_array_index(const GLchar *name, unsigned *array_index)
516 {
517 long idx = 0;
518 const GLchar *out_base_name_end;
519
520 idx = parse_program_resource_name(name, &out_base_name_end);
521 if (idx < 0)
522 return false;
523
524 if (array_index)
525 *array_index = idx;
526
527 return true;
528 }
529
530 static uint32_t
compute_resource_key(GLenum programInterface,const char * name)531 compute_resource_key(GLenum programInterface, const char *name)
532 {
533 struct mesa_sha1 ctx;
534 unsigned char sha1[20];
535
536 _mesa_sha1_init(&ctx);
537 _mesa_sha1_update(&ctx, &programInterface, sizeof(programInterface));
538 _mesa_sha1_update(&ctx, name, strlen(name));
539 _mesa_sha1_final(&ctx, sha1);
540
541 return _mesa_hash_data(sha1, sizeof(sha1));
542 }
543
544 static struct gl_program_resource *
search_resource_hash(struct gl_shader_program * shProg,GLenum programInterface,const char * name,unsigned * array_index)545 search_resource_hash(struct gl_shader_program *shProg,
546 GLenum programInterface, const char *name,
547 unsigned *array_index)
548 {
549 const char *base_name_end;
550 long index = parse_program_resource_name(name, &base_name_end);
551 char *name_copy;
552
553 /* If dealing with array, we need to get the basename. */
554 if (index >= 0) {
555 name_copy = (char *) malloc(base_name_end - name + 1);
556 memcpy(name_copy, name, base_name_end - name);
557 name_copy[base_name_end - name] = '\0';
558 } else {
559 name_copy = (char*) name;
560 }
561
562 uint32_t key = compute_resource_key(programInterface, name_copy);
563 struct gl_program_resource *res = (struct gl_program_resource *)
564 _mesa_hash_table_u64_search(shProg->data->ProgramResourceHash, key);
565
566 if (name_copy != name)
567 free(name_copy);
568
569 if (res && array_index)
570 *array_index = index >= 0 ? index : 0;
571
572 return res;
573 }
574
575 /* Find a program resource with specific name in given interface.
576 */
577 struct gl_program_resource *
_mesa_program_resource_find_name(struct gl_shader_program * shProg,GLenum programInterface,const char * name,unsigned * array_index)578 _mesa_program_resource_find_name(struct gl_shader_program *shProg,
579 GLenum programInterface, const char *name,
580 unsigned *array_index)
581 {
582 struct gl_program_resource *res = NULL;
583
584 if (name == NULL)
585 return NULL;
586
587 /* If we have a name, try the ProgramResourceHash first. */
588 if (shProg->data->ProgramResourceHash)
589 res = search_resource_hash(shProg, programInterface, name, array_index);
590
591 if (res)
592 return res;
593
594 res = shProg->data->ProgramResourceList;
595 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
596 if (res->Type != programInterface)
597 continue;
598
599 /* Resource basename. */
600 const char *rname = _mesa_program_resource_name(res);
601
602 /* Since ARB_gl_spirv lack of name reflections is a possibility */
603 if (rname == NULL)
604 continue;
605
606 unsigned baselen = strlen(rname);
607 unsigned baselen_without_array_index = baselen;
608 const char *rname_last_square_bracket = strrchr(rname, '[');
609 bool found = false;
610 bool rname_has_array_index_zero = false;
611 /* From ARB_program_interface_query spec:
612 *
613 * "uint GetProgramResourceIndex(uint program, enum programInterface,
614 * const char *name);
615 * [...]
616 * If <name> exactly matches the name string of one of the active
617 * resources for <programInterface>, the index of the matched resource is
618 * returned. Additionally, if <name> would exactly match the name string
619 * of an active resource if "[0]" were appended to <name>, the index of
620 * the matched resource is returned. [...]"
621 *
622 * "A string provided to GetProgramResourceLocation or
623 * GetProgramResourceLocationIndex is considered to match an active variable
624 * if:
625 *
626 * * the string exactly matches the name of the active variable;
627 *
628 * * if the string identifies the base name of an active array, where the
629 * string would exactly match the name of the variable if the suffix
630 * "[0]" were appended to the string; [...]"
631 */
632 /* Remove array's index from interface block name comparison only if
633 * array's index is zero and the resulting string length is the same
634 * than the provided name's length.
635 */
636 if (rname_last_square_bracket) {
637 baselen_without_array_index -= strlen(rname_last_square_bracket);
638 rname_has_array_index_zero =
639 (strcmp(rname_last_square_bracket, "[0]") == 0) &&
640 (baselen_without_array_index == strlen(name));
641 }
642
643 if (strncmp(rname, name, baselen) == 0)
644 found = true;
645 else if (rname_has_array_index_zero &&
646 strncmp(rname, name, baselen_without_array_index) == 0)
647 found = true;
648
649 if (found) {
650 switch (programInterface) {
651 case GL_UNIFORM_BLOCK:
652 case GL_SHADER_STORAGE_BLOCK:
653 /* Basename match, check if array or struct. */
654 if (rname_has_array_index_zero ||
655 name[baselen] == '\0' ||
656 name[baselen] == '[' ||
657 name[baselen] == '.') {
658 return res;
659 }
660 break;
661 case GL_TRANSFORM_FEEDBACK_VARYING:
662 case GL_BUFFER_VARIABLE:
663 case GL_UNIFORM:
664 case GL_VERTEX_SUBROUTINE_UNIFORM:
665 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
666 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
667 case GL_COMPUTE_SUBROUTINE_UNIFORM:
668 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
669 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
670 case GL_VERTEX_SUBROUTINE:
671 case GL_GEOMETRY_SUBROUTINE:
672 case GL_FRAGMENT_SUBROUTINE:
673 case GL_COMPUTE_SUBROUTINE:
674 case GL_TESS_CONTROL_SUBROUTINE:
675 case GL_TESS_EVALUATION_SUBROUTINE:
676 if (name[baselen] == '.') {
677 return res;
678 }
679 /* fall-through */
680 case GL_PROGRAM_INPUT:
681 case GL_PROGRAM_OUTPUT:
682 if (name[baselen] == '\0') {
683 return res;
684 } else if (name[baselen] == '[' &&
685 valid_array_index(name, array_index)) {
686 return res;
687 }
688 break;
689 default:
690 assert(!"not implemented for given interface");
691 }
692 }
693 }
694 return NULL;
695 }
696
697 /* Find an uniform or buffer variable program resource with an specific offset
698 * inside a block with an specific binding.
699 *
700 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
701 */
702 static struct gl_program_resource *
program_resource_find_binding_offset(struct gl_shader_program * shProg,GLenum programInterface,const GLuint binding,const GLint offset)703 program_resource_find_binding_offset(struct gl_shader_program *shProg,
704 GLenum programInterface,
705 const GLuint binding,
706 const GLint offset)
707 {
708
709 /* First we need to get the BLOCK_INDEX from the BUFFER_BINDING */
710 GLenum blockInterface;
711
712 switch (programInterface) {
713 case GL_BUFFER_VARIABLE:
714 blockInterface = GL_SHADER_STORAGE_BLOCK;
715 break;
716 case GL_UNIFORM:
717 blockInterface = GL_UNIFORM_BLOCK;
718 break;
719 default:
720 assert("Invalid program interface");
721 return NULL;
722 }
723
724 int block_index = -1;
725 int starting_index = -1;
726 struct gl_program_resource *res = shProg->data->ProgramResourceList;
727
728 /* Blocks are added to the resource list in the same order that they are
729 * added to UniformBlocks/ShaderStorageBlocks. Furthermore, all the blocks
730 * of each type (UBO/SSBO) are contiguous, so we can infer block_index from
731 * the resource list.
732 */
733 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
734 if (res->Type != blockInterface)
735 continue;
736
737 /* Store the first index where a resource of the specific interface is. */
738 if (starting_index == -1)
739 starting_index = i;
740
741 const struct gl_uniform_block *block = RESOURCE_UBO(res);
742
743 if (block->Binding == binding) {
744 /* For arrays, or arrays of arrays of blocks, we want the resource
745 * for the block with base index. Most properties for members of each
746 * block are inherited from the block with the base index, including
747 * a uniform being active or not.
748 */
749 block_index = i - starting_index - block->linearized_array_index;
750 break;
751 }
752 }
753
754 if (block_index == -1)
755 return NULL;
756
757 /* We now look for the resource corresponding to the uniform or buffer
758 * variable using the BLOCK_INDEX and OFFSET.
759 */
760 res = shProg->data->ProgramResourceList;
761 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
762 if (res->Type != programInterface)
763 continue;
764
765 const struct gl_uniform_storage *uniform = RESOURCE_UNI(res);
766
767 if (uniform->block_index == block_index && uniform->offset == offset) {
768 return res;
769 }
770 }
771
772 return NULL;
773 }
774
775 /* Checks if an uniform or buffer variable is in the active program resource
776 * list.
777 *
778 * It takes into accout that for variables coming from SPIR-V binaries their
779 * names could not be available (ARB_gl_spirv). In that case, it will use the
780 * the offset and the block binding to locate the resource.
781 *
782 * Valid interfaces are GL_BUFFER_VARIABLE and GL_UNIFORM.
783 */
784 struct gl_program_resource *
_mesa_program_resource_find_active_variable(struct gl_shader_program * shProg,GLenum programInterface,const gl_uniform_block * block,unsigned index)785 _mesa_program_resource_find_active_variable(struct gl_shader_program *shProg,
786 GLenum programInterface,
787 const gl_uniform_block *block,
788 unsigned index)
789 {
790 struct gl_program_resource *res;
791 struct gl_uniform_buffer_variable uni = block->Uniforms[index];
792
793 assert(programInterface == GL_UNIFORM ||
794 programInterface == GL_BUFFER_VARIABLE);
795
796 if (uni.IndexName) {
797 res = _mesa_program_resource_find_name(shProg, programInterface, uni.IndexName,
798 NULL);
799 } else {
800 /* As the resource has no associated name (ARB_gl_spirv),
801 * we can use the UBO/SSBO binding and offset to find it.
802 */
803 res = program_resource_find_binding_offset(shProg, programInterface,
804 block->Binding, uni.Offset);
805 }
806
807 return res;
808 }
809
810 static GLuint
calc_resource_index(struct gl_shader_program * shProg,struct gl_program_resource * res)811 calc_resource_index(struct gl_shader_program *shProg,
812 struct gl_program_resource *res)
813 {
814 unsigned i;
815 GLuint index = 0;
816 for (i = 0; i < shProg->data->NumProgramResourceList; i++) {
817 if (&shProg->data->ProgramResourceList[i] == res)
818 return index;
819 if (shProg->data->ProgramResourceList[i].Type == res->Type)
820 index++;
821 }
822 return GL_INVALID_INDEX;
823 }
824
825 /**
826 * Calculate index for the given resource.
827 */
828 GLuint
_mesa_program_resource_index(struct gl_shader_program * shProg,struct gl_program_resource * res)829 _mesa_program_resource_index(struct gl_shader_program *shProg,
830 struct gl_program_resource *res)
831 {
832 if (!res)
833 return GL_INVALID_INDEX;
834
835 switch (res->Type) {
836 case GL_ATOMIC_COUNTER_BUFFER:
837 return RESOURCE_ATC(res) - shProg->data->AtomicBuffers;
838 case GL_VERTEX_SUBROUTINE:
839 case GL_GEOMETRY_SUBROUTINE:
840 case GL_FRAGMENT_SUBROUTINE:
841 case GL_COMPUTE_SUBROUTINE:
842 case GL_TESS_CONTROL_SUBROUTINE:
843 case GL_TESS_EVALUATION_SUBROUTINE:
844 return RESOURCE_SUB(res)->index;
845 case GL_UNIFORM_BLOCK:
846 case GL_SHADER_STORAGE_BLOCK:
847 case GL_TRANSFORM_FEEDBACK_BUFFER:
848 case GL_TRANSFORM_FEEDBACK_VARYING:
849 default:
850 return calc_resource_index(shProg, res);
851 }
852 }
853
854 /**
855 * Find a program resource that points to given data.
856 */
857 static struct gl_program_resource*
program_resource_find_data(struct gl_shader_program * shProg,void * data)858 program_resource_find_data(struct gl_shader_program *shProg, void *data)
859 {
860 struct gl_program_resource *res = shProg->data->ProgramResourceList;
861 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
862 i++, res++) {
863 if (res->Data == data)
864 return res;
865 }
866 return NULL;
867 }
868
869 /* Find a program resource with specific index in given interface.
870 */
871 struct gl_program_resource *
_mesa_program_resource_find_index(struct gl_shader_program * shProg,GLenum programInterface,GLuint index)872 _mesa_program_resource_find_index(struct gl_shader_program *shProg,
873 GLenum programInterface, GLuint index)
874 {
875 struct gl_program_resource *res = shProg->data->ProgramResourceList;
876 int idx = -1;
877
878 for (unsigned i = 0; i < shProg->data->NumProgramResourceList;
879 i++, res++) {
880 if (res->Type != programInterface)
881 continue;
882
883 switch (res->Type) {
884 case GL_UNIFORM_BLOCK:
885 case GL_ATOMIC_COUNTER_BUFFER:
886 case GL_SHADER_STORAGE_BLOCK:
887 case GL_TRANSFORM_FEEDBACK_BUFFER:
888 if (_mesa_program_resource_index(shProg, res) == index)
889 return res;
890 break;
891 case GL_TRANSFORM_FEEDBACK_VARYING:
892 case GL_PROGRAM_INPUT:
893 case GL_PROGRAM_OUTPUT:
894 case GL_UNIFORM:
895 case GL_VERTEX_SUBROUTINE_UNIFORM:
896 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
897 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
898 case GL_COMPUTE_SUBROUTINE_UNIFORM:
899 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
900 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
901 case GL_VERTEX_SUBROUTINE:
902 case GL_GEOMETRY_SUBROUTINE:
903 case GL_FRAGMENT_SUBROUTINE:
904 case GL_COMPUTE_SUBROUTINE:
905 case GL_TESS_CONTROL_SUBROUTINE:
906 case GL_TESS_EVALUATION_SUBROUTINE:
907 case GL_BUFFER_VARIABLE:
908 if (++idx == (int) index)
909 return res;
910 break;
911 default:
912 assert(!"not implemented for given interface");
913 }
914 }
915 return NULL;
916 }
917
918 /* Function returns if resource name is expected to have index
919 * appended into it.
920 *
921 *
922 * Page 61 (page 73 of the PDF) in section 2.11 of the OpenGL ES 3.0
923 * spec says:
924 *
925 * "If the active uniform is an array, the uniform name returned in
926 * name will always be the name of the uniform array appended with
927 * "[0]"."
928 *
929 * The same text also appears in the OpenGL 4.2 spec. It does not,
930 * however, appear in any previous spec. Previous specifications are
931 * ambiguous in this regard. However, either name can later be passed
932 * to glGetUniformLocation (and related APIs), so there shouldn't be any
933 * harm in always appending "[0]" to uniform array names.
934 */
935 static bool
add_index_to_name(struct gl_program_resource * res)936 add_index_to_name(struct gl_program_resource *res)
937 {
938 /* Transform feedback varyings have array index already appended
939 * in their names.
940 */
941 return res->Type != GL_TRANSFORM_FEEDBACK_VARYING;
942 }
943
944 /* Get name length of a program resource. This consists of
945 * base name + 3 for '[0]' if resource is an array.
946 */
947 extern unsigned
_mesa_program_resource_name_len(struct gl_program_resource * res)948 _mesa_program_resource_name_len(struct gl_program_resource *res)
949 {
950 const char* name = _mesa_program_resource_name(res);
951
952 /* For shaders constructed from SPIR-V binaries, variables may not
953 * have names associated with them.
954 */
955 if (!name)
956 return 0;
957
958 unsigned length = strlen(name);
959 if (_mesa_program_resource_array_size(res) && add_index_to_name(res))
960 length += 3;
961 return length;
962 }
963
964 /* Get full name of a program resource.
965 */
966 bool
_mesa_get_program_resource_name(struct gl_shader_program * shProg,GLenum programInterface,GLuint index,GLsizei bufSize,GLsizei * length,GLchar * name,const char * caller)967 _mesa_get_program_resource_name(struct gl_shader_program *shProg,
968 GLenum programInterface, GLuint index,
969 GLsizei bufSize, GLsizei *length,
970 GLchar *name, const char *caller)
971 {
972 GET_CURRENT_CONTEXT(ctx);
973
974 /* Find resource with given interface and index. */
975 struct gl_program_resource *res =
976 _mesa_program_resource_find_index(shProg, programInterface, index);
977
978 /* The error INVALID_VALUE is generated if <index> is greater than
979 * or equal to the number of entries in the active resource list for
980 * <programInterface>.
981 */
982 if (!res) {
983 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index %u)", caller, index);
984 return false;
985 }
986
987 if (bufSize < 0) {
988 _mesa_error(ctx, GL_INVALID_VALUE, "%s(bufSize %d)", caller, bufSize);
989 return false;
990 }
991
992 GLsizei localLength;
993
994 if (length == NULL)
995 length = &localLength;
996
997 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
998
999 /* The resource name can be NULL for shaders constructed from SPIR-V
1000 * binaries. In that case, we do not add the '[0]'.
1001 */
1002 if (name && name[0] != '\0' &&
1003 _mesa_program_resource_array_size(res) && add_index_to_name(res)) {
1004 int i;
1005
1006 /* The comparison is strange because *length does *NOT* include the
1007 * terminating NUL, but maxLength does.
1008 */
1009 for (i = 0; i < 3 && (*length + i + 1) < bufSize; i++)
1010 name[*length + i] = "[0]"[i];
1011
1012 name[*length + i] = '\0';
1013 *length += i;
1014 }
1015 return true;
1016 }
1017
1018 static GLint
program_resource_location(struct gl_program_resource * res,unsigned array_index)1019 program_resource_location(struct gl_program_resource *res, unsigned array_index)
1020 {
1021 switch (res->Type) {
1022 case GL_PROGRAM_INPUT: {
1023 const gl_shader_variable *var = RESOURCE_VAR(res);
1024
1025 if (var->location == -1)
1026 return -1;
1027
1028 /* If the input is an array, fail if the index is out of bounds. */
1029 if (array_index > 0
1030 && array_index >= var->type->length) {
1031 return -1;
1032 }
1033 return var->location +
1034 (array_index * var->type->without_array()->matrix_columns);
1035 }
1036 case GL_PROGRAM_OUTPUT:
1037 if (RESOURCE_VAR(res)->location == -1)
1038 return -1;
1039
1040 /* If the output is an array, fail if the index is out of bounds. */
1041 if (array_index > 0
1042 && array_index >= RESOURCE_VAR(res)->type->length) {
1043 return -1;
1044 }
1045 return RESOURCE_VAR(res)->location + array_index;
1046 case GL_UNIFORM:
1047 /* If the uniform is built-in, fail. */
1048 if (RESOURCE_UNI(res)->builtin)
1049 return -1;
1050
1051 /* From page 79 of the OpenGL 4.2 spec:
1052 *
1053 * "A valid name cannot be a structure, an array of structures, or any
1054 * portion of a single vector or a matrix."
1055 */
1056 if (RESOURCE_UNI(res)->type->without_array()->is_struct())
1057 return -1;
1058
1059 /* From the GL_ARB_uniform_buffer_object spec:
1060 *
1061 * "The value -1 will be returned if <name> does not correspond to an
1062 * active uniform variable name in <program>, if <name> is associated
1063 * with a named uniform block, or if <name> starts with the reserved
1064 * prefix "gl_"."
1065 */
1066 if (RESOURCE_UNI(res)->block_index != -1 ||
1067 RESOURCE_UNI(res)->atomic_buffer_index != -1)
1068 return -1;
1069
1070 /* fallthrough */
1071 case GL_VERTEX_SUBROUTINE_UNIFORM:
1072 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1073 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1074 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1075 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1076 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1077 /* If the uniform is an array, fail if the index is out of bounds. */
1078 if (array_index > 0
1079 && array_index >= RESOURCE_UNI(res)->array_elements) {
1080 return -1;
1081 }
1082
1083 /* location in remap table + array element offset */
1084 return RESOURCE_UNI(res)->remap_location + array_index;
1085 default:
1086 return -1;
1087 }
1088 }
1089
1090 /**
1091 * Function implements following location queries:
1092 * glGetUniformLocation
1093 */
1094 GLint
_mesa_program_resource_location(struct gl_shader_program * shProg,GLenum programInterface,const char * name)1095 _mesa_program_resource_location(struct gl_shader_program *shProg,
1096 GLenum programInterface, const char *name)
1097 {
1098 unsigned array_index = 0;
1099 struct gl_program_resource *res =
1100 _mesa_program_resource_find_name(shProg, programInterface, name,
1101 &array_index);
1102
1103 /* Resource not found. */
1104 if (!res)
1105 return -1;
1106
1107 return program_resource_location(res, array_index);
1108 }
1109
1110 static GLint
_get_resource_location_index(struct gl_program_resource * res)1111 _get_resource_location_index(struct gl_program_resource *res)
1112 {
1113 /* Non-existent variable or resource is not referenced by fragment stage. */
1114 if (!res || !(res->StageReferences & (1 << MESA_SHADER_FRAGMENT)))
1115 return -1;
1116
1117 /* From OpenGL 4.5 spec, 7.3 Program Objects
1118 * "The value -1 will be returned by either command...
1119 * ... or if name identifies an active variable that does not have a
1120 * valid location assigned.
1121 */
1122 if (RESOURCE_VAR(res)->location == -1)
1123 return -1;
1124 return RESOURCE_VAR(res)->index;
1125 }
1126
1127 /**
1128 * Function implements following index queries:
1129 * glGetFragDataIndex
1130 */
1131 GLint
_mesa_program_resource_location_index(struct gl_shader_program * shProg,GLenum programInterface,const char * name)1132 _mesa_program_resource_location_index(struct gl_shader_program *shProg,
1133 GLenum programInterface, const char *name)
1134 {
1135 struct gl_program_resource *res =
1136 _mesa_program_resource_find_name(shProg, programInterface, name, NULL);
1137
1138 return _get_resource_location_index(res);
1139 }
1140
1141 static uint8_t
stage_from_enum(GLenum ref)1142 stage_from_enum(GLenum ref)
1143 {
1144 switch (ref) {
1145 case GL_REFERENCED_BY_VERTEX_SHADER:
1146 return MESA_SHADER_VERTEX;
1147 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1148 return MESA_SHADER_TESS_CTRL;
1149 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1150 return MESA_SHADER_TESS_EVAL;
1151 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1152 return MESA_SHADER_GEOMETRY;
1153 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1154 return MESA_SHADER_FRAGMENT;
1155 case GL_REFERENCED_BY_COMPUTE_SHADER:
1156 return MESA_SHADER_COMPUTE;
1157 default:
1158 assert(!"shader stage not supported");
1159 return MESA_SHADER_STAGES;
1160 }
1161 }
1162
1163 /**
1164 * Check if resource is referenced by given 'referenced by' stage enum.
1165 * ATC and UBO resources hold stage references of their own.
1166 */
1167 static bool
is_resource_referenced(struct gl_shader_program * shProg,struct gl_program_resource * res,GLuint index,uint8_t stage)1168 is_resource_referenced(struct gl_shader_program *shProg,
1169 struct gl_program_resource *res,
1170 GLuint index, uint8_t stage)
1171 {
1172 /* First, check if we even have such a stage active. */
1173 if (!shProg->_LinkedShaders[stage])
1174 return false;
1175
1176 if (res->Type == GL_ATOMIC_COUNTER_BUFFER)
1177 return RESOURCE_ATC(res)->StageReferences[stage];
1178
1179 if (res->Type == GL_UNIFORM_BLOCK)
1180 return shProg->data->UniformBlocks[index].stageref & (1 << stage);
1181
1182 if (res->Type == GL_SHADER_STORAGE_BLOCK)
1183 return shProg->data->ShaderStorageBlocks[index].stageref & (1 << stage);
1184
1185 return res->StageReferences & (1 << stage);
1186 }
1187
1188 static unsigned
get_buffer_property(struct gl_shader_program * shProg,struct gl_program_resource * res,const GLenum prop,GLint * val,const char * caller)1189 get_buffer_property(struct gl_shader_program *shProg,
1190 struct gl_program_resource *res, const GLenum prop,
1191 GLint *val, const char *caller)
1192 {
1193 GET_CURRENT_CONTEXT(ctx);
1194 if (res->Type != GL_UNIFORM_BLOCK &&
1195 res->Type != GL_ATOMIC_COUNTER_BUFFER &&
1196 res->Type != GL_SHADER_STORAGE_BLOCK &&
1197 res->Type != GL_TRANSFORM_FEEDBACK_BUFFER)
1198 goto invalid_operation;
1199
1200 if (res->Type == GL_UNIFORM_BLOCK) {
1201 switch (prop) {
1202 case GL_BUFFER_BINDING:
1203 *val = RESOURCE_UBO(res)->Binding;
1204 return 1;
1205 case GL_BUFFER_DATA_SIZE:
1206 *val = RESOURCE_UBO(res)->UniformBufferSize;
1207 return 1;
1208 case GL_NUM_ACTIVE_VARIABLES:
1209 *val = 0;
1210 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1211 struct gl_program_resource *uni =
1212 _mesa_program_resource_find_active_variable(
1213 shProg,
1214 GL_UNIFORM,
1215 RESOURCE_UBO(res),
1216 i);
1217
1218 if (!uni)
1219 continue;
1220 (*val)++;
1221 }
1222 return 1;
1223 case GL_ACTIVE_VARIABLES: {
1224 unsigned num_values = 0;
1225 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1226 struct gl_program_resource *uni =
1227 _mesa_program_resource_find_active_variable(
1228 shProg,
1229 GL_UNIFORM,
1230 RESOURCE_UBO(res),
1231 i);
1232
1233 if (!uni)
1234 continue;
1235 *val++ =
1236 _mesa_program_resource_index(shProg, uni);
1237 num_values++;
1238 }
1239 return num_values;
1240 }
1241 }
1242 } else if (res->Type == GL_SHADER_STORAGE_BLOCK) {
1243 switch (prop) {
1244 case GL_BUFFER_BINDING:
1245 *val = RESOURCE_UBO(res)->Binding;
1246 return 1;
1247 case GL_BUFFER_DATA_SIZE:
1248 *val = RESOURCE_UBO(res)->UniformBufferSize;
1249 return 1;
1250 case GL_NUM_ACTIVE_VARIABLES:
1251 *val = 0;
1252 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1253 struct gl_program_resource *uni =
1254 _mesa_program_resource_find_active_variable(
1255 shProg,
1256 GL_BUFFER_VARIABLE,
1257 RESOURCE_UBO(res),
1258 i);
1259
1260 if (!uni)
1261 continue;
1262 (*val)++;
1263 }
1264 return 1;
1265 case GL_ACTIVE_VARIABLES: {
1266 unsigned num_values = 0;
1267 for (unsigned i = 0; i < RESOURCE_UBO(res)->NumUniforms; i++) {
1268 struct gl_program_resource *uni =
1269 _mesa_program_resource_find_active_variable(
1270 shProg,
1271 GL_BUFFER_VARIABLE,
1272 RESOURCE_UBO(res),
1273 i);
1274
1275 if (!uni)
1276 continue;
1277 *val++ =
1278 _mesa_program_resource_index(shProg, uni);
1279 num_values++;
1280 }
1281 return num_values;
1282 }
1283 }
1284 } else if (res->Type == GL_ATOMIC_COUNTER_BUFFER) {
1285 switch (prop) {
1286 case GL_BUFFER_BINDING:
1287 *val = RESOURCE_ATC(res)->Binding;
1288 return 1;
1289 case GL_BUFFER_DATA_SIZE:
1290 *val = RESOURCE_ATC(res)->MinimumSize;
1291 return 1;
1292 case GL_NUM_ACTIVE_VARIABLES:
1293 *val = RESOURCE_ATC(res)->NumUniforms;
1294 return 1;
1295 case GL_ACTIVE_VARIABLES:
1296 for (unsigned i = 0; i < RESOURCE_ATC(res)->NumUniforms; i++) {
1297 /* Active atomic buffer contains index to UniformStorage. Find
1298 * out gl_program_resource via data pointer and then calculate
1299 * index of that uniform.
1300 */
1301 unsigned idx = RESOURCE_ATC(res)->Uniforms[i];
1302 struct gl_program_resource *uni =
1303 program_resource_find_data(shProg,
1304 &shProg->data->UniformStorage[idx]);
1305 assert(uni);
1306 *val++ = _mesa_program_resource_index(shProg, uni);
1307 }
1308 return RESOURCE_ATC(res)->NumUniforms;
1309 }
1310 } else if (res->Type == GL_TRANSFORM_FEEDBACK_BUFFER) {
1311 switch (prop) {
1312 case GL_BUFFER_BINDING:
1313 *val = RESOURCE_XFB(res)->Binding;
1314 return 1;
1315 case GL_NUM_ACTIVE_VARIABLES:
1316 *val = RESOURCE_XFB(res)->NumVaryings;
1317 return 1;
1318 case GL_ACTIVE_VARIABLES:
1319 struct gl_transform_feedback_info *linked_xfb =
1320 shProg->last_vert_prog->sh.LinkedTransformFeedback;
1321 for (int i = 0; i < linked_xfb->NumVarying; i++) {
1322 unsigned index = linked_xfb->Varyings[i].BufferIndex;
1323 struct gl_program_resource *buf_res =
1324 _mesa_program_resource_find_index(shProg,
1325 GL_TRANSFORM_FEEDBACK_BUFFER,
1326 index);
1327 assert(buf_res);
1328 if (res == buf_res) {
1329 *val++ = i;
1330 }
1331 }
1332 return RESOURCE_XFB(res)->NumVaryings;
1333 }
1334 }
1335 assert(!"support for property type not implemented");
1336
1337 invalid_operation:
1338 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1339 _mesa_enum_to_string(res->Type),
1340 _mesa_enum_to_string(prop));
1341
1342 return 0;
1343 }
1344
1345 unsigned
_mesa_program_resource_prop(struct gl_shader_program * shProg,struct gl_program_resource * res,GLuint index,const GLenum prop,GLint * val,const char * caller)1346 _mesa_program_resource_prop(struct gl_shader_program *shProg,
1347 struct gl_program_resource *res, GLuint index,
1348 const GLenum prop, GLint *val, const char *caller)
1349 {
1350 GET_CURRENT_CONTEXT(ctx);
1351
1352 #define VALIDATE_TYPE(type)\
1353 if (res->Type != type)\
1354 goto invalid_operation;
1355
1356 #define VALIDATE_TYPE_2(type1, type2)\
1357 if (res->Type != type1 && res->Type != type2)\
1358 goto invalid_operation;
1359
1360 switch(prop) {
1361 case GL_NAME_LENGTH:
1362 switch (res->Type) {
1363 case GL_ATOMIC_COUNTER_BUFFER:
1364 case GL_TRANSFORM_FEEDBACK_BUFFER:
1365 goto invalid_operation;
1366 default:
1367 /* Resource name length + terminator. */
1368 *val = _mesa_program_resource_name_len(res) + 1;
1369 }
1370 return 1;
1371 case GL_TYPE:
1372 switch (res->Type) {
1373 case GL_UNIFORM:
1374 case GL_BUFFER_VARIABLE:
1375 *val = RESOURCE_UNI(res)->type->gl_type;
1376 return 1;
1377 case GL_PROGRAM_INPUT:
1378 case GL_PROGRAM_OUTPUT:
1379 *val = RESOURCE_VAR(res)->type->gl_type;
1380 return 1;
1381 case GL_TRANSFORM_FEEDBACK_VARYING:
1382 *val = RESOURCE_XFV(res)->Type;
1383 return 1;
1384 default:
1385 goto invalid_operation;
1386 }
1387 case GL_ARRAY_SIZE:
1388 switch (res->Type) {
1389 case GL_UNIFORM:
1390 case GL_BUFFER_VARIABLE:
1391 case GL_VERTEX_SUBROUTINE_UNIFORM:
1392 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1393 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1394 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1395 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1396 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1397
1398 /* Test if a buffer variable is an array or an unsized array.
1399 * Unsized arrays return zero as array size.
1400 */
1401 if (RESOURCE_UNI(res)->is_shader_storage &&
1402 RESOURCE_UNI(res)->array_stride > 0)
1403 *val = RESOURCE_UNI(res)->array_elements;
1404 else
1405 *val = MAX2(RESOURCE_UNI(res)->array_elements, 1);
1406 return 1;
1407 case GL_PROGRAM_INPUT:
1408 case GL_PROGRAM_OUTPUT:
1409 *val = MAX2(_mesa_program_resource_array_size(res), 1);
1410 return 1;
1411 case GL_TRANSFORM_FEEDBACK_VARYING:
1412 *val = RESOURCE_XFV(res)->Size;
1413 return 1;
1414 default:
1415 goto invalid_operation;
1416 }
1417 case GL_OFFSET:
1418 switch (res->Type) {
1419 case GL_UNIFORM:
1420 case GL_BUFFER_VARIABLE:
1421 *val = RESOURCE_UNI(res)->offset;
1422 return 1;
1423 case GL_TRANSFORM_FEEDBACK_VARYING:
1424 *val = RESOURCE_XFV(res)->Offset;
1425 return 1;
1426 default:
1427 goto invalid_operation;
1428 }
1429 case GL_BLOCK_INDEX:
1430 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1431 *val = RESOURCE_UNI(res)->block_index;
1432 return 1;
1433 case GL_ARRAY_STRIDE:
1434 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1435 *val = RESOURCE_UNI(res)->array_stride;
1436 return 1;
1437 case GL_MATRIX_STRIDE:
1438 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1439 *val = RESOURCE_UNI(res)->matrix_stride;
1440 return 1;
1441 case GL_IS_ROW_MAJOR:
1442 VALIDATE_TYPE_2(GL_UNIFORM, GL_BUFFER_VARIABLE);
1443 *val = RESOURCE_UNI(res)->row_major;
1444 return 1;
1445 case GL_ATOMIC_COUNTER_BUFFER_INDEX:
1446 VALIDATE_TYPE(GL_UNIFORM);
1447 *val = RESOURCE_UNI(res)->atomic_buffer_index;
1448 return 1;
1449 case GL_BUFFER_BINDING:
1450 case GL_BUFFER_DATA_SIZE:
1451 case GL_NUM_ACTIVE_VARIABLES:
1452 case GL_ACTIVE_VARIABLES:
1453 return get_buffer_property(shProg, res, prop, val, caller);
1454 case GL_REFERENCED_BY_COMPUTE_SHADER:
1455 if (!_mesa_has_compute_shaders(ctx))
1456 goto invalid_enum;
1457 /* fallthrough */
1458 case GL_REFERENCED_BY_VERTEX_SHADER:
1459 case GL_REFERENCED_BY_TESS_CONTROL_SHADER:
1460 case GL_REFERENCED_BY_TESS_EVALUATION_SHADER:
1461 case GL_REFERENCED_BY_GEOMETRY_SHADER:
1462 case GL_REFERENCED_BY_FRAGMENT_SHADER:
1463 switch (res->Type) {
1464 case GL_UNIFORM:
1465 case GL_PROGRAM_INPUT:
1466 case GL_PROGRAM_OUTPUT:
1467 case GL_UNIFORM_BLOCK:
1468 case GL_BUFFER_VARIABLE:
1469 case GL_SHADER_STORAGE_BLOCK:
1470 case GL_ATOMIC_COUNTER_BUFFER:
1471 *val = is_resource_referenced(shProg, res, index,
1472 stage_from_enum(prop));
1473 return 1;
1474 default:
1475 goto invalid_operation;
1476 }
1477 case GL_LOCATION:
1478 switch (res->Type) {
1479 case GL_UNIFORM:
1480 case GL_VERTEX_SUBROUTINE_UNIFORM:
1481 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
1482 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
1483 case GL_COMPUTE_SUBROUTINE_UNIFORM:
1484 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
1485 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
1486 case GL_PROGRAM_INPUT:
1487 case GL_PROGRAM_OUTPUT:
1488 *val = program_resource_location(res, 0);
1489 return 1;
1490 default:
1491 goto invalid_operation;
1492 }
1493 case GL_LOCATION_COMPONENT:
1494 switch (res->Type) {
1495 case GL_PROGRAM_INPUT:
1496 case GL_PROGRAM_OUTPUT:
1497 *val = RESOURCE_VAR(res)->component;
1498 return 1;
1499 default:
1500 goto invalid_operation;
1501 }
1502 case GL_LOCATION_INDEX: {
1503 int tmp;
1504 if (res->Type != GL_PROGRAM_OUTPUT)
1505 goto invalid_operation;
1506 tmp = program_resource_location(res, 0);
1507 if (tmp == -1)
1508 *val = -1;
1509 else
1510 *val = _get_resource_location_index(res);
1511 return 1;
1512 }
1513 case GL_NUM_COMPATIBLE_SUBROUTINES:
1514 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1515 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1516 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1517 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1518 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1519 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1520 goto invalid_operation;
1521 *val = RESOURCE_UNI(res)->num_compatible_subroutines;
1522 return 1;
1523 case GL_COMPATIBLE_SUBROUTINES: {
1524 const struct gl_uniform_storage *uni;
1525 struct gl_program *p;
1526 unsigned count, i;
1527 int j;
1528
1529 if (res->Type != GL_VERTEX_SUBROUTINE_UNIFORM &&
1530 res->Type != GL_FRAGMENT_SUBROUTINE_UNIFORM &&
1531 res->Type != GL_GEOMETRY_SUBROUTINE_UNIFORM &&
1532 res->Type != GL_COMPUTE_SUBROUTINE_UNIFORM &&
1533 res->Type != GL_TESS_CONTROL_SUBROUTINE_UNIFORM &&
1534 res->Type != GL_TESS_EVALUATION_SUBROUTINE_UNIFORM)
1535 goto invalid_operation;
1536 uni = RESOURCE_UNI(res);
1537
1538 p = shProg->_LinkedShaders[_mesa_shader_stage_from_subroutine_uniform(res->Type)]->Program;
1539 count = 0;
1540 for (i = 0; i < p->sh.NumSubroutineFunctions; i++) {
1541 struct gl_subroutine_function *fn = &p->sh.SubroutineFunctions[i];
1542 for (j = 0; j < fn->num_compat_types; j++) {
1543 if (fn->types[j] == uni->type) {
1544 val[count++] = i;
1545 break;
1546 }
1547 }
1548 }
1549 return count;
1550 }
1551
1552 case GL_TOP_LEVEL_ARRAY_SIZE:
1553 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1554 *val = RESOURCE_UNI(res)->top_level_array_size;
1555 return 1;
1556
1557 case GL_TOP_LEVEL_ARRAY_STRIDE:
1558 VALIDATE_TYPE(GL_BUFFER_VARIABLE);
1559 *val = RESOURCE_UNI(res)->top_level_array_stride;
1560 return 1;
1561
1562 /* GL_ARB_tessellation_shader */
1563 case GL_IS_PER_PATCH:
1564 switch (res->Type) {
1565 case GL_PROGRAM_INPUT:
1566 case GL_PROGRAM_OUTPUT:
1567 *val = RESOURCE_VAR(res)->patch;
1568 return 1;
1569 default:
1570 goto invalid_operation;
1571 }
1572
1573 case GL_TRANSFORM_FEEDBACK_BUFFER_INDEX:
1574 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_VARYING);
1575 *val = RESOURCE_XFV(res)->BufferIndex;
1576 return 1;
1577 case GL_TRANSFORM_FEEDBACK_BUFFER_STRIDE:
1578 VALIDATE_TYPE(GL_TRANSFORM_FEEDBACK_BUFFER);
1579 *val = RESOURCE_XFB(res)->Stride * 4;
1580 return 1;
1581
1582 default:
1583 goto invalid_enum;
1584 }
1585
1586 #undef VALIDATE_TYPE
1587 #undef VALIDATE_TYPE_2
1588
1589 invalid_enum:
1590 _mesa_error(ctx, GL_INVALID_ENUM, "%s(%s prop %s)", caller,
1591 _mesa_enum_to_string(res->Type),
1592 _mesa_enum_to_string(prop));
1593 return 0;
1594
1595 invalid_operation:
1596 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s prop %s)", caller,
1597 _mesa_enum_to_string(res->Type),
1598 _mesa_enum_to_string(prop));
1599 return 0;
1600 }
1601
1602 extern void
_mesa_get_program_resourceiv(struct gl_shader_program * shProg,GLenum programInterface,GLuint index,GLsizei propCount,const GLenum * props,GLsizei bufSize,GLsizei * length,GLint * params)1603 _mesa_get_program_resourceiv(struct gl_shader_program *shProg,
1604 GLenum programInterface, GLuint index, GLsizei propCount,
1605 const GLenum *props, GLsizei bufSize,
1606 GLsizei *length, GLint *params)
1607 {
1608 GET_CURRENT_CONTEXT(ctx);
1609 GLint *val = (GLint *) params;
1610 const GLenum *prop = props;
1611 GLsizei amount = 0;
1612
1613 struct gl_program_resource *res =
1614 _mesa_program_resource_find_index(shProg, programInterface, index);
1615
1616 /* No such resource found or bufSize negative. */
1617 if (!res || bufSize < 0) {
1618 _mesa_error(ctx, GL_INVALID_VALUE,
1619 "glGetProgramResourceiv(%s index %d bufSize %d)",
1620 _mesa_enum_to_string(programInterface), index, bufSize);
1621 return;
1622 }
1623
1624 /* Write propCount values until error occurs or bufSize reached. */
1625 for (int i = 0; i < propCount && i < bufSize; i++, val++, prop++) {
1626 int props_written =
1627 _mesa_program_resource_prop(shProg, res, index, *prop, val,
1628 "glGetProgramResourceiv");
1629
1630 /* Error happened. */
1631 if (props_written == 0)
1632 return;
1633
1634 amount += props_written;
1635 }
1636
1637 /* If <length> is not NULL, the actual number of integer values
1638 * written to <params> will be written to <length>.
1639 */
1640 if (length)
1641 *length = amount;
1642 }
1643
1644 static bool
validate_io(struct gl_program * producer,struct gl_program * consumer)1645 validate_io(struct gl_program *producer, struct gl_program *consumer)
1646 {
1647 if (producer->sh.data->linked_stages == consumer->sh.data->linked_stages)
1648 return true;
1649
1650 const bool producer_is_array_stage =
1651 producer->info.stage == MESA_SHADER_TESS_CTRL;
1652 const bool consumer_is_array_stage =
1653 consumer->info.stage == MESA_SHADER_GEOMETRY ||
1654 consumer->info.stage == MESA_SHADER_TESS_CTRL ||
1655 consumer->info.stage == MESA_SHADER_TESS_EVAL;
1656
1657 bool valid = true;
1658
1659 gl_shader_variable const **outputs =
1660 (gl_shader_variable const **) calloc(producer->sh.data->NumProgramResourceList,
1661 sizeof(gl_shader_variable *));
1662 if (outputs == NULL)
1663 return false;
1664
1665 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1666 * says:
1667 *
1668 * At an interface between program objects, the set of inputs and
1669 * outputs are considered to match exactly if and only if:
1670 *
1671 * - Every declared input variable has a matching output, as described
1672 * above.
1673 * - There are no user-defined output variables declared without a
1674 * matching input variable declaration.
1675 *
1676 * Every input has an output, and every output has an input. Scan the list
1677 * of producer resources once, and generate the list of outputs. As inputs
1678 * and outputs are matched, remove the matched outputs from the set. At
1679 * the end, the set must be empty. If the set is not empty, then there is
1680 * some output that did not have an input.
1681 */
1682 unsigned num_outputs = 0;
1683 for (unsigned i = 0; i < producer->sh.data->NumProgramResourceList; i++) {
1684 struct gl_program_resource *res =
1685 &producer->sh.data->ProgramResourceList[i];
1686
1687 if (res->Type != GL_PROGRAM_OUTPUT)
1688 continue;
1689
1690 gl_shader_variable const *const var = RESOURCE_VAR(res);
1691
1692 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1693 * says:
1694 *
1695 * Built-in inputs or outputs do not affect interface matching.
1696 */
1697 if (is_gl_identifier(var->name))
1698 continue;
1699
1700 outputs[num_outputs++] = var;
1701 }
1702
1703 unsigned match_index = 0;
1704 for (unsigned i = 0; i < consumer->sh.data->NumProgramResourceList; i++) {
1705 struct gl_program_resource *res =
1706 &consumer->sh.data->ProgramResourceList[i];
1707
1708 if (res->Type != GL_PROGRAM_INPUT)
1709 continue;
1710
1711 gl_shader_variable const *const consumer_var = RESOURCE_VAR(res);
1712 gl_shader_variable const *producer_var = NULL;
1713
1714 if (is_gl_identifier(consumer_var->name))
1715 continue;
1716
1717 /* Inputs with explicit locations match other outputs with explicit
1718 * locations by location instead of by name.
1719 */
1720 if (consumer_var->explicit_location) {
1721 for (unsigned j = 0; j < num_outputs; j++) {
1722 const gl_shader_variable *const var = outputs[j];
1723
1724 if (var->explicit_location &&
1725 consumer_var->location == var->location) {
1726 producer_var = var;
1727 match_index = j;
1728 break;
1729 }
1730 }
1731 } else {
1732 for (unsigned j = 0; j < num_outputs; j++) {
1733 const gl_shader_variable *const var = outputs[j];
1734
1735 if (!var->explicit_location &&
1736 strcmp(consumer_var->name, var->name) == 0) {
1737 producer_var = var;
1738 match_index = j;
1739 break;
1740 }
1741 }
1742 }
1743
1744 /* Section 7.4.1 (Shader Interface Matching) of the OpenGL ES 3.1 spec
1745 * says:
1746 *
1747 * - An output variable is considered to match an input variable in
1748 * the subsequent shader if:
1749 *
1750 * - the two variables match in name, type, and qualification; or
1751 *
1752 * - the two variables are declared with the same location
1753 * qualifier and match in type and qualification.
1754 */
1755 if (producer_var == NULL) {
1756 valid = false;
1757 goto out;
1758 }
1759
1760 /* An output cannot match more than one input, so remove the output from
1761 * the set of possible outputs.
1762 */
1763 outputs[match_index] = NULL;
1764 num_outputs--;
1765 if (match_index < num_outputs)
1766 outputs[match_index] = outputs[num_outputs];
1767
1768 /* Section 7.4.1 (Shader Interface Matching) of the ES 3.2 spec says:
1769 *
1770 * "Tessellation control shader per-vertex output variables and
1771 * blocks and tessellation control, tessellation evaluation, and
1772 * geometry shader per-vertex input variables and blocks are
1773 * required to be declared as arrays, with each element representing
1774 * input or output values for a single vertex of a multi-vertex
1775 * primitive. For the purposes of interface matching, such variables
1776 * and blocks are treated as though they were not declared as
1777 * arrays."
1778 *
1779 * So we unwrap those types before matching.
1780 */
1781 const glsl_type *consumer_type = consumer_var->type;
1782 const glsl_type *consumer_interface_type = consumer_var->interface_type;
1783 const glsl_type *producer_type = producer_var->type;
1784 const glsl_type *producer_interface_type = producer_var->interface_type;
1785
1786 if (consumer_is_array_stage) {
1787 if (consumer_interface_type) {
1788 /* the interface is the array; the underlying types should match */
1789 if (consumer_interface_type->is_array() && !consumer_var->patch)
1790 consumer_interface_type = consumer_interface_type->fields.array;
1791 } else {
1792 if (consumer_type->is_array() && !consumer_var->patch)
1793 consumer_type = consumer_type->fields.array;
1794 }
1795 }
1796
1797 if (producer_is_array_stage) {
1798 if (producer_interface_type) {
1799 /* the interface is the array; the underlying types should match */
1800 if (producer_interface_type->is_array() && !producer_var->patch)
1801 producer_interface_type = producer_interface_type->fields.array;
1802 } else {
1803 if (producer_type->is_array() && !producer_var->patch)
1804 producer_type = producer_type->fields.array;
1805 }
1806 }
1807
1808 if (producer_type != consumer_type) {
1809 valid = false;
1810 goto out;
1811 }
1812
1813 if (producer_interface_type != consumer_interface_type) {
1814 valid = false;
1815 goto out;
1816 }
1817
1818 /* Section 9.2.2 (Separable Programs) of the GLSL ES spec says:
1819 *
1820 * Qualifier Class| Qualifier |in/out
1821 * ---------------+-------------+------
1822 * Storage | in |
1823 * | out | N/A
1824 * | uniform |
1825 * ---------------+-------------+------
1826 * Auxiliary | centroid | No
1827 * ---------------+-------------+------
1828 * | location | Yes
1829 * | Block layout| N/A
1830 * | binding | N/A
1831 * | offset | N/A
1832 * | format | N/A
1833 * ---------------+-------------+------
1834 * Interpolation | smooth |
1835 * | flat | Yes
1836 * ---------------+-------------+------
1837 * | lowp |
1838 * Precision | mediump | Yes
1839 * | highp |
1840 * ---------------+-------------+------
1841 * Variance | invariant | No
1842 * ---------------+-------------+------
1843 * Memory | all | N/A
1844 *
1845 * Note that location mismatches are detected by the loops above that
1846 * find the producer variable that goes with the consumer variable.
1847 */
1848 unsigned producer_interpolation = producer_var->interpolation;
1849 unsigned consumer_interpolation = consumer_var->interpolation;
1850 if (producer_interpolation == INTERP_MODE_NONE)
1851 producer_interpolation = INTERP_MODE_SMOOTH;
1852 if (consumer_interpolation == INTERP_MODE_NONE)
1853 consumer_interpolation = INTERP_MODE_SMOOTH;
1854 if (producer_interpolation != consumer_interpolation) {
1855 valid = false;
1856 goto out;
1857 }
1858
1859 if (producer_var->precision != consumer_var->precision) {
1860 valid = false;
1861 goto out;
1862 }
1863
1864 if (producer_var->outermost_struct_type != consumer_var->outermost_struct_type) {
1865 valid = false;
1866 goto out;
1867 }
1868 }
1869
1870 out:
1871 free(outputs);
1872 return valid && num_outputs == 0;
1873 }
1874
1875 /**
1876 * Validate inputs against outputs in a program pipeline.
1877 */
1878 extern "C" bool
_mesa_validate_pipeline_io(struct gl_pipeline_object * pipeline)1879 _mesa_validate_pipeline_io(struct gl_pipeline_object *pipeline)
1880 {
1881 struct gl_program **prog = (struct gl_program **) pipeline->CurrentProgram;
1882
1883 /* Find first active stage in pipeline. */
1884 unsigned idx, prev = 0;
1885 for (idx = 0; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1886 if (prog[idx]) {
1887 prev = idx;
1888 break;
1889 }
1890 }
1891
1892 for (idx = prev + 1; idx < ARRAY_SIZE(pipeline->CurrentProgram); idx++) {
1893 if (prog[idx]) {
1894 /* Pipeline might include both non-compute and a compute program, do
1895 * not attempt to validate varyings between non-compute and compute
1896 * stage.
1897 */
1898 if (prog[idx]->info.stage == MESA_SHADER_COMPUTE)
1899 break;
1900
1901 if (!validate_io(prog[prev], prog[idx]))
1902 return false;
1903
1904 prev = idx;
1905 }
1906 }
1907 return true;
1908 }
1909
1910 extern "C" void
_mesa_create_program_resource_hash(struct gl_shader_program * shProg)1911 _mesa_create_program_resource_hash(struct gl_shader_program *shProg)
1912 {
1913 /* Rebuild resource hash. */
1914 if (shProg->data->ProgramResourceHash)
1915 _mesa_hash_table_u64_destroy(shProg->data->ProgramResourceHash, NULL);
1916
1917 shProg->data->ProgramResourceHash = _mesa_hash_table_u64_create(shProg);
1918
1919 struct gl_program_resource *res = shProg->data->ProgramResourceList;
1920 for (unsigned i = 0; i < shProg->data->NumProgramResourceList; i++, res++) {
1921 const char *name = _mesa_program_resource_name(res);
1922 if (name) {
1923 uint32_t key = compute_resource_key(res->Type, name);
1924 _mesa_hash_table_u64_insert(shProg->data->ProgramResourceHash, key,
1925 res);
1926 }
1927 }
1928 }
1929