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