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