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