i965/gen6 depth surface: calculate depth (array size) for depth surface
[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 for (unsigned i = 0; i < this->length; i++) {
494 if (this->fields.structure[i].type != b->fields.structure[i].type)
495 return false;
496 if (strcmp(this->fields.structure[i].name,
497 b->fields.structure[i].name) != 0)
498 return false;
499 if (this->fields.structure[i].matrix_layout
500 != b->fields.structure[i].matrix_layout)
501 return false;
502 if (this->fields.structure[i].location
503 != b->fields.structure[i].location)
504 return false;
505 if (this->fields.structure[i].interpolation
506 != b->fields.structure[i].interpolation)
507 return false;
508 if (this->fields.structure[i].centroid
509 != b->fields.structure[i].centroid)
510 return false;
511 if (this->fields.structure[i].sample
512 != b->fields.structure[i].sample)
513 return false;
514 }
515
516 return true;
517 }
518
519
520 int
521 glsl_type::record_key_compare(const void *a, const void *b)
522 {
523 const glsl_type *const key1 = (glsl_type *) a;
524 const glsl_type *const key2 = (glsl_type *) b;
525
526 /* Return zero is the types match (there is zero difference) or non-zero
527 * otherwise.
528 */
529 if (strcmp(key1->name, key2->name) != 0)
530 return 1;
531
532 return !key1->record_compare(key2);
533 }
534
535
536 unsigned
537 glsl_type::record_key_hash(const void *a)
538 {
539 const glsl_type *const key = (glsl_type *) a;
540 char hash_key[128];
541 unsigned size = 0;
542
543 size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
544
545 for (unsigned i = 0; i < key->length; i++) {
546 if (size >= sizeof(hash_key))
547 break;
548
549 size += snprintf(& hash_key[size], sizeof(hash_key) - size,
550 "%p", (void *) key->fields.structure[i].type);
551 }
552
553 return hash_table_string_hash(& hash_key);
554 }
555
556
557 const glsl_type *
558 glsl_type::get_record_instance(const glsl_struct_field *fields,
559 unsigned num_fields,
560 const char *name)
561 {
562 const glsl_type key(fields, num_fields, name);
563
564 if (record_types == NULL) {
565 record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
566 }
567
568 const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
569 if (t == NULL) {
570 t = new glsl_type(fields, num_fields, name);
571
572 hash_table_insert(record_types, (void *) t, t);
573 }
574
575 assert(t->base_type == GLSL_TYPE_STRUCT);
576 assert(t->length == num_fields);
577 assert(strcmp(t->name, name) == 0);
578
579 return t;
580 }
581
582
583 const glsl_type *
584 glsl_type::get_interface_instance(const glsl_struct_field *fields,
585 unsigned num_fields,
586 enum glsl_interface_packing packing,
587 const char *block_name)
588 {
589 const glsl_type key(fields, num_fields, packing, block_name);
590
591 if (interface_types == NULL) {
592 interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
593 }
594
595 const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
596 if (t == NULL) {
597 t = new glsl_type(fields, num_fields, packing, block_name);
598
599 hash_table_insert(interface_types, (void *) t, t);
600 }
601
602 assert(t->base_type == GLSL_TYPE_INTERFACE);
603 assert(t->length == num_fields);
604 assert(strcmp(t->name, block_name) == 0);
605
606 return t;
607 }
608
609
610 const glsl_type *
611 glsl_type::field_type(const char *name) const
612 {
613 if (this->base_type != GLSL_TYPE_STRUCT
614 && this->base_type != GLSL_TYPE_INTERFACE)
615 return error_type;
616
617 for (unsigned i = 0; i < this->length; i++) {
618 if (strcmp(name, this->fields.structure[i].name) == 0)
619 return this->fields.structure[i].type;
620 }
621
622 return error_type;
623 }
624
625
626 int
627 glsl_type::field_index(const char *name) const
628 {
629 if (this->base_type != GLSL_TYPE_STRUCT
630 && this->base_type != GLSL_TYPE_INTERFACE)
631 return -1;
632
633 for (unsigned i = 0; i < this->length; i++) {
634 if (strcmp(name, this->fields.structure[i].name) == 0)
635 return i;
636 }
637
638 return -1;
639 }
640
641
642 unsigned
643 glsl_type::component_slots() const
644 {
645 switch (this->base_type) {
646 case GLSL_TYPE_UINT:
647 case GLSL_TYPE_INT:
648 case GLSL_TYPE_FLOAT:
649 case GLSL_TYPE_BOOL:
650 return this->components();
651
652 case GLSL_TYPE_STRUCT:
653 case GLSL_TYPE_INTERFACE: {
654 unsigned size = 0;
655
656 for (unsigned i = 0; i < this->length; i++)
657 size += this->fields.structure[i].type->component_slots();
658
659 return size;
660 }
661
662 case GLSL_TYPE_ARRAY:
663 return this->length * this->fields.array->component_slots();
664
665 case GLSL_TYPE_IMAGE:
666 return 1;
667
668 case GLSL_TYPE_SAMPLER:
669 case GLSL_TYPE_ATOMIC_UINT:
670 case GLSL_TYPE_VOID:
671 case GLSL_TYPE_ERROR:
672 break;
673 }
674
675 return 0;
676 }
677
678 unsigned
679 glsl_type::uniform_locations() const
680 {
681 if (this->is_matrix())
682 return 1;
683
684 unsigned size = 0;
685
686 switch (this->base_type) {
687 case GLSL_TYPE_STRUCT:
688 case GLSL_TYPE_INTERFACE:
689 for (unsigned i = 0; i < this->length; i++)
690 size += this->fields.structure[i].type->uniform_locations();
691 return size;
692 case GLSL_TYPE_ARRAY:
693 return this->length * this->fields.array->uniform_locations();
694 default:
695 break;
696 }
697
698 /* The location count for many types match with component_slots() result,
699 * all expections should be handled above.
700 */
701 return component_slots();
702 }
703
704 bool
705 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
706 _mesa_glsl_parse_state *state) const
707 {
708 if (this == desired)
709 return true;
710
711 /* There is no conversion among matrix types. */
712 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
713 return false;
714
715 /* Vector size must match. */
716 if (this->vector_elements != desired->vector_elements)
717 return false;
718
719 /* int and uint can be converted to float. */
720 if (desired->is_float() && this->is_integer())
721 return true;
722
723 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
724 * Note that state may be NULL here, when resolving function calls in the
725 * linker. By this time, all the state-dependent checks have already
726 * happened though, so allow anything that's allowed in any shader version. */
727 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
728 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
729 return true;
730
731 return false;
732 }
733
734 unsigned
735 glsl_type::std140_base_alignment(bool row_major) const
736 {
737 /* (1) If the member is a scalar consuming <N> basic machine units, the
738 * base alignment is <N>.
739 *
740 * (2) If the member is a two- or four-component vector with components
741 * consuming <N> basic machine units, the base alignment is 2<N> or
742 * 4<N>, respectively.
743 *
744 * (3) If the member is a three-component vector with components consuming
745 * <N> basic machine units, the base alignment is 4<N>.
746 */
747 if (this->is_scalar() || this->is_vector()) {
748 switch (this->vector_elements) {
749 case 1:
750 return 4;
751 case 2:
752 return 8;
753 case 3:
754 case 4:
755 return 16;
756 }
757 }
758
759 /* (4) If the member is an array of scalars or vectors, the base alignment
760 * and array stride are set to match the base alignment of a single
761 * array element, according to rules (1), (2), and (3), and rounded up
762 * to the base alignment of a vec4. The array may have padding at the
763 * end; the base offset of the member following the array is rounded up
764 * to the next multiple of the base alignment.
765 *
766 * (6) If the member is an array of <S> column-major matrices with <C>
767 * columns and <R> rows, the matrix is stored identically to a row of
768 * <S>*<C> column vectors with <R> components each, according to rule
769 * (4).
770 *
771 * (8) If the member is an array of <S> row-major matrices with <C> columns
772 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
773 * row vectors with <C> components each, according to rule (4).
774 *
775 * (10) If the member is an array of <S> structures, the <S> elements of
776 * the array are laid out in order, according to rule (9).
777 */
778 if (this->is_array()) {
779 if (this->fields.array->is_scalar() ||
780 this->fields.array->is_vector() ||
781 this->fields.array->is_matrix()) {
782 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
783 } else {
784 assert(this->fields.array->is_record());
785 return this->fields.array->std140_base_alignment(row_major);
786 }
787 }
788
789 /* (5) If the member is a column-major matrix with <C> columns and
790 * <R> rows, the matrix is stored identically to an array of
791 * <C> column vectors with <R> components each, according to
792 * rule (4).
793 *
794 * (7) If the member is a row-major matrix with <C> columns and <R>
795 * rows, the matrix is stored identically to an array of <R>
796 * row vectors with <C> components each, according to rule (4).
797 */
798 if (this->is_matrix()) {
799 const struct glsl_type *vec_type, *array_type;
800 int c = this->matrix_columns;
801 int r = this->vector_elements;
802
803 if (row_major) {
804 vec_type = get_instance(GLSL_TYPE_FLOAT, c, 1);
805 array_type = glsl_type::get_array_instance(vec_type, r);
806 } else {
807 vec_type = get_instance(GLSL_TYPE_FLOAT, r, 1);
808 array_type = glsl_type::get_array_instance(vec_type, c);
809 }
810
811 return array_type->std140_base_alignment(false);
812 }
813
814 /* (9) If the member is a structure, the base alignment of the
815 * structure is <N>, where <N> is the largest base alignment
816 * value of any of its members, and rounded up to the base
817 * alignment of a vec4. The individual members of this
818 * sub-structure are then assigned offsets by applying this set
819 * of rules recursively, where the base offset of the first
820 * member of the sub-structure is equal to the aligned offset
821 * of the structure. The structure may have padding at the end;
822 * the base offset of the member following the sub-structure is
823 * rounded up to the next multiple of the base alignment of the
824 * structure.
825 */
826 if (this->is_record()) {
827 unsigned base_alignment = 16;
828 for (unsigned i = 0; i < this->length; i++) {
829 bool field_row_major = row_major;
830 const enum glsl_matrix_layout matrix_layout =
831 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
832 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
833 field_row_major = true;
834 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
835 field_row_major = false;
836 }
837
838 const struct glsl_type *field_type = this->fields.structure[i].type;
839 base_alignment = MAX2(base_alignment,
840 field_type->std140_base_alignment(field_row_major));
841 }
842 return base_alignment;
843 }
844
845 assert(!"not reached");
846 return -1;
847 }
848
849 unsigned
850 glsl_type::std140_size(bool row_major) const
851 {
852 /* (1) If the member is a scalar consuming <N> basic machine units, the
853 * base alignment is <N>.
854 *
855 * (2) If the member is a two- or four-component vector with components
856 * consuming <N> basic machine units, the base alignment is 2<N> or
857 * 4<N>, respectively.
858 *
859 * (3) If the member is a three-component vector with components consuming
860 * <N> basic machine units, the base alignment is 4<N>.
861 */
862 if (this->is_scalar() || this->is_vector()) {
863 return this->vector_elements * 4;
864 }
865
866 /* (5) If the member is a column-major matrix with <C> columns and
867 * <R> rows, the matrix is stored identically to an array of
868 * <C> column vectors with <R> components each, according to
869 * rule (4).
870 *
871 * (6) If the member is an array of <S> column-major matrices with <C>
872 * columns and <R> rows, the matrix is stored identically to a row of
873 * <S>*<C> column vectors with <R> components each, according to rule
874 * (4).
875 *
876 * (7) If the member is a row-major matrix with <C> columns and <R>
877 * rows, the matrix is stored identically to an array of <R>
878 * row vectors with <C> components each, according to rule (4).
879 *
880 * (8) If the member is an array of <S> row-major matrices with <C> columns
881 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
882 * row vectors with <C> components each, according to rule (4).
883 */
884 if (this->without_array()->is_matrix()) {
885 const struct glsl_type *element_type;
886 const struct glsl_type *vec_type;
887 unsigned int array_len;
888
889 if (this->is_array()) {
890 element_type = this->fields.array;
891 array_len = this->length;
892 } else {
893 element_type = this;
894 array_len = 1;
895 }
896
897 if (row_major) {
898 vec_type = get_instance(GLSL_TYPE_FLOAT,
899 element_type->matrix_columns, 1);
900 array_len *= element_type->vector_elements;
901 } else {
902 vec_type = get_instance(GLSL_TYPE_FLOAT,
903 element_type->vector_elements, 1);
904 array_len *= element_type->matrix_columns;
905 }
906 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
907 array_len);
908
909 return array_type->std140_size(false);
910 }
911
912 /* (4) If the member is an array of scalars or vectors, the base alignment
913 * and array stride are set to match the base alignment of a single
914 * array element, according to rules (1), (2), and (3), and rounded up
915 * to the base alignment of a vec4. The array may have padding at the
916 * end; the base offset of the member following the array is rounded up
917 * to the next multiple of the base alignment.
918 *
919 * (10) If the member is an array of <S> structures, the <S> elements of
920 * the array are laid out in order, according to rule (9).
921 */
922 if (this->is_array()) {
923 if (this->fields.array->is_record()) {
924 return this->length * this->fields.array->std140_size(row_major);
925 } else {
926 unsigned element_base_align =
927 this->fields.array->std140_base_alignment(row_major);
928 return this->length * MAX2(element_base_align, 16);
929 }
930 }
931
932 /* (9) If the member is a structure, the base alignment of the
933 * structure is <N>, where <N> is the largest base alignment
934 * value of any of its members, and rounded up to the base
935 * alignment of a vec4. The individual members of this
936 * sub-structure are then assigned offsets by applying this set
937 * of rules recursively, where the base offset of the first
938 * member of the sub-structure is equal to the aligned offset
939 * of the structure. The structure may have padding at the end;
940 * the base offset of the member following the sub-structure is
941 * rounded up to the next multiple of the base alignment of the
942 * structure.
943 */
944 if (this->is_record()) {
945 unsigned size = 0;
946 unsigned max_align = 0;
947
948 for (unsigned i = 0; i < this->length; i++) {
949 bool field_row_major = row_major;
950 const enum glsl_matrix_layout matrix_layout =
951 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
952 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
953 field_row_major = true;
954 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
955 field_row_major = false;
956 }
957
958 const struct glsl_type *field_type = this->fields.structure[i].type;
959 unsigned align = field_type->std140_base_alignment(field_row_major);
960 size = glsl_align(size, align);
961 size += field_type->std140_size(field_row_major);
962
963 max_align = MAX2(align, max_align);
964
965 if (field_type->is_record() && (i + 1 < this->length))
966 size = glsl_align(size, 16);
967 }
968 size = glsl_align(size, max_align);
969 return size;
970 }
971
972 assert(!"not reached");
973 return -1;
974 }
975
976
977 unsigned
978 glsl_type::count_attribute_slots() const
979 {
980 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
981 *
982 * "A scalar input counts the same amount against this limit as a vec4,
983 * so applications may want to consider packing groups of four
984 * unrelated float inputs together into a vector to better utilize the
985 * capabilities of the underlying hardware. A matrix input will use up
986 * multiple locations. The number of locations used will equal the
987 * number of columns in the matrix."
988 *
989 * The spec does not explicitly say how arrays are counted. However, it
990 * should be safe to assume the total number of slots consumed by an array
991 * is the number of entries in the array multiplied by the number of slots
992 * consumed by a single element of the array.
993 *
994 * The spec says nothing about how structs are counted, because vertex
995 * attributes are not allowed to be (or contain) structs. However, Mesa
996 * allows varying structs, the number of varying slots taken up by a
997 * varying struct is simply equal to the sum of the number of slots taken
998 * up by each element.
999 */
1000 switch (this->base_type) {
1001 case GLSL_TYPE_UINT:
1002 case GLSL_TYPE_INT:
1003 case GLSL_TYPE_FLOAT:
1004 case GLSL_TYPE_BOOL:
1005 return this->matrix_columns;
1006
1007 case GLSL_TYPE_STRUCT:
1008 case GLSL_TYPE_INTERFACE: {
1009 unsigned size = 0;
1010
1011 for (unsigned i = 0; i < this->length; i++)
1012 size += this->fields.structure[i].type->count_attribute_slots();
1013
1014 return size;
1015 }
1016
1017 case GLSL_TYPE_ARRAY:
1018 return this->length * this->fields.array->count_attribute_slots();
1019
1020 case GLSL_TYPE_SAMPLER:
1021 case GLSL_TYPE_IMAGE:
1022 case GLSL_TYPE_ATOMIC_UINT:
1023 case GLSL_TYPE_VOID:
1024 case GLSL_TYPE_ERROR:
1025 break;
1026 }
1027
1028 assert(!"Unexpected type in count_attribute_slots()");
1029
1030 return 0;
1031 }
1032
1033 int
1034 glsl_type::coordinate_components() const
1035 {
1036 int size;
1037
1038 switch (sampler_dimensionality) {
1039 case GLSL_SAMPLER_DIM_1D:
1040 case GLSL_SAMPLER_DIM_BUF:
1041 size = 1;
1042 break;
1043 case GLSL_SAMPLER_DIM_2D:
1044 case GLSL_SAMPLER_DIM_RECT:
1045 case GLSL_SAMPLER_DIM_MS:
1046 case GLSL_SAMPLER_DIM_EXTERNAL:
1047 size = 2;
1048 break;
1049 case GLSL_SAMPLER_DIM_3D:
1050 case GLSL_SAMPLER_DIM_CUBE:
1051 size = 3;
1052 break;
1053 default:
1054 assert(!"Should not get here.");
1055 size = 1;
1056 break;
1057 }
1058
1059 /* Array textures need an additional component for the array index. */
1060 if (sampler_array)
1061 size += 1;
1062
1063 return size;
1064 }