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