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 #include "util/u_cpu_detect.h"
30 #include "util/u_string.h"
31 
32 
33 mtx_t glsl_type::hash_mutex = _MTX_INITIALIZER_NP;
34 hash_table *glsl_type::explicit_matrix_types = NULL;
35 hash_table *glsl_type::array_types = NULL;
36 hash_table *glsl_type::struct_types = NULL;
37 hash_table *glsl_type::interface_types = NULL;
38 hash_table *glsl_type::function_types = NULL;
39 hash_table *glsl_type::subroutine_types = NULL;
40 
41 /* There might be multiple users for types (e.g. application using OpenGL
42  * and Vulkan simultaneously or app using multiple Vulkan instances). Counter
43  * is used to make sure we don't release the types if a user is still present.
44  */
45 static uint32_t glsl_type_users = 0;
46 
glsl_type(GLenum gl_type,glsl_base_type base_type,unsigned vector_elements,unsigned matrix_columns,const char * name,unsigned explicit_stride,bool row_major,unsigned explicit_alignment)47 glsl_type::glsl_type(GLenum gl_type,
48                      glsl_base_type base_type, unsigned vector_elements,
49                      unsigned matrix_columns, const char *name,
50                      unsigned explicit_stride, bool row_major,
51                      unsigned explicit_alignment) :
52    gl_type(gl_type),
53    base_type(base_type), sampled_type(GLSL_TYPE_VOID),
54    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
55    interface_packing(0), interface_row_major(row_major), packed(0),
56    vector_elements(vector_elements), matrix_columns(matrix_columns),
57    length(0), explicit_stride(explicit_stride),
58    explicit_alignment(explicit_alignment)
59 {
60    /* Values of these types must fit in the two bits of
61     * glsl_type::sampled_type.
62     */
63    STATIC_ASSERT((unsigned(GLSL_TYPE_UINT)  & 3) == unsigned(GLSL_TYPE_UINT));
64    STATIC_ASSERT((unsigned(GLSL_TYPE_INT)   & 3) == unsigned(GLSL_TYPE_INT));
65    STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT) & 3) == unsigned(GLSL_TYPE_FLOAT));
66 
67    ASSERT_BITFIELD_SIZE(glsl_type, base_type, GLSL_TYPE_ERROR);
68    ASSERT_BITFIELD_SIZE(glsl_type, sampled_type, GLSL_TYPE_ERROR);
69    ASSERT_BITFIELD_SIZE(glsl_type, sampler_dimensionality,
70                         GLSL_SAMPLER_DIM_SUBPASS_MS);
71 
72    this->mem_ctx = ralloc_context(NULL);
73    assert(this->mem_ctx != NULL);
74 
75    assert(name != NULL);
76    this->name = ralloc_strdup(this->mem_ctx, name);
77 
78    /* Neither dimension is zero or both dimensions are zero.
79     */
80    assert((vector_elements == 0) == (matrix_columns == 0));
81    assert(util_is_power_of_two_or_zero(explicit_alignment));
82    memset(& fields, 0, sizeof(fields));
83 }
84 
glsl_type(GLenum gl_type,glsl_base_type base_type,enum glsl_sampler_dim dim,bool shadow,bool array,glsl_base_type type,const char * name)85 glsl_type::glsl_type(GLenum gl_type, glsl_base_type base_type,
86                      enum glsl_sampler_dim dim, bool shadow, bool array,
87                      glsl_base_type type, const char *name) :
88    gl_type(gl_type),
89    base_type(base_type), sampled_type(type),
90    sampler_dimensionality(dim), sampler_shadow(shadow),
91    sampler_array(array), interface_packing(0),
92    interface_row_major(0), packed(0),
93    length(0), explicit_stride(0), explicit_alignment(0)
94 {
95    this->mem_ctx = ralloc_context(NULL);
96    assert(this->mem_ctx != NULL);
97 
98    assert(name != NULL);
99    this->name = ralloc_strdup(this->mem_ctx, name);
100 
101    memset(& fields, 0, sizeof(fields));
102 
103    matrix_columns = vector_elements = 1;
104 }
105 
glsl_type(const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed,unsigned explicit_alignment)106 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
107                      const char *name, bool packed,
108                      unsigned explicit_alignment) :
109    gl_type(0),
110    base_type(GLSL_TYPE_STRUCT), sampled_type(GLSL_TYPE_VOID),
111    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
112    interface_packing(0), interface_row_major(0), packed(packed),
113    vector_elements(0), matrix_columns(0),
114    length(num_fields), explicit_stride(0),
115    explicit_alignment(explicit_alignment)
116 {
117    unsigned int i;
118 
119    assert(util_is_power_of_two_or_zero(explicit_alignment));
120 
121    this->mem_ctx = ralloc_context(NULL);
122    assert(this->mem_ctx != NULL);
123 
124    assert(name != NULL);
125    this->name = ralloc_strdup(this->mem_ctx, name);
126    /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
127     * due to uninitialized unused bits in bit fields. */
128    this->fields.structure = rzalloc_array(this->mem_ctx,
129                                           glsl_struct_field, length);
130 
131    for (i = 0; i < length; i++) {
132       this->fields.structure[i] = fields[i];
133       this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
134                                                      fields[i].name);
135    }
136 }
137 
glsl_type(const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * name)138 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
139                      enum glsl_interface_packing packing,
140                      bool row_major, const char *name) :
141    gl_type(0),
142    base_type(GLSL_TYPE_INTERFACE), sampled_type(GLSL_TYPE_VOID),
143    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
144    interface_packing((unsigned) packing),
145    interface_row_major((unsigned) row_major), packed(0),
146    vector_elements(0), matrix_columns(0),
147    length(num_fields), explicit_stride(0), explicit_alignment(0)
148 {
149    unsigned int i;
150 
151    this->mem_ctx = ralloc_context(NULL);
152    assert(this->mem_ctx != NULL);
153 
154    assert(name != NULL);
155    this->name = ralloc_strdup(this->mem_ctx, name);
156    this->fields.structure = rzalloc_array(this->mem_ctx,
157                                           glsl_struct_field, length);
158    for (i = 0; i < length; i++) {
159       this->fields.structure[i] = fields[i];
160       this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
161                                                      fields[i].name);
162    }
163 }
164 
glsl_type(const glsl_type * return_type,const glsl_function_param * params,unsigned num_params)165 glsl_type::glsl_type(const glsl_type *return_type,
166                      const glsl_function_param *params, unsigned num_params) :
167    gl_type(0),
168    base_type(GLSL_TYPE_FUNCTION), sampled_type(GLSL_TYPE_VOID),
169    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
170    interface_packing(0), interface_row_major(0), packed(0),
171    vector_elements(0), matrix_columns(0),
172    length(num_params), explicit_stride(0), explicit_alignment(0)
173 {
174    unsigned int i;
175 
176    this->mem_ctx = ralloc_context(NULL);
177    assert(this->mem_ctx != NULL);
178 
179    this->name = ralloc_strdup(this->mem_ctx, "");
180 
181    this->fields.parameters = rzalloc_array(this->mem_ctx,
182                                            glsl_function_param, num_params + 1);
183 
184    /* We store the return type as the first parameter */
185    this->fields.parameters[0].type = return_type;
186    this->fields.parameters[0].in = false;
187    this->fields.parameters[0].out = true;
188 
189    /* We store the i'th parameter in slot i+1 */
190    for (i = 0; i < length; i++) {
191       this->fields.parameters[i + 1].type = params[i].type;
192       this->fields.parameters[i + 1].in = params[i].in;
193       this->fields.parameters[i + 1].out = params[i].out;
194    }
195 }
196 
glsl_type(const char * subroutine_name)197 glsl_type::glsl_type(const char *subroutine_name) :
198    gl_type(0),
199    base_type(GLSL_TYPE_SUBROUTINE), sampled_type(GLSL_TYPE_VOID),
200    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
201    interface_packing(0), interface_row_major(0), packed(0),
202    vector_elements(1), matrix_columns(1),
203    length(0), explicit_stride(0), explicit_alignment(0)
204 {
205    this->mem_ctx = ralloc_context(NULL);
206    assert(this->mem_ctx != NULL);
207 
208    assert(subroutine_name != NULL);
209    this->name = ralloc_strdup(this->mem_ctx, subroutine_name);
210 }
211 
~glsl_type()212 glsl_type::~glsl_type()
213 {
214     ralloc_free(this->mem_ctx);
215 }
216 
217 bool
contains_sampler() const218 glsl_type::contains_sampler() const
219 {
220    if (this->is_array()) {
221       return this->fields.array->contains_sampler();
222    } else if (this->is_struct() || this->is_interface()) {
223       for (unsigned int i = 0; i < this->length; i++) {
224          if (this->fields.structure[i].type->contains_sampler())
225             return true;
226       }
227       return false;
228    } else {
229       return this->is_sampler();
230    }
231 }
232 
233 bool
contains_array() const234 glsl_type::contains_array() const
235 {
236    if (this->is_struct() || this->is_interface()) {
237       for (unsigned int i = 0; i < this->length; i++) {
238          if (this->fields.structure[i].type->contains_array())
239             return true;
240       }
241       return false;
242    } else {
243       return this->is_array();
244    }
245 }
246 
247 bool
contains_integer() const248 glsl_type::contains_integer() const
249 {
250    if (this->is_array()) {
251       return this->fields.array->contains_integer();
252    } else if (this->is_struct() || this->is_interface()) {
253       for (unsigned int i = 0; i < this->length; i++) {
254          if (this->fields.structure[i].type->contains_integer())
255             return true;
256       }
257       return false;
258    } else {
259       return this->is_integer();
260    }
261 }
262 
263 bool
contains_double() const264 glsl_type::contains_double() const
265 {
266    if (this->is_array()) {
267       return this->fields.array->contains_double();
268    } else if (this->is_struct() || this->is_interface()) {
269       for (unsigned int i = 0; i < this->length; i++) {
270          if (this->fields.structure[i].type->contains_double())
271             return true;
272       }
273       return false;
274    } else {
275       return this->is_double();
276    }
277 }
278 
279 bool
contains_64bit() const280 glsl_type::contains_64bit() const
281 {
282    if (this->is_array()) {
283       return this->fields.array->contains_64bit();
284    } else if (this->is_struct() || this->is_interface()) {
285       for (unsigned int i = 0; i < this->length; i++) {
286          if (this->fields.structure[i].type->contains_64bit())
287             return true;
288       }
289       return false;
290    } else {
291       return this->is_64bit();
292    }
293 }
294 
295 bool
contains_opaque() const296 glsl_type::contains_opaque() const {
297    switch (base_type) {
298    case GLSL_TYPE_SAMPLER:
299    case GLSL_TYPE_IMAGE:
300    case GLSL_TYPE_ATOMIC_UINT:
301       return true;
302    case GLSL_TYPE_ARRAY:
303       return fields.array->contains_opaque();
304    case GLSL_TYPE_STRUCT:
305    case GLSL_TYPE_INTERFACE:
306       for (unsigned int i = 0; i < length; i++) {
307          if (fields.structure[i].type->contains_opaque())
308             return true;
309       }
310       return false;
311    default:
312       return false;
313    }
314 }
315 
316 bool
contains_subroutine() const317 glsl_type::contains_subroutine() const
318 {
319    if (this->is_array()) {
320       return this->fields.array->contains_subroutine();
321    } else if (this->is_struct() || this->is_interface()) {
322       for (unsigned int i = 0; i < this->length; i++) {
323          if (this->fields.structure[i].type->contains_subroutine())
324             return true;
325       }
326       return false;
327    } else {
328       return this->is_subroutine();
329    }
330 }
331 
332 gl_texture_index
sampler_index() const333 glsl_type::sampler_index() const
334 {
335    const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
336 
337    assert(t->is_sampler() || t->is_image());
338 
339    switch (t->sampler_dimensionality) {
340    case GLSL_SAMPLER_DIM_1D:
341       return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
342    case GLSL_SAMPLER_DIM_2D:
343       return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
344    case GLSL_SAMPLER_DIM_3D:
345       return TEXTURE_3D_INDEX;
346    case GLSL_SAMPLER_DIM_CUBE:
347       return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
348    case GLSL_SAMPLER_DIM_RECT:
349       return TEXTURE_RECT_INDEX;
350    case GLSL_SAMPLER_DIM_BUF:
351       return TEXTURE_BUFFER_INDEX;
352    case GLSL_SAMPLER_DIM_EXTERNAL:
353       return TEXTURE_EXTERNAL_INDEX;
354    case GLSL_SAMPLER_DIM_MS:
355       return (t->sampler_array) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX : TEXTURE_2D_MULTISAMPLE_INDEX;
356    default:
357       assert(!"Should not get here.");
358       return TEXTURE_BUFFER_INDEX;
359    }
360 }
361 
362 bool
contains_image() const363 glsl_type::contains_image() const
364 {
365    if (this->is_array()) {
366       return this->fields.array->contains_image();
367    } else if (this->is_struct() || this->is_interface()) {
368       for (unsigned int i = 0; i < this->length; i++) {
369          if (this->fields.structure[i].type->contains_image())
370             return true;
371       }
372       return false;
373    } else {
374       return this->is_image();
375    }
376 }
377 
get_base_type() const378 const glsl_type *glsl_type::get_base_type() const
379 {
380    switch (base_type) {
381    case GLSL_TYPE_UINT:
382       return uint_type;
383    case GLSL_TYPE_UINT16:
384       return uint16_t_type;
385    case GLSL_TYPE_UINT8:
386       return uint8_t_type;
387    case GLSL_TYPE_INT:
388       return int_type;
389    case GLSL_TYPE_INT16:
390       return int16_t_type;
391    case GLSL_TYPE_INT8:
392       return int8_t_type;
393    case GLSL_TYPE_FLOAT:
394       return float_type;
395    case GLSL_TYPE_FLOAT16:
396       return float16_t_type;
397    case GLSL_TYPE_DOUBLE:
398       return double_type;
399    case GLSL_TYPE_BOOL:
400       return bool_type;
401    case GLSL_TYPE_UINT64:
402       return uint64_t_type;
403    case GLSL_TYPE_INT64:
404       return int64_t_type;
405    default:
406       return error_type;
407    }
408 }
409 
410 
get_scalar_type() const411 const glsl_type *glsl_type::get_scalar_type() const
412 {
413    const glsl_type *type = this;
414 
415    /* Handle arrays */
416    while (type->base_type == GLSL_TYPE_ARRAY)
417       type = type->fields.array;
418 
419    const glsl_type *scalar_type = type->get_base_type();
420    if (scalar_type == error_type)
421       return type;
422 
423    return scalar_type;
424 }
425 
426 
get_bare_type() const427 const glsl_type *glsl_type::get_bare_type() const
428 {
429    switch (this->base_type) {
430    case GLSL_TYPE_UINT8:
431    case GLSL_TYPE_INT8:
432    case GLSL_TYPE_UINT16:
433    case GLSL_TYPE_INT16:
434    case GLSL_TYPE_FLOAT16:
435    case GLSL_TYPE_UINT:
436    case GLSL_TYPE_INT:
437    case GLSL_TYPE_FLOAT:
438    case GLSL_TYPE_BOOL:
439    case GLSL_TYPE_DOUBLE:
440    case GLSL_TYPE_UINT64:
441    case GLSL_TYPE_INT64:
442       return get_instance(this->base_type, this->vector_elements,
443                           this->matrix_columns);
444 
445    case GLSL_TYPE_STRUCT:
446    case GLSL_TYPE_INTERFACE: {
447       glsl_struct_field *bare_fields = new glsl_struct_field[this->length];
448       for (unsigned i = 0; i < this->length; i++) {
449          bare_fields[i].type = this->fields.structure[i].type->get_bare_type();
450          bare_fields[i].name = this->fields.structure[i].name;
451       }
452       const glsl_type *bare_type =
453          get_struct_instance(bare_fields, this->length, this->name);
454       delete[] bare_fields;
455       return bare_type;
456    }
457 
458    case GLSL_TYPE_ARRAY:
459       return get_array_instance(this->fields.array->get_bare_type(),
460                                 this->length);
461 
462    case GLSL_TYPE_SAMPLER:
463    case GLSL_TYPE_IMAGE:
464    case GLSL_TYPE_ATOMIC_UINT:
465    case GLSL_TYPE_VOID:
466    case GLSL_TYPE_SUBROUTINE:
467    case GLSL_TYPE_FUNCTION:
468    case GLSL_TYPE_ERROR:
469       return this;
470    }
471 
472    unreachable("Invalid base type");
473 }
474 
get_float16_type() const475 const glsl_type *glsl_type::get_float16_type() const
476 {
477    assert(this->base_type == GLSL_TYPE_FLOAT);
478 
479    return get_instance(GLSL_TYPE_FLOAT16,
480                        this->vector_elements,
481                        this->matrix_columns,
482                        this->explicit_stride,
483                        this->interface_row_major);
484 }
485 
get_int16_type() const486 const glsl_type *glsl_type::get_int16_type() const
487 {
488    assert(this->base_type == GLSL_TYPE_INT);
489 
490    return get_instance(GLSL_TYPE_INT16,
491                        this->vector_elements,
492                        this->matrix_columns,
493                        this->explicit_stride,
494                        this->interface_row_major);
495 }
496 
get_uint16_type() const497 const glsl_type *glsl_type::get_uint16_type() const
498 {
499    assert(this->base_type == GLSL_TYPE_UINT);
500 
501    return get_instance(GLSL_TYPE_UINT16,
502                        this->vector_elements,
503                        this->matrix_columns,
504                        this->explicit_stride,
505                        this->interface_row_major);
506 }
507 
508 static void
hash_free_type_function(struct hash_entry * entry)509 hash_free_type_function(struct hash_entry *entry)
510 {
511    glsl_type *type = (glsl_type *) entry->data;
512 
513    if (type->is_array())
514       free((void*)entry->key);
515 
516    delete type;
517 }
518 
519 void
glsl_type_singleton_init_or_ref()520 glsl_type_singleton_init_or_ref()
521 {
522    /* This is required for _mesa_half_to_float() which is
523     * required for constant-folding 16-bit float ops.
524     */
525    util_cpu_detect();
526 
527    mtx_lock(&glsl_type::hash_mutex);
528    glsl_type_users++;
529    mtx_unlock(&glsl_type::hash_mutex);
530 }
531 
532 void
glsl_type_singleton_decref()533 glsl_type_singleton_decref()
534 {
535    mtx_lock(&glsl_type::hash_mutex);
536    assert(glsl_type_users > 0);
537 
538    /* Do not release glsl_types if they are still used. */
539    if (--glsl_type_users) {
540       mtx_unlock(&glsl_type::hash_mutex);
541       return;
542    }
543 
544    if (glsl_type::explicit_matrix_types != NULL) {
545       _mesa_hash_table_destroy(glsl_type::explicit_matrix_types,
546                                hash_free_type_function);
547       glsl_type::explicit_matrix_types = NULL;
548    }
549 
550    if (glsl_type::array_types != NULL) {
551       _mesa_hash_table_destroy(glsl_type::array_types, hash_free_type_function);
552       glsl_type::array_types = NULL;
553    }
554 
555    if (glsl_type::struct_types != NULL) {
556       _mesa_hash_table_destroy(glsl_type::struct_types, hash_free_type_function);
557       glsl_type::struct_types = NULL;
558    }
559 
560    if (glsl_type::interface_types != NULL) {
561       _mesa_hash_table_destroy(glsl_type::interface_types, hash_free_type_function);
562       glsl_type::interface_types = NULL;
563    }
564 
565    if (glsl_type::function_types != NULL) {
566       _mesa_hash_table_destroy(glsl_type::function_types, hash_free_type_function);
567       glsl_type::function_types = NULL;
568    }
569 
570    if (glsl_type::subroutine_types != NULL) {
571       _mesa_hash_table_destroy(glsl_type::subroutine_types, hash_free_type_function);
572       glsl_type::subroutine_types = NULL;
573    }
574 
575    mtx_unlock(&glsl_type::hash_mutex);
576 }
577 
578 
glsl_type(const glsl_type * array,unsigned length,unsigned explicit_stride)579 glsl_type::glsl_type(const glsl_type *array, unsigned length,
580                      unsigned explicit_stride) :
581    base_type(GLSL_TYPE_ARRAY), sampled_type(GLSL_TYPE_VOID),
582    sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
583    interface_packing(0), interface_row_major(0), packed(0),
584    vector_elements(0), matrix_columns(0),
585    length(length), name(NULL), explicit_stride(explicit_stride),
586    explicit_alignment(array->explicit_alignment)
587 {
588    this->fields.array = array;
589    /* Inherit the gl type of the base. The GL type is used for
590     * uniform/statevar handling in Mesa and the arrayness of the type
591     * is represented by the size rather than the type.
592     */
593    this->gl_type = array->gl_type;
594 
595    /* Allow a maximum of 10 characters for the array size.  This is enough
596     * for 32-bits of ~0.  The extra 3 are for the '[', ']', and terminating
597     * NUL.
598     */
599    const unsigned name_length = strlen(array->name) + 10 + 3;
600 
601    this->mem_ctx = ralloc_context(NULL);
602    assert(this->mem_ctx != NULL);
603 
604    char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
605 
606    if (length == 0)
607       snprintf(n, name_length, "%s[]", array->name);
608    else {
609       /* insert outermost dimensions in the correct spot
610        * otherwise the dimension order will be backwards
611        */
612       const char *pos = strchr(array->name, '[');
613       if (pos) {
614          int idx = pos - array->name;
615          snprintf(n, idx+1, "%s", array->name);
616          snprintf(n + idx, name_length - idx, "[%u]%s",
617                        length, array->name + idx);
618       } else {
619          snprintf(n, name_length, "%s[%u]", array->name, length);
620       }
621    }
622 
623    this->name = n;
624 }
625 
626 const glsl_type *
vec(unsigned components,const glsl_type * const ts[])627 glsl_type::vec(unsigned components, const glsl_type *const ts[])
628 {
629    unsigned n = components;
630 
631    if (components == 8)
632       n = 5;
633    else if (components == 16)
634       n = 6;
635 
636    if (n == 0 || n > 6)
637       return error_type;
638 
639    return ts[n - 1];
640 }
641 
642 #define VECN(components, sname, vname)           \
643 const glsl_type *                                \
644 glsl_type:: vname (unsigned components)          \
645 {                                                \
646    static const glsl_type *const ts[] = {        \
647       sname ## _type, vname ## 2_type,           \
648       vname ## 3_type, vname ## 4_type,          \
649       vname ## 8_type, vname ## 16_type,         \
650    };                                            \
651    return glsl_type::vec(components, ts);        \
652 }
653 
VECN(components,float,vec)654 VECN(components, float, vec)
655 VECN(components, float16_t, f16vec)
656 VECN(components, double, dvec)
657 VECN(components, int, ivec)
658 VECN(components, uint, uvec)
659 VECN(components, bool, bvec)
660 VECN(components, int64_t, i64vec)
661 VECN(components, uint64_t, u64vec)
662 VECN(components, int16_t, i16vec)
663 VECN(components, uint16_t, u16vec)
664 VECN(components, int8_t, i8vec)
665 VECN(components, uint8_t, u8vec)
666 
667 const glsl_type *
668 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns,
669                         unsigned explicit_stride, bool row_major,
670                         unsigned explicit_alignment)
671 {
672    if (base_type == GLSL_TYPE_VOID) {
673       assert(explicit_stride == 0 && explicit_alignment == 0 && !row_major);
674       return void_type;
675    }
676 
677    /* Matrix and vector types with explicit strides or alignment have to be
678     * looked up in a table so they're handled separately.
679     */
680    if (explicit_stride > 0 || explicit_alignment > 0) {
681       if (explicit_alignment > 0) {
682          assert(util_is_power_of_two_nonzero(explicit_alignment));
683          assert(explicit_stride % explicit_alignment == 0);
684       }
685 
686       const glsl_type *bare_type = get_instance(base_type, rows, columns);
687 
688       assert(columns > 1 || (rows > 1 && !row_major));
689 
690       char name[128];
691       snprintf(name, sizeof(name), "%sx%ua%uB%s", bare_type->name,
692                explicit_stride, explicit_alignment, row_major ? "RM" : "");
693 
694       mtx_lock(&glsl_type::hash_mutex);
695       assert(glsl_type_users > 0);
696 
697       if (explicit_matrix_types == NULL) {
698          explicit_matrix_types =
699             _mesa_hash_table_create(NULL, _mesa_hash_string,
700                                     _mesa_key_string_equal);
701       }
702 
703       const struct hash_entry *entry =
704          _mesa_hash_table_search(explicit_matrix_types, name);
705       if (entry == NULL) {
706          const glsl_type *t = new glsl_type(bare_type->gl_type,
707                                             (glsl_base_type)base_type,
708                                             rows, columns, name,
709                                             explicit_stride, row_major,
710                                             explicit_alignment);
711 
712          entry = _mesa_hash_table_insert(explicit_matrix_types,
713                                          t->name, (void *)t);
714       }
715 
716       assert(((glsl_type *) entry->data)->base_type == base_type);
717       assert(((glsl_type *) entry->data)->vector_elements == rows);
718       assert(((glsl_type *) entry->data)->matrix_columns == columns);
719       assert(((glsl_type *) entry->data)->explicit_stride == explicit_stride);
720       assert(((glsl_type *) entry->data)->explicit_alignment == explicit_alignment);
721 
722       const glsl_type *t = (const glsl_type *) entry->data;
723 
724       mtx_unlock(&glsl_type::hash_mutex);
725 
726       return t;
727    }
728 
729    assert(!row_major);
730 
731    /* Treat GLSL vectors as Nx1 matrices.
732     */
733    if (columns == 1) {
734       switch (base_type) {
735       case GLSL_TYPE_UINT:
736          return uvec(rows);
737       case GLSL_TYPE_INT:
738          return ivec(rows);
739       case GLSL_TYPE_FLOAT:
740          return vec(rows);
741       case GLSL_TYPE_FLOAT16:
742          return f16vec(rows);
743       case GLSL_TYPE_DOUBLE:
744          return dvec(rows);
745       case GLSL_TYPE_BOOL:
746          return bvec(rows);
747       case GLSL_TYPE_UINT64:
748          return u64vec(rows);
749       case GLSL_TYPE_INT64:
750          return i64vec(rows);
751       case GLSL_TYPE_UINT16:
752          return u16vec(rows);
753       case GLSL_TYPE_INT16:
754          return i16vec(rows);
755       case GLSL_TYPE_UINT8:
756          return u8vec(rows);
757       case GLSL_TYPE_INT8:
758          return i8vec(rows);
759       default:
760          return error_type;
761       }
762    } else {
763       if ((base_type != GLSL_TYPE_FLOAT &&
764            base_type != GLSL_TYPE_DOUBLE &&
765            base_type != GLSL_TYPE_FLOAT16) || (rows == 1))
766          return error_type;
767 
768       /* GLSL matrix types are named mat{COLUMNS}x{ROWS}.  Only the following
769        * combinations are valid:
770        *
771        *   1 2 3 4
772        * 1
773        * 2   x x x
774        * 3   x x x
775        * 4   x x x
776        */
777 #define IDX(c,r) (((c-1)*3) + (r-1))
778 
779       switch (base_type) {
780       case GLSL_TYPE_DOUBLE: {
781          switch (IDX(columns, rows)) {
782          case IDX(2,2): return dmat2_type;
783          case IDX(2,3): return dmat2x3_type;
784          case IDX(2,4): return dmat2x4_type;
785          case IDX(3,2): return dmat3x2_type;
786          case IDX(3,3): return dmat3_type;
787          case IDX(3,4): return dmat3x4_type;
788          case IDX(4,2): return dmat4x2_type;
789          case IDX(4,3): return dmat4x3_type;
790          case IDX(4,4): return dmat4_type;
791          default: return error_type;
792          }
793       }
794       case GLSL_TYPE_FLOAT: {
795          switch (IDX(columns, rows)) {
796          case IDX(2,2): return mat2_type;
797          case IDX(2,3): return mat2x3_type;
798          case IDX(2,4): return mat2x4_type;
799          case IDX(3,2): return mat3x2_type;
800          case IDX(3,3): return mat3_type;
801          case IDX(3,4): return mat3x4_type;
802          case IDX(4,2): return mat4x2_type;
803          case IDX(4,3): return mat4x3_type;
804          case IDX(4,4): return mat4_type;
805          default: return error_type;
806          }
807       }
808       case GLSL_TYPE_FLOAT16: {
809          switch (IDX(columns, rows)) {
810          case IDX(2,2): return f16mat2_type;
811          case IDX(2,3): return f16mat2x3_type;
812          case IDX(2,4): return f16mat2x4_type;
813          case IDX(3,2): return f16mat3x2_type;
814          case IDX(3,3): return f16mat3_type;
815          case IDX(3,4): return f16mat3x4_type;
816          case IDX(4,2): return f16mat4x2_type;
817          case IDX(4,3): return f16mat4x3_type;
818          case IDX(4,4): return f16mat4_type;
819          default: return error_type;
820          }
821       }
822       default: return error_type;
823       }
824    }
825 
826    assert(!"Should not get here.");
827    return error_type;
828 }
829 
830 const glsl_type *
get_sampler_instance(enum glsl_sampler_dim dim,bool shadow,bool array,glsl_base_type type)831 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim,
832                                 bool shadow,
833                                 bool array,
834                                 glsl_base_type type)
835 {
836    switch (type) {
837    case GLSL_TYPE_FLOAT:
838       switch (dim) {
839       case GLSL_SAMPLER_DIM_1D:
840          if (shadow)
841             return (array ? sampler1DArrayShadow_type : sampler1DShadow_type);
842          else
843             return (array ? sampler1DArray_type : sampler1D_type);
844       case GLSL_SAMPLER_DIM_2D:
845          if (shadow)
846             return (array ? sampler2DArrayShadow_type : sampler2DShadow_type);
847          else
848             return (array ? sampler2DArray_type : sampler2D_type);
849       case GLSL_SAMPLER_DIM_3D:
850          if (shadow || array)
851             return error_type;
852          else
853             return sampler3D_type;
854       case GLSL_SAMPLER_DIM_CUBE:
855          if (shadow)
856             return (array ? samplerCubeArrayShadow_type : samplerCubeShadow_type);
857          else
858             return (array ? samplerCubeArray_type : samplerCube_type);
859       case GLSL_SAMPLER_DIM_RECT:
860          if (array)
861             return error_type;
862          if (shadow)
863             return sampler2DRectShadow_type;
864          else
865             return sampler2DRect_type;
866       case GLSL_SAMPLER_DIM_BUF:
867          if (shadow || array)
868             return error_type;
869          else
870             return samplerBuffer_type;
871       case GLSL_SAMPLER_DIM_MS:
872          if (shadow)
873             return error_type;
874          return (array ? sampler2DMSArray_type : sampler2DMS_type);
875       case GLSL_SAMPLER_DIM_EXTERNAL:
876          if (shadow || array)
877             return error_type;
878          else
879             return samplerExternalOES_type;
880       case GLSL_SAMPLER_DIM_SUBPASS:
881       case GLSL_SAMPLER_DIM_SUBPASS_MS:
882          return error_type;
883       }
884    case GLSL_TYPE_INT:
885       if (shadow)
886          return error_type;
887       switch (dim) {
888       case GLSL_SAMPLER_DIM_1D:
889          return (array ? isampler1DArray_type : isampler1D_type);
890       case GLSL_SAMPLER_DIM_2D:
891          return (array ? isampler2DArray_type : isampler2D_type);
892       case GLSL_SAMPLER_DIM_3D:
893          if (array)
894             return error_type;
895          return isampler3D_type;
896       case GLSL_SAMPLER_DIM_CUBE:
897          return (array ? isamplerCubeArray_type : isamplerCube_type);
898       case GLSL_SAMPLER_DIM_RECT:
899          if (array)
900             return error_type;
901          return isampler2DRect_type;
902       case GLSL_SAMPLER_DIM_BUF:
903          if (array)
904             return error_type;
905          return isamplerBuffer_type;
906       case GLSL_SAMPLER_DIM_MS:
907          return (array ? isampler2DMSArray_type : isampler2DMS_type);
908       case GLSL_SAMPLER_DIM_EXTERNAL:
909          return error_type;
910       case GLSL_SAMPLER_DIM_SUBPASS:
911       case GLSL_SAMPLER_DIM_SUBPASS_MS:
912          return error_type;
913       }
914    case GLSL_TYPE_UINT:
915       if (shadow)
916          return error_type;
917       switch (dim) {
918       case GLSL_SAMPLER_DIM_1D:
919          return (array ? usampler1DArray_type : usampler1D_type);
920       case GLSL_SAMPLER_DIM_2D:
921          return (array ? usampler2DArray_type : usampler2D_type);
922       case GLSL_SAMPLER_DIM_3D:
923          if (array)
924             return error_type;
925          return usampler3D_type;
926       case GLSL_SAMPLER_DIM_CUBE:
927          return (array ? usamplerCubeArray_type : usamplerCube_type);
928       case GLSL_SAMPLER_DIM_RECT:
929          if (array)
930             return error_type;
931          return usampler2DRect_type;
932       case GLSL_SAMPLER_DIM_BUF:
933          if (array)
934             return error_type;
935          return usamplerBuffer_type;
936       case GLSL_SAMPLER_DIM_MS:
937          return (array ? usampler2DMSArray_type : usampler2DMS_type);
938       case GLSL_SAMPLER_DIM_EXTERNAL:
939          return error_type;
940       case GLSL_SAMPLER_DIM_SUBPASS:
941       case GLSL_SAMPLER_DIM_SUBPASS_MS:
942          return error_type;
943       }
944    case GLSL_TYPE_VOID:
945       return shadow ? samplerShadow_type : sampler_type;
946    default:
947       return error_type;
948    }
949 
950    unreachable("switch statement above should be complete");
951 }
952 
953 const glsl_type *
get_image_instance(enum glsl_sampler_dim dim,bool array,glsl_base_type type)954 glsl_type::get_image_instance(enum glsl_sampler_dim dim,
955                               bool array, glsl_base_type type)
956 {
957    switch (type) {
958    case GLSL_TYPE_FLOAT:
959       switch (dim) {
960       case GLSL_SAMPLER_DIM_1D:
961          return (array ? image1DArray_type : image1D_type);
962       case GLSL_SAMPLER_DIM_2D:
963          return (array ? image2DArray_type : image2D_type);
964       case GLSL_SAMPLER_DIM_3D:
965          return image3D_type;
966       case GLSL_SAMPLER_DIM_CUBE:
967          return (array ? imageCubeArray_type : imageCube_type);
968       case GLSL_SAMPLER_DIM_RECT:
969          if (array)
970             return error_type;
971          else
972             return image2DRect_type;
973       case GLSL_SAMPLER_DIM_BUF:
974          if (array)
975             return error_type;
976          else
977             return imageBuffer_type;
978       case GLSL_SAMPLER_DIM_MS:
979          return (array ? image2DMSArray_type : image2DMS_type);
980       case GLSL_SAMPLER_DIM_SUBPASS:
981          return subpassInput_type;
982       case GLSL_SAMPLER_DIM_SUBPASS_MS:
983          return subpassInputMS_type;
984       case GLSL_SAMPLER_DIM_EXTERNAL:
985          return error_type;
986       }
987    case GLSL_TYPE_INT:
988       switch (dim) {
989       case GLSL_SAMPLER_DIM_1D:
990          return (array ? iimage1DArray_type : iimage1D_type);
991       case GLSL_SAMPLER_DIM_2D:
992          return (array ? iimage2DArray_type : iimage2D_type);
993       case GLSL_SAMPLER_DIM_3D:
994          if (array)
995             return error_type;
996          return iimage3D_type;
997       case GLSL_SAMPLER_DIM_CUBE:
998          return (array ? iimageCubeArray_type : iimageCube_type);
999       case GLSL_SAMPLER_DIM_RECT:
1000          if (array)
1001             return error_type;
1002          return iimage2DRect_type;
1003       case GLSL_SAMPLER_DIM_BUF:
1004          if (array)
1005             return error_type;
1006          return iimageBuffer_type;
1007       case GLSL_SAMPLER_DIM_MS:
1008          return (array ? iimage2DMSArray_type : iimage2DMS_type);
1009       case GLSL_SAMPLER_DIM_SUBPASS:
1010          return isubpassInput_type;
1011       case GLSL_SAMPLER_DIM_SUBPASS_MS:
1012          return isubpassInputMS_type;
1013       case GLSL_SAMPLER_DIM_EXTERNAL:
1014          return error_type;
1015       }
1016    case GLSL_TYPE_UINT:
1017       switch (dim) {
1018       case GLSL_SAMPLER_DIM_1D:
1019          return (array ? uimage1DArray_type : uimage1D_type);
1020       case GLSL_SAMPLER_DIM_2D:
1021          return (array ? uimage2DArray_type : uimage2D_type);
1022       case GLSL_SAMPLER_DIM_3D:
1023          if (array)
1024             return error_type;
1025          return uimage3D_type;
1026       case GLSL_SAMPLER_DIM_CUBE:
1027          return (array ? uimageCubeArray_type : uimageCube_type);
1028       case GLSL_SAMPLER_DIM_RECT:
1029          if (array)
1030             return error_type;
1031          return uimage2DRect_type;
1032       case GLSL_SAMPLER_DIM_BUF:
1033          if (array)
1034             return error_type;
1035          return uimageBuffer_type;
1036       case GLSL_SAMPLER_DIM_MS:
1037          return (array ? uimage2DMSArray_type : uimage2DMS_type);
1038       case GLSL_SAMPLER_DIM_SUBPASS:
1039          return usubpassInput_type;
1040       case GLSL_SAMPLER_DIM_SUBPASS_MS:
1041          return usubpassInputMS_type;
1042       case GLSL_SAMPLER_DIM_EXTERNAL:
1043          return error_type;
1044       }
1045    case GLSL_TYPE_INT64:
1046       switch (dim) {
1047       case GLSL_SAMPLER_DIM_1D:
1048          return (array ? i64image1DArray_type : i64image1D_type);
1049       case GLSL_SAMPLER_DIM_2D:
1050          return (array ? i64image2DArray_type : i64image2D_type);
1051       case GLSL_SAMPLER_DIM_3D:
1052          if (array)
1053             return error_type;
1054          return i64image3D_type;
1055       case GLSL_SAMPLER_DIM_CUBE:
1056          return (array ? i64imageCubeArray_type : i64imageCube_type);
1057       case GLSL_SAMPLER_DIM_RECT:
1058          if (array)
1059             return error_type;
1060          return i64image2DRect_type;
1061       case GLSL_SAMPLER_DIM_BUF:
1062          if (array)
1063             return error_type;
1064          return i64imageBuffer_type;
1065       case GLSL_SAMPLER_DIM_MS:
1066          return (array ? i64image2DMSArray_type : i64image2DMS_type);
1067       case GLSL_SAMPLER_DIM_SUBPASS:
1068       case GLSL_SAMPLER_DIM_SUBPASS_MS:
1069       case GLSL_SAMPLER_DIM_EXTERNAL:
1070          return error_type;
1071       }
1072    case GLSL_TYPE_UINT64:
1073       switch (dim) {
1074       case GLSL_SAMPLER_DIM_1D:
1075          return (array ? u64image1DArray_type : u64image1D_type);
1076       case GLSL_SAMPLER_DIM_2D:
1077          return (array ? u64image2DArray_type : u64image2D_type);
1078       case GLSL_SAMPLER_DIM_3D:
1079          if (array)
1080             return error_type;
1081          return u64image3D_type;
1082       case GLSL_SAMPLER_DIM_CUBE:
1083          return (array ? u64imageCubeArray_type : u64imageCube_type);
1084       case GLSL_SAMPLER_DIM_RECT:
1085          if (array)
1086             return error_type;
1087          return u64image2DRect_type;
1088       case GLSL_SAMPLER_DIM_BUF:
1089          if (array)
1090             return error_type;
1091          return u64imageBuffer_type;
1092       case GLSL_SAMPLER_DIM_MS:
1093          return (array ? u64image2DMSArray_type : u64image2DMS_type);
1094       case GLSL_SAMPLER_DIM_SUBPASS:
1095       case GLSL_SAMPLER_DIM_SUBPASS_MS:
1096       case GLSL_SAMPLER_DIM_EXTERNAL:
1097          return error_type;
1098       }
1099    case GLSL_TYPE_VOID:
1100       switch (dim) {
1101       case GLSL_SAMPLER_DIM_1D:
1102          return (array ? vimage1DArray_type : vimage1D_type);
1103       case GLSL_SAMPLER_DIM_2D:
1104          return (array ? vimage2DArray_type : vimage2D_type);
1105       case GLSL_SAMPLER_DIM_3D:
1106          return (array ? error_type : vimage3D_type);
1107       case GLSL_SAMPLER_DIM_BUF:
1108          return (array ? error_type : vbuffer_type);
1109       default:
1110          return error_type;
1111       }
1112    default:
1113       return error_type;
1114    }
1115 
1116    unreachable("switch statement above should be complete");
1117 }
1118 
1119 const glsl_type *
get_array_instance(const glsl_type * base,unsigned array_size,unsigned explicit_stride)1120 glsl_type::get_array_instance(const glsl_type *base,
1121                               unsigned array_size,
1122                               unsigned explicit_stride)
1123 {
1124    /* Generate a name using the base type pointer in the key.  This is
1125     * done because the name of the base type may not be unique across
1126     * shaders.  For example, two shaders may have different record types
1127     * named 'foo'.
1128     */
1129    char key[128];
1130    snprintf(key, sizeof(key), "%p[%u]x%uB", (void *) base, array_size,
1131             explicit_stride);
1132 
1133    mtx_lock(&glsl_type::hash_mutex);
1134    assert(glsl_type_users > 0);
1135 
1136    if (array_types == NULL) {
1137       array_types = _mesa_hash_table_create(NULL, _mesa_hash_string,
1138                                             _mesa_key_string_equal);
1139    }
1140 
1141    const struct hash_entry *entry = _mesa_hash_table_search(array_types, key);
1142    if (entry == NULL) {
1143       const glsl_type *t = new glsl_type(base, array_size, explicit_stride);
1144 
1145       entry = _mesa_hash_table_insert(array_types,
1146                                       strdup(key),
1147                                       (void *) t);
1148    }
1149 
1150    assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_ARRAY);
1151    assert(((glsl_type *) entry->data)->length == array_size);
1152    assert(((glsl_type *) entry->data)->fields.array == base);
1153 
1154    glsl_type *t = (glsl_type *) entry->data;
1155 
1156    mtx_unlock(&glsl_type::hash_mutex);
1157 
1158    return t;
1159 }
1160 
1161 bool
compare_no_precision(const glsl_type * b) const1162 glsl_type::compare_no_precision(const glsl_type *b) const
1163 {
1164    if (this == b)
1165       return true;
1166 
1167    if (this->is_array()) {
1168       if (!b->is_array() || this->length != b->length)
1169          return false;
1170 
1171       const glsl_type *b_no_array = b->fields.array;
1172 
1173       return this->fields.array->compare_no_precision(b_no_array);
1174    }
1175 
1176    if (this->is_struct()) {
1177       if (!b->is_struct())
1178          return false;
1179    } else if (this->is_interface()) {
1180       if (!b->is_interface())
1181          return false;
1182    } else {
1183       return false;
1184    }
1185 
1186    return record_compare(b,
1187                          true, /* match_name */
1188                          true, /* match_locations */
1189                          false /* match_precision */);
1190 }
1191 
1192 bool
record_compare(const glsl_type * b,bool match_name,bool match_locations,bool match_precision) const1193 glsl_type::record_compare(const glsl_type *b, bool match_name,
1194                           bool match_locations, bool match_precision) const
1195 {
1196    if (this->length != b->length)
1197       return false;
1198 
1199    if (this->interface_packing != b->interface_packing)
1200       return false;
1201 
1202    if (this->interface_row_major != b->interface_row_major)
1203       return false;
1204 
1205    if (this->explicit_alignment != b->explicit_alignment)
1206       return false;
1207 
1208    if (this->packed != b->packed)
1209       return false;
1210 
1211    /* From the GLSL 4.20 specification (Sec 4.2):
1212     *
1213     *     "Structures must have the same name, sequence of type names, and
1214     *     type definitions, and field names to be considered the same type."
1215     *
1216     * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1217     *
1218     * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1219     *
1220     *     "Variables or block members declared as structures are considered
1221     *     to match in type if and only if structure members match in name,
1222     *     type, qualification, and declaration order."
1223     */
1224    if (match_name)
1225       if (strcmp(this->name, b->name) != 0)
1226          return false;
1227 
1228    for (unsigned i = 0; i < this->length; i++) {
1229       if (match_precision) {
1230          if (this->fields.structure[i].type != b->fields.structure[i].type)
1231             return false;
1232       } else {
1233          const glsl_type *ta = this->fields.structure[i].type;
1234          const glsl_type *tb = b->fields.structure[i].type;
1235          if (!ta->compare_no_precision(tb))
1236             return false;
1237       }
1238       if (strcmp(this->fields.structure[i].name,
1239                  b->fields.structure[i].name) != 0)
1240          return false;
1241       if (this->fields.structure[i].matrix_layout
1242          != b->fields.structure[i].matrix_layout)
1243         return false;
1244       if (match_locations && this->fields.structure[i].location
1245           != b->fields.structure[i].location)
1246          return false;
1247       if (this->fields.structure[i].component
1248           != b->fields.structure[i].component)
1249          return false;
1250       if (this->fields.structure[i].offset
1251           != b->fields.structure[i].offset)
1252          return false;
1253       if (this->fields.structure[i].interpolation
1254           != b->fields.structure[i].interpolation)
1255          return false;
1256       if (this->fields.structure[i].centroid
1257           != b->fields.structure[i].centroid)
1258          return false;
1259       if (this->fields.structure[i].sample
1260           != b->fields.structure[i].sample)
1261          return false;
1262       if (this->fields.structure[i].patch
1263           != b->fields.structure[i].patch)
1264          return false;
1265       if (this->fields.structure[i].memory_read_only
1266           != b->fields.structure[i].memory_read_only)
1267          return false;
1268       if (this->fields.structure[i].memory_write_only
1269           != b->fields.structure[i].memory_write_only)
1270          return false;
1271       if (this->fields.structure[i].memory_coherent
1272           != b->fields.structure[i].memory_coherent)
1273          return false;
1274       if (this->fields.structure[i].memory_volatile
1275           != b->fields.structure[i].memory_volatile)
1276          return false;
1277       if (this->fields.structure[i].memory_restrict
1278           != b->fields.structure[i].memory_restrict)
1279          return false;
1280       if (this->fields.structure[i].image_format
1281           != b->fields.structure[i].image_format)
1282          return false;
1283       if (match_precision &&
1284           this->fields.structure[i].precision
1285           != b->fields.structure[i].precision)
1286          return false;
1287       if (this->fields.structure[i].explicit_xfb_buffer
1288           != b->fields.structure[i].explicit_xfb_buffer)
1289          return false;
1290       if (this->fields.structure[i].xfb_buffer
1291           != b->fields.structure[i].xfb_buffer)
1292          return false;
1293       if (this->fields.structure[i].xfb_stride
1294           != b->fields.structure[i].xfb_stride)
1295          return false;
1296    }
1297 
1298    return true;
1299 }
1300 
1301 
1302 bool
record_key_compare(const void * a,const void * b)1303 glsl_type::record_key_compare(const void *a, const void *b)
1304 {
1305    const glsl_type *const key1 = (glsl_type *) a;
1306    const glsl_type *const key2 = (glsl_type *) b;
1307 
1308    return strcmp(key1->name, key2->name) == 0 &&
1309                  key1->record_compare(key2, true);
1310 }
1311 
1312 
1313 /**
1314  * Generate an integer hash value for a glsl_type structure type.
1315  */
1316 unsigned
record_key_hash(const void * a)1317 glsl_type::record_key_hash(const void *a)
1318 {
1319    const glsl_type *const key = (glsl_type *) a;
1320    uintptr_t hash = key->length;
1321    unsigned retval;
1322 
1323    for (unsigned i = 0; i < key->length; i++) {
1324       /* casting pointer to uintptr_t */
1325       hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
1326    }
1327 
1328    if (sizeof(hash) == 8)
1329       retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
1330    else
1331       retval = hash;
1332 
1333    return retval;
1334 }
1335 
1336 
1337 const glsl_type *
get_struct_instance(const glsl_struct_field * fields,unsigned num_fields,const char * name,bool packed,unsigned explicit_alignment)1338 glsl_type::get_struct_instance(const glsl_struct_field *fields,
1339                                unsigned num_fields,
1340                                const char *name,
1341                                bool packed, unsigned explicit_alignment)
1342 {
1343    const glsl_type key(fields, num_fields, name, packed, explicit_alignment);
1344 
1345    mtx_lock(&glsl_type::hash_mutex);
1346    assert(glsl_type_users > 0);
1347 
1348    if (struct_types == NULL) {
1349       struct_types = _mesa_hash_table_create(NULL, record_key_hash,
1350                                              record_key_compare);
1351    }
1352 
1353    const struct hash_entry *entry = _mesa_hash_table_search(struct_types,
1354                                                             &key);
1355    if (entry == NULL) {
1356       const glsl_type *t = new glsl_type(fields, num_fields, name, packed,
1357                                          explicit_alignment);
1358 
1359       entry = _mesa_hash_table_insert(struct_types, t, (void *) t);
1360    }
1361 
1362    assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_STRUCT);
1363    assert(((glsl_type *) entry->data)->length == num_fields);
1364    assert(strcmp(((glsl_type *) entry->data)->name, name) == 0);
1365    assert(((glsl_type *) entry->data)->packed == packed);
1366    assert(((glsl_type *) entry->data)->explicit_alignment == explicit_alignment);
1367 
1368    glsl_type *t = (glsl_type *) entry->data;
1369 
1370    mtx_unlock(&glsl_type::hash_mutex);
1371 
1372    return t;
1373 }
1374 
1375 
1376 const glsl_type *
get_interface_instance(const glsl_struct_field * fields,unsigned num_fields,enum glsl_interface_packing packing,bool row_major,const char * block_name)1377 glsl_type::get_interface_instance(const glsl_struct_field *fields,
1378                                   unsigned num_fields,
1379                                   enum glsl_interface_packing packing,
1380                                   bool row_major,
1381                                   const char *block_name)
1382 {
1383    const glsl_type key(fields, num_fields, packing, row_major, block_name);
1384 
1385    mtx_lock(&glsl_type::hash_mutex);
1386    assert(glsl_type_users > 0);
1387 
1388    if (interface_types == NULL) {
1389       interface_types = _mesa_hash_table_create(NULL, record_key_hash,
1390                                                 record_key_compare);
1391    }
1392 
1393    const struct hash_entry *entry = _mesa_hash_table_search(interface_types,
1394                                                             &key);
1395    if (entry == NULL) {
1396       const glsl_type *t = new glsl_type(fields, num_fields,
1397                                          packing, row_major, block_name);
1398 
1399       entry = _mesa_hash_table_insert(interface_types, t, (void *) t);
1400    }
1401 
1402    assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_INTERFACE);
1403    assert(((glsl_type *) entry->data)->length == num_fields);
1404    assert(strcmp(((glsl_type *) entry->data)->name, block_name) == 0);
1405 
1406    glsl_type *t = (glsl_type *) entry->data;
1407 
1408    mtx_unlock(&glsl_type::hash_mutex);
1409 
1410    return t;
1411 }
1412 
1413 const glsl_type *
get_subroutine_instance(const char * subroutine_name)1414 glsl_type::get_subroutine_instance(const char *subroutine_name)
1415 {
1416    const glsl_type key(subroutine_name);
1417 
1418    mtx_lock(&glsl_type::hash_mutex);
1419    assert(glsl_type_users > 0);
1420 
1421    if (subroutine_types == NULL) {
1422       subroutine_types = _mesa_hash_table_create(NULL, record_key_hash,
1423                                                  record_key_compare);
1424    }
1425 
1426    const struct hash_entry *entry = _mesa_hash_table_search(subroutine_types,
1427                                                             &key);
1428    if (entry == NULL) {
1429       const glsl_type *t = new glsl_type(subroutine_name);
1430 
1431       entry = _mesa_hash_table_insert(subroutine_types, t, (void *) t);
1432    }
1433 
1434    assert(((glsl_type *) entry->data)->base_type == GLSL_TYPE_SUBROUTINE);
1435    assert(strcmp(((glsl_type *) entry->data)->name, subroutine_name) == 0);
1436 
1437    glsl_type *t = (glsl_type *) entry->data;
1438 
1439    mtx_unlock(&glsl_type::hash_mutex);
1440 
1441    return t;
1442 }
1443 
1444 
1445 static bool
function_key_compare(const void * a,const void * b)1446 function_key_compare(const void *a, const void *b)
1447 {
1448    const glsl_type *const key1 = (glsl_type *) a;
1449    const glsl_type *const key2 = (glsl_type *) b;
1450 
1451    if (key1->length != key2->length)
1452       return false;
1453 
1454    return memcmp(key1->fields.parameters, key2->fields.parameters,
1455                  (key1->length + 1) * sizeof(*key1->fields.parameters)) == 0;
1456 }
1457 
1458 
1459 static uint32_t
function_key_hash(const void * a)1460 function_key_hash(const void *a)
1461 {
1462    const glsl_type *const key = (glsl_type *) a;
1463    return _mesa_hash_data(key->fields.parameters,
1464                           (key->length + 1) * sizeof(*key->fields.parameters));
1465 }
1466 
1467 const glsl_type *
get_function_instance(const glsl_type * return_type,const glsl_function_param * params,unsigned num_params)1468 glsl_type::get_function_instance(const glsl_type *return_type,
1469                                  const glsl_function_param *params,
1470                                  unsigned num_params)
1471 {
1472    const glsl_type key(return_type, params, num_params);
1473 
1474    mtx_lock(&glsl_type::hash_mutex);
1475    assert(glsl_type_users > 0);
1476 
1477    if (function_types == NULL) {
1478       function_types = _mesa_hash_table_create(NULL, function_key_hash,
1479                                                function_key_compare);
1480    }
1481 
1482    struct hash_entry *entry = _mesa_hash_table_search(function_types, &key);
1483    if (entry == NULL) {
1484       const glsl_type *t = new glsl_type(return_type, params, num_params);
1485 
1486       entry = _mesa_hash_table_insert(function_types, t, (void *) t);
1487    }
1488 
1489    const glsl_type *t = (const glsl_type *)entry->data;
1490 
1491    assert(t->base_type == GLSL_TYPE_FUNCTION);
1492    assert(t->length == num_params);
1493 
1494    mtx_unlock(&glsl_type::hash_mutex);
1495 
1496    return t;
1497 }
1498 
1499 
1500 const glsl_type *
get_mul_type(const glsl_type * type_a,const glsl_type * type_b)1501 glsl_type::get_mul_type(const glsl_type *type_a, const glsl_type *type_b)
1502 {
1503    if (type_a->is_matrix() && type_b->is_matrix()) {
1504       /* Matrix multiply.  The columns of A must match the rows of B.  Given
1505        * the other previously tested constraints, this means the vector type
1506        * of a row from A must be the same as the vector type of a column from
1507        * B.
1508        */
1509       if (type_a->row_type() == type_b->column_type()) {
1510          /* The resulting matrix has the number of columns of matrix B and
1511           * the number of rows of matrix A.  We get the row count of A by
1512           * looking at the size of a vector that makes up a column.  The
1513           * transpose (size of a row) is done for B.
1514           */
1515          const glsl_type *const type =
1516             get_instance(type_a->base_type,
1517                          type_a->column_type()->vector_elements,
1518                          type_b->row_type()->vector_elements);
1519          assert(type != error_type);
1520 
1521          return type;
1522       }
1523    } else if (type_a == type_b) {
1524       return type_a;
1525    } else if (type_a->is_matrix()) {
1526       /* A is a matrix and B is a column vector.  Columns of A must match
1527        * rows of B.  Given the other previously tested constraints, this
1528        * means the vector type of a row from A must be the same as the
1529        * vector the type of B.
1530        */
1531       if (type_a->row_type() == type_b) {
1532          /* The resulting vector has a number of elements equal to
1533           * the number of rows of matrix A. */
1534          const glsl_type *const type =
1535             get_instance(type_a->base_type,
1536                          type_a->column_type()->vector_elements,
1537                          1);
1538          assert(type != error_type);
1539 
1540          return type;
1541       }
1542    } else {
1543       assert(type_b->is_matrix());
1544 
1545       /* A is a row vector and B is a matrix.  Columns of A must match rows
1546        * of B.  Given the other previously tested constraints, this means
1547        * the type of A must be the same as the vector type of a column from
1548        * B.
1549        */
1550       if (type_a == type_b->column_type()) {
1551          /* The resulting vector has a number of elements equal to
1552           * the number of columns of matrix B. */
1553          const glsl_type *const type =
1554             get_instance(type_a->base_type,
1555                          type_b->row_type()->vector_elements,
1556                          1);
1557          assert(type != error_type);
1558 
1559          return type;
1560       }
1561    }
1562 
1563    return error_type;
1564 }
1565 
1566 
1567 const glsl_type *
field_type(const char * name) const1568 glsl_type::field_type(const char *name) const
1569 {
1570    if (this->base_type != GLSL_TYPE_STRUCT
1571        && this->base_type != GLSL_TYPE_INTERFACE)
1572       return error_type;
1573 
1574    for (unsigned i = 0; i < this->length; i++) {
1575       if (strcmp(name, this->fields.structure[i].name) == 0)
1576          return this->fields.structure[i].type;
1577    }
1578 
1579    return error_type;
1580 }
1581 
1582 
1583 int
field_index(const char * name) const1584 glsl_type::field_index(const char *name) const
1585 {
1586    if (this->base_type != GLSL_TYPE_STRUCT
1587        && this->base_type != GLSL_TYPE_INTERFACE)
1588       return -1;
1589 
1590    for (unsigned i = 0; i < this->length; i++) {
1591       if (strcmp(name, this->fields.structure[i].name) == 0)
1592          return i;
1593    }
1594 
1595    return -1;
1596 }
1597 
1598 
1599 unsigned
component_slots() const1600 glsl_type::component_slots() const
1601 {
1602    switch (this->base_type) {
1603    case GLSL_TYPE_UINT:
1604    case GLSL_TYPE_INT:
1605    case GLSL_TYPE_UINT8:
1606    case GLSL_TYPE_INT8:
1607    case GLSL_TYPE_UINT16:
1608    case GLSL_TYPE_INT16:
1609    case GLSL_TYPE_FLOAT:
1610    case GLSL_TYPE_FLOAT16:
1611    case GLSL_TYPE_BOOL:
1612       return this->components();
1613 
1614    case GLSL_TYPE_DOUBLE:
1615    case GLSL_TYPE_UINT64:
1616    case GLSL_TYPE_INT64:
1617       return 2 * this->components();
1618 
1619    case GLSL_TYPE_STRUCT:
1620    case GLSL_TYPE_INTERFACE: {
1621       unsigned size = 0;
1622 
1623       for (unsigned i = 0; i < this->length; i++)
1624          size += this->fields.structure[i].type->component_slots();
1625 
1626       return size;
1627    }
1628 
1629    case GLSL_TYPE_ARRAY:
1630       return this->length * this->fields.array->component_slots();
1631 
1632    case GLSL_TYPE_SAMPLER:
1633    case GLSL_TYPE_IMAGE:
1634       return 2;
1635 
1636    case GLSL_TYPE_SUBROUTINE:
1637       return 1;
1638 
1639    case GLSL_TYPE_FUNCTION:
1640    case GLSL_TYPE_ATOMIC_UINT:
1641    case GLSL_TYPE_VOID:
1642    case GLSL_TYPE_ERROR:
1643       break;
1644    }
1645 
1646    return 0;
1647 }
1648 
1649 unsigned
component_slots_aligned(unsigned offset) const1650 glsl_type::component_slots_aligned(unsigned offset) const
1651 {
1652    /* Align 64bit type only if it crosses attribute slot boundary. */
1653    switch (this->base_type) {
1654    case GLSL_TYPE_UINT:
1655    case GLSL_TYPE_INT:
1656    case GLSL_TYPE_UINT8:
1657    case GLSL_TYPE_INT8:
1658    case GLSL_TYPE_UINT16:
1659    case GLSL_TYPE_INT16:
1660    case GLSL_TYPE_FLOAT:
1661    case GLSL_TYPE_FLOAT16:
1662    case GLSL_TYPE_BOOL:
1663       return this->components();
1664 
1665    case GLSL_TYPE_DOUBLE:
1666    case GLSL_TYPE_UINT64:
1667    case GLSL_TYPE_INT64: {
1668       unsigned size = 2 * this->components();
1669       if (offset % 2 == 1 && (offset % 4 + size) > 4) {
1670          size++;
1671       }
1672 
1673       return size;
1674    }
1675 
1676    case GLSL_TYPE_STRUCT:
1677    case GLSL_TYPE_INTERFACE: {
1678       unsigned size = 0;
1679 
1680       for (unsigned i = 0; i < this->length; i++) {
1681          const glsl_type *member = this->fields.structure[i].type;
1682          size += member->component_slots_aligned(size + offset);
1683       }
1684 
1685       return size;
1686    }
1687 
1688    case GLSL_TYPE_ARRAY: {
1689       unsigned size = 0;
1690 
1691       for (unsigned i = 0; i < this->length; i++) {
1692          size += this->fields.array->component_slots_aligned(size + offset);
1693       }
1694 
1695       return size;
1696    }
1697 
1698    case GLSL_TYPE_SAMPLER:
1699    case GLSL_TYPE_IMAGE:
1700       return 2 + ((offset % 4) == 3 ? 1 : 0);
1701 
1702    case GLSL_TYPE_SUBROUTINE:
1703       return 1;
1704 
1705    case GLSL_TYPE_FUNCTION:
1706    case GLSL_TYPE_ATOMIC_UINT:
1707    case GLSL_TYPE_VOID:
1708    case GLSL_TYPE_ERROR:
1709       break;
1710    }
1711 
1712    return 0;
1713 }
1714 
1715 unsigned
struct_location_offset(unsigned length) const1716 glsl_type::struct_location_offset(unsigned length) const
1717 {
1718    unsigned offset = 0;
1719    const glsl_type *t = this->without_array();
1720    if (t->is_struct()) {
1721       assert(length <= t->length);
1722 
1723       for (unsigned i = 0; i < length; i++) {
1724          const glsl_type *st = t->fields.structure[i].type;
1725          const glsl_type *wa = st->without_array();
1726          if (wa->is_struct()) {
1727             unsigned r_offset = wa->struct_location_offset(wa->length);
1728             offset += st->is_array() ?
1729                st->arrays_of_arrays_size() * r_offset : r_offset;
1730          } else if (st->is_array() && st->fields.array->is_array()) {
1731             unsigned outer_array_size = st->length;
1732             const glsl_type *base_type = st->fields.array;
1733 
1734             /* For arrays of arrays the outer arrays take up a uniform
1735              * slot for each element. The innermost array elements share a
1736              * single slot so we ignore the innermost array when calculating
1737              * the offset.
1738              */
1739             while (base_type->fields.array->is_array()) {
1740                outer_array_size = outer_array_size * base_type->length;
1741                base_type = base_type->fields.array;
1742             }
1743             offset += outer_array_size;
1744          } else {
1745             /* We dont worry about arrays here because unless the array
1746              * contains a structure or another array it only takes up a single
1747              * uniform slot.
1748              */
1749             offset += 1;
1750          }
1751       }
1752    }
1753    return offset;
1754 }
1755 
1756 unsigned
uniform_locations() const1757 glsl_type::uniform_locations() const
1758 {
1759    unsigned size = 0;
1760 
1761    switch (this->base_type) {
1762    case GLSL_TYPE_UINT:
1763    case GLSL_TYPE_INT:
1764    case GLSL_TYPE_FLOAT:
1765    case GLSL_TYPE_FLOAT16:
1766    case GLSL_TYPE_DOUBLE:
1767    case GLSL_TYPE_UINT16:
1768    case GLSL_TYPE_UINT8:
1769    case GLSL_TYPE_INT16:
1770    case GLSL_TYPE_INT8:
1771    case GLSL_TYPE_UINT64:
1772    case GLSL_TYPE_INT64:
1773    case GLSL_TYPE_BOOL:
1774    case GLSL_TYPE_SAMPLER:
1775    case GLSL_TYPE_IMAGE:
1776    case GLSL_TYPE_SUBROUTINE:
1777       return 1;
1778 
1779    case GLSL_TYPE_STRUCT:
1780    case GLSL_TYPE_INTERFACE:
1781       for (unsigned i = 0; i < this->length; i++)
1782          size += this->fields.structure[i].type->uniform_locations();
1783       return size;
1784    case GLSL_TYPE_ARRAY:
1785       return this->length * this->fields.array->uniform_locations();
1786    default:
1787       return 0;
1788    }
1789 }
1790 
1791 unsigned
varying_count() const1792 glsl_type::varying_count() const
1793 {
1794    unsigned size = 0;
1795 
1796    switch (this->base_type) {
1797    case GLSL_TYPE_UINT:
1798    case GLSL_TYPE_INT:
1799    case GLSL_TYPE_FLOAT:
1800    case GLSL_TYPE_FLOAT16:
1801    case GLSL_TYPE_DOUBLE:
1802    case GLSL_TYPE_BOOL:
1803    case GLSL_TYPE_UINT16:
1804    case GLSL_TYPE_UINT8:
1805    case GLSL_TYPE_INT16:
1806    case GLSL_TYPE_INT8:
1807    case GLSL_TYPE_UINT64:
1808    case GLSL_TYPE_INT64:
1809       return 1;
1810 
1811    case GLSL_TYPE_STRUCT:
1812    case GLSL_TYPE_INTERFACE:
1813       for (unsigned i = 0; i < this->length; i++)
1814          size += this->fields.structure[i].type->varying_count();
1815       return size;
1816    case GLSL_TYPE_ARRAY:
1817       /* Don't count innermost array elements */
1818       if (this->without_array()->is_struct() ||
1819           this->without_array()->is_interface() ||
1820           this->fields.array->is_array())
1821          return this->length * this->fields.array->varying_count();
1822       else
1823          return this->fields.array->varying_count();
1824    default:
1825       assert(!"unsupported varying type");
1826       return 0;
1827    }
1828 }
1829 
1830 bool
can_implicitly_convert_to(const glsl_type * desired,_mesa_glsl_parse_state * state) const1831 glsl_type::can_implicitly_convert_to(const glsl_type *desired,
1832                                      _mesa_glsl_parse_state *state) const
1833 {
1834    if (this == desired)
1835       return true;
1836 
1837    /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1838     * state, we're doing intra-stage function linking where these checks have
1839     * already been done.
1840     */
1841    if (state && !state->has_implicit_conversions())
1842       return false;
1843 
1844    /* There is no conversion among matrix types. */
1845    if (this->matrix_columns > 1 || desired->matrix_columns > 1)
1846       return false;
1847 
1848    /* Vector size must match. */
1849    if (this->vector_elements != desired->vector_elements)
1850       return false;
1851 
1852    /* int and uint can be converted to float. */
1853    if (desired->is_float() && this->is_integer_32())
1854       return true;
1855 
1856    /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1857     * can be converted to uint.  Note that state may be NULL here, when
1858     * resolving function calls in the linker. By this time, all the
1859     * state-dependent checks have already happened though, so allow anything
1860     * that's allowed in any shader version.
1861     */
1862    if ((!state || state->has_implicit_int_to_uint_conversion()) &&
1863          desired->base_type == GLSL_TYPE_UINT && this->base_type == GLSL_TYPE_INT)
1864       return true;
1865 
1866    /* No implicit conversions from double. */
1867    if ((!state || state->has_double()) && this->is_double())
1868       return false;
1869 
1870    /* Conversions from different types to double. */
1871    if ((!state || state->has_double()) && desired->is_double()) {
1872       if (this->is_float())
1873          return true;
1874       if (this->is_integer_32())
1875          return true;
1876    }
1877 
1878    return false;
1879 }
1880 
1881 unsigned
std140_base_alignment(bool row_major) const1882 glsl_type::std140_base_alignment(bool row_major) const
1883 {
1884    unsigned N = is_64bit() ? 8 : 4;
1885 
1886    /* (1) If the member is a scalar consuming <N> basic machine units, the
1887     *     base alignment is <N>.
1888     *
1889     * (2) If the member is a two- or four-component vector with components
1890     *     consuming <N> basic machine units, the base alignment is 2<N> or
1891     *     4<N>, respectively.
1892     *
1893     * (3) If the member is a three-component vector with components consuming
1894     *     <N> basic machine units, the base alignment is 4<N>.
1895     */
1896    if (this->is_scalar() || this->is_vector()) {
1897       switch (this->vector_elements) {
1898       case 1:
1899          return N;
1900       case 2:
1901          return 2 * N;
1902       case 3:
1903       case 4:
1904          return 4 * N;
1905       }
1906    }
1907 
1908    /* (4) If the member is an array of scalars or vectors, the base alignment
1909     *     and array stride are set to match the base alignment of a single
1910     *     array element, according to rules (1), (2), and (3), and rounded up
1911     *     to the base alignment of a vec4. The array may have padding at the
1912     *     end; the base offset of the member following the array is rounded up
1913     *     to the next multiple of the base alignment.
1914     *
1915     * (6) If the member is an array of <S> column-major matrices with <C>
1916     *     columns and <R> rows, the matrix is stored identically to a row of
1917     *     <S>*<C> column vectors with <R> components each, according to rule
1918     *     (4).
1919     *
1920     * (8) If the member is an array of <S> row-major matrices with <C> columns
1921     *     and <R> rows, the matrix is stored identically to a row of <S>*<R>
1922     *     row vectors with <C> components each, according to rule (4).
1923     *
1924     * (10) If the member is an array of <S> structures, the <S> elements of
1925     *      the array are laid out in order, according to rule (9).
1926     */
1927    if (this->is_array()) {
1928       if (this->fields.array->is_scalar() ||
1929           this->fields.array->is_vector() ||
1930           this->fields.array->is_matrix()) {
1931          return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
1932       } else {
1933          assert(this->fields.array->is_struct() ||
1934                 this->fields.array->is_array());
1935          return this->fields.array->std140_base_alignment(row_major);
1936       }
1937    }
1938 
1939    /* (5) If the member is a column-major matrix with <C> columns and
1940     *     <R> rows, the matrix is stored identically to an array of
1941     *     <C> column vectors with <R> components each, according to
1942     *     rule (4).
1943     *
1944     * (7) If the member is a row-major matrix with <C> columns and <R>
1945     *     rows, the matrix is stored identically to an array of <R>
1946     *     row vectors with <C> components each, according to rule (4).
1947     */
1948    if (this->is_matrix()) {
1949       const struct glsl_type *vec_type, *array_type;
1950       int c = this->matrix_columns;
1951       int r = this->vector_elements;
1952 
1953       if (row_major) {
1954          vec_type = get_instance(base_type, c, 1);
1955          array_type = glsl_type::get_array_instance(vec_type, r);
1956       } else {
1957          vec_type = get_instance(base_type, r, 1);
1958          array_type = glsl_type::get_array_instance(vec_type, c);
1959       }
1960 
1961       return array_type->std140_base_alignment(false);
1962    }
1963 
1964    /* (9) If the member is a structure, the base alignment of the
1965     *     structure is <N>, where <N> is the largest base alignment
1966     *     value of any of its members, and rounded up to the base
1967     *     alignment of a vec4. The individual members of this
1968     *     sub-structure are then assigned offsets by applying this set
1969     *     of rules recursively, where the base offset of the first
1970     *     member of the sub-structure is equal to the aligned offset
1971     *     of the structure. The structure may have padding at the end;
1972     *     the base offset of the member following the sub-structure is
1973     *     rounded up to the next multiple of the base alignment of the
1974     *     structure.
1975     */
1976    if (this->is_struct()) {
1977       unsigned base_alignment = 16;
1978       for (unsigned i = 0; i < this->length; i++) {
1979          bool field_row_major = row_major;
1980          const enum glsl_matrix_layout matrix_layout =
1981             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
1982          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
1983             field_row_major = true;
1984          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
1985             field_row_major = false;
1986          }
1987 
1988          const struct glsl_type *field_type = this->fields.structure[i].type;
1989          base_alignment = MAX2(base_alignment,
1990                                field_type->std140_base_alignment(field_row_major));
1991       }
1992       return base_alignment;
1993    }
1994 
1995    assert(!"not reached");
1996    return -1;
1997 }
1998 
1999 unsigned
std140_size(bool row_major) const2000 glsl_type::std140_size(bool row_major) const
2001 {
2002    unsigned N = is_64bit() ? 8 : 4;
2003 
2004    /* (1) If the member is a scalar consuming <N> basic machine units, the
2005     *     base alignment is <N>.
2006     *
2007     * (2) If the member is a two- or four-component vector with components
2008     *     consuming <N> basic machine units, the base alignment is 2<N> or
2009     *     4<N>, respectively.
2010     *
2011     * (3) If the member is a three-component vector with components consuming
2012     *     <N> basic machine units, the base alignment is 4<N>.
2013     */
2014    if (this->is_scalar() || this->is_vector()) {
2015       assert(this->explicit_stride == 0);
2016       return this->vector_elements * N;
2017    }
2018 
2019    /* (5) If the member is a column-major matrix with <C> columns and
2020     *     <R> rows, the matrix is stored identically to an array of
2021     *     <C> column vectors with <R> components each, according to
2022     *     rule (4).
2023     *
2024     * (6) If the member is an array of <S> column-major matrices with <C>
2025     *     columns and <R> rows, the matrix is stored identically to a row of
2026     *     <S>*<C> column vectors with <R> components each, according to rule
2027     *     (4).
2028     *
2029     * (7) If the member is a row-major matrix with <C> columns and <R>
2030     *     rows, the matrix is stored identically to an array of <R>
2031     *     row vectors with <C> components each, according to rule (4).
2032     *
2033     * (8) If the member is an array of <S> row-major matrices with <C> columns
2034     *     and <R> rows, the matrix is stored identically to a row of <S>*<R>
2035     *     row vectors with <C> components each, according to rule (4).
2036     */
2037    if (this->without_array()->is_matrix()) {
2038       const struct glsl_type *element_type;
2039       const struct glsl_type *vec_type;
2040       unsigned int array_len;
2041 
2042       if (this->is_array()) {
2043          element_type = this->without_array();
2044          array_len = this->arrays_of_arrays_size();
2045       } else {
2046          element_type = this;
2047          array_len = 1;
2048       }
2049 
2050       if (row_major) {
2051          vec_type = get_instance(element_type->base_type,
2052                                  element_type->matrix_columns, 1);
2053 
2054          array_len *= element_type->vector_elements;
2055       } else {
2056          vec_type = get_instance(element_type->base_type,
2057                                  element_type->vector_elements, 1);
2058          array_len *= element_type->matrix_columns;
2059       }
2060       const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
2061                                                                   array_len);
2062 
2063       return array_type->std140_size(false);
2064    }
2065 
2066    /* (4) If the member is an array of scalars or vectors, the base alignment
2067     *     and array stride are set to match the base alignment of a single
2068     *     array element, according to rules (1), (2), and (3), and rounded up
2069     *     to the base alignment of a vec4. The array may have padding at the
2070     *     end; the base offset of the member following the array is rounded up
2071     *     to the next multiple of the base alignment.
2072     *
2073     * (10) If the member is an array of <S> structures, the <S> elements of
2074     *      the array are laid out in order, according to rule (9).
2075     */
2076    if (this->is_array()) {
2077       unsigned stride;
2078       if (this->without_array()->is_struct()) {
2079 	 stride = this->without_array()->std140_size(row_major);
2080       } else {
2081 	 unsigned element_base_align =
2082 	    this->without_array()->std140_base_alignment(row_major);
2083          stride = MAX2(element_base_align, 16);
2084       }
2085 
2086       unsigned size = this->arrays_of_arrays_size() * stride;
2087       assert(this->explicit_stride == 0 ||
2088              size == this->length * this->explicit_stride);
2089       return size;
2090    }
2091 
2092    /* (9) If the member is a structure, the base alignment of the
2093     *     structure is <N>, where <N> is the largest base alignment
2094     *     value of any of its members, and rounded up to the base
2095     *     alignment of a vec4. The individual members of this
2096     *     sub-structure are then assigned offsets by applying this set
2097     *     of rules recursively, where the base offset of the first
2098     *     member of the sub-structure is equal to the aligned offset
2099     *     of the structure. The structure may have padding at the end;
2100     *     the base offset of the member following the sub-structure is
2101     *     rounded up to the next multiple of the base alignment of the
2102     *     structure.
2103     */
2104    if (this->is_struct() || this->is_interface()) {
2105       unsigned size = 0;
2106       unsigned max_align = 0;
2107 
2108       for (unsigned i = 0; i < this->length; i++) {
2109          bool field_row_major = row_major;
2110          const enum glsl_matrix_layout matrix_layout =
2111             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2112          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2113             field_row_major = true;
2114          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2115             field_row_major = false;
2116          }
2117 
2118          const struct glsl_type *field_type = this->fields.structure[i].type;
2119          unsigned align = field_type->std140_base_alignment(field_row_major);
2120 
2121          /* Ignore unsized arrays when calculating size */
2122          if (field_type->is_unsized_array())
2123             continue;
2124 
2125          size = glsl_align(size, align);
2126          size += field_type->std140_size(field_row_major);
2127 
2128          max_align = MAX2(align, max_align);
2129 
2130          if (field_type->is_struct() && (i + 1 < this->length))
2131             size = glsl_align(size, 16);
2132       }
2133       size = glsl_align(size, MAX2(max_align, 16));
2134       return size;
2135    }
2136 
2137    assert(!"not reached");
2138    return -1;
2139 }
2140 
2141 const glsl_type *
get_explicit_std140_type(bool row_major) const2142 glsl_type::get_explicit_std140_type(bool row_major) const
2143 {
2144    if (this->is_vector() || this->is_scalar()) {
2145       return this;
2146    } else if (this->is_matrix()) {
2147       const glsl_type *vec_type;
2148       if (row_major)
2149          vec_type = get_instance(this->base_type, this->matrix_columns, 1);
2150       else
2151          vec_type = get_instance(this->base_type, this->vector_elements, 1);
2152       unsigned elem_size = vec_type->std140_size(false);
2153       unsigned stride = glsl_align(elem_size, 16);
2154       return get_instance(this->base_type, this->vector_elements,
2155                           this->matrix_columns, stride, row_major);
2156    } else if (this->is_array()) {
2157       unsigned elem_size = this->fields.array->std140_size(row_major);
2158       const glsl_type *elem_type =
2159          this->fields.array->get_explicit_std140_type(row_major);
2160       unsigned stride = glsl_align(elem_size, 16);
2161       return get_array_instance(elem_type, this->length, stride);
2162    } else if (this->is_struct() || this->is_interface()) {
2163       glsl_struct_field *fields = new glsl_struct_field[this->length];
2164       unsigned offset = 0;
2165       for (unsigned i = 0; i < length; i++) {
2166          fields[i] = this->fields.structure[i];
2167 
2168          bool field_row_major = row_major;
2169          if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2170             field_row_major = false;
2171          } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2172             field_row_major = true;
2173          }
2174          fields[i].type =
2175             fields[i].type->get_explicit_std140_type(field_row_major);
2176 
2177          unsigned fsize = fields[i].type->std140_size(field_row_major);
2178          unsigned falign = fields[i].type->std140_base_alignment(field_row_major);
2179          /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2180           * Layout Qualifiers":
2181           *
2182           *    "The actual offset of a member is computed as follows: If
2183           *    offset was declared, start with that offset, otherwise start
2184           *    with the next available offset. If the resulting offset is not
2185           *    a multiple of the actual alignment, increase it to the first
2186           *    offset that is a multiple of the actual alignment. This results
2187           *    in the actual offset the member will have."
2188           */
2189          if (fields[i].offset >= 0) {
2190             assert((unsigned)fields[i].offset >= offset);
2191             offset = fields[i].offset;
2192          }
2193          offset = glsl_align(offset, falign);
2194          fields[i].offset = offset;
2195          offset += fsize;
2196       }
2197 
2198       const glsl_type *type;
2199       if (this->is_struct())
2200          type = get_struct_instance(fields, this->length, this->name);
2201       else
2202          type = get_interface_instance(fields, this->length,
2203                                        (enum glsl_interface_packing)this->interface_packing,
2204                                        this->interface_row_major,
2205                                        this->name);
2206 
2207       delete[] fields;
2208       return type;
2209    } else {
2210       unreachable("Invalid type for UBO or SSBO");
2211    }
2212 }
2213 
2214 unsigned
std430_base_alignment(bool row_major) const2215 glsl_type::std430_base_alignment(bool row_major) const
2216 {
2217 
2218    unsigned N = is_64bit() ? 8 : 4;
2219 
2220    /* (1) If the member is a scalar consuming <N> basic machine units, the
2221     *     base alignment is <N>.
2222     *
2223     * (2) If the member is a two- or four-component vector with components
2224     *     consuming <N> basic machine units, the base alignment is 2<N> or
2225     *     4<N>, respectively.
2226     *
2227     * (3) If the member is a three-component vector with components consuming
2228     *     <N> basic machine units, the base alignment is 4<N>.
2229     */
2230    if (this->is_scalar() || this->is_vector()) {
2231       switch (this->vector_elements) {
2232       case 1:
2233          return N;
2234       case 2:
2235          return 2 * N;
2236       case 3:
2237       case 4:
2238          return 4 * N;
2239       }
2240    }
2241 
2242    /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2243     *
2244     * "When using the std430 storage layout, shader storage blocks will be
2245     * laid out in buffer storage identically to uniform and shader storage
2246     * blocks using the std140 layout, except that the base alignment and
2247     * stride of arrays of scalars and vectors in rule 4 and of structures
2248     * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2249     */
2250 
2251    /* (1) If the member is a scalar consuming <N> basic machine units, the
2252     *     base alignment is <N>.
2253     *
2254     * (2) If the member is a two- or four-component vector with components
2255     *     consuming <N> basic machine units, the base alignment is 2<N> or
2256     *     4<N>, respectively.
2257     *
2258     * (3) If the member is a three-component vector with components consuming
2259     *     <N> basic machine units, the base alignment is 4<N>.
2260     */
2261    if (this->is_array())
2262       return this->fields.array->std430_base_alignment(row_major);
2263 
2264    /* (5) If the member is a column-major matrix with <C> columns and
2265     *     <R> rows, the matrix is stored identically to an array of
2266     *     <C> column vectors with <R> components each, according to
2267     *     rule (4).
2268     *
2269     * (7) If the member is a row-major matrix with <C> columns and <R>
2270     *     rows, the matrix is stored identically to an array of <R>
2271     *     row vectors with <C> components each, according to rule (4).
2272     */
2273    if (this->is_matrix()) {
2274       const struct glsl_type *vec_type, *array_type;
2275       int c = this->matrix_columns;
2276       int r = this->vector_elements;
2277 
2278       if (row_major) {
2279          vec_type = get_instance(base_type, c, 1);
2280          array_type = glsl_type::get_array_instance(vec_type, r);
2281       } else {
2282          vec_type = get_instance(base_type, r, 1);
2283          array_type = glsl_type::get_array_instance(vec_type, c);
2284       }
2285 
2286       return array_type->std430_base_alignment(false);
2287    }
2288 
2289       /* (9) If the member is a structure, the base alignment of the
2290     *     structure is <N>, where <N> is the largest base alignment
2291     *     value of any of its members, and rounded up to the base
2292     *     alignment of a vec4. The individual members of this
2293     *     sub-structure are then assigned offsets by applying this set
2294     *     of rules recursively, where the base offset of the first
2295     *     member of the sub-structure is equal to the aligned offset
2296     *     of the structure. The structure may have padding at the end;
2297     *     the base offset of the member following the sub-structure is
2298     *     rounded up to the next multiple of the base alignment of the
2299     *     structure.
2300     */
2301    if (this->is_struct()) {
2302       unsigned base_alignment = 0;
2303       for (unsigned i = 0; i < this->length; i++) {
2304          bool field_row_major = row_major;
2305          const enum glsl_matrix_layout matrix_layout =
2306             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2307          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2308             field_row_major = true;
2309          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2310             field_row_major = false;
2311          }
2312 
2313          const struct glsl_type *field_type = this->fields.structure[i].type;
2314          base_alignment = MAX2(base_alignment,
2315                                field_type->std430_base_alignment(field_row_major));
2316       }
2317       assert(base_alignment > 0);
2318       return base_alignment;
2319    }
2320    assert(!"not reached");
2321    return -1;
2322 }
2323 
2324 unsigned
std430_array_stride(bool row_major) const2325 glsl_type::std430_array_stride(bool row_major) const
2326 {
2327    unsigned N = is_64bit() ? 8 : 4;
2328 
2329    /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2330     * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2331     *
2332     * (3) If the member is a three-component vector with components consuming
2333     *     <N> basic machine units, the base alignment is 4<N>.
2334     */
2335    if (this->is_vector() && this->vector_elements == 3)
2336       return 4 * N;
2337 
2338    /* By default use std430_size(row_major) */
2339    unsigned stride = this->std430_size(row_major);
2340    assert(this->explicit_stride == 0 || this->explicit_stride == stride);
2341    return stride;
2342 }
2343 
2344 /* Note that the value returned by this method is only correct if the
2345  * explit offset, and stride values are set, so only with SPIR-V shaders.
2346  * Should not be used with GLSL shaders.
2347  */
2348 
2349 unsigned
explicit_size(bool align_to_stride) const2350 glsl_type::explicit_size(bool align_to_stride) const
2351 {
2352    if (this->is_struct() || this->is_interface()) {
2353       if (this->length > 0) {
2354          unsigned size = 0;
2355 
2356          for (unsigned i = 0; i < this->length; i++) {
2357             assert(this->fields.structure[i].offset >= 0);
2358             unsigned last_byte = this->fields.structure[i].offset +
2359                this->fields.structure[i].type->explicit_size();
2360             size = MAX2(size, last_byte);
2361          }
2362 
2363          return size;
2364       } else {
2365          return 0;
2366       }
2367    } else if (this->is_array()) {
2368       /* From ARB_program_interface_query spec:
2369        *
2370        *   "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2371        *   minimum total buffer object size, in basic machine units, required to
2372        *   hold all active variables associated with an active uniform block, shader
2373        *   storage block, or atomic counter buffer is written to <params>.  If the
2374        *   final member of an active shader storage block is array with no declared
2375        *   size, the minimum buffer size is computed assuming the array was declared
2376        *   as an array with one element."
2377        *
2378        */
2379       if (this->is_unsized_array())
2380          return this->explicit_stride;
2381 
2382       assert(this->length > 0);
2383       unsigned elem_size = align_to_stride ? this->explicit_stride : this->fields.array->explicit_size();
2384       assert(this->explicit_stride >= elem_size);
2385 
2386       return this->explicit_stride * (this->length - 1) + elem_size;
2387    } else if (this->is_matrix()) {
2388       const struct glsl_type *elem_type;
2389       unsigned length;
2390 
2391       if (this->interface_row_major) {
2392          elem_type = get_instance(this->base_type,
2393                                   this->matrix_columns, 1);
2394          length = this->vector_elements;
2395       } else {
2396          elem_type = get_instance(this->base_type,
2397                                   this->vector_elements, 1);
2398          length = this->matrix_columns;
2399       }
2400 
2401       unsigned elem_size = align_to_stride ? this->explicit_stride : elem_type->explicit_size();
2402 
2403       assert(this->explicit_stride);
2404       return this->explicit_stride * (length - 1) + elem_size;
2405    }
2406 
2407    unsigned N = this->bit_size() / 8;
2408 
2409    return this->vector_elements * N;
2410 }
2411 
2412 unsigned
std430_size(bool row_major) const2413 glsl_type::std430_size(bool row_major) const
2414 {
2415    unsigned N = is_64bit() ? 8 : 4;
2416 
2417    /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2418     *
2419     * "When using the std430 storage layout, shader storage blocks will be
2420     * laid out in buffer storage identically to uniform and shader storage
2421     * blocks using the std140 layout, except that the base alignment and
2422     * stride of arrays of scalars and vectors in rule 4 and of structures
2423     * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2424     */
2425    if (this->is_scalar() || this->is_vector()) {
2426       assert(this->explicit_stride == 0);
2427       return this->vector_elements * N;
2428    }
2429 
2430    if (this->without_array()->is_matrix()) {
2431       const struct glsl_type *element_type;
2432       const struct glsl_type *vec_type;
2433       unsigned int array_len;
2434 
2435       if (this->is_array()) {
2436          element_type = this->without_array();
2437          array_len = this->arrays_of_arrays_size();
2438       } else {
2439          element_type = this;
2440          array_len = 1;
2441       }
2442 
2443       if (row_major) {
2444          vec_type = get_instance(element_type->base_type,
2445                                  element_type->matrix_columns, 1);
2446 
2447          array_len *= element_type->vector_elements;
2448       } else {
2449          vec_type = get_instance(element_type->base_type,
2450                                  element_type->vector_elements, 1);
2451          array_len *= element_type->matrix_columns;
2452       }
2453       const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
2454                                                                   array_len);
2455 
2456       return array_type->std430_size(false);
2457    }
2458 
2459    if (this->is_array()) {
2460       unsigned stride;
2461       if (this->without_array()->is_struct())
2462          stride = this->without_array()->std430_size(row_major);
2463       else
2464          stride = this->without_array()->std430_base_alignment(row_major);
2465 
2466       unsigned size = this->arrays_of_arrays_size() * stride;
2467       assert(this->explicit_stride == 0 ||
2468              size == this->length * this->explicit_stride);
2469       return size;
2470    }
2471 
2472    if (this->is_struct() || this->is_interface()) {
2473       unsigned size = 0;
2474       unsigned max_align = 0;
2475 
2476       for (unsigned i = 0; i < this->length; i++) {
2477          bool field_row_major = row_major;
2478          const enum glsl_matrix_layout matrix_layout =
2479             glsl_matrix_layout(this->fields.structure[i].matrix_layout);
2480          if (matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2481             field_row_major = true;
2482          } else if (matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2483             field_row_major = false;
2484          }
2485 
2486          const struct glsl_type *field_type = this->fields.structure[i].type;
2487          unsigned align = field_type->std430_base_alignment(field_row_major);
2488          size = glsl_align(size, align);
2489          size += field_type->std430_size(field_row_major);
2490 
2491          max_align = MAX2(align, max_align);
2492       }
2493       size = glsl_align(size, max_align);
2494       return size;
2495    }
2496 
2497    assert(!"not reached");
2498    return -1;
2499 }
2500 
2501 const glsl_type *
get_explicit_std430_type(bool row_major) const2502 glsl_type::get_explicit_std430_type(bool row_major) const
2503 {
2504    if (this->is_vector() || this->is_scalar()) {
2505       return this;
2506    } else if (this->is_matrix()) {
2507       const glsl_type *vec_type;
2508       if (row_major)
2509          vec_type = get_instance(this->base_type, this->matrix_columns, 1);
2510       else
2511          vec_type = get_instance(this->base_type, this->vector_elements, 1);
2512       unsigned stride = vec_type->std430_array_stride(false);
2513       return get_instance(this->base_type, this->vector_elements,
2514                           this->matrix_columns, stride, row_major);
2515    } else if (this->is_array()) {
2516       const glsl_type *elem_type =
2517          this->fields.array->get_explicit_std430_type(row_major);
2518       unsigned stride = this->fields.array->std430_array_stride(row_major);
2519       return get_array_instance(elem_type, this->length, stride);
2520    } else if (this->is_struct() || this->is_interface()) {
2521       glsl_struct_field *fields = new glsl_struct_field[this->length];
2522       unsigned offset = 0;
2523       for (unsigned i = 0; i < length; i++) {
2524          fields[i] = this->fields.structure[i];
2525 
2526          bool field_row_major = row_major;
2527          if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR) {
2528             field_row_major = false;
2529          } else if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
2530             field_row_major = true;
2531          }
2532          fields[i].type =
2533             fields[i].type->get_explicit_std430_type(field_row_major);
2534 
2535          unsigned fsize = fields[i].type->std430_size(field_row_major);
2536          unsigned falign = fields[i].type->std430_base_alignment(field_row_major);
2537          /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2538           * Layout Qualifiers":
2539           *
2540           *    "The actual offset of a member is computed as follows: If
2541           *    offset was declared, start with that offset, otherwise start
2542           *    with the next available offset. If the resulting offset is not
2543           *    a multiple of the actual alignment, increase it to the first
2544           *    offset that is a multiple of the actual alignment. This results
2545           *    in the actual offset the member will have."
2546           */
2547          if (fields[i].offset >= 0) {
2548             assert((unsigned)fields[i].offset >= offset);
2549             offset = fields[i].offset;
2550          }
2551          offset = glsl_align(offset, falign);
2552          fields[i].offset = offset;
2553          offset += fsize;
2554       }
2555 
2556       const glsl_type *type;
2557       if (this->is_struct())
2558          type = get_struct_instance(fields, this->length, this->name);
2559       else
2560          type = get_interface_instance(fields, this->length,
2561                                        (enum glsl_interface_packing)this->interface_packing,
2562                                        this->interface_row_major,
2563                                        this->name);
2564 
2565       delete[] fields;
2566       return type;
2567    } else {
2568       unreachable("Invalid type for SSBO");
2569    }
2570 }
2571 
2572 const glsl_type *
get_explicit_interface_type(bool supports_std430) const2573 glsl_type::get_explicit_interface_type(bool supports_std430) const
2574 {
2575    enum glsl_interface_packing packing =
2576       this->get_internal_ifc_packing(supports_std430);
2577    if (packing == GLSL_INTERFACE_PACKING_STD140) {
2578       return this->get_explicit_std140_type(this->interface_row_major);
2579    } else {
2580       assert(packing == GLSL_INTERFACE_PACKING_STD430);
2581       return this->get_explicit_std430_type(this->interface_row_major);
2582    }
2583 }
2584 
2585 static unsigned
explicit_type_scalar_byte_size(const glsl_type * type)2586 explicit_type_scalar_byte_size(const glsl_type *type)
2587 {
2588    if (type->base_type == GLSL_TYPE_BOOL)
2589       return 4;
2590    else
2591       return glsl_base_type_get_bit_size(type->base_type) / 8;
2592 }
2593 
2594 /* This differs from get_explicit_std430_type() in that it:
2595  * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2596  *   of "stride * len")
2597  * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2598  *   packed more tightly
2599  * - overrides any struct field offsets but get_explicit_std430_type() tries to
2600  *   respect any existing ones
2601  */
2602 const glsl_type *
get_explicit_type_for_size_align(glsl_type_size_align_func type_info,unsigned * size,unsigned * alignment) const2603 glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info,
2604                                             unsigned *size, unsigned *alignment) const
2605 {
2606    if (this->is_image() || this->is_sampler()) {
2607       type_info(this, size, alignment);
2608       assert(*alignment > 0);
2609       return this;
2610    } else if (this->is_scalar()) {
2611       type_info(this, size, alignment);
2612       assert(*size == explicit_type_scalar_byte_size(this));
2613       assert(*alignment == explicit_type_scalar_byte_size(this));
2614       return this;
2615    } else if (this->is_vector()) {
2616       type_info(this, size, alignment);
2617       assert(*alignment > 0);
2618       assert(*alignment % explicit_type_scalar_byte_size(this) == 0);
2619       return glsl_type::get_instance(this->base_type, this->vector_elements,
2620                                      1, 0, false, *alignment);
2621    } else if (this->is_array()) {
2622       unsigned elem_size, elem_align;
2623       const struct glsl_type *explicit_element =
2624          this->fields.array->get_explicit_type_for_size_align(type_info, &elem_size, &elem_align);
2625 
2626       unsigned stride = align(elem_size, elem_align);
2627 
2628       *size = stride * (this->length - 1) + elem_size;
2629       *alignment = elem_align;
2630       return glsl_type::get_array_instance(explicit_element, this->length, stride);
2631    } else if (this->is_struct() || this->is_interface()) {
2632       struct glsl_struct_field *fields = (struct glsl_struct_field *)
2633          malloc(sizeof(struct glsl_struct_field) * this->length);
2634 
2635       *size = 0;
2636       *alignment = 0;
2637       for (unsigned i = 0; i < this->length; i++) {
2638          fields[i] = this->fields.structure[i];
2639          assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR);
2640 
2641          unsigned field_size, field_align;
2642          fields[i].type =
2643             fields[i].type->get_explicit_type_for_size_align(type_info, &field_size, &field_align);
2644          field_align = this->packed ? 1 : field_align;
2645          fields[i].offset = align(*size, field_align);
2646 
2647          *size = fields[i].offset + field_size;
2648          *alignment = MAX2(*alignment, field_align);
2649       }
2650 
2651       const glsl_type *type;
2652       if (this->is_struct()) {
2653          type = get_struct_instance(fields, this->length, this->name,
2654                                     this->packed, *alignment);
2655       } else {
2656          assert(!this->packed);
2657          type = get_interface_instance(fields, this->length,
2658                                        (enum glsl_interface_packing)this->interface_packing,
2659                                        this->interface_row_major,
2660                                        this->name);
2661       }
2662       free(fields);
2663       return type;
2664    } else if (this->is_matrix()) {
2665       unsigned col_size, col_align;
2666       type_info(this->column_type(), &col_size, &col_align);
2667       unsigned stride = align(col_size, col_align);
2668 
2669       *size = this->matrix_columns * stride;
2670       /* Matrix and column alignments match. See glsl_type::column_type() */
2671       assert(col_align > 0);
2672       *alignment = col_align;
2673       return glsl_type::get_instance(this->base_type, this->vector_elements,
2674                                      this->matrix_columns, stride, false, *alignment);
2675    } else {
2676       unreachable("Unhandled type.");
2677    }
2678 }
2679 
2680 const glsl_type *
replace_vec3_with_vec4() const2681 glsl_type::replace_vec3_with_vec4() const
2682 {
2683    if (this->is_scalar() || this->is_vector() || this->is_matrix()) {
2684       if (this->interface_row_major) {
2685          if (this->matrix_columns == 3) {
2686             return glsl_type::get_instance(this->base_type,
2687                                            this->vector_elements,
2688                                            4, /* matrix columns */
2689                                            this->explicit_stride,
2690                                            this->interface_row_major,
2691                                            this->explicit_alignment);
2692          } else {
2693             return this;
2694          }
2695       } else {
2696          if (this->vector_elements == 3) {
2697             return glsl_type::get_instance(this->base_type,
2698                                            4, /* vector elements */
2699                                            this->matrix_columns,
2700                                            this->explicit_stride,
2701                                            this->interface_row_major,
2702                                            this->explicit_alignment);
2703          } else {
2704             return this;
2705          }
2706       }
2707    } else if (this->is_array()) {
2708       const glsl_type *vec4_elem_type =
2709          this->fields.array->replace_vec3_with_vec4();
2710       if (vec4_elem_type == this->fields.array)
2711          return this;
2712       return glsl_type::get_array_instance(vec4_elem_type,
2713                                            this->length,
2714                                            this->explicit_stride);
2715    } else if (this->is_struct() || this->is_interface()) {
2716       struct glsl_struct_field *fields = (struct glsl_struct_field *)
2717          malloc(sizeof(struct glsl_struct_field) * this->length);
2718 
2719       bool needs_new_type = false;
2720       for (unsigned i = 0; i < this->length; i++) {
2721          fields[i] = this->fields.structure[i];
2722          assert(fields[i].matrix_layout != GLSL_MATRIX_LAYOUT_ROW_MAJOR);
2723          fields[i].type = fields[i].type->replace_vec3_with_vec4();
2724          if (fields[i].type != this->fields.structure[i].type)
2725             needs_new_type = true;
2726       }
2727 
2728       const glsl_type *type;
2729       if (!needs_new_type) {
2730          type = this;
2731       } else if (this->is_struct()) {
2732          type = get_struct_instance(fields, this->length, this->name,
2733                                     this->packed, this->explicit_alignment);
2734       } else {
2735          assert(!this->packed);
2736          type = get_interface_instance(fields, this->length,
2737                                        (enum glsl_interface_packing)this->interface_packing,
2738                                        this->interface_row_major,
2739                                        this->name);
2740       }
2741       free(fields);
2742       return type;
2743    } else {
2744       unreachable("Unhandled type.");
2745    }
2746 }
2747 
2748 unsigned
count_vec4_slots(bool is_gl_vertex_input,bool is_bindless) const2749 glsl_type::count_vec4_slots(bool is_gl_vertex_input, bool is_bindless) const
2750 {
2751    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2752     *
2753     *     "A scalar input counts the same amount against this limit as a vec4,
2754     *     so applications may want to consider packing groups of four
2755     *     unrelated float inputs together into a vector to better utilize the
2756     *     capabilities of the underlying hardware. A matrix input will use up
2757     *     multiple locations.  The number of locations used will equal the
2758     *     number of columns in the matrix."
2759     *
2760     * The spec does not explicitly say how arrays are counted.  However, it
2761     * should be safe to assume the total number of slots consumed by an array
2762     * is the number of entries in the array multiplied by the number of slots
2763     * consumed by a single element of the array.
2764     *
2765     * The spec says nothing about how structs are counted, because vertex
2766     * attributes are not allowed to be (or contain) structs.  However, Mesa
2767     * allows varying structs, the number of varying slots taken up by a
2768     * varying struct is simply equal to the sum of the number of slots taken
2769     * up by each element.
2770     *
2771     * Doubles are counted different depending on whether they are vertex
2772     * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2773     * take one location no matter what size they are, otherwise dvec3/4
2774     * take two locations.
2775     */
2776    switch (this->base_type) {
2777    case GLSL_TYPE_UINT:
2778    case GLSL_TYPE_INT:
2779    case GLSL_TYPE_UINT8:
2780    case GLSL_TYPE_INT8:
2781    case GLSL_TYPE_UINT16:
2782    case GLSL_TYPE_INT16:
2783    case GLSL_TYPE_FLOAT:
2784    case GLSL_TYPE_FLOAT16:
2785    case GLSL_TYPE_BOOL:
2786       return this->matrix_columns;
2787    case GLSL_TYPE_DOUBLE:
2788    case GLSL_TYPE_UINT64:
2789    case GLSL_TYPE_INT64:
2790       if (this->vector_elements > 2 && !is_gl_vertex_input)
2791          return this->matrix_columns * 2;
2792       else
2793          return this->matrix_columns;
2794    case GLSL_TYPE_STRUCT:
2795    case GLSL_TYPE_INTERFACE: {
2796       unsigned size = 0;
2797 
2798       for (unsigned i = 0; i < this->length; i++) {
2799          const glsl_type *member_type = this->fields.structure[i].type;
2800          size += member_type->count_vec4_slots(is_gl_vertex_input, is_bindless);
2801       }
2802 
2803       return size;
2804    }
2805 
2806    case GLSL_TYPE_ARRAY: {
2807       const glsl_type *element = this->fields.array;
2808       return this->length * element->count_vec4_slots(is_gl_vertex_input,
2809                                                       is_bindless);
2810    }
2811 
2812    case GLSL_TYPE_SAMPLER:
2813    case GLSL_TYPE_IMAGE:
2814       if (!is_bindless)
2815          return 0;
2816       else
2817          return 1;
2818 
2819    case GLSL_TYPE_SUBROUTINE:
2820       return 1;
2821 
2822    case GLSL_TYPE_FUNCTION:
2823    case GLSL_TYPE_ATOMIC_UINT:
2824    case GLSL_TYPE_VOID:
2825    case GLSL_TYPE_ERROR:
2826       break;
2827    }
2828 
2829    assert(!"Unexpected type in count_attribute_slots()");
2830 
2831    return 0;
2832 }
2833 
2834 unsigned
count_dword_slots(bool is_bindless) const2835 glsl_type::count_dword_slots(bool is_bindless) const
2836 {
2837    switch (this->base_type) {
2838    case GLSL_TYPE_UINT:
2839    case GLSL_TYPE_INT:
2840    case GLSL_TYPE_FLOAT:
2841    case GLSL_TYPE_BOOL:
2842       return this->components();
2843    case GLSL_TYPE_UINT16:
2844    case GLSL_TYPE_INT16:
2845    case GLSL_TYPE_FLOAT16:
2846       return DIV_ROUND_UP(this->components(), 2);
2847    case GLSL_TYPE_UINT8:
2848    case GLSL_TYPE_INT8:
2849       return DIV_ROUND_UP(this->components(), 4);
2850    case GLSL_TYPE_IMAGE:
2851    case GLSL_TYPE_SAMPLER:
2852       if (!is_bindless)
2853          return 0;
2854       FALLTHROUGH;
2855    case GLSL_TYPE_DOUBLE:
2856    case GLSL_TYPE_UINT64:
2857    case GLSL_TYPE_INT64:
2858       return this->components() * 2;
2859    case GLSL_TYPE_ARRAY:
2860       return this->fields.array->count_dword_slots(is_bindless) *
2861              this->length;
2862 
2863    case GLSL_TYPE_INTERFACE:
2864    case GLSL_TYPE_STRUCT: {
2865       unsigned size = 0;
2866       for (unsigned i = 0; i < this->length; i++) {
2867          size += this->fields.structure[i].type->count_dword_slots(is_bindless);
2868       }
2869       return size;
2870    }
2871 
2872    case GLSL_TYPE_ATOMIC_UINT:
2873       return 0;
2874    case GLSL_TYPE_SUBROUTINE:
2875       return 1;
2876    case GLSL_TYPE_VOID:
2877    case GLSL_TYPE_ERROR:
2878    case GLSL_TYPE_FUNCTION:
2879    default:
2880       unreachable("invalid type in st_glsl_type_dword_size()");
2881    }
2882 
2883    return 0;
2884 }
2885 
2886 int
coordinate_components() const2887 glsl_type::coordinate_components() const
2888 {
2889    enum glsl_sampler_dim dim = (enum glsl_sampler_dim)sampler_dimensionality;
2890    int size = glsl_get_sampler_dim_coordinate_components(dim);
2891 
2892    /* Array textures need an additional component for the array index, except
2893     * for cubemap array images that behave like a 2D array of interleaved
2894     * cubemap faces.
2895     */
2896    if (sampler_array &&
2897        !(is_image() && sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE))
2898       size += 1;
2899 
2900    return size;
2901 }
2902 
2903 /**
2904  * Declarations of type flyweights (glsl_type::_foo_type) and
2905  * convenience pointers (glsl_type::foo_type).
2906  * @{
2907  */
2908 #define DECL_TYPE(NAME, ...)                                    \
2909    const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2910    const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2911 
2912 #define STRUCT_TYPE(NAME)
2913 
2914 #include "compiler/builtin_type_macros.h"
2915 /** @} */
2916 
2917 union packed_type {
2918    uint32_t u32;
2919    struct {
2920       unsigned base_type:5;
2921       unsigned interface_row_major:1;
2922       unsigned vector_elements:3;
2923       unsigned matrix_columns:3;
2924       unsigned explicit_stride:16;
2925       unsigned explicit_alignment:4;
2926    } basic;
2927    struct {
2928       unsigned base_type:5;
2929       unsigned dimensionality:4;
2930       unsigned shadow:1;
2931       unsigned array:1;
2932       unsigned sampled_type:5;
2933       unsigned _pad:16;
2934    } sampler;
2935    struct {
2936       unsigned base_type:5;
2937       unsigned length:13;
2938       unsigned explicit_stride:14;
2939    } array;
2940    struct {
2941       unsigned base_type:5;
2942       unsigned interface_packing_or_packed:2;
2943       unsigned interface_row_major:1;
2944       unsigned length:20;
2945       unsigned explicit_alignment:4;
2946    } strct;
2947 };
2948 
2949 static void
encode_glsl_struct_field(blob * blob,const glsl_struct_field * struct_field)2950 encode_glsl_struct_field(blob *blob, const glsl_struct_field *struct_field)
2951 {
2952    encode_type_to_blob(blob, struct_field->type);
2953    blob_write_string(blob, struct_field->name);
2954    blob_write_uint32(blob, struct_field->location);
2955    blob_write_uint32(blob, struct_field->component);
2956    blob_write_uint32(blob, struct_field->offset);
2957    blob_write_uint32(blob, struct_field->xfb_buffer);
2958    blob_write_uint32(blob, struct_field->xfb_stride);
2959    blob_write_uint32(blob, struct_field->image_format);
2960    blob_write_uint32(blob, struct_field->flags);
2961 }
2962 
2963 static void
decode_glsl_struct_field_from_blob(blob_reader * blob,glsl_struct_field * struct_field)2964 decode_glsl_struct_field_from_blob(blob_reader *blob, glsl_struct_field *struct_field)
2965 {
2966    struct_field->type = decode_type_from_blob(blob);
2967    struct_field->name = blob_read_string(blob);
2968    struct_field->location = blob_read_uint32(blob);
2969    struct_field->component = blob_read_uint32(blob);
2970    struct_field->offset = blob_read_uint32(blob);
2971    struct_field->xfb_buffer = blob_read_uint32(blob);
2972    struct_field->xfb_stride = blob_read_uint32(blob);
2973    struct_field->image_format = (pipe_format)blob_read_uint32(blob);
2974    struct_field->flags = blob_read_uint32(blob);
2975 }
2976 
2977 void
encode_type_to_blob(struct blob * blob,const glsl_type * type)2978 encode_type_to_blob(struct blob *blob, const glsl_type *type)
2979 {
2980    if (!type) {
2981       blob_write_uint32(blob, 0);
2982       return;
2983    }
2984 
2985    STATIC_ASSERT(sizeof(union packed_type) == 4);
2986    union packed_type encoded;
2987    encoded.u32 = 0;
2988    encoded.basic.base_type = type->base_type;
2989 
2990    switch (type->base_type) {
2991    case GLSL_TYPE_UINT:
2992    case GLSL_TYPE_INT:
2993    case GLSL_TYPE_FLOAT:
2994    case GLSL_TYPE_FLOAT16:
2995    case GLSL_TYPE_DOUBLE:
2996    case GLSL_TYPE_UINT8:
2997    case GLSL_TYPE_INT8:
2998    case GLSL_TYPE_UINT16:
2999    case GLSL_TYPE_INT16:
3000    case GLSL_TYPE_UINT64:
3001    case GLSL_TYPE_INT64:
3002    case GLSL_TYPE_BOOL:
3003       encoded.basic.interface_row_major = type->interface_row_major;
3004       assert(type->matrix_columns < 8);
3005       if (type->vector_elements <= 4)
3006          encoded.basic.vector_elements = type->vector_elements;
3007       else if (type->vector_elements == 8)
3008          encoded.basic.vector_elements = 5;
3009       else if (type->vector_elements == 16)
3010          encoded.basic.vector_elements = 6;
3011       encoded.basic.matrix_columns = type->matrix_columns;
3012       encoded.basic.explicit_stride = MIN2(type->explicit_stride, 0xffff);
3013       encoded.basic.explicit_alignment =
3014          MIN2(ffs(type->explicit_alignment), 0xf);
3015       blob_write_uint32(blob, encoded.u32);
3016       /* If we don't have enough bits for explicit_stride, store it
3017        * separately.
3018        */
3019       if (encoded.basic.explicit_stride == 0xffff)
3020          blob_write_uint32(blob, type->explicit_stride);
3021       if (encoded.basic.explicit_alignment == 0xf)
3022          blob_write_uint32(blob, type->explicit_alignment);
3023       return;
3024    case GLSL_TYPE_SAMPLER:
3025       encoded.sampler.dimensionality = type->sampler_dimensionality;
3026       encoded.sampler.shadow = type->sampler_shadow;
3027       encoded.sampler.array = type->sampler_array;
3028       encoded.sampler.sampled_type = type->sampled_type;
3029       break;
3030    case GLSL_TYPE_SUBROUTINE:
3031       blob_write_uint32(blob, encoded.u32);
3032       blob_write_string(blob, type->name);
3033       return;
3034    case GLSL_TYPE_IMAGE:
3035       encoded.sampler.dimensionality = type->sampler_dimensionality;
3036       encoded.sampler.array = type->sampler_array;
3037       encoded.sampler.sampled_type = type->sampled_type;
3038       break;
3039    case GLSL_TYPE_ATOMIC_UINT:
3040       break;
3041    case GLSL_TYPE_ARRAY:
3042       encoded.array.length = MIN2(type->length, 0x1fff);
3043       encoded.array.explicit_stride = MIN2(type->explicit_stride, 0x3fff);
3044       blob_write_uint32(blob, encoded.u32);
3045       /* If we don't have enough bits for length or explicit_stride, store it
3046        * separately.
3047        */
3048       if (encoded.array.length == 0x1fff)
3049          blob_write_uint32(blob, type->length);
3050       if (encoded.array.explicit_stride == 0x3fff)
3051          blob_write_uint32(blob, type->explicit_stride);
3052       encode_type_to_blob(blob, type->fields.array);
3053       return;
3054    case GLSL_TYPE_STRUCT:
3055    case GLSL_TYPE_INTERFACE:
3056       encoded.strct.length = MIN2(type->length, 0xfffff);
3057       encoded.strct.explicit_alignment =
3058          MIN2(ffs(type->explicit_alignment), 0xf);
3059       if (type->is_interface()) {
3060          encoded.strct.interface_packing_or_packed = type->interface_packing;
3061          encoded.strct.interface_row_major = type->interface_row_major;
3062       } else {
3063          encoded.strct.interface_packing_or_packed = type->packed;
3064       }
3065       blob_write_uint32(blob, encoded.u32);
3066       blob_write_string(blob, type->name);
3067 
3068       /* If we don't have enough bits for length, store it separately. */
3069       if (encoded.strct.length == 0xfffff)
3070          blob_write_uint32(blob, type->length);
3071       if (encoded.strct.explicit_alignment == 0xf)
3072          blob_write_uint32(blob, type->explicit_alignment);
3073 
3074       for (unsigned i = 0; i < type->length; i++)
3075          encode_glsl_struct_field(blob, &type->fields.structure[i]);
3076       return;
3077    case GLSL_TYPE_VOID:
3078       break;
3079    case GLSL_TYPE_ERROR:
3080    default:
3081       assert(!"Cannot encode type!");
3082       encoded.u32 = 0;
3083       break;
3084    }
3085 
3086    blob_write_uint32(blob, encoded.u32);
3087 }
3088 
3089 const glsl_type *
decode_type_from_blob(struct blob_reader * blob)3090 decode_type_from_blob(struct blob_reader *blob)
3091 {
3092    union packed_type encoded;
3093    encoded.u32 = blob_read_uint32(blob);
3094 
3095    if (encoded.u32 == 0) {
3096       return NULL;
3097    }
3098 
3099    glsl_base_type base_type = (glsl_base_type)encoded.basic.base_type;
3100 
3101    switch (base_type) {
3102    case GLSL_TYPE_UINT:
3103    case GLSL_TYPE_INT:
3104    case GLSL_TYPE_FLOAT:
3105    case GLSL_TYPE_FLOAT16:
3106    case GLSL_TYPE_DOUBLE:
3107    case GLSL_TYPE_UINT8:
3108    case GLSL_TYPE_INT8:
3109    case GLSL_TYPE_UINT16:
3110    case GLSL_TYPE_INT16:
3111    case GLSL_TYPE_UINT64:
3112    case GLSL_TYPE_INT64:
3113    case GLSL_TYPE_BOOL: {
3114       unsigned explicit_stride = encoded.basic.explicit_stride;
3115       if (explicit_stride == 0xffff)
3116          explicit_stride = blob_read_uint32(blob);
3117       unsigned explicit_alignment = encoded.basic.explicit_alignment;
3118       if (explicit_alignment == 0xf)
3119          explicit_alignment = blob_read_uint32(blob);
3120       else if (explicit_alignment > 0)
3121          explicit_alignment = 1 << (explicit_alignment - 1);
3122       uint32_t vector_elements = encoded.basic.vector_elements;
3123       if (vector_elements == 5)
3124          vector_elements = 8;
3125       else if (vector_elements == 6)
3126          vector_elements = 16;
3127       return glsl_type::get_instance(base_type, encoded.basic.vector_elements,
3128                                      encoded.basic.matrix_columns,
3129                                      explicit_stride,
3130                                      encoded.basic.interface_row_major,
3131                                      explicit_alignment);
3132    }
3133    case GLSL_TYPE_SAMPLER:
3134       return glsl_type::get_sampler_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
3135                                              encoded.sampler.shadow,
3136                                              encoded.sampler.array,
3137                                              (glsl_base_type) encoded.sampler.sampled_type);
3138    case GLSL_TYPE_SUBROUTINE:
3139       return glsl_type::get_subroutine_instance(blob_read_string(blob));
3140    case GLSL_TYPE_IMAGE:
3141       return glsl_type::get_image_instance((enum glsl_sampler_dim)encoded.sampler.dimensionality,
3142                                            encoded.sampler.array,
3143                                            (glsl_base_type) encoded.sampler.sampled_type);
3144    case GLSL_TYPE_ATOMIC_UINT:
3145       return glsl_type::atomic_uint_type;
3146    case GLSL_TYPE_ARRAY: {
3147       unsigned length = encoded.array.length;
3148       if (length == 0x1fff)
3149          length = blob_read_uint32(blob);
3150       unsigned explicit_stride = encoded.array.explicit_stride;
3151       if (explicit_stride == 0x3fff)
3152          explicit_stride = blob_read_uint32(blob);
3153       return glsl_type::get_array_instance(decode_type_from_blob(blob),
3154                                            length, explicit_stride);
3155    }
3156    case GLSL_TYPE_STRUCT:
3157    case GLSL_TYPE_INTERFACE: {
3158       char *name = blob_read_string(blob);
3159       unsigned num_fields = encoded.strct.length;
3160       if (num_fields == 0xfffff)
3161          num_fields = blob_read_uint32(blob);
3162       unsigned explicit_alignment = encoded.strct.explicit_alignment;
3163       if (explicit_alignment == 0xf)
3164          explicit_alignment = blob_read_uint32(blob);
3165       else if (explicit_alignment > 0)
3166          explicit_alignment = 1 << (explicit_alignment - 1);
3167 
3168       glsl_struct_field *fields =
3169          (glsl_struct_field *) malloc(sizeof(glsl_struct_field) * num_fields);
3170       for (unsigned i = 0; i < num_fields; i++)
3171          decode_glsl_struct_field_from_blob(blob, &fields[i]);
3172 
3173       const glsl_type *t;
3174       if (base_type == GLSL_TYPE_INTERFACE) {
3175          assert(explicit_alignment == 0);
3176          enum glsl_interface_packing packing =
3177             (glsl_interface_packing) encoded.strct.interface_packing_or_packed;
3178          bool row_major = encoded.strct.interface_row_major;
3179          t = glsl_type::get_interface_instance(fields, num_fields, packing,
3180                                                row_major, name);
3181       } else {
3182          unsigned packed = encoded.strct.interface_packing_or_packed;
3183          t = glsl_type::get_struct_instance(fields, num_fields, name, packed,
3184                                             explicit_alignment);
3185       }
3186 
3187       free(fields);
3188       return t;
3189    }
3190    case GLSL_TYPE_VOID:
3191       return glsl_type::void_type;
3192    case GLSL_TYPE_ERROR:
3193    default:
3194       assert(!"Cannot decode type!");
3195       return NULL;
3196    }
3197 }
3198 
3199 unsigned
cl_alignment() const3200 glsl_type::cl_alignment() const
3201 {
3202    /* vectors unlike arrays are aligned to their size */
3203    if (this->is_scalar() || this->is_vector())
3204       return this->cl_size();
3205    else if (this->is_array())
3206       return this->without_array()->cl_alignment();
3207    else if (this->is_struct()) {
3208       /* Packed Structs are 0x1 aligned despite their size. */
3209       if (this->packed)
3210          return 1;
3211 
3212       unsigned res = 1;
3213       for (unsigned i = 0; i < this->length; ++i) {
3214          struct glsl_struct_field &field = this->fields.structure[i];
3215          res = MAX2(res, field.type->cl_alignment());
3216       }
3217       return res;
3218    }
3219    return 1;
3220 }
3221 
3222 unsigned
cl_size() const3223 glsl_type::cl_size() const
3224 {
3225    if (this->is_scalar() || this->is_vector()) {
3226       return util_next_power_of_two(this->vector_elements) *
3227              explicit_type_scalar_byte_size(this);
3228    } else if (this->is_array()) {
3229       unsigned size = this->without_array()->cl_size();
3230       return size * this->length;
3231    } else if (this->is_struct()) {
3232       unsigned size = 0;
3233       for (unsigned i = 0; i < this->length; ++i) {
3234          struct glsl_struct_field &field = this->fields.structure[i];
3235          /* if a struct is packed, members don't get aligned */
3236          if (!this->packed)
3237             size = align(size, field.type->cl_alignment());
3238          size += field.type->cl_size();
3239       }
3240       return size;
3241    }
3242    return 1;
3243 }
3244 
3245 extern "C" {
3246 
3247 int
glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)3248 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim)
3249 {
3250    switch (dim) {
3251    case GLSL_SAMPLER_DIM_1D:
3252    case GLSL_SAMPLER_DIM_BUF:
3253       return 1;
3254    case GLSL_SAMPLER_DIM_2D:
3255    case GLSL_SAMPLER_DIM_RECT:
3256    case GLSL_SAMPLER_DIM_MS:
3257    case GLSL_SAMPLER_DIM_EXTERNAL:
3258    case GLSL_SAMPLER_DIM_SUBPASS:
3259    case GLSL_SAMPLER_DIM_SUBPASS_MS:
3260       return 2;
3261    case GLSL_SAMPLER_DIM_3D:
3262    case GLSL_SAMPLER_DIM_CUBE:
3263       return 3;
3264    default:
3265       unreachable("Unknown sampler dim");
3266    }
3267 }
3268 
3269 void
glsl_print_type(FILE * f,const glsl_type * t)3270 glsl_print_type(FILE *f, const glsl_type *t)
3271 {
3272    if (t->is_array()) {
3273       fprintf(f, "(array ");
3274       glsl_print_type(f, t->fields.array);
3275       fprintf(f, " %u)", t->length);
3276    } else if (t->is_struct() && !is_gl_identifier(t->name)) {
3277       fprintf(f, "%s@%p", t->name, (void *) t);
3278    } else {
3279       fprintf(f, "%s", t->name);
3280    }
3281 }
3282 
3283 }
3284