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