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