glsl: move to compiler/
[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 /* There is no conversion among matrix types. */
1143 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1144 return false;
1145
1146 /* Vector size must match. */
1147 if (this->vector_elements != desired->vector_elements)
1148 return false;
1149
1150 /* int and uint can be converted to float. */
1151 if (desired->is_float() && this->is_integer())
1152 return true;
1153
1154 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1155 * Note that state may be NULL here, when resolving function calls in the
1156 * linker. By this time, all the state-dependent checks have already
1157 * happened though, so allow anything that's allowed in any shader version. */
1158 if ((!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) &&
1159 desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1160 return true;
1161
1162 /* No implicit conversions from double. */
1163 if ((!state || state->has_double()) && this->is_double())
1164 return false;
1165
1166 /* Conversions from different types to double. */
1167 if ((!state || state->has_double()) && desired->is_double()) {
1168 if (this->is_float())
1169 return true;
1170 if (this->is_integer())
1171 return true;
1172 }
1173
1174 return false;
1175 }
1176
1177 unsigned
1178 glsl_type::std140_base_alignment(bool row_major) const
1179 {
1180 unsigned N = is_double() ? 8 : 4;
1181
1182 /* (1) If the member is a scalar consuming <N> basic machine units, the
1183 * base alignment is <N>.
1184 *
1185 * (2) If the member is a two- or four-component vector with components
1186 * consuming <N> basic machine units, the base alignment is 2<N> or
1187 * 4<N>, respectively.
1188 *
1189 * (3) If the member is a three-component vector with components consuming
1190 * <N> basic machine units, the base alignment is 4<N>.
1191 */
1192 if (this->is_scalar() || this->is_vector()) {
1193 switch (this->vector_elements) {
1194 case 1:
1195 return N;
1196 case 2:
1197 return 2 * N;
1198 case 3:
1199 case 4:
1200 return 4 * N;
1201 }
1202 }
1203
1204 /* (4) If the member is an array of scalars or vectors, the base alignment
1205 * and array stride are set to match the base alignment of a single
1206 * array element, according to rules (1), (2), and (3), and rounded up
1207 * to the base alignment of a vec4. The array may have padding at the
1208 * end; the base offset of the member following the array is rounded up
1209 * to the next multiple of the base alignment.
1210 *
1211 * (6) If the member is an array of <S> column-major matrices with <C>
1212 * columns and <R> rows, the matrix is stored identically to a row of
1213 * <S>*<C> column vectors with <R> components each, according to rule
1214 * (4).
1215 *
1216 * (8) If the member is an array of <S> row-major matrices with <C> columns
1217 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1218 * row vectors with <C> components each, according to rule (4).
1219 *
1220 * (10) If the member is an array of <S> structures, the <S> elements of
1221 * the array are laid out in order, according to rule (9).
1222 */
1223 if (this->is_array()) {
1224 if (this->fields.array->is_scalar() ||
1225 this->fields.array->is_vector() ||
1226 this->fields.array->is_matrix()) {
1227 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1228 } else {
1229 assert(this->fields.array->is_record() ||
1230 this->fields.array->is_array());
1231 return this->fields.array->std140_base_alignment(row_major);
1232 }
1233 }
1234
1235 /* (5) If the member is a column-major matrix with <C> columns and
1236 * <R> rows, the matrix is stored identically to an array of
1237 * <C> column vectors with <R> components each, according to
1238 * rule (4).
1239 *
1240 * (7) If the member is a row-major matrix with <C> columns and <R>
1241 * rows, the matrix is stored identically to an array of <R>
1242 * row vectors with <C> components each, according to rule (4).
1243 */
1244 if (this->is_matrix()) {
1245 const struct glsl_type *vec_type, *array_type;
1246 int c = this->matrix_columns;
1247 int r = this->vector_elements;
1248
1249 if (row_major) {
1250 vec_type = get_instance(base_type, c, 1);
1251 array_type = glsl_type::get_array_instance(vec_type, r);
1252 } else {
1253 vec_type = get_instance(base_type, r, 1);
1254 array_type = glsl_type::get_array_instance(vec_type, c);
1255 }
1256
1257 return array_type->std140_base_alignment(false);
1258 }
1259
1260 /* (9) If the member is a structure, the base alignment of the
1261 * structure is <N>, where <N> is the largest base alignment
1262 * value of any of its members, and rounded up to the base
1263 * alignment of a vec4. The individual members of this
1264 * sub-structure are then assigned offsets by applying this set
1265 * of rules recursively, where the base offset of the first
1266 * member of the sub-structure is equal to the aligned offset
1267 * of the structure. The structure may have padding at the end;
1268 * the base offset of the member following the sub-structure is
1269 * rounded up to the next multiple of the base alignment of the
1270 * structure.
1271 */
1272 if (this->is_record()) {
1273 unsigned base_alignment = 16;
1274 for (unsigned i = 0; i < this->length; i++) {
1275 bool field_row_major = row_major;
1276 const enum glsl_matrix_layout matrix_layout =
1277 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1278 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1279 field_row_major = true;
1280 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1281 field_row_major = false;
1282 }
1283
1284 const struct glsl_type *field_type = this->fields.structure[i].type;
1285 base_alignment = MAX2(base_alignment,
1286 field_type->std140_base_alignment(field_row_major));
1287 }
1288 return base_alignment;
1289 }
1290
1291 assert(!"not reached");
1292 return -1;
1293 }
1294
1295 unsigned
1296 glsl_type::std140_size(bool row_major) const
1297 {
1298 unsigned N = is_double() ? 8 : 4;
1299
1300 /* (1) If the member is a scalar consuming <N> basic machine units, the
1301 * base alignment is <N>.
1302 *
1303 * (2) If the member is a two- or four-component vector with components
1304 * consuming <N> basic machine units, the base alignment is 2<N> or
1305 * 4<N>, respectively.
1306 *
1307 * (3) If the member is a three-component vector with components consuming
1308 * <N> basic machine units, the base alignment is 4<N>.
1309 */
1310 if (this->is_scalar() || this->is_vector()) {
1311 return this->vector_elements * N;
1312 }
1313
1314 /* (5) If the member is a column-major matrix with <C> columns and
1315 * <R> rows, the matrix is stored identically to an array of
1316 * <C> column vectors with <R> components each, according to
1317 * rule (4).
1318 *
1319 * (6) If the member is an array of <S> column-major matrices with <C>
1320 * columns and <R> rows, the matrix is stored identically to a row of
1321 * <S>*<C> column vectors with <R> components each, according to rule
1322 * (4).
1323 *
1324 * (7) If the member is a row-major matrix with <C> columns and <R>
1325 * rows, the matrix is stored identically to an array of <R>
1326 * row vectors with <C> components each, according to rule (4).
1327 *
1328 * (8) If the member is an array of <S> row-major matrices with <C> columns
1329 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1330 * row vectors with <C> components each, according to rule (4).
1331 */
1332 if (this->without_array()->is_matrix()) {
1333 const struct glsl_type *element_type;
1334 const struct glsl_type *vec_type;
1335 unsigned int array_len;
1336
1337 if (this->is_array()) {
1338 element_type = this->without_array();
1339 array_len = this->arrays_of_arrays_size();
1340 } else {
1341 element_type = this;
1342 array_len = 1;
1343 }
1344
1345 if (row_major) {
1346 vec_type = get_instance(element_type->base_type,
1347 element_type->matrix_columns, 1);
1348
1349 array_len *= element_type->vector_elements;
1350 } else {
1351 vec_type = get_instance(element_type->base_type,
1352 element_type->vector_elements, 1);
1353 array_len *= element_type->matrix_columns;
1354 }
1355 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1356 array_len);
1357
1358 return array_type->std140_size(false);
1359 }
1360
1361 /* (4) If the member is an array of scalars or vectors, the base alignment
1362 * and array stride are set to match the base alignment of a single
1363 * array element, according to rules (1), (2), and (3), and rounded up
1364 * to the base alignment of a vec4. The array may have padding at the
1365 * end; the base offset of the member following the array is rounded up
1366 * to the next multiple of the base alignment.
1367 *
1368 * (10) If the member is an array of <S> structures, the <S> elements of
1369 * the array are laid out in order, according to rule (9).
1370 */
1371 if (this->is_array()) {
1372 if (this->without_array()->is_record()) {
1373 return this->arrays_of_arrays_size() *
1374 this->without_array()->std140_size(row_major);
1375 } else {
1376 unsigned element_base_align =
1377 this->without_array()->std140_base_alignment(row_major);
1378 return this->arrays_of_arrays_size() * MAX2(element_base_align, 16);
1379 }
1380 }
1381
1382 /* (9) If the member is a structure, the base alignment of the
1383 * structure is <N>, where <N> is the largest base alignment
1384 * value of any of its members, and rounded up to the base
1385 * alignment of a vec4. The individual members of this
1386 * sub-structure are then assigned offsets by applying this set
1387 * of rules recursively, where the base offset of the first
1388 * member of the sub-structure is equal to the aligned offset
1389 * of the structure. The structure may have padding at the end;
1390 * the base offset of the member following the sub-structure is
1391 * rounded up to the next multiple of the base alignment of the
1392 * structure.
1393 */
1394 if (this->is_record() || this->is_interface()) {
1395 unsigned size = 0;
1396 unsigned max_align = 0;
1397
1398 for (unsigned i = 0; i < this->length; i++) {
1399 bool field_row_major = row_major;
1400 const enum glsl_matrix_layout matrix_layout =
1401 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1402 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1403 field_row_major = true;
1404 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1405 field_row_major = false;
1406 }
1407
1408 const struct glsl_type *field_type = this->fields.structure[i].type;
1409 unsigned align = field_type->std140_base_alignment(field_row_major);
1410
1411 /* Ignore unsized arrays when calculating size */
1412 if (field_type->is_unsized_array())
1413 continue;
1414
1415 size = glsl_align(size, align);
1416 size += field_type->std140_size(field_row_major);
1417
1418 max_align = MAX2(align, max_align);
1419
1420 if (field_type->is_record() && (i + 1 < this->length))
1421 size = glsl_align(size, 16);
1422 }
1423 size = glsl_align(size, MAX2(max_align, 16));
1424 return size;
1425 }
1426
1427 assert(!"not reached");
1428 return -1;
1429 }
1430
1431 unsigned
1432 glsl_type::std430_base_alignment(bool row_major) const
1433 {
1434
1435 unsigned N = is_double() ? 8 : 4;
1436
1437 /* (1) If the member is a scalar consuming <N> basic machine units, the
1438 * base alignment is <N>.
1439 *
1440 * (2) If the member is a two- or four-component vector with components
1441 * consuming <N> basic machine units, the base alignment is 2<N> or
1442 * 4<N>, respectively.
1443 *
1444 * (3) If the member is a three-component vector with components consuming
1445 * <N> basic machine units, the base alignment is 4<N>.
1446 */
1447 if (this->is_scalar() || this->is_vector()) {
1448 switch (this->vector_elements) {
1449 case 1:
1450 return N;
1451 case 2:
1452 return 2 * N;
1453 case 3:
1454 case 4:
1455 return 4 * N;
1456 }
1457 }
1458
1459 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1460 *
1461 * "When using the std430 storage layout, shader storage blocks will be
1462 * laid out in buffer storage identically to uniform and shader storage
1463 * blocks using the std140 layout, except that the base alignment and
1464 * stride of arrays of scalars and vectors in rule 4 and of structures
1465 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1466 */
1467
1468 /* (1) If the member is a scalar consuming <N> basic machine units, the
1469 * base alignment is <N>.
1470 *
1471 * (2) If the member is a two- or four-component vector with components
1472 * consuming <N> basic machine units, the base alignment is 2<N> or
1473 * 4<N>, respectively.
1474 *
1475 * (3) If the member is a three-component vector with components consuming
1476 * <N> basic machine units, the base alignment is 4<N>.
1477 */
1478 if (this->is_array())
1479 return this->fields.array->std430_base_alignment(row_major);
1480
1481 /* (5) If the member is a column-major matrix with <C> columns and
1482 * <R> rows, the matrix is stored identically to an array of
1483 * <C> column vectors with <R> components each, according to
1484 * rule (4).
1485 *
1486 * (7) If the member is a row-major matrix with <C> columns and <R>
1487 * rows, the matrix is stored identically to an array of <R>
1488 * row vectors with <C> components each, according to rule (4).
1489 */
1490 if (this->is_matrix()) {
1491 const struct glsl_type *vec_type, *array_type;
1492 int c = this->matrix_columns;
1493 int r = this->vector_elements;
1494
1495 if (row_major) {
1496 vec_type = get_instance(base_type, c, 1);
1497 array_type = glsl_type::get_array_instance(vec_type, r);
1498 } else {
1499 vec_type = get_instance(base_type, r, 1);
1500 array_type = glsl_type::get_array_instance(vec_type, c);
1501 }
1502
1503 return array_type->std430_base_alignment(false);
1504 }
1505
1506 /* (9) If the member is a structure, the base alignment of the
1507 * structure is <N>, where <N> is the largest base alignment
1508 * value of any of its members, and rounded up to the base
1509 * alignment of a vec4. The individual members of this
1510 * sub-structure are then assigned offsets by applying this set
1511 * of rules recursively, where the base offset of the first
1512 * member of the sub-structure is equal to the aligned offset
1513 * of the structure. The structure may have padding at the end;
1514 * the base offset of the member following the sub-structure is
1515 * rounded up to the next multiple of the base alignment of the
1516 * structure.
1517 */
1518 if (this->is_record()) {
1519 unsigned base_alignment = 0;
1520 for (unsigned i = 0; i < this->length; i++) {
1521 bool field_row_major = row_major;
1522 const enum glsl_matrix_layout matrix_layout =
1523 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1524 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1525 field_row_major = true;
1526 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1527 field_row_major = false;
1528 }
1529
1530 const struct glsl_type *field_type = this->fields.structure[i].type;
1531 base_alignment = MAX2(base_alignment,
1532 field_type->std430_base_alignment(field_row_major));
1533 }
1534 assert(base_alignment > 0);
1535 return base_alignment;
1536 }
1537 assert(!"not reached");
1538 return -1;
1539 }
1540
1541 unsigned
1542 glsl_type::std430_array_stride(bool row_major) const
1543 {
1544 unsigned N = is_double() ? 8 : 4;
1545
1546 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1547 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1548 *
1549 * (3) If the member is a three-component vector with components consuming
1550 * <N> basic machine units, the base alignment is 4<N>.
1551 */
1552 if (this->is_vector() && this->vector_elements == 3)
1553 return 4 * N;
1554
1555 /* By default use std430_size(row_major) */
1556 return this->std430_size(row_major);
1557 }
1558
1559 unsigned
1560 glsl_type::std430_size(bool row_major) const
1561 {
1562 unsigned N = is_double() ? 8 : 4;
1563
1564 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1565 *
1566 * "When using the std430 storage layout, shader storage blocks will be
1567 * laid out in buffer storage identically to uniform and shader storage
1568 * blocks using the std140 layout, except that the base alignment and
1569 * stride of arrays of scalars and vectors in rule 4 and of structures
1570 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1571 */
1572 if (this->is_scalar() || this->is_vector())
1573 return this->vector_elements * N;
1574
1575 if (this->without_array()->is_matrix()) {
1576 const struct glsl_type *element_type;
1577 const struct glsl_type *vec_type;
1578 unsigned int array_len;
1579
1580 if (this->is_array()) {
1581 element_type = this->without_array();
1582 array_len = this->arrays_of_arrays_size();
1583 } else {
1584 element_type = this;
1585 array_len = 1;
1586 }
1587
1588 if (row_major) {
1589 vec_type = get_instance(element_type->base_type,
1590 element_type->matrix_columns, 1);
1591
1592 array_len *= element_type->vector_elements;
1593 } else {
1594 vec_type = get_instance(element_type->base_type,
1595 element_type->vector_elements, 1);
1596 array_len *= element_type->matrix_columns;
1597 }
1598 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
1599 array_len);
1600
1601 return array_type->std430_size(false);
1602 }
1603
1604 if (this->is_array()) {
1605 if (this->without_array()->is_record())
1606 return this->arrays_of_arrays_size() *
1607 this->without_array()->std430_size(row_major);
1608 else
1609 return this->arrays_of_arrays_size() *
1610 this->without_array()->std430_base_alignment(row_major);
1611 }
1612
1613 if (this->is_record() || this->is_interface()) {
1614 unsigned size = 0;
1615 unsigned max_align = 0;
1616
1617 for (unsigned i = 0; i < this->length; i++) {
1618 bool field_row_major = row_major;
1619 const enum glsl_matrix_layout matrix_layout =
1620 glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1621 if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1622 field_row_major = true;
1623 } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1624 field_row_major = false;
1625 }
1626
1627 const struct glsl_type *field_type = this->fields.structure[i].type;
1628 unsigned align = field_type->std430_base_alignment(field_row_major);
1629 size = glsl_align(size, align);
1630 size += field_type->std430_size(field_row_major);
1631
1632 max_align = MAX2(align, max_align);
1633 }
1634 size = glsl_align(size, max_align);
1635 return size;
1636 }
1637
1638 assert(!"not reached");
1639 return -1;
1640 }
1641
1642 unsigned
1643 glsl_type::count_attribute_slots(bool vertex_input_slots) const
1644 {
1645 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1646 *
1647 * "A scalar input counts the same amount against this limit as a vec4,
1648 * so applications may want to consider packing groups of four
1649 * unrelated float inputs together into a vector to better utilize the
1650 * capabilities of the underlying hardware. A matrix input will use up
1651 * multiple locations. The number of locations used will equal the
1652 * number of columns in the matrix."
1653 *
1654 * The spec does not explicitly say how arrays are counted. However, it
1655 * should be safe to assume the total number of slots consumed by an array
1656 * is the number of entries in the array multiplied by the number of slots
1657 * consumed by a single element of the array.
1658 *
1659 * The spec says nothing about how structs are counted, because vertex
1660 * attributes are not allowed to be (or contain) structs. However, Mesa
1661 * allows varying structs, the number of varying slots taken up by a
1662 * varying struct is simply equal to the sum of the number of slots taken
1663 * up by each element.
1664 *
1665 * Doubles are counted different depending on whether they are vertex
1666 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1667 * take one location no matter what size they are, otherwise dvec3/4
1668 * take two locations.
1669 */
1670 switch (this->base_type) {
1671 case GLSL_TYPE_UINT:
1672 case GLSL_TYPE_INT:
1673 case GLSL_TYPE_FLOAT:
1674 case GLSL_TYPE_BOOL:
1675 return this->matrix_columns;
1676 case GLSL_TYPE_DOUBLE:
1677 if (this->vector_elements > 2 && !vertex_input_slots)
1678 return this->matrix_columns * 2;
1679 else
1680 return this->matrix_columns;
1681 case GLSL_TYPE_STRUCT:
1682 case GLSL_TYPE_INTERFACE: {
1683 unsigned size = 0;
1684
1685 for (unsigned i = 0; i < this->length; i++)
1686 size += this->fields.structure[i].type->count_attribute_slots(vertex_input_slots);
1687
1688 return size;
1689 }
1690
1691 case GLSL_TYPE_ARRAY:
1692 return this->length * this->fields.array->count_attribute_slots(vertex_input_slots);
1693
1694 case GLSL_TYPE_SAMPLER:
1695 case GLSL_TYPE_IMAGE:
1696 case GLSL_TYPE_ATOMIC_UINT:
1697 case GLSL_TYPE_VOID:
1698 case GLSL_TYPE_SUBROUTINE:
1699 case GLSL_TYPE_ERROR:
1700 break;
1701 }
1702
1703 assert(!"Unexpected type in count_attribute_slots()");
1704
1705 return 0;
1706 }
1707
1708 int
1709 glsl_type::coordinate_components() const
1710 {
1711 int size;
1712
1713 switch (sampler_dimensionality) {
1714 case GLSL_SAMPLER_DIM_1D:
1715 case GLSL_SAMPLER_DIM_BUF:
1716 size = 1;
1717 break;
1718 case GLSL_SAMPLER_DIM_2D:
1719 case GLSL_SAMPLER_DIM_RECT:
1720 case GLSL_SAMPLER_DIM_MS:
1721 case GLSL_SAMPLER_DIM_EXTERNAL:
1722 size = 2;
1723 break;
1724 case GLSL_SAMPLER_DIM_3D:
1725 case GLSL_SAMPLER_DIM_CUBE:
1726 size = 3;
1727 break;
1728 default:
1729 assert(!"Should not get here.");
1730 size = 1;
1731 break;
1732 }
1733
1734 /* Array textures need an additional component for the array index, except
1735 * for cubemap array images that behave like a 2D array of interleaved
1736 * cubemap faces.
1737 */
1738 if (sampler_array &&
1739 !(base_type == GLSL_TYPE_IMAGE &&
1740 sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
1741 size += 1;
1742
1743 return size;
1744 }
1745
1746 /**
1747 * Declarations of type flyweights (glsl_type::_foo_type) and
1748 * convenience pointers (glsl_type::foo_type).
1749 * @{
1750 */
1751 #define DECL_TYPE(NAME, ...) \
1752 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1753 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1754
1755 #define STRUCT_TYPE(NAME)
1756
1757 #include "compiler/builtin_type_macros.h"
1758 /** @} */