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