glsl: disallow implicit conversions in ESSL shaders
[mesa.git] / src / compiler / 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/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/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 hash_table *glsl_type::subroutine_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 this->fields.structure[i].patch = fields[i].patch;
127 this->fields.structure[i].image_read_only = fields[i].image_read_only;
128 this->fields.structure[i].image_write_only = fields[i].image_write_only;
129 this->fields.structure[i].image_coherent = fields[i].image_coherent;
130 this->fields.structure[i].image_volatile = fields[i].image_volatile;
131 this->fields.structure[i].image_restrict = fields[i].image_restrict;
132 this->fields.structure[i].precision = fields[i].precision;
133 }
134
135 mtx_unlock(&glsl_type::mutex);
136 }
137
138 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
139 enum glsl_interface_packing packing, const char *name) :
140 gl_type(0),
141 base_type(GLSL_TYPE_INTERFACE),
142 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
143 sampler_type(0), interface_packing((unsigned) packing),
144 vector_elements(0), matrix_columns(0),
145 length(num_fields)
146 {
147 unsigned int i;
148
149 mtx_lock(&glsl_type::mutex);
150
151 init_ralloc_type_ctx();
152 assert(name != NULL);
153 this->name = ralloc_strdup(this->mem_ctx, name);
154 this->fields.structure = ralloc_array(this->mem_ctx,
155 glsl_struct_field, length);
156 for (i = 0; i < length; i++) {
157 this->fields.structure[i].type = fields[i].type;
158 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
159 fields[i].name);
160 this->fields.structure[i].location = fields[i].location;
161 this->fields.structure[i].interpolation = fields[i].interpolation;
162 this->fields.structure[i].centroid = fields[i].centroid;
163 this->fields.structure[i].sample = fields[i].sample;
164 this->fields.structure[i].matrix_layout = fields[i].matrix_layout;
165 this->fields.structure[i].patch = fields[i].patch;
166 this->fields.structure[i].precision = fields[i].precision;
167 }
168
169 mtx_unlock(&glsl_type::mutex);
170 }
171
172 glsl_type::glsl_type(const char *subroutine_name) :
173 gl_type(0),
174 base_type(GLSL_TYPE_SUBROUTINE),
175 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
176 sampler_type(0), interface_packing(0),
177 vector_elements(1), matrix_columns(1),
178 length(0)
179 {
180 mtx_lock(&glsl_type::mutex);
181
182 init_ralloc_type_ctx();
183 assert(subroutine_name != NULL);
184 this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
185 mtx_unlock(&glsl_type::mutex);
186 }
187
188 bool
189 glsl_type::contains_sampler() const
190 {
191 if (this->is_array()) {
192 return this->fields.array->contains_sampler();
193 } else if (this->is_record()) {
194 for (unsigned int i = 0; i < this->length; i++) {
195 if (this->fields.structure[i].type->contains_sampler())
196 return true;
197 }
198 return false;
199 } else {
200 return this->is_sampler();
201 }
202 }
203
204
205 bool
206 glsl_type::contains_integer() const
207 {
208 if (this->is_array()) {
209 return this->fields.array->contains_integer();
210 } else if (this->is_record()) {
211 for (unsigned int i = 0; i < this->length; i++) {
212 if (this->fields.structure[i].type->contains_integer())
213 return true;
214 }
215 return false;
216 } else {
217 return this->is_integer();
218 }
219 }
220
221 bool
222 glsl_type::contains_double() const
223 {
224 if (this->is_array()) {
225 return this->fields.array->contains_double();
226 } else if (this->is_record()) {
227 for (unsigned int i = 0; i < this->length; i++) {
228 if (this->fields.structure[i].type->contains_double())
229 return true;
230 }
231 return false;
232 } else {
233 return this->is_double();
234 }
235 }
236
237 bool
238 glsl_type::contains_opaque() const {
239 switch (base_type) {
240 case GLSL_TYPE_SAMPLER:
241 case GLSL_TYPE_IMAGE:
242 case GLSL_TYPE_ATOMIC_UINT:
243 return true;
244 case GLSL_TYPE_ARRAY:
245 return fields.array->contains_opaque();
246 case GLSL_TYPE_STRUCT:
247 for (unsigned int i = 0; i < length; i++) {
248 if (fields.structure[i].type->contains_opaque())
249 return true;
250 }
251 return false;
252 default:
253 return false;
254 }
255 }
256
257 bool
258 glsl_type::contains_subroutine() const
259 {
260 if (this->is_array()) {
261 return this->fields.array->contains_subroutine();
262 } else if (this->is_record()) {
263 for (unsigned int i = 0; i < this->length; i++) {
264 if (this->fields.structure[i].type->contains_subroutine())
265 return true;
266 }
267 return false;
268 } else {
269 return this->is_subroutine();
270 }
271 }
272
273 gl_texture_index
274 glsl_type::sampler_index() const
275 {
276 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
277
278 assert(t->is_sampler());
279
280 switch (t->sampler_dimensionality) {
281 case GLSL_SAMPLER_DIM_1D:
282 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
283 case GLSL_SAMPLER_DIM_2D:
284 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
285 case GLSL_SAMPLER_DIM_3D:
286 return TEXTURE_3D_INDEX;
287 case GLSL_SAMPLER_DIM_CUBE:
288 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
289 case GLSL_SAMPLER_DIM_RECT:
290 return TEXTURE_RECT_INDEX;
291 case GLSL_SAMPLER_DIM_BUF:
292 return TEXTURE_BUFFER_INDEX;
293 case GLSL_SAMPLER_DIM_EXTERNAL:
294 return TEXTURE_EXTERNAL_INDEX;
295 case GLSL_SAMPLER_DIM_MS:
296 return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
297 default:
298 assert(!"Should not get here.");
299 return TEXTURE_BUFFER_INDEX;
300 }
301 }
302
303 bool
304 glsl_type::contains_image() const
305 {
306 if (this->is_array()) {
307 return this->fields.array->contains_image();
308 } else if (this->is_record()) {
309 for (unsigned int i = 0; i < this->length; i++) {
310 if (this->fields.structure[i].type->contains_image())
311 return true;
312 }
313 return false;
314 } else {
315 return this->is_image();
316 }
317 }
318
319 const glsl_type *glsl_type::get_base_type() const
320 {
321 switch (base_type) {
322 case GLSL_TYPE_UINT:
323 return uint_type;
324 case GLSL_TYPE_INT:
325 return int_type;
326 case GLSL_TYPE_FLOAT:
327 return float_type;
328 case GLSL_TYPE_DOUBLE:
329 return double_type;
330 case GLSL_TYPE_BOOL:
331 return bool_type;
332 default:
333 return error_type;
334 }
335 }
336
337
338 const glsl_type *glsl_type::get_scalar_type() const
339 {
340 const glsl_type *type = this;
341
342 /* Handle arrays */
343 while (type->base_type == GLSL_TYPE_ARRAY)
344 type = type->fields.array;
345
346 /* Handle vectors and matrices */
347 switch (type->base_type) {
348 case GLSL_TYPE_UINT:
349 return uint_type;
350 case GLSL_TYPE_INT:
351 return int_type;
352 case GLSL_TYPE_FLOAT:
353 return float_type;
354 case GLSL_TYPE_DOUBLE:
355 return double_type;
356 case GLSL_TYPE_BOOL:
357 return bool_type;
358 default:
359 /* Handle everything else */
360 return type;
361 }
362 }
363
364
365 void
366 _mesa_glsl_release_types(void)
367 {
368 /* Should only be called during atexit (either when unloading shared
369 * object, or if process terminates), so no mutex-locking should be
370 * necessary.
371 */
372 if (glsl_type::array_types != NULL) {
373 _mesa_hash_table_destroy(glsl_type::array_types, NULL);
374 glsl_type::array_types = NULL;
375 }
376
377 if (glsl_type::record_types != NULL) {
378 _mesa_hash_table_destroy(glsl_type::record_types, NULL);
379 glsl_type::record_types = NULL;
380 }
381
382 if (glsl_type::interface_types != NULL) {
383 _mesa_hash_table_destroy(glsl_type::interface_types, NULL);
384 glsl_type::interface_types = NULL;
385 }
386 }
387
388
389 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
390 base_type(GLSL_TYPE_ARRAY),
391 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
392 sampler_type(0), interface_packing(0),
393 vector_elements(0), matrix_columns(0),
394 length(length), name(NULL)
395 {
396 this->fields.array = array;
397 /* Inherit the gl type of the base. The GL type is used for
398 * uniform/statevar handling in Mesa and the arrayness of the type
399 * is represented by the size rather than the type.
400 */
401 this->gl_type = array->gl_type;
402
403 /* Allow a maximum of 10 characters for the array size. This is enough
404 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
405 * NUL.
406 */
407 const unsigned name_length = strlen(array->name) + 10 + 3;
408
409 mtx_lock(&glsl_type::mutex);
410 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
411 mtx_unlock(&glsl_type::mutex);
412
413 if (length == 0)
414 snprintf(n, name_length, "%s[]", array->name);
415 else {
416 /* insert outermost dimensions in the correct spot
417 * otherwise the dimension order will be backwards
418 */
419 const char *pos = strchr(array->name, '[');
420 if (pos) {
421 int idx = pos - array->name;
422 snprintf(n, idx+1, "%s", array->name);
423 snprintf(n + idx, name_length - idx, "[%u]%s",
424 length, array->name + idx);
425 } else {
426 snprintf(n, name_length, "%s[%u]", array->name, length);
427 }
428 }
429
430 this->name = n;
431 }
432
433
434 const glsl_type *
435 glsl_type::vec(unsigned components)
436 {
437 if (components == 0 || components > 4)
438 return error_type;
439
440 static const glsl_type *const ts[] = {
441 float_type, vec2_type, vec3_type, vec4_type
442 };
443 return ts[components - 1];
444 }
445
446 const glsl_type *
447 glsl_type::dvec(unsigned components)
448 {
449 if (components == 0 || components > 4)
450 return error_type;
451
452 static const glsl_type *const ts[] = {
453 double_type, dvec2_type, dvec3_type, dvec4_type
454 };
455 return ts[components - 1];
456 }
457
458 const glsl_type *
459 glsl_type::ivec(unsigned components)
460 {
461 if (components == 0 || components > 4)
462 return error_type;
463
464 static const glsl_type *const ts[] = {
465 int_type, ivec2_type, ivec3_type, ivec4_type
466 };
467 return ts[components - 1];
468 }
469
470
471 const glsl_type *
472 glsl_type::uvec(unsigned components)
473 {
474 if (components == 0 || components > 4)
475 return error_type;
476
477 static const glsl_type *const ts[] = {
478 uint_type, uvec2_type, uvec3_type, uvec4_type
479 };
480 return ts[components - 1];
481 }
482
483
484 const glsl_type *
485 glsl_type::bvec(unsigned components)
486 {
487 if (components == 0 || components > 4)
488 return error_type;
489
490 static const glsl_type *const ts[] = {
491 bool_type, bvec2_type, bvec3_type, bvec4_type
492 };
493 return ts[components - 1];
494 }
495
496
497 const glsl_type *
498 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
499 {
500 if (base_type == GLSL_TYPE_VOID)
501 return void_type;
502
503 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
504 return error_type;
505
506 /* Treat GLSL vectors as Nx1 matrices.
507 */
508 if (columns == 1) {
509 switch (base_type) {
510 case GLSL_TYPE_UINT:
511 return uvec(rows);
512 case GLSL_TYPE_INT:
513 return ivec(rows);
514 case GLSL_TYPE_FLOAT:
515 return vec(rows);
516 case GLSL_TYPE_DOUBLE:
517 return dvec(rows);
518 case GLSL_TYPE_BOOL:
519 return bvec(rows);
520 default:
521 return error_type;
522 }
523 } else {
524 if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
525 return error_type;
526
527 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
528 * combinations are valid:
529 *
530 * 1 2 3 4
531 * 1
532 * 2 x x x
533 * 3 x x x
534 * 4 x x x
535 */
536 #define IDX(c,r) (((c-1)*3) + (r-1))
537
538 if (base_type == GLSL_TYPE_DOUBLE) {
539 switch (IDX(columns, rows)) {
540 case IDX(2,2): return dmat2_type;
541 case IDX(2,3): return dmat2x3_type;
542 case IDX(2,4): return dmat2x4_type;
543 case IDX(3,2): return dmat3x2_type;
544 case IDX(3,3): return dmat3_type;
545 case IDX(3,4): return dmat3x4_type;
546 case IDX(4,2): return dmat4x2_type;
547 case IDX(4,3): return dmat4x3_type;
548 case IDX(4,4): return dmat4_type;
549 default: return error_type;
550 }
551 } else {
552 switch (IDX(columns, rows)) {
553 case IDX(2,2): return mat2_type;
554 case IDX(2,3): return mat2x3_type;
555 case IDX(2,4): return mat2x4_type;
556 case IDX(3,2): return mat3x2_type;
557 case IDX(3,3): return mat3_type;
558 case IDX(3,4): return mat3x4_type;
559 case IDX(4,2): return mat4x2_type;
560 case IDX(4,3): return mat4x3_type;
561 case IDX(4,4): return mat4_type;
562 default: return error_type;
563 }
564 }
565 }
566
567 assert(!"Should not get here.");
568 return error_type;
569 }
570
571 const glsl_type *
572 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
573 bool shadow,
574 bool array,
575 glsl_base_type type)
576 {
577 switch (type) {
578 case GLSL_TYPE_FLOAT:
579 switch (dim) {
580 case GLSL_SAMPLER_DIM_1D:
581 if (shadow)
582 return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
583 else
584 return (array ? sampler1DArray_type : sampler1D_type);
585 case GLSL_SAMPLER_DIM_2D:
586 if (shadow)
587 return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
588 else
589 return (array ? sampler2DArray_type : sampler2D_type);
590 case GLSL_SAMPLER_DIM_3D:
591 if (shadow || array)
592 return error_type;
593 else
594 return sampler3D_type;
595 case GLSL_SAMPLER_DIM_CUBE:
596 if (shadow)
597 return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
598 else
599 return (array ? samplerCubeArray_type : samplerCube_type);
600 case GLSL_SAMPLER_DIM_RECT:
601 if (array)
602 return error_type;
603 if (shadow)
604 return sampler2DRectShadow_type;
605 else
606 return sampler2DRect_type;
607 case GLSL_SAMPLER_DIM_BUF:
608 if (shadow || array)
609 return error_type;
610 else
611 return samplerBuffer_type;
612 case GLSL_SAMPLER_DIM_MS:
613 if (shadow)
614 return error_type;
615 return (array ? sampler2DMSArray_type : sampler2DMS_type);
616 case GLSL_SAMPLER_DIM_EXTERNAL:
617 if (shadow || array)
618 return error_type;
619 else
620 return samplerExternalOES_type;
621 }
622 case GLSL_TYPE_INT:
623 if (shadow)
624 return error_type;
625 switch (dim) {
626 case GLSL_SAMPLER_DIM_1D:
627 return (array ? isampler1DArray_type : isampler1D_type);
628 case GLSL_SAMPLER_DIM_2D:
629 return (array ? isampler2DArray_type : isampler2D_type);
630 case GLSL_SAMPLER_DIM_3D:
631 if (array)
632 return error_type;
633 return isampler3D_type;
634 case GLSL_SAMPLER_DIM_CUBE:
635 return (array ? isamplerCubeArray_type : isamplerCube_type);
636 case GLSL_SAMPLER_DIM_RECT:
637 if (array)
638 return error_type;
639 return isampler2DRect_type;
640 case GLSL_SAMPLER_DIM_BUF:
641 if (array)
642 return error_type;
643 return isamplerBuffer_type;
644 case GLSL_SAMPLER_DIM_MS:
645 return (array ? isampler2DMSArray_type : isampler2DMS_type);
646 case GLSL_SAMPLER_DIM_EXTERNAL:
647 return error_type;
648 }
649 case GLSL_TYPE_UINT:
650 if (shadow)
651 return error_type;
652 switch (dim) {
653 case GLSL_SAMPLER_DIM_1D:
654 return (array ? usampler1DArray_type : usampler1D_type);
655 case GLSL_SAMPLER_DIM_2D:
656 return (array ? usampler2DArray_type : usampler2D_type);
657 case GLSL_SAMPLER_DIM_3D:
658 if (array)
659 return error_type;
660 return usampler3D_type;
661 case GLSL_SAMPLER_DIM_CUBE:
662 return (array ? usamplerCubeArray_type : usamplerCube_type);
663 case GLSL_SAMPLER_DIM_RECT:
664 if (array)
665 return error_type;
666 return usampler2DRect_type;
667 case GLSL_SAMPLER_DIM_BUF:
668 if (array)
669 return error_type;
670 return usamplerBuffer_type;
671 case GLSL_SAMPLER_DIM_MS:
672 return (array ? usampler2DMSArray_type : usampler2DMS_type);
673 case GLSL_SAMPLER_DIM_EXTERNAL:
674 return error_type;
675 }
676 default:
677 return error_type;
678 }
679
680 unreachable("switch statement above should be complete");
681 }
682
683 const glsl_type *
684 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
685 {
686 /* Generate a name using the base type pointer in the key. This is
687 * done because the name of the base type may not be unique across
688 * shaders. For example, two shaders may have different record types
689 * named 'foo'.
690 */
691 char key[128];
692 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
693
694 mtx_lock(&glsl_type::mutex);
695
696 if (array_types == NULL) {
697 array_types = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
698 _mesa_key_string_equal);
699 }
700
701 const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
702 if (entry == NULL) {
703 mtx_unlock(&glsl_type::mutex);
704 const glsl_type *t = new glsl_type(base, array_size);
705 mtx_lock(&glsl_type::mutex);
706
707 entry = _mesa_hash_table_insert(array_types,
708 ralloc_strdup(mem_ctx, key),
709 (void *) t);
710 }
711
712 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
713 assert(((glsl_type *) entry->data)->length == array_size);
714 assert(((glsl_type *) entry->data)->fields.array == base);
715
716 mtx_unlock(&glsl_type::mutex);
717
718 return (glsl_type *) entry->data;
719 }
720
721
722 bool
723 glsl_type::record_compare(const glsl_type *b) const
724 {
725 if (this->length != b->length)
726 return false;
727
728 if (this->interface_packing != b->interface_packing)
729 return false;
730
731 /* From the GLSL 4.20 specification (Sec 4.2):
732 *
733 * "Structures must have the same name, sequence of type names, and
734 * type definitions, and field names to be considered the same type."
735 *
736 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
737 *
738 * Note that we cannot force type name check when comparing unnamed
739 * structure types, these have a unique name assigned during parsing.
740 */
741 if (!this->is_anonymous() && !b->is_anonymous())
742 if (strcmp(this->name, b->name) != 0)
743 return false;
744
745 for (unsigned i = 0; i < this->length; i++) {
746 if (this->fields.structure[i].type != b->fields.structure[i].type)
747 return false;
748 if (strcmp(this->fields.structure[i].name,
749 b->fields.structure[i].name) != 0)
750 return false;
751 if (this->fields.structure[i].matrix_layout
752 != b->fields.structure[i].matrix_layout)
753 return false;
754 if (this->fields.structure[i].location
755 != b->fields.structure[i].location)
756 return false;
757 if (this->fields.structure[i].interpolation
758 != b->fields.structure[i].interpolation)
759 return false;
760 if (this->fields.structure[i].centroid
761 != b->fields.structure[i].centroid)
762 return false;
763 if (this->fields.structure[i].sample
764 != b->fields.structure[i].sample)
765 return false;
766 if (this->fields.structure[i].patch
767 != b->fields.structure[i].patch)
768 return false;
769 if (this->fields.structure[i].image_read_only
770 != b->fields.structure[i].image_read_only)
771 return false;
772 if (this->fields.structure[i].image_write_only
773 != b->fields.structure[i].image_write_only)
774 return false;
775 if (this->fields.structure[i].image_coherent
776 != b->fields.structure[i].image_coherent)
777 return false;
778 if (this->fields.structure[i].image_volatile
779 != b->fields.structure[i].image_volatile)
780 return false;
781 if (this->fields.structure[i].image_restrict
782 != b->fields.structure[i].image_restrict)
783 return false;
784 if (this->fields.structure[i].precision
785 != b->fields.structure[i].precision)
786 return false;
787 }
788
789 return true;
790 }
791
792
793 bool
794 glsl_type::record_key_compare(const void *a, const void *b)
795 {
796 const glsl_type *const key1 = (glsl_type *) a;
797 const glsl_type *const key2 = (glsl_type *) b;
798
799 return strcmp(key1->name, key2->name) == 0 && key1->record_compare(key2);
800 }
801
802
803 /**
804 * Generate an integer hash value for a glsl_type structure type.
805 */
806 unsigned
807 glsl_type::record_key_hash(const void *a)
808 {
809 const glsl_type *const key = (glsl_type *) a;
810 uintptr_t hash = key->length;
811 unsigned retval;
812
813 for (unsigned i = 0; i < key->length; i++) {
814 /* casting pointer to uintptr_t */
815 hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
816 }
817
818 if (sizeof(hash) == 8)
819 retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
820 else
821 retval = hash;
822
823 return retval;
824 }
825
826
827 const glsl_type *
828 glsl_type::get_record_instance(const glsl_struct_field *fields,
829 unsigned num_fields,
830 const char *name)
831 {
832 const glsl_type key(fields, num_fields, name);
833
834 mtx_lock(&glsl_type::mutex);
835
836 if (record_types == NULL) {
837 record_types = _mesa_hash_table_create(NULL, record_key_hash,
838 record_key_compare);
839 }
840
841 const struct hash_entry *entry = _mesa_hash_table_search(record_types,
842 &key);
843 if (entry == NULL) {
844 mtx_unlock(&glsl_type::mutex);
845 const glsl_type *t = new glsl_type(fields, num_fields, name);
846 mtx_lock(&glsl_type::mutex);
847
848 entry = _mesa_hash_table_insert(record_types, t, (void *) t);
849 }
850
851 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
852 assert(((glsl_type *) entry->data)->length == num_fields);
853 assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
854
855 mtx_unlock(&glsl_type::mutex);
856
857 return (glsl_type *) entry->data;
858 }
859
860
861 const glsl_type *
862 glsl_type::get_interface_instance(const glsl_struct_field *fields,
863 unsigned num_fields,
864 enum glsl_interface_packing packing,
865 const char *block_name)
866 {
867 const glsl_type key(fields, num_fields, packing, block_name);
868
869 mtx_lock(&glsl_type::mutex);
870
871 if (interface_types == NULL) {
872 interface_types = _mesa_hash_table_create(NULL, record_key_hash,
873 record_key_compare);
874 }
875
876 const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
877 &key);
878 if (entry == NULL) {
879 mtx_unlock(&glsl_type::mutex);
880 const glsl_type *t = new glsl_type(fields, num_fields,
881 packing, block_name);
882 mtx_lock(&glsl_type::mutex);
883
884 entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
885 }
886
887 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
888 assert(((glsl_type *) entry->data)->length == num_fields);
889 assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
890
891 mtx_unlock(&glsl_type::mutex);
892
893 return (glsl_type *) entry->data;
894 }
895
896 const glsl_type *
897 glsl_type::get_subroutine_instance(const char *subroutine_name)
898 {
899 const glsl_type key(subroutine_name);
900
901 mtx_lock(&glsl_type::mutex);
902
903 if (subroutine_types == NULL) {
904 subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
905 record_key_compare);
906 }
907
908 const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
909 &key);
910 if (entry == NULL) {
911 mtx_unlock(&glsl_type::mutex);
912 const glsl_type *t = new glsl_type(subroutine_name);
913 mtx_lock(&glsl_type::mutex);
914
915 entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
916 }
917
918 assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
919 assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
920
921 mtx_unlock(&glsl_type::mutex);
922
923 return (glsl_type *) entry->data;
924 }
925
926
927 const glsl_type *
928 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
929 {
930 if (type_a == type_b) {
931 return type_a;
932 } else if (type_a->is_matrix() && type_b->is_matrix()) {
933 /* Matrix multiply. The columns of A must match the rows of B. Given
934 * the other previously tested constraints, this means the vector type
935 * of a row from A must be the same as the vector type of a column from
936 * B.
937 */
938 if (type_a->row_type() == type_b->column_type()) {
939 /* The resulting matrix has the number of columns of matrix B and
940 * the number of rows of matrix A. We get the row count of A by
941 * looking at the size of a vector that makes up a column. The
942 * transpose (size of a row) is done for B.
943 */
944 const glsl_type *const type =
945 get_instance(type_a->base_type,
946 type_a->column_type()->vector_elements,
947 type_b->row_type()->vector_elements);
948 assert(type != error_type);
949
950 return type;
951 }
952 } else if (type_a->is_matrix()) {
953 /* A is a matrix and B is a column vector. Columns of A must match
954 * rows of B. Given the other previously tested constraints, this
955 * means the vector type of a row from A must be the same as the
956 * vector the type of B.
957 */
958 if (type_a->row_type() == type_b) {
959 /* The resulting vector has a number of elements equal to
960 * the number of rows of matrix A. */
961 const glsl_type *const type =
962 get_instance(type_a->base_type,
963 type_a->column_type()->vector_elements,
964 1);
965 assert(type != error_type);
966
967 return type;
968 }
969 } else {
970 assert(type_b->is_matrix());
971
972 /* A is a row vector and B is a matrix. Columns of A must match rows
973 * of B. Given the other previously tested constraints, this means
974 * the type of A must be the same as the vector type of a column from
975 * B.
976 */
977 if (type_a == type_b->column_type()) {
978 /* The resulting vector has a number of elements equal to
979 * the number of columns of matrix B. */
980 const glsl_type *const type =
981 get_instance(type_a->base_type,
982 type_b->row_type()->vector_elements,
983 1);
984 assert(type != error_type);
985
986 return type;
987 }
988 }
989
990 return error_type;
991 }
992
993
994 const glsl_type *
995 glsl_type::field_type(const char *name) const
996 {
997 if (this->base_type != GLSL_TYPE_STRUCT
998 && this->base_type != GLSL_TYPE_INTERFACE)
999 return error_type;
1000
1001 for (unsigned i = 0; i < this->length; i++) {
1002 if (strcmp(name, this->fields.structure[i].name) == 0)
1003 return this->fields.structure[i].type;
1004 }
1005
1006 return error_type;
1007 }
1008
1009
1010 int
1011 glsl_type::field_index(const char *name) const
1012 {
1013 if (this->base_type != GLSL_TYPE_STRUCT
1014 && this->base_type != GLSL_TYPE_INTERFACE)
1015 return -1;
1016
1017 for (unsigned i = 0; i < this->length; i++) {
1018 if (strcmp(name, this->fields.structure[i].name) == 0)
1019 return i;
1020 }
1021
1022 return -1;
1023 }
1024
1025
1026 unsigned
1027 glsl_type::component_slots() const
1028 {
1029 switch (this->base_type) {
1030 case GLSL_TYPE_UINT:
1031 case GLSL_TYPE_INT:
1032 case GLSL_TYPE_FLOAT:
1033 case GLSL_TYPE_BOOL:
1034 return this->components();
1035
1036 case GLSL_TYPE_DOUBLE:
1037 return 2 * this->components();
1038
1039 case GLSL_TYPE_STRUCT:
1040 case GLSL_TYPE_INTERFACE: {
1041 unsigned size = 0;
1042
1043 for (unsigned i = 0; i < this->length; i++)
1044 size += this->fields.structure[i].type->component_slots();
1045
1046 return size;
1047 }
1048
1049 case GLSL_TYPE_ARRAY:
1050 return this->length * this->fields.array->component_slots();
1051
1052 case GLSL_TYPE_IMAGE:
1053 return 1;
1054 case GLSL_TYPE_SUBROUTINE:
1055 return 1;
1056 case GLSL_TYPE_SAMPLER:
1057 case GLSL_TYPE_ATOMIC_UINT:
1058 case GLSL_TYPE_VOID:
1059 case GLSL_TYPE_ERROR:
1060 break;
1061 }
1062
1063 return 0;
1064 }
1065
1066 unsigned
1067 glsl_type::record_location_offset(unsigned length) const
1068 {
1069 unsigned offset = 0;
1070 const glsl_type *t = this->without_array();
1071 if (t->is_record()) {
1072 assert(length <= t->length);
1073
1074 for (unsigned i = 0; i < length; i++) {
1075 const glsl_type *st = t->fields.structure[i].type;
1076 const glsl_type *wa = st->without_array();
1077 if (wa->is_record()) {
1078 unsigned r_offset = wa->record_location_offset(wa->length);
1079 offset += st->is_array() ?
1080 st->arrays_of_arrays_size() * r_offset : r_offset;
1081 } else if (st->is_array() && st->fields.array->is_array()) {
1082 unsigned outer_array_size = st->length;
1083 const glsl_type *base_type = st->fields.array;
1084
1085 /* For arrays of arrays the outer arrays take up a uniform
1086 * slot for each element. The innermost array elements share a
1087 * single slot so we ignore the innermost array when calculating
1088 * the offset.
1089 */
1090 while (base_type->fields.array->is_array()) {
1091 outer_array_size = outer_array_size * base_type->length;
1092 base_type = base_type->fields.array;
1093 }
1094 offset += outer_array_size;
1095 } else {
1096 /* We dont worry about arrays here because unless the array
1097 * contains a structure or another array it only takes up a single
1098 * uniform slot.
1099 */
1100 offset += 1;
1101 }
1102 }
1103 }
1104 return offset;
1105 }
1106
1107 unsigned
1108 glsl_type::uniform_locations() const
1109 {
1110 unsigned size = 0;
1111
1112 switch (this->base_type) {
1113 case GLSL_TYPE_UINT:
1114 case GLSL_TYPE_INT:
1115 case GLSL_TYPE_FLOAT:
1116 case GLSL_TYPE_DOUBLE:
1117 case GLSL_TYPE_BOOL:
1118 case GLSL_TYPE_SAMPLER:
1119 case GLSL_TYPE_IMAGE:
1120 case GLSL_TYPE_SUBROUTINE:
1121 return 1;
1122
1123 case GLSL_TYPE_STRUCT:
1124 case GLSL_TYPE_INTERFACE:
1125 for (unsigned i = 0; i < this->length; i++)
1126 size += this->fields.structure[i].type->uniform_locations();
1127 return size;
1128 case GLSL_TYPE_ARRAY:
1129 return this->length * this->fields.array->uniform_locations();
1130 default:
1131 return 0;
1132 }
1133 }
1134
1135 bool
1136 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1137 _mesa_glsl_parse_state *state) const
1138 {
1139 if (this == desired)
1140 return true;
1141
1142 /* ESSL does not allow implicit conversions. If there is no state, we're
1143 * doing intra-stage function linking where these checks have already been
1144 * done.
1145 */
1146 if (state && state->es_shader)
1147 return false;
1148
1149 /* There is no conversion among matrix types. */
1150 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1151 return false;
1152
1153 /* Vector size must match. */
1154 if (this->vector_elements != desired->vector_elements)
1155 return false;
1156
1157 /* int and uint can be converted to float. */
1158 if (desired->is_float() && this->is_integer())
1159 return true;
1160
1161 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1162 * Note that state may be NULL here, when resolving function calls in the
1163 * linker. By this time, all the state-dependent checks have already
1164 * happened though, so allow anything that's allowed in any shader version. */
1165 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1166 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1167 return true;
1168
1169 /* No implicit conversions from double. */
1170 if ((!state || state->has_double()) && this->is_double())
1171 return false;
1172
1173 /* Conversions from different types to double. */
1174 if ((!state || state->has_double()) && desired->is_double()) {
1175 if (this->is_float())
1176 return true;
1177 if (this->is_integer())
1178 return true;
1179 }
1180
1181 return false;
1182 }
1183
1184 unsigned
1185 glsl_type::std140_base_alignment(bool row_major) const
1186 {
1187 unsigned N = is_double() ? 8 : 4;
1188
1189 /* (1) If the member is a scalar consuming <N> basic machine units, the
1190 * base alignment is <N>.
1191 *
1192 * (2) If the member is a two- or four-component vector with components
1193 * consuming <N> basic machine units, the base alignment is 2<N> or
1194 * 4<N>, respectively.
1195 *
1196 * (3) If the member is a three-component vector with components consuming
1197 * <N> basic machine units, the base alignment is 4<N>.
1198 */
1199 if (this->is_scalar() || this->is_vector()) {
1200 switch (this->vector_elements) {
1201 case 1:
1202 return N;
1203 case 2:
1204 return 2 * N;
1205 case 3:
1206 case 4:
1207 return 4 * N;
1208 }
1209 }
1210
1211 /* (4) If the member is an array of scalars or vectors, the base alignment
1212 * and array stride are set to match the base alignment of a single
1213 * array element, according to rules (1), (2), and (3), and rounded up
1214 * to the base alignment of a vec4. The array may have padding at the
1215 * end; the base offset of the member following the array is rounded up
1216 * to the next multiple of the base alignment.
1217 *
1218 * (6) If the member is an array of <S> column-major matrices with <C>
1219 * columns and <R> rows, the matrix is stored identically to a row of
1220 * <S>*<C> column vectors with <R> components each, according to rule
1221 * (4).
1222 *
1223 * (8) If the member is an array of <S> row-major matrices with <C> columns
1224 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1225 * row vectors with <C> components each, according to rule (4).
1226 *
1227 * (10) If the member is an array of <S> structures, the <S> elements of
1228 * the array are laid out in order, according to rule (9).
1229 */
1230 if (this->is_array()) {
1231 if (this->fields.array->is_scalar() ||
1232 this->fields.array->is_vector() ||
1233 this->fields.array->is_matrix()) {
1234 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1235 } else {
1236 assert(this->fields.array->is_record() ||
1237 this->fields.array->is_array());
1238 return this->fields.array->std140_base_alignment(row_major);
1239 }
1240 }
1241
1242 /* (5) If the member is a column-major matrix with <C> columns and
1243 * <R> rows, the matrix is stored identically to an array of
1244 * <C> column vectors with <R> components each, according to
1245 * rule (4).
1246 *
1247 * (7) If the member is a row-major matrix with <C> columns and <R>
1248 * rows, the matrix is stored identically to an array of <R>
1249 * row vectors with <C> components each, according to rule (4).
1250 */
1251 if (this->is_matrix()) {
1252 const struct glsl_type *vec_type, *array_type;
1253 int c = this->matrix_columns;
1254 int r = this->vector_elements;
1255
1256 if (row_major) {
1257 vec_type = get_instance(base_type, c, 1);
1258 array_type = glsl_type::get_array_instance(vec_type, r);
1259 } else {
1260 vec_type = get_instance(base_type, r, 1);
1261 array_type = glsl_type::get_array_instance(vec_type, c);
1262 }
1263
1264 return array_type->std140_base_alignment(false);
1265 }
1266
1267 /* (9) If the member is a structure, the base alignment of the
1268 * structure is <N>, where <N> is the largest base alignment
1269 * value of any of its members, and rounded up to the base
1270 * alignment of a vec4. The individual members of this
1271 * sub-structure are then assigned offsets by applying this set
1272 * of rules recursively, where the base offset of the first
1273 * member of the sub-structure is equal to the aligned offset
1274 * of the structure. The structure may have padding at the end;
1275 * the base offset of the member following the sub-structure is
1276 * rounded up to the next multiple of the base alignment of the
1277 * structure.
1278 */
1279 if (this->is_record()) {
1280 unsigned base_alignment = 16;
1281 for (unsigned i = 0; i < this->length; i++) {
1282 bool field_row_major = row_major;
1283 const enum glsl_matrix_layout matrix_layout =
1284 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1285 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1286 field_row_major = true;
1287 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1288 field_row_major = false;
1289 }
1290
1291 const struct glsl_type *field_type = this->fields.structure[i].type;
1292 base_alignment = MAX2(base_alignment,
1293 field_type->std140_base_alignment(field_row_major));
1294 }
1295 return base_alignment;
1296 }
1297
1298 assert(!"not reached");
1299 return -1;
1300 }
1301
1302 unsigned
1303 glsl_type::std140_size(bool row_major) const
1304 {
1305 unsigned N = is_double() ? 8 : 4;
1306
1307 /* (1) If the member is a scalar consuming <N> basic machine units, the
1308 * base alignment is <N>.
1309 *
1310 * (2) If the member is a two- or four-component vector with components
1311 * consuming <N> basic machine units, the base alignment is 2<N> or
1312 * 4<N>, respectively.
1313 *
1314 * (3) If the member is a three-component vector with components consuming
1315 * <N> basic machine units, the base alignment is 4<N>.
1316 */
1317 if (this->is_scalar() || this->is_vector()) {
1318 return this->vector_elements * N;
1319 }
1320
1321 /* (5) If the member is a column-major matrix with <C> columns and
1322 * <R> rows, the matrix is stored identically to an array of
1323 * <C> column vectors with <R> components each, according to
1324 * rule (4).
1325 *
1326 * (6) If the member is an array of <S> column-major matrices with <C>
1327 * columns and <R> rows, the matrix is stored identically to a row of
1328 * <S>*<C> column vectors with <R> components each, according to rule
1329 * (4).
1330 *
1331 * (7) If the member is a row-major matrix with <C> columns and <R>
1332 * rows, the matrix is stored identically to an array of <R>
1333 * row vectors with <C> components each, according to rule (4).
1334 *
1335 * (8) If the member is an array of <S> row-major matrices with <C> columns
1336 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1337 * row vectors with <C> components each, according to rule (4).
1338 */
1339 if (this->without_array()->is_matrix()) {
1340 const struct glsl_type *element_type;
1341 const struct glsl_type *vec_type;
1342 unsigned int array_len;
1343
1344 if (this->is_array()) {
1345 element_type = this->without_array();
1346 array_len = this->arrays_of_arrays_size();
1347 } else {
1348 element_type = this;
1349 array_len = 1;
1350 }
1351
1352 if (row_major) {
1353 vec_type = get_instance(element_type->base_type,
1354 element_type->matrix_columns, 1);
1355
1356 array_len *= element_type->vector_elements;
1357 } else {
1358 vec_type = get_instance(element_type->base_type,
1359 element_type->vector_elements, 1);
1360 array_len *= element_type->matrix_columns;
1361 }
1362 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1363 array_len);
1364
1365 return array_type->std140_size(false);
1366 }
1367
1368 /* (4) If the member is an array of scalars or vectors, the base alignment
1369 * and array stride are set to match the base alignment of a single
1370 * array element, according to rules (1), (2), and (3), and rounded up
1371 * to the base alignment of a vec4. The array may have padding at the
1372 * end; the base offset of the member following the array is rounded up
1373 * to the next multiple of the base alignment.
1374 *
1375 * (10) If the member is an array of <S> structures, the <S> elements of
1376 * the array are laid out in order, according to rule (9).
1377 */
1378 if (this->is_array()) {
1379 if (this->without_array()->is_record()) {
1380 return this->arrays_of_arrays_size() *
1381 this->without_array()->std140_size(row_major);
1382 } else {
1383 unsigned element_base_align =
1384 this->without_array()->std140_base_alignment(row_major);
1385 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1386 }
1387 }
1388
1389 /* (9) If the member is a structure, the base alignment of the
1390 * structure is <N>, where <N> is the largest base alignment
1391 * value of any of its members, and rounded up to the base
1392 * alignment of a vec4. The individual members of this
1393 * sub-structure are then assigned offsets by applying this set
1394 * of rules recursively, where the base offset of the first
1395 * member of the sub-structure is equal to the aligned offset
1396 * of the structure. The structure may have padding at the end;
1397 * the base offset of the member following the sub-structure is
1398 * rounded up to the next multiple of the base alignment of the
1399 * structure.
1400 */
1401 if (this->is_record() || this->is_interface()) {
1402 unsigned size = 0;
1403 unsigned max_align = 0;
1404
1405 for (unsigned i = 0; i < this->length; i++) {
1406 bool field_row_major = row_major;
1407 const enum glsl_matrix_layout matrix_layout =
1408 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1409 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1410 field_row_major = true;
1411 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1412 field_row_major = false;
1413 }
1414
1415 const struct glsl_type *field_type = this->fields.structure[i].type;
1416 unsigned align = field_type->std140_base_alignment(field_row_major);
1417
1418 /* Ignore unsized arrays when calculating size */
1419 if (field_type->is_unsized_array())
1420 continue;
1421
1422 size = glsl_align(size, align);
1423 size += field_type->std140_size(field_row_major);
1424
1425 max_align = MAX2(align, max_align);
1426
1427 if (field_type->is_record() && (i + 1 < this->length))
1428 size = glsl_align(size, 16);
1429 }
1430 size = glsl_align(size, MAX2(max_align, 16));
1431 return size;
1432 }
1433
1434 assert(!"not reached");
1435 return -1;
1436 }
1437
1438 unsigned
1439 glsl_type::std430_base_alignment(bool row_major) const
1440 {
1441
1442 unsigned N = is_double() ? 8 : 4;
1443
1444 /* (1) If the member is a scalar consuming <N> basic machine units, the
1445 * base alignment is <N>.
1446 *
1447 * (2) If the member is a two- or four-component vector with components
1448 * consuming <N> basic machine units, the base alignment is 2<N> or
1449 * 4<N>, respectively.
1450 *
1451 * (3) If the member is a three-component vector with components consuming
1452 * <N> basic machine units, the base alignment is 4<N>.
1453 */
1454 if (this->is_scalar() || this->is_vector()) {
1455 switch (this->vector_elements) {
1456 case 1:
1457 return N;
1458 case 2:
1459 return 2 * N;
1460 case 3:
1461 case 4:
1462 return 4 * N;
1463 }
1464 }
1465
1466 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1467 *
1468 * "When using the std430 storage layout, shader storage blocks will be
1469 * laid out in buffer storage identically to uniform and shader storage
1470 * blocks using the std140 layout, except that the base alignment and
1471 * stride of arrays of scalars and vectors in rule 4 and of structures
1472 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1473 */
1474
1475 /* (1) If the member is a scalar consuming <N> basic machine units, the
1476 * base alignment is <N>.
1477 *
1478 * (2) If the member is a two- or four-component vector with components
1479 * consuming <N> basic machine units, the base alignment is 2<N> or
1480 * 4<N>, respectively.
1481 *
1482 * (3) If the member is a three-component vector with components consuming
1483 * <N> basic machine units, the base alignment is 4<N>.
1484 */
1485 if (this->is_array())
1486 return this->fields.array->std430_base_alignment(row_major);
1487
1488 /* (5) If the member is a column-major matrix with <C> columns and
1489 * <R> rows, the matrix is stored identically to an array of
1490 * <C> column vectors with <R> components each, according to
1491 * rule (4).
1492 *
1493 * (7) If the member is a row-major matrix with <C> columns and <R>
1494 * rows, the matrix is stored identically to an array of <R>
1495 * row vectors with <C> components each, according to rule (4).
1496 */
1497 if (this->is_matrix()) {
1498 const struct glsl_type *vec_type, *array_type;
1499 int c = this->matrix_columns;
1500 int r = this->vector_elements;
1501
1502 if (row_major) {
1503 vec_type = get_instance(base_type, c, 1);
1504 array_type = glsl_type::get_array_instance(vec_type, r);
1505 } else {
1506 vec_type = get_instance(base_type, r, 1);
1507 array_type = glsl_type::get_array_instance(vec_type, c);
1508 }
1509
1510 return array_type->std430_base_alignment(false);
1511 }
1512
1513 /* (9) If the member is a structure, the base alignment of the
1514 * structure is <N>, where <N> is the largest base alignment
1515 * value of any of its members, and rounded up to the base
1516 * alignment of a vec4. The individual members of this
1517 * sub-structure are then assigned offsets by applying this set
1518 * of rules recursively, where the base offset of the first
1519 * member of the sub-structure is equal to the aligned offset
1520 * of the structure. The structure may have padding at the end;
1521 * the base offset of the member following the sub-structure is
1522 * rounded up to the next multiple of the base alignment of the
1523 * structure.
1524 */
1525 if (this->is_record()) {
1526 unsigned base_alignment = 0;
1527 for (unsigned i = 0; i < this->length; i++) {
1528 bool field_row_major = row_major;
1529 const enum glsl_matrix_layout matrix_layout =
1530 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1531 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1532 field_row_major = true;
1533 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1534 field_row_major = false;
1535 }
1536
1537 const struct glsl_type *field_type = this->fields.structure[i].type;
1538 base_alignment = MAX2(base_alignment,
1539 field_type->std430_base_alignment(field_row_major));
1540 }
1541 assert(base_alignment > 0);
1542 return base_alignment;
1543 }
1544 assert(!"not reached");
1545 return -1;
1546 }
1547
1548 unsigned
1549 glsl_type::std430_array_stride(bool row_major) const
1550 {
1551 unsigned N = is_double() ? 8 : 4;
1552
1553 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1554 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1555 *
1556 * (3) If the member is a three-component vector with components consuming
1557 * <N> basic machine units, the base alignment is 4<N>.
1558 */
1559 if (this->is_vector() && this->vector_elements == 3)
1560 return 4 * N;
1561
1562 /* By default use std430_size(row_major) */
1563 return this->std430_size(row_major);
1564 }
1565
1566 unsigned
1567 glsl_type::std430_size(bool row_major) const
1568 {
1569 unsigned N = is_double() ? 8 : 4;
1570
1571 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1572 *
1573 * "When using the std430 storage layout, shader storage blocks will be
1574 * laid out in buffer storage identically to uniform and shader storage
1575 * blocks using the std140 layout, except that the base alignment and
1576 * stride of arrays of scalars and vectors in rule 4 and of structures
1577 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1578 */
1579 if (this->is_scalar() || this->is_vector())
1580 return this->vector_elements * N;
1581
1582 if (this->without_array()->is_matrix()) {
1583 const struct glsl_type *element_type;
1584 const struct glsl_type *vec_type;
1585 unsigned int array_len;
1586
1587 if (this->is_array()) {
1588 element_type = this->without_array();
1589 array_len = this->arrays_of_arrays_size();
1590 } else {
1591 element_type = this;
1592 array_len = 1;
1593 }
1594
1595 if (row_major) {
1596 vec_type = get_instance(element_type->base_type,
1597 element_type->matrix_columns, 1);
1598
1599 array_len *= element_type->vector_elements;
1600 } else {
1601 vec_type = get_instance(element_type->base_type,
1602 element_type->vector_elements, 1);
1603 array_len *= element_type->matrix_columns;
1604 }
1605 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1606 array_len);
1607
1608 return array_type->std430_size(false);
1609 }
1610
1611 if (this->is_array()) {
1612 if (this->without_array()->is_record())
1613 return this->arrays_of_arrays_size() *
1614 this->without_array()->std430_size(row_major);
1615 else
1616 return this->arrays_of_arrays_size() *
1617 this->without_array()->std430_base_alignment(row_major);
1618 }
1619
1620 if (this->is_record() || this->is_interface()) {
1621 unsigned size = 0;
1622 unsigned max_align = 0;
1623
1624 for (unsigned i = 0; i < this->length; i++) {
1625 bool field_row_major = row_major;
1626 const enum glsl_matrix_layout matrix_layout =
1627 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1628 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1629 field_row_major = true;
1630 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1631 field_row_major = false;
1632 }
1633
1634 const struct glsl_type *field_type = this->fields.structure[i].type;
1635 unsigned align = field_type->std430_base_alignment(field_row_major);
1636 size = glsl_align(size, align);
1637 size += field_type->std430_size(field_row_major);
1638
1639 max_align = MAX2(align, max_align);
1640 }
1641 size = glsl_align(size, max_align);
1642 return size;
1643 }
1644
1645 assert(!"not reached");
1646 return -1;
1647 }
1648
1649 unsigned
1650 glsl_type::count_attribute_slots(bool vertex_input_slots) const
1651 {
1652 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1653 *
1654 * "A scalar input counts the same amount against this limit as a vec4,
1655 * so applications may want to consider packing groups of four
1656 * unrelated float inputs together into a vector to better utilize the
1657 * capabilities of the underlying hardware. A matrix input will use up
1658 * multiple locations. The number of locations used will equal the
1659 * number of columns in the matrix."
1660 *
1661 * The spec does not explicitly say how arrays are counted. However, it
1662 * should be safe to assume the total number of slots consumed by an array
1663 * is the number of entries in the array multiplied by the number of slots
1664 * consumed by a single element of the array.
1665 *
1666 * The spec says nothing about how structs are counted, because vertex
1667 * attributes are not allowed to be (or contain) structs. However, Mesa
1668 * allows varying structs, the number of varying slots taken up by a
1669 * varying struct is simply equal to the sum of the number of slots taken
1670 * up by each element.
1671 *
1672 * Doubles are counted different depending on whether they are vertex
1673 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1674 * take one location no matter what size they are, otherwise dvec3/4
1675 * take two locations.
1676 */
1677 switch (this->base_type) {
1678 case GLSL_TYPE_UINT:
1679 case GLSL_TYPE_INT:
1680 case GLSL_TYPE_FLOAT:
1681 case GLSL_TYPE_BOOL:
1682 return this->matrix_columns;
1683 case GLSL_TYPE_DOUBLE:
1684 if (this->vector_elements > 2 && !vertex_input_slots)
1685 return this->matrix_columns * 2;
1686 else
1687 return this->matrix_columns;
1688 case GLSL_TYPE_STRUCT:
1689 case GLSL_TYPE_INTERFACE: {
1690 unsigned size = 0;
1691
1692 for (unsigned i = 0; i < this->length; i++)
1693 size += this->fields.structure[i].type->count_attribute_slots(vertex_input_slots);
1694
1695 return size;
1696 }
1697
1698 case GLSL_TYPE_ARRAY:
1699 return this->length * this->fields.array->count_attribute_slots(vertex_input_slots);
1700
1701 case GLSL_TYPE_SAMPLER:
1702 case GLSL_TYPE_IMAGE:
1703 case GLSL_TYPE_ATOMIC_UINT:
1704 case GLSL_TYPE_VOID:
1705 case GLSL_TYPE_SUBROUTINE:
1706 case GLSL_TYPE_ERROR:
1707 break;
1708 }
1709
1710 assert(!"Unexpected type in count_attribute_slots()");
1711
1712 return 0;
1713 }
1714
1715 int
1716 glsl_type::coordinate_components() const
1717 {
1718 int size;
1719
1720 switch (sampler_dimensionality) {
1721 case GLSL_SAMPLER_DIM_1D:
1722 case GLSL_SAMPLER_DIM_BUF:
1723 size = 1;
1724 break;
1725 case GLSL_SAMPLER_DIM_2D:
1726 case GLSL_SAMPLER_DIM_RECT:
1727 case GLSL_SAMPLER_DIM_MS:
1728 case GLSL_SAMPLER_DIM_EXTERNAL:
1729 size = 2;
1730 break;
1731 case GLSL_SAMPLER_DIM_3D:
1732 case GLSL_SAMPLER_DIM_CUBE:
1733 size = 3;
1734 break;
1735 default:
1736 assert(!"Should not get here.");
1737 size = 1;
1738 break;
1739 }
1740
1741 /* Array textures need an additional component for the array index, except
1742 * for cubemap array images that behave like a 2D array of interleaved
1743 * cubemap faces.
1744 */
1745 if (sampler_array &&
1746 !(base_type == GLSL_TYPE_IMAGE &&
1747 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1748 size += 1;
1749
1750 return size;
1751 }
1752
1753 /**
1754 * Declarations of type flyweights (glsl_type::_foo_type) and
1755 * convenience pointers (glsl_type::foo_type).
1756 * @{
1757 */
1758 #define DECL_TYPE(NAME, ...) \
1759 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1760 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1761
1762 #define STRUCT_TYPE(NAME)
1763
1764 #include "compiler/builtin_type_macros.h"
1765 /** @} */