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