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