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