glsl: add matrix layout information to interface block types
[mesa.git] / src / compiler / glsl_types.cpp
1 /*
2 * Copyright © 2009 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 #include <stdio.h>
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
29
30
31 mtx_t glsl_type::mutex = _MTX_INITIALIZER_NP;
32 hash_table *glsl_type::array_types = NULL;
33 hash_table *glsl_type::record_types = NULL;
34 hash_table *glsl_type::interface_types = NULL;
35 hash_table *glsl_type::function_types = NULL;
36 hash_table *glsl_type::subroutine_types = NULL;
37 void *glsl_type::mem_ctx = NULL;
38
39 void
40 glsl_type::init_ralloc_type_ctx(void)
41 {
42 if (glsl_type::mem_ctx == NULL) {
43 glsl_type::mem_ctx = ralloc_autofree_context();
44 assert(glsl_type::mem_ctx != NULL);
45 }
46 }
47
48 glsl_type::glsl_type(GLenum gl_type,
49 glsl_base_type base_type, unsigned vector_elements,
50 unsigned matrix_columns, const char *name) :
51 gl_type(gl_type),
52 base_type(base_type),
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements), matrix_columns(matrix_columns),
56 length(0)
57 {
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
60 */
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT) & 3) == unsigned(GLSL_TYPE_UINT));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT) & 3) == unsigned(GLSL_TYPE_INT));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT));
64
65 mtx_lock(&glsl_type::mutex);
66
67 init_ralloc_type_ctx();
68 assert(name != NULL);
69 this->name = ralloc_strdup(this->mem_ctx, name);
70
71 mtx_unlock(&glsl_type::mutex);
72
73 /* Neither dimension is zero or both dimensions are zero.
74 */
75 assert((vector_elements == 0) == (matrix_columns == 0));
76 memset(& fields, 0, sizeof(fields));
77 }
78
79 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
80 enum glsl_sampler_dim dim, bool shadow, bool array,
81 unsigned type, const char *name) :
82 gl_type(gl_type),
83 base_type(base_type),
84 sampler_dimensionality(dim), sampler_shadow(shadow),
85 sampler_array(array), sampled_type(type), interface_packing(0),
86 interface_row_major(0), length(0)
87 {
88 mtx_lock(&glsl_type::mutex);
89
90 init_ralloc_type_ctx();
91 assert(name != NULL);
92 this->name = ralloc_strdup(this->mem_ctx, name);
93
94 mtx_unlock(&glsl_type::mutex);
95
96 memset(& fields, 0, sizeof(fields));
97
98 if (base_type == GLSL_TYPE_SAMPLER) {
99 /* Samplers take no storage whatsoever. */
100 matrix_columns = vector_elements = 0;
101 } else {
102 matrix_columns = vector_elements = 1;
103 }
104 }
105
106 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
107 const char *name) :
108 gl_type(0),
109 base_type(GLSL_TYPE_STRUCT),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 sampled_type(0), interface_packing(0), interface_row_major(0),
112 vector_elements(0), matrix_columns(0),
113 length(num_fields)
114 {
115 unsigned int i;
116
117 mtx_lock(&glsl_type::mutex);
118
119 init_ralloc_type_ctx();
120 assert(name != NULL);
121 this->name = ralloc_strdup(this->mem_ctx, name);
122 this->fields.structure = ralloc_array(this->mem_ctx,
123 glsl_struct_field, length);
124
125 for (i = 0; i < length; i++) {
126 this->fields.structure[i].type = fields[i].type;
127 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
128 fields[i].name);
129 this->fields.structure[i].location = fields[i].location;
130 this->fields.structure[i].offset = fields[i].offset;
131 this->fields.structure[i].interpolation = fields[i].interpolation;
132 this->fields.structure[i].centroid = fields[i].centroid;
133 this->fields.structure[i].sample = fields[i].sample;
134 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
135 this->fields.structure[i].patch = fields[i].patch;
136 this->fields.structure[i].image_read_only = fields[i].image_read_only;
137 this->fields.structure[i].image_write_only = fields[i].image_write_only;
138 this->fields.structure[i].image_coherent = fields[i].image_coherent;
139 this->fields.structure[i].image_volatile = fields[i].image_volatile;
140 this->fields.structure[i].image_restrict = fields[i].image_restrict;
141 this->fields.structure[i].precision = fields[i].precision;
142 this->fields.structure[i].explicit_xfb_buffer =
143 fields[i].explicit_xfb_buffer;
144 this->fields.structure[i].xfb_buffer = fields[i].xfb_buffer;
145 this->fields.structure[i].xfb_stride = fields[i].xfb_stride;
146 }
147
148 mtx_unlock(&glsl_type::mutex);
149 }
150
151 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
152 enum glsl_interface_packing packing,
153 bool row_major, const char *name) :
154 gl_type(0),
155 base_type(GLSL_TYPE_INTERFACE),
156 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
157 sampled_type(0), interface_packing((unsigned) packing),
158 interface_row_major((unsigned) row_major),
159 vector_elements(0), matrix_columns(0),
160 length(num_fields)
161 {
162 unsigned int i;
163
164 mtx_lock(&glsl_type::mutex);
165
166 init_ralloc_type_ctx();
167 assert(name != NULL);
168 this->name = ralloc_strdup(this->mem_ctx, name);
169 this->fields.structure = ralloc_array(this->mem_ctx,
170 glsl_struct_field, length);
171 for (i = 0; i < length; i++) {
172 this->fields.structure[i].type = fields[i].type;
173 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
174 fields[i].name);
175 this->fields.structure[i].location = fields[i].location;
176 this->fields.structure[i].offset = fields[i].offset;
177 this->fields.structure[i].interpolation = fields[i].interpolation;
178 this->fields.structure[i].centroid = fields[i].centroid;
179 this->fields.structure[i].sample = fields[i].sample;
180 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
181 this->fields.structure[i].patch = fields[i].patch;
182 this->fields.structure[i].image_read_only = fields[i].image_read_only;
183 this->fields.structure[i].image_write_only = fields[i].image_write_only;
184 this->fields.structure[i].image_coherent = fields[i].image_coherent;
185 this->fields.structure[i].image_volatile = fields[i].image_volatile;
186 this->fields.structure[i].image_restrict = fields[i].image_restrict;
187 this->fields.structure[i].precision = fields[i].precision;
188 this->fields.structure[i].explicit_xfb_buffer =
189 fields[i].explicit_xfb_buffer;
190 this->fields.structure[i].xfb_buffer = fields[i].xfb_buffer;
191 this->fields.structure[i].xfb_stride = fields[i].xfb_stride;
192 }
193
194 mtx_unlock(&glsl_type::mutex);
195 }
196
197 glsl_type::glsl_type(const glsl_type *return_type,
198 const glsl_function_param *params, unsigned num_params) :
199 gl_type(0),
200 base_type(GLSL_TYPE_FUNCTION),
201 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
202 sampled_type(0), interface_packing(0), interface_row_major(0),
203 vector_elements(0), matrix_columns(0),
204 length(num_params)
205 {
206 unsigned int i;
207
208 mtx_lock(&glsl_type::mutex);
209
210 init_ralloc_type_ctx();
211
212 this->fields.parameters = rzalloc_array(this->mem_ctx,
213 glsl_function_param, num_params + 1);
214
215 /* We store the return type as the first parameter */
216 this->fields.parameters[0].type = return_type;
217 this->fields.parameters[0].in = false;
218 this->fields.parameters[0].out = true;
219
220 /* We store the i'th parameter in slot i+1 */
221 for (i = 0; i < length; i++) {
222 this->fields.parameters[i + 1].type = params[i].type;
223 this->fields.parameters[i + 1].in = params[i].in;
224 this->fields.parameters[i + 1].out = params[i].out;
225 }
226
227 mtx_unlock(&glsl_type::mutex);
228 }
229
230 glsl_type::glsl_type(const char *subroutine_name) :
231 gl_type(0),
232 base_type(GLSL_TYPE_SUBROUTINE),
233 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
234 sampled_type(0), interface_packing(0), interface_row_major(0),
235 vector_elements(1), matrix_columns(1),
236 length(0)
237 {
238 mtx_lock(&glsl_type::mutex);
239
240 init_ralloc_type_ctx();
241 assert(subroutine_name != NULL);
242 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
243 mtx_unlock(&glsl_type::mutex);
244 }
245
246 bool
247 glsl_type::contains_sampler() const
248 {
249 if (this->is_array()) {
250 return this->fields.array->contains_sampler();
251 } else if (this->is_record()) {
252 for (unsigned int i = 0; i < this->length; i++) {
253 if (this->fields.structure[i].type->contains_sampler())
254 return true;
255 }
256 return false;
257 } else {
258 return this->is_sampler();
259 }
260 }
261
262
263 bool
264 glsl_type::contains_integer() const
265 {
266 if (this->is_array()) {
267 return this->fields.array->contains_integer();
268 } else if (this->is_record()) {
269 for (unsigned int i = 0; i < this->length; i++) {
270 if (this->fields.structure[i].type->contains_integer())
271 return true;
272 }
273 return false;
274 } else {
275 return this->is_integer();
276 }
277 }
278
279 bool
280 glsl_type::contains_double() const
281 {
282 if (this->is_array()) {
283 return this->fields.array->contains_double();
284 } else if (this->is_record()) {
285 for (unsigned int i = 0; i < this->length; i++) {
286 if (this->fields.structure[i].type->contains_double())
287 return true;
288 }
289 return false;
290 } else {
291 return this->is_double();
292 }
293 }
294
295 bool
296 glsl_type::contains_opaque() const {
297 switch (base_type) {
298 case GLSL_TYPE_SAMPLER:
299 case GLSL_TYPE_IMAGE:
300 case GLSL_TYPE_ATOMIC_UINT:
301 return true;
302 case GLSL_TYPE_ARRAY:
303 return fields.array->contains_opaque();
304 case GLSL_TYPE_STRUCT:
305 for (unsigned int i = 0; i < length; i++) {
306 if (fields.structure[i].type->contains_opaque())
307 return true;
308 }
309 return false;
310 default:
311 return false;
312 }
313 }
314
315 bool
316 glsl_type::contains_subroutine() const
317 {
318 if (this->is_array()) {
319 return this->fields.array->contains_subroutine();
320 } else if (this->is_record()) {
321 for (unsigned int i = 0; i < this->length; i++) {
322 if (this->fields.structure[i].type->contains_subroutine())
323 return true;
324 }
325 return false;
326 } else {
327 return this->is_subroutine();
328 }
329 }
330
331 gl_texture_index
332 glsl_type::sampler_index() const
333 {
334 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
335
336 assert(t->is_sampler());
337
338 switch (t->sampler_dimensionality) {
339 case GLSL_SAMPLER_DIM_1D:
340 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
341 case GLSL_SAMPLER_DIM_2D:
342 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
343 case GLSL_SAMPLER_DIM_3D:
344 return TEXTURE_3D_INDEX;
345 case GLSL_SAMPLER_DIM_CUBE:
346 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
347 case GLSL_SAMPLER_DIM_RECT:
348 return TEXTURE_RECT_INDEX;
349 case GLSL_SAMPLER_DIM_BUF:
350 return TEXTURE_BUFFER_INDEX;
351 case GLSL_SAMPLER_DIM_EXTERNAL:
352 return TEXTURE_EXTERNAL_INDEX;
353 case GLSL_SAMPLER_DIM_MS:
354 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
355 default:
356 assert(!"Should not get here.");
357 return TEXTURE_BUFFER_INDEX;
358 }
359 }
360
361 bool
362 glsl_type::contains_image() const
363 {
364 if (this->is_array()) {
365 return this->fields.array->contains_image();
366 } else if (this->is_record()) {
367 for (unsigned int i = 0; i < this->length; i++) {
368 if (this->fields.structure[i].type->contains_image())
369 return true;
370 }
371 return false;
372 } else {
373 return this->is_image();
374 }
375 }
376
377 const glsl_type *glsl_type::get_base_type() const
378 {
379 switch (base_type) {
380 case GLSL_TYPE_UINT:
381 return uint_type;
382 case GLSL_TYPE_INT:
383 return int_type;
384 case GLSL_TYPE_FLOAT:
385 return float_type;
386 case GLSL_TYPE_DOUBLE:
387 return double_type;
388 case GLSL_TYPE_BOOL:
389 return bool_type;
390 default:
391 return error_type;
392 }
393 }
394
395
396 const glsl_type *glsl_type::get_scalar_type() const
397 {
398 const glsl_type *type = this;
399
400 /* Handle arrays */
401 while (type->base_type == GLSL_TYPE_ARRAY)
402 type = type->fields.array;
403
404 /* Handle vectors and matrices */
405 switch (type->base_type) {
406 case GLSL_TYPE_UINT:
407 return uint_type;
408 case GLSL_TYPE_INT:
409 return int_type;
410 case GLSL_TYPE_FLOAT:
411 return float_type;
412 case GLSL_TYPE_DOUBLE:
413 return double_type;
414 case GLSL_TYPE_BOOL:
415 return bool_type;
416 default:
417 /* Handle everything else */
418 return type;
419 }
420 }
421
422
423 void
424 _mesa_glsl_release_types(void)
425 {
426 /* Should only be called during atexit (either when unloading shared
427 * object, or if process terminates), so no mutex-locking should be
428 * necessary.
429 */
430 if (glsl_type::array_types != NULL) {
431 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
432 glsl_type::array_types = NULL;
433 }
434
435 if (glsl_type::record_types != NULL) {
436 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
437 glsl_type::record_types = NULL;
438 }
439
440 if (glsl_type::interface_types != NULL) {
441 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
442 glsl_type::interface_types = NULL;
443 }
444 }
445
446
447 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
448 base_type(GLSL_TYPE_ARRAY),
449 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
450 sampled_type(0), interface_packing(0), interface_row_major(0),
451 vector_elements(0), matrix_columns(0),
452 length(length), name(NULL)
453 {
454 this->fields.array = array;
455 /* Inherit the gl type of the base. The GL type is used for
456 * uniform/statevar handling in Mesa and the arrayness of the type
457 * is represented by the size rather than the type.
458 */
459 this->gl_type = array->gl_type;
460
461 /* Allow a maximum of 10 characters for the array size. This is enough
462 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
463 * NUL.
464 */
465 const unsigned name_length = strlen(array->name) + 10 + 3;
466
467 mtx_lock(&glsl_type::mutex);
468 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
469 mtx_unlock(&glsl_type::mutex);
470
471 if (length == 0)
472 snprintf(n, name_length, "%s[]", array->name);
473 else {
474 /* insert outermost dimensions in the correct spot
475 * otherwise the dimension order will be backwards
476 */
477 const char *pos = strchr(array->name, '[');
478 if (pos) {
479 int idx = pos - array->name;
480 snprintf(n, idx+1, "%s", array->name);
481 snprintf(n + idx, name_length - idx, "[%u]%s",
482 length, array->name + idx);
483 } else {
484 snprintf(n, name_length, "%s[%u]", array->name, length);
485 }
486 }
487
488 this->name = n;
489 }
490
491
492 const glsl_type *
493 glsl_type::vec(unsigned components)
494 {
495 if (components == 0 || components > 4)
496 return error_type;
497
498 static const glsl_type *const ts[] = {
499 float_type, vec2_type, vec3_type, vec4_type
500 };
501 return ts[components - 1];
502 }
503
504 const glsl_type *
505 glsl_type::dvec(unsigned components)
506 {
507 if (components == 0 || components > 4)
508 return error_type;
509
510 static const glsl_type *const ts[] = {
511 double_type, dvec2_type, dvec3_type, dvec4_type
512 };
513 return ts[components - 1];
514 }
515
516 const glsl_type *
517 glsl_type::ivec(unsigned components)
518 {
519 if (components == 0 || components > 4)
520 return error_type;
521
522 static const glsl_type *const ts[] = {
523 int_type, ivec2_type, ivec3_type, ivec4_type
524 };
525 return ts[components - 1];
526 }
527
528
529 const glsl_type *
530 glsl_type::uvec(unsigned components)
531 {
532 if (components == 0 || components > 4)
533 return error_type;
534
535 static const glsl_type *const ts[] = {
536 uint_type, uvec2_type, uvec3_type, uvec4_type
537 };
538 return ts[components - 1];
539 }
540
541
542 const glsl_type *
543 glsl_type::bvec(unsigned components)
544 {
545 if (components == 0 || components > 4)
546 return error_type;
547
548 static const glsl_type *const ts[] = {
549 bool_type, bvec2_type, bvec3_type, bvec4_type
550 };
551 return ts[components - 1];
552 }
553
554
555 const glsl_type *
556 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
557 {
558 if (base_type == GLSL_TYPE_VOID)
559 return void_type;
560
561 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
562 return error_type;
563
564 /* Treat GLSL vectors as Nx1 matrices.
565 */
566 if (columns == 1) {
567 switch (base_type) {
568 case GLSL_TYPE_UINT:
569 return uvec(rows);
570 case GLSL_TYPE_INT:
571 return ivec(rows);
572 case GLSL_TYPE_FLOAT:
573 return vec(rows);
574 case GLSL_TYPE_DOUBLE:
575 return dvec(rows);
576 case GLSL_TYPE_BOOL:
577 return bvec(rows);
578 default:
579 return error_type;
580 }
581 } else {
582 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
583 return error_type;
584
585 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
586 * combinations are valid:
587 *
588 * 1 2 3 4
589 * 1
590 * 2 x x x
591 * 3 x x x
592 * 4 x x x
593 */
594 #define IDX(c,r) (((c-1)*3) + (r-1))
595
596 if (base_type == GLSL_TYPE_DOUBLE) {
597 switch (IDX(columns, rows)) {
598 case IDX(2,2): return dmat2_type;
599 case IDX(2,3): return dmat2x3_type;
600 case IDX(2,4): return dmat2x4_type;
601 case IDX(3,2): return dmat3x2_type;
602 case IDX(3,3): return dmat3_type;
603 case IDX(3,4): return dmat3x4_type;
604 case IDX(4,2): return dmat4x2_type;
605 case IDX(4,3): return dmat4x3_type;
606 case IDX(4,4): return dmat4_type;
607 default: return error_type;
608 }
609 } else {
610 switch (IDX(columns, rows)) {
611 case IDX(2,2): return mat2_type;
612 case IDX(2,3): return mat2x3_type;
613 case IDX(2,4): return mat2x4_type;
614 case IDX(3,2): return mat3x2_type;
615 case IDX(3,3): return mat3_type;
616 case IDX(3,4): return mat3x4_type;
617 case IDX(4,2): return mat4x2_type;
618 case IDX(4,3): return mat4x3_type;
619 case IDX(4,4): return mat4_type;
620 default: return error_type;
621 }
622 }
623 }
624
625 assert(!"Should not get here.");
626 return error_type;
627 }
628
629 const glsl_type *
630 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
631 bool shadow,
632 bool array,
633 glsl_base_type type)
634 {
635 switch (type) {
636 case GLSL_TYPE_FLOAT:
637 switch (dim) {
638 case GLSL_SAMPLER_DIM_1D:
639 if (shadow)
640 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
641 else
642 return (array ? sampler1DArray_type : sampler1D_type);
643 case GLSL_SAMPLER_DIM_2D:
644 if (shadow)
645 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
646 else
647 return (array ? sampler2DArray_type : sampler2D_type);
648 case GLSL_SAMPLER_DIM_3D:
649 if (shadow || array)
650 return error_type;
651 else
652 return sampler3D_type;
653 case GLSL_SAMPLER_DIM_CUBE:
654 if (shadow)
655 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
656 else
657 return (array ? samplerCubeArray_type : samplerCube_type);
658 case GLSL_SAMPLER_DIM_RECT:
659 if (array)
660 return error_type;
661 if (shadow)
662 return sampler2DRectShadow_type;
663 else
664 return sampler2DRect_type;
665 case GLSL_SAMPLER_DIM_BUF:
666 if (shadow || array)
667 return error_type;
668 else
669 return samplerBuffer_type;
670 case GLSL_SAMPLER_DIM_MS:
671 if (shadow)
672 return error_type;
673 return (array ? sampler2DMSArray_type : sampler2DMS_type);
674 case GLSL_SAMPLER_DIM_EXTERNAL:
675 if (shadow || array)
676 return error_type;
677 else
678 return samplerExternalOES_type;
679 case GLSL_SAMPLER_DIM_SUBPASS:
680 return error_type;
681 }
682 case GLSL_TYPE_INT:
683 if (shadow)
684 return error_type;
685 switch (dim) {
686 case GLSL_SAMPLER_DIM_1D:
687 return (array ? isampler1DArray_type : isampler1D_type);
688 case GLSL_SAMPLER_DIM_2D:
689 return (array ? isampler2DArray_type : isampler2D_type);
690 case GLSL_SAMPLER_DIM_3D:
691 if (array)
692 return error_type;
693 return isampler3D_type;
694 case GLSL_SAMPLER_DIM_CUBE:
695 return (array ? isamplerCubeArray_type : isamplerCube_type);
696 case GLSL_SAMPLER_DIM_RECT:
697 if (array)
698 return error_type;
699 return isampler2DRect_type;
700 case GLSL_SAMPLER_DIM_BUF:
701 if (array)
702 return error_type;
703 return isamplerBuffer_type;
704 case GLSL_SAMPLER_DIM_MS:
705 return (array ? isampler2DMSArray_type : isampler2DMS_type);
706 case GLSL_SAMPLER_DIM_EXTERNAL:
707 return error_type;
708 case GLSL_SAMPLER_DIM_SUBPASS:
709 return error_type;
710 }
711 case GLSL_TYPE_UINT:
712 if (shadow)
713 return error_type;
714 switch (dim) {
715 case GLSL_SAMPLER_DIM_1D:
716 return (array ? usampler1DArray_type : usampler1D_type);
717 case GLSL_SAMPLER_DIM_2D:
718 return (array ? usampler2DArray_type : usampler2D_type);
719 case GLSL_SAMPLER_DIM_3D:
720 if (array)
721 return error_type;
722 return usampler3D_type;
723 case GLSL_SAMPLER_DIM_CUBE:
724 return (array ? usamplerCubeArray_type : usamplerCube_type);
725 case GLSL_SAMPLER_DIM_RECT:
726 if (array)
727 return error_type;
728 return usampler2DRect_type;
729 case GLSL_SAMPLER_DIM_BUF:
730 if (array)
731 return error_type;
732 return usamplerBuffer_type;
733 case GLSL_SAMPLER_DIM_MS:
734 return (array ? usampler2DMSArray_type : usampler2DMS_type);
735 case GLSL_SAMPLER_DIM_EXTERNAL:
736 return error_type;
737 case GLSL_SAMPLER_DIM_SUBPASS:
738 return error_type;
739 }
740 default:
741 return error_type;
742 }
743
744 unreachable("switch statement above should be complete");
745 }
746
747 const glsl_type *
748 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
749 bool array, glsl_base_type type)
750 {
751 if (dim == GLSL_SAMPLER_DIM_SUBPASS)
752 return subpassInput_type;
753 switch (type) {
754 case GLSL_TYPE_FLOAT:
755 switch (dim) {
756 case GLSL_SAMPLER_DIM_1D:
757 return (array ? image1DArray_type : image1D_type);
758 case GLSL_SAMPLER_DIM_2D:
759 return (array ? image2DArray_type : image2D_type);
760 case GLSL_SAMPLER_DIM_3D:
761 return image3D_type;
762 case GLSL_SAMPLER_DIM_CUBE:
763 return (array ? imageCubeArray_type : imageCube_type);
764 case GLSL_SAMPLER_DIM_RECT:
765 if (array)
766 return error_type;
767 else
768 return image2DRect_type;
769 case GLSL_SAMPLER_DIM_BUF:
770 if (array)
771 return error_type;
772 else
773 return imageBuffer_type;
774 case GLSL_SAMPLER_DIM_MS:
775 return (array ? image2DMSArray_type : image2DMS_type);
776 case GLSL_SAMPLER_DIM_EXTERNAL:
777 case GLSL_SAMPLER_DIM_SUBPASS:
778 return error_type;
779 }
780 case GLSL_TYPE_INT:
781 switch (dim) {
782 case GLSL_SAMPLER_DIM_1D:
783 return (array ? iimage1DArray_type : iimage1D_type);
784 case GLSL_SAMPLER_DIM_2D:
785 return (array ? iimage2DArray_type : iimage2D_type);
786 case GLSL_SAMPLER_DIM_3D:
787 if (array)
788 return error_type;
789 return iimage3D_type;
790 case GLSL_SAMPLER_DIM_CUBE:
791 return (array ? iimageCubeArray_type : iimageCube_type);
792 case GLSL_SAMPLER_DIM_RECT:
793 if (array)
794 return error_type;
795 return iimage2DRect_type;
796 case GLSL_SAMPLER_DIM_BUF:
797 if (array)
798 return error_type;
799 return iimageBuffer_type;
800 case GLSL_SAMPLER_DIM_MS:
801 return (array ? iimage2DMSArray_type : iimage2DMS_type);
802 case GLSL_SAMPLER_DIM_EXTERNAL:
803 case GLSL_SAMPLER_DIM_SUBPASS:
804 return error_type;
805 }
806 case GLSL_TYPE_UINT:
807 switch (dim) {
808 case GLSL_SAMPLER_DIM_1D:
809 return (array ? uimage1DArray_type : uimage1D_type);
810 case GLSL_SAMPLER_DIM_2D:
811 return (array ? uimage2DArray_type : uimage2D_type);
812 case GLSL_SAMPLER_DIM_3D:
813 if (array)
814 return error_type;
815 return uimage3D_type;
816 case GLSL_SAMPLER_DIM_CUBE:
817 return (array ? uimageCubeArray_type : uimageCube_type);
818 case GLSL_SAMPLER_DIM_RECT:
819 if (array)
820 return error_type;
821 return uimage2DRect_type;
822 case GLSL_SAMPLER_DIM_BUF:
823 if (array)
824 return error_type;
825 return uimageBuffer_type;
826 case GLSL_SAMPLER_DIM_MS:
827 return (array ? uimage2DMSArray_type : uimage2DMS_type);
828 case GLSL_SAMPLER_DIM_EXTERNAL:
829 case GLSL_SAMPLER_DIM_SUBPASS:
830 return error_type;
831 }
832 default:
833 return error_type;
834 }
835
836 unreachable("switch statement above should be complete");
837 }
838
839 const glsl_type *
840 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
841 {
842 /* Generate a name using the base type pointer in the key. This is
843 * done because the name of the base type may not be unique across
844 * shaders. For example, two shaders may have different record types
845 * named 'foo'.
846 */
847 char key[128];
848 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
849
850 mtx_lock(&glsl_type::mutex);
851
852 if (array_types == NULL) {
853 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
854 _mesa_key_string_equal);
855 }
856
857 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
858 if (entry == NULL) {
859 mtx_unlock(&glsl_type::mutex);
860 const glsl_type *t = new glsl_type(base, array_size);
861 mtx_lock(&glsl_type::mutex);
862
863 entry = _mesa_hash_table_insert(array_types,
864 ralloc_strdup(mem_ctx, key),
865 (void *) t);
866 }
867
868 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
869 assert(((glsl_type *) entry->data)->length == array_size);
870 assert(((glsl_type *) entry->data)->fields.array == base);
871
872 mtx_unlock(&glsl_type::mutex);
873
874 return (glsl_type *) entry->data;
875 }
876
877
878 bool
879 glsl_type::record_compare(const glsl_type *b, bool match_locations) const
880 {
881 if (this->length != b->length)
882 return false;
883
884 if (this->interface_packing != b->interface_packing)
885 return false;
886
887 if (this->interface_row_major != b->interface_row_major)
888 return false;
889
890 /* From the GLSL 4.20 specification (Sec 4.2):
891 *
892 * "Structures must have the same name, sequence of type names, and
893 * type definitions, and field names to be considered the same type."
894 *
895 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
896 *
897 * Note that we cannot force type name check when comparing unnamed
898 * structure types, these have a unique name assigned during parsing.
899 */
900 if (!this->is_anonymous() && !b->is_anonymous())
901 if (strcmp(this->name, b->name) != 0)
902 return false;
903
904 for (unsigned i = 0; i < this->length; i++) {
905 if (this->fields.structure[i].type != b->fields.structure[i].type)
906 return false;
907 if (strcmp(this->fields.structure[i].name,
908 b->fields.structure[i].name) != 0)
909 return false;
910 if (this->fields.structure[i].matrix_layout
911 != b->fields.structure[i].matrix_layout)
912 return false;
913 if (match_locations && this->fields.structure[i].location
914 != b->fields.structure[i].location)
915 return false;
916 if (this->fields.structure[i].offset
917 != b->fields.structure[i].offset)
918 return false;
919 if (this->fields.structure[i].interpolation
920 != b->fields.structure[i].interpolation)
921 return false;
922 if (this->fields.structure[i].centroid
923 != b->fields.structure[i].centroid)
924 return false;
925 if (this->fields.structure[i].sample
926 != b->fields.structure[i].sample)
927 return false;
928 if (this->fields.structure[i].patch
929 != b->fields.structure[i].patch)
930 return false;
931 if (this->fields.structure[i].image_read_only
932 != b->fields.structure[i].image_read_only)
933 return false;
934 if (this->fields.structure[i].image_write_only
935 != b->fields.structure[i].image_write_only)
936 return false;
937 if (this->fields.structure[i].image_coherent
938 != b->fields.structure[i].image_coherent)
939 return false;
940 if (this->fields.structure[i].image_volatile
941 != b->fields.structure[i].image_volatile)
942 return false;
943 if (this->fields.structure[i].image_restrict
944 != b->fields.structure[i].image_restrict)
945 return false;
946 if (this->fields.structure[i].precision
947 != b->fields.structure[i].precision)
948 return false;
949 if (this->fields.structure[i].explicit_xfb_buffer
950 != b->fields.structure[i].explicit_xfb_buffer)
951 return false;
952 if (this->fields.structure[i].xfb_buffer
953 != b->fields.structure[i].xfb_buffer)
954 return false;
955 if (this->fields.structure[i].xfb_stride
956 != b->fields.structure[i].xfb_stride)
957 return false;
958 }
959
960 return true;
961 }
962
963
964 bool
965 glsl_type::record_key_compare(const void *a, const void *b)
966 {
967 const glsl_type *const key1 = (glsl_type *) a;
968 const glsl_type *const key2 = (glsl_type *) b;
969
970 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
971 }
972
973
974 /**
975 * Generate an integer hash value for a glsl_type structure type.
976 */
977 unsigned
978 glsl_type::record_key_hash(const void *a)
979 {
980 const glsl_type *const key = (glsl_type *) a;
981 uintptr_t hash = key->length;
982 unsigned retval;
983
984 for (unsigned i = 0; i < key->length; i++) {
985 /* casting pointer to uintptr_t */
986 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
987 }
988
989 if (sizeof(hash) == 8)
990 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
991 else
992 retval = hash;
993
994 return retval;
995 }
996
997
998 const glsl_type *
999 glsl_type::get_record_instance(const glsl_struct_field *fields,
1000 unsigned num_fields,
1001 const char *name)
1002 {
1003 const glsl_type key(fields, num_fields, name);
1004
1005 mtx_lock(&glsl_type::mutex);
1006
1007 if (record_types == NULL) {
1008 record_types = _mesa_hash_table_create(NULL, record_key_hash,
1009 record_key_compare);
1010 }
1011
1012 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
1013 &key);
1014 if (entry == NULL) {
1015 mtx_unlock(&glsl_type::mutex);
1016 const glsl_type *t = new glsl_type(fields, num_fields, name);
1017 mtx_lock(&glsl_type::mutex);
1018
1019 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
1020 }
1021
1022 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1023 assert(((glsl_type *) entry->data)->length == num_fields);
1024 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1025
1026 mtx_unlock(&glsl_type::mutex);
1027
1028 return (glsl_type *) entry->data;
1029 }
1030
1031
1032 const glsl_type *
1033 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1034 unsigned num_fields,
1035 enum glsl_interface_packing packing,
1036 bool row_major,
1037 const char *block_name)
1038 {
1039 const glsl_type key(fields, num_fields, packing, row_major, block_name);
1040
1041 mtx_lock(&glsl_type::mutex);
1042
1043 if (interface_types == NULL) {
1044 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1045 record_key_compare);
1046 }
1047
1048 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1049 &key);
1050 if (entry == NULL) {
1051 mtx_unlock(&glsl_type::mutex);
1052 const glsl_type *t = new glsl_type(fields, num_fields,
1053 packing, row_major, block_name);
1054 mtx_lock(&glsl_type::mutex);
1055
1056 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1057 }
1058
1059 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1060 assert(((glsl_type *) entry->data)->length == num_fields);
1061 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1062
1063 mtx_unlock(&glsl_type::mutex);
1064
1065 return (glsl_type *) entry->data;
1066 }
1067
1068 const glsl_type *
1069 glsl_type::get_subroutine_instance(const char *subroutine_name)
1070 {
1071 const glsl_type key(subroutine_name);
1072
1073 mtx_lock(&glsl_type::mutex);
1074
1075 if (subroutine_types == NULL) {
1076 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1077 record_key_compare);
1078 }
1079
1080 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1081 &key);
1082 if (entry == NULL) {
1083 mtx_unlock(&glsl_type::mutex);
1084 const glsl_type *t = new glsl_type(subroutine_name);
1085 mtx_lock(&glsl_type::mutex);
1086
1087 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1088 }
1089
1090 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1091 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1092
1093 mtx_unlock(&glsl_type::mutex);
1094
1095 return (glsl_type *) entry->data;
1096 }
1097
1098
1099 static bool
1100 function_key_compare(const void *a, const void *b)
1101 {
1102 const glsl_type *const key1 = (glsl_type *) a;
1103 const glsl_type *const key2 = (glsl_type *) b;
1104
1105 if (key1->length != key2->length)
1106 return false;
1107
1108 return memcmp(key1->fields.parameters, key2->fields.parameters,
1109 (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1110 }
1111
1112
1113 static uint32_t
1114 function_key_hash(const void *a)
1115 {
1116 const glsl_type *const key = (glsl_type *) a;
1117 return _mesa_hash_data(key->fields.parameters,
1118 (key->length + 1) * sizeof(*key->fields.parameters));
1119 }
1120
1121 const glsl_type *
1122 glsl_type::get_function_instance(const glsl_type *return_type,
1123 const glsl_function_param *params,
1124 unsigned num_params)
1125 {
1126 const glsl_type key(return_type, params, num_params);
1127
1128 mtx_lock(&glsl_type::mutex);
1129
1130 if (function_types == NULL) {
1131 function_types = _mesa_hash_table_create(NULL, function_key_hash,
1132 function_key_compare);
1133 }
1134
1135 struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1136 if (entry == NULL) {
1137 mtx_unlock(&glsl_type::mutex);
1138 const glsl_type *t = new glsl_type(return_type, params, num_params);
1139 mtx_lock(&glsl_type::mutex);
1140
1141 entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1142 }
1143
1144 const glsl_type *t = (const glsl_type *)entry->data;
1145
1146 assert(t->base_type == GLSL_TYPE_FUNCTION);
1147 assert(t->length == num_params);
1148
1149 mtx_unlock(&glsl_type::mutex);
1150
1151 return t;
1152 }
1153
1154
1155 const glsl_type *
1156 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1157 {
1158 if (type_a == type_b) {
1159 return type_a;
1160 } else if (type_a->is_matrix() && type_b->is_matrix()) {
1161 /* Matrix multiply. The columns of A must match the rows of B. Given
1162 * the other previously tested constraints, this means the vector type
1163 * of a row from A must be the same as the vector type of a column from
1164 * B.
1165 */
1166 if (type_a->row_type() == type_b->column_type()) {
1167 /* The resulting matrix has the number of columns of matrix B and
1168 * the number of rows of matrix A. We get the row count of A by
1169 * looking at the size of a vector that makes up a column. The
1170 * transpose (size of a row) is done for B.
1171 */
1172 const glsl_type *const type =
1173 get_instance(type_a->base_type,
1174 type_a->column_type()->vector_elements,
1175 type_b->row_type()->vector_elements);
1176 assert(type != error_type);
1177
1178 return type;
1179 }
1180 } else if (type_a->is_matrix()) {
1181 /* A is a matrix and B is a column vector. Columns of A must match
1182 * rows of B. Given the other previously tested constraints, this
1183 * means the vector type of a row from A must be the same as the
1184 * vector the type of B.
1185 */
1186 if (type_a->row_type() == type_b) {
1187 /* The resulting vector has a number of elements equal to
1188 * the number of rows of matrix A. */
1189 const glsl_type *const type =
1190 get_instance(type_a->base_type,
1191 type_a->column_type()->vector_elements,
1192 1);
1193 assert(type != error_type);
1194
1195 return type;
1196 }
1197 } else {
1198 assert(type_b->is_matrix());
1199
1200 /* A is a row vector and B is a matrix. Columns of A must match rows
1201 * of B. Given the other previously tested constraints, this means
1202 * the type of A must be the same as the vector type of a column from
1203 * B.
1204 */
1205 if (type_a == type_b->column_type()) {
1206 /* The resulting vector has a number of elements equal to
1207 * the number of columns of matrix B. */
1208 const glsl_type *const type =
1209 get_instance(type_a->base_type,
1210 type_b->row_type()->vector_elements,
1211 1);
1212 assert(type != error_type);
1213
1214 return type;
1215 }
1216 }
1217
1218 return error_type;
1219 }
1220
1221
1222 const glsl_type *
1223 glsl_type::field_type(const char *name) const
1224 {
1225 if (this->base_type != GLSL_TYPE_STRUCT
1226 && this->base_type != GLSL_TYPE_INTERFACE)
1227 return error_type;
1228
1229 for (unsigned i = 0; i < this->length; i++) {
1230 if (strcmp(name, this->fields.structure[i].name) == 0)
1231 return this->fields.structure[i].type;
1232 }
1233
1234 return error_type;
1235 }
1236
1237
1238 int
1239 glsl_type::field_index(const char *name) const
1240 {
1241 if (this->base_type != GLSL_TYPE_STRUCT
1242 && this->base_type != GLSL_TYPE_INTERFACE)
1243 return -1;
1244
1245 for (unsigned i = 0; i < this->length; i++) {
1246 if (strcmp(name, this->fields.structure[i].name) == 0)
1247 return i;
1248 }
1249
1250 return -1;
1251 }
1252
1253
1254 unsigned
1255 glsl_type::component_slots() const
1256 {
1257 switch (this->base_type) {
1258 case GLSL_TYPE_UINT:
1259 case GLSL_TYPE_INT:
1260 case GLSL_TYPE_FLOAT:
1261 case GLSL_TYPE_BOOL:
1262 return this->components();
1263
1264 case GLSL_TYPE_DOUBLE:
1265 return 2 * this->components();
1266
1267 case GLSL_TYPE_STRUCT:
1268 case GLSL_TYPE_INTERFACE: {
1269 unsigned size = 0;
1270
1271 for (unsigned i = 0; i < this->length; i++)
1272 size += this->fields.structure[i].type->component_slots();
1273
1274 return size;
1275 }
1276
1277 case GLSL_TYPE_ARRAY:
1278 return this->length * this->fields.array->component_slots();
1279
1280 case GLSL_TYPE_IMAGE:
1281 return 1;
1282 case GLSL_TYPE_SUBROUTINE:
1283 return 1;
1284
1285 case GLSL_TYPE_FUNCTION:
1286 case GLSL_TYPE_SAMPLER:
1287 case GLSL_TYPE_ATOMIC_UINT:
1288 case GLSL_TYPE_VOID:
1289 case GLSL_TYPE_ERROR:
1290 break;
1291 }
1292
1293 return 0;
1294 }
1295
1296 unsigned
1297 glsl_type::record_location_offset(unsigned length) const
1298 {
1299 unsigned offset = 0;
1300 const glsl_type *t = this->without_array();
1301 if (t->is_record()) {
1302 assert(length <= t->length);
1303
1304 for (unsigned i = 0; i < length; i++) {
1305 const glsl_type *st = t->fields.structure[i].type;
1306 const glsl_type *wa = st->without_array();
1307 if (wa->is_record()) {
1308 unsigned r_offset = wa->record_location_offset(wa->length);
1309 offset += st->is_array() ?
1310 st->arrays_of_arrays_size() * r_offset : r_offset;
1311 } else if (st->is_array() && st->fields.array->is_array()) {
1312 unsigned outer_array_size = st->length;
1313 const glsl_type *base_type = st->fields.array;
1314
1315 /* For arrays of arrays the outer arrays take up a uniform
1316 * slot for each element. The innermost array elements share a
1317 * single slot so we ignore the innermost array when calculating
1318 * the offset.
1319 */
1320 while (base_type->fields.array->is_array()) {
1321 outer_array_size = outer_array_size * base_type->length;
1322 base_type = base_type->fields.array;
1323 }
1324 offset += outer_array_size;
1325 } else {
1326 /* We dont worry about arrays here because unless the array
1327 * contains a structure or another array it only takes up a single
1328 * uniform slot.
1329 */
1330 offset += 1;
1331 }
1332 }
1333 }
1334 return offset;
1335 }
1336
1337 unsigned
1338 glsl_type::uniform_locations() const
1339 {
1340 unsigned size = 0;
1341
1342 switch (this->base_type) {
1343 case GLSL_TYPE_UINT:
1344 case GLSL_TYPE_INT:
1345 case GLSL_TYPE_FLOAT:
1346 case GLSL_TYPE_DOUBLE:
1347 case GLSL_TYPE_BOOL:
1348 case GLSL_TYPE_SAMPLER:
1349 case GLSL_TYPE_IMAGE:
1350 case GLSL_TYPE_SUBROUTINE:
1351 return 1;
1352
1353 case GLSL_TYPE_STRUCT:
1354 case GLSL_TYPE_INTERFACE:
1355 for (unsigned i = 0; i < this->length; i++)
1356 size += this->fields.structure[i].type->uniform_locations();
1357 return size;
1358 case GLSL_TYPE_ARRAY:
1359 return this->length * this->fields.array->uniform_locations();
1360 default:
1361 return 0;
1362 }
1363 }
1364
1365 unsigned
1366 glsl_type::varying_count() const
1367 {
1368 unsigned size = 0;
1369
1370 switch (this->base_type) {
1371 case GLSL_TYPE_UINT:
1372 case GLSL_TYPE_INT:
1373 case GLSL_TYPE_FLOAT:
1374 case GLSL_TYPE_DOUBLE:
1375 case GLSL_TYPE_BOOL:
1376 return 1;
1377
1378 case GLSL_TYPE_STRUCT:
1379 case GLSL_TYPE_INTERFACE:
1380 for (unsigned i = 0; i < this->length; i++)
1381 size += this->fields.structure[i].type->varying_count();
1382 return size;
1383 case GLSL_TYPE_ARRAY:
1384 /* Don't count innermost array elements */
1385 if (this->without_array()->is_record() ||
1386 this->without_array()->is_interface() ||
1387 this->fields.array->is_array())
1388 return this->length * this->fields.array->varying_count();
1389 else
1390 return this->fields.array->varying_count();
1391 default:
1392 assert(!"unsupported varying type");
1393 return 0;
1394 }
1395 }
1396
1397 bool
1398 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1399 _mesa_glsl_parse_state *state) const
1400 {
1401 if (this == desired)
1402 return true;
1403
1404 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1405 * state, we're doing intra-stage function linking where these checks have
1406 * already been done.
1407 */
1408 if (state && (state->es_shader || !state->is_version(120, 0)))
1409 return false;
1410
1411 /* There is no conversion among matrix types. */
1412 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1413 return false;
1414
1415 /* Vector size must match. */
1416 if (this->vector_elements != desired->vector_elements)
1417 return false;
1418
1419 /* int and uint can be converted to float. */
1420 if (desired->is_float() && this->is_integer())
1421 return true;
1422
1423 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1424 * can be converted to uint. Note that state may be NULL here, when
1425 * resolving function calls in the linker. By this time, all the
1426 * state-dependent checks have already happened though, so allow anything
1427 * that's allowed in any shader version.
1428 */
1429 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable ||
1430 state->MESA_shader_integer_functions_enable) &&
1431 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1432 return true;
1433
1434 /* No implicit conversions from double. */
1435 if ((!state || state->has_double()) && this->is_double())
1436 return false;
1437
1438 /* Conversions from different types to double. */
1439 if ((!state || state->has_double()) && desired->is_double()) {
1440 if (this->is_float())
1441 return true;
1442 if (this->is_integer())
1443 return true;
1444 }
1445
1446 return false;
1447 }
1448
1449 unsigned
1450 glsl_type::std140_base_alignment(bool row_major) const
1451 {
1452 unsigned N = is_64bit() ? 8 : 4;
1453
1454 /* (1) If the member is a scalar consuming <N> basic machine units, the
1455 * base alignment is <N>.
1456 *
1457 * (2) If the member is a two- or four-component vector with components
1458 * consuming <N> basic machine units, the base alignment is 2<N> or
1459 * 4<N>, respectively.
1460 *
1461 * (3) If the member is a three-component vector with components consuming
1462 * <N> basic machine units, the base alignment is 4<N>.
1463 */
1464 if (this->is_scalar() || this->is_vector()) {
1465 switch (this->vector_elements) {
1466 case 1:
1467 return N;
1468 case 2:
1469 return 2 * N;
1470 case 3:
1471 case 4:
1472 return 4 * N;
1473 }
1474 }
1475
1476 /* (4) If the member is an array of scalars or vectors, the base alignment
1477 * and array stride are set to match the base alignment of a single
1478 * array element, according to rules (1), (2), and (3), and rounded up
1479 * to the base alignment of a vec4. The array may have padding at the
1480 * end; the base offset of the member following the array is rounded up
1481 * to the next multiple of the base alignment.
1482 *
1483 * (6) If the member is an array of <S> column-major matrices with <C>
1484 * columns and <R> rows, the matrix is stored identically to a row of
1485 * <S>*<C> column vectors with <R> components each, according to rule
1486 * (4).
1487 *
1488 * (8) If the member is an array of <S> row-major matrices with <C> columns
1489 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1490 * row vectors with <C> components each, according to rule (4).
1491 *
1492 * (10) If the member is an array of <S> structures, the <S> elements of
1493 * the array are laid out in order, according to rule (9).
1494 */
1495 if (this->is_array()) {
1496 if (this->fields.array->is_scalar() ||
1497 this->fields.array->is_vector() ||
1498 this->fields.array->is_matrix()) {
1499 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1500 } else {
1501 assert(this->fields.array->is_record() ||
1502 this->fields.array->is_array());
1503 return this->fields.array->std140_base_alignment(row_major);
1504 }
1505 }
1506
1507 /* (5) If the member is a column-major matrix with <C> columns and
1508 * <R> rows, the matrix is stored identically to an array of
1509 * <C> column vectors with <R> components each, according to
1510 * rule (4).
1511 *
1512 * (7) If the member is a row-major matrix with <C> columns and <R>
1513 * rows, the matrix is stored identically to an array of <R>
1514 * row vectors with <C> components each, according to rule (4).
1515 */
1516 if (this->is_matrix()) {
1517 const struct glsl_type *vec_type, *array_type;
1518 int c = this->matrix_columns;
1519 int r = this->vector_elements;
1520
1521 if (row_major) {
1522 vec_type = get_instance(base_type, c, 1);
1523 array_type = glsl_type::get_array_instance(vec_type, r);
1524 } else {
1525 vec_type = get_instance(base_type, r, 1);
1526 array_type = glsl_type::get_array_instance(vec_type, c);
1527 }
1528
1529 return array_type->std140_base_alignment(false);
1530 }
1531
1532 /* (9) If the member is a structure, the base alignment of the
1533 * structure is <N>, where <N> is the largest base alignment
1534 * value of any of its members, and rounded up to the base
1535 * alignment of a vec4. The individual members of this
1536 * sub-structure are then assigned offsets by applying this set
1537 * of rules recursively, where the base offset of the first
1538 * member of the sub-structure is equal to the aligned offset
1539 * of the structure. The structure may have padding at the end;
1540 * the base offset of the member following the sub-structure is
1541 * rounded up to the next multiple of the base alignment of the
1542 * structure.
1543 */
1544 if (this->is_record()) {
1545 unsigned base_alignment = 16;
1546 for (unsigned i = 0; i < this->length; i++) {
1547 bool field_row_major = row_major;
1548 const enum glsl_matrix_layout matrix_layout =
1549 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1550 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1551 field_row_major = true;
1552 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1553 field_row_major = false;
1554 }
1555
1556 const struct glsl_type *field_type = this->fields.structure[i].type;
1557 base_alignment = MAX2(base_alignment,
1558 field_type->std140_base_alignment(field_row_major));
1559 }
1560 return base_alignment;
1561 }
1562
1563 assert(!"not reached");
1564 return -1;
1565 }
1566
1567 unsigned
1568 glsl_type::std140_size(bool row_major) const
1569 {
1570 unsigned N = is_64bit() ? 8 : 4;
1571
1572 /* (1) If the member is a scalar consuming <N> basic machine units, the
1573 * base alignment is <N>.
1574 *
1575 * (2) If the member is a two- or four-component vector with components
1576 * consuming <N> basic machine units, the base alignment is 2<N> or
1577 * 4<N>, respectively.
1578 *
1579 * (3) If the member is a three-component vector with components consuming
1580 * <N> basic machine units, the base alignment is 4<N>.
1581 */
1582 if (this->is_scalar() || this->is_vector()) {
1583 return this->vector_elements * N;
1584 }
1585
1586 /* (5) If the member is a column-major matrix with <C> columns and
1587 * <R> rows, the matrix is stored identically to an array of
1588 * <C> column vectors with <R> components each, according to
1589 * rule (4).
1590 *
1591 * (6) If the member is an array of <S> column-major matrices with <C>
1592 * columns and <R> rows, the matrix is stored identically to a row of
1593 * <S>*<C> column vectors with <R> components each, according to rule
1594 * (4).
1595 *
1596 * (7) If the member is a row-major matrix with <C> columns and <R>
1597 * rows, the matrix is stored identically to an array of <R>
1598 * row vectors with <C> components each, according to rule (4).
1599 *
1600 * (8) If the member is an array of <S> row-major matrices with <C> columns
1601 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1602 * row vectors with <C> components each, according to rule (4).
1603 */
1604 if (this->without_array()->is_matrix()) {
1605 const struct glsl_type *element_type;
1606 const struct glsl_type *vec_type;
1607 unsigned int array_len;
1608
1609 if (this->is_array()) {
1610 element_type = this->without_array();
1611 array_len = this->arrays_of_arrays_size();
1612 } else {
1613 element_type = this;
1614 array_len = 1;
1615 }
1616
1617 if (row_major) {
1618 vec_type = get_instance(element_type->base_type,
1619 element_type->matrix_columns, 1);
1620
1621 array_len *= element_type->vector_elements;
1622 } else {
1623 vec_type = get_instance(element_type->base_type,
1624 element_type->vector_elements, 1);
1625 array_len *= element_type->matrix_columns;
1626 }
1627 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1628 array_len);
1629
1630 return array_type->std140_size(false);
1631 }
1632
1633 /* (4) If the member is an array of scalars or vectors, the base alignment
1634 * and array stride are set to match the base alignment of a single
1635 * array element, according to rules (1), (2), and (3), and rounded up
1636 * to the base alignment of a vec4. The array may have padding at the
1637 * end; the base offset of the member following the array is rounded up
1638 * to the next multiple of the base alignment.
1639 *
1640 * (10) If the member is an array of <S> structures, the <S> elements of
1641 * the array are laid out in order, according to rule (9).
1642 */
1643 if (this->is_array()) {
1644 if (this->without_array()->is_record()) {
1645 return this->arrays_of_arrays_size() *
1646 this->without_array()->std140_size(row_major);
1647 } else {
1648 unsigned element_base_align =
1649 this->without_array()->std140_base_alignment(row_major);
1650 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1651 }
1652 }
1653
1654 /* (9) If the member is a structure, the base alignment of the
1655 * structure is <N>, where <N> is the largest base alignment
1656 * value of any of its members, and rounded up to the base
1657 * alignment of a vec4. The individual members of this
1658 * sub-structure are then assigned offsets by applying this set
1659 * of rules recursively, where the base offset of the first
1660 * member of the sub-structure is equal to the aligned offset
1661 * of the structure. The structure may have padding at the end;
1662 * the base offset of the member following the sub-structure is
1663 * rounded up to the next multiple of the base alignment of the
1664 * structure.
1665 */
1666 if (this->is_record() || this->is_interface()) {
1667 unsigned size = 0;
1668 unsigned max_align = 0;
1669
1670 for (unsigned i = 0; i < this->length; i++) {
1671 bool field_row_major = row_major;
1672 const enum glsl_matrix_layout matrix_layout =
1673 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1674 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1675 field_row_major = true;
1676 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1677 field_row_major = false;
1678 }
1679
1680 const struct glsl_type *field_type = this->fields.structure[i].type;
1681 unsigned align = field_type->std140_base_alignment(field_row_major);
1682
1683 /* Ignore unsized arrays when calculating size */
1684 if (field_type->is_unsized_array())
1685 continue;
1686
1687 size = glsl_align(size, align);
1688 size += field_type->std140_size(field_row_major);
1689
1690 max_align = MAX2(align, max_align);
1691
1692 if (field_type->is_record() && (i + 1 < this->length))
1693 size = glsl_align(size, 16);
1694 }
1695 size = glsl_align(size, MAX2(max_align, 16));
1696 return size;
1697 }
1698
1699 assert(!"not reached");
1700 return -1;
1701 }
1702
1703 unsigned
1704 glsl_type::std430_base_alignment(bool row_major) const
1705 {
1706
1707 unsigned N = is_64bit() ? 8 : 4;
1708
1709 /* (1) If the member is a scalar consuming <N> basic machine units, the
1710 * base alignment is <N>.
1711 *
1712 * (2) If the member is a two- or four-component vector with components
1713 * consuming <N> basic machine units, the base alignment is 2<N> or
1714 * 4<N>, respectively.
1715 *
1716 * (3) If the member is a three-component vector with components consuming
1717 * <N> basic machine units, the base alignment is 4<N>.
1718 */
1719 if (this->is_scalar() || this->is_vector()) {
1720 switch (this->vector_elements) {
1721 case 1:
1722 return N;
1723 case 2:
1724 return 2 * N;
1725 case 3:
1726 case 4:
1727 return 4 * N;
1728 }
1729 }
1730
1731 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1732 *
1733 * "When using the std430 storage layout, shader storage blocks will be
1734 * laid out in buffer storage identically to uniform and shader storage
1735 * blocks using the std140 layout, except that the base alignment and
1736 * stride of arrays of scalars and vectors in rule 4 and of structures
1737 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1738 */
1739
1740 /* (1) If the member is a scalar consuming <N> basic machine units, the
1741 * base alignment is <N>.
1742 *
1743 * (2) If the member is a two- or four-component vector with components
1744 * consuming <N> basic machine units, the base alignment is 2<N> or
1745 * 4<N>, respectively.
1746 *
1747 * (3) If the member is a three-component vector with components consuming
1748 * <N> basic machine units, the base alignment is 4<N>.
1749 */
1750 if (this->is_array())
1751 return this->fields.array->std430_base_alignment(row_major);
1752
1753 /* (5) If the member is a column-major matrix with <C> columns and
1754 * <R> rows, the matrix is stored identically to an array of
1755 * <C> column vectors with <R> components each, according to
1756 * rule (4).
1757 *
1758 * (7) If the member is a row-major matrix with <C> columns and <R>
1759 * rows, the matrix is stored identically to an array of <R>
1760 * row vectors with <C> components each, according to rule (4).
1761 */
1762 if (this->is_matrix()) {
1763 const struct glsl_type *vec_type, *array_type;
1764 int c = this->matrix_columns;
1765 int r = this->vector_elements;
1766
1767 if (row_major) {
1768 vec_type = get_instance(base_type, c, 1);
1769 array_type = glsl_type::get_array_instance(vec_type, r);
1770 } else {
1771 vec_type = get_instance(base_type, r, 1);
1772 array_type = glsl_type::get_array_instance(vec_type, c);
1773 }
1774
1775 return array_type->std430_base_alignment(false);
1776 }
1777
1778 /* (9) If the member is a structure, the base alignment of the
1779 * structure is <N>, where <N> is the largest base alignment
1780 * value of any of its members, and rounded up to the base
1781 * alignment of a vec4. The individual members of this
1782 * sub-structure are then assigned offsets by applying this set
1783 * of rules recursively, where the base offset of the first
1784 * member of the sub-structure is equal to the aligned offset
1785 * of the structure. The structure may have padding at the end;
1786 * the base offset of the member following the sub-structure is
1787 * rounded up to the next multiple of the base alignment of the
1788 * structure.
1789 */
1790 if (this->is_record()) {
1791 unsigned base_alignment = 0;
1792 for (unsigned i = 0; i < this->length; i++) {
1793 bool field_row_major = row_major;
1794 const enum glsl_matrix_layout matrix_layout =
1795 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1796 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1797 field_row_major = true;
1798 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1799 field_row_major = false;
1800 }
1801
1802 const struct glsl_type *field_type = this->fields.structure[i].type;
1803 base_alignment = MAX2(base_alignment,
1804 field_type->std430_base_alignment(field_row_major));
1805 }
1806 assert(base_alignment > 0);
1807 return base_alignment;
1808 }
1809 assert(!"not reached");
1810 return -1;
1811 }
1812
1813 unsigned
1814 glsl_type::std430_array_stride(bool row_major) const
1815 {
1816 unsigned N = is_64bit() ? 8 : 4;
1817
1818 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1819 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1820 *
1821 * (3) If the member is a three-component vector with components consuming
1822 * <N> basic machine units, the base alignment is 4<N>.
1823 */
1824 if (this->is_vector() && this->vector_elements == 3)
1825 return 4 * N;
1826
1827 /* By default use std430_size(row_major) */
1828 return this->std430_size(row_major);
1829 }
1830
1831 unsigned
1832 glsl_type::std430_size(bool row_major) const
1833 {
1834 unsigned N = is_64bit() ? 8 : 4;
1835
1836 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1837 *
1838 * "When using the std430 storage layout, shader storage blocks will be
1839 * laid out in buffer storage identically to uniform and shader storage
1840 * blocks using the std140 layout, except that the base alignment and
1841 * stride of arrays of scalars and vectors in rule 4 and of structures
1842 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1843 */
1844 if (this->is_scalar() || this->is_vector())
1845 return this->vector_elements * N;
1846
1847 if (this->without_array()->is_matrix()) {
1848 const struct glsl_type *element_type;
1849 const struct glsl_type *vec_type;
1850 unsigned int array_len;
1851
1852 if (this->is_array()) {
1853 element_type = this->without_array();
1854 array_len = this->arrays_of_arrays_size();
1855 } else {
1856 element_type = this;
1857 array_len = 1;
1858 }
1859
1860 if (row_major) {
1861 vec_type = get_instance(element_type->base_type,
1862 element_type->matrix_columns, 1);
1863
1864 array_len *= element_type->vector_elements;
1865 } else {
1866 vec_type = get_instance(element_type->base_type,
1867 element_type->vector_elements, 1);
1868 array_len *= element_type->matrix_columns;
1869 }
1870 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1871 array_len);
1872
1873 return array_type->std430_size(false);
1874 }
1875
1876 if (this->is_array()) {
1877 if (this->without_array()->is_record())
1878 return this->arrays_of_arrays_size() *
1879 this->without_array()->std430_size(row_major);
1880 else
1881 return this->arrays_of_arrays_size() *
1882 this->without_array()->std430_base_alignment(row_major);
1883 }
1884
1885 if (this->is_record() || this->is_interface()) {
1886 unsigned size = 0;
1887 unsigned max_align = 0;
1888
1889 for (unsigned i = 0; i < this->length; i++) {
1890 bool field_row_major = row_major;
1891 const enum glsl_matrix_layout matrix_layout =
1892 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1893 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1894 field_row_major = true;
1895 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1896 field_row_major = false;
1897 }
1898
1899 const struct glsl_type *field_type = this->fields.structure[i].type;
1900 unsigned align = field_type->std430_base_alignment(field_row_major);
1901 size = glsl_align(size, align);
1902 size += field_type->std430_size(field_row_major);
1903
1904 max_align = MAX2(align, max_align);
1905 }
1906 size = glsl_align(size, max_align);
1907 return size;
1908 }
1909
1910 assert(!"not reached");
1911 return -1;
1912 }
1913
1914 unsigned
1915 glsl_type::count_attribute_slots(bool is_vertex_input) const
1916 {
1917 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1918 *
1919 * "A scalar input counts the same amount against this limit as a vec4,
1920 * so applications may want to consider packing groups of four
1921 * unrelated float inputs together into a vector to better utilize the
1922 * capabilities of the underlying hardware. A matrix input will use up
1923 * multiple locations. The number of locations used will equal the
1924 * number of columns in the matrix."
1925 *
1926 * The spec does not explicitly say how arrays are counted. However, it
1927 * should be safe to assume the total number of slots consumed by an array
1928 * is the number of entries in the array multiplied by the number of slots
1929 * consumed by a single element of the array.
1930 *
1931 * The spec says nothing about how structs are counted, because vertex
1932 * attributes are not allowed to be (or contain) structs. However, Mesa
1933 * allows varying structs, the number of varying slots taken up by a
1934 * varying struct is simply equal to the sum of the number of slots taken
1935 * up by each element.
1936 *
1937 * Doubles are counted different depending on whether they are vertex
1938 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1939 * take one location no matter what size they are, otherwise dvec3/4
1940 * take two locations.
1941 */
1942 switch (this->base_type) {
1943 case GLSL_TYPE_UINT:
1944 case GLSL_TYPE_INT:
1945 case GLSL_TYPE_FLOAT:
1946 case GLSL_TYPE_BOOL:
1947 return this->matrix_columns;
1948 case GLSL_TYPE_DOUBLE:
1949 if (this->vector_elements > 2 && !is_vertex_input)
1950 return this->matrix_columns * 2;
1951 else
1952 return this->matrix_columns;
1953 case GLSL_TYPE_STRUCT:
1954 case GLSL_TYPE_INTERFACE: {
1955 unsigned size = 0;
1956
1957 for (unsigned i = 0; i < this->length; i++)
1958 size += this->fields.structure[i].type->count_attribute_slots(is_vertex_input);
1959
1960 return size;
1961 }
1962
1963 case GLSL_TYPE_ARRAY:
1964 return this->length * this->fields.array->count_attribute_slots(is_vertex_input);
1965
1966 case GLSL_TYPE_FUNCTION:
1967 case GLSL_TYPE_SAMPLER:
1968 case GLSL_TYPE_IMAGE:
1969 case GLSL_TYPE_ATOMIC_UINT:
1970 case GLSL_TYPE_VOID:
1971 case GLSL_TYPE_SUBROUTINE:
1972 case GLSL_TYPE_ERROR:
1973 break;
1974 }
1975
1976 assert(!"Unexpected type in count_attribute_slots()");
1977
1978 return 0;
1979 }
1980
1981 int
1982 glsl_type::coordinate_components() const
1983 {
1984 int size;
1985
1986 switch (sampler_dimensionality) {
1987 case GLSL_SAMPLER_DIM_1D:
1988 case GLSL_SAMPLER_DIM_BUF:
1989 size = 1;
1990 break;
1991 case GLSL_SAMPLER_DIM_2D:
1992 case GLSL_SAMPLER_DIM_RECT:
1993 case GLSL_SAMPLER_DIM_MS:
1994 case GLSL_SAMPLER_DIM_EXTERNAL:
1995 case GLSL_SAMPLER_DIM_SUBPASS:
1996 size = 2;
1997 break;
1998 case GLSL_SAMPLER_DIM_3D:
1999 case GLSL_SAMPLER_DIM_CUBE:
2000 size = 3;
2001 break;
2002 default:
2003 assert(!"Should not get here.");
2004 size = 1;
2005 break;
2006 }
2007
2008 /* Array textures need an additional component for the array index, except
2009 * for cubemap array images that behave like a 2D array of interleaved
2010 * cubemap faces.
2011 */
2012 if (sampler_array &&
2013 !(base_type == GLSL_TYPE_IMAGE &&
2014 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2015 size += 1;
2016
2017 return size;
2018 }
2019
2020 /**
2021 * Declarations of type flyweights (glsl_type::_foo_type) and
2022 * convenience pointers (glsl_type::foo_type).
2023 * @{
2024 */
2025 #define DECL_TYPE(NAME, ...) \
2026 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2027 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2028
2029 #define STRUCT_TYPE(NAME)
2030
2031 #include "compiler/builtin_type_macros.h"
2032 /** @} */