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