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