Home | History | Annotate | Download | only in compiler
      1 /*
      2  * Copyright  2014 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 DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Connor Abbott (cwabbott0 (at) gmail.com)
     25  *
     26  */
     27 
     28 #include "nir_types.h"
     29 #include "compiler/glsl/ir.h"
     30 
     31 const char *
     32 glsl_get_type_name(const glsl_type *type)
     33 {
     34    return type->name;
     35 }
     36 
     37 const glsl_type *
     38 glsl_get_array_element(const glsl_type* type)
     39 {
     40    if (type->is_matrix())
     41       return type->column_type();
     42    else if (type->is_vector())
     43       return type->get_scalar_type();
     44    return type->fields.array;
     45 }
     46 
     47 const glsl_type *
     48 glsl_without_array(const glsl_type *type)
     49 {
     50    return type->without_array();
     51 }
     52 
     53 const glsl_type *
     54 glsl_get_array_instance(const glsl_type *type,
     55                         unsigned array_size)
     56 {
     57    return glsl_type::get_array_instance(type, array_size);
     58 }
     59 
     60 const glsl_type *
     61 glsl_get_struct_field(const glsl_type *type, unsigned index)
     62 {
     63    return type->fields.structure[index].type;
     64 }
     65 
     66 const glsl_type *
     67 glsl_get_function_return_type(const glsl_type *type)
     68 {
     69    return type->fields.parameters[0].type;
     70 }
     71 
     72 const glsl_function_param *
     73 glsl_get_function_param(const glsl_type *type, unsigned index)
     74 {
     75    return &type->fields.parameters[index + 1];
     76 }
     77 
     78 const struct glsl_type *
     79 glsl_get_column_type(const struct glsl_type *type)
     80 {
     81    return type->column_type();
     82 }
     83 
     84 enum glsl_base_type
     85 glsl_get_base_type(const struct glsl_type *type)
     86 {
     87    return type->base_type;
     88 }
     89 
     90 unsigned
     91 glsl_get_vector_elements(const struct glsl_type *type)
     92 {
     93    return type->vector_elements;
     94 }
     95 
     96 unsigned
     97 glsl_get_components(const struct glsl_type *type)
     98 {
     99    return type->components();
    100 }
    101 
    102 unsigned
    103 glsl_get_matrix_columns(const struct glsl_type *type)
    104 {
    105    return type->matrix_columns;
    106 }
    107 
    108 unsigned
    109 glsl_get_length(const struct glsl_type *type)
    110 {
    111    return type->is_matrix() ? type->matrix_columns : type->length;
    112 }
    113 
    114 unsigned
    115 glsl_get_aoa_size(const struct glsl_type *type)
    116 {
    117    return type->arrays_of_arrays_size();
    118 }
    119 
    120 unsigned
    121 glsl_count_attribute_slots(const struct glsl_type *type,
    122                            bool is_vertex_input)
    123 {
    124    return type->count_attribute_slots(is_vertex_input);
    125 }
    126 
    127 const char *
    128 glsl_get_struct_elem_name(const struct glsl_type *type, unsigned index)
    129 {
    130    return type->fields.structure[index].name;
    131 }
    132 
    133 glsl_sampler_dim
    134 glsl_get_sampler_dim(const struct glsl_type *type)
    135 {
    136    assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
    137    return (glsl_sampler_dim)type->sampler_dimensionality;
    138 }
    139 
    140 glsl_base_type
    141 glsl_get_sampler_result_type(const struct glsl_type *type)
    142 {
    143    assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
    144    return (glsl_base_type)type->sampled_type;
    145 }
    146 
    147 unsigned
    148 glsl_get_record_location_offset(const struct glsl_type *type,
    149                                 unsigned length)
    150 {
    151    return type->record_location_offset(length);
    152 }
    153 
    154 bool
    155 glsl_type_is_64bit(const glsl_type *type)
    156 {
    157    return type->is_64bit();
    158 }
    159 
    160 bool
    161 glsl_type_is_void(const glsl_type *type)
    162 {
    163    return type->is_void();
    164 }
    165 
    166 bool
    167 glsl_type_is_error(const glsl_type *type)
    168 {
    169    return type->is_error();
    170 }
    171 
    172 bool
    173 glsl_type_is_vector(const struct glsl_type *type)
    174 {
    175    return type->is_vector();
    176 }
    177 
    178 bool
    179 glsl_type_is_scalar(const struct glsl_type *type)
    180 {
    181    return type->is_scalar();
    182 }
    183 
    184 bool
    185 glsl_type_is_vector_or_scalar(const struct glsl_type *type)
    186 {
    187    return type->is_vector() || type->is_scalar();
    188 }
    189 
    190 bool
    191 glsl_type_is_matrix(const struct glsl_type *type)
    192 {
    193    return type->is_matrix();
    194 }
    195 
    196 bool
    197 glsl_type_is_array(const struct glsl_type *type)
    198 {
    199    return type->is_array();
    200 }
    201 
    202 bool
    203 glsl_type_is_array_of_arrays(const struct glsl_type *type)
    204 {
    205    return type->is_array_of_arrays();
    206 }
    207 
    208 bool
    209 glsl_type_is_struct(const struct glsl_type *type)
    210 {
    211    return type->is_record() || type->is_interface();
    212 }
    213 
    214 bool
    215 glsl_type_is_sampler(const struct glsl_type *type)
    216 {
    217    return type->is_sampler();
    218 }
    219 
    220 bool
    221 glsl_type_is_image(const struct glsl_type *type)
    222 {
    223    return type->is_image();
    224 }
    225 
    226 bool
    227 glsl_sampler_type_is_shadow(const struct glsl_type *type)
    228 {
    229    assert(glsl_type_is_sampler(type));
    230    return type->sampler_shadow;
    231 }
    232 
    233 bool
    234 glsl_sampler_type_is_array(const struct glsl_type *type)
    235 {
    236    assert(glsl_type_is_sampler(type) || glsl_type_is_image(type));
    237    return type->sampler_array;
    238 }
    239 
    240 bool
    241 glsl_type_is_dual_slot(const struct glsl_type *type)
    242 {
    243    return type->is_dual_slot();
    244 }
    245 
    246 bool
    247 glsl_type_is_numeric(const struct glsl_type *type)
    248 {
    249    return type->is_numeric();
    250 }
    251 
    252 bool
    253 glsl_type_is_boolean(const struct glsl_type *type)
    254 {
    255    return type->is_boolean();
    256 }
    257 
    258 const glsl_type *
    259 glsl_void_type(void)
    260 {
    261    return glsl_type::void_type;
    262 }
    263 
    264 const glsl_type *
    265 glsl_float_type(void)
    266 {
    267    return glsl_type::float_type;
    268 }
    269 
    270 const glsl_type *
    271 glsl_double_type(void)
    272 {
    273    return glsl_type::double_type;
    274 }
    275 
    276 const glsl_type *
    277 glsl_float16_t_type(void)
    278 {
    279    return glsl_type::float16_t_type;
    280 }
    281 
    282 const glsl_type *
    283 glsl_vec_type(unsigned n)
    284 {
    285    return glsl_type::vec(n);
    286 }
    287 
    288 const glsl_type *
    289 glsl_dvec_type(unsigned n)
    290 {
    291    return glsl_type::dvec(n);
    292 }
    293 
    294 const glsl_type *
    295 glsl_vec4_type(void)
    296 {
    297    return glsl_type::vec4_type;
    298 }
    299 
    300 const glsl_type *
    301 glsl_uvec4_type(void)
    302 {
    303    return glsl_type::uvec4_type;
    304 }
    305 
    306 const glsl_type *
    307 glsl_int_type(void)
    308 {
    309    return glsl_type::int_type;
    310 }
    311 
    312 const glsl_type *
    313 glsl_uint_type(void)
    314 {
    315    return glsl_type::uint_type;
    316 }
    317 
    318 const glsl_type *
    319 glsl_int64_t_type(void)
    320 {
    321    return glsl_type::int64_t_type;
    322 }
    323 
    324 const glsl_type *
    325 glsl_uint64_t_type(void)
    326 {
    327    return glsl_type::uint64_t_type;
    328 }
    329 
    330 const glsl_type *
    331 glsl_int16_t_type(void)
    332 {
    333    return glsl_type::int16_t_type;
    334 }
    335 
    336 const glsl_type *
    337 glsl_uint16_t_type(void)
    338 {
    339    return glsl_type::uint16_t_type;
    340 }
    341 
    342 const glsl_type *
    343 glsl_bool_type(void)
    344 {
    345    return glsl_type::bool_type;
    346 }
    347 
    348 const glsl_type *
    349 glsl_scalar_type(enum glsl_base_type base_type)
    350 {
    351    return glsl_type::get_instance(base_type, 1, 1);
    352 }
    353 
    354 const glsl_type *
    355 glsl_vector_type(enum glsl_base_type base_type, unsigned components)
    356 {
    357    assert(components > 1 && components <= 4);
    358    return glsl_type::get_instance(base_type, components, 1);
    359 }
    360 
    361 const glsl_type *
    362 glsl_matrix_type(enum glsl_base_type base_type, unsigned rows, unsigned columns)
    363 {
    364    assert(rows > 1 && rows <= 4 && columns >= 1 && columns <= 4);
    365    return glsl_type::get_instance(base_type, rows, columns);
    366 }
    367 
    368 const glsl_type *
    369 glsl_array_type(const glsl_type *base, unsigned elements)
    370 {
    371    return glsl_type::get_array_instance(base, elements);
    372 }
    373 
    374 const glsl_type *
    375 glsl_struct_type(const glsl_struct_field *fields,
    376                  unsigned num_fields, const char *name)
    377 {
    378    return glsl_type::get_record_instance(fields, num_fields, name);
    379 }
    380 
    381 const glsl_type *
    382 glsl_interface_type(const glsl_struct_field *fields,
    383                     unsigned num_fields,
    384                     enum glsl_interface_packing packing,
    385                     bool row_major,
    386                     const char *block_name)
    387 {
    388    return glsl_type::get_interface_instance(fields, num_fields, packing,
    389                                             row_major, block_name);
    390 }
    391 
    392 const struct glsl_type *
    393 glsl_sampler_type(enum glsl_sampler_dim dim, bool is_shadow, bool is_array,
    394                   enum glsl_base_type base_type)
    395 {
    396    return glsl_type::get_sampler_instance(dim, is_shadow, is_array, base_type);
    397 }
    398 
    399 const struct glsl_type *
    400 glsl_bare_sampler_type()
    401 {
    402    return glsl_type::sampler_type;
    403 }
    404 
    405 const struct glsl_type *
    406 glsl_image_type(enum glsl_sampler_dim dim, bool is_array,
    407                 enum glsl_base_type base_type)
    408 {
    409    return glsl_type::get_image_instance(dim, is_array, base_type);
    410 }
    411 
    412 const glsl_type *
    413 glsl_function_type(const glsl_type *return_type,
    414                    const glsl_function_param *params, unsigned num_params)
    415 {
    416    return glsl_type::get_function_instance(return_type, params, num_params);
    417 }
    418 
    419 const glsl_type *
    420 glsl_transposed_type(const struct glsl_type *type)
    421 {
    422    assert(glsl_type_is_matrix(type));
    423    return glsl_type::get_instance(type->base_type, type->matrix_columns,
    424                                   type->vector_elements);
    425 }
    426 
    427 const glsl_type *
    428 glsl_channel_type(const glsl_type *t)
    429 {
    430    switch (glsl_get_base_type(t)) {
    431    case GLSL_TYPE_ARRAY: {
    432       const glsl_type *base = glsl_channel_type(glsl_get_array_element(t));
    433       return glsl_array_type(base, glsl_get_length(t));
    434    }
    435    case GLSL_TYPE_UINT:
    436       return glsl_uint_type();
    437    case GLSL_TYPE_INT:
    438       return glsl_int_type();
    439    case GLSL_TYPE_FLOAT:
    440       return glsl_float_type();
    441    case GLSL_TYPE_BOOL:
    442       return glsl_bool_type();
    443    case GLSL_TYPE_DOUBLE:
    444       return glsl_double_type();
    445    case GLSL_TYPE_UINT64:
    446       return glsl_uint64_t_type();
    447    case GLSL_TYPE_INT64:
    448       return glsl_int64_t_type();
    449    default:
    450       unreachable("Unhandled base type glsl_channel_type()");
    451    }
    452 }
    453