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