4a2c8790790ca65a6650c722aceb13270e8b4d35
[mesa.git] / src / glsl / 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 <stdlib.h>
26 #include "main/core.h" /* for Elements */
27 #include "glsl_symbol_table.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_types.h"
30 #include "builtin_types.h"
31 extern "C" {
32 #include "program/hash_table.h"
33 }
34
35 hash_table *glsl_type::array_types = NULL;
36 hash_table *glsl_type::record_types = NULL;
37 hash_table *glsl_type::interface_types = NULL;
38 void *glsl_type::mem_ctx = NULL;
39
40 void
41 glsl_type::init_ralloc_type_ctx(void)
42 {
43 if (glsl_type::mem_ctx == NULL) {
44 glsl_type::mem_ctx = ralloc_autofree_context();
45 assert(glsl_type::mem_ctx != NULL);
46 }
47 }
48
49 glsl_type::glsl_type(GLenum gl_type,
50 glsl_base_type base_type, unsigned vector_elements,
51 unsigned matrix_columns, const char *name) :
52 gl_type(gl_type),
53 base_type(base_type),
54 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
55 sampler_type(0), interface_packing(0),
56 vector_elements(vector_elements), matrix_columns(matrix_columns),
57 length(0)
58 {
59 init_ralloc_type_ctx();
60 this->name = ralloc_strdup(this->mem_ctx, name);
61 /* Neither dimension is zero or both dimensions are zero.
62 */
63 assert((vector_elements == 0) == (matrix_columns == 0));
64 memset(& fields, 0, sizeof(fields));
65 }
66
67 glsl_type::glsl_type(GLenum gl_type,
68 enum glsl_sampler_dim dim, bool shadow, bool array,
69 unsigned type, const char *name) :
70 gl_type(gl_type),
71 base_type(GLSL_TYPE_SAMPLER),
72 sampler_dimensionality(dim), sampler_shadow(shadow),
73 sampler_array(array), sampler_type(type), interface_packing(0),
74 vector_elements(0), matrix_columns(0),
75 length(0)
76 {
77 init_ralloc_type_ctx();
78 this->name = ralloc_strdup(this->mem_ctx, name);
79 memset(& fields, 0, sizeof(fields));
80 }
81
82 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
83 const char *name) :
84 base_type(GLSL_TYPE_STRUCT),
85 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
86 sampler_type(0), interface_packing(0),
87 vector_elements(0), matrix_columns(0),
88 length(num_fields)
89 {
90 unsigned int i;
91
92 init_ralloc_type_ctx();
93 this->name = ralloc_strdup(this->mem_ctx, name);
94 this->fields.structure = ralloc_array(this->mem_ctx,
95 glsl_struct_field, length);
96 for (i = 0; i < length; i++) {
97 this->fields.structure[i].type = fields[i].type;
98 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
99 fields[i].name);
100 this->fields.structure[i].row_major = fields[i].row_major;
101 }
102 }
103
104 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
105 enum glsl_interface_packing packing, const char *name) :
106 base_type(GLSL_TYPE_INTERFACE),
107 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
108 sampler_type(0), interface_packing((unsigned) packing),
109 vector_elements(0), matrix_columns(0),
110 length(num_fields)
111 {
112 unsigned int i;
113
114 init_ralloc_type_ctx();
115 this->name = ralloc_strdup(this->mem_ctx, name);
116 this->fields.structure = ralloc_array(this->mem_ctx,
117 glsl_struct_field, length);
118 for (i = 0; i < length; i++) {
119 this->fields.structure[i].type = fields[i].type;
120 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
121 fields[i].name);
122 this->fields.structure[i].row_major = fields[i].row_major;
123 }
124 }
125
126 static void
127 add_types_to_symbol_table(glsl_symbol_table *symtab,
128 const struct glsl_type *types,
129 unsigned num_types, bool warn,
130 bool skip_1d)
131 {
132 (void) warn;
133
134 for (unsigned i = 0; i < num_types; i++) {
135 if (skip_1d && types[i].base_type == GLSL_TYPE_SAMPLER
136 && types[i].sampler_dimensionality == GLSL_SAMPLER_DIM_1D)
137 continue;
138
139 symtab->add_type(types[i].name, & types[i]);
140 }
141 }
142
143 bool
144 glsl_type::contains_sampler() const
145 {
146 if (this->is_array()) {
147 return this->fields.array->contains_sampler();
148 } else if (this->is_record()) {
149 for (unsigned int i = 0; i < this->length; i++) {
150 if (this->fields.structure[i].type->contains_sampler())
151 return true;
152 }
153 return false;
154 } else {
155 return this->is_sampler();
156 }
157 }
158
159 gl_texture_index
160 glsl_type::sampler_index() const
161 {
162 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
163
164 assert(t->is_sampler());
165
166 switch (t->sampler_dimensionality) {
167 case GLSL_SAMPLER_DIM_1D:
168 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
169 case GLSL_SAMPLER_DIM_2D:
170 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
171 case GLSL_SAMPLER_DIM_3D:
172 return TEXTURE_3D_INDEX;
173 case GLSL_SAMPLER_DIM_CUBE:
174 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
175 case GLSL_SAMPLER_DIM_RECT:
176 return TEXTURE_RECT_INDEX;
177 case GLSL_SAMPLER_DIM_BUF:
178 return TEXTURE_BUFFER_INDEX;
179 case GLSL_SAMPLER_DIM_EXTERNAL:
180 return TEXTURE_EXTERNAL_INDEX;
181 default:
182 assert(!"Should not get here.");
183 return TEXTURE_BUFFER_INDEX;
184 }
185 }
186
187 void
188 glsl_type::generate_100ES_types(glsl_symbol_table *symtab)
189 {
190 bool skip_1d = false;
191 add_types_to_symbol_table(symtab, builtin_core_types,
192 Elements(builtin_core_types),
193 false, skip_1d);
194 add_types_to_symbol_table(symtab, builtin_structure_types,
195 Elements(builtin_structure_types),
196 false, skip_1d);
197 add_types_to_symbol_table(symtab, void_type, 1, false, skip_1d);
198 }
199
200 void
201 glsl_type::generate_300ES_types(glsl_symbol_table *symtab)
202 {
203 /* GLSL 3.00 ES types are the same as GLSL 1.30 types, except that 1D
204 * samplers are skipped, and samplerCubeShadow is added.
205 */
206 bool add_deprecated = false;
207 bool skip_1d = true;
208
209 generate_130_types(symtab, add_deprecated, skip_1d);
210
211 add_types_to_symbol_table(symtab, &_samplerCubeShadow_type, 1, false,
212 skip_1d);
213 }
214
215 void
216 glsl_type::generate_110_types(glsl_symbol_table *symtab, bool add_deprecated,
217 bool skip_1d)
218 {
219 generate_100ES_types(symtab);
220
221 add_types_to_symbol_table(symtab, builtin_110_types,
222 Elements(builtin_110_types),
223 false, skip_1d);
224 add_types_to_symbol_table(symtab, &_sampler3D_type, 1, false, skip_1d);
225 if (add_deprecated) {
226 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
227 Elements(builtin_110_deprecated_structure_types),
228 false, skip_1d);
229 }
230 }
231
232
233 void
234 glsl_type::generate_120_types(glsl_symbol_table *symtab, bool add_deprecated,
235 bool skip_1d)
236 {
237 generate_110_types(symtab, add_deprecated, skip_1d);
238
239 add_types_to_symbol_table(symtab, builtin_120_types,
240 Elements(builtin_120_types), false, skip_1d);
241 }
242
243
244 void
245 glsl_type::generate_130_types(glsl_symbol_table *symtab, bool add_deprecated,
246 bool skip_1d)
247 {
248 generate_120_types(symtab, add_deprecated, skip_1d);
249
250 add_types_to_symbol_table(symtab, builtin_130_types,
251 Elements(builtin_130_types), false, skip_1d);
252 generate_EXT_texture_array_types(symtab, false);
253 }
254
255
256 void
257 glsl_type::generate_140_types(glsl_symbol_table *symtab)
258 {
259 bool skip_1d = false;
260
261 generate_130_types(symtab, false, skip_1d);
262
263 add_types_to_symbol_table(symtab, builtin_140_types,
264 Elements(builtin_140_types), false, skip_1d);
265
266 add_types_to_symbol_table(symtab, builtin_EXT_texture_buffer_object_types,
267 Elements(builtin_EXT_texture_buffer_object_types),
268 false, skip_1d);
269 }
270
271
272 void
273 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab,
274 bool warn)
275 {
276 bool skip_1d = false;
277
278 add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
279 Elements(builtin_ARB_texture_rectangle_types),
280 warn, skip_1d);
281 }
282
283
284 void
285 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab,
286 bool warn)
287 {
288 bool skip_1d = false;
289
290 add_types_to_symbol_table(symtab, builtin_EXT_texture_array_types,
291 Elements(builtin_EXT_texture_array_types),
292 warn, skip_1d);
293 }
294
295
296 void
297 glsl_type::generate_OES_texture_3D_types(glsl_symbol_table *symtab, bool warn)
298 {
299 bool skip_1d = false;
300
301 add_types_to_symbol_table(symtab, &_sampler3D_type, 1, warn, skip_1d);
302 }
303
304
305 void
306 glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table *symtab,
307 bool warn)
308 {
309 bool skip_1d = false;
310
311 add_types_to_symbol_table(symtab, builtin_OES_EGL_image_external_types,
312 Elements(builtin_OES_EGL_image_external_types),
313 warn, skip_1d);
314 }
315
316 void
317 glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table *symtab,
318 bool warn)
319 {
320 bool skip_1d = false;
321
322 add_types_to_symbol_table(symtab, builtin_ARB_texture_cube_map_array_types,
323 Elements(builtin_ARB_texture_cube_map_array_types),
324 warn, skip_1d);
325 }
326
327 void
328 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
329 {
330 if (state->es_shader) {
331 switch (state->language_version) {
332 case 100:
333 assert(state->es_shader);
334 glsl_type::generate_100ES_types(state->symbols);
335 break;
336 case 300:
337 glsl_type::generate_300ES_types(state->symbols);
338 break;
339 default:
340 assert(!"Unexpected language version");
341 break;
342 }
343 } else {
344 bool skip_1d = false;
345 switch (state->language_version) {
346 case 110:
347 glsl_type::generate_110_types(state->symbols, true, skip_1d);
348 break;
349 case 120:
350 glsl_type::generate_120_types(state->symbols, true, skip_1d);
351 break;
352 case 130:
353 glsl_type::generate_130_types(state->symbols, true, skip_1d);
354 break;
355 case 140:
356 glsl_type::generate_140_types(state->symbols);
357 break;
358 default:
359 assert(!"Unexpected language version");
360 break;
361 }
362 }
363
364 if (state->ARB_texture_rectangle_enable ||
365 state->is_version(140, 0)) {
366 glsl_type::generate_ARB_texture_rectangle_types(state->symbols,
367 state->ARB_texture_rectangle_warn);
368 }
369 if (state->OES_texture_3D_enable
370 && state->is_version(0, 100)) {
371 glsl_type::generate_OES_texture_3D_types(state->symbols,
372 state->OES_texture_3D_warn);
373 }
374
375 if (state->EXT_texture_array_enable
376 && !state->is_version(130, 0)) {
377 // These are already included in 130; don't create twice.
378 glsl_type::generate_EXT_texture_array_types(state->symbols,
379 state->EXT_texture_array_warn);
380 }
381
382 /* We cannot check for language_version == 100 here because we need the
383 * types to support fixed-function program generation. But this is fine
384 * since the extension is never enabled for OpenGL contexts.
385 */
386 if (state->OES_EGL_image_external_enable) {
387 glsl_type::generate_OES_EGL_image_external_types(state->symbols,
388 state->OES_EGL_image_external_warn);
389 }
390
391 if (state->ARB_texture_cube_map_array_enable) {
392 glsl_type::generate_ARB_texture_cube_map_array_types(state->symbols,
393 state->ARB_texture_cube_map_array_warn);
394 }
395 }
396
397
398 const glsl_type *glsl_type::get_base_type() const
399 {
400 switch (base_type) {
401 case GLSL_TYPE_UINT:
402 return uint_type;
403 case GLSL_TYPE_INT:
404 return int_type;
405 case GLSL_TYPE_FLOAT:
406 return float_type;
407 case GLSL_TYPE_BOOL:
408 return bool_type;
409 default:
410 return error_type;
411 }
412 }
413
414
415 const glsl_type *glsl_type::get_scalar_type() const
416 {
417 const glsl_type *type = this;
418
419 /* Handle arrays */
420 while (type->base_type == GLSL_TYPE_ARRAY)
421 type = type->fields.array;
422
423 /* Handle vectors and matrices */
424 switch (type->base_type) {
425 case GLSL_TYPE_UINT:
426 return uint_type;
427 case GLSL_TYPE_INT:
428 return int_type;
429 case GLSL_TYPE_FLOAT:
430 return float_type;
431 default:
432 /* Handle everything else */
433 return type;
434 }
435 }
436
437
438 void
439 _mesa_glsl_release_types(void)
440 {
441 if (glsl_type::array_types != NULL) {
442 hash_table_dtor(glsl_type::array_types);
443 glsl_type::array_types = NULL;
444 }
445
446 if (glsl_type::record_types != NULL) {
447 hash_table_dtor(glsl_type::record_types);
448 glsl_type::record_types = NULL;
449 }
450 }
451
452
453 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
454 base_type(GLSL_TYPE_ARRAY),
455 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
456 sampler_type(0), interface_packing(0),
457 vector_elements(0), matrix_columns(0),
458 name(NULL), length(length)
459 {
460 this->fields.array = array;
461 /* Inherit the gl type of the base. The GL type is used for
462 * uniform/statevar handling in Mesa and the arrayness of the type
463 * is represented by the size rather than the type.
464 */
465 this->gl_type = array->gl_type;
466
467 /* Allow a maximum of 10 characters for the array size. This is enough
468 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
469 * NUL.
470 */
471 const unsigned name_length = strlen(array->name) + 10 + 3;
472 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
473
474 if (length == 0)
475 snprintf(n, name_length, "%s[]", array->name);
476 else
477 snprintf(n, name_length, "%s[%u]", array->name, length);
478
479 this->name = n;
480 }
481
482
483 const glsl_type *
484 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
485 {
486 if (base_type == GLSL_TYPE_VOID)
487 return void_type;
488
489 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
490 return error_type;
491
492 /* Treat GLSL vectors as Nx1 matrices.
493 */
494 if (columns == 1) {
495 switch (base_type) {
496 case GLSL_TYPE_UINT:
497 return uint_type + (rows - 1);
498 case GLSL_TYPE_INT:
499 return int_type + (rows - 1);
500 case GLSL_TYPE_FLOAT:
501 return float_type + (rows - 1);
502 case GLSL_TYPE_BOOL:
503 return bool_type + (rows - 1);
504 default:
505 return error_type;
506 }
507 } else {
508 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
509 return error_type;
510
511 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
512 * combinations are valid:
513 *
514 * 1 2 3 4
515 * 1
516 * 2 x x x
517 * 3 x x x
518 * 4 x x x
519 */
520 #define IDX(c,r) (((c-1)*3) + (r-1))
521
522 switch (IDX(columns, rows)) {
523 case IDX(2,2): return mat2_type;
524 case IDX(2,3): return mat2x3_type;
525 case IDX(2,4): return mat2x4_type;
526 case IDX(3,2): return mat3x2_type;
527 case IDX(3,3): return mat3_type;
528 case IDX(3,4): return mat3x4_type;
529 case IDX(4,2): return mat4x2_type;
530 case IDX(4,3): return mat4x3_type;
531 case IDX(4,4): return mat4_type;
532 default: return error_type;
533 }
534 }
535
536 assert(!"Should not get here.");
537 return error_type;
538 }
539
540
541 const glsl_type *
542 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
543 {
544
545 if (array_types == NULL) {
546 array_types = hash_table_ctor(64, hash_table_string_hash,
547 hash_table_string_compare);
548 }
549
550 /* Generate a name using the base type pointer in the key. This is
551 * done because the name of the base type may not be unique across
552 * shaders. For example, two shaders may have different record types
553 * named 'foo'.
554 */
555 char key[128];
556 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
557
558 const glsl_type *t = (glsl_type *) hash_table_find(array_types, key);
559 if (t == NULL) {
560 t = new glsl_type(base, array_size);
561
562 hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key));
563 }
564
565 assert(t->base_type == GLSL_TYPE_ARRAY);
566 assert(t->length == array_size);
567 assert(t->fields.array == base);
568
569 return t;
570 }
571
572
573 int
574 glsl_type::record_key_compare(const void *a, const void *b)
575 {
576 const glsl_type *const key1 = (glsl_type *) a;
577 const glsl_type *const key2 = (glsl_type *) b;
578
579 /* Return zero is the types match (there is zero difference) or non-zero
580 * otherwise.
581 */
582 if (strcmp(key1->name, key2->name) != 0)
583 return 1;
584
585 if (key1->length != key2->length)
586 return 1;
587
588 if (key1->interface_packing != key2->interface_packing)
589 return 1;
590
591 for (unsigned i = 0; i < key1->length; i++) {
592 if (key1->fields.structure[i].type != key2->fields.structure[i].type)
593 return 1;
594 if (strcmp(key1->fields.structure[i].name,
595 key2->fields.structure[i].name) != 0)
596 return 1;
597 if (key1->fields.structure[i].row_major
598 != key2->fields.structure[i].row_major)
599 return 1;
600 }
601
602 return 0;
603 }
604
605
606 unsigned
607 glsl_type::record_key_hash(const void *a)
608 {
609 const glsl_type *const key = (glsl_type *) a;
610 char hash_key[128];
611 unsigned size = 0;
612
613 size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
614
615 for (unsigned i = 0; i < key->length; i++) {
616 if (size >= sizeof(hash_key))
617 break;
618
619 size += snprintf(& hash_key[size], sizeof(hash_key) - size,
620 "%p", (void *) key->fields.structure[i].type);
621 }
622
623 return hash_table_string_hash(& hash_key);
624 }
625
626
627 const glsl_type *
628 glsl_type::get_record_instance(const glsl_struct_field *fields,
629 unsigned num_fields,
630 const char *name)
631 {
632 const glsl_type key(fields, num_fields, name);
633
634 if (record_types == NULL) {
635 record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
636 }
637
638 const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
639 if (t == NULL) {
640 t = new glsl_type(fields, num_fields, name);
641
642 hash_table_insert(record_types, (void *) t, t);
643 }
644
645 assert(t->base_type == GLSL_TYPE_STRUCT);
646 assert(t->length == num_fields);
647 assert(strcmp(t->name, name) == 0);
648
649 return t;
650 }
651
652
653 const glsl_type *
654 glsl_type::get_interface_instance(const glsl_struct_field *fields,
655 unsigned num_fields,
656 enum glsl_interface_packing packing,
657 const char *name)
658 {
659 const glsl_type key(fields, num_fields, packing, name);
660
661 if (interface_types == NULL) {
662 interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
663 }
664
665 const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
666 if (t == NULL) {
667 t = new glsl_type(fields, num_fields, packing, name);
668
669 hash_table_insert(interface_types, (void *) t, t);
670 }
671
672 assert(t->base_type == GLSL_TYPE_INTERFACE);
673 assert(t->length == num_fields);
674 assert(strcmp(t->name, name) == 0);
675
676 return t;
677 }
678
679
680 const glsl_type *
681 glsl_type::field_type(const char *name) const
682 {
683 if (this->base_type != GLSL_TYPE_STRUCT
684 && this->base_type != GLSL_TYPE_INTERFACE)
685 return error_type;
686
687 for (unsigned i = 0; i < this->length; i++) {
688 if (strcmp(name, this->fields.structure[i].name) == 0)
689 return this->fields.structure[i].type;
690 }
691
692 return error_type;
693 }
694
695
696 int
697 glsl_type::field_index(const char *name) const
698 {
699 if (this->base_type != GLSL_TYPE_STRUCT
700 && this->base_type != GLSL_TYPE_INTERFACE)
701 return -1;
702
703 for (unsigned i = 0; i < this->length; i++) {
704 if (strcmp(name, this->fields.structure[i].name) == 0)
705 return i;
706 }
707
708 return -1;
709 }
710
711
712 unsigned
713 glsl_type::component_slots() const
714 {
715 switch (this->base_type) {
716 case GLSL_TYPE_UINT:
717 case GLSL_TYPE_INT:
718 case GLSL_TYPE_FLOAT:
719 case GLSL_TYPE_BOOL:
720 return this->components();
721
722 case GLSL_TYPE_STRUCT:
723 case GLSL_TYPE_INTERFACE: {
724 unsigned size = 0;
725
726 for (unsigned i = 0; i < this->length; i++)
727 size += this->fields.structure[i].type->component_slots();
728
729 return size;
730 }
731
732 case GLSL_TYPE_ARRAY:
733 return this->length * this->fields.array->component_slots();
734
735 case GLSL_TYPE_SAMPLER:
736 case GLSL_TYPE_VOID:
737 case GLSL_TYPE_ERROR:
738 break;
739 }
740
741 return 0;
742 }
743
744 bool
745 glsl_type::can_implicitly_convert_to(const glsl_type *desired) const
746 {
747 if (this == desired)
748 return true;
749
750 /* There is no conversion among matrix types. */
751 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
752 return false;
753
754 /* int and uint can be converted to float. */
755 return desired->is_float()
756 && this->is_integer()
757 && this->vector_elements == desired->vector_elements;
758 }
759
760 unsigned
761 glsl_type::std140_base_alignment(bool row_major) const
762 {
763 /* (1) If the member is a scalar consuming <N> basic machine units, the
764 * base alignment is <N>.
765 *
766 * (2) If the member is a two- or four-component vector with components
767 * consuming <N> basic machine units, the base alignment is 2<N> or
768 * 4<N>, respectively.
769 *
770 * (3) If the member is a three-component vector with components consuming
771 * <N> basic machine units, the base alignment is 4<N>.
772 */
773 if (this->is_scalar() || this->is_vector()) {
774 switch (this->vector_elements) {
775 case 1:
776 return 4;
777 case 2:
778 return 8;
779 case 3:
780 case 4:
781 return 16;
782 }
783 }
784
785 /* (4) If the member is an array of scalars or vectors, the base alignment
786 * and array stride are set to match the base alignment of a single
787 * array element, according to rules (1), (2), and (3), and rounded up
788 * to the base alignment of a vec4. The array may have padding at the
789 * end; the base offset of the member following the array is rounded up
790 * to the next multiple of the base alignment.
791 *
792 * (6) If the member is an array of <S> column-major matrices with <C>
793 * columns and <R> rows, the matrix is stored identically to a row of
794 * <S>*<C> column vectors with <R> components each, according to rule
795 * (4).
796 *
797 * (8) If the member is an array of <S> row-major matrices with <C> columns
798 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
799 * row vectors with <C> components each, according to rule (4).
800 *
801 * (10) If the member is an array of <S> structures, the <S> elements of
802 * the array are laid out in order, according to rule (9).
803 */
804 if (this->is_array()) {
805 if (this->fields.array->is_scalar() ||
806 this->fields.array->is_vector() ||
807 this->fields.array->is_matrix()) {
808 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
809 } else {
810 assert(this->fields.array->is_record());
811 return this->fields.array->std140_base_alignment(row_major);
812 }
813 }
814
815 /* (5) If the member is a column-major matrix with <C> columns and
816 * <R> rows, the matrix is stored identically to an array of
817 * <C> column vectors with <R> components each, according to
818 * rule (4).
819 *
820 * (7) If the member is a row-major matrix with <C> columns and <R>
821 * rows, the matrix is stored identically to an array of <R>
822 * row vectors with <C> components each, according to rule (4).
823 */
824 if (this->is_matrix()) {
825 const struct glsl_type *vec_type, *array_type;
826 int c = this->matrix_columns;
827 int r = this->vector_elements;
828
829 if (row_major) {
830 vec_type = get_instance(GLSL_TYPE_FLOAT, c, 1);
831 array_type = glsl_type::get_array_instance(vec_type, r);
832 } else {
833 vec_type = get_instance(GLSL_TYPE_FLOAT, r, 1);
834 array_type = glsl_type::get_array_instance(vec_type, c);
835 }
836
837 return array_type->std140_base_alignment(false);
838 }
839
840 /* (9) If the member is a structure, the base alignment of the
841 * structure is <N>, where <N> is the largest base alignment
842 * value of any of its members, and rounded up to the base
843 * alignment of a vec4. The individual members of this
844 * sub-structure are then assigned offsets by applying this set
845 * of rules recursively, where the base offset of the first
846 * member of the sub-structure is equal to the aligned offset
847 * of the structure. The structure may have padding at the end;
848 * the base offset of the member following the sub-structure is
849 * rounded up to the next multiple of the base alignment of the
850 * structure.
851 */
852 if (this->is_record()) {
853 unsigned base_alignment = 16;
854 for (unsigned i = 0; i < this->length; i++) {
855 const struct glsl_type *field_type = this->fields.structure[i].type;
856 base_alignment = MAX2(base_alignment,
857 field_type->std140_base_alignment(row_major));
858 }
859 return base_alignment;
860 }
861
862 assert(!"not reached");
863 return -1;
864 }
865
866 unsigned
867 glsl_type::std140_size(bool row_major) const
868 {
869 /* (1) If the member is a scalar consuming <N> basic machine units, the
870 * base alignment is <N>.
871 *
872 * (2) If the member is a two- or four-component vector with components
873 * consuming <N> basic machine units, the base alignment is 2<N> or
874 * 4<N>, respectively.
875 *
876 * (3) If the member is a three-component vector with components consuming
877 * <N> basic machine units, the base alignment is 4<N>.
878 */
879 if (this->is_scalar() || this->is_vector()) {
880 return this->vector_elements * 4;
881 }
882
883 /* (5) If the member is a column-major matrix with <C> columns and
884 * <R> rows, the matrix is stored identically to an array of
885 * <C> column vectors with <R> components each, according to
886 * rule (4).
887 *
888 * (6) If the member is an array of <S> column-major matrices with <C>
889 * columns and <R> rows, the matrix is stored identically to a row of
890 * <S>*<C> column vectors with <R> components each, according to rule
891 * (4).
892 *
893 * (7) If the member is a row-major matrix with <C> columns and <R>
894 * rows, the matrix is stored identically to an array of <R>
895 * row vectors with <C> components each, according to rule (4).
896 *
897 * (8) If the member is an array of <S> row-major matrices with <C> columns
898 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
899 * row vectors with <C> components each, according to rule (4).
900 */
901 if (this->is_matrix() || (this->is_array() &&
902 this->fields.array->is_matrix())) {
903 const struct glsl_type *element_type;
904 const struct glsl_type *vec_type;
905 unsigned int array_len;
906
907 if (this->is_array()) {
908 element_type = this->fields.array;
909 array_len = this->length;
910 } else {
911 element_type = this;
912 array_len = 1;
913 }
914
915 if (row_major) {
916 vec_type = get_instance(GLSL_TYPE_FLOAT,
917 element_type->matrix_columns, 1);
918 array_len *= element_type->vector_elements;
919 } else {
920 vec_type = get_instance(GLSL_TYPE_FLOAT,
921 element_type->vector_elements, 1);
922 array_len *= element_type->matrix_columns;
923 }
924 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
925 array_len);
926
927 return array_type->std140_size(false);
928 }
929
930 /* (4) If the member is an array of scalars or vectors, the base alignment
931 * and array stride are set to match the base alignment of a single
932 * array element, according to rules (1), (2), and (3), and rounded up
933 * to the base alignment of a vec4. The array may have padding at the
934 * end; the base offset of the member following the array is rounded up
935 * to the next multiple of the base alignment.
936 *
937 * (10) If the member is an array of <S> structures, the <S> elements of
938 * the array are laid out in order, according to rule (9).
939 */
940 if (this->is_array()) {
941 if (this->fields.array->is_record()) {
942 return this->length * this->fields.array->std140_size(row_major);
943 } else {
944 unsigned element_base_align =
945 this->fields.array->std140_base_alignment(row_major);
946 return this->length * MAX2(element_base_align, 16);
947 }
948 }
949
950 /* (9) If the member is a structure, the base alignment of the
951 * structure is <N>, where <N> is the largest base alignment
952 * value of any of its members, and rounded up to the base
953 * alignment of a vec4. The individual members of this
954 * sub-structure are then assigned offsets by applying this set
955 * of rules recursively, where the base offset of the first
956 * member of the sub-structure is equal to the aligned offset
957 * of the structure. The structure may have padding at the end;
958 * the base offset of the member following the sub-structure is
959 * rounded up to the next multiple of the base alignment of the
960 * structure.
961 */
962 if (this->is_record()) {
963 unsigned size = 0;
964 for (unsigned i = 0; i < this->length; i++) {
965 const struct glsl_type *field_type = this->fields.structure[i].type;
966 unsigned align = field_type->std140_base_alignment(row_major);
967 size = glsl_align(size, align);
968 size += field_type->std140_size(row_major);
969 }
970 size = glsl_align(size,
971 this->fields.structure[0].type->std140_base_alignment(row_major));
972 return size;
973 }
974
975 assert(!"not reached");
976 return -1;
977 }