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