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