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