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