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