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