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