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