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