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