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