OSDN Git Service

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