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