Home | History | Annotate | Download | only in glsl
      1 /*
      2  * Copyright  2013 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 /**
     25  * \file builtin_functions.cpp
     26  *
     27  * Support for GLSL built-in functions.
     28  *
     29  * This file is split into several main components:
     30  *
     31  * 1. Availability predicates
     32  *
     33  *    A series of small functions that check whether the current shader
     34  *    supports the version/extensions required to expose a built-in.
     35  *
     36  * 2. Core builtin_builder class functionality
     37  *
     38  * 3. Lists of built-in functions
     39  *
     40  *    The builtin_builder::create_builtins() function contains lists of all
     41  *    built-in function signatures, where they're available, what types they
     42  *    take, and so on.
     43  *
     44  * 4. Implementations of built-in function signatures
     45  *
     46  *    A series of functions which create ir_function_signatures and emit IR
     47  *    via ir_builder to implement them.
     48  *
     49  * 5. External API
     50  *
     51  *    A few functions the rest of the compiler can use to interact with the
     52  *    built-in function module.  For example, searching for a built-in by
     53  *    name and parameters.
     54  */
     55 
     56 
     57 /**
     58  * Unfortunately, some versions of MinGW produce bad code if this file
     59  * is compiled with -O2 or -O3.  The resulting driver will crash in random
     60  * places if the app uses GLSL.
     61  * The work-around is to disable optimizations for just this file.  Luckily,
     62  * this code is basically just executed once.
     63  *
     64  * MinGW 4.6.3 (in Ubuntu 13.10) does not have this bug.
     65  * MinGW 5.3.1 (in Ubuntu 16.04) definitely has this bug.
     66  * MinGW 6.2.0 (in Ubuntu 16.10) definitely has this bug.
     67  * MinGW x.y.z - don't know.  Assume versions after 4.6.x are buggy
     68  */
     69 
     70 #if defined(__MINGW32__) && ((__GNUC__ * 100) + __GNUC_MINOR >= 407)
     71 #warning "disabling optimizations for this file to work around compiler bug"
     72 #pragma GCC optimize("O1")
     73 #endif
     74 
     75 
     76 #include <stdarg.h>
     77 #include <stdio.h>
     78 #include "main/core.h" /* for struct gl_shader */
     79 #include "main/shaderobj.h"
     80 #include "ir_builder.h"
     81 #include "glsl_parser_extras.h"
     82 #include "program/prog_instruction.h"
     83 #include <math.h>
     84 #include "builtin_functions.h"
     85 #include "util/hash_table.h"
     86 
     87 #define M_PIf   ((float) M_PI)
     88 #define M_PI_2f ((float) M_PI_2)
     89 #define M_PI_4f ((float) M_PI_4)
     90 
     91 using namespace ir_builder;
     92 
     93 /**
     94  * Availability predicates:
     95  *  @{
     96  */
     97 static bool
     98 always_available(const _mesa_glsl_parse_state *)
     99 {
    100    return true;
    101 }
    102 
    103 static bool
    104 compatibility_vs_only(const _mesa_glsl_parse_state *state)
    105 {
    106    return state->stage == MESA_SHADER_VERTEX &&
    107           state->language_version <= 130 &&
    108           !state->es_shader;
    109 }
    110 
    111 static bool
    112 fs_only(const _mesa_glsl_parse_state *state)
    113 {
    114    return state->stage == MESA_SHADER_FRAGMENT;
    115 }
    116 
    117 static bool
    118 gs_only(const _mesa_glsl_parse_state *state)
    119 {
    120    return state->stage == MESA_SHADER_GEOMETRY;
    121 }
    122 
    123 static bool
    124 v110(const _mesa_glsl_parse_state *state)
    125 {
    126    return !state->es_shader;
    127 }
    128 
    129 static bool
    130 v110_fs_only(const _mesa_glsl_parse_state *state)
    131 {
    132    return !state->es_shader && state->stage == MESA_SHADER_FRAGMENT;
    133 }
    134 
    135 static bool
    136 v120(const _mesa_glsl_parse_state *state)
    137 {
    138    return state->is_version(120, 300);
    139 }
    140 
    141 static bool
    142 v130(const _mesa_glsl_parse_state *state)
    143 {
    144    return state->is_version(130, 300);
    145 }
    146 
    147 static bool
    148 v130_desktop(const _mesa_glsl_parse_state *state)
    149 {
    150    return state->is_version(130, 0);
    151 }
    152 
    153 static bool
    154 v460_desktop(const _mesa_glsl_parse_state *state)
    155 {
    156    return state->is_version(460, 0);
    157 }
    158 
    159 static bool
    160 v130_fs_only(const _mesa_glsl_parse_state *state)
    161 {
    162    return state->is_version(130, 300) &&
    163           state->stage == MESA_SHADER_FRAGMENT;
    164 }
    165 
    166 static bool
    167 v140_or_es3(const _mesa_glsl_parse_state *state)
    168 {
    169    return state->is_version(140, 300);
    170 }
    171 
    172 static bool
    173 v400_fs_only(const _mesa_glsl_parse_state *state)
    174 {
    175    return state->is_version(400, 0) &&
    176           state->stage == MESA_SHADER_FRAGMENT;
    177 }
    178 
    179 static bool
    180 texture_rectangle(const _mesa_glsl_parse_state *state)
    181 {
    182    return state->ARB_texture_rectangle_enable;
    183 }
    184 
    185 static bool
    186 texture_external(const _mesa_glsl_parse_state *state)
    187 {
    188    return state->OES_EGL_image_external_enable;
    189 }
    190 
    191 /** True if texturing functions with explicit LOD are allowed. */
    192 static bool
    193 lod_exists_in_stage(const _mesa_glsl_parse_state *state)
    194 {
    195    /* Texturing functions with "Lod" in their name exist:
    196     * - In the vertex shader stage (for all languages)
    197     * - In any stage for GLSL 1.30+ or GLSL ES 3.00
    198     * - In any stage for desktop GLSL with ARB_shader_texture_lod enabled.
    199     *
    200     * Since ARB_shader_texture_lod can only be enabled on desktop GLSL, we
    201     * don't need to explicitly check state->es_shader.
    202     */
    203    return state->stage == MESA_SHADER_VERTEX ||
    204           state->is_version(130, 300) ||
    205           state->ARB_shader_texture_lod_enable;
    206 }
    207 
    208 static bool
    209 v110_lod(const _mesa_glsl_parse_state *state)
    210 {
    211    return !state->es_shader && lod_exists_in_stage(state);
    212 }
    213 
    214 static bool
    215 texture_buffer(const _mesa_glsl_parse_state *state)
    216 {
    217    return state->is_version(140, 320) ||
    218       state->EXT_texture_buffer_enable ||
    219       state->OES_texture_buffer_enable;
    220 }
    221 
    222 static bool
    223 shader_texture_lod(const _mesa_glsl_parse_state *state)
    224 {
    225    return state->ARB_shader_texture_lod_enable;
    226 }
    227 
    228 static bool
    229 shader_texture_lod_and_rect(const _mesa_glsl_parse_state *state)
    230 {
    231    return state->ARB_shader_texture_lod_enable &&
    232           state->ARB_texture_rectangle_enable;
    233 }
    234 
    235 static bool
    236 shader_bit_encoding(const _mesa_glsl_parse_state *state)
    237 {
    238    return state->is_version(330, 300) ||
    239           state->ARB_shader_bit_encoding_enable ||
    240           state->ARB_gpu_shader5_enable;
    241 }
    242 
    243 static bool
    244 shader_integer_mix(const _mesa_glsl_parse_state *state)
    245 {
    246    return state->is_version(450, 310) ||
    247           state->ARB_ES3_1_compatibility_enable ||
    248           (v130(state) && state->EXT_shader_integer_mix_enable);
    249 }
    250 
    251 static bool
    252 shader_packing_or_es3(const _mesa_glsl_parse_state *state)
    253 {
    254    return state->ARB_shading_language_packing_enable ||
    255           state->is_version(420, 300);
    256 }
    257 
    258 static bool
    259 shader_packing_or_es3_or_gpu_shader5(const _mesa_glsl_parse_state *state)
    260 {
    261    return state->ARB_shading_language_packing_enable ||
    262           state->ARB_gpu_shader5_enable ||
    263           state->is_version(400, 300);
    264 }
    265 
    266 static bool
    267 gpu_shader5(const _mesa_glsl_parse_state *state)
    268 {
    269    return state->is_version(400, 0) || state->ARB_gpu_shader5_enable;
    270 }
    271 
    272 static bool
    273 gpu_shader5_es(const _mesa_glsl_parse_state *state)
    274 {
    275    return state->is_version(400, 320) ||
    276           state->ARB_gpu_shader5_enable ||
    277           state->EXT_gpu_shader5_enable ||
    278           state->OES_gpu_shader5_enable;
    279 }
    280 
    281 static bool
    282 gpu_shader5_or_OES_texture_cube_map_array(const _mesa_glsl_parse_state *state)
    283 {
    284    return state->is_version(400, 320) ||
    285           state->ARB_gpu_shader5_enable ||
    286           state->EXT_texture_cube_map_array_enable ||
    287           state->OES_texture_cube_map_array_enable;
    288 }
    289 
    290 static bool
    291 es31_not_gs5(const _mesa_glsl_parse_state *state)
    292 {
    293    return state->is_version(0, 310) && !gpu_shader5_es(state);
    294 }
    295 
    296 static bool
    297 gpu_shader5_or_es31(const _mesa_glsl_parse_state *state)
    298 {
    299    return state->is_version(400, 310) || state->ARB_gpu_shader5_enable;
    300 }
    301 
    302 static bool
    303 shader_packing_or_es31_or_gpu_shader5(const _mesa_glsl_parse_state *state)
    304 {
    305    return state->ARB_shading_language_packing_enable ||
    306           state->ARB_gpu_shader5_enable ||
    307           state->is_version(400, 310);
    308 }
    309 
    310 static bool
    311 gpu_shader5_or_es31_or_integer_functions(const _mesa_glsl_parse_state *state)
    312 {
    313    return gpu_shader5_or_es31(state) ||
    314           state->MESA_shader_integer_functions_enable;
    315 }
    316 
    317 static bool
    318 fs_interpolate_at(const _mesa_glsl_parse_state *state)
    319 {
    320    return state->stage == MESA_SHADER_FRAGMENT &&
    321           (state->is_version(400, 320) ||
    322            state->ARB_gpu_shader5_enable ||
    323            state->OES_shader_multisample_interpolation_enable);
    324 }
    325 
    326 
    327 static bool
    328 texture_array_lod(const _mesa_glsl_parse_state *state)
    329 {
    330    return lod_exists_in_stage(state) &&
    331           state->EXT_texture_array_enable;
    332 }
    333 
    334 static bool
    335 fs_texture_array(const _mesa_glsl_parse_state *state)
    336 {
    337    return state->stage == MESA_SHADER_FRAGMENT &&
    338           state->EXT_texture_array_enable;
    339 }
    340 
    341 static bool
    342 texture_array(const _mesa_glsl_parse_state *state)
    343 {
    344    return state->EXT_texture_array_enable;
    345 }
    346 
    347 static bool
    348 texture_multisample(const _mesa_glsl_parse_state *state)
    349 {
    350    return state->is_version(150, 310) ||
    351           state->ARB_texture_multisample_enable;
    352 }
    353 
    354 static bool
    355 texture_multisample_array(const _mesa_glsl_parse_state *state)
    356 {
    357    return state->is_version(150, 320) ||
    358           state->ARB_texture_multisample_enable ||
    359           state->OES_texture_storage_multisample_2d_array_enable;
    360 }
    361 
    362 static bool
    363 texture_samples_identical(const _mesa_glsl_parse_state *state)
    364 {
    365    return texture_multisample(state) &&
    366           state->EXT_shader_samples_identical_enable;
    367 }
    368 
    369 static bool
    370 texture_samples_identical_array(const _mesa_glsl_parse_state *state)
    371 {
    372    return texture_multisample_array(state) &&
    373           state->EXT_shader_samples_identical_enable;
    374 }
    375 
    376 static bool
    377 fs_texture_cube_map_array(const _mesa_glsl_parse_state *state)
    378 {
    379    return state->stage == MESA_SHADER_FRAGMENT &&
    380           state->has_texture_cube_map_array();
    381 }
    382 
    383 static bool
    384 texture_cube_map_array(const _mesa_glsl_parse_state *state)
    385 {
    386    return state->has_texture_cube_map_array();
    387 }
    388 
    389 static bool
    390 texture_query_levels(const _mesa_glsl_parse_state *state)
    391 {
    392    return state->is_version(430, 0) ||
    393           state->ARB_texture_query_levels_enable;
    394 }
    395 
    396 static bool
    397 texture_query_lod(const _mesa_glsl_parse_state *state)
    398 {
    399    return state->stage == MESA_SHADER_FRAGMENT &&
    400           state->ARB_texture_query_lod_enable;
    401 }
    402 
    403 static bool
    404 texture_gather_cube_map_array(const _mesa_glsl_parse_state *state)
    405 {
    406    return state->is_version(400, 320) ||
    407           state->ARB_texture_gather_enable ||
    408           state->ARB_gpu_shader5_enable ||
    409           state->EXT_texture_cube_map_array_enable ||
    410           state->OES_texture_cube_map_array_enable;
    411 }
    412 
    413 static bool
    414 texture_gather_or_es31(const _mesa_glsl_parse_state *state)
    415 {
    416    return state->is_version(400, 310) ||
    417           state->ARB_texture_gather_enable ||
    418           state->ARB_gpu_shader5_enable;
    419 }
    420 
    421 /* Only ARB_texture_gather but not GLSL 4.0 or ARB_gpu_shader5.
    422  * used for relaxation of const offset requirements.
    423  */
    424 static bool
    425 texture_gather_only_or_es31(const _mesa_glsl_parse_state *state)
    426 {
    427    return !state->is_version(400, 320) &&
    428           !state->ARB_gpu_shader5_enable &&
    429           !state->EXT_gpu_shader5_enable &&
    430           !state->OES_gpu_shader5_enable &&
    431           (state->ARB_texture_gather_enable ||
    432            state->is_version(0, 310));
    433 }
    434 
    435 /* Desktop GL or OES_standard_derivatives + fragment shader only */
    436 static bool
    437 fs_oes_derivatives(const _mesa_glsl_parse_state *state)
    438 {
    439    return state->stage == MESA_SHADER_FRAGMENT &&
    440           (state->is_version(110, 300) ||
    441            state->OES_standard_derivatives_enable);
    442 }
    443 
    444 static bool
    445 fs_derivative_control(const _mesa_glsl_parse_state *state)
    446 {
    447    return state->stage == MESA_SHADER_FRAGMENT &&
    448           (state->is_version(450, 0) ||
    449            state->ARB_derivative_control_enable);
    450 }
    451 
    452 static bool
    453 tex1d_lod(const _mesa_glsl_parse_state *state)
    454 {
    455    return !state->es_shader && lod_exists_in_stage(state);
    456 }
    457 
    458 /** True if sampler3D exists */
    459 static bool
    460 tex3d(const _mesa_glsl_parse_state *state)
    461 {
    462    /* sampler3D exists in all desktop GLSL versions, GLSL ES 1.00 with the
    463     * OES_texture_3D extension, and in GLSL ES 3.00.
    464     */
    465    return !state->es_shader ||
    466           state->OES_texture_3D_enable ||
    467           state->language_version >= 300;
    468 }
    469 
    470 static bool
    471 fs_tex3d(const _mesa_glsl_parse_state *state)
    472 {
    473    return state->stage == MESA_SHADER_FRAGMENT &&
    474           (!state->es_shader || state->OES_texture_3D_enable);
    475 }
    476 
    477 static bool
    478 tex3d_lod(const _mesa_glsl_parse_state *state)
    479 {
    480    return tex3d(state) && lod_exists_in_stage(state);
    481 }
    482 
    483 static bool
    484 shader_atomic_counters(const _mesa_glsl_parse_state *state)
    485 {
    486    return state->has_atomic_counters();
    487 }
    488 
    489 static bool
    490 shader_atomic_counter_ops(const _mesa_glsl_parse_state *state)
    491 {
    492    return state->ARB_shader_atomic_counter_ops_enable;
    493 }
    494 
    495 static bool
    496 shader_atomic_counter_ops_or_v460_desktop(const _mesa_glsl_parse_state *state)
    497 {
    498    return state->ARB_shader_atomic_counter_ops_enable || v460_desktop(state);
    499 }
    500 
    501 static bool
    502 shader_ballot(const _mesa_glsl_parse_state *state)
    503 {
    504    return state->ARB_shader_ballot_enable;
    505 }
    506 
    507 static bool
    508 shader_clock(const _mesa_glsl_parse_state *state)
    509 {
    510    return state->ARB_shader_clock_enable;
    511 }
    512 
    513 static bool
    514 shader_clock_int64(const _mesa_glsl_parse_state *state)
    515 {
    516    return state->ARB_shader_clock_enable &&
    517           state->ARB_gpu_shader_int64_enable;
    518 }
    519 
    520 static bool
    521 shader_storage_buffer_object(const _mesa_glsl_parse_state *state)
    522 {
    523    return state->has_shader_storage_buffer_objects();
    524 }
    525 
    526 static bool
    527 shader_trinary_minmax(const _mesa_glsl_parse_state *state)
    528 {
    529    return state->AMD_shader_trinary_minmax_enable;
    530 }
    531 
    532 static bool
    533 shader_image_load_store(const _mesa_glsl_parse_state *state)
    534 {
    535    return (state->is_version(420, 310) ||
    536            state->ARB_shader_image_load_store_enable);
    537 }
    538 
    539 static bool
    540 shader_image_atomic(const _mesa_glsl_parse_state *state)
    541 {
    542    return (state->is_version(420, 320) ||
    543            state->ARB_shader_image_load_store_enable ||
    544            state->OES_shader_image_atomic_enable);
    545 }
    546 
    547 static bool
    548 shader_image_atomic_exchange_float(const _mesa_glsl_parse_state *state)
    549 {
    550    return (state->is_version(450, 320) ||
    551            state->ARB_ES3_1_compatibility_enable ||
    552            state->OES_shader_image_atomic_enable);
    553 }
    554 
    555 static bool
    556 shader_image_size(const _mesa_glsl_parse_state *state)
    557 {
    558    return state->is_version(430, 310) ||
    559            state->ARB_shader_image_size_enable;
    560 }
    561 
    562 static bool
    563 shader_samples(const _mesa_glsl_parse_state *state)
    564 {
    565    return state->is_version(450, 0) ||
    566           state->ARB_shader_texture_image_samples_enable;
    567 }
    568 
    569 static bool
    570 gs_streams(const _mesa_glsl_parse_state *state)
    571 {
    572    return gpu_shader5(state) && gs_only(state);
    573 }
    574 
    575 static bool
    576 fp64(const _mesa_glsl_parse_state *state)
    577 {
    578    return state->has_double();
    579 }
    580 
    581 static bool
    582 int64(const _mesa_glsl_parse_state *state)
    583 {
    584    return state->has_int64();
    585 }
    586 
    587 static bool
    588 int64_fp64(const _mesa_glsl_parse_state *state)
    589 {
    590    return state->has_int64() && state->has_double();
    591 }
    592 
    593 static bool
    594 compute_shader(const _mesa_glsl_parse_state *state)
    595 {
    596    return state->stage == MESA_SHADER_COMPUTE;
    597 }
    598 
    599 static bool
    600 compute_shader_supported(const _mesa_glsl_parse_state *state)
    601 {
    602    return state->has_compute_shader();
    603 }
    604 
    605 static bool
    606 buffer_atomics_supported(const _mesa_glsl_parse_state *state)
    607 {
    608    return compute_shader(state) || shader_storage_buffer_object(state);
    609 }
    610 
    611 static bool
    612 barrier_supported(const _mesa_glsl_parse_state *state)
    613 {
    614    return compute_shader(state) ||
    615           state->stage == MESA_SHADER_TESS_CTRL;
    616 }
    617 
    618 static bool
    619 vote(const _mesa_glsl_parse_state *state)
    620 {
    621    return state->ARB_shader_group_vote_enable;
    622 }
    623 
    624 static bool
    625 vote_or_v460_desktop(const _mesa_glsl_parse_state *state)
    626 {
    627    return state->ARB_shader_group_vote_enable || v460_desktop(state);
    628 }
    629 
    630 static bool
    631 integer_functions_supported(const _mesa_glsl_parse_state *state)
    632 {
    633    return state->extensions->MESA_shader_integer_functions;
    634 }
    635 /** @} */
    636 
    637 /******************************************************************************/
    638 
    639 namespace {
    640 
    641 /**
    642  * builtin_builder: A singleton object representing the core of the built-in
    643  * function module.
    644  *
    645  * It generates IR for every built-in function signature, and organizes them
    646  * into functions.
    647  */
    648 class builtin_builder {
    649 public:
    650    builtin_builder();
    651    ~builtin_builder();
    652 
    653    void initialize();
    654    void release();
    655    ir_function_signature *find(_mesa_glsl_parse_state *state,
    656                                const char *name, exec_list *actual_parameters);
    657 
    658    /**
    659     * A shader to hold all the built-in signatures; created by this module.
    660     *
    661     * This includes signatures for every built-in, regardless of version or
    662     * enabled extensions.  The availability predicate associated with each
    663     * signature allows matching_signature() to filter out the irrelevant ones.
    664     */
    665    gl_shader *shader;
    666 
    667 private:
    668    void *mem_ctx;
    669 
    670    void create_shader();
    671    void create_intrinsics();
    672    void create_builtins();
    673 
    674    /**
    675     * IR builder helpers:
    676     *
    677     * These convenience functions assist in emitting IR, but don't necessarily
    678     * fit in ir_builder itself.  Many of them rely on having a mem_ctx class
    679     * member available.
    680     */
    681    ir_variable *in_var(const glsl_type *type, const char *name);
    682    ir_variable *out_var(const glsl_type *type, const char *name);
    683    ir_constant *imm(float f, unsigned vector_elements=1);
    684    ir_constant *imm(bool b, unsigned vector_elements=1);
    685    ir_constant *imm(int i, unsigned vector_elements=1);
    686    ir_constant *imm(unsigned u, unsigned vector_elements=1);
    687    ir_constant *imm(double d, unsigned vector_elements=1);
    688    ir_constant *imm(const glsl_type *type, const ir_constant_data &);
    689    ir_dereference_variable *var_ref(ir_variable *var);
    690    ir_dereference_array *array_ref(ir_variable *var, int i);
    691    ir_swizzle *matrix_elt(ir_variable *var, int col, int row);
    692 
    693    ir_expression *asin_expr(ir_variable *x, float p0, float p1);
    694    void do_atan(ir_factory &body, const glsl_type *type, ir_variable *res, operand y_over_x);
    695 
    696    /**
    697     * Call function \param f with parameters specified as the linked
    698     * list \param params of \c ir_variable objects.  \param ret should
    699     * point to the ir_variable that will hold the function return
    700     * value, or be \c NULL if the function has void return type.
    701     */
    702    ir_call *call(ir_function *f, ir_variable *ret, exec_list params);
    703 
    704    /** Create a new function and add the given signatures. */
    705    void add_function(const char *name, ...);
    706 
    707    typedef ir_function_signature *(builtin_builder::*image_prototype_ctr)(const glsl_type *image_type,
    708                                                                           unsigned num_arguments,
    709                                                                           unsigned flags);
    710 
    711    /**
    712     * Create a new image built-in function for all known image types.
    713     * \p flags is a bitfield of \c image_function_flags flags.
    714     */
    715    void add_image_function(const char *name,
    716                            const char *intrinsic_name,
    717                            image_prototype_ctr prototype,
    718                            unsigned num_arguments,
    719                            unsigned flags,
    720                            enum ir_intrinsic_id id);
    721 
    722    /**
    723     * Create new functions for all known image built-ins and types.
    724     * If \p glsl is \c true, use the GLSL built-in names and emit code
    725     * to call into the actual compiler intrinsic.  If \p glsl is
    726     * false, emit a function prototype with no body for each image
    727     * intrinsic name.
    728     */
    729    void add_image_functions(bool glsl);
    730 
    731    ir_function_signature *new_sig(const glsl_type *return_type,
    732                                   builtin_available_predicate avail,
    733                                   int num_params, ...);
    734 
    735    /**
    736     * Function signature generators:
    737     *  @{
    738     */
    739    ir_function_signature *unop(builtin_available_predicate avail,
    740                                ir_expression_operation opcode,
    741                                const glsl_type *return_type,
    742                                const glsl_type *param_type);
    743    ir_function_signature *binop(builtin_available_predicate avail,
    744                                 ir_expression_operation opcode,
    745                                 const glsl_type *return_type,
    746                                 const glsl_type *param0_type,
    747                                 const glsl_type *param1_type,
    748                                 bool swap_operands = false);
    749 
    750 #define B0(X) ir_function_signature *_##X();
    751 #define B1(X) ir_function_signature *_##X(const glsl_type *);
    752 #define B2(X) ir_function_signature *_##X(const glsl_type *, const glsl_type *);
    753 #define B3(X) ir_function_signature *_##X(const glsl_type *, const glsl_type *, const glsl_type *);
    754 #define BA1(X) ir_function_signature *_##X(builtin_available_predicate, const glsl_type *);
    755 #define BA2(X) ir_function_signature *_##X(builtin_available_predicate, const glsl_type *, const glsl_type *);
    756    B1(radians)
    757    B1(degrees)
    758    B1(sin)
    759    B1(cos)
    760    B1(tan)
    761    B1(asin)
    762    B1(acos)
    763    B1(atan2)
    764    B1(atan)
    765    B1(sinh)
    766    B1(cosh)
    767    B1(tanh)
    768    B1(asinh)
    769    B1(acosh)
    770    B1(atanh)
    771    B1(pow)
    772    B1(exp)
    773    B1(log)
    774    B1(exp2)
    775    B1(log2)
    776    BA1(sqrt)
    777    BA1(inversesqrt)
    778    BA1(abs)
    779    BA1(sign)
    780    BA1(floor)
    781    BA1(trunc)
    782    BA1(round)
    783    BA1(roundEven)
    784    BA1(ceil)
    785    BA1(fract)
    786    BA2(mod)
    787    BA1(modf)
    788    BA2(min)
    789    BA2(max)
    790    BA2(clamp)
    791    BA2(mix_lrp)
    792    ir_function_signature *_mix_sel(builtin_available_predicate avail,
    793                                    const glsl_type *val_type,
    794                                    const glsl_type *blend_type);
    795    BA2(step)
    796    BA2(smoothstep)
    797    BA1(isnan)
    798    BA1(isinf)
    799    B1(floatBitsToInt)
    800    B1(floatBitsToUint)
    801    B1(intBitsToFloat)
    802    B1(uintBitsToFloat)
    803 
    804    BA1(doubleBitsToInt64)
    805    BA1(doubleBitsToUint64)
    806    BA1(int64BitsToDouble)
    807    BA1(uint64BitsToDouble)
    808 
    809    ir_function_signature *_packUnorm2x16(builtin_available_predicate avail);
    810    ir_function_signature *_packSnorm2x16(builtin_available_predicate avail);
    811    ir_function_signature *_packUnorm4x8(builtin_available_predicate avail);
    812    ir_function_signature *_packSnorm4x8(builtin_available_predicate avail);
    813    ir_function_signature *_unpackUnorm2x16(builtin_available_predicate avail);
    814    ir_function_signature *_unpackSnorm2x16(builtin_available_predicate avail);
    815    ir_function_signature *_unpackUnorm4x8(builtin_available_predicate avail);
    816    ir_function_signature *_unpackSnorm4x8(builtin_available_predicate avail);
    817    ir_function_signature *_packHalf2x16(builtin_available_predicate avail);
    818    ir_function_signature *_unpackHalf2x16(builtin_available_predicate avail);
    819    ir_function_signature *_packDouble2x32(builtin_available_predicate avail);
    820    ir_function_signature *_unpackDouble2x32(builtin_available_predicate avail);
    821    ir_function_signature *_packInt2x32(builtin_available_predicate avail);
    822    ir_function_signature *_unpackInt2x32(builtin_available_predicate avail);
    823    ir_function_signature *_packUint2x32(builtin_available_predicate avail);
    824    ir_function_signature *_unpackUint2x32(builtin_available_predicate avail);
    825 
    826    BA1(length)
    827    BA1(distance);
    828    BA1(dot);
    829    BA1(cross);
    830    BA1(normalize);
    831    B0(ftransform);
    832    BA1(faceforward);
    833    BA1(reflect);
    834    BA1(refract);
    835    BA1(matrixCompMult);
    836    BA1(outerProduct);
    837    BA1(determinant_mat2);
    838    BA1(determinant_mat3);
    839    BA1(determinant_mat4);
    840    BA1(inverse_mat2);
    841    BA1(inverse_mat3);
    842    BA1(inverse_mat4);
    843    BA1(transpose);
    844    BA1(lessThan);
    845    BA1(lessThanEqual);
    846    BA1(greaterThan);
    847    BA1(greaterThanEqual);
    848    BA1(equal);
    849    BA1(notEqual);
    850    B1(any);
    851    B1(all);
    852    B1(not);
    853    BA2(textureSize);
    854    BA1(textureSamples);
    855 
    856 /** Flags to _texture() */
    857 #define TEX_PROJECT 1
    858 #define TEX_OFFSET  2
    859 #define TEX_COMPONENT 4
    860 #define TEX_OFFSET_NONCONST 8
    861 #define TEX_OFFSET_ARRAY 16
    862 
    863    ir_function_signature *_texture(ir_texture_opcode opcode,
    864                                    builtin_available_predicate avail,
    865                                    const glsl_type *return_type,
    866                                    const glsl_type *sampler_type,
    867                                    const glsl_type *coord_type,
    868                                    int flags = 0);
    869    BA1(textureCubeArrayShadow);
    870    ir_function_signature *_texelFetch(builtin_available_predicate avail,
    871                                       const glsl_type *return_type,
    872                                       const glsl_type *sampler_type,
    873                                       const glsl_type *coord_type,
    874                                       const glsl_type *offset_type = NULL);
    875 
    876    B0(EmitVertex)
    877    B0(EndPrimitive)
    878    ir_function_signature *_EmitStreamVertex(builtin_available_predicate avail,
    879                                             const glsl_type *stream_type);
    880    ir_function_signature *_EndStreamPrimitive(builtin_available_predicate avail,
    881                                               const glsl_type *stream_type);
    882    B0(barrier)
    883 
    884    BA2(textureQueryLod);
    885    BA1(textureQueryLevels);
    886    BA2(textureSamplesIdentical);
    887    B1(dFdx);
    888    B1(dFdy);
    889    B1(fwidth);
    890    B1(dFdxCoarse);
    891    B1(dFdyCoarse);
    892    B1(fwidthCoarse);
    893    B1(dFdxFine);
    894    B1(dFdyFine);
    895    B1(fwidthFine);
    896    B1(noise1);
    897    B1(noise2);
    898    B1(noise3);
    899    B1(noise4);
    900 
    901    B1(bitfieldExtract)
    902    B1(bitfieldInsert)
    903    B1(bitfieldReverse)
    904    B1(bitCount)
    905    B1(findLSB)
    906    B1(findMSB)
    907    BA1(fma)
    908    B2(ldexp)
    909    B2(frexp)
    910    B2(dfrexp)
    911    B1(uaddCarry)
    912    B1(usubBorrow)
    913    B1(mulExtended)
    914    B1(interpolateAtCentroid)
    915    B1(interpolateAtOffset)
    916    B1(interpolateAtSample)
    917 
    918    ir_function_signature *_atomic_counter_intrinsic(builtin_available_predicate avail,
    919                                                     enum ir_intrinsic_id id);
    920    ir_function_signature *_atomic_counter_intrinsic1(builtin_available_predicate avail,
    921                                                      enum ir_intrinsic_id id);
    922    ir_function_signature *_atomic_counter_intrinsic2(builtin_available_predicate avail,
    923                                                      enum ir_intrinsic_id id);
    924    ir_function_signature *_atomic_counter_op(const char *intrinsic,
    925                                              builtin_available_predicate avail);
    926    ir_function_signature *_atomic_counter_op1(const char *intrinsic,
    927                                               builtin_available_predicate avail);
    928    ir_function_signature *_atomic_counter_op2(const char *intrinsic,
    929                                               builtin_available_predicate avail);
    930 
    931    ir_function_signature *_atomic_intrinsic2(builtin_available_predicate avail,
    932                                              const glsl_type *type,
    933                                              enum ir_intrinsic_id id);
    934    ir_function_signature *_atomic_op2(const char *intrinsic,
    935                                       builtin_available_predicate avail,
    936                                       const glsl_type *type);
    937    ir_function_signature *_atomic_intrinsic3(builtin_available_predicate avail,
    938                                              const glsl_type *type,
    939                                              enum ir_intrinsic_id id);
    940    ir_function_signature *_atomic_op3(const char *intrinsic,
    941                                       builtin_available_predicate avail,
    942                                       const glsl_type *type);
    943 
    944    B1(min3)
    945    B1(max3)
    946    B1(mid3)
    947 
    948    ir_function_signature *_image_prototype(const glsl_type *image_type,
    949                                            unsigned num_arguments,
    950                                            unsigned flags);
    951    ir_function_signature *_image_size_prototype(const glsl_type *image_type,
    952                                                 unsigned num_arguments,
    953                                                 unsigned flags);
    954    ir_function_signature *_image_samples_prototype(const glsl_type *image_type,
    955                                                    unsigned num_arguments,
    956                                                    unsigned flags);
    957    ir_function_signature *_image(image_prototype_ctr prototype,
    958                                  const glsl_type *image_type,
    959                                  const char *intrinsic_name,
    960                                  unsigned num_arguments,
    961                                  unsigned flags,
    962                                  enum ir_intrinsic_id id);
    963 
    964    ir_function_signature *_memory_barrier_intrinsic(
    965       builtin_available_predicate avail,
    966       enum ir_intrinsic_id id);
    967    ir_function_signature *_memory_barrier(const char *intrinsic_name,
    968                                           builtin_available_predicate avail);
    969 
    970    ir_function_signature *_ballot_intrinsic();
    971    ir_function_signature *_ballot();
    972    ir_function_signature *_read_first_invocation_intrinsic(const glsl_type *type);
    973    ir_function_signature *_read_first_invocation(const glsl_type *type);
    974    ir_function_signature *_read_invocation_intrinsic(const glsl_type *type);
    975    ir_function_signature *_read_invocation(const glsl_type *type);
    976 
    977    ir_function_signature *_shader_clock_intrinsic(builtin_available_predicate avail,
    978                                                   const glsl_type *type);
    979    ir_function_signature *_shader_clock(builtin_available_predicate avail,
    980                                         const glsl_type *type);
    981 
    982    ir_function_signature *_vote_intrinsic(builtin_available_predicate avail,
    983                                           enum ir_intrinsic_id id);
    984    ir_function_signature *_vote(const char *intrinsic_name,
    985                                 builtin_available_predicate avail);
    986 
    987 #undef B0
    988 #undef B1
    989 #undef B2
    990 #undef B3
    991 #undef BA1
    992 #undef BA2
    993    /** @} */
    994 };
    995 
    996 enum image_function_flags {
    997    IMAGE_FUNCTION_EMIT_STUB = (1 << 0),
    998    IMAGE_FUNCTION_RETURNS_VOID = (1 << 1),
    999    IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE = (1 << 2),
   1000    IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE = (1 << 3),
   1001    IMAGE_FUNCTION_READ_ONLY = (1 << 4),
   1002    IMAGE_FUNCTION_WRITE_ONLY = (1 << 5),
   1003    IMAGE_FUNCTION_AVAIL_ATOMIC = (1 << 6),
   1004    IMAGE_FUNCTION_MS_ONLY = (1 << 7),
   1005    IMAGE_FUNCTION_AVAIL_ATOMIC_EXCHANGE = (1 << 8)
   1006 };
   1007 
   1008 } /* anonymous namespace */
   1009 
   1010 /**
   1011  * Core builtin_builder functionality:
   1012  *  @{
   1013  */
   1014 builtin_builder::builtin_builder()
   1015    : shader(NULL)
   1016 {
   1017    mem_ctx = NULL;
   1018 }
   1019 
   1020 builtin_builder::~builtin_builder()
   1021 {
   1022    ralloc_free(mem_ctx);
   1023 }
   1024 
   1025 ir_function_signature *
   1026 builtin_builder::find(_mesa_glsl_parse_state *state,
   1027                       const char *name, exec_list *actual_parameters)
   1028 {
   1029    /* The shader currently being compiled requested a built-in function;
   1030     * it needs to link against builtin_builder::shader in order to get them.
   1031     *
   1032     * Even if we don't find a matching signature, we still need to do this so
   1033     * that the "no matching signature" error will list potential candidates
   1034     * from the available built-ins.
   1035     */
   1036    state->uses_builtin_functions = true;
   1037 
   1038    ir_function *f = shader->symbols->get_function(name);
   1039    if (f == NULL)
   1040       return NULL;
   1041 
   1042    ir_function_signature *sig =
   1043       f->matching_signature(state, actual_parameters, true);
   1044    if (sig == NULL)
   1045       return NULL;
   1046 
   1047    return sig;
   1048 }
   1049 
   1050 void
   1051 builtin_builder::initialize()
   1052 {
   1053    /* If already initialized, don't do it again. */
   1054    if (mem_ctx != NULL)
   1055       return;
   1056 
   1057    mem_ctx = ralloc_context(NULL);
   1058    create_shader();
   1059    create_intrinsics();
   1060    create_builtins();
   1061 }
   1062 
   1063 void
   1064 builtin_builder::release()
   1065 {
   1066    ralloc_free(mem_ctx);
   1067    mem_ctx = NULL;
   1068 
   1069    ralloc_free(shader);
   1070    shader = NULL;
   1071 }
   1072 
   1073 void
   1074 builtin_builder::create_shader()
   1075 {
   1076    /* The target doesn't actually matter.  There's no target for generic
   1077     * GLSL utility code that could be linked against any stage, so just
   1078     * arbitrarily pick GL_VERTEX_SHADER.
   1079     */
   1080    shader = _mesa_new_shader(0, MESA_SHADER_VERTEX);
   1081    shader->symbols = new(mem_ctx) glsl_symbol_table;
   1082 }
   1083 
   1084 /** @} */
   1085 
   1086 /**
   1087  * Create ir_function and ir_function_signature objects for each
   1088  * intrinsic.
   1089  */
   1090 void
   1091 builtin_builder::create_intrinsics()
   1092 {
   1093    add_function("__intrinsic_atomic_read",
   1094                 _atomic_counter_intrinsic(shader_atomic_counters,
   1095                                           ir_intrinsic_atomic_counter_read),
   1096                 NULL);
   1097    add_function("__intrinsic_atomic_increment",
   1098                 _atomic_counter_intrinsic(shader_atomic_counters,
   1099                                           ir_intrinsic_atomic_counter_increment),
   1100                 NULL);
   1101    add_function("__intrinsic_atomic_predecrement",
   1102                 _atomic_counter_intrinsic(shader_atomic_counters,
   1103                                           ir_intrinsic_atomic_counter_predecrement),
   1104                 NULL);
   1105 
   1106    add_function("__intrinsic_atomic_add",
   1107                 _atomic_intrinsic2(buffer_atomics_supported,
   1108                                    glsl_type::uint_type,
   1109                                    ir_intrinsic_generic_atomic_add),
   1110                 _atomic_intrinsic2(buffer_atomics_supported,
   1111                                    glsl_type::int_type,
   1112                                    ir_intrinsic_generic_atomic_add),
   1113                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1114                                            ir_intrinsic_atomic_counter_add),
   1115                 NULL);
   1116    add_function("__intrinsic_atomic_min",
   1117                 _atomic_intrinsic2(buffer_atomics_supported,
   1118                                    glsl_type::uint_type,
   1119                                    ir_intrinsic_generic_atomic_min),
   1120                 _atomic_intrinsic2(buffer_atomics_supported,
   1121                                    glsl_type::int_type,
   1122                                    ir_intrinsic_generic_atomic_min),
   1123                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1124                                            ir_intrinsic_atomic_counter_min),
   1125                 NULL);
   1126    add_function("__intrinsic_atomic_max",
   1127                 _atomic_intrinsic2(buffer_atomics_supported,
   1128                                    glsl_type::uint_type,
   1129                                    ir_intrinsic_generic_atomic_max),
   1130                 _atomic_intrinsic2(buffer_atomics_supported,
   1131                                    glsl_type::int_type,
   1132                                    ir_intrinsic_generic_atomic_max),
   1133                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1134                                            ir_intrinsic_atomic_counter_max),
   1135                 NULL);
   1136    add_function("__intrinsic_atomic_and",
   1137                 _atomic_intrinsic2(buffer_atomics_supported,
   1138                                    glsl_type::uint_type,
   1139                                    ir_intrinsic_generic_atomic_and),
   1140                 _atomic_intrinsic2(buffer_atomics_supported,
   1141                                    glsl_type::int_type,
   1142                                    ir_intrinsic_generic_atomic_and),
   1143                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1144                                            ir_intrinsic_atomic_counter_and),
   1145                 NULL);
   1146    add_function("__intrinsic_atomic_or",
   1147                 _atomic_intrinsic2(buffer_atomics_supported,
   1148                                    glsl_type::uint_type,
   1149                                    ir_intrinsic_generic_atomic_or),
   1150                 _atomic_intrinsic2(buffer_atomics_supported,
   1151                                    glsl_type::int_type,
   1152                                    ir_intrinsic_generic_atomic_or),
   1153                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1154                                            ir_intrinsic_atomic_counter_or),
   1155                 NULL);
   1156    add_function("__intrinsic_atomic_xor",
   1157                 _atomic_intrinsic2(buffer_atomics_supported,
   1158                                    glsl_type::uint_type,
   1159                                    ir_intrinsic_generic_atomic_xor),
   1160                 _atomic_intrinsic2(buffer_atomics_supported,
   1161                                    glsl_type::int_type,
   1162                                    ir_intrinsic_generic_atomic_xor),
   1163                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1164                                            ir_intrinsic_atomic_counter_xor),
   1165                 NULL);
   1166    add_function("__intrinsic_atomic_exchange",
   1167                 _atomic_intrinsic2(buffer_atomics_supported,
   1168                                    glsl_type::uint_type,
   1169                                    ir_intrinsic_generic_atomic_exchange),
   1170                 _atomic_intrinsic2(buffer_atomics_supported,
   1171                                    glsl_type::int_type,
   1172                                    ir_intrinsic_generic_atomic_exchange),
   1173                 _atomic_counter_intrinsic1(shader_atomic_counter_ops_or_v460_desktop,
   1174                                            ir_intrinsic_atomic_counter_exchange),
   1175                 NULL);
   1176    add_function("__intrinsic_atomic_comp_swap",
   1177                 _atomic_intrinsic3(buffer_atomics_supported,
   1178                                    glsl_type::uint_type,
   1179                                    ir_intrinsic_generic_atomic_comp_swap),
   1180                 _atomic_intrinsic3(buffer_atomics_supported,
   1181                                    glsl_type::int_type,
   1182                                    ir_intrinsic_generic_atomic_comp_swap),
   1183                 _atomic_counter_intrinsic2(shader_atomic_counter_ops_or_v460_desktop,
   1184                                            ir_intrinsic_atomic_counter_comp_swap),
   1185                 NULL);
   1186 
   1187    add_image_functions(false);
   1188 
   1189    add_function("__intrinsic_memory_barrier",
   1190                 _memory_barrier_intrinsic(shader_image_load_store,
   1191                                           ir_intrinsic_memory_barrier),
   1192                 NULL);
   1193    add_function("__intrinsic_group_memory_barrier",
   1194                 _memory_barrier_intrinsic(compute_shader,
   1195                                           ir_intrinsic_group_memory_barrier),
   1196                 NULL);
   1197    add_function("__intrinsic_memory_barrier_atomic_counter",
   1198                 _memory_barrier_intrinsic(compute_shader_supported,
   1199                                           ir_intrinsic_memory_barrier_atomic_counter),
   1200                 NULL);
   1201    add_function("__intrinsic_memory_barrier_buffer",
   1202                 _memory_barrier_intrinsic(compute_shader_supported,
   1203                                           ir_intrinsic_memory_barrier_buffer),
   1204                 NULL);
   1205    add_function("__intrinsic_memory_barrier_image",
   1206                 _memory_barrier_intrinsic(compute_shader_supported,
   1207                                           ir_intrinsic_memory_barrier_image),
   1208                 NULL);
   1209    add_function("__intrinsic_memory_barrier_shared",
   1210                 _memory_barrier_intrinsic(compute_shader,
   1211                                           ir_intrinsic_memory_barrier_shared),
   1212                 NULL);
   1213 
   1214    add_function("__intrinsic_shader_clock",
   1215                 _shader_clock_intrinsic(shader_clock,
   1216                                         glsl_type::uvec2_type),
   1217                 NULL);
   1218 
   1219    add_function("__intrinsic_vote_all",
   1220                 _vote_intrinsic(vote_or_v460_desktop, ir_intrinsic_vote_all),
   1221                 NULL);
   1222    add_function("__intrinsic_vote_any",
   1223                 _vote_intrinsic(vote_or_v460_desktop, ir_intrinsic_vote_any),
   1224                 NULL);
   1225    add_function("__intrinsic_vote_eq",
   1226                 _vote_intrinsic(vote_or_v460_desktop, ir_intrinsic_vote_eq),
   1227                 NULL);
   1228 
   1229    add_function("__intrinsic_ballot", _ballot_intrinsic(), NULL);
   1230 
   1231    add_function("__intrinsic_read_invocation",
   1232                 _read_invocation_intrinsic(glsl_type::float_type),
   1233                 _read_invocation_intrinsic(glsl_type::vec2_type),
   1234                 _read_invocation_intrinsic(glsl_type::vec3_type),
   1235                 _read_invocation_intrinsic(glsl_type::vec4_type),
   1236 
   1237                 _read_invocation_intrinsic(glsl_type::int_type),
   1238                 _read_invocation_intrinsic(glsl_type::ivec2_type),
   1239                 _read_invocation_intrinsic(glsl_type::ivec3_type),
   1240                 _read_invocation_intrinsic(glsl_type::ivec4_type),
   1241 
   1242                 _read_invocation_intrinsic(glsl_type::uint_type),
   1243                 _read_invocation_intrinsic(glsl_type::uvec2_type),
   1244                 _read_invocation_intrinsic(glsl_type::uvec3_type),
   1245                 _read_invocation_intrinsic(glsl_type::uvec4_type),
   1246                 NULL);
   1247 
   1248    add_function("__intrinsic_read_first_invocation",
   1249                 _read_first_invocation_intrinsic(glsl_type::float_type),
   1250                 _read_first_invocation_intrinsic(glsl_type::vec2_type),
   1251                 _read_first_invocation_intrinsic(glsl_type::vec3_type),
   1252                 _read_first_invocation_intrinsic(glsl_type::vec4_type),
   1253 
   1254                 _read_first_invocation_intrinsic(glsl_type::int_type),
   1255                 _read_first_invocation_intrinsic(glsl_type::ivec2_type),
   1256                 _read_first_invocation_intrinsic(glsl_type::ivec3_type),
   1257                 _read_first_invocation_intrinsic(glsl_type::ivec4_type),
   1258 
   1259                 _read_first_invocation_intrinsic(glsl_type::uint_type),
   1260                 _read_first_invocation_intrinsic(glsl_type::uvec2_type),
   1261                 _read_first_invocation_intrinsic(glsl_type::uvec3_type),
   1262                 _read_first_invocation_intrinsic(glsl_type::uvec4_type),
   1263                 NULL);
   1264 
   1265 }
   1266 
   1267 /**
   1268  * Create ir_function and ir_function_signature objects for each built-in.
   1269  *
   1270  * Contains a list of every available built-in.
   1271  */
   1272 void
   1273 builtin_builder::create_builtins()
   1274 {
   1275 #define F(NAME)                                 \
   1276    add_function(#NAME,                          \
   1277                 _##NAME(glsl_type::float_type), \
   1278                 _##NAME(glsl_type::vec2_type),  \
   1279                 _##NAME(glsl_type::vec3_type),  \
   1280                 _##NAME(glsl_type::vec4_type),  \
   1281                 NULL);
   1282 
   1283 #define FD(NAME)                                 \
   1284    add_function(#NAME,                          \
   1285                 _##NAME(always_available, glsl_type::float_type), \
   1286                 _##NAME(always_available, glsl_type::vec2_type),  \
   1287                 _##NAME(always_available, glsl_type::vec3_type),  \
   1288                 _##NAME(always_available, glsl_type::vec4_type),  \
   1289                 _##NAME(fp64, glsl_type::double_type),  \
   1290                 _##NAME(fp64, glsl_type::dvec2_type),    \
   1291                 _##NAME(fp64, glsl_type::dvec3_type),     \
   1292                 _##NAME(fp64, glsl_type::dvec4_type),      \
   1293                 NULL);
   1294 
   1295 #define FD130(NAME)                                 \
   1296    add_function(#NAME,                          \
   1297                 _##NAME(v130, glsl_type::float_type), \
   1298                 _##NAME(v130, glsl_type::vec2_type),  \
   1299                 _##NAME(v130, glsl_type::vec3_type),                  \
   1300                 _##NAME(v130, glsl_type::vec4_type),  \
   1301                 _##NAME(fp64, glsl_type::double_type),  \
   1302                 _##NAME(fp64, glsl_type::dvec2_type),    \
   1303                 _##NAME(fp64, glsl_type::dvec3_type),     \
   1304                 _##NAME(fp64, glsl_type::dvec4_type),      \
   1305                 NULL);
   1306 
   1307 #define FDGS5(NAME)                                 \
   1308    add_function(#NAME,                          \
   1309                 _##NAME(gpu_shader5_es, glsl_type::float_type), \
   1310                 _##NAME(gpu_shader5_es, glsl_type::vec2_type),  \
   1311                 _##NAME(gpu_shader5_es, glsl_type::vec3_type),                  \
   1312                 _##NAME(gpu_shader5_es, glsl_type::vec4_type),  \
   1313                 _##NAME(fp64, glsl_type::double_type),  \
   1314                 _##NAME(fp64, glsl_type::dvec2_type),    \
   1315                 _##NAME(fp64, glsl_type::dvec3_type),     \
   1316                 _##NAME(fp64, glsl_type::dvec4_type),      \
   1317                 NULL);
   1318 
   1319 #define FI(NAME)                                \
   1320    add_function(#NAME,                          \
   1321                 _##NAME(glsl_type::float_type), \
   1322                 _##NAME(glsl_type::vec2_type),  \
   1323                 _##NAME(glsl_type::vec3_type),  \
   1324                 _##NAME(glsl_type::vec4_type),  \
   1325                 _##NAME(glsl_type::int_type),   \
   1326                 _##NAME(glsl_type::ivec2_type), \
   1327                 _##NAME(glsl_type::ivec3_type), \
   1328                 _##NAME(glsl_type::ivec4_type), \
   1329                 NULL);
   1330 
   1331 #define FI64(NAME)                                \
   1332    add_function(#NAME,                          \
   1333                 _##NAME(always_available, glsl_type::float_type), \
   1334                 _##NAME(always_available, glsl_type::vec2_type),  \
   1335                 _##NAME(always_available, glsl_type::vec3_type),  \
   1336                 _##NAME(always_available, glsl_type::vec4_type),  \
   1337                 _##NAME(always_available, glsl_type::int_type),   \
   1338                 _##NAME(always_available, glsl_type::ivec2_type), \
   1339                 _##NAME(always_available, glsl_type::ivec3_type), \
   1340                 _##NAME(always_available, glsl_type::ivec4_type), \
   1341                 _##NAME(fp64, glsl_type::double_type), \
   1342                 _##NAME(fp64, glsl_type::dvec2_type),  \
   1343                 _##NAME(fp64, glsl_type::dvec3_type),  \
   1344                 _##NAME(fp64, glsl_type::dvec4_type),  \
   1345                 _##NAME(int64, glsl_type::int64_t_type), \
   1346                 _##NAME(int64, glsl_type::i64vec2_type),  \
   1347                 _##NAME(int64, glsl_type::i64vec3_type),  \
   1348                 _##NAME(int64, glsl_type::i64vec4_type),  \
   1349                 NULL);
   1350 
   1351 #define FIUD_VEC(NAME)                                            \
   1352    add_function(#NAME,                                            \
   1353                 _##NAME(always_available, glsl_type::vec2_type),  \
   1354                 _##NAME(always_available, glsl_type::vec3_type),  \
   1355                 _##NAME(always_available, glsl_type::vec4_type),  \
   1356                                                                   \
   1357                 _##NAME(always_available, glsl_type::ivec2_type), \
   1358                 _##NAME(always_available, glsl_type::ivec3_type), \
   1359                 _##NAME(always_available, glsl_type::ivec4_type), \
   1360                                                                   \
   1361                 _##NAME(v130, glsl_type::uvec2_type),             \
   1362                 _##NAME(v130, glsl_type::uvec3_type),             \
   1363                 _##NAME(v130, glsl_type::uvec4_type),             \
   1364                 _##NAME(fp64, glsl_type::dvec2_type),  \
   1365                 _##NAME(fp64, glsl_type::dvec3_type),  \
   1366                 _##NAME(fp64, glsl_type::dvec4_type),  \
   1367                 _##NAME(int64, glsl_type::int64_t_type), \
   1368                 _##NAME(int64, glsl_type::i64vec2_type),  \
   1369                 _##NAME(int64, glsl_type::i64vec3_type),  \
   1370                 _##NAME(int64, glsl_type::i64vec4_type),  \
   1371                 _##NAME(int64, glsl_type::uint64_t_type), \
   1372                 _##NAME(int64, glsl_type::u64vec2_type),  \
   1373                 _##NAME(int64, glsl_type::u64vec3_type),  \
   1374                 _##NAME(int64, glsl_type::u64vec4_type),  \
   1375                 NULL);
   1376 
   1377 #define IU(NAME)                                \
   1378    add_function(#NAME,                          \
   1379                 _##NAME(glsl_type::int_type),   \
   1380                 _##NAME(glsl_type::ivec2_type), \
   1381                 _##NAME(glsl_type::ivec3_type), \
   1382                 _##NAME(glsl_type::ivec4_type), \
   1383                                                 \
   1384                 _##NAME(glsl_type::uint_type),  \
   1385                 _##NAME(glsl_type::uvec2_type), \
   1386                 _##NAME(glsl_type::uvec3_type), \
   1387                 _##NAME(glsl_type::uvec4_type), \
   1388                 NULL);
   1389 
   1390 #define FIUBD_VEC(NAME)                                           \
   1391    add_function(#NAME,                                            \
   1392                 _##NAME(always_available, glsl_type::vec2_type),  \
   1393                 _##NAME(always_available, glsl_type::vec3_type),  \
   1394                 _##NAME(always_available, glsl_type::vec4_type),  \
   1395                                                                   \
   1396                 _##NAME(always_available, glsl_type::ivec2_type), \
   1397                 _##NAME(always_available, glsl_type::ivec3_type), \
   1398                 _##NAME(always_available, glsl_type::ivec4_type), \
   1399                                                                   \
   1400                 _##NAME(v130, glsl_type::uvec2_type),             \
   1401                 _##NAME(v130, glsl_type::uvec3_type),             \
   1402                 _##NAME(v130, glsl_type::uvec4_type),             \
   1403                                                                   \
   1404                 _##NAME(always_available, glsl_type::bvec2_type), \
   1405                 _##NAME(always_available, glsl_type::bvec3_type), \
   1406                 _##NAME(always_available, glsl_type::bvec4_type), \
   1407                                                                   \
   1408                 _##NAME(fp64, glsl_type::dvec2_type), \
   1409                 _##NAME(fp64, glsl_type::dvec3_type), \
   1410                 _##NAME(fp64, glsl_type::dvec4_type), \
   1411                 _##NAME(int64, glsl_type::int64_t_type), \
   1412                 _##NAME(int64, glsl_type::i64vec2_type),  \
   1413                 _##NAME(int64, glsl_type::i64vec3_type),  \
   1414                 _##NAME(int64, glsl_type::i64vec4_type),  \
   1415                 _##NAME(int64, glsl_type::uint64_t_type), \
   1416                 _##NAME(int64, glsl_type::u64vec2_type),  \
   1417                 _##NAME(int64, glsl_type::u64vec3_type),  \
   1418                 _##NAME(int64, glsl_type::u64vec4_type),  \
   1419                 NULL);
   1420 
   1421 #define FIUD2_MIXED(NAME)                                                                 \
   1422    add_function(#NAME,                                                                   \
   1423                 _##NAME(always_available, glsl_type::float_type, glsl_type::float_type), \
   1424                 _##NAME(always_available, glsl_type::vec2_type,  glsl_type::float_type), \
   1425                 _##NAME(always_available, glsl_type::vec3_type,  glsl_type::float_type), \
   1426                 _##NAME(always_available, glsl_type::vec4_type,  glsl_type::float_type), \
   1427                                                                                          \
   1428                 _##NAME(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),  \
   1429                 _##NAME(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),  \
   1430                 _##NAME(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),  \
   1431                                                                                          \
   1432                 _##NAME(always_available, glsl_type::int_type,   glsl_type::int_type),   \
   1433                 _##NAME(always_available, glsl_type::ivec2_type, glsl_type::int_type),   \
   1434                 _##NAME(always_available, glsl_type::ivec3_type, glsl_type::int_type),   \
   1435                 _##NAME(always_available, glsl_type::ivec4_type, glsl_type::int_type),   \
   1436                                                                                          \
   1437                 _##NAME(always_available, glsl_type::ivec2_type, glsl_type::ivec2_type), \
   1438                 _##NAME(always_available, glsl_type::ivec3_type, glsl_type::ivec3_type), \
   1439                 _##NAME(always_available, glsl_type::ivec4_type, glsl_type::ivec4_type), \
   1440                                                                                          \
   1441                 _##NAME(v130, glsl_type::uint_type,  glsl_type::uint_type),              \
   1442                 _##NAME(v130, glsl_type::uvec2_type, glsl_type::uint_type),              \
   1443                 _##NAME(v130, glsl_type::uvec3_type, glsl_type::uint_type),              \
   1444                 _##NAME(v130, glsl_type::uvec4_type, glsl_type::uint_type),              \
   1445                                                                                          \
   1446                 _##NAME(v130, glsl_type::uvec2_type, glsl_type::uvec2_type),             \
   1447                 _##NAME(v130, glsl_type::uvec3_type, glsl_type::uvec3_type),             \
   1448                 _##NAME(v130, glsl_type::uvec4_type, glsl_type::uvec4_type),             \
   1449                                                                                          \
   1450                 _##NAME(fp64, glsl_type::double_type, glsl_type::double_type),           \
   1451                 _##NAME(fp64, glsl_type::dvec2_type, glsl_type::double_type),           \
   1452                 _##NAME(fp64, glsl_type::dvec3_type, glsl_type::double_type),           \
   1453                 _##NAME(fp64, glsl_type::dvec4_type, glsl_type::double_type),           \
   1454                 _##NAME(fp64, glsl_type::dvec2_type, glsl_type::dvec2_type),           \
   1455                 _##NAME(fp64, glsl_type::dvec3_type, glsl_type::dvec3_type),           \
   1456                 _##NAME(fp64, glsl_type::dvec4_type, glsl_type::dvec4_type),           \
   1457                                                                         \
   1458                 _##NAME(int64, glsl_type::int64_t_type, glsl_type::int64_t_type),           \
   1459                 _##NAME(int64, glsl_type::i64vec2_type, glsl_type::int64_t_type),           \
   1460                 _##NAME(int64, glsl_type::i64vec3_type, glsl_type::int64_t_type),           \
   1461                 _##NAME(int64, glsl_type::i64vec4_type, glsl_type::int64_t_type),           \
   1462                 _##NAME(int64, glsl_type::i64vec2_type, glsl_type::i64vec2_type),           \
   1463                 _##NAME(int64, glsl_type::i64vec3_type, glsl_type::i64vec3_type),           \
   1464                 _##NAME(int64, glsl_type::i64vec4_type, glsl_type::i64vec4_type),           \
   1465                 _##NAME(int64, glsl_type::uint64_t_type, glsl_type::uint64_t_type),           \
   1466                 _##NAME(int64, glsl_type::u64vec2_type, glsl_type::uint64_t_type),           \
   1467                 _##NAME(int64, glsl_type::u64vec3_type, glsl_type::uint64_t_type),           \
   1468                 _##NAME(int64, glsl_type::u64vec4_type, glsl_type::uint64_t_type),           \
   1469                 _##NAME(int64, glsl_type::u64vec2_type, glsl_type::u64vec2_type),           \
   1470                 _##NAME(int64, glsl_type::u64vec3_type, glsl_type::u64vec3_type),           \
   1471                 _##NAME(int64, glsl_type::u64vec4_type, glsl_type::u64vec4_type),           \
   1472                 NULL);
   1473 
   1474    F(radians)
   1475    F(degrees)
   1476    F(sin)
   1477    F(cos)
   1478    F(tan)
   1479    F(asin)
   1480    F(acos)
   1481 
   1482    add_function("atan",
   1483                 _atan(glsl_type::float_type),
   1484                 _atan(glsl_type::vec2_type),
   1485                 _atan(glsl_type::vec3_type),
   1486                 _atan(glsl_type::vec4_type),
   1487                 _atan2(glsl_type::float_type),
   1488                 _atan2(glsl_type::vec2_type),
   1489                 _atan2(glsl_type::vec3_type),
   1490                 _atan2(glsl_type::vec4_type),
   1491                 NULL);
   1492 
   1493    F(sinh)
   1494    F(cosh)
   1495    F(tanh)
   1496    F(asinh)
   1497    F(acosh)
   1498    F(atanh)
   1499    F(pow)
   1500    F(exp)
   1501    F(log)
   1502    F(exp2)
   1503    F(log2)
   1504    FD(sqrt)
   1505    FD(inversesqrt)
   1506    FI64(abs)
   1507    FI64(sign)
   1508    FD(floor)
   1509    FD(trunc)
   1510    FD(round)
   1511    FD(roundEven)
   1512    FD(ceil)
   1513    FD(fract)
   1514 
   1515    add_function("mod",
   1516                 _mod(always_available, glsl_type::float_type, glsl_type::float_type),
   1517                 _mod(always_available, glsl_type::vec2_type,  glsl_type::float_type),
   1518                 _mod(always_available, glsl_type::vec3_type,  glsl_type::float_type),
   1519                 _mod(always_available, glsl_type::vec4_type,  glsl_type::float_type),
   1520 
   1521                 _mod(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
   1522                 _mod(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
   1523                 _mod(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
   1524 
   1525                 _mod(fp64, glsl_type::double_type, glsl_type::double_type),
   1526                 _mod(fp64, glsl_type::dvec2_type,  glsl_type::double_type),
   1527                 _mod(fp64, glsl_type::dvec3_type,  glsl_type::double_type),
   1528                 _mod(fp64, glsl_type::dvec4_type,  glsl_type::double_type),
   1529 
   1530                 _mod(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
   1531                 _mod(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
   1532                 _mod(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
   1533                 NULL);
   1534 
   1535    FD(modf)
   1536 
   1537    FIUD2_MIXED(min)
   1538    FIUD2_MIXED(max)
   1539    FIUD2_MIXED(clamp)
   1540 
   1541    add_function("mix",
   1542                 _mix_lrp(always_available, glsl_type::float_type, glsl_type::float_type),
   1543                 _mix_lrp(always_available, glsl_type::vec2_type,  glsl_type::float_type),
   1544                 _mix_lrp(always_available, glsl_type::vec3_type,  glsl_type::float_type),
   1545                 _mix_lrp(always_available, glsl_type::vec4_type,  glsl_type::float_type),
   1546 
   1547                 _mix_lrp(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
   1548                 _mix_lrp(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
   1549                 _mix_lrp(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
   1550 
   1551                 _mix_lrp(fp64, glsl_type::double_type, glsl_type::double_type),
   1552                 _mix_lrp(fp64, glsl_type::dvec2_type,  glsl_type::double_type),
   1553                 _mix_lrp(fp64, glsl_type::dvec3_type,  glsl_type::double_type),
   1554                 _mix_lrp(fp64, glsl_type::dvec4_type,  glsl_type::double_type),
   1555 
   1556                 _mix_lrp(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
   1557                 _mix_lrp(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
   1558                 _mix_lrp(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
   1559 
   1560                 _mix_sel(v130, glsl_type::float_type, glsl_type::bool_type),
   1561                 _mix_sel(v130, glsl_type::vec2_type,  glsl_type::bvec2_type),
   1562                 _mix_sel(v130, glsl_type::vec3_type,  glsl_type::bvec3_type),
   1563                 _mix_sel(v130, glsl_type::vec4_type,  glsl_type::bvec4_type),
   1564 
   1565                 _mix_sel(fp64, glsl_type::double_type, glsl_type::bool_type),
   1566                 _mix_sel(fp64, glsl_type::dvec2_type,  glsl_type::bvec2_type),
   1567                 _mix_sel(fp64, glsl_type::dvec3_type,  glsl_type::bvec3_type),
   1568                 _mix_sel(fp64, glsl_type::dvec4_type,  glsl_type::bvec4_type),
   1569 
   1570                 _mix_sel(shader_integer_mix, glsl_type::int_type,   glsl_type::bool_type),
   1571                 _mix_sel(shader_integer_mix, glsl_type::ivec2_type, glsl_type::bvec2_type),
   1572                 _mix_sel(shader_integer_mix, glsl_type::ivec3_type, glsl_type::bvec3_type),
   1573                 _mix_sel(shader_integer_mix, glsl_type::ivec4_type, glsl_type::bvec4_type),
   1574 
   1575                 _mix_sel(shader_integer_mix, glsl_type::uint_type,  glsl_type::bool_type),
   1576                 _mix_sel(shader_integer_mix, glsl_type::uvec2_type, glsl_type::bvec2_type),
   1577                 _mix_sel(shader_integer_mix, glsl_type::uvec3_type, glsl_type::bvec3_type),
   1578                 _mix_sel(shader_integer_mix, glsl_type::uvec4_type, glsl_type::bvec4_type),
   1579 
   1580                 _mix_sel(shader_integer_mix, glsl_type::bool_type,  glsl_type::bool_type),
   1581                 _mix_sel(shader_integer_mix, glsl_type::bvec2_type, glsl_type::bvec2_type),
   1582                 _mix_sel(shader_integer_mix, glsl_type::bvec3_type, glsl_type::bvec3_type),
   1583                 _mix_sel(shader_integer_mix, glsl_type::bvec4_type, glsl_type::bvec4_type),
   1584 
   1585                 _mix_sel(int64, glsl_type::int64_t_type, glsl_type::bool_type),
   1586                 _mix_sel(int64, glsl_type::i64vec2_type, glsl_type::bvec2_type),
   1587                 _mix_sel(int64, glsl_type::i64vec3_type, glsl_type::bvec3_type),
   1588                 _mix_sel(int64, glsl_type::i64vec4_type, glsl_type::bvec4_type),
   1589 
   1590                 _mix_sel(int64, glsl_type::uint64_t_type,  glsl_type::bool_type),
   1591                 _mix_sel(int64, glsl_type::u64vec2_type, glsl_type::bvec2_type),
   1592                 _mix_sel(int64, glsl_type::u64vec3_type, glsl_type::bvec3_type),
   1593                 _mix_sel(int64, glsl_type::u64vec4_type, glsl_type::bvec4_type),
   1594                 NULL);
   1595 
   1596    add_function("step",
   1597                 _step(always_available, glsl_type::float_type, glsl_type::float_type),
   1598                 _step(always_available, glsl_type::float_type, glsl_type::vec2_type),
   1599                 _step(always_available, glsl_type::float_type, glsl_type::vec3_type),
   1600                 _step(always_available, glsl_type::float_type, glsl_type::vec4_type),
   1601 
   1602                 _step(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
   1603                 _step(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
   1604                 _step(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
   1605                 _step(fp64, glsl_type::double_type, glsl_type::double_type),
   1606                 _step(fp64, glsl_type::double_type, glsl_type::dvec2_type),
   1607                 _step(fp64, glsl_type::double_type, glsl_type::dvec3_type),
   1608                 _step(fp64, glsl_type::double_type, glsl_type::dvec4_type),
   1609 
   1610                 _step(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
   1611                 _step(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
   1612                 _step(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
   1613                 NULL);
   1614 
   1615    add_function("smoothstep",
   1616                 _smoothstep(always_available, glsl_type::float_type, glsl_type::float_type),
   1617                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec2_type),
   1618                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec3_type),
   1619                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec4_type),
   1620 
   1621                 _smoothstep(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
   1622                 _smoothstep(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
   1623                 _smoothstep(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
   1624                 _smoothstep(fp64, glsl_type::double_type, glsl_type::double_type),
   1625                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec2_type),
   1626                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec3_type),
   1627                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec4_type),
   1628 
   1629                 _smoothstep(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
   1630                 _smoothstep(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
   1631                 _smoothstep(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
   1632                 NULL);
   1633 
   1634    FD130(isnan)
   1635    FD130(isinf)
   1636 
   1637    F(floatBitsToInt)
   1638    F(floatBitsToUint)
   1639    add_function("intBitsToFloat",
   1640                 _intBitsToFloat(glsl_type::int_type),
   1641                 _intBitsToFloat(glsl_type::ivec2_type),
   1642                 _intBitsToFloat(glsl_type::ivec3_type),
   1643                 _intBitsToFloat(glsl_type::ivec4_type),
   1644                 NULL);
   1645    add_function("uintBitsToFloat",
   1646                 _uintBitsToFloat(glsl_type::uint_type),
   1647                 _uintBitsToFloat(glsl_type::uvec2_type),
   1648                 _uintBitsToFloat(glsl_type::uvec3_type),
   1649                 _uintBitsToFloat(glsl_type::uvec4_type),
   1650                 NULL);
   1651 
   1652    add_function("doubleBitsToInt64",
   1653                 _doubleBitsToInt64(int64_fp64, glsl_type::double_type),
   1654                 _doubleBitsToInt64(int64_fp64, glsl_type::dvec2_type),
   1655                 _doubleBitsToInt64(int64_fp64, glsl_type::dvec3_type),
   1656                 _doubleBitsToInt64(int64_fp64, glsl_type::dvec4_type),
   1657                 NULL);
   1658 
   1659    add_function("doubleBitsToUint64",
   1660                 _doubleBitsToUint64(int64_fp64, glsl_type::double_type),
   1661                 _doubleBitsToUint64(int64_fp64, glsl_type::dvec2_type),
   1662                 _doubleBitsToUint64(int64_fp64, glsl_type::dvec3_type),
   1663                 _doubleBitsToUint64(int64_fp64, glsl_type::dvec4_type),
   1664                 NULL);
   1665 
   1666    add_function("int64BitsToDouble",
   1667                 _int64BitsToDouble(int64_fp64, glsl_type::int64_t_type),
   1668                 _int64BitsToDouble(int64_fp64, glsl_type::i64vec2_type),
   1669                 _int64BitsToDouble(int64_fp64, glsl_type::i64vec3_type),
   1670                 _int64BitsToDouble(int64_fp64, glsl_type::i64vec4_type),
   1671                 NULL);
   1672 
   1673    add_function("uint64BitsToDouble",
   1674                 _uint64BitsToDouble(int64_fp64, glsl_type::uint64_t_type),
   1675                 _uint64BitsToDouble(int64_fp64, glsl_type::u64vec2_type),
   1676                 _uint64BitsToDouble(int64_fp64, glsl_type::u64vec3_type),
   1677                 _uint64BitsToDouble(int64_fp64, glsl_type::u64vec4_type),
   1678                 NULL);
   1679 
   1680    add_function("packUnorm2x16",   _packUnorm2x16(shader_packing_or_es3_or_gpu_shader5),   NULL);
   1681    add_function("packSnorm2x16",   _packSnorm2x16(shader_packing_or_es3),                  NULL);
   1682    add_function("packUnorm4x8",    _packUnorm4x8(shader_packing_or_es31_or_gpu_shader5),   NULL);
   1683    add_function("packSnorm4x8",    _packSnorm4x8(shader_packing_or_es31_or_gpu_shader5),   NULL);
   1684    add_function("unpackUnorm2x16", _unpackUnorm2x16(shader_packing_or_es3_or_gpu_shader5), NULL);
   1685    add_function("unpackSnorm2x16", _unpackSnorm2x16(shader_packing_or_es3),                NULL);
   1686    add_function("unpackUnorm4x8",  _unpackUnorm4x8(shader_packing_or_es31_or_gpu_shader5), NULL);
   1687    add_function("unpackSnorm4x8",  _unpackSnorm4x8(shader_packing_or_es31_or_gpu_shader5), NULL);
   1688    add_function("packHalf2x16",    _packHalf2x16(shader_packing_or_es3),                   NULL);
   1689    add_function("unpackHalf2x16",  _unpackHalf2x16(shader_packing_or_es3),                 NULL);
   1690    add_function("packDouble2x32",    _packDouble2x32(fp64),                   NULL);
   1691    add_function("unpackDouble2x32",  _unpackDouble2x32(fp64),                 NULL);
   1692 
   1693    add_function("packInt2x32",     _packInt2x32(int64),                    NULL);
   1694    add_function("unpackInt2x32",   _unpackInt2x32(int64),                  NULL);
   1695    add_function("packUint2x32",    _packUint2x32(int64),                   NULL);
   1696    add_function("unpackUint2x32",  _unpackUint2x32(int64),                 NULL);
   1697 
   1698    FD(length)
   1699    FD(distance)
   1700    FD(dot)
   1701 
   1702    add_function("cross", _cross(always_available, glsl_type::vec3_type),
   1703                 _cross(fp64, glsl_type::dvec3_type), NULL);
   1704 
   1705    FD(normalize)
   1706    add_function("ftransform", _ftransform(), NULL);
   1707    FD(faceforward)
   1708    FD(reflect)
   1709    FD(refract)
   1710    // ...
   1711    add_function("matrixCompMult",
   1712                 _matrixCompMult(always_available, glsl_type::mat2_type),
   1713                 _matrixCompMult(always_available, glsl_type::mat3_type),
   1714                 _matrixCompMult(always_available, glsl_type::mat4_type),
   1715                 _matrixCompMult(always_available, glsl_type::mat2x3_type),
   1716                 _matrixCompMult(always_available, glsl_type::mat2x4_type),
   1717                 _matrixCompMult(always_available, glsl_type::mat3x2_type),
   1718                 _matrixCompMult(always_available, glsl_type::mat3x4_type),
   1719                 _matrixCompMult(always_available, glsl_type::mat4x2_type),
   1720                 _matrixCompMult(always_available, glsl_type::mat4x3_type),
   1721                 _matrixCompMult(fp64, glsl_type::dmat2_type),
   1722                 _matrixCompMult(fp64, glsl_type::dmat3_type),
   1723                 _matrixCompMult(fp64, glsl_type::dmat4_type),
   1724                 _matrixCompMult(fp64, glsl_type::dmat2x3_type),
   1725                 _matrixCompMult(fp64, glsl_type::dmat2x4_type),
   1726                 _matrixCompMult(fp64, glsl_type::dmat3x2_type),
   1727                 _matrixCompMult(fp64, glsl_type::dmat3x4_type),
   1728                 _matrixCompMult(fp64, glsl_type::dmat4x2_type),
   1729                 _matrixCompMult(fp64, glsl_type::dmat4x3_type),
   1730                 NULL);
   1731    add_function("outerProduct",
   1732                 _outerProduct(v120, glsl_type::mat2_type),
   1733                 _outerProduct(v120, glsl_type::mat3_type),
   1734                 _outerProduct(v120, glsl_type::mat4_type),
   1735                 _outerProduct(v120, glsl_type::mat2x3_type),
   1736                 _outerProduct(v120, glsl_type::mat2x4_type),
   1737                 _outerProduct(v120, glsl_type::mat3x2_type),
   1738                 _outerProduct(v120, glsl_type::mat3x4_type),
   1739                 _outerProduct(v120, glsl_type::mat4x2_type),
   1740                 _outerProduct(v120, glsl_type::mat4x3_type),
   1741                 _outerProduct(fp64, glsl_type::dmat2_type),
   1742                 _outerProduct(fp64, glsl_type::dmat3_type),
   1743                 _outerProduct(fp64, glsl_type::dmat4_type),
   1744                 _outerProduct(fp64, glsl_type::dmat2x3_type),
   1745                 _outerProduct(fp64, glsl_type::dmat2x4_type),
   1746                 _outerProduct(fp64, glsl_type::dmat3x2_type),
   1747                 _outerProduct(fp64, glsl_type::dmat3x4_type),
   1748                 _outerProduct(fp64, glsl_type::dmat4x2_type),
   1749                 _outerProduct(fp64, glsl_type::dmat4x3_type),
   1750                 NULL);
   1751    add_function("determinant",
   1752                 _determinant_mat2(v120, glsl_type::mat2_type),
   1753                 _determinant_mat3(v120, glsl_type::mat3_type),
   1754                 _determinant_mat4(v120, glsl_type::mat4_type),
   1755                 _determinant_mat2(fp64, glsl_type::dmat2_type),
   1756                 _determinant_mat3(fp64, glsl_type::dmat3_type),
   1757                 _determinant_mat4(fp64, glsl_type::dmat4_type),
   1758 
   1759                 NULL);
   1760    add_function("inverse",
   1761                 _inverse_mat2(v140_or_es3, glsl_type::mat2_type),
   1762                 _inverse_mat3(v140_or_es3, glsl_type::mat3_type),
   1763                 _inverse_mat4(v140_or_es3, glsl_type::mat4_type),
   1764                 _inverse_mat2(fp64, glsl_type::dmat2_type),
   1765                 _inverse_mat3(fp64, glsl_type::dmat3_type),
   1766                 _inverse_mat4(fp64, glsl_type::dmat4_type),
   1767                 NULL);
   1768    add_function("transpose",
   1769                 _transpose(v120, glsl_type::mat2_type),
   1770                 _transpose(v120, glsl_type::mat3_type),
   1771                 _transpose(v120, glsl_type::mat4_type),
   1772                 _transpose(v120, glsl_type::mat2x3_type),
   1773                 _transpose(v120, glsl_type::mat2x4_type),
   1774                 _transpose(v120, glsl_type::mat3x2_type),
   1775                 _transpose(v120, glsl_type::mat3x4_type),
   1776                 _transpose(v120, glsl_type::mat4x2_type),
   1777                 _transpose(v120, glsl_type::mat4x3_type),
   1778                 _transpose(fp64, glsl_type::dmat2_type),
   1779                 _transpose(fp64, glsl_type::dmat3_type),
   1780                 _transpose(fp64, glsl_type::dmat4_type),
   1781                 _transpose(fp64, glsl_type::dmat2x3_type),
   1782                 _transpose(fp64, glsl_type::dmat2x4_type),
   1783                 _transpose(fp64, glsl_type::dmat3x2_type),
   1784                 _transpose(fp64, glsl_type::dmat3x4_type),
   1785                 _transpose(fp64, glsl_type::dmat4x2_type),
   1786                 _transpose(fp64, glsl_type::dmat4x3_type),
   1787                 NULL);
   1788    FIUD_VEC(lessThan)
   1789    FIUD_VEC(lessThanEqual)
   1790    FIUD_VEC(greaterThan)
   1791    FIUD_VEC(greaterThanEqual)
   1792    FIUBD_VEC(notEqual)
   1793    FIUBD_VEC(equal)
   1794 
   1795    add_function("any",
   1796                 _any(glsl_type::bvec2_type),
   1797                 _any(glsl_type::bvec3_type),
   1798                 _any(glsl_type::bvec4_type),
   1799                 NULL);
   1800 
   1801    add_function("all",
   1802                 _all(glsl_type::bvec2_type),
   1803                 _all(glsl_type::bvec3_type),
   1804                 _all(glsl_type::bvec4_type),
   1805                 NULL);
   1806 
   1807    add_function("not",
   1808                 _not(glsl_type::bvec2_type),
   1809                 _not(glsl_type::bvec3_type),
   1810                 _not(glsl_type::bvec4_type),
   1811                 NULL);
   1812 
   1813    add_function("textureSize",
   1814                 _textureSize(v130, glsl_type::int_type,   glsl_type::sampler1D_type),
   1815                 _textureSize(v130, glsl_type::int_type,   glsl_type::isampler1D_type),
   1816                 _textureSize(v130, glsl_type::int_type,   glsl_type::usampler1D_type),
   1817 
   1818                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2D_type),
   1819                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler2D_type),
   1820                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler2D_type),
   1821 
   1822                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler3D_type),
   1823                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::isampler3D_type),
   1824                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::usampler3D_type),
   1825 
   1826                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::samplerCube_type),
   1827                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isamplerCube_type),
   1828                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usamplerCube_type),
   1829 
   1830                 _textureSize(v130, glsl_type::int_type,   glsl_type::sampler1DShadow_type),
   1831                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DShadow_type),
   1832                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::samplerCubeShadow_type),
   1833 
   1834                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler1DArray_type),
   1835                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler1DArray_type),
   1836                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler1DArray_type),
   1837                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler2DArray_type),
   1838                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::isampler2DArray_type),
   1839                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::usampler2DArray_type),
   1840 
   1841                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler1DArrayShadow_type),
   1842                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler2DArrayShadow_type),
   1843 
   1844                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::samplerCubeArray_type),
   1845                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::isamplerCubeArray_type),
   1846                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::usamplerCubeArray_type),
   1847                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::samplerCubeArrayShadow_type),
   1848 
   1849                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DRect_type),
   1850                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler2DRect_type),
   1851                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler2DRect_type),
   1852                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DRectShadow_type),
   1853 
   1854                 _textureSize(texture_buffer, glsl_type::int_type,   glsl_type::samplerBuffer_type),
   1855                 _textureSize(texture_buffer, glsl_type::int_type,   glsl_type::isamplerBuffer_type),
   1856                 _textureSize(texture_buffer, glsl_type::int_type,   glsl_type::usamplerBuffer_type),
   1857                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::sampler2DMS_type),
   1858                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::isampler2DMS_type),
   1859                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::usampler2DMS_type),
   1860 
   1861                 _textureSize(texture_multisample_array, glsl_type::ivec3_type, glsl_type::sampler2DMSArray_type),
   1862                 _textureSize(texture_multisample_array, glsl_type::ivec3_type, glsl_type::isampler2DMSArray_type),
   1863                 _textureSize(texture_multisample_array, glsl_type::ivec3_type, glsl_type::usampler2DMSArray_type),
   1864                 NULL);
   1865 
   1866    add_function("textureSamples",
   1867                 _textureSamples(shader_samples, glsl_type::sampler2DMS_type),
   1868                 _textureSamples(shader_samples, glsl_type::isampler2DMS_type),
   1869                 _textureSamples(shader_samples, glsl_type::usampler2DMS_type),
   1870 
   1871                 _textureSamples(shader_samples, glsl_type::sampler2DMSArray_type),
   1872                 _textureSamples(shader_samples, glsl_type::isampler2DMSArray_type),
   1873                 _textureSamples(shader_samples, glsl_type::usampler2DMSArray_type),
   1874                 NULL);
   1875 
   1876    add_function("texture",
   1877                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
   1878                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
   1879                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
   1880 
   1881                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
   1882                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
   1883                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
   1884 
   1885                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
   1886                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
   1887                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
   1888 
   1889                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
   1890                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   1891                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   1892 
   1893                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
   1894                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
   1895                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
   1896 
   1897                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
   1898                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
   1899                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
   1900 
   1901                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
   1902                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
   1903                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
   1904 
   1905                 _texture(ir_tex, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
   1906                 _texture(ir_tex, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
   1907                 _texture(ir_tex, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
   1908 
   1909                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   1910                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
   1911                 /* samplerCubeArrayShadow is special; it has an extra parameter
   1912                  * for the shadow comparator since there is no vec5 type.
   1913                  */
   1914                 _textureCubeArrayShadow(texture_cube_map_array, glsl_type::samplerCubeArrayShadow_type),
   1915 
   1916                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type),
   1917                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
   1918                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
   1919 
   1920                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
   1921 
   1922                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
   1923                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
   1924                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
   1925 
   1926                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
   1927                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
   1928                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
   1929 
   1930                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
   1931                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
   1932                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
   1933 
   1934                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
   1935                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   1936                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   1937 
   1938                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
   1939                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
   1940                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
   1941 
   1942                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
   1943                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
   1944                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
   1945 
   1946                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
   1947                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
   1948                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
   1949 
   1950                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
   1951                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
   1952                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
   1953 
   1954                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   1955                 NULL);
   1956 
   1957    add_function("textureLod",
   1958                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
   1959                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
   1960                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
   1961 
   1962                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
   1963                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
   1964                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
   1965 
   1966                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
   1967                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
   1968                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
   1969 
   1970                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
   1971                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   1972                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   1973 
   1974                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
   1975                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
   1976 
   1977                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
   1978                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
   1979                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
   1980 
   1981                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
   1982                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
   1983                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
   1984 
   1985                 _texture(ir_txl, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
   1986                 _texture(ir_txl, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
   1987                 _texture(ir_txl, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
   1988 
   1989                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   1990                 NULL);
   1991 
   1992    add_function("textureOffset",
   1993                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
   1994                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
   1995                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
   1996 
   1997                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
   1998                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   1999                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2000 
   2001                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
   2002                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2003                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2004 
   2005                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type, TEX_OFFSET),
   2006                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
   2007                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
   2008 
   2009                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2010 
   2011                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2012                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2013 
   2014                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
   2015                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2016                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2017 
   2018                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
   2019                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2020                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2021 
   2022                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2023                 /* The next one was forgotten in GLSL 1.30 spec. It's from
   2024                  * EXT_gpu_shader4 originally. It was added in 4.30 with the
   2025                  * wrong syntax. This was corrected in 4.40. 4.30 indicates
   2026                  * that it was intended to be included previously, so allow it
   2027                  * in 1.30.
   2028                  */
   2029                 _texture(ir_tex, v130_desktop, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type, TEX_OFFSET),
   2030 
   2031                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
   2032                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2033                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2034 
   2035                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
   2036                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2037                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2038 
   2039                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
   2040                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2041                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2042 
   2043                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2044                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2045 
   2046                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
   2047                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2048                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2049 
   2050                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
   2051                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2052                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2053 
   2054                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2055                 NULL);
   2056 
   2057    add_function("textureProj",
   2058                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
   2059                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2060                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2061                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2062                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2063                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2064 
   2065                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
   2066                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2067                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2068                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2069                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2070                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2071 
   2072                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2073                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2074                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2075 
   2076                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2077                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2078 
   2079                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT),
   2080                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2081                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2082                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT),
   2083                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2084                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2085 
   2086                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2087 
   2088                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
   2089                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2090                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2091                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2092                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2093                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2094 
   2095                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
   2096                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2097                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2098                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2099                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2100                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2101 
   2102                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2103                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2104                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2105 
   2106                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2107                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2108                 NULL);
   2109 
   2110    add_function("texelFetch",
   2111                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::int_type),
   2112                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::int_type),
   2113                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::int_type),
   2114 
   2115                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::ivec2_type),
   2116                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::ivec2_type),
   2117                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::ivec2_type),
   2118 
   2119                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::ivec3_type),
   2120                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::ivec3_type),
   2121                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::ivec3_type),
   2122 
   2123                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::ivec2_type),
   2124                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::ivec2_type),
   2125                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::ivec2_type),
   2126 
   2127                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::ivec2_type),
   2128                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::ivec2_type),
   2129                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::ivec2_type),
   2130 
   2131                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::ivec3_type),
   2132                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::ivec3_type),
   2133                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::ivec3_type),
   2134 
   2135                 _texelFetch(texture_buffer, glsl_type::vec4_type,  glsl_type::samplerBuffer_type,  glsl_type::int_type),
   2136                 _texelFetch(texture_buffer, glsl_type::ivec4_type, glsl_type::isamplerBuffer_type, glsl_type::int_type),
   2137                 _texelFetch(texture_buffer, glsl_type::uvec4_type, glsl_type::usamplerBuffer_type, glsl_type::int_type),
   2138 
   2139                 _texelFetch(texture_multisample, glsl_type::vec4_type,  glsl_type::sampler2DMS_type,  glsl_type::ivec2_type),
   2140                 _texelFetch(texture_multisample, glsl_type::ivec4_type, glsl_type::isampler2DMS_type, glsl_type::ivec2_type),
   2141                 _texelFetch(texture_multisample, glsl_type::uvec4_type, glsl_type::usampler2DMS_type, glsl_type::ivec2_type),
   2142 
   2143                 _texelFetch(texture_multisample_array, glsl_type::vec4_type,  glsl_type::sampler2DMSArray_type,  glsl_type::ivec3_type),
   2144                 _texelFetch(texture_multisample_array, glsl_type::ivec4_type, glsl_type::isampler2DMSArray_type, glsl_type::ivec3_type),
   2145                 _texelFetch(texture_multisample_array, glsl_type::uvec4_type, glsl_type::usampler2DMSArray_type, glsl_type::ivec3_type),
   2146                 NULL);
   2147 
   2148    add_function("texelFetchOffset",
   2149                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::int_type, glsl_type::int_type),
   2150                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::int_type, glsl_type::int_type),
   2151                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::int_type, glsl_type::int_type),
   2152 
   2153                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::ivec2_type, glsl_type::ivec2_type),
   2154                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
   2155                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
   2156 
   2157                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::ivec3_type, glsl_type::ivec3_type),
   2158                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::ivec3_type, glsl_type::ivec3_type),
   2159                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::ivec3_type, glsl_type::ivec3_type),
   2160 
   2161                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::ivec2_type, glsl_type::ivec2_type),
   2162                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
   2163                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
   2164 
   2165                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::ivec2_type, glsl_type::int_type),
   2166                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::ivec2_type, glsl_type::int_type),
   2167                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::ivec2_type, glsl_type::int_type),
   2168 
   2169                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::ivec3_type, glsl_type::ivec2_type),
   2170                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::ivec3_type, glsl_type::ivec2_type),
   2171                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::ivec3_type, glsl_type::ivec2_type),
   2172 
   2173                 NULL);
   2174 
   2175    add_function("textureProjOffset",
   2176                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2177                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2178                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2179                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2180                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2181                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2182 
   2183                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2184                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2185                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2186                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2187                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2188                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2189 
   2190                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2191                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2192                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2193 
   2194                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2195                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2196 
   2197                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2198                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2199                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2200                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2201                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2202                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2203 
   2204                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2205 
   2206                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2207                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2208                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2209                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2210                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2211                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2212 
   2213                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2214                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2215                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2216                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2217                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2218                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2219 
   2220                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2221                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2222                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2223 
   2224                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2225                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2226                 NULL);
   2227 
   2228    add_function("textureLodOffset",
   2229                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
   2230                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2231                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2232 
   2233                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
   2234                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2235                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2236 
   2237                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
   2238                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2239                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2240 
   2241                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2242                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2243 
   2244                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
   2245                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2246                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2247 
   2248                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
   2249                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2250                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2251 
   2252                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2253                 NULL);
   2254 
   2255    add_function("textureProjLod",
   2256                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
   2257                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2258                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2259                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2260                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2261                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2262 
   2263                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
   2264                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2265                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2266                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2267                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2268                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2269 
   2270                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2271                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2272                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2273 
   2274                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2275                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2276                 NULL);
   2277 
   2278    add_function("textureProjLodOffset",
   2279                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2280                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2281                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2282                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2283                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2284                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2285 
   2286                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2287                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2288                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2289                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2290                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2291                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2292 
   2293                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2294                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2295                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2296 
   2297                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2298                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2299                 NULL);
   2300 
   2301    add_function("textureGrad",
   2302                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
   2303                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
   2304                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
   2305 
   2306                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
   2307                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
   2308                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
   2309 
   2310                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
   2311                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
   2312                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
   2313 
   2314                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
   2315                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   2316                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   2317 
   2318                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type),
   2319                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
   2320                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
   2321 
   2322                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
   2323 
   2324                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
   2325                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
   2326                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
   2327 
   2328                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
   2329                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
   2330                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
   2331 
   2332                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
   2333                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
   2334                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
   2335 
   2336                 _texture(ir_txd, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
   2337                 _texture(ir_txd, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
   2338                 _texture(ir_txd, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
   2339 
   2340                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   2341                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
   2342                 NULL);
   2343 
   2344    add_function("textureGradOffset",
   2345                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
   2346                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2347                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
   2348 
   2349                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
   2350                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2351                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2352 
   2353                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
   2354                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2355                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
   2356 
   2357                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type, TEX_OFFSET),
   2358                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
   2359                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
   2360 
   2361                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2362 
   2363                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2364                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2365 
   2366                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
   2367                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2368                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
   2369 
   2370                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
   2371                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2372                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2373 
   2374                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2375                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type, TEX_OFFSET),
   2376                 NULL);
   2377 
   2378    add_function("textureProjGrad",
   2379                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
   2380                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2381                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2382                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2383                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2384                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2385 
   2386                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
   2387                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2388                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2389                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2390                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2391                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2392 
   2393                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
   2394                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2395                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2396 
   2397                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT),
   2398                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2399                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2400                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT),
   2401                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2402                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2403 
   2404                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2405 
   2406                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2407                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2408                 NULL);
   2409 
   2410    add_function("textureProjGradOffset",
   2411                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2412                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2413                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
   2414                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2415                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2416                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2417 
   2418                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2419                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2420                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2421                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2422                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2423                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2424 
   2425                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2426                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2427                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2428 
   2429                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2430                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2431                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
   2432                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2433                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2434                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2435 
   2436                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2437 
   2438                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2439                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
   2440                 NULL);
   2441 
   2442    add_function("EmitVertex",   _EmitVertex(),   NULL);
   2443    add_function("EndPrimitive", _EndPrimitive(), NULL);
   2444    add_function("EmitStreamVertex",
   2445                 _EmitStreamVertex(gs_streams, glsl_type::uint_type),
   2446                 _EmitStreamVertex(gs_streams, glsl_type::int_type),
   2447                 NULL);
   2448    add_function("EndStreamPrimitive",
   2449                 _EndStreamPrimitive(gs_streams, glsl_type::uint_type),
   2450                 _EndStreamPrimitive(gs_streams, glsl_type::int_type),
   2451                 NULL);
   2452    add_function("barrier", _barrier(), NULL);
   2453 
   2454    add_function("textureQueryLOD",
   2455                 _textureQueryLod(texture_query_lod, glsl_type::sampler1D_type,  glsl_type::float_type),
   2456                 _textureQueryLod(texture_query_lod, glsl_type::isampler1D_type, glsl_type::float_type),
   2457                 _textureQueryLod(texture_query_lod, glsl_type::usampler1D_type, glsl_type::float_type),
   2458 
   2459                 _textureQueryLod(texture_query_lod, glsl_type::sampler2D_type,  glsl_type::vec2_type),
   2460                 _textureQueryLod(texture_query_lod, glsl_type::isampler2D_type, glsl_type::vec2_type),
   2461                 _textureQueryLod(texture_query_lod, glsl_type::usampler2D_type, glsl_type::vec2_type),
   2462 
   2463                 _textureQueryLod(texture_query_lod, glsl_type::sampler3D_type,  glsl_type::vec3_type),
   2464                 _textureQueryLod(texture_query_lod, glsl_type::isampler3D_type, glsl_type::vec3_type),
   2465                 _textureQueryLod(texture_query_lod, glsl_type::usampler3D_type, glsl_type::vec3_type),
   2466 
   2467                 _textureQueryLod(texture_query_lod, glsl_type::samplerCube_type,  glsl_type::vec3_type),
   2468                 _textureQueryLod(texture_query_lod, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   2469                 _textureQueryLod(texture_query_lod, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   2470 
   2471                 _textureQueryLod(texture_query_lod, glsl_type::sampler1DArray_type,  glsl_type::float_type),
   2472                 _textureQueryLod(texture_query_lod, glsl_type::isampler1DArray_type, glsl_type::float_type),
   2473                 _textureQueryLod(texture_query_lod, glsl_type::usampler1DArray_type, glsl_type::float_type),
   2474 
   2475                 _textureQueryLod(texture_query_lod, glsl_type::sampler2DArray_type,  glsl_type::vec2_type),
   2476                 _textureQueryLod(texture_query_lod, glsl_type::isampler2DArray_type, glsl_type::vec2_type),
   2477                 _textureQueryLod(texture_query_lod, glsl_type::usampler2DArray_type, glsl_type::vec2_type),
   2478 
   2479                 _textureQueryLod(texture_query_lod, glsl_type::samplerCubeArray_type,  glsl_type::vec3_type),
   2480                 _textureQueryLod(texture_query_lod, glsl_type::isamplerCubeArray_type, glsl_type::vec3_type),
   2481                 _textureQueryLod(texture_query_lod, glsl_type::usamplerCubeArray_type, glsl_type::vec3_type),
   2482 
   2483                 _textureQueryLod(texture_query_lod, glsl_type::sampler1DShadow_type, glsl_type::float_type),
   2484                 _textureQueryLod(texture_query_lod, glsl_type::sampler2DShadow_type, glsl_type::vec2_type),
   2485                 _textureQueryLod(texture_query_lod, glsl_type::samplerCubeShadow_type, glsl_type::vec3_type),
   2486                 _textureQueryLod(texture_query_lod, glsl_type::sampler1DArrayShadow_type, glsl_type::float_type),
   2487                 _textureQueryLod(texture_query_lod, glsl_type::sampler2DArrayShadow_type, glsl_type::vec2_type),
   2488                 _textureQueryLod(texture_query_lod, glsl_type::samplerCubeArrayShadow_type, glsl_type::vec3_type),
   2489                 NULL);
   2490 
   2491    add_function("textureQueryLod",
   2492                 _textureQueryLod(v400_fs_only, glsl_type::sampler1D_type,  glsl_type::float_type),
   2493                 _textureQueryLod(v400_fs_only, glsl_type::isampler1D_type, glsl_type::float_type),
   2494                 _textureQueryLod(v400_fs_only, glsl_type::usampler1D_type, glsl_type::float_type),
   2495 
   2496                 _textureQueryLod(v400_fs_only, glsl_type::sampler2D_type,  glsl_type::vec2_type),
   2497                 _textureQueryLod(v400_fs_only, glsl_type::isampler2D_type, glsl_type::vec2_type),
   2498                 _textureQueryLod(v400_fs_only, glsl_type::usampler2D_type, glsl_type::vec2_type),
   2499 
   2500                 _textureQueryLod(v400_fs_only, glsl_type::sampler3D_type,  glsl_type::vec3_type),
   2501                 _textureQueryLod(v400_fs_only, glsl_type::isampler3D_type, glsl_type::vec3_type),
   2502                 _textureQueryLod(v400_fs_only, glsl_type::usampler3D_type, glsl_type::vec3_type),
   2503 
   2504                 _textureQueryLod(v400_fs_only, glsl_type::samplerCube_type,  glsl_type::vec3_type),
   2505                 _textureQueryLod(v400_fs_only, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   2506                 _textureQueryLod(v400_fs_only, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   2507 
   2508                 _textureQueryLod(v400_fs_only, glsl_type::sampler1DArray_type,  glsl_type::float_type),
   2509                 _textureQueryLod(v400_fs_only, glsl_type::isampler1DArray_type, glsl_type::float_type),
   2510                 _textureQueryLod(v400_fs_only, glsl_type::usampler1DArray_type, glsl_type::float_type),
   2511 
   2512                 _textureQueryLod(v400_fs_only, glsl_type::sampler2DArray_type,  glsl_type::vec2_type),
   2513                 _textureQueryLod(v400_fs_only, glsl_type::isampler2DArray_type, glsl_type::vec2_type),
   2514                 _textureQueryLod(v400_fs_only, glsl_type::usampler2DArray_type, glsl_type::vec2_type),
   2515 
   2516                 _textureQueryLod(v400_fs_only, glsl_type::samplerCubeArray_type,  glsl_type::vec3_type),
   2517                 _textureQueryLod(v400_fs_only, glsl_type::isamplerCubeArray_type, glsl_type::vec3_type),
   2518                 _textureQueryLod(v400_fs_only, glsl_type::usamplerCubeArray_type, glsl_type::vec3_type),
   2519 
   2520                 _textureQueryLod(v400_fs_only, glsl_type::sampler1DShadow_type, glsl_type::float_type),
   2521                 _textureQueryLod(v400_fs_only, glsl_type::sampler2DShadow_type, glsl_type::vec2_type),
   2522                 _textureQueryLod(v400_fs_only, glsl_type::samplerCubeShadow_type, glsl_type::vec3_type),
   2523                 _textureQueryLod(v400_fs_only, glsl_type::sampler1DArrayShadow_type, glsl_type::float_type),
   2524                 _textureQueryLod(v400_fs_only, glsl_type::sampler2DArrayShadow_type, glsl_type::vec2_type),
   2525                 _textureQueryLod(v400_fs_only, glsl_type::samplerCubeArrayShadow_type, glsl_type::vec3_type),
   2526                 NULL);
   2527 
   2528    add_function("textureQueryLevels",
   2529                 _textureQueryLevels(texture_query_levels, glsl_type::sampler1D_type),
   2530                 _textureQueryLevels(texture_query_levels, glsl_type::sampler2D_type),
   2531                 _textureQueryLevels(texture_query_levels, glsl_type::sampler3D_type),
   2532                 _textureQueryLevels(texture_query_levels, glsl_type::samplerCube_type),
   2533                 _textureQueryLevels(texture_query_levels, glsl_type::sampler1DArray_type),
   2534                 _textureQueryLevels(texture_query_levels, glsl_type::sampler2DArray_type),
   2535                 _textureQueryLevels(texture_query_levels, glsl_type::samplerCubeArray_type),
   2536                 _textureQueryLevels(texture_query_levels, glsl_type::sampler1DShadow_type),
   2537                 _textureQueryLevels(texture_query_levels, glsl_type::sampler2DShadow_type),
   2538                 _textureQueryLevels(texture_query_levels, glsl_type::samplerCubeShadow_type),
   2539                 _textureQueryLevels(texture_query_levels, glsl_type::sampler1DArrayShadow_type),
   2540                 _textureQueryLevels(texture_query_levels, glsl_type::sampler2DArrayShadow_type),
   2541                 _textureQueryLevels(texture_query_levels, glsl_type::samplerCubeArrayShadow_type),
   2542 
   2543                 _textureQueryLevels(texture_query_levels, glsl_type::isampler1D_type),
   2544                 _textureQueryLevels(texture_query_levels, glsl_type::isampler2D_type),
   2545                 _textureQueryLevels(texture_query_levels, glsl_type::isampler3D_type),
   2546                 _textureQueryLevels(texture_query_levels, glsl_type::isamplerCube_type),
   2547                 _textureQueryLevels(texture_query_levels, glsl_type::isampler1DArray_type),
   2548                 _textureQueryLevels(texture_query_levels, glsl_type::isampler2DArray_type),
   2549                 _textureQueryLevels(texture_query_levels, glsl_type::isamplerCubeArray_type),
   2550 
   2551                 _textureQueryLevels(texture_query_levels, glsl_type::usampler1D_type),
   2552                 _textureQueryLevels(texture_query_levels, glsl_type::usampler2D_type),
   2553                 _textureQueryLevels(texture_query_levels, glsl_type::usampler3D_type),
   2554                 _textureQueryLevels(texture_query_levels, glsl_type::usamplerCube_type),
   2555                 _textureQueryLevels(texture_query_levels, glsl_type::usampler1DArray_type),
   2556                 _textureQueryLevels(texture_query_levels, glsl_type::usampler2DArray_type),
   2557                 _textureQueryLevels(texture_query_levels, glsl_type::usamplerCubeArray_type),
   2558 
   2559                 NULL);
   2560 
   2561    add_function("textureSamplesIdenticalEXT",
   2562                 _textureSamplesIdentical(texture_samples_identical, glsl_type::sampler2DMS_type,  glsl_type::ivec2_type),
   2563                 _textureSamplesIdentical(texture_samples_identical, glsl_type::isampler2DMS_type, glsl_type::ivec2_type),
   2564                 _textureSamplesIdentical(texture_samples_identical, glsl_type::usampler2DMS_type, glsl_type::ivec2_type),
   2565 
   2566                 _textureSamplesIdentical(texture_samples_identical_array, glsl_type::sampler2DMSArray_type,  glsl_type::ivec3_type),
   2567                 _textureSamplesIdentical(texture_samples_identical_array, glsl_type::isampler2DMSArray_type, glsl_type::ivec3_type),
   2568                 _textureSamplesIdentical(texture_samples_identical_array, glsl_type::usampler2DMSArray_type, glsl_type::ivec3_type),
   2569                 NULL);
   2570 
   2571    add_function("texture1D",
   2572                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
   2573                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
   2574                 NULL);
   2575 
   2576    add_function("texture1DArray",
   2577                 _texture(ir_tex, texture_array,    glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
   2578                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
   2579                 NULL);
   2580 
   2581    add_function("texture1DProj",
   2582                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2583                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2584                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2585                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2586                 NULL);
   2587 
   2588    add_function("texture1DLod",
   2589                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
   2590                 NULL);
   2591 
   2592    add_function("texture1DArrayLod",
   2593                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
   2594                 NULL);
   2595 
   2596    add_function("texture1DProjLod",
   2597                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2598                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2599                 NULL);
   2600 
   2601    add_function("texture2D",
   2602                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
   2603                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
   2604                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec2_type),
   2605                 NULL);
   2606 
   2607    add_function("texture2DArray",
   2608                 _texture(ir_tex, texture_array,    glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
   2609                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
   2610                 NULL);
   2611 
   2612    add_function("texture2DProj",
   2613                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2614                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2615                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2616                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2617                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec3_type, TEX_PROJECT),
   2618                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec4_type, TEX_PROJECT),
   2619                 NULL);
   2620 
   2621    add_function("texture2DLod",
   2622                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
   2623                 NULL);
   2624 
   2625    add_function("texture2DArrayLod",
   2626                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
   2627                 NULL);
   2628 
   2629    add_function("texture2DProjLod",
   2630                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2631                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2632                 NULL);
   2633 
   2634    add_function("texture3D",
   2635                 _texture(ir_tex, tex3d,    glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
   2636                 _texture(ir_txb, fs_tex3d, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
   2637                 NULL);
   2638 
   2639    add_function("texture3DProj",
   2640                 _texture(ir_tex, tex3d,    glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2641                 _texture(ir_txb, fs_tex3d, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2642                 NULL);
   2643 
   2644    add_function("texture3DLod",
   2645                 _texture(ir_txl, tex3d_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
   2646                 NULL);
   2647 
   2648    add_function("texture3DProjLod",
   2649                 _texture(ir_txl, tex3d_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2650                 NULL);
   2651 
   2652    add_function("textureCube",
   2653                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
   2654                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
   2655                 NULL);
   2656 
   2657    add_function("textureCubeLod",
   2658                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
   2659                 NULL);
   2660 
   2661    add_function("texture2DRect",
   2662                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec2_type),
   2663                 NULL);
   2664 
   2665    add_function("texture2DRectProj",
   2666                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2667                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2668                 NULL);
   2669 
   2670    add_function("shadow1D",
   2671                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
   2672                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
   2673                 NULL);
   2674 
   2675    add_function("shadow1DArray",
   2676                 _texture(ir_tex, texture_array,    glsl_type::vec4_type,  glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   2677                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type,  glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   2678                 NULL);
   2679 
   2680    add_function("shadow2D",
   2681                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
   2682                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
   2683                 NULL);
   2684 
   2685    add_function("shadow2DArray",
   2686                 _texture(ir_tex, texture_array,    glsl_type::vec4_type,  glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
   2687                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type,  glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
   2688                 NULL);
   2689 
   2690    add_function("shadow1DProj",
   2691                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2692                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2693                 NULL);
   2694 
   2695    add_function("shadow2DProj",
   2696                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2697                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2698                 NULL);
   2699 
   2700    add_function("shadow1DLod",
   2701                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
   2702                 NULL);
   2703 
   2704    add_function("shadow2DLod",
   2705                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
   2706                 NULL);
   2707 
   2708    add_function("shadow1DArrayLod",
   2709                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
   2710                 NULL);
   2711 
   2712    add_function("shadow1DProjLod",
   2713                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2714                 NULL);
   2715 
   2716    add_function("shadow2DProjLod",
   2717                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2718                 NULL);
   2719 
   2720    add_function("shadow2DRect",
   2721                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
   2722                 NULL);
   2723 
   2724    add_function("shadow2DRectProj",
   2725                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2726                 NULL);
   2727 
   2728    add_function("texture1DGradARB",
   2729                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
   2730                 NULL);
   2731 
   2732    add_function("texture1DProjGradARB",
   2733                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
   2734                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
   2735                 NULL);
   2736 
   2737    add_function("texture2DGradARB",
   2738                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
   2739                 NULL);
   2740 
   2741    add_function("texture2DProjGradARB",
   2742                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
   2743                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
   2744                 NULL);
   2745 
   2746    add_function("texture3DGradARB",
   2747                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
   2748                 NULL);
   2749 
   2750    add_function("texture3DProjGradARB",
   2751                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
   2752                 NULL);
   2753 
   2754    add_function("textureCubeGradARB",
   2755                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
   2756                 NULL);
   2757 
   2758    add_function("shadow1DGradARB",
   2759                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
   2760                 NULL);
   2761 
   2762    add_function("shadow1DProjGradARB",
   2763                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2764                 NULL);
   2765 
   2766    add_function("shadow2DGradARB",
   2767                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
   2768                 NULL);
   2769 
   2770    add_function("shadow2DProjGradARB",
   2771                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2772                 NULL);
   2773 
   2774    add_function("texture2DRectGradARB",
   2775                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec2_type),
   2776                 NULL);
   2777 
   2778    add_function("texture2DRectProjGradARB",
   2779                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
   2780                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
   2781                 NULL);
   2782 
   2783    add_function("shadow2DRectGradARB",
   2784                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
   2785                 NULL);
   2786 
   2787    add_function("shadow2DRectProjGradARB",
   2788                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
   2789                 NULL);
   2790 
   2791    add_function("textureGather",
   2792                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type),
   2793                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
   2794                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
   2795 
   2796                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type),
   2797                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
   2798                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
   2799 
   2800                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
   2801                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
   2802                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
   2803 
   2804                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::samplerCube_type, glsl_type::vec3_type),
   2805                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
   2806                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
   2807 
   2808                 _texture(ir_tg4, texture_gather_cube_map_array, glsl_type::vec4_type, glsl_type::samplerCubeArray_type, glsl_type::vec4_type),
   2809                 _texture(ir_tg4, texture_gather_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
   2810                 _texture(ir_tg4, texture_gather_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
   2811 
   2812                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
   2813                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
   2814                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
   2815 
   2816                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
   2817                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
   2818                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
   2819 
   2820                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
   2821                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
   2822                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
   2823 
   2824                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::samplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
   2825                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
   2826                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
   2827 
   2828                 _texture(ir_tg4, gpu_shader5_or_OES_texture_cube_map_array, glsl_type::vec4_type, glsl_type::samplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
   2829                 _texture(ir_tg4, gpu_shader5_or_OES_texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
   2830                 _texture(ir_tg4, gpu_shader5_or_OES_texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
   2831 
   2832                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type),
   2833                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type),
   2834                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::samplerCubeShadow_type, glsl_type::vec3_type),
   2835                 _texture(ir_tg4, gpu_shader5_or_OES_texture_cube_map_array, glsl_type::vec4_type, glsl_type::samplerCubeArrayShadow_type, glsl_type::vec4_type),
   2836                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type),
   2837                 NULL);
   2838 
   2839    add_function("textureGatherOffset",
   2840                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2841                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2842                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
   2843 
   2844                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2845                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2846                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
   2847 
   2848                 _texture(ir_tg4, es31_not_gs5, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
   2849                 _texture(ir_tg4, es31_not_gs5, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
   2850                 _texture(ir_tg4, es31_not_gs5, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
   2851 
   2852                 _texture(ir_tg4, es31_not_gs5, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
   2853                 _texture(ir_tg4, es31_not_gs5, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
   2854                 _texture(ir_tg4, es31_not_gs5, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
   2855 
   2856                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2857                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2858                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2859 
   2860                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
   2861                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
   2862                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
   2863 
   2864                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2865                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2866                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2867 
   2868                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2869                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2870                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2871 
   2872                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2873                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2874                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2875 
   2876                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2877                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2878                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
   2879 
   2880                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2881                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
   2882                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
   2883 
   2884                 _texture(ir_tg4, es31_not_gs5, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET),
   2885                 _texture(ir_tg4, es31_not_gs5, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
   2886                 NULL);
   2887 
   2888    add_function("textureGatherOffsets",
   2889                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2890                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2891                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2892 
   2893                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2894                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2895                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2896 
   2897                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
   2898                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
   2899                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
   2900 
   2901                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2902                 _texture(ir_tg4, gpu_shader5_es, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2903                 _texture(ir_tg4, gpu_shader5_es, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2904 
   2905                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2906                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2907                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2908 
   2909                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2910                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2911                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
   2912 
   2913                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2914                 _texture(ir_tg4, gpu_shader5_es, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
   2915                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
   2916                 NULL);
   2917 
   2918    F(dFdx)
   2919    F(dFdy)
   2920    F(fwidth)
   2921    F(dFdxCoarse)
   2922    F(dFdyCoarse)
   2923    F(fwidthCoarse)
   2924    F(dFdxFine)
   2925    F(dFdyFine)
   2926    F(fwidthFine)
   2927    F(noise1)
   2928    F(noise2)
   2929    F(noise3)
   2930    F(noise4)
   2931 
   2932    IU(bitfieldExtract)
   2933    IU(bitfieldInsert)
   2934    IU(bitfieldReverse)
   2935    IU(bitCount)
   2936    IU(findLSB)
   2937    IU(findMSB)
   2938    FDGS5(fma)
   2939 
   2940    add_function("ldexp",
   2941                 _ldexp(glsl_type::float_type, glsl_type::int_type),
   2942                 _ldexp(glsl_type::vec2_type,  glsl_type::ivec2_type),
   2943                 _ldexp(glsl_type::vec3_type,  glsl_type::ivec3_type),
   2944                 _ldexp(glsl_type::vec4_type,  glsl_type::ivec4_type),
   2945                 _ldexp(glsl_type::double_type, glsl_type::int_type),
   2946                 _ldexp(glsl_type::dvec2_type,  glsl_type::ivec2_type),
   2947                 _ldexp(glsl_type::dvec3_type,  glsl_type::ivec3_type),
   2948                 _ldexp(glsl_type::dvec4_type,  glsl_type::ivec4_type),
   2949                 NULL);
   2950 
   2951    add_function("frexp",
   2952                 _frexp(glsl_type::float_type, glsl_type::int_type),
   2953                 _frexp(glsl_type::vec2_type,  glsl_type::ivec2_type),
   2954                 _frexp(glsl_type::vec3_type,  glsl_type::ivec3_type),
   2955                 _frexp(glsl_type::vec4_type,  glsl_type::ivec4_type),
   2956                 _dfrexp(glsl_type::double_type, glsl_type::int_type),
   2957                 _dfrexp(glsl_type::dvec2_type,  glsl_type::ivec2_type),
   2958                 _dfrexp(glsl_type::dvec3_type,  glsl_type::ivec3_type),
   2959                 _dfrexp(glsl_type::dvec4_type,  glsl_type::ivec4_type),
   2960                 NULL);
   2961    add_function("uaddCarry",
   2962                 _uaddCarry(glsl_type::uint_type),
   2963                 _uaddCarry(glsl_type::uvec2_type),
   2964                 _uaddCarry(glsl_type::uvec3_type),
   2965                 _uaddCarry(glsl_type::uvec4_type),
   2966                 NULL);
   2967    add_function("usubBorrow",
   2968                 _usubBorrow(glsl_type::uint_type),
   2969                 _usubBorrow(glsl_type::uvec2_type),
   2970                 _usubBorrow(glsl_type::uvec3_type),
   2971                 _usubBorrow(glsl_type::uvec4_type),
   2972                 NULL);
   2973    add_function("imulExtended",
   2974                 _mulExtended(glsl_type::int_type),
   2975                 _mulExtended(glsl_type::ivec2_type),
   2976                 _mulExtended(glsl_type::ivec3_type),
   2977                 _mulExtended(glsl_type::ivec4_type),
   2978                 NULL);
   2979    add_function("umulExtended",
   2980                 _mulExtended(glsl_type::uint_type),
   2981                 _mulExtended(glsl_type::uvec2_type),
   2982                 _mulExtended(glsl_type::uvec3_type),
   2983                 _mulExtended(glsl_type::uvec4_type),
   2984                 NULL);
   2985    add_function("interpolateAtCentroid",
   2986                 _interpolateAtCentroid(glsl_type::float_type),
   2987                 _interpolateAtCentroid(glsl_type::vec2_type),
   2988                 _interpolateAtCentroid(glsl_type::vec3_type),
   2989                 _interpolateAtCentroid(glsl_type::vec4_type),
   2990                 NULL);
   2991    add_function("interpolateAtOffset",
   2992                 _interpolateAtOffset(glsl_type::float_type),
   2993                 _interpolateAtOffset(glsl_type::vec2_type),
   2994                 _interpolateAtOffset(glsl_type::vec3_type),
   2995                 _interpolateAtOffset(glsl_type::vec4_type),
   2996                 NULL);
   2997    add_function("interpolateAtSample",
   2998                 _interpolateAtSample(glsl_type::float_type),
   2999                 _interpolateAtSample(glsl_type::vec2_type),
   3000                 _interpolateAtSample(glsl_type::vec3_type),
   3001                 _interpolateAtSample(glsl_type::vec4_type),
   3002                 NULL);
   3003 
   3004    add_function("atomicCounter",
   3005                 _atomic_counter_op("__intrinsic_atomic_read",
   3006                                    shader_atomic_counters),
   3007                 NULL);
   3008    add_function("atomicCounterIncrement",
   3009                 _atomic_counter_op("__intrinsic_atomic_increment",
   3010                                    shader_atomic_counters),
   3011                 NULL);
   3012    add_function("atomicCounterDecrement",
   3013                 _atomic_counter_op("__intrinsic_atomic_predecrement",
   3014                                    shader_atomic_counters),
   3015                 NULL);
   3016 
   3017    add_function("atomicCounterAddARB",
   3018                 _atomic_counter_op1("__intrinsic_atomic_add",
   3019                                     shader_atomic_counter_ops),
   3020                 NULL);
   3021    add_function("atomicCounterSubtractARB",
   3022                 _atomic_counter_op1("__intrinsic_atomic_sub",
   3023                                     shader_atomic_counter_ops),
   3024                 NULL);
   3025    add_function("atomicCounterMinARB",
   3026                 _atomic_counter_op1("__intrinsic_atomic_min",
   3027                                     shader_atomic_counter_ops),
   3028                 NULL);
   3029    add_function("atomicCounterMaxARB",
   3030                 _atomic_counter_op1("__intrinsic_atomic_max",
   3031                                     shader_atomic_counter_ops),
   3032                 NULL);
   3033    add_function("atomicCounterAndARB",
   3034                 _atomic_counter_op1("__intrinsic_atomic_and",
   3035                                     shader_atomic_counter_ops),
   3036                 NULL);
   3037    add_function("atomicCounterOrARB",
   3038                 _atomic_counter_op1("__intrinsic_atomic_or",
   3039                                     shader_atomic_counter_ops),
   3040                 NULL);
   3041    add_function("atomicCounterXorARB",
   3042                 _atomic_counter_op1("__intrinsic_atomic_xor",
   3043                                     shader_atomic_counter_ops),
   3044                 NULL);
   3045    add_function("atomicCounterExchangeARB",
   3046                 _atomic_counter_op1("__intrinsic_atomic_exchange",
   3047                                     shader_atomic_counter_ops),
   3048                 NULL);
   3049    add_function("atomicCounterCompSwapARB",
   3050                 _atomic_counter_op2("__intrinsic_atomic_comp_swap",
   3051                                     shader_atomic_counter_ops),
   3052                 NULL);
   3053 
   3054    add_function("atomicCounterAdd",
   3055                 _atomic_counter_op1("__intrinsic_atomic_add",
   3056                                     v460_desktop),
   3057                 NULL);
   3058    add_function("atomicCounterSubtract",
   3059                 _atomic_counter_op1("__intrinsic_atomic_sub",
   3060                                     v460_desktop),
   3061                 NULL);
   3062    add_function("atomicCounterMin",
   3063                 _atomic_counter_op1("__intrinsic_atomic_min",
   3064                                     v460_desktop),
   3065                 NULL);
   3066    add_function("atomicCounterMax",
   3067                 _atomic_counter_op1("__intrinsic_atomic_max",
   3068                                     v460_desktop),
   3069                 NULL);
   3070    add_function("atomicCounterAnd",
   3071                 _atomic_counter_op1("__intrinsic_atomic_and",
   3072                                     v460_desktop),
   3073                 NULL);
   3074    add_function("atomicCounterOr",
   3075                 _atomic_counter_op1("__intrinsic_atomic_or",
   3076                                     v460_desktop),
   3077                 NULL);
   3078    add_function("atomicCounterXor",
   3079                 _atomic_counter_op1("__intrinsic_atomic_xor",
   3080                                     v460_desktop),
   3081                 NULL);
   3082    add_function("atomicCounterExchange",
   3083                 _atomic_counter_op1("__intrinsic_atomic_exchange",
   3084                                     v460_desktop),
   3085                 NULL);
   3086    add_function("atomicCounterCompSwap",
   3087                 _atomic_counter_op2("__intrinsic_atomic_comp_swap",
   3088                                     v460_desktop),
   3089                 NULL);
   3090 
   3091    add_function("atomicAdd",
   3092                 _atomic_op2("__intrinsic_atomic_add",
   3093                             buffer_atomics_supported,
   3094                             glsl_type::uint_type),
   3095                 _atomic_op2("__intrinsic_atomic_add",
   3096                             buffer_atomics_supported,
   3097                             glsl_type::int_type),
   3098                 NULL);
   3099    add_function("atomicMin",
   3100                 _atomic_op2("__intrinsic_atomic_min",
   3101                             buffer_atomics_supported,
   3102                             glsl_type::uint_type),
   3103                 _atomic_op2("__intrinsic_atomic_min",
   3104                             buffer_atomics_supported,
   3105                             glsl_type::int_type),
   3106                 NULL);
   3107    add_function("atomicMax",
   3108                 _atomic_op2("__intrinsic_atomic_max",
   3109                             buffer_atomics_supported,
   3110                             glsl_type::uint_type),
   3111                 _atomic_op2("__intrinsic_atomic_max",
   3112                             buffer_atomics_supported,
   3113                             glsl_type::int_type),
   3114                 NULL);
   3115    add_function("atomicAnd",
   3116                 _atomic_op2("__intrinsic_atomic_and",
   3117                             buffer_atomics_supported,
   3118                             glsl_type::uint_type),
   3119                 _atomic_op2("__intrinsic_atomic_and",
   3120                             buffer_atomics_supported,
   3121                             glsl_type::int_type),
   3122                 NULL);
   3123    add_function("atomicOr",
   3124                 _atomic_op2("__intrinsic_atomic_or",
   3125                             buffer_atomics_supported,
   3126                             glsl_type::uint_type),
   3127                 _atomic_op2("__intrinsic_atomic_or",
   3128                             buffer_atomics_supported,
   3129                             glsl_type::int_type),
   3130                 NULL);
   3131    add_function("atomicXor",
   3132                 _atomic_op2("__intrinsic_atomic_xor",
   3133                             buffer_atomics_supported,
   3134                             glsl_type::uint_type),
   3135                 _atomic_op2("__intrinsic_atomic_xor",
   3136                             buffer_atomics_supported,
   3137                             glsl_type::int_type),
   3138                 NULL);
   3139    add_function("atomicExchange",
   3140                 _atomic_op2("__intrinsic_atomic_exchange",
   3141                             buffer_atomics_supported,
   3142                             glsl_type::uint_type),
   3143                 _atomic_op2("__intrinsic_atomic_exchange",
   3144                             buffer_atomics_supported,
   3145                             glsl_type::int_type),
   3146                 NULL);
   3147    add_function("atomicCompSwap",
   3148                 _atomic_op3("__intrinsic_atomic_comp_swap",
   3149                             buffer_atomics_supported,
   3150                             glsl_type::uint_type),
   3151                 _atomic_op3("__intrinsic_atomic_comp_swap",
   3152                             buffer_atomics_supported,
   3153                             glsl_type::int_type),
   3154                 NULL);
   3155 
   3156    add_function("min3",
   3157                 _min3(glsl_type::float_type),
   3158                 _min3(glsl_type::vec2_type),
   3159                 _min3(glsl_type::vec3_type),
   3160                 _min3(glsl_type::vec4_type),
   3161 
   3162                 _min3(glsl_type::int_type),
   3163                 _min3(glsl_type::ivec2_type),
   3164                 _min3(glsl_type::ivec3_type),
   3165                 _min3(glsl_type::ivec4_type),
   3166 
   3167                 _min3(glsl_type::uint_type),
   3168                 _min3(glsl_type::uvec2_type),
   3169                 _min3(glsl_type::uvec3_type),
   3170                 _min3(glsl_type::uvec4_type),
   3171                 NULL);
   3172 
   3173    add_function("max3",
   3174                 _max3(glsl_type::float_type),
   3175                 _max3(glsl_type::vec2_type),
   3176                 _max3(glsl_type::vec3_type),
   3177                 _max3(glsl_type::vec4_type),
   3178 
   3179                 _max3(glsl_type::int_type),
   3180                 _max3(glsl_type::ivec2_type),
   3181                 _max3(glsl_type::ivec3_type),
   3182                 _max3(glsl_type::ivec4_type),
   3183 
   3184                 _max3(glsl_type::uint_type),
   3185                 _max3(glsl_type::uvec2_type),
   3186                 _max3(glsl_type::uvec3_type),
   3187                 _max3(glsl_type::uvec4_type),
   3188                 NULL);
   3189 
   3190    add_function("mid3",
   3191                 _mid3(glsl_type::float_type),
   3192                 _mid3(glsl_type::vec2_type),
   3193                 _mid3(glsl_type::vec3_type),
   3194                 _mid3(glsl_type::vec4_type),
   3195 
   3196                 _mid3(glsl_type::int_type),
   3197                 _mid3(glsl_type::ivec2_type),
   3198                 _mid3(glsl_type::ivec3_type),
   3199                 _mid3(glsl_type::ivec4_type),
   3200 
   3201                 _mid3(glsl_type::uint_type),
   3202                 _mid3(glsl_type::uvec2_type),
   3203                 _mid3(glsl_type::uvec3_type),
   3204                 _mid3(glsl_type::uvec4_type),
   3205                 NULL);
   3206 
   3207    add_image_functions(true);
   3208 
   3209    add_function("memoryBarrier",
   3210                 _memory_barrier("__intrinsic_memory_barrier",
   3211                                 shader_image_load_store),
   3212                 NULL);
   3213    add_function("groupMemoryBarrier",
   3214                 _memory_barrier("__intrinsic_group_memory_barrier",
   3215                                 compute_shader),
   3216                 NULL);
   3217    add_function("memoryBarrierAtomicCounter",
   3218                 _memory_barrier("__intrinsic_memory_barrier_atomic_counter",
   3219                                 compute_shader_supported),
   3220                 NULL);
   3221    add_function("memoryBarrierBuffer",
   3222                 _memory_barrier("__intrinsic_memory_barrier_buffer",
   3223                                 compute_shader_supported),
   3224                 NULL);
   3225    add_function("memoryBarrierImage",
   3226                 _memory_barrier("__intrinsic_memory_barrier_image",
   3227                                 compute_shader_supported),
   3228                 NULL);
   3229    add_function("memoryBarrierShared",
   3230                 _memory_barrier("__intrinsic_memory_barrier_shared",
   3231                                 compute_shader),
   3232                 NULL);
   3233 
   3234    add_function("ballotARB", _ballot(), NULL);
   3235 
   3236    add_function("readInvocationARB",
   3237                 _read_invocation(glsl_type::float_type),
   3238                 _read_invocation(glsl_type::vec2_type),
   3239                 _read_invocation(glsl_type::vec3_type),
   3240                 _read_invocation(glsl_type::vec4_type),
   3241 
   3242                 _read_invocation(glsl_type::int_type),
   3243                 _read_invocation(glsl_type::ivec2_type),
   3244                 _read_invocation(glsl_type::ivec3_type),
   3245                 _read_invocation(glsl_type::ivec4_type),
   3246 
   3247                 _read_invocation(glsl_type::uint_type),
   3248                 _read_invocation(glsl_type::uvec2_type),
   3249                 _read_invocation(glsl_type::uvec3_type),
   3250                 _read_invocation(glsl_type::uvec4_type),
   3251                 NULL);
   3252 
   3253    add_function("readFirstInvocationARB",
   3254                 _read_first_invocation(glsl_type::float_type),
   3255                 _read_first_invocation(glsl_type::vec2_type),
   3256                 _read_first_invocation(glsl_type::vec3_type),
   3257                 _read_first_invocation(glsl_type::vec4_type),
   3258 
   3259                 _read_first_invocation(glsl_type::int_type),
   3260                 _read_first_invocation(glsl_type::ivec2_type),
   3261                 _read_first_invocation(glsl_type::ivec3_type),
   3262                 _read_first_invocation(glsl_type::ivec4_type),
   3263 
   3264                 _read_first_invocation(glsl_type::uint_type),
   3265                 _read_first_invocation(glsl_type::uvec2_type),
   3266                 _read_first_invocation(glsl_type::uvec3_type),
   3267                 _read_first_invocation(glsl_type::uvec4_type),
   3268                 NULL);
   3269 
   3270    add_function("clock2x32ARB",
   3271                 _shader_clock(shader_clock,
   3272                               glsl_type::uvec2_type),
   3273                 NULL);
   3274 
   3275    add_function("clockARB",
   3276                 _shader_clock(shader_clock_int64,
   3277                               glsl_type::uint64_t_type),
   3278                 NULL);
   3279 
   3280    add_function("anyInvocationARB",
   3281                 _vote("__intrinsic_vote_any", vote),
   3282                 NULL);
   3283 
   3284    add_function("allInvocationsARB",
   3285                 _vote("__intrinsic_vote_all", vote),
   3286                 NULL);
   3287 
   3288    add_function("allInvocationsEqualARB",
   3289                 _vote("__intrinsic_vote_eq", vote),
   3290                 NULL);
   3291 
   3292    add_function("anyInvocation",
   3293                 _vote("__intrinsic_vote_any", v460_desktop),
   3294                 NULL);
   3295 
   3296    add_function("allInvocations",
   3297                 _vote("__intrinsic_vote_all", v460_desktop),
   3298                 NULL);
   3299 
   3300    add_function("allInvocationsEqual",
   3301                 _vote("__intrinsic_vote_eq", v460_desktop),
   3302                 NULL);
   3303 
   3304    add_function("__builtin_idiv64",
   3305                 generate_ir::idiv64(mem_ctx, integer_functions_supported),
   3306                 NULL);
   3307 
   3308    add_function("__builtin_imod64",
   3309                 generate_ir::imod64(mem_ctx, integer_functions_supported),
   3310                 NULL);
   3311 
   3312    add_function("__builtin_sign64",
   3313                 generate_ir::sign64(mem_ctx, integer_functions_supported),
   3314                 NULL);
   3315 
   3316    add_function("__builtin_udiv64",
   3317                 generate_ir::udiv64(mem_ctx, integer_functions_supported),
   3318                 NULL);
   3319 
   3320    add_function("__builtin_umod64",
   3321                 generate_ir::umod64(mem_ctx, integer_functions_supported),
   3322                 NULL);
   3323 
   3324    add_function("__builtin_umul64",
   3325                 generate_ir::umul64(mem_ctx, integer_functions_supported),
   3326                 NULL);
   3327 
   3328 #undef F
   3329 #undef FI
   3330 #undef FIUD_VEC
   3331 #undef FIUBD_VEC
   3332 #undef FIU2_MIXED
   3333 }
   3334 
   3335 void
   3336 builtin_builder::add_function(const char *name, ...)
   3337 {
   3338    va_list ap;
   3339 
   3340    ir_function *f = new(mem_ctx) ir_function(name);
   3341 
   3342    va_start(ap, name);
   3343    while (true) {
   3344       ir_function_signature *sig = va_arg(ap, ir_function_signature *);
   3345       if (sig == NULL)
   3346          break;
   3347 
   3348       if (false) {
   3349          exec_list stuff;
   3350          stuff.push_tail(sig);
   3351          validate_ir_tree(&stuff);
   3352       }
   3353 
   3354       f->add_signature(sig);
   3355    }
   3356    va_end(ap);
   3357 
   3358    shader->symbols->add_function(f);
   3359 }
   3360 
   3361 void
   3362 builtin_builder::add_image_function(const char *name,
   3363                                     const char *intrinsic_name,
   3364                                     image_prototype_ctr prototype,
   3365                                     unsigned num_arguments,
   3366                                     unsigned flags,
   3367                                     enum ir_intrinsic_id intrinsic_id)
   3368 {
   3369    static const glsl_type *const types[] = {
   3370       glsl_type::image1D_type,
   3371       glsl_type::image2D_type,
   3372       glsl_type::image3D_type,
   3373       glsl_type::image2DRect_type,
   3374       glsl_type::imageCube_type,
   3375       glsl_type::imageBuffer_type,
   3376       glsl_type::image1DArray_type,
   3377       glsl_type::image2DArray_type,
   3378       glsl_type::imageCubeArray_type,
   3379       glsl_type::image2DMS_type,
   3380       glsl_type::image2DMSArray_type,
   3381       glsl_type::iimage1D_type,
   3382       glsl_type::iimage2D_type,
   3383       glsl_type::iimage3D_type,
   3384       glsl_type::iimage2DRect_type,
   3385       glsl_type::iimageCube_type,
   3386       glsl_type::iimageBuffer_type,
   3387       glsl_type::iimage1DArray_type,
   3388       glsl_type::iimage2DArray_type,
   3389       glsl_type::iimageCubeArray_type,
   3390       glsl_type::iimage2DMS_type,
   3391       glsl_type::iimage2DMSArray_type,
   3392       glsl_type::uimage1D_type,
   3393       glsl_type::uimage2D_type,
   3394       glsl_type::uimage3D_type,
   3395       glsl_type::uimage2DRect_type,
   3396       glsl_type::uimageCube_type,
   3397       glsl_type::uimageBuffer_type,
   3398       glsl_type::uimage1DArray_type,
   3399       glsl_type::uimage2DArray_type,
   3400       glsl_type::uimageCubeArray_type,
   3401       glsl_type::uimage2DMS_type,
   3402       glsl_type::uimage2DMSArray_type
   3403    };
   3404 
   3405    ir_function *f = new(mem_ctx) ir_function(name);
   3406 
   3407    for (unsigned i = 0; i < ARRAY_SIZE(types); ++i) {
   3408       if ((types[i]->sampled_type != GLSL_TYPE_FLOAT ||
   3409            (flags & IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE)) &&
   3410           (types[i]->sampler_dimensionality == GLSL_SAMPLER_DIM_MS ||
   3411            !(flags & IMAGE_FUNCTION_MS_ONLY)))
   3412          f->add_signature(_image(prototype, types[i], intrinsic_name,
   3413                                  num_arguments, flags, intrinsic_id));
   3414    }
   3415 
   3416    shader->symbols->add_function(f);
   3417 }
   3418 
   3419 void
   3420 builtin_builder::add_image_functions(bool glsl)
   3421 {
   3422    const unsigned flags = (glsl ? IMAGE_FUNCTION_EMIT_STUB : 0);
   3423 
   3424    add_image_function(glsl ? "imageLoad" : "__intrinsic_image_load",
   3425                        "__intrinsic_image_load",
   3426                        &builtin_builder::_image_prototype, 0,
   3427                        (flags | IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE |
   3428                        IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE |
   3429                        IMAGE_FUNCTION_READ_ONLY),
   3430                       ir_intrinsic_image_load);
   3431 
   3432    add_image_function(glsl ? "imageStore" : "__intrinsic_image_store",
   3433                       "__intrinsic_image_store",
   3434                       &builtin_builder::_image_prototype, 1,
   3435                       (flags | IMAGE_FUNCTION_RETURNS_VOID |
   3436                        IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE |
   3437                        IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE |
   3438                        IMAGE_FUNCTION_WRITE_ONLY),
   3439                       ir_intrinsic_image_store);
   3440 
   3441    const unsigned atom_flags = flags | IMAGE_FUNCTION_AVAIL_ATOMIC;
   3442 
   3443    add_image_function(glsl ? "imageAtomicAdd" : "__intrinsic_image_atomic_add",
   3444                       "__intrinsic_image_atomic_add",
   3445                       &builtin_builder::_image_prototype, 1, atom_flags,
   3446                       ir_intrinsic_image_atomic_add);
   3447 
   3448    add_image_function(glsl ? "imageAtomicMin" : "__intrinsic_image_atomic_min",
   3449                       "__intrinsic_image_atomic_min",
   3450                       &builtin_builder::_image_prototype, 1, atom_flags,
   3451                       ir_intrinsic_image_atomic_min);
   3452 
   3453    add_image_function(glsl ? "imageAtomicMax" : "__intrinsic_image_atomic_max",
   3454                       "__intrinsic_image_atomic_max",
   3455                       &builtin_builder::_image_prototype, 1, atom_flags,
   3456                       ir_intrinsic_image_atomic_max);
   3457 
   3458    add_image_function(glsl ? "imageAtomicAnd" : "__intrinsic_image_atomic_and",
   3459                       "__intrinsic_image_atomic_and",
   3460                       &builtin_builder::_image_prototype, 1, atom_flags,
   3461                       ir_intrinsic_image_atomic_and);
   3462 
   3463    add_image_function(glsl ? "imageAtomicOr" : "__intrinsic_image_atomic_or",
   3464                       "__intrinsic_image_atomic_or",
   3465                       &builtin_builder::_image_prototype, 1, atom_flags,
   3466                       ir_intrinsic_image_atomic_or);
   3467 
   3468    add_image_function(glsl ? "imageAtomicXor" : "__intrinsic_image_atomic_xor",
   3469                       "__intrinsic_image_atomic_xor",
   3470                       &builtin_builder::_image_prototype, 1, atom_flags,
   3471                       ir_intrinsic_image_atomic_xor);
   3472 
   3473    add_image_function((glsl ? "imageAtomicExchange" :
   3474                        "__intrinsic_image_atomic_exchange"),
   3475                       "__intrinsic_image_atomic_exchange",
   3476                       &builtin_builder::_image_prototype, 1,
   3477                       (flags | IMAGE_FUNCTION_AVAIL_ATOMIC_EXCHANGE |
   3478                        IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE),
   3479                       ir_intrinsic_image_atomic_exchange);
   3480 
   3481    add_image_function((glsl ? "imageAtomicCompSwap" :
   3482                        "__intrinsic_image_atomic_comp_swap"),
   3483                       "__intrinsic_image_atomic_comp_swap",
   3484                       &builtin_builder::_image_prototype, 2, atom_flags,
   3485                       ir_intrinsic_image_atomic_comp_swap);
   3486 
   3487    add_image_function(glsl ? "imageSize" : "__intrinsic_image_size",
   3488                       "__intrinsic_image_size",
   3489                       &builtin_builder::_image_size_prototype, 1,
   3490                       flags | IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE,
   3491                       ir_intrinsic_image_size);
   3492 
   3493    add_image_function(glsl ? "imageSamples" : "__intrinsic_image_samples",
   3494                       "__intrinsic_image_samples",
   3495                       &builtin_builder::_image_samples_prototype, 1,
   3496                       flags | IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE |
   3497                       IMAGE_FUNCTION_MS_ONLY,
   3498                       ir_intrinsic_image_samples);
   3499 }
   3500 
   3501 ir_variable *
   3502 builtin_builder::in_var(const glsl_type *type, const char *name)
   3503 {
   3504    return new(mem_ctx) ir_variable(type, name, ir_var_function_in);
   3505 }
   3506 
   3507 ir_variable *
   3508 builtin_builder::out_var(const glsl_type *type, const char *name)
   3509 {
   3510    return new(mem_ctx) ir_variable(type, name, ir_var_function_out);
   3511 }
   3512 
   3513 ir_constant *
   3514 builtin_builder::imm(bool b, unsigned vector_elements)
   3515 {
   3516    return new(mem_ctx) ir_constant(b, vector_elements);
   3517 }
   3518 
   3519 ir_constant *
   3520 builtin_builder::imm(float f, unsigned vector_elements)
   3521 {
   3522    return new(mem_ctx) ir_constant(f, vector_elements);
   3523 }
   3524 
   3525 ir_constant *
   3526 builtin_builder::imm(int i, unsigned vector_elements)
   3527 {
   3528    return new(mem_ctx) ir_constant(i, vector_elements);
   3529 }
   3530 
   3531 ir_constant *
   3532 builtin_builder::imm(unsigned u, unsigned vector_elements)
   3533 {
   3534    return new(mem_ctx) ir_constant(u, vector_elements);
   3535 }
   3536 
   3537 ir_constant *
   3538 builtin_builder::imm(double d, unsigned vector_elements)
   3539 {
   3540    return new(mem_ctx) ir_constant(d, vector_elements);
   3541 }
   3542 
   3543 ir_constant *
   3544 builtin_builder::imm(const glsl_type *type, const ir_constant_data &data)
   3545 {
   3546    return new(mem_ctx) ir_constant(type, &data);
   3547 }
   3548 
   3549 #define IMM_FP(type, val) (type->is_double()) ? imm(val) : imm((float)val)
   3550 
   3551 ir_dereference_variable *
   3552 builtin_builder::var_ref(ir_variable *var)
   3553 {
   3554    return new(mem_ctx) ir_dereference_variable(var);
   3555 }
   3556 
   3557 ir_dereference_array *
   3558 builtin_builder::array_ref(ir_variable *var, int idx)
   3559 {
   3560    return new(mem_ctx) ir_dereference_array(var, imm(idx));
   3561 }
   3562 
   3563 /** Return an element of a matrix */
   3564 ir_swizzle *
   3565 builtin_builder::matrix_elt(ir_variable *var, int column, int row)
   3566 {
   3567    return swizzle(array_ref(var, column), row, 1);
   3568 }
   3569 
   3570 /**
   3571  * Implementations of built-in functions:
   3572  *  @{
   3573  */
   3574 ir_function_signature *
   3575 builtin_builder::new_sig(const glsl_type *return_type,
   3576                          builtin_available_predicate avail,
   3577                          int num_params,
   3578                          ...)
   3579 {
   3580    va_list ap;
   3581 
   3582    ir_function_signature *sig =
   3583       new(mem_ctx) ir_function_signature(return_type, avail);
   3584 
   3585    exec_list plist;
   3586    va_start(ap, num_params);
   3587    for (int i = 0; i < num_params; i++) {
   3588       plist.push_tail(va_arg(ap, ir_variable *));
   3589    }
   3590    va_end(ap);
   3591 
   3592    sig->replace_parameters(&plist);
   3593    return sig;
   3594 }
   3595 
   3596 #define MAKE_SIG(return_type, avail, ...)  \
   3597    ir_function_signature *sig =               \
   3598       new_sig(return_type, avail, __VA_ARGS__);      \
   3599    ir_factory body(&sig->body, mem_ctx);             \
   3600    sig->is_defined = true;
   3601 
   3602 #define MAKE_INTRINSIC(return_type, id, avail, ...)  \
   3603    ir_function_signature *sig =                      \
   3604       new_sig(return_type, avail, __VA_ARGS__);      \
   3605    sig->intrinsic_id = id;
   3606 
   3607 ir_function_signature *
   3608 builtin_builder::unop(builtin_available_predicate avail,
   3609                       ir_expression_operation opcode,
   3610                       const glsl_type *return_type,
   3611                       const glsl_type *param_type)
   3612 {
   3613    ir_variable *x = in_var(param_type, "x");
   3614    MAKE_SIG(return_type, avail, 1, x);
   3615    body.emit(ret(expr(opcode, x)));
   3616    return sig;
   3617 }
   3618 
   3619 #define UNOP(NAME, OPCODE, AVAIL)               \
   3620 ir_function_signature *                         \
   3621 builtin_builder::_##NAME(const glsl_type *type) \
   3622 {                                               \
   3623    return unop(&AVAIL, OPCODE, type, type);     \
   3624 }
   3625 
   3626 #define UNOPA(NAME, OPCODE)               \
   3627 ir_function_signature *                         \
   3628 builtin_builder::_##NAME(builtin_available_predicate avail, const glsl_type *type) \
   3629 {                                               \
   3630    return unop(avail, OPCODE, type, type);     \
   3631 }
   3632 
   3633 ir_function_signature *
   3634 builtin_builder::binop(builtin_available_predicate avail,
   3635                        ir_expression_operation opcode,
   3636                        const glsl_type *return_type,
   3637                        const glsl_type *param0_type,
   3638                        const glsl_type *param1_type,
   3639                        bool swap_operands)
   3640 {
   3641    ir_variable *x = in_var(param0_type, "x");
   3642    ir_variable *y = in_var(param1_type, "y");
   3643    MAKE_SIG(return_type, avail, 2, x, y);
   3644 
   3645    if (swap_operands)
   3646       body.emit(ret(expr(opcode, y, x)));
   3647    else
   3648       body.emit(ret(expr(opcode, x, y)));
   3649 
   3650    return sig;
   3651 }
   3652 
   3653 #define BINOP(NAME, OPCODE, AVAIL)                                      \
   3654 ir_function_signature *                                                 \
   3655 builtin_builder::_##NAME(const glsl_type *return_type,                  \
   3656                          const glsl_type *param0_type,                  \
   3657                          const glsl_type *param1_type)                  \
   3658 {                                                                       \
   3659    return binop(&AVAIL, OPCODE, return_type, param0_type, param1_type); \
   3660 }
   3661 
   3662 /**
   3663  * Angle and Trigonometry Functions @{
   3664  */
   3665 
   3666 ir_function_signature *
   3667 builtin_builder::_radians(const glsl_type *type)
   3668 {
   3669    ir_variable *degrees = in_var(type, "degrees");
   3670    MAKE_SIG(type, always_available, 1, degrees);
   3671    body.emit(ret(mul(degrees, imm(0.0174532925f))));
   3672    return sig;
   3673 }
   3674 
   3675 ir_function_signature *
   3676 builtin_builder::_degrees(const glsl_type *type)
   3677 {
   3678    ir_variable *radians = in_var(type, "radians");
   3679    MAKE_SIG(type, always_available, 1, radians);
   3680    body.emit(ret(mul(radians, imm(57.29578f))));
   3681    return sig;
   3682 }
   3683 
   3684 UNOP(sin, ir_unop_sin, always_available)
   3685 UNOP(cos, ir_unop_cos, always_available)
   3686 
   3687 ir_function_signature *
   3688 builtin_builder::_tan(const glsl_type *type)
   3689 {
   3690    ir_variable *theta = in_var(type, "theta");
   3691    MAKE_SIG(type, always_available, 1, theta);
   3692    body.emit(ret(div(sin(theta), cos(theta))));
   3693    return sig;
   3694 }
   3695 
   3696 ir_expression *
   3697 builtin_builder::asin_expr(ir_variable *x, float p0, float p1)
   3698 {
   3699    return mul(sign(x),
   3700               sub(imm(M_PI_2f),
   3701                   mul(sqrt(sub(imm(1.0f), abs(x))),
   3702                       add(imm(M_PI_2f),
   3703                           mul(abs(x),
   3704                               add(imm(M_PI_4f - 1.0f),
   3705                                   mul(abs(x),
   3706                                       add(imm(p0),
   3707                                           mul(abs(x), imm(p1))))))))));
   3708 }
   3709 
   3710 /**
   3711  * Generate a ir_call to a function with a set of parameters
   3712  *
   3713  * The input \c params can either be a list of \c ir_variable or a list of
   3714  * \c ir_dereference_variable.  In the latter case, all nodes will be removed
   3715  * from \c params and used directly as the parameters to the generated
   3716  * \c ir_call.
   3717  */
   3718 ir_call *
   3719 builtin_builder::call(ir_function *f, ir_variable *ret, exec_list params)
   3720 {
   3721    exec_list actual_params;
   3722 
   3723    foreach_in_list_safe(ir_instruction, ir, &params) {
   3724       ir_dereference_variable *d = ir->as_dereference_variable();
   3725       if (d != NULL) {
   3726          d->remove();
   3727          actual_params.push_tail(d);
   3728       } else {
   3729          ir_variable *var = ir->as_variable();
   3730          assert(var != NULL);
   3731          actual_params.push_tail(var_ref(var));
   3732       }
   3733    }
   3734 
   3735    ir_function_signature *sig =
   3736       f->exact_matching_signature(NULL, &actual_params);
   3737    if (!sig)
   3738       return NULL;
   3739 
   3740    ir_dereference_variable *deref =
   3741       (sig->return_type->is_void() ? NULL : var_ref(ret));
   3742 
   3743    return new(mem_ctx) ir_call(sig, deref, &actual_params);
   3744 }
   3745 
   3746 ir_function_signature *
   3747 builtin_builder::_asin(const glsl_type *type)
   3748 {
   3749    ir_variable *x = in_var(type, "x");
   3750    MAKE_SIG(type, always_available, 1, x);
   3751 
   3752    body.emit(ret(asin_expr(x, 0.086566724f, -0.03102955f)));
   3753 
   3754    return sig;
   3755 }
   3756 
   3757 ir_function_signature *
   3758 builtin_builder::_acos(const glsl_type *type)
   3759 {
   3760    ir_variable *x = in_var(type, "x");
   3761    MAKE_SIG(type, always_available, 1, x);
   3762 
   3763    body.emit(ret(sub(imm(M_PI_2f), asin_expr(x, 0.08132463f, -0.02363318f))));
   3764 
   3765    return sig;
   3766 }
   3767 
   3768 ir_function_signature *
   3769 builtin_builder::_atan2(const glsl_type *type)
   3770 {
   3771    const unsigned n = type->vector_elements;
   3772    ir_variable *y = in_var(type, "y");
   3773    ir_variable *x = in_var(type, "x");
   3774    MAKE_SIG(type, always_available, 2, y, x);
   3775 
   3776    /* If we're on the left half-plane rotate the coordinates /2 clock-wise
   3777     * for the y=0 discontinuity to end up aligned with the vertical
   3778     * discontinuity of atan(s/t) along t=0.  This also makes sure that we
   3779     * don't attempt to divide by zero along the vertical line, which may give
   3780     * unspecified results on non-GLSL 4.1-capable hardware.
   3781     */
   3782    ir_variable *flip = body.make_temp(glsl_type::bvec(n), "flip");
   3783    body.emit(assign(flip, gequal(imm(0.0f, n), x)));
   3784    ir_variable *s = body.make_temp(type, "s");
   3785    body.emit(assign(s, csel(flip, abs(x), y)));
   3786    ir_variable *t = body.make_temp(type, "t");
   3787    body.emit(assign(t, csel(flip, y, abs(x))));
   3788 
   3789    /* If the magnitude of the denominator exceeds some huge value, scale down
   3790     * the arguments in order to prevent the reciprocal operation from flushing
   3791     * its result to zero, which would cause precision problems, and for s
   3792     * infinite would cause us to return a NaN instead of the correct finite
   3793     * value.
   3794     *
   3795     * If fmin and fmax are respectively the smallest and largest positive
   3796     * normalized floating point values representable by the implementation,
   3797     * the constants below should be in agreement with:
   3798     *
   3799     *    huge <= 1 / fmin
   3800     *    scale <= 1 / fmin / fmax (for |t| >= huge)
   3801     *
   3802     * In addition scale should be a negative power of two in order to avoid
   3803     * loss of precision.  The values chosen below should work for most usual
   3804     * floating point representations with at least the dynamic range of ATI's
   3805     * 24-bit representation.
   3806     */
   3807    ir_constant *huge = imm(1e18f, n);
   3808    ir_variable *scale = body.make_temp(type, "scale");
   3809    body.emit(assign(scale, csel(gequal(abs(t), huge),
   3810                                 imm(0.25f, n), imm(1.0f, n))));
   3811    ir_variable *rcp_scaled_t = body.make_temp(type, "rcp_scaled_t");
   3812    body.emit(assign(rcp_scaled_t, rcp(mul(t, scale))));
   3813    ir_expression *s_over_t = mul(mul(s, scale), rcp_scaled_t);
   3814 
   3815    /* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily
   3816     * that / = 1) in order to comply with the rather artificial rules
   3817     * inherited from IEEE 754-2008, namely:
   3818     *
   3819     *  "atan2(, ) is 3/4
   3820     *   atan2(, +) is /4"
   3821     *
   3822     * Note that this is inconsistent with the rules for the neighborhood of
   3823     * zero that are based on iterated limits:
   3824     *
   3825     *  "atan2(0, 0) is 
   3826     *   atan2(0, +0) is 0"
   3827     *
   3828     * but GLSL specifically allows implementations to deviate from IEEE rules
   3829     * at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as
   3830     * well).
   3831     */
   3832    ir_expression *tan = csel(equal(abs(x), abs(y)),
   3833                              imm(1.0f, n), abs(s_over_t));
   3834 
   3835    /* Calculate the arctangent and fix up the result if we had flipped the
   3836     * coordinate system.
   3837     */
   3838    ir_variable *arc = body.make_temp(type, "arc");
   3839    do_atan(body, type, arc, tan);
   3840    body.emit(assign(arc, add(arc, mul(b2f(flip), imm(M_PI_2f)))));
   3841 
   3842    /* Rather convoluted calculation of the sign of the result.  When x < 0 we
   3843     * cannot use fsign because we need to be able to distinguish between
   3844     * negative and positive zero.  Unfortunately we cannot use bitwise
   3845     * arithmetic tricks either because of back-ends without integer support.
   3846     * When x >= 0 rcp_scaled_t will always be non-negative so this won't be
   3847     * able to distinguish between negative and positive zero, but we don't
   3848     * care because atan2 is continuous along the whole positive y = 0
   3849     * half-line, so it won't affect the result significantly.
   3850     */
   3851    body.emit(ret(csel(less(min2(y, rcp_scaled_t), imm(0.0f, n)),
   3852                       neg(arc), arc)));
   3853 
   3854    return sig;
   3855 }
   3856 
   3857 void
   3858 builtin_builder::do_atan(ir_factory &body, const glsl_type *type, ir_variable *res, operand y_over_x)
   3859 {
   3860    /*
   3861     * range-reduction, first step:
   3862     *
   3863     *      / y_over_x         if |y_over_x| <= 1.0;
   3864     * x = <
   3865     *      \ 1.0 / y_over_x   otherwise
   3866     */
   3867    ir_variable *x = body.make_temp(type, "atan_x");
   3868    body.emit(assign(x, div(min2(abs(y_over_x),
   3869                                 imm(1.0f)),
   3870                            max2(abs(y_over_x),
   3871                                 imm(1.0f)))));
   3872 
   3873    /*
   3874     * approximate atan by evaluating polynomial:
   3875     *
   3876     * x   * 0.9999793128310355 - x^3  * 0.3326756418091246 +
   3877     * x^5 * 0.1938924977115610 - x^7  * 0.1173503194786851 +
   3878     * x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
   3879     */
   3880    ir_variable *tmp = body.make_temp(type, "atan_tmp");
   3881    body.emit(assign(tmp, mul(x, x)));
   3882    body.emit(assign(tmp, mul(add(mul(sub(mul(add(mul(sub(mul(add(mul(imm(-0.0121323213173444f),
   3883                                                                      tmp),
   3884                                                                  imm(0.0536813784310406f)),
   3885                                                              tmp),
   3886                                                          imm(0.1173503194786851f)),
   3887                                                      tmp),
   3888                                                  imm(0.1938924977115610f)),
   3889                                              tmp),
   3890                                          imm(0.3326756418091246f)),
   3891                                      tmp),
   3892                                  imm(0.9999793128310355f)),
   3893                              x)));
   3894 
   3895    /* range-reduction fixup */
   3896    body.emit(assign(tmp, add(tmp,
   3897                              mul(b2f(greater(abs(y_over_x),
   3898                                           imm(1.0f, type->components()))),
   3899                                   add(mul(tmp,
   3900                                           imm(-2.0f)),
   3901                                       imm(M_PI_2f))))));
   3902 
   3903    /* sign fixup */
   3904    body.emit(assign(res, mul(tmp, sign(y_over_x))));
   3905 }
   3906 
   3907 ir_function_signature *
   3908 builtin_builder::_atan(const glsl_type *type)
   3909 {
   3910    ir_variable *y_over_x = in_var(type, "y_over_x");
   3911    MAKE_SIG(type, always_available, 1, y_over_x);
   3912 
   3913    ir_variable *tmp = body.make_temp(type, "tmp");
   3914    do_atan(body, type, tmp, y_over_x);
   3915    body.emit(ret(tmp));
   3916 
   3917    return sig;
   3918 }
   3919 
   3920 ir_function_signature *
   3921 builtin_builder::_sinh(const glsl_type *type)
   3922 {
   3923    ir_variable *x = in_var(type, "x");
   3924    MAKE_SIG(type, v130, 1, x);
   3925 
   3926    /* 0.5 * (e^x - e^(-x)) */
   3927    body.emit(ret(mul(imm(0.5f), sub(exp(x), exp(neg(x))))));
   3928 
   3929    return sig;
   3930 }
   3931 
   3932 ir_function_signature *
   3933 builtin_builder::_cosh(const glsl_type *type)
   3934 {
   3935    ir_variable *x = in_var(type, "x");
   3936    MAKE_SIG(type, v130, 1, x);
   3937 
   3938    /* 0.5 * (e^x + e^(-x)) */
   3939    body.emit(ret(mul(imm(0.5f), add(exp(x), exp(neg(x))))));
   3940 
   3941    return sig;
   3942 }
   3943 
   3944 ir_function_signature *
   3945 builtin_builder::_tanh(const glsl_type *type)
   3946 {
   3947    ir_variable *x = in_var(type, "x");
   3948    MAKE_SIG(type, v130, 1, x);
   3949 
   3950    /* tanh(x) := (0.5 * (e^x - e^(-x))) / (0.5 * (e^x + e^(-x)))
   3951     *
   3952     * With a little algebra this reduces to (e^2x - 1) / (e^2x + 1)
   3953     *
   3954     * Clamp x to (-inf, +10] to avoid precision problems.  When x > 10, e^2x
   3955     * is so much larger than 1.0 that 1.0 gets flushed to zero in the
   3956     * computation e^2x +/- 1 so it can be ignored.
   3957     */
   3958    ir_variable *t = body.make_temp(type, "tmp");
   3959    body.emit(assign(t, min2(x, imm(10.0f))));
   3960 
   3961    body.emit(ret(div(sub(exp(mul(t, imm(2.0f))), imm(1.0f)),
   3962                      add(exp(mul(t, imm(2.0f))), imm(1.0f)))));
   3963 
   3964    return sig;
   3965 }
   3966 
   3967 ir_function_signature *
   3968 builtin_builder::_asinh(const glsl_type *type)
   3969 {
   3970    ir_variable *x = in_var(type, "x");
   3971    MAKE_SIG(type, v130, 1, x);
   3972 
   3973    body.emit(ret(mul(sign(x), log(add(abs(x), sqrt(add(mul(x, x),
   3974                                                        imm(1.0f))))))));
   3975    return sig;
   3976 }
   3977 
   3978 ir_function_signature *
   3979 builtin_builder::_acosh(const glsl_type *type)
   3980 {
   3981    ir_variable *x = in_var(type, "x");
   3982    MAKE_SIG(type, v130, 1, x);
   3983 
   3984    body.emit(ret(log(add(x, sqrt(sub(mul(x, x), imm(1.0f)))))));
   3985    return sig;
   3986 }
   3987 
   3988 ir_function_signature *
   3989 builtin_builder::_atanh(const glsl_type *type)
   3990 {
   3991    ir_variable *x = in_var(type, "x");
   3992    MAKE_SIG(type, v130, 1, x);
   3993 
   3994    body.emit(ret(mul(imm(0.5f), log(div(add(imm(1.0f), x),
   3995                                         sub(imm(1.0f), x))))));
   3996    return sig;
   3997 }
   3998 /** @} */
   3999 
   4000 /**
   4001  * Exponential Functions @{
   4002  */
   4003 
   4004 ir_function_signature *
   4005 builtin_builder::_pow(const glsl_type *type)
   4006 {
   4007    return binop(always_available, ir_binop_pow, type, type, type);
   4008 }
   4009 
   4010 UNOP(exp,         ir_unop_exp,  always_available)
   4011 UNOP(log,         ir_unop_log,  always_available)
   4012 UNOP(exp2,        ir_unop_exp2, always_available)
   4013 UNOP(log2,        ir_unop_log2, always_available)
   4014 UNOPA(sqrt,        ir_unop_sqrt)
   4015 UNOPA(inversesqrt, ir_unop_rsq)
   4016 
   4017 /** @} */
   4018 
   4019 UNOPA(abs,       ir_unop_abs)
   4020 UNOPA(sign,      ir_unop_sign)
   4021 UNOPA(floor,     ir_unop_floor)
   4022 UNOPA(trunc,     ir_unop_trunc)
   4023 UNOPA(round,     ir_unop_round_even)
   4024 UNOPA(roundEven, ir_unop_round_even)
   4025 UNOPA(ceil,      ir_unop_ceil)
   4026 UNOPA(fract,     ir_unop_fract)
   4027 
   4028 ir_function_signature *
   4029 builtin_builder::_mod(builtin_available_predicate avail,
   4030                       const glsl_type *x_type, const glsl_type *y_type)
   4031 {
   4032    return binop(avail, ir_binop_mod, x_type, x_type, y_type);
   4033 }
   4034 
   4035 ir_function_signature *
   4036 builtin_builder::_modf(builtin_available_predicate avail, const glsl_type *type)
   4037 {
   4038    ir_variable *x = in_var(type, "x");
   4039    ir_variable *i = out_var(type, "i");
   4040    MAKE_SIG(type, avail, 2, x, i);
   4041 
   4042    ir_variable *t = body.make_temp(type, "t");
   4043    body.emit(assign(t, expr(ir_unop_trunc, x)));
   4044    body.emit(assign(i, t));
   4045    body.emit(ret(sub(x, t)));
   4046 
   4047    return sig;
   4048 }
   4049 
   4050 ir_function_signature *
   4051 builtin_builder::_min(builtin_available_predicate avail,
   4052                       const glsl_type *x_type, const glsl_type *y_type)
   4053 {
   4054    return binop(avail, ir_binop_min, x_type, x_type, y_type);
   4055 }
   4056 
   4057 ir_function_signature *
   4058 builtin_builder::_max(builtin_available_predicate avail,
   4059                       const glsl_type *x_type, const glsl_type *y_type)
   4060 {
   4061    return binop(avail, ir_binop_max, x_type, x_type, y_type);
   4062 }
   4063 
   4064 ir_function_signature *
   4065 builtin_builder::_clamp(builtin_available_predicate avail,
   4066                         const glsl_type *val_type, const glsl_type *bound_type)
   4067 {
   4068    ir_variable *x = in_var(val_type, "x");
   4069    ir_variable *minVal = in_var(bound_type, "minVal");
   4070    ir_variable *maxVal = in_var(bound_type, "maxVal");
   4071    MAKE_SIG(val_type, avail, 3, x, minVal, maxVal);
   4072 
   4073    body.emit(ret(clamp(x, minVal, maxVal)));
   4074 
   4075    return sig;
   4076 }
   4077 
   4078 ir_function_signature *
   4079 builtin_builder::_mix_lrp(builtin_available_predicate avail, const glsl_type *val_type, const glsl_type *blend_type)
   4080 {
   4081    ir_variable *x = in_var(val_type, "x");
   4082    ir_variable *y = in_var(val_type, "y");
   4083    ir_variable *a = in_var(blend_type, "a");
   4084    MAKE_SIG(val_type, avail, 3, x, y, a);
   4085 
   4086    body.emit(ret(lrp(x, y, a)));
   4087 
   4088    return sig;
   4089 }
   4090 
   4091 ir_function_signature *
   4092 builtin_builder::_mix_sel(builtin_available_predicate avail,
   4093                           const glsl_type *val_type,
   4094                           const glsl_type *blend_type)
   4095 {
   4096    ir_variable *x = in_var(val_type, "x");
   4097    ir_variable *y = in_var(val_type, "y");
   4098    ir_variable *a = in_var(blend_type, "a");
   4099    MAKE_SIG(val_type, avail, 3, x, y, a);
   4100 
   4101    /* csel matches the ternary operator in that a selector of true choses the
   4102     * first argument. This differs from mix(x, y, false) which choses the
   4103     * second argument (to remain consistent with the interpolating version of
   4104     * mix() which takes a blend factor from 0.0 to 1.0 where 0.0 is only x.
   4105     *
   4106     * To handle the behavior mismatch, reverse the x and y arguments.
   4107     */
   4108    body.emit(ret(csel(a, y, x)));
   4109 
   4110    return sig;
   4111 }
   4112 
   4113 ir_function_signature *
   4114 builtin_builder::_step(builtin_available_predicate avail, const glsl_type *edge_type, const glsl_type *x_type)
   4115 {
   4116    ir_variable *edge = in_var(edge_type, "edge");
   4117    ir_variable *x = in_var(x_type, "x");
   4118    MAKE_SIG(x_type, avail, 2, edge, x);
   4119 
   4120    ir_variable *t = body.make_temp(x_type, "t");
   4121    if (x_type->vector_elements == 1) {
   4122       /* Both are floats */
   4123       if (edge_type->is_double())
   4124          body.emit(assign(t, f2d(b2f(gequal(x, edge)))));
   4125       else
   4126          body.emit(assign(t, b2f(gequal(x, edge))));
   4127    } else if (edge_type->vector_elements == 1) {
   4128       /* x is a vector but edge is a float */
   4129       for (int i = 0; i < x_type->vector_elements; i++) {
   4130          if (edge_type->is_double())
   4131             body.emit(assign(t, f2d(b2f(gequal(swizzle(x, i, 1), edge))), 1 << i));
   4132          else
   4133             body.emit(assign(t, b2f(gequal(swizzle(x, i, 1), edge)), 1 << i));
   4134       }
   4135    } else {
   4136       /* Both are vectors */
   4137       for (int i = 0; i < x_type->vector_elements; i++) {
   4138          if (edge_type->is_double())
   4139             body.emit(assign(t, f2d(b2f(gequal(swizzle(x, i, 1), swizzle(edge, i, 1)))),
   4140                              1 << i));
   4141          else
   4142             body.emit(assign(t, b2f(gequal(swizzle(x, i, 1), swizzle(edge, i, 1))),
   4143                              1 << i));
   4144 
   4145       }
   4146    }
   4147    body.emit(ret(t));
   4148 
   4149    return sig;
   4150 }
   4151 
   4152 ir_function_signature *
   4153 builtin_builder::_smoothstep(builtin_available_predicate avail, const glsl_type *edge_type, const glsl_type *x_type)
   4154 {
   4155    ir_variable *edge0 = in_var(edge_type, "edge0");
   4156    ir_variable *edge1 = in_var(edge_type, "edge1");
   4157    ir_variable *x = in_var(x_type, "x");
   4158    MAKE_SIG(x_type, avail, 3, edge0, edge1, x);
   4159 
   4160    /* From the GLSL 1.10 specification:
   4161     *
   4162     *    genType t;
   4163     *    t = clamp((x - edge0) / (edge1 - edge0), 0, 1);
   4164     *    return t * t * (3 - 2 * t);
   4165     */
   4166 
   4167    ir_variable *t = body.make_temp(x_type, "t");
   4168    body.emit(assign(t, clamp(div(sub(x, edge0), sub(edge1, edge0)),
   4169                              IMM_FP(x_type, 0.0), IMM_FP(x_type, 1.0))));
   4170 
   4171    body.emit(ret(mul(t, mul(t, sub(IMM_FP(x_type, 3.0), mul(IMM_FP(x_type, 2.0), t))))));
   4172 
   4173    return sig;
   4174 }
   4175 
   4176 ir_function_signature *
   4177 builtin_builder::_isnan(builtin_available_predicate avail, const glsl_type *type)
   4178 {
   4179    ir_variable *x = in_var(type, "x");
   4180    MAKE_SIG(glsl_type::bvec(type->vector_elements), avail, 1, x);
   4181 
   4182    body.emit(ret(nequal(x, x)));
   4183 
   4184    return sig;
   4185 }
   4186 
   4187 ir_function_signature *
   4188 builtin_builder::_isinf(builtin_available_predicate avail, const glsl_type *type)
   4189 {
   4190    ir_variable *x = in_var(type, "x");
   4191    MAKE_SIG(glsl_type::bvec(type->vector_elements), avail, 1, x);
   4192 
   4193    ir_constant_data infinities;
   4194    for (int i = 0; i < type->vector_elements; i++) {
   4195       switch (type->base_type) {
   4196       case GLSL_TYPE_FLOAT:
   4197          infinities.f[i] = INFINITY;
   4198          break;
   4199       case GLSL_TYPE_DOUBLE:
   4200          infinities.d[i] = INFINITY;
   4201          break;
   4202       default:
   4203          unreachable("unknown type");
   4204       }
   4205    }
   4206 
   4207    body.emit(ret(equal(abs(x), imm(type, infinities))));
   4208 
   4209    return sig;
   4210 }
   4211 
   4212 ir_function_signature *
   4213 builtin_builder::_floatBitsToInt(const glsl_type *type)
   4214 {
   4215    ir_variable *x = in_var(type, "x");
   4216    MAKE_SIG(glsl_type::ivec(type->vector_elements), shader_bit_encoding, 1, x);
   4217    body.emit(ret(bitcast_f2i(x)));
   4218    return sig;
   4219 }
   4220 
   4221 ir_function_signature *
   4222 builtin_builder::_floatBitsToUint(const glsl_type *type)
   4223 {
   4224    ir_variable *x = in_var(type, "x");
   4225    MAKE_SIG(glsl_type::uvec(type->vector_elements), shader_bit_encoding, 1, x);
   4226    body.emit(ret(bitcast_f2u(x)));
   4227    return sig;
   4228 }
   4229 
   4230 ir_function_signature *
   4231 builtin_builder::_intBitsToFloat(const glsl_type *type)
   4232 {
   4233    ir_variable *x = in_var(type, "x");
   4234    MAKE_SIG(glsl_type::vec(type->vector_elements), shader_bit_encoding, 1, x);
   4235    body.emit(ret(bitcast_i2f(x)));
   4236    return sig;
   4237 }
   4238 
   4239 ir_function_signature *
   4240 builtin_builder::_uintBitsToFloat(const glsl_type *type)
   4241 {
   4242    ir_variable *x = in_var(type, "x");
   4243    MAKE_SIG(glsl_type::vec(type->vector_elements), shader_bit_encoding, 1, x);
   4244    body.emit(ret(bitcast_u2f(x)));
   4245    return sig;
   4246 }
   4247 
   4248 ir_function_signature *
   4249 builtin_builder::_doubleBitsToInt64(builtin_available_predicate avail, const glsl_type *type)
   4250 {
   4251    ir_variable *x = in_var(type, "x");
   4252    MAKE_SIG(glsl_type::i64vec(type->vector_elements), avail, 1, x);
   4253    body.emit(ret(bitcast_d2i64(x)));
   4254    return sig;
   4255 }
   4256 
   4257 ir_function_signature *
   4258 builtin_builder::_doubleBitsToUint64(builtin_available_predicate avail, const glsl_type *type)
   4259 {
   4260    ir_variable *x = in_var(type, "x");
   4261    MAKE_SIG(glsl_type::u64vec(type->vector_elements), avail, 1, x);
   4262    body.emit(ret(bitcast_d2u64(x)));
   4263    return sig;
   4264 }
   4265 
   4266 ir_function_signature *
   4267 builtin_builder::_int64BitsToDouble(builtin_available_predicate avail, const glsl_type *type)
   4268 {
   4269    ir_variable *x = in_var(type, "x");
   4270    MAKE_SIG(glsl_type::dvec(type->vector_elements), avail, 1, x);
   4271    body.emit(ret(bitcast_i642d(x)));
   4272    return sig;
   4273 }
   4274 
   4275 ir_function_signature *
   4276 builtin_builder::_uint64BitsToDouble(builtin_available_predicate avail, const glsl_type *type)
   4277 {
   4278    ir_variable *x = in_var(type, "x");
   4279    MAKE_SIG(glsl_type::dvec(type->vector_elements), avail, 1, x);
   4280    body.emit(ret(bitcast_u642d(x)));
   4281    return sig;
   4282 }
   4283 
   4284 ir_function_signature *
   4285 builtin_builder::_packUnorm2x16(builtin_available_predicate avail)
   4286 {
   4287    ir_variable *v = in_var(glsl_type::vec2_type, "v");
   4288    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
   4289    body.emit(ret(expr(ir_unop_pack_unorm_2x16, v)));
   4290    return sig;
   4291 }
   4292 
   4293 ir_function_signature *
   4294 builtin_builder::_packSnorm2x16(builtin_available_predicate avail)
   4295 {
   4296    ir_variable *v = in_var(glsl_type::vec2_type, "v");
   4297    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
   4298    body.emit(ret(expr(ir_unop_pack_snorm_2x16, v)));
   4299    return sig;
   4300 }
   4301 
   4302 ir_function_signature *
   4303 builtin_builder::_packUnorm4x8(builtin_available_predicate avail)
   4304 {
   4305    ir_variable *v = in_var(glsl_type::vec4_type, "v");
   4306    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
   4307    body.emit(ret(expr(ir_unop_pack_unorm_4x8, v)));
   4308    return sig;
   4309 }
   4310 
   4311 ir_function_signature *
   4312 builtin_builder::_packSnorm4x8(builtin_available_predicate avail)
   4313 {
   4314    ir_variable *v = in_var(glsl_type::vec4_type, "v");
   4315    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
   4316    body.emit(ret(expr(ir_unop_pack_snorm_4x8, v)));
   4317    return sig;
   4318 }
   4319 
   4320 ir_function_signature *
   4321 builtin_builder::_unpackUnorm2x16(builtin_available_predicate avail)
   4322 {
   4323    ir_variable *p = in_var(glsl_type::uint_type, "p");
   4324    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
   4325    body.emit(ret(expr(ir_unop_unpack_unorm_2x16, p)));
   4326    return sig;
   4327 }
   4328 
   4329 ir_function_signature *
   4330 builtin_builder::_unpackSnorm2x16(builtin_available_predicate avail)
   4331 {
   4332    ir_variable *p = in_var(glsl_type::uint_type, "p");
   4333    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
   4334    body.emit(ret(expr(ir_unop_unpack_snorm_2x16, p)));
   4335    return sig;
   4336 }
   4337 
   4338 
   4339 ir_function_signature *
   4340 builtin_builder::_unpackUnorm4x8(builtin_available_predicate avail)
   4341 {
   4342    ir_variable *p = in_var(glsl_type::uint_type, "p");
   4343    MAKE_SIG(glsl_type::vec4_type, avail, 1, p);
   4344    body.emit(ret(expr(ir_unop_unpack_unorm_4x8, p)));
   4345    return sig;
   4346 }
   4347 
   4348 ir_function_signature *
   4349 builtin_builder::_unpackSnorm4x8(builtin_available_predicate avail)
   4350 {
   4351    ir_variable *p = in_var(glsl_type::uint_type, "p");
   4352    MAKE_SIG(glsl_type::vec4_type, avail, 1, p);
   4353    body.emit(ret(expr(ir_unop_unpack_snorm_4x8, p)));
   4354    return sig;
   4355 }
   4356 
   4357 ir_function_signature *
   4358 builtin_builder::_packHalf2x16(builtin_available_predicate avail)
   4359 {
   4360    ir_variable *v = in_var(glsl_type::vec2_type, "v");
   4361    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
   4362    body.emit(ret(expr(ir_unop_pack_half_2x16, v)));
   4363    return sig;
   4364 }
   4365 
   4366 ir_function_signature *
   4367 builtin_builder::_unpackHalf2x16(builtin_available_predicate avail)
   4368 {
   4369    ir_variable *p = in_var(glsl_type::uint_type, "p");
   4370    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
   4371    body.emit(ret(expr(ir_unop_unpack_half_2x16, p)));
   4372    return sig;
   4373 }
   4374 
   4375 ir_function_signature *
   4376 builtin_builder::_packDouble2x32(builtin_available_predicate avail)
   4377 {
   4378    ir_variable *v = in_var(glsl_type::uvec2_type, "v");
   4379    MAKE_SIG(glsl_type::double_type, avail, 1, v);
   4380    body.emit(ret(expr(ir_unop_pack_double_2x32, v)));
   4381    return sig;
   4382 }
   4383 
   4384 ir_function_signature *
   4385 builtin_builder::_unpackDouble2x32(builtin_available_predicate avail)
   4386 {
   4387    ir_variable *p = in_var(glsl_type::double_type, "p");
   4388    MAKE_SIG(glsl_type::uvec2_type, avail, 1, p);
   4389    body.emit(ret(expr(ir_unop_unpack_double_2x32, p)));
   4390    return sig;
   4391 }
   4392 
   4393 ir_function_signature *
   4394 builtin_builder::_packInt2x32(builtin_available_predicate avail)
   4395 {
   4396    ir_variable *v = in_var(glsl_type::ivec2_type, "v");
   4397    MAKE_SIG(glsl_type::int64_t_type, avail, 1, v);
   4398    body.emit(ret(expr(ir_unop_pack_int_2x32, v)));
   4399    return sig;
   4400 }
   4401 
   4402 ir_function_signature *
   4403 builtin_builder::_unpackInt2x32(builtin_available_predicate avail)
   4404 {
   4405    ir_variable *p = in_var(glsl_type::int64_t_type, "p");
   4406    MAKE_SIG(glsl_type::ivec2_type, avail, 1, p);
   4407    body.emit(ret(expr(ir_unop_unpack_int_2x32, p)));
   4408    return sig;
   4409 }
   4410 
   4411 ir_function_signature *
   4412 builtin_builder::_packUint2x32(builtin_available_predicate avail)
   4413 {
   4414    ir_variable *v = in_var(glsl_type::uvec2_type, "v");
   4415    MAKE_SIG(glsl_type::uint64_t_type, avail, 1, v);
   4416    body.emit(ret(expr(ir_unop_pack_uint_2x32, v)));
   4417    return sig;
   4418 }
   4419 
   4420 ir_function_signature *
   4421 builtin_builder::_unpackUint2x32(builtin_available_predicate avail)
   4422 {
   4423    ir_variable *p = in_var(glsl_type::uint64_t_type, "p");
   4424    MAKE_SIG(glsl_type::uvec2_type, avail, 1, p);
   4425    body.emit(ret(expr(ir_unop_unpack_uint_2x32, p)));
   4426    return sig;
   4427 }
   4428 
   4429 ir_function_signature *
   4430 builtin_builder::_length(builtin_available_predicate avail, const glsl_type *type)
   4431 {
   4432    ir_variable *x = in_var(type, "x");
   4433    MAKE_SIG(type->get_base_type(), avail, 1, x);
   4434 
   4435    body.emit(ret(sqrt(dot(x, x))));
   4436 
   4437    return sig;
   4438 }
   4439 
   4440 ir_function_signature *
   4441 builtin_builder::_distance(builtin_available_predicate avail, const glsl_type *type)
   4442 {
   4443    ir_variable *p0 = in_var(type, "p0");
   4444    ir_variable *p1 = in_var(type, "p1");
   4445    MAKE_SIG(type->get_base_type(), avail, 2, p0, p1);
   4446 
   4447    if (type->vector_elements == 1) {
   4448       body.emit(ret(abs(sub(p0, p1))));
   4449    } else {
   4450       ir_variable *p = body.make_temp(type, "p");
   4451       body.emit(assign(p, sub(p0, p1)));
   4452       body.emit(ret(sqrt(dot(p, p))));
   4453    }
   4454 
   4455    return sig;
   4456 }
   4457 
   4458 ir_function_signature *
   4459 builtin_builder::_dot(builtin_available_predicate avail, const glsl_type *type)
   4460 {
   4461    if (type->vector_elements == 1)
   4462       return binop(avail, ir_binop_mul, type, type, type);
   4463 
   4464    return binop(avail, ir_binop_dot,
   4465                 type->get_base_type(), type, type);
   4466 }
   4467 
   4468 ir_function_signature *
   4469 builtin_builder::_cross(builtin_available_predicate avail, const glsl_type *type)
   4470 {
   4471    ir_variable *a = in_var(type, "a");
   4472    ir_variable *b = in_var(type, "b");
   4473    MAKE_SIG(type, avail, 2, a, b);
   4474 
   4475    int yzx = MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, 0);
   4476    int zxy = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, 0);
   4477 
   4478    body.emit(ret(sub(mul(swizzle(a, yzx, 3), swizzle(b, zxy, 3)),
   4479                      mul(swizzle(a, zxy, 3), swizzle(b, yzx, 3)))));
   4480 
   4481    return sig;
   4482 }
   4483 
   4484 ir_function_signature *
   4485 builtin_builder::_normalize(builtin_available_predicate avail, const glsl_type *type)
   4486 {
   4487    ir_variable *x = in_var(type, "x");
   4488    MAKE_SIG(type, avail, 1, x);
   4489 
   4490    if (type->vector_elements == 1) {
   4491       body.emit(ret(sign(x)));
   4492    } else {
   4493       body.emit(ret(mul(x, rsq(dot(x, x)))));
   4494    }
   4495 
   4496    return sig;
   4497 }
   4498 
   4499 ir_function_signature *
   4500 builtin_builder::_ftransform()
   4501 {
   4502    MAKE_SIG(glsl_type::vec4_type, compatibility_vs_only, 0);
   4503 
   4504    /* ftransform() refers to global variables, and is always emitted
   4505     * directly by ast_function.cpp.  Just emit a prototype here so we
   4506     * can recognize calls to it.
   4507     */
   4508    return sig;
   4509 }
   4510 
   4511 ir_function_signature *
   4512 builtin_builder::_faceforward(builtin_available_predicate avail, const glsl_type *type)
   4513 {
   4514    ir_variable *N = in_var(type, "N");
   4515    ir_variable *I = in_var(type, "I");
   4516    ir_variable *Nref = in_var(type, "Nref");
   4517    MAKE_SIG(type, avail, 3, N, I, Nref);
   4518 
   4519    body.emit(if_tree(less(dot(Nref, I), IMM_FP(type, 0.0)),
   4520                      ret(N), ret(neg(N))));
   4521 
   4522    return sig;
   4523 }
   4524 
   4525 ir_function_signature *
   4526 builtin_builder::_reflect(builtin_available_predicate avail, const glsl_type *type)
   4527 {
   4528    ir_variable *I = in_var(type, "I");
   4529    ir_variable *N = in_var(type, "N");
   4530    MAKE_SIG(type, avail, 2, I, N);
   4531 
   4532    /* I - 2 * dot(N, I) * N */
   4533    body.emit(ret(sub(I, mul(IMM_FP(type, 2.0), mul(dot(N, I), N)))));
   4534 
   4535    return sig;
   4536 }
   4537 
   4538 ir_function_signature *
   4539 builtin_builder::_refract(builtin_available_predicate avail, const glsl_type *type)
   4540 {
   4541    ir_variable *I = in_var(type, "I");
   4542    ir_variable *N = in_var(type, "N");
   4543    ir_variable *eta = in_var(type->get_base_type(), "eta");
   4544    MAKE_SIG(type, avail, 3, I, N, eta);
   4545 
   4546    ir_variable *n_dot_i = body.make_temp(type->get_base_type(), "n_dot_i");
   4547    body.emit(assign(n_dot_i, dot(N, I)));
   4548 
   4549    /* From the GLSL 1.10 specification:
   4550     * k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
   4551     * if (k < 0.0)
   4552     *    return genType(0.0)
   4553     * else
   4554     *    return eta * I - (eta * dot(N, I) + sqrt(k)) * N
   4555     */
   4556    ir_variable *k = body.make_temp(type->get_base_type(), "k");
   4557    body.emit(assign(k, sub(IMM_FP(type, 1.0),
   4558                            mul(eta, mul(eta, sub(IMM_FP(type, 1.0),
   4559                                                  mul(n_dot_i, n_dot_i)))))));
   4560    body.emit(if_tree(less(k, IMM_FP(type, 0.0)),
   4561                      ret(ir_constant::zero(mem_ctx, type)),
   4562                      ret(sub(mul(eta, I),
   4563                              mul(add(mul(eta, n_dot_i), sqrt(k)), N)))));
   4564 
   4565    return sig;
   4566 }
   4567 
   4568 ir_function_signature *
   4569 builtin_builder::_matrixCompMult(builtin_available_predicate avail, const glsl_type *type)
   4570 {
   4571    ir_variable *x = in_var(type, "x");
   4572    ir_variable *y = in_var(type, "y");
   4573    MAKE_SIG(type, avail, 2, x, y);
   4574 
   4575    ir_variable *z = body.make_temp(type, "z");
   4576    for (int i = 0; i < type->matrix_columns; i++) {
   4577       body.emit(assign(array_ref(z, i), mul(array_ref(x, i), array_ref(y, i))));
   4578    }
   4579    body.emit(ret(z));
   4580 
   4581    return sig;
   4582 }
   4583 
   4584 ir_function_signature *
   4585 builtin_builder::_outerProduct(builtin_available_predicate avail, const glsl_type *type)
   4586 {
   4587    ir_variable *c;
   4588    ir_variable *r;
   4589 
   4590    if (type->is_double()) {
   4591       r = in_var(glsl_type::dvec(type->matrix_columns), "r");
   4592       c = in_var(glsl_type::dvec(type->vector_elements), "c");
   4593    } else {
   4594       r = in_var(glsl_type::vec(type->matrix_columns), "r");
   4595       c = in_var(glsl_type::vec(type->vector_elements), "c");
   4596    }
   4597    MAKE_SIG(type, avail, 2, c, r);
   4598 
   4599    ir_variable *m = body.make_temp(type, "m");
   4600    for (int i = 0; i < type->matrix_columns; i++) {
   4601       body.emit(assign(array_ref(m, i), mul(c, swizzle(r, i, 1))));
   4602    }
   4603    body.emit(ret(m));
   4604 
   4605    return sig;
   4606 }
   4607 
   4608 ir_function_signature *
   4609 builtin_builder::_transpose(builtin_available_predicate avail, const glsl_type *orig_type)
   4610 {
   4611    const glsl_type *transpose_type =
   4612       glsl_type::get_instance(orig_type->base_type,
   4613                               orig_type->matrix_columns,
   4614                               orig_type->vector_elements);
   4615 
   4616    ir_variable *m = in_var(orig_type, "m");
   4617    MAKE_SIG(transpose_type, avail, 1, m);
   4618 
   4619    ir_variable *t = body.make_temp(transpose_type, "t");
   4620    for (int i = 0; i < orig_type->matrix_columns; i++) {
   4621       for (int j = 0; j < orig_type->vector_elements; j++) {
   4622          body.emit(assign(array_ref(t, j),
   4623                           matrix_elt(m, i, j),
   4624                           1 << i));
   4625       }
   4626    }
   4627    body.emit(ret(t));
   4628 
   4629    return sig;
   4630 }
   4631 
   4632 ir_function_signature *
   4633 builtin_builder::_determinant_mat2(builtin_available_predicate avail, const glsl_type *type)
   4634 {
   4635    ir_variable *m = in_var(type, "m");
   4636    MAKE_SIG(type->get_base_type(), avail, 1, m);
   4637 
   4638    body.emit(ret(sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
   4639                      mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1)))));
   4640 
   4641    return sig;
   4642 }
   4643 
   4644 ir_function_signature *
   4645 builtin_builder::_determinant_mat3(builtin_available_predicate avail, const glsl_type *type)
   4646 {
   4647    ir_variable *m = in_var(type, "m");
   4648    MAKE_SIG(type->get_base_type(), avail, 1, m);
   4649 
   4650    ir_expression *f1 =
   4651       sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)),
   4652           mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 1)));
   4653 
   4654    ir_expression *f2 =
   4655       sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)),
   4656           mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 0)));
   4657 
   4658    ir_expression *f3 =
   4659       sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)),
   4660           mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 0)));
   4661 
   4662    body.emit(ret(add(sub(mul(matrix_elt(m, 0, 0), f1),
   4663                          mul(matrix_elt(m, 0, 1), f2)),
   4664                      mul(matrix_elt(m, 0, 2), f3))));
   4665 
   4666    return sig;
   4667 }
   4668 
   4669 ir_function_signature *
   4670 builtin_builder::_determinant_mat4(builtin_available_predicate avail, const glsl_type *type)
   4671 {
   4672    ir_variable *m = in_var(type, "m");
   4673    const glsl_type *btype = type->get_base_type();
   4674    MAKE_SIG(btype, avail, 1, m);
   4675 
   4676    ir_variable *SubFactor00 = body.make_temp(btype, "SubFactor00");
   4677    ir_variable *SubFactor01 = body.make_temp(btype, "SubFactor01");
   4678    ir_variable *SubFactor02 = body.make_temp(btype, "SubFactor02");
   4679    ir_variable *SubFactor03 = body.make_temp(btype, "SubFactor03");
   4680    ir_variable *SubFactor04 = body.make_temp(btype, "SubFactor04");
   4681    ir_variable *SubFactor05 = body.make_temp(btype, "SubFactor05");
   4682    ir_variable *SubFactor06 = body.make_temp(btype, "SubFactor06");
   4683    ir_variable *SubFactor07 = body.make_temp(btype, "SubFactor07");
   4684    ir_variable *SubFactor08 = body.make_temp(btype, "SubFactor08");
   4685    ir_variable *SubFactor09 = body.make_temp(btype, "SubFactor09");
   4686    ir_variable *SubFactor10 = body.make_temp(btype, "SubFactor10");
   4687    ir_variable *SubFactor11 = body.make_temp(btype, "SubFactor11");
   4688    ir_variable *SubFactor12 = body.make_temp(btype, "SubFactor12");
   4689    ir_variable *SubFactor13 = body.make_temp(btype, "SubFactor13");
   4690    ir_variable *SubFactor14 = body.make_temp(btype, "SubFactor14");
   4691    ir_variable *SubFactor15 = body.make_temp(btype, "SubFactor15");
   4692    ir_variable *SubFactor16 = body.make_temp(btype, "SubFactor16");
   4693    ir_variable *SubFactor17 = body.make_temp(btype, "SubFactor17");
   4694    ir_variable *SubFactor18 = body.make_temp(btype, "SubFactor18");
   4695 
   4696    body.emit(assign(SubFactor00, sub(mul(matrix_elt(m, 2, 2), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 2), matrix_elt(m, 2, 3)))));
   4697    body.emit(assign(SubFactor01, sub(mul(matrix_elt(m, 2, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 2, 3)))));
   4698    body.emit(assign(SubFactor02, sub(mul(matrix_elt(m, 2, 1), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 2, 2)))));
   4699    body.emit(assign(SubFactor03, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 3)))));
   4700    body.emit(assign(SubFactor04, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 2)))));
   4701    body.emit(assign(SubFactor05, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 1)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 1)))));
   4702    body.emit(assign(SubFactor06, sub(mul(matrix_elt(m, 1, 2), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 2), matrix_elt(m, 1, 3)))));
   4703    body.emit(assign(SubFactor07, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 3)))));
   4704    body.emit(assign(SubFactor08, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 2)))));
   4705    body.emit(assign(SubFactor09, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 3)))));
   4706    body.emit(assign(SubFactor10, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 2)))));
   4707    body.emit(assign(SubFactor11, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 3)))));
   4708    body.emit(assign(SubFactor12, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 1)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 1)))));
   4709    body.emit(assign(SubFactor13, sub(mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 2), matrix_elt(m, 1, 3)))));
   4710    body.emit(assign(SubFactor14, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 3)))));
   4711    body.emit(assign(SubFactor15, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)), mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 2)))));
   4712    body.emit(assign(SubFactor16, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 3)))));
   4713    body.emit(assign(SubFactor17, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 2)))));
   4714    body.emit(assign(SubFactor18, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 1)))));
   4715 
   4716    ir_variable *adj_0 = body.make_temp(btype == glsl_type::float_type ? glsl_type::vec4_type : glsl_type::dvec4_type, "adj_0");
   4717 
   4718    body.emit(assign(adj_0,
   4719                     add(sub(mul(matrix_elt(m, 1, 1), SubFactor00),
   4720                             mul(matrix_elt(m, 1, 2), SubFactor01)),
   4721                         mul(matrix_elt(m, 1, 3), SubFactor02)),
   4722                     WRITEMASK_X));
   4723    body.emit(assign(adj_0, neg(
   4724                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor00),
   4725                             mul(matrix_elt(m, 1, 2), SubFactor03)),
   4726                         mul(matrix_elt(m, 1, 3), SubFactor04))),
   4727                     WRITEMASK_Y));
   4728    body.emit(assign(adj_0,
   4729                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor01),
   4730                             mul(matrix_elt(m, 1, 1), SubFactor03)),
   4731                         mul(matrix_elt(m, 1, 3), SubFactor05)),
   4732                     WRITEMASK_Z));
   4733    body.emit(assign(adj_0, neg(
   4734                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor02),
   4735                             mul(matrix_elt(m, 1, 1), SubFactor04)),
   4736                         mul(matrix_elt(m, 1, 2), SubFactor05))),
   4737                     WRITEMASK_W));
   4738 
   4739    body.emit(ret(dot(array_ref(m, 0), adj_0)));
   4740 
   4741    return sig;
   4742 }
   4743 
   4744 ir_function_signature *
   4745 builtin_builder::_inverse_mat2(builtin_available_predicate avail, const glsl_type *type)
   4746 {
   4747    ir_variable *m = in_var(type, "m");
   4748    MAKE_SIG(type, avail, 1, m);
   4749 
   4750    ir_variable *adj = body.make_temp(type, "adj");
   4751    body.emit(assign(array_ref(adj, 0), matrix_elt(m, 1, 1), 1 << 0));
   4752    body.emit(assign(array_ref(adj, 0), neg(matrix_elt(m, 0, 1)), 1 << 1));
   4753    body.emit(assign(array_ref(adj, 1), neg(matrix_elt(m, 1, 0)), 1 << 0));
   4754    body.emit(assign(array_ref(adj, 1), matrix_elt(m, 0, 0), 1 << 1));
   4755 
   4756    ir_expression *det =
   4757       sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
   4758           mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1)));
   4759 
   4760    body.emit(ret(div(adj, det)));
   4761    return sig;
   4762 }
   4763 
   4764 ir_function_signature *
   4765 builtin_builder::_inverse_mat3(builtin_available_predicate avail, const glsl_type *type)
   4766 {
   4767    ir_variable *m = in_var(type, "m");
   4768    const glsl_type *btype = type->get_base_type();
   4769    MAKE_SIG(type, avail, 1, m);
   4770 
   4771    ir_variable *f11_22_21_12 = body.make_temp(btype, "f11_22_21_12");
   4772    ir_variable *f10_22_20_12 = body.make_temp(btype, "f10_22_20_12");
   4773    ir_variable *f10_21_20_11 = body.make_temp(btype, "f10_21_20_11");
   4774 
   4775    body.emit(assign(f11_22_21_12,
   4776                     sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)),
   4777                         mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 2)))));
   4778    body.emit(assign(f10_22_20_12,
   4779                     sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)),
   4780                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 2)))));
   4781    body.emit(assign(f10_21_20_11,
   4782                     sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)),
   4783                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 1)))));
   4784 
   4785    ir_variable *adj = body.make_temp(type, "adj");
   4786    body.emit(assign(array_ref(adj, 0), f11_22_21_12, WRITEMASK_X));
   4787    body.emit(assign(array_ref(adj, 1), neg(f10_22_20_12), WRITEMASK_X));
   4788    body.emit(assign(array_ref(adj, 2), f10_21_20_11, WRITEMASK_X));
   4789 
   4790    body.emit(assign(array_ref(adj, 0), neg(
   4791                     sub(mul(matrix_elt(m, 0, 1), matrix_elt(m, 2, 2)),
   4792                         mul(matrix_elt(m, 2, 1), matrix_elt(m, 0, 2)))),
   4793                     WRITEMASK_Y));
   4794    body.emit(assign(array_ref(adj, 1),
   4795                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 2, 2)),
   4796                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 0, 2))),
   4797                     WRITEMASK_Y));
   4798    body.emit(assign(array_ref(adj, 2), neg(
   4799                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 2, 1)),
   4800                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 0, 1)))),
   4801                     WRITEMASK_Y));
   4802 
   4803    body.emit(assign(array_ref(adj, 0),
   4804                     sub(mul(matrix_elt(m, 0, 1), matrix_elt(m, 1, 2)),
   4805                         mul(matrix_elt(m, 1, 1), matrix_elt(m, 0, 2))),
   4806                     WRITEMASK_Z));
   4807    body.emit(assign(array_ref(adj, 1), neg(
   4808                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 2)),
   4809                         mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 2)))),
   4810                     WRITEMASK_Z));
   4811    body.emit(assign(array_ref(adj, 2),
   4812                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
   4813                         mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1))),
   4814                     WRITEMASK_Z));
   4815 
   4816    ir_expression *det =
   4817       add(sub(mul(matrix_elt(m, 0, 0), f11_22_21_12),
   4818               mul(matrix_elt(m, 0, 1), f10_22_20_12)),
   4819           mul(matrix_elt(m, 0, 2), f10_21_20_11));
   4820 
   4821    body.emit(ret(div(adj, det)));
   4822 
   4823    return sig;
   4824 }
   4825 
   4826 ir_function_signature *
   4827 builtin_builder::_inverse_mat4(builtin_available_predicate avail, const glsl_type *type)
   4828 {
   4829    ir_variable *m = in_var(type, "m");
   4830    const glsl_type *btype = type->get_base_type();
   4831    MAKE_SIG(type, avail, 1, m);
   4832 
   4833    ir_variable *SubFactor00 = body.make_temp(btype, "SubFactor00");
   4834    ir_variable *SubFactor01 = body.make_temp(btype, "SubFactor01");
   4835    ir_variable *SubFactor02 = body.make_temp(btype, "SubFactor02");
   4836    ir_variable *SubFactor03 = body.make_temp(btype, "SubFactor03");
   4837    ir_variable *SubFactor04 = body.make_temp(btype, "SubFactor04");
   4838    ir_variable *SubFactor05 = body.make_temp(btype, "SubFactor05");
   4839    ir_variable *SubFactor06 = body.make_temp(btype, "SubFactor06");
   4840    ir_variable *SubFactor07 = body.make_temp(btype, "SubFactor07");
   4841    ir_variable *SubFactor08 = body.make_temp(btype, "SubFactor08");
   4842    ir_variable *SubFactor09 = body.make_temp(btype, "SubFactor09");
   4843    ir_variable *SubFactor10 = body.make_temp(btype, "SubFactor10");
   4844    ir_variable *SubFactor11 = body.make_temp(btype, "SubFactor11");
   4845    ir_variable *SubFactor12 = body.make_temp(btype, "SubFactor12");
   4846    ir_variable *SubFactor13 = body.make_temp(btype, "SubFactor13");
   4847    ir_variable *SubFactor14 = body.make_temp(btype, "SubFactor14");
   4848    ir_variable *SubFactor15 = body.make_temp(btype, "SubFactor15");
   4849    ir_variable *SubFactor16 = body.make_temp(btype, "SubFactor16");
   4850    ir_variable *SubFactor17 = body.make_temp(btype, "SubFactor17");
   4851    ir_variable *SubFactor18 = body.make_temp(btype, "SubFactor18");
   4852 
   4853    body.emit(assign(SubFactor00, sub(mul(matrix_elt(m, 2, 2), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 2), matrix_elt(m, 2, 3)))));
   4854    body.emit(assign(SubFactor01, sub(mul(matrix_elt(m, 2, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 2, 3)))));
   4855    body.emit(assign(SubFactor02, sub(mul(matrix_elt(m, 2, 1), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 2, 2)))));
   4856    body.emit(assign(SubFactor03, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 3)))));
   4857    body.emit(assign(SubFactor04, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 2)))));
   4858    body.emit(assign(SubFactor05, sub(mul(matrix_elt(m, 2, 0), matrix_elt(m, 3, 1)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 2, 1)))));
   4859    body.emit(assign(SubFactor06, sub(mul(matrix_elt(m, 1, 2), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 2), matrix_elt(m, 1, 3)))));
   4860    body.emit(assign(SubFactor07, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 3)))));
   4861    body.emit(assign(SubFactor08, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 2)))));
   4862    body.emit(assign(SubFactor09, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 3)))));
   4863    body.emit(assign(SubFactor10, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 2)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 2)))));
   4864    body.emit(assign(SubFactor11, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 3, 3)), mul(matrix_elt(m, 3, 1), matrix_elt(m, 1, 3)))));
   4865    body.emit(assign(SubFactor12, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 3, 1)), mul(matrix_elt(m, 3, 0), matrix_elt(m, 1, 1)))));
   4866    body.emit(assign(SubFactor13, sub(mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 2), matrix_elt(m, 1, 3)))));
   4867    body.emit(assign(SubFactor14, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 3)))));
   4868    body.emit(assign(SubFactor15, sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)), mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 2)))));
   4869    body.emit(assign(SubFactor16, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 3)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 3)))));
   4870    body.emit(assign(SubFactor17, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 2)))));
   4871    body.emit(assign(SubFactor18, sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)), mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 1)))));
   4872 
   4873    ir_variable *adj = body.make_temp(btype == glsl_type::float_type ? glsl_type::mat4_type : glsl_type::dmat4_type, "adj");
   4874    body.emit(assign(array_ref(adj, 0),
   4875                     add(sub(mul(matrix_elt(m, 1, 1), SubFactor00),
   4876                             mul(matrix_elt(m, 1, 2), SubFactor01)),
   4877                         mul(matrix_elt(m, 1, 3), SubFactor02)),
   4878                     WRITEMASK_X));
   4879    body.emit(assign(array_ref(adj, 1), neg(
   4880                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor00),
   4881                             mul(matrix_elt(m, 1, 2), SubFactor03)),
   4882                         mul(matrix_elt(m, 1, 3), SubFactor04))),
   4883                     WRITEMASK_X));
   4884    body.emit(assign(array_ref(adj, 2),
   4885                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor01),
   4886                             mul(matrix_elt(m, 1, 1), SubFactor03)),
   4887                         mul(matrix_elt(m, 1, 3), SubFactor05)),
   4888                     WRITEMASK_X));
   4889    body.emit(assign(array_ref(adj, 3), neg(
   4890                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor02),
   4891                             mul(matrix_elt(m, 1, 1), SubFactor04)),
   4892                         mul(matrix_elt(m, 1, 2), SubFactor05))),
   4893                     WRITEMASK_X));
   4894 
   4895    body.emit(assign(array_ref(adj, 0), neg(
   4896                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor00),
   4897                             mul(matrix_elt(m, 0, 2), SubFactor01)),
   4898                         mul(matrix_elt(m, 0, 3), SubFactor02))),
   4899                     WRITEMASK_Y));
   4900    body.emit(assign(array_ref(adj, 1),
   4901                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor00),
   4902                             mul(matrix_elt(m, 0, 2), SubFactor03)),
   4903                         mul(matrix_elt(m, 0, 3), SubFactor04)),
   4904                     WRITEMASK_Y));
   4905    body.emit(assign(array_ref(adj, 2), neg(
   4906                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor01),
   4907                             mul(matrix_elt(m, 0, 1), SubFactor03)),
   4908                         mul(matrix_elt(m, 0, 3), SubFactor05))),
   4909                     WRITEMASK_Y));
   4910    body.emit(assign(array_ref(adj, 3),
   4911                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor02),
   4912                             mul(matrix_elt(m, 0, 1), SubFactor04)),
   4913                         mul(matrix_elt(m, 0, 2), SubFactor05)),
   4914                     WRITEMASK_Y));
   4915 
   4916    body.emit(assign(array_ref(adj, 0),
   4917                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor06),
   4918                             mul(matrix_elt(m, 0, 2), SubFactor07)),
   4919                         mul(matrix_elt(m, 0, 3), SubFactor08)),
   4920                     WRITEMASK_Z));
   4921    body.emit(assign(array_ref(adj, 1), neg(
   4922                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor06),
   4923                             mul(matrix_elt(m, 0, 2), SubFactor09)),
   4924                         mul(matrix_elt(m, 0, 3), SubFactor10))),
   4925                     WRITEMASK_Z));
   4926    body.emit(assign(array_ref(adj, 2),
   4927                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor11),
   4928                             mul(matrix_elt(m, 0, 1), SubFactor09)),
   4929                         mul(matrix_elt(m, 0, 3), SubFactor12)),
   4930                     WRITEMASK_Z));
   4931    body.emit(assign(array_ref(adj, 3), neg(
   4932                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor08),
   4933                             mul(matrix_elt(m, 0, 1), SubFactor10)),
   4934                         mul(matrix_elt(m, 0, 2), SubFactor12))),
   4935                     WRITEMASK_Z));
   4936 
   4937    body.emit(assign(array_ref(adj, 0), neg(
   4938                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor13),
   4939                             mul(matrix_elt(m, 0, 2), SubFactor14)),
   4940                         mul(matrix_elt(m, 0, 3), SubFactor15))),
   4941                     WRITEMASK_W));
   4942    body.emit(assign(array_ref(adj, 1),
   4943                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor13),
   4944                             mul(matrix_elt(m, 0, 2), SubFactor16)),
   4945                         mul(matrix_elt(m, 0, 3), SubFactor17)),
   4946                     WRITEMASK_W));
   4947    body.emit(assign(array_ref(adj, 2), neg(
   4948                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor14),
   4949                             mul(matrix_elt(m, 0, 1), SubFactor16)),
   4950                         mul(matrix_elt(m, 0, 3), SubFactor18))),
   4951                     WRITEMASK_W));
   4952    body.emit(assign(array_ref(adj, 3),
   4953                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor15),
   4954                             mul(matrix_elt(m, 0, 1), SubFactor17)),
   4955                         mul(matrix_elt(m, 0, 2), SubFactor18)),
   4956                     WRITEMASK_W));
   4957 
   4958    ir_expression *det =
   4959       add(mul(matrix_elt(m, 0, 0), matrix_elt(adj, 0, 0)),
   4960           add(mul(matrix_elt(m, 0, 1), matrix_elt(adj, 1, 0)),
   4961               add(mul(matrix_elt(m, 0, 2), matrix_elt(adj, 2, 0)),
   4962                   mul(matrix_elt(m, 0, 3), matrix_elt(adj, 3, 0)))));
   4963 
   4964    body.emit(ret(div(adj, det)));
   4965 
   4966    return sig;
   4967 }
   4968 
   4969 
   4970 ir_function_signature *
   4971 builtin_builder::_lessThan(builtin_available_predicate avail,
   4972                            const glsl_type *type)
   4973 {
   4974    return binop(avail, ir_binop_less,
   4975                 glsl_type::bvec(type->vector_elements), type, type);
   4976 }
   4977 
   4978 ir_function_signature *
   4979 builtin_builder::_lessThanEqual(builtin_available_predicate avail,
   4980                                 const glsl_type *type)
   4981 {
   4982    return binop(avail, ir_binop_gequal,
   4983                 glsl_type::bvec(type->vector_elements), type, type,
   4984                 true);
   4985 }
   4986 
   4987 ir_function_signature *
   4988 builtin_builder::_greaterThan(builtin_available_predicate avail,
   4989                               const glsl_type *type)
   4990 {
   4991    return binop(avail, ir_binop_less,
   4992                 glsl_type::bvec(type->vector_elements), type, type,
   4993                 true);
   4994 }
   4995 
   4996 ir_function_signature *
   4997 builtin_builder::_greaterThanEqual(builtin_available_predicate avail,
   4998                                    const glsl_type *type)
   4999 {
   5000    return binop(avail, ir_binop_gequal,
   5001                 glsl_type::bvec(type->vector_elements), type, type);
   5002 }
   5003 
   5004 ir_function_signature *
   5005 builtin_builder::_equal(builtin_available_predicate avail,
   5006                         const glsl_type *type)
   5007 {
   5008    return binop(avail, ir_binop_equal,
   5009                 glsl_type::bvec(type->vector_elements), type, type);
   5010 }
   5011 
   5012 ir_function_signature *
   5013 builtin_builder::_notEqual(builtin_available_predicate avail,
   5014                            const glsl_type *type)
   5015 {
   5016    return binop(avail, ir_binop_nequal,
   5017                 glsl_type::bvec(type->vector_elements), type, type);
   5018 }
   5019 
   5020 ir_function_signature *
   5021 builtin_builder::_any(const glsl_type *type)
   5022 {
   5023    ir_variable *v = in_var(type, "v");
   5024    MAKE_SIG(glsl_type::bool_type, always_available, 1, v);
   5025 
   5026    const unsigned vec_elem = v->type->vector_elements;
   5027    body.emit(ret(expr(ir_binop_any_nequal, v, imm(false, vec_elem))));
   5028 
   5029    return sig;
   5030 }
   5031 
   5032 ir_function_signature *
   5033 builtin_builder::_all(const glsl_type *type)
   5034 {
   5035    ir_variable *v = in_var(type, "v");
   5036    MAKE_SIG(glsl_type::bool_type, always_available, 1, v);
   5037 
   5038    const unsigned vec_elem = v->type->vector_elements;
   5039    body.emit(ret(expr(ir_binop_all_equal, v, imm(true, vec_elem))));
   5040 
   5041    return sig;
   5042 }
   5043 
   5044 UNOP(not, ir_unop_logic_not, always_available)
   5045 
   5046 static bool
   5047 has_lod(const glsl_type *sampler_type)
   5048 {
   5049    assert(sampler_type->is_sampler());
   5050 
   5051    switch (sampler_type->sampler_dimensionality) {
   5052    case GLSL_SAMPLER_DIM_RECT:
   5053    case GLSL_SAMPLER_DIM_BUF:
   5054    case GLSL_SAMPLER_DIM_MS:
   5055       return false;
   5056    default:
   5057       return true;
   5058    }
   5059 }
   5060 
   5061 ir_function_signature *
   5062 builtin_builder::_textureSize(builtin_available_predicate avail,
   5063                               const glsl_type *return_type,
   5064                               const glsl_type *sampler_type)
   5065 {
   5066    ir_variable *s = in_var(sampler_type, "sampler");
   5067    /* The sampler always exists; add optional lod later. */
   5068    MAKE_SIG(return_type, avail, 1, s);
   5069 
   5070    ir_texture *tex = new(mem_ctx) ir_texture(ir_txs);
   5071    tex->set_sampler(new(mem_ctx) ir_dereference_variable(s), return_type);
   5072 
   5073    if (has_lod(sampler_type)) {
   5074       ir_variable *lod = in_var(glsl_type::int_type, "lod");
   5075       sig->parameters.push_tail(lod);
   5076       tex->lod_info.lod = var_ref(lod);
   5077    } else {
   5078       tex->lod_info.lod = imm(0u);
   5079    }
   5080 
   5081    body.emit(ret(tex));
   5082 
   5083    return sig;
   5084 }
   5085 
   5086 ir_function_signature *
   5087 builtin_builder::_textureSamples(builtin_available_predicate avail,
   5088                                  const glsl_type *sampler_type)
   5089 {
   5090    ir_variable *s = in_var(sampler_type, "sampler");
   5091    MAKE_SIG(glsl_type::int_type, avail, 1, s);
   5092 
   5093    ir_texture *tex = new(mem_ctx) ir_texture(ir_texture_samples);
   5094    tex->set_sampler(new(mem_ctx) ir_dereference_variable(s), glsl_type::int_type);
   5095    body.emit(ret(tex));
   5096 
   5097    return sig;
   5098 }
   5099 
   5100 ir_function_signature *
   5101 builtin_builder::_texture(ir_texture_opcode opcode,
   5102                           builtin_available_predicate avail,
   5103                           const glsl_type *return_type,
   5104                           const glsl_type *sampler_type,
   5105                           const glsl_type *coord_type,
   5106                           int flags)
   5107 {
   5108    ir_variable *s = in_var(sampler_type, "sampler");
   5109    ir_variable *P = in_var(coord_type, "P");
   5110    /* The sampler and coordinate always exist; add optional parameters later. */
   5111    MAKE_SIG(return_type, avail, 2, s, P);
   5112 
   5113    ir_texture *tex = new(mem_ctx) ir_texture(opcode);
   5114    tex->set_sampler(var_ref(s), return_type);
   5115 
   5116    const int coord_size = sampler_type->coordinate_components();
   5117 
   5118    if (coord_size == coord_type->vector_elements) {
   5119       tex->coordinate = var_ref(P);
   5120    } else {
   5121       /* The incoming coordinate also has the projector or shadow comparator,
   5122        * so we need to swizzle those away.
   5123        */
   5124       tex->coordinate = swizzle_for_size(P, coord_size);
   5125    }
   5126 
   5127    /* The projector is always in the last component. */
   5128    if (flags & TEX_PROJECT)
   5129       tex->projector = swizzle(P, coord_type->vector_elements - 1, 1);
   5130 
   5131    if (sampler_type->sampler_shadow) {
   5132       if (opcode == ir_tg4) {
   5133          /* gather has refz as a separate parameter, immediately after the
   5134           * coordinate
   5135           */
   5136          ir_variable *refz = in_var(glsl_type::float_type, "refz");
   5137          sig->parameters.push_tail(refz);
   5138          tex->shadow_comparator = var_ref(refz);
   5139       } else {
   5140          /* The shadow comparator is normally in the Z component, but a few types
   5141           * have sufficiently large coordinates that it's in W.
   5142           */
   5143          tex->shadow_comparator = swizzle(P, MAX2(coord_size, SWIZZLE_Z), 1);
   5144       }
   5145    }
   5146 
   5147    if (opcode == ir_txl) {
   5148       ir_variable *lod = in_var(glsl_type::float_type, "lod");
   5149       sig->parameters.push_tail(lod);
   5150       tex->lod_info.lod = var_ref(lod);
   5151    } else if (opcode == ir_txd) {
   5152       int grad_size = coord_size - (sampler_type->sampler_array ? 1 : 0);
   5153       ir_variable *dPdx = in_var(glsl_type::vec(grad_size), "dPdx");
   5154       ir_variable *dPdy = in_var(glsl_type::vec(grad_size), "dPdy");
   5155       sig->parameters.push_tail(dPdx);
   5156       sig->parameters.push_tail(dPdy);
   5157       tex->lod_info.grad.dPdx = var_ref(dPdx);
   5158       tex->lod_info.grad.dPdy = var_ref(dPdy);
   5159    }
   5160 
   5161    if (flags & (TEX_OFFSET | TEX_OFFSET_NONCONST)) {
   5162       int offset_size = coord_size - (sampler_type->sampler_array ? 1 : 0);
   5163       ir_variable *offset =
   5164          new(mem_ctx) ir_variable(glsl_type::ivec(offset_size), "offset",
   5165                                   (flags & TEX_OFFSET) ? ir_var_const_in : ir_var_function_in);
   5166       sig->parameters.push_tail(offset);
   5167       tex->offset = var_ref(offset);
   5168    }
   5169 
   5170    if (flags & TEX_OFFSET_ARRAY) {
   5171       ir_variable *offsets =
   5172          new(mem_ctx) ir_variable(glsl_type::get_array_instance(glsl_type::ivec2_type, 4),
   5173                                   "offsets", ir_var_const_in);
   5174       sig->parameters.push_tail(offsets);
   5175       tex->offset = var_ref(offsets);
   5176    }
   5177 
   5178    if (opcode == ir_tg4) {
   5179       if (flags & TEX_COMPONENT) {
   5180          ir_variable *component =
   5181             new(mem_ctx) ir_variable(glsl_type::int_type, "comp", ir_var_const_in);
   5182          sig->parameters.push_tail(component);
   5183          tex->lod_info.component = var_ref(component);
   5184       }
   5185       else {
   5186          tex->lod_info.component = imm(0);
   5187       }
   5188    }
   5189 
   5190    /* The "bias" parameter comes /after/ the "offset" parameter, which is
   5191     * inconsistent with both textureLodOffset and textureGradOffset.
   5192     */
   5193    if (opcode == ir_txb) {
   5194       ir_variable *bias = in_var(glsl_type::float_type, "bias");
   5195       sig->parameters.push_tail(bias);
   5196       tex->lod_info.bias = var_ref(bias);
   5197    }
   5198 
   5199    body.emit(ret(tex));
   5200 
   5201    return sig;
   5202 }
   5203 
   5204 ir_function_signature *
   5205 builtin_builder::_textureCubeArrayShadow(builtin_available_predicate avail,
   5206                                          const glsl_type *sampler_type)
   5207 {
   5208    ir_variable *s = in_var(sampler_type, "sampler");
   5209    ir_variable *P = in_var(glsl_type::vec4_type, "P");
   5210    ir_variable *compare = in_var(glsl_type::float_type, "compare");
   5211    MAKE_SIG(glsl_type::float_type, avail, 3, s, P, compare);
   5212 
   5213    ir_texture *tex = new(mem_ctx) ir_texture(ir_tex);
   5214    tex->set_sampler(var_ref(s), glsl_type::float_type);
   5215 
   5216    tex->coordinate = var_ref(P);
   5217    tex->shadow_comparator = var_ref(compare);
   5218 
   5219    body.emit(ret(tex));
   5220 
   5221    return sig;
   5222 }
   5223 
   5224 ir_function_signature *
   5225 builtin_builder::_texelFetch(builtin_available_predicate avail,
   5226                              const glsl_type *return_type,
   5227                              const glsl_type *sampler_type,
   5228                              const glsl_type *coord_type,
   5229                              const glsl_type *offset_type)
   5230 {
   5231    ir_variable *s = in_var(sampler_type, "sampler");
   5232    ir_variable *P = in_var(coord_type, "P");
   5233    /* The sampler and coordinate always exist; add optional parameters later. */
   5234    MAKE_SIG(return_type, avail, 2, s, P);
   5235 
   5236    ir_texture *tex = new(mem_ctx) ir_texture(ir_txf);
   5237    tex->coordinate = var_ref(P);
   5238    tex->set_sampler(var_ref(s), return_type);
   5239 
   5240    if (sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
   5241       ir_variable *sample = in_var(glsl_type::int_type, "sample");
   5242       sig->parameters.push_tail(sample);
   5243       tex->lod_info.sample_index = var_ref(sample);
   5244       tex->op = ir_txf_ms;
   5245    } else if (has_lod(sampler_type)) {
   5246       ir_variable *lod = in_var(glsl_type::int_type, "lod");
   5247       sig->parameters.push_tail(lod);
   5248       tex->lod_info.lod = var_ref(lod);
   5249    } else {
   5250       tex->lod_info.lod = imm(0u);
   5251    }
   5252 
   5253    if (offset_type != NULL) {
   5254       ir_variable *offset =
   5255          new(mem_ctx) ir_variable(offset_type, "offset", ir_var_const_in);
   5256       sig->parameters.push_tail(offset);
   5257       tex->offset = var_ref(offset);
   5258    }
   5259 
   5260    body.emit(ret(tex));
   5261 
   5262    return sig;
   5263 }
   5264 
   5265 ir_function_signature *
   5266 builtin_builder::_EmitVertex()
   5267 {
   5268    MAKE_SIG(glsl_type::void_type, gs_only, 0);
   5269 
   5270    ir_rvalue *stream = new(mem_ctx) ir_constant(0, 1);
   5271    body.emit(new(mem_ctx) ir_emit_vertex(stream));
   5272 
   5273    return sig;
   5274 }
   5275 
   5276 ir_function_signature *
   5277 builtin_builder::_EmitStreamVertex(builtin_available_predicate avail,
   5278                                    const glsl_type *stream_type)
   5279 {
   5280    /* Section 8.12 (Geometry Shader Functions) of the GLSL 4.0 spec says:
   5281     *
   5282     *     "Emit the current values of output variables to the current output
   5283     *     primitive on stream stream. The argument to stream must be a constant
   5284     *     integral expression."
   5285     */
   5286    ir_variable *stream =
   5287       new(mem_ctx) ir_variable(stream_type, "stream", ir_var_const_in);
   5288 
   5289    MAKE_SIG(glsl_type::void_type, avail, 1, stream);
   5290 
   5291    body.emit(new(mem_ctx) ir_emit_vertex(var_ref(stream)));
   5292 
   5293    return sig;
   5294 }
   5295 
   5296 ir_function_signature *
   5297 builtin_builder::_EndPrimitive()
   5298 {
   5299    MAKE_SIG(glsl_type::void_type, gs_only, 0);
   5300 
   5301    ir_rvalue *stream = new(mem_ctx) ir_constant(0, 1);
   5302    body.emit(new(mem_ctx) ir_end_primitive(stream));
   5303 
   5304    return sig;
   5305 }
   5306 
   5307 ir_function_signature *
   5308 builtin_builder::_EndStreamPrimitive(builtin_available_predicate avail,
   5309                                      const glsl_type *stream_type)
   5310 {
   5311    /* Section 8.12 (Geometry Shader Functions) of the GLSL 4.0 spec says:
   5312     *
   5313     *     "Completes the current output primitive on stream stream and starts
   5314     *     a new one. The argument to stream must be a constant integral
   5315     *     expression."
   5316     */
   5317    ir_variable *stream =
   5318       new(mem_ctx) ir_variable(stream_type, "stream", ir_var_const_in);
   5319 
   5320    MAKE_SIG(glsl_type::void_type, avail, 1, stream);
   5321 
   5322    body.emit(new(mem_ctx) ir_end_primitive(var_ref(stream)));
   5323 
   5324    return sig;
   5325 }
   5326 
   5327 ir_function_signature *
   5328 builtin_builder::_barrier()
   5329 {
   5330    MAKE_SIG(glsl_type::void_type, barrier_supported, 0);
   5331 
   5332    body.emit(new(mem_ctx) ir_barrier());
   5333    return sig;
   5334 }
   5335 
   5336 ir_function_signature *
   5337 builtin_builder::_textureQueryLod(builtin_available_predicate avail,
   5338                                   const glsl_type *sampler_type,
   5339                                   const glsl_type *coord_type)
   5340 {
   5341    ir_variable *s = in_var(sampler_type, "sampler");
   5342    ir_variable *coord = in_var(coord_type, "coord");
   5343    /* The sampler and coordinate always exist; add optional parameters later. */
   5344    MAKE_SIG(glsl_type::vec2_type, avail, 2, s, coord);
   5345 
   5346    ir_texture *tex = new(mem_ctx) ir_texture(ir_lod);
   5347    tex->coordinate = var_ref(coord);
   5348    tex->set_sampler(var_ref(s), glsl_type::vec2_type);
   5349 
   5350    body.emit(ret(tex));
   5351 
   5352    return sig;
   5353 }
   5354 
   5355 ir_function_signature *
   5356 builtin_builder::_textureQueryLevels(builtin_available_predicate avail,
   5357                                      const glsl_type *sampler_type)
   5358 {
   5359    ir_variable *s = in_var(sampler_type, "sampler");
   5360    const glsl_type *return_type = glsl_type::int_type;
   5361    MAKE_SIG(return_type, avail, 1, s);
   5362 
   5363    ir_texture *tex = new(mem_ctx) ir_texture(ir_query_levels);
   5364    tex->set_sampler(var_ref(s), return_type);
   5365 
   5366    body.emit(ret(tex));
   5367 
   5368    return sig;
   5369 }
   5370 
   5371 ir_function_signature *
   5372 builtin_builder::_textureSamplesIdentical(builtin_available_predicate avail,
   5373                                           const glsl_type *sampler_type,
   5374                                           const glsl_type *coord_type)
   5375 {
   5376    ir_variable *s = in_var(sampler_type, "sampler");
   5377    ir_variable *P = in_var(coord_type, "P");
   5378    const glsl_type *return_type = glsl_type::bool_type;
   5379    MAKE_SIG(return_type, avail, 2, s, P);
   5380 
   5381    ir_texture *tex = new(mem_ctx) ir_texture(ir_samples_identical);
   5382    tex->coordinate = var_ref(P);
   5383    tex->set_sampler(var_ref(s), return_type);
   5384 
   5385    body.emit(ret(tex));
   5386 
   5387    return sig;
   5388 }
   5389 
   5390 UNOP(dFdx, ir_unop_dFdx, fs_oes_derivatives)
   5391 UNOP(dFdxCoarse, ir_unop_dFdx_coarse, fs_derivative_control)
   5392 UNOP(dFdxFine, ir_unop_dFdx_fine, fs_derivative_control)
   5393 UNOP(dFdy, ir_unop_dFdy, fs_oes_derivatives)
   5394 UNOP(dFdyCoarse, ir_unop_dFdy_coarse, fs_derivative_control)
   5395 UNOP(dFdyFine, ir_unop_dFdy_fine, fs_derivative_control)
   5396 
   5397 ir_function_signature *
   5398 builtin_builder::_fwidth(const glsl_type *type)
   5399 {
   5400    ir_variable *p = in_var(type, "p");
   5401    MAKE_SIG(type, fs_oes_derivatives, 1, p);
   5402 
   5403    body.emit(ret(add(abs(expr(ir_unop_dFdx, p)), abs(expr(ir_unop_dFdy, p)))));
   5404 
   5405    return sig;
   5406 }
   5407 
   5408 ir_function_signature *
   5409 builtin_builder::_fwidthCoarse(const glsl_type *type)
   5410 {
   5411    ir_variable *p = in_var(type, "p");
   5412    MAKE_SIG(type, fs_derivative_control, 1, p);
   5413 
   5414    body.emit(ret(add(abs(expr(ir_unop_dFdx_coarse, p)),
   5415                      abs(expr(ir_unop_dFdy_coarse, p)))));
   5416 
   5417    return sig;
   5418 }
   5419 
   5420 ir_function_signature *
   5421 builtin_builder::_fwidthFine(const glsl_type *type)
   5422 {
   5423    ir_variable *p = in_var(type, "p");
   5424    MAKE_SIG(type, fs_derivative_control, 1, p);
   5425 
   5426    body.emit(ret(add(abs(expr(ir_unop_dFdx_fine, p)),
   5427                      abs(expr(ir_unop_dFdy_fine, p)))));
   5428 
   5429    return sig;
   5430 }
   5431 
   5432 ir_function_signature *
   5433 builtin_builder::_noise1(const glsl_type *type)
   5434 {
   5435    return unop(v110, ir_unop_noise, glsl_type::float_type, type);
   5436 }
   5437 
   5438 ir_function_signature *
   5439 builtin_builder::_noise2(const glsl_type *type)
   5440 {
   5441    ir_variable *p = in_var(type, "p");
   5442    MAKE_SIG(glsl_type::vec2_type, v110, 1, p);
   5443 
   5444    ir_constant_data b_offset;
   5445    b_offset.f[0] = 601.0f;
   5446    b_offset.f[1] = 313.0f;
   5447    b_offset.f[2] = 29.0f;
   5448    b_offset.f[3] = 277.0f;
   5449 
   5450    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
   5451    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
   5452    ir_variable *t = body.make_temp(glsl_type::vec2_type,  "t");
   5453    body.emit(assign(a, expr(ir_unop_noise, p)));
   5454    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, b_offset)))));
   5455    body.emit(assign(t, a, WRITEMASK_X));
   5456    body.emit(assign(t, b, WRITEMASK_Y));
   5457    body.emit(ret(t));
   5458 
   5459    return sig;
   5460 }
   5461 
   5462 ir_function_signature *
   5463 builtin_builder::_noise3(const glsl_type *type)
   5464 {
   5465    ir_variable *p = in_var(type, "p");
   5466    MAKE_SIG(glsl_type::vec3_type, v110, 1, p);
   5467 
   5468    ir_constant_data b_offset;
   5469    b_offset.f[0] = 601.0f;
   5470    b_offset.f[1] = 313.0f;
   5471    b_offset.f[2] = 29.0f;
   5472    b_offset.f[3] = 277.0f;
   5473 
   5474    ir_constant_data c_offset;
   5475    c_offset.f[0] = 1559.0f;
   5476    c_offset.f[1] = 113.0f;
   5477    c_offset.f[2] = 1861.0f;
   5478    c_offset.f[3] = 797.0f;
   5479 
   5480    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
   5481    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
   5482    ir_variable *c = body.make_temp(glsl_type::float_type, "c");
   5483    ir_variable *t = body.make_temp(glsl_type::vec3_type,  "t");
   5484    body.emit(assign(a, expr(ir_unop_noise, p)));
   5485    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, b_offset)))));
   5486    body.emit(assign(c, expr(ir_unop_noise, add(p, imm(type, c_offset)))));
   5487    body.emit(assign(t, a, WRITEMASK_X));
   5488    body.emit(assign(t, b, WRITEMASK_Y));
   5489    body.emit(assign(t, c, WRITEMASK_Z));
   5490    body.emit(ret(t));
   5491 
   5492    return sig;
   5493 }
   5494 
   5495 ir_function_signature *
   5496 builtin_builder::_noise4(const glsl_type *type)
   5497 {
   5498    ir_variable *p = in_var(type, "p");
   5499    MAKE_SIG(glsl_type::vec4_type, v110, 1, p);
   5500 
   5501    ir_variable *_p = body.make_temp(type, "_p");
   5502 
   5503    ir_constant_data p_offset;
   5504    p_offset.f[0] = 1559.0f;
   5505    p_offset.f[1] = 113.0f;
   5506    p_offset.f[2] = 1861.0f;
   5507    p_offset.f[3] = 797.0f;
   5508 
   5509    body.emit(assign(_p, add(p, imm(type, p_offset))));
   5510 
   5511    ir_constant_data offset;
   5512    offset.f[0] = 601.0f;
   5513    offset.f[1] = 313.0f;
   5514    offset.f[2] = 29.0f;
   5515    offset.f[3] = 277.0f;
   5516 
   5517    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
   5518    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
   5519    ir_variable *c = body.make_temp(glsl_type::float_type, "c");
   5520    ir_variable *d = body.make_temp(glsl_type::float_type, "d");
   5521    ir_variable *t = body.make_temp(glsl_type::vec4_type,  "t");
   5522    body.emit(assign(a, expr(ir_unop_noise, p)));
   5523    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, offset)))));
   5524    body.emit(assign(c, expr(ir_unop_noise, _p)));
   5525    body.emit(assign(d, expr(ir_unop_noise, add(_p, imm(type, offset)))));
   5526    body.emit(assign(t, a, WRITEMASK_X));
   5527    body.emit(assign(t, b, WRITEMASK_Y));
   5528    body.emit(assign(t, c, WRITEMASK_Z));
   5529    body.emit(assign(t, d, WRITEMASK_W));
   5530    body.emit(ret(t));
   5531 
   5532    return sig;
   5533 }
   5534 
   5535 ir_function_signature *
   5536 builtin_builder::_bitfieldExtract(const glsl_type *type)
   5537 {
   5538    bool is_uint = type->base_type == GLSL_TYPE_UINT;
   5539    ir_variable *value  = in_var(type, "value");
   5540    ir_variable *offset = in_var(glsl_type::int_type, "offset");
   5541    ir_variable *bits   = in_var(glsl_type::int_type, "bits");
   5542    MAKE_SIG(type, gpu_shader5_or_es31_or_integer_functions, 3, value, offset,
   5543             bits);
   5544 
   5545    operand cast_offset = is_uint ? i2u(offset) : operand(offset);
   5546    operand cast_bits = is_uint ? i2u(bits) : operand(bits);
   5547 
   5548    body.emit(ret(expr(ir_triop_bitfield_extract, value,
   5549       swizzle(cast_offset, SWIZZLE_XXXX, type->vector_elements),
   5550       swizzle(cast_bits, SWIZZLE_XXXX, type->vector_elements))));
   5551 
   5552    return sig;
   5553 }
   5554 
   5555 ir_function_signature *
   5556 builtin_builder::_bitfieldInsert(const glsl_type *type)
   5557 {
   5558    bool is_uint = type->base_type == GLSL_TYPE_UINT;
   5559    ir_variable *base   = in_var(type, "base");
   5560    ir_variable *insert = in_var(type, "insert");
   5561    ir_variable *offset = in_var(glsl_type::int_type, "offset");
   5562    ir_variable *bits   = in_var(glsl_type::int_type, "bits");
   5563    MAKE_SIG(type, gpu_shader5_or_es31_or_integer_functions, 4, base, insert,
   5564             offset, bits);
   5565 
   5566    operand cast_offset = is_uint ? i2u(offset) : operand(offset);
   5567    operand cast_bits = is_uint ? i2u(bits) : operand(bits);
   5568 
   5569    body.emit(ret(bitfield_insert(base, insert,
   5570       swizzle(cast_offset, SWIZZLE_XXXX, type->vector_elements),
   5571       swizzle(cast_bits, SWIZZLE_XXXX, type->vector_elements))));
   5572 
   5573    return sig;
   5574 }
   5575 
   5576 UNOP(bitfieldReverse, ir_unop_bitfield_reverse, gpu_shader5_or_es31_or_integer_functions)
   5577 
   5578 ir_function_signature *
   5579 builtin_builder::_bitCount(const glsl_type *type)
   5580 {
   5581    return unop(gpu_shader5_or_es31_or_integer_functions, ir_unop_bit_count,
   5582                glsl_type::ivec(type->vector_elements), type);
   5583 }
   5584 
   5585 ir_function_signature *
   5586 builtin_builder::_findLSB(const glsl_type *type)
   5587 {
   5588    return unop(gpu_shader5_or_es31_or_integer_functions, ir_unop_find_lsb,
   5589                glsl_type::ivec(type->vector_elements), type);
   5590 }
   5591 
   5592 ir_function_signature *
   5593 builtin_builder::_findMSB(const glsl_type *type)
   5594 {
   5595    return unop(gpu_shader5_or_es31_or_integer_functions, ir_unop_find_msb,
   5596                glsl_type::ivec(type->vector_elements), type);
   5597 }
   5598 
   5599 ir_function_signature *
   5600 builtin_builder::_fma(builtin_available_predicate avail, const glsl_type *type)
   5601 {
   5602    ir_variable *a = in_var(type, "a");
   5603    ir_variable *b = in_var(type, "b");
   5604    ir_variable *c = in_var(type, "c");
   5605    MAKE_SIG(type, avail, 3, a, b, c);
   5606 
   5607    body.emit(ret(ir_builder::fma(a, b, c)));
   5608 
   5609    return sig;
   5610 }
   5611 
   5612 ir_function_signature *
   5613 builtin_builder::_ldexp(const glsl_type *x_type, const glsl_type *exp_type)
   5614 {
   5615    return binop(x_type->is_double() ? fp64 : gpu_shader5_or_es31_or_integer_functions,
   5616                 ir_binop_ldexp, x_type, x_type, exp_type);
   5617 }
   5618 
   5619 ir_function_signature *
   5620 builtin_builder::_dfrexp(const glsl_type *x_type, const glsl_type *exp_type)
   5621 {
   5622    ir_variable *x = in_var(x_type, "x");
   5623    ir_variable *exponent = out_var(exp_type, "exp");
   5624    MAKE_SIG(x_type, fp64, 2, x, exponent);
   5625 
   5626    body.emit(assign(exponent, expr(ir_unop_frexp_exp, x)));
   5627 
   5628    body.emit(ret(expr(ir_unop_frexp_sig, x)));
   5629    return sig;
   5630 }
   5631 
   5632 ir_function_signature *
   5633 builtin_builder::_frexp(const glsl_type *x_type, const glsl_type *exp_type)
   5634 {
   5635    ir_variable *x = in_var(x_type, "x");
   5636    ir_variable *exponent = out_var(exp_type, "exp");
   5637    MAKE_SIG(x_type, gpu_shader5_or_es31_or_integer_functions, 2, x, exponent);
   5638 
   5639    const unsigned vec_elem = x_type->vector_elements;
   5640    const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
   5641    const glsl_type *uvec = glsl_type::get_instance(GLSL_TYPE_UINT, vec_elem, 1);
   5642 
   5643    /* Single-precision floating-point values are stored as
   5644     *   1 sign bit;
   5645     *   8 exponent bits;
   5646     *   23 mantissa bits.
   5647     *
   5648     * An exponent shift of 23 will shift the mantissa out, leaving only the
   5649     * exponent and sign bit (which itself may be zero, if the absolute value
   5650     * was taken before the bitcast and shift.
   5651     */
   5652    ir_constant *exponent_shift = imm(23);
   5653    ir_constant *exponent_bias = imm(-126, vec_elem);
   5654 
   5655    ir_constant *sign_mantissa_mask = imm(0x807fffffu, vec_elem);
   5656 
   5657    /* Exponent of floating-point values in the range [0.5, 1.0). */
   5658    ir_constant *exponent_value = imm(0x3f000000u, vec_elem);
   5659 
   5660    ir_variable *is_not_zero = body.make_temp(bvec, "is_not_zero");
   5661    body.emit(assign(is_not_zero, nequal(abs(x), imm(0.0f, vec_elem))));
   5662 
   5663    /* Since abs(x) ensures that the sign bit is zero, we don't need to bitcast
   5664     * to unsigned integers to ensure that 1 bits aren't shifted in.
   5665     */
   5666    body.emit(assign(exponent, rshift(bitcast_f2i(abs(x)), exponent_shift)));
   5667    body.emit(assign(exponent, add(exponent, csel(is_not_zero, exponent_bias,
   5668                                                      imm(0, vec_elem)))));
   5669 
   5670    ir_variable *bits = body.make_temp(uvec, "bits");
   5671    body.emit(assign(bits, bitcast_f2u(x)));
   5672    body.emit(assign(bits, bit_and(bits, sign_mantissa_mask)));
   5673    body.emit(assign(bits, bit_or(bits, csel(is_not_zero, exponent_value,
   5674                                                 imm(0u, vec_elem)))));
   5675    body.emit(ret(bitcast_u2f(bits)));
   5676 
   5677    return sig;
   5678 }
   5679 
   5680 ir_function_signature *
   5681 builtin_builder::_uaddCarry(const glsl_type *type)
   5682 {
   5683    ir_variable *x = in_var(type, "x");
   5684    ir_variable *y = in_var(type, "y");
   5685    ir_variable *carry = out_var(type, "carry");
   5686    MAKE_SIG(type, gpu_shader5_or_es31_or_integer_functions, 3, x, y, carry);
   5687 
   5688    body.emit(assign(carry, ir_builder::carry(x, y)));
   5689    body.emit(ret(add(x, y)));
   5690 
   5691    return sig;
   5692 }
   5693 
   5694 ir_function_signature *
   5695 builtin_builder::_usubBorrow(const glsl_type *type)
   5696 {
   5697    ir_variable *x = in_var(type, "x");
   5698    ir_variable *y = in_var(type, "y");
   5699    ir_variable *borrow = out_var(type, "borrow");
   5700    MAKE_SIG(type, gpu_shader5_or_es31_or_integer_functions, 3, x, y, borrow);
   5701 
   5702    body.emit(assign(borrow, ir_builder::borrow(x, y)));
   5703    body.emit(ret(sub(x, y)));
   5704 
   5705    return sig;
   5706 }
   5707 
   5708 /**
   5709  * For both imulExtended() and umulExtended() built-ins.
   5710  */
   5711 ir_function_signature *
   5712 builtin_builder::_mulExtended(const glsl_type *type)
   5713 {
   5714    ir_variable *x = in_var(type, "x");
   5715    ir_variable *y = in_var(type, "y");
   5716    ir_variable *msb = out_var(type, "msb");
   5717    ir_variable *lsb = out_var(type, "lsb");
   5718    MAKE_SIG(glsl_type::void_type, gpu_shader5_or_es31_or_integer_functions, 4, x, y, msb, lsb);
   5719 
   5720    body.emit(assign(msb, imul_high(x, y)));
   5721    body.emit(assign(lsb, mul(x, y)));
   5722 
   5723    return sig;
   5724 }
   5725 
   5726 ir_function_signature *
   5727 builtin_builder::_interpolateAtCentroid(const glsl_type *type)
   5728 {
   5729    ir_variable *interpolant = in_var(type, "interpolant");
   5730    interpolant->data.must_be_shader_input = 1;
   5731    MAKE_SIG(type, fs_interpolate_at, 1, interpolant);
   5732 
   5733    body.emit(ret(interpolate_at_centroid(interpolant)));
   5734 
   5735    return sig;
   5736 }
   5737 
   5738 ir_function_signature *
   5739 builtin_builder::_interpolateAtOffset(const glsl_type *type)
   5740 {
   5741    ir_variable *interpolant = in_var(type, "interpolant");
   5742    interpolant->data.must_be_shader_input = 1;
   5743    ir_variable *offset = in_var(glsl_type::vec2_type, "offset");
   5744    MAKE_SIG(type, fs_interpolate_at, 2, interpolant, offset);
   5745 
   5746    body.emit(ret(interpolate_at_offset(interpolant, offset)));
   5747 
   5748    return sig;
   5749 }
   5750 
   5751 ir_function_signature *
   5752 builtin_builder::_interpolateAtSample(const glsl_type *type)
   5753 {
   5754    ir_variable *interpolant = in_var(type, "interpolant");
   5755    interpolant->data.must_be_shader_input = 1;
   5756    ir_variable *sample_num = in_var(glsl_type::int_type, "sample_num");
   5757    MAKE_SIG(type, fs_interpolate_at, 2, interpolant, sample_num);
   5758 
   5759    body.emit(ret(interpolate_at_sample(interpolant, sample_num)));
   5760 
   5761    return sig;
   5762 }
   5763 
   5764 ir_function_signature *
   5765 builtin_builder::_atomic_counter_intrinsic(builtin_available_predicate avail,
   5766                                            enum ir_intrinsic_id id)
   5767 {
   5768    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "counter");
   5769    MAKE_INTRINSIC(glsl_type::uint_type, id, avail, 1, counter);
   5770    return sig;
   5771 }
   5772 
   5773 ir_function_signature *
   5774 builtin_builder::_atomic_counter_intrinsic1(builtin_available_predicate avail,
   5775                                             enum ir_intrinsic_id id)
   5776 {
   5777    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "counter");
   5778    ir_variable *data = in_var(glsl_type::uint_type, "data");
   5779    MAKE_INTRINSIC(glsl_type::uint_type, id, avail, 2, counter, data);
   5780    return sig;
   5781 }
   5782 
   5783 ir_function_signature *
   5784 builtin_builder::_atomic_counter_intrinsic2(builtin_available_predicate avail,
   5785                                             enum ir_intrinsic_id id)
   5786 {
   5787    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "counter");
   5788    ir_variable *compare = in_var(glsl_type::uint_type, "compare");
   5789    ir_variable *data = in_var(glsl_type::uint_type, "data");
   5790    MAKE_INTRINSIC(glsl_type::uint_type, id, avail, 3, counter, compare, data);
   5791    return sig;
   5792 }
   5793 
   5794 ir_function_signature *
   5795 builtin_builder::_atomic_intrinsic2(builtin_available_predicate avail,
   5796                                     const glsl_type *type,
   5797                                     enum ir_intrinsic_id id)
   5798 {
   5799    ir_variable *atomic = in_var(type, "atomic");
   5800    ir_variable *data = in_var(type, "data");
   5801    MAKE_INTRINSIC(type, id, avail, 2, atomic, data);
   5802    return sig;
   5803 }
   5804 
   5805 ir_function_signature *
   5806 builtin_builder::_atomic_intrinsic3(builtin_available_predicate avail,
   5807                                     const glsl_type *type,
   5808                                     enum ir_intrinsic_id id)
   5809 {
   5810    ir_variable *atomic = in_var(type, "atomic");
   5811    ir_variable *data1 = in_var(type, "data1");
   5812    ir_variable *data2 = in_var(type, "data2");
   5813    MAKE_INTRINSIC(type, id, avail, 3, atomic, data1, data2);
   5814    return sig;
   5815 }
   5816 
   5817 ir_function_signature *
   5818 builtin_builder::_atomic_counter_op(const char *intrinsic,
   5819                                     builtin_available_predicate avail)
   5820 {
   5821    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "atomic_counter");
   5822    MAKE_SIG(glsl_type::uint_type, avail, 1, counter);
   5823 
   5824    ir_variable *retval = body.make_temp(glsl_type::uint_type, "atomic_retval");
   5825    body.emit(call(shader->symbols->get_function(intrinsic), retval,
   5826                   sig->parameters));
   5827    body.emit(ret(retval));
   5828    return sig;
   5829 }
   5830 
   5831 ir_function_signature *
   5832 builtin_builder::_atomic_counter_op1(const char *intrinsic,
   5833                                      builtin_available_predicate avail)
   5834 {
   5835    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "atomic_counter");
   5836    ir_variable *data = in_var(glsl_type::uint_type, "data");
   5837    MAKE_SIG(glsl_type::uint_type, avail, 2, counter, data);
   5838 
   5839    ir_variable *retval = body.make_temp(glsl_type::uint_type, "atomic_retval");
   5840 
   5841    /* Instead of generating an __intrinsic_atomic_sub, generate an
   5842     * __intrinsic_atomic_add with the data parameter negated.
   5843     */
   5844    if (strcmp("__intrinsic_atomic_sub", intrinsic) == 0) {
   5845       ir_variable *const neg_data =
   5846          body.make_temp(glsl_type::uint_type, "neg_data");
   5847 
   5848       body.emit(assign(neg_data, neg(data)));
   5849 
   5850       exec_list parameters;
   5851 
   5852       parameters.push_tail(new(mem_ctx) ir_dereference_variable(counter));
   5853       parameters.push_tail(new(mem_ctx) ir_dereference_variable(neg_data));
   5854 
   5855       ir_function *const func =
   5856          shader->symbols->get_function("__intrinsic_atomic_add");
   5857       ir_instruction *const c = call(func, retval, parameters);
   5858 
   5859       assert(c != NULL);
   5860       assert(parameters.is_empty());
   5861 
   5862       body.emit(c);
   5863    } else {
   5864       body.emit(call(shader->symbols->get_function(intrinsic), retval,
   5865                      sig->parameters));
   5866    }
   5867 
   5868    body.emit(ret(retval));
   5869    return sig;
   5870 }
   5871 
   5872 ir_function_signature *
   5873 builtin_builder::_atomic_counter_op2(const char *intrinsic,
   5874                                     builtin_available_predicate avail)
   5875 {
   5876    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "atomic_counter");
   5877    ir_variable *compare = in_var(glsl_type::uint_type, "compare");
   5878    ir_variable *data = in_var(glsl_type::uint_type, "data");
   5879    MAKE_SIG(glsl_type::uint_type, avail, 3, counter, compare, data);
   5880 
   5881    ir_variable *retval = body.make_temp(glsl_type::uint_type, "atomic_retval");
   5882    body.emit(call(shader->symbols->get_function(intrinsic), retval,
   5883                   sig->parameters));
   5884    body.emit(ret(retval));
   5885    return sig;
   5886 }
   5887 
   5888 ir_function_signature *
   5889 builtin_builder::_atomic_op2(const char *intrinsic,
   5890                              builtin_available_predicate avail,
   5891                              const glsl_type *type)
   5892 {
   5893    ir_variable *atomic = in_var(type, "atomic_var");
   5894    ir_variable *data = in_var(type, "atomic_data");
   5895    MAKE_SIG(type, avail, 2, atomic, data);
   5896 
   5897    ir_variable *retval = body.make_temp(type, "atomic_retval");
   5898    body.emit(call(shader->symbols->get_function(intrinsic), retval,
   5899                   sig->parameters));
   5900    body.emit(ret(retval));
   5901    return sig;
   5902 }
   5903 
   5904 ir_function_signature *
   5905 builtin_builder::_atomic_op3(const char *intrinsic,
   5906                              builtin_available_predicate avail,
   5907                              const glsl_type *type)
   5908 {
   5909    ir_variable *atomic = in_var(type, "atomic_var");
   5910    ir_variable *data1 = in_var(type, "atomic_data1");
   5911    ir_variable *data2 = in_var(type, "atomic_data2");
   5912    MAKE_SIG(type, avail, 3, atomic, data1, data2);
   5913 
   5914    ir_variable *retval = body.make_temp(type, "atomic_retval");
   5915    body.emit(call(shader->symbols->get_function(intrinsic), retval,
   5916                   sig->parameters));
   5917    body.emit(ret(retval));
   5918    return sig;
   5919 }
   5920 
   5921 ir_function_signature *
   5922 builtin_builder::_min3(const glsl_type *type)
   5923 {
   5924    ir_variable *x = in_var(type, "x");
   5925    ir_variable *y = in_var(type, "y");
   5926    ir_variable *z = in_var(type, "z");
   5927    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
   5928 
   5929    ir_expression *min3 = min2(x, min2(y,z));
   5930    body.emit(ret(min3));
   5931 
   5932    return sig;
   5933 }
   5934 
   5935 ir_function_signature *
   5936 builtin_builder::_max3(const glsl_type *type)
   5937 {
   5938    ir_variable *x = in_var(type, "x");
   5939    ir_variable *y = in_var(type, "y");
   5940    ir_variable *z = in_var(type, "z");
   5941    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
   5942 
   5943    ir_expression *max3 = max2(x, max2(y,z));
   5944    body.emit(ret(max3));
   5945 
   5946    return sig;
   5947 }
   5948 
   5949 ir_function_signature *
   5950 builtin_builder::_mid3(const glsl_type *type)
   5951 {
   5952    ir_variable *x = in_var(type, "x");
   5953    ir_variable *y = in_var(type, "y");
   5954    ir_variable *z = in_var(type, "z");
   5955    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
   5956 
   5957    ir_expression *mid3 = max2(min2(x, y), max2(min2(x, z), min2(y, z)));
   5958    body.emit(ret(mid3));
   5959 
   5960    return sig;
   5961 }
   5962 
   5963 static builtin_available_predicate
   5964 get_image_available_predicate(const glsl_type *type, unsigned flags)
   5965 {
   5966    if ((flags & IMAGE_FUNCTION_AVAIL_ATOMIC_EXCHANGE) &&
   5967        type->sampled_type == GLSL_TYPE_FLOAT)
   5968       return shader_image_atomic_exchange_float;
   5969 
   5970    else if (flags & (IMAGE_FUNCTION_AVAIL_ATOMIC_EXCHANGE |
   5971                      IMAGE_FUNCTION_AVAIL_ATOMIC))
   5972       return shader_image_atomic;
   5973 
   5974    else
   5975       return shader_image_load_store;
   5976 }
   5977 
   5978 ir_function_signature *
   5979 builtin_builder::_image_prototype(const glsl_type *image_type,
   5980                                   unsigned num_arguments,
   5981                                   unsigned flags)
   5982 {
   5983    const glsl_type *data_type = glsl_type::get_instance(
   5984       image_type->sampled_type,
   5985       (flags & IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE ? 4 : 1),
   5986       1);
   5987    const glsl_type *ret_type = (flags & IMAGE_FUNCTION_RETURNS_VOID ?
   5988                                 glsl_type::void_type : data_type);
   5989 
   5990    /* Addressing arguments that are always present. */
   5991    ir_variable *image = in_var(image_type, "image");
   5992    ir_variable *coord = in_var(
   5993       glsl_type::ivec(image_type->coordinate_components()), "coord");
   5994 
   5995    ir_function_signature *sig = new_sig(
   5996       ret_type, get_image_available_predicate(image_type, flags),
   5997       2, image, coord);
   5998 
   5999    /* Sample index for multisample images. */
   6000    if (image_type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS)
   6001       sig->parameters.push_tail(in_var(glsl_type::int_type, "sample"));
   6002 
   6003    /* Data arguments. */
   6004    for (unsigned i = 0; i < num_arguments; ++i) {
   6005       char *arg_name = ralloc_asprintf(NULL, "arg%d", i);
   6006       sig->parameters.push_tail(in_var(data_type, arg_name));
   6007       ralloc_free(arg_name);
   6008    }
   6009 
   6010    /* Set the maximal set of qualifiers allowed for this image
   6011     * built-in.  Function calls with arguments having fewer
   6012     * qualifiers than present in the prototype are allowed by the
   6013     * spec, but not with more, i.e. this will make the compiler
   6014     * accept everything that needs to be accepted, and reject cases
   6015     * like loads from write-only or stores to read-only images.
   6016     */
   6017    image->data.memory_read_only = (flags & IMAGE_FUNCTION_READ_ONLY) != 0;
   6018    image->data.memory_write_only = (flags & IMAGE_FUNCTION_WRITE_ONLY) != 0;
   6019    image->data.memory_coherent = true;
   6020    image->data.memory_volatile = true;
   6021    image->data.memory_restrict = true;
   6022 
   6023    return sig;
   6024 }
   6025 
   6026 ir_function_signature *
   6027 builtin_builder::_image_size_prototype(const glsl_type *image_type,
   6028                                        unsigned /* num_arguments */,
   6029                                        unsigned /* flags */)
   6030 {
   6031    const glsl_type *ret_type;
   6032    unsigned num_components = image_type->coordinate_components();
   6033 
   6034    /* From the ARB_shader_image_size extension:
   6035     * "Cube images return the dimensions of one face."
   6036     */
   6037    if (image_type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
   6038        !image_type->sampler_array) {
   6039       num_components = 2;
   6040    }
   6041 
   6042    /* FIXME: Add the highp precision qualifier for GLES 3.10 when it is
   6043     * supported by mesa.
   6044     */
   6045    ret_type = glsl_type::get_instance(GLSL_TYPE_INT, num_components, 1);
   6046 
   6047    ir_variable *image = in_var(image_type, "image");
   6048    ir_function_signature *sig = new_sig(ret_type, shader_image_size, 1, image);
   6049 
   6050    /* Set the maximal set of qualifiers allowed for this image
   6051     * built-in.  Function calls with arguments having fewer
   6052     * qualifiers than present in the prototype are allowed by the
   6053     * spec, but not with more, i.e. this will make the compiler
   6054     * accept everything that needs to be accepted, and reject cases
   6055     * like loads from write-only or stores to read-only images.
   6056     */
   6057    image->data.memory_read_only = true;
   6058    image->data.memory_write_only = true;
   6059    image->data.memory_coherent = true;
   6060    image->data.memory_volatile = true;
   6061    image->data.memory_restrict = true;
   6062 
   6063    return sig;
   6064 }
   6065 
   6066 ir_function_signature *
   6067 builtin_builder::_image_samples_prototype(const glsl_type *image_type,
   6068                                           unsigned /* num_arguments */,
   6069                                           unsigned /* flags */)
   6070 {
   6071    ir_variable *image = in_var(image_type, "image");
   6072    ir_function_signature *sig =
   6073       new_sig(glsl_type::int_type, shader_samples, 1, image);
   6074 
   6075    /* Set the maximal set of qualifiers allowed for this image
   6076     * built-in.  Function calls with arguments having fewer
   6077     * qualifiers than present in the prototype are allowed by the
   6078     * spec, but not with more, i.e. this will make the compiler
   6079     * accept everything that needs to be accepted, and reject cases
   6080     * like loads from write-only or stores to read-only images.
   6081     */
   6082    image->data.memory_read_only = true;
   6083    image->data.memory_write_only = true;
   6084    image->data.memory_coherent = true;
   6085    image->data.memory_volatile = true;
   6086    image->data.memory_restrict = true;
   6087 
   6088    return sig;
   6089 }
   6090 
   6091 ir_function_signature *
   6092 builtin_builder::_image(image_prototype_ctr prototype,
   6093                         const glsl_type *image_type,
   6094                         const char *intrinsic_name,
   6095                         unsigned num_arguments,
   6096                         unsigned flags,
   6097                         enum ir_intrinsic_id id)
   6098 {
   6099    ir_function_signature *sig = (this->*prototype)(image_type,
   6100                                                    num_arguments, flags);
   6101 
   6102    if (flags & IMAGE_FUNCTION_EMIT_STUB) {
   6103       ir_factory body(&sig->body, mem_ctx);
   6104       ir_function *f = shader->symbols->get_function(intrinsic_name);
   6105 
   6106       if (flags & IMAGE_FUNCTION_RETURNS_VOID) {
   6107          body.emit(call(f, NULL, sig->parameters));
   6108       } else {
   6109          ir_variable *ret_val =
   6110             body.make_temp(sig->return_type, "_ret_val");
   6111          body.emit(call(f, ret_val, sig->parameters));
   6112          body.emit(ret(ret_val));
   6113       }
   6114 
   6115       sig->is_defined = true;
   6116 
   6117    } else {
   6118       sig->intrinsic_id = id;
   6119    }
   6120 
   6121    return sig;
   6122 }
   6123 
   6124 ir_function_signature *
   6125 builtin_builder::_memory_barrier_intrinsic(builtin_available_predicate avail,
   6126                                            enum ir_intrinsic_id id)
   6127 {
   6128    MAKE_INTRINSIC(glsl_type::void_type, id, avail, 0);
   6129    return sig;
   6130 }
   6131 
   6132 ir_function_signature *
   6133 builtin_builder::_memory_barrier(const char *intrinsic_name,
   6134                                  builtin_available_predicate avail)
   6135 {
   6136    MAKE_SIG(glsl_type::void_type, avail, 0);
   6137    body.emit(call(shader->symbols->get_function(intrinsic_name),
   6138                   NULL, sig->parameters));
   6139    return sig;
   6140 }
   6141 
   6142 ir_function_signature *
   6143 builtin_builder::_ballot_intrinsic()
   6144 {
   6145    ir_variable *value = in_var(glsl_type::bool_type, "value");
   6146    MAKE_INTRINSIC(glsl_type::uint64_t_type, ir_intrinsic_ballot, shader_ballot,
   6147                   1, value);
   6148    return sig;
   6149 }
   6150 
   6151 ir_function_signature *
   6152 builtin_builder::_ballot()
   6153 {
   6154    ir_variable *value = in_var(glsl_type::bool_type, "value");
   6155 
   6156    MAKE_SIG(glsl_type::uint64_t_type, shader_ballot, 1, value);
   6157    ir_variable *retval = body.make_temp(glsl_type::uint64_t_type, "retval");
   6158 
   6159    body.emit(call(shader->symbols->get_function("__intrinsic_ballot"),
   6160                   retval, sig->parameters));
   6161    body.emit(ret(retval));
   6162    return sig;
   6163 }
   6164 
   6165 ir_function_signature *
   6166 builtin_builder::_read_first_invocation_intrinsic(const glsl_type *type)
   6167 {
   6168    ir_variable *value = in_var(type, "value");
   6169    MAKE_INTRINSIC(type, ir_intrinsic_read_first_invocation, shader_ballot,
   6170                   1, value);
   6171    return sig;
   6172 }
   6173 
   6174 ir_function_signature *
   6175 builtin_builder::_read_first_invocation(const glsl_type *type)
   6176 {
   6177    ir_variable *value = in_var(type, "value");
   6178 
   6179    MAKE_SIG(type, shader_ballot, 1, value);
   6180    ir_variable *retval = body.make_temp(type, "retval");
   6181 
   6182    body.emit(call(shader->symbols->get_function("__intrinsic_read_first_invocation"),
   6183                   retval, sig->parameters));
   6184    body.emit(ret(retval));
   6185    return sig;
   6186 }
   6187 
   6188 ir_function_signature *
   6189 builtin_builder::_read_invocation_intrinsic(const glsl_type *type)
   6190 {
   6191    ir_variable *value = in_var(type, "value");
   6192    ir_variable *invocation = in_var(glsl_type::uint_type, "invocation");
   6193    MAKE_INTRINSIC(type, ir_intrinsic_read_invocation, shader_ballot,
   6194                   2, value, invocation);
   6195    return sig;
   6196 }
   6197 
   6198 ir_function_signature *
   6199 builtin_builder::_read_invocation(const glsl_type *type)
   6200 {
   6201    ir_variable *value = in_var(type, "value");
   6202    ir_variable *invocation = in_var(glsl_type::uint_type, "invocation");
   6203 
   6204    MAKE_SIG(type, shader_ballot, 2, value, invocation);
   6205    ir_variable *retval = body.make_temp(type, "retval");
   6206 
   6207    body.emit(call(shader->symbols->get_function("__intrinsic_read_invocation"),
   6208                   retval, sig->parameters));
   6209    body.emit(ret(retval));
   6210    return sig;
   6211 }
   6212 
   6213 ir_function_signature *
   6214 builtin_builder::_shader_clock_intrinsic(builtin_available_predicate avail,
   6215                                          const glsl_type *type)
   6216 {
   6217    MAKE_INTRINSIC(type, ir_intrinsic_shader_clock, avail, 0);
   6218    return sig;
   6219 }
   6220 
   6221 ir_function_signature *
   6222 builtin_builder::_shader_clock(builtin_available_predicate avail,
   6223                                const glsl_type *type)
   6224 {
   6225    MAKE_SIG(type, avail, 0);
   6226 
   6227    ir_variable *retval = body.make_temp(glsl_type::uvec2_type, "clock_retval");
   6228 
   6229    body.emit(call(shader->symbols->get_function("__intrinsic_shader_clock"),
   6230                   retval, sig->parameters));
   6231 
   6232    if (type == glsl_type::uint64_t_type) {
   6233       body.emit(ret(expr(ir_unop_pack_uint_2x32, retval)));
   6234    } else {
   6235       body.emit(ret(retval));
   6236    }
   6237 
   6238    return sig;
   6239 }
   6240 
   6241 ir_function_signature *
   6242 builtin_builder::_vote_intrinsic(builtin_available_predicate avail,
   6243                                  enum ir_intrinsic_id id)
   6244 {
   6245    ir_variable *value = in_var(glsl_type::bool_type, "value");
   6246    MAKE_INTRINSIC(glsl_type::bool_type, id, avail, 1, value);
   6247    return sig;
   6248 }
   6249 
   6250 ir_function_signature *
   6251 builtin_builder::_vote(const char *intrinsic_name,
   6252                        builtin_available_predicate avail)
   6253 {
   6254    ir_variable *value = in_var(glsl_type::bool_type, "value");
   6255 
   6256    MAKE_SIG(glsl_type::bool_type, avail, 1, value);
   6257 
   6258    ir_variable *retval = body.make_temp(glsl_type::bool_type, "retval");
   6259 
   6260    body.emit(call(shader->symbols->get_function(intrinsic_name),
   6261                   retval, sig->parameters));
   6262    body.emit(ret(retval));
   6263    return sig;
   6264 }
   6265 
   6266 /** @} */
   6267 
   6268 /******************************************************************************/
   6269 
   6270 /* The singleton instance of builtin_builder. */
   6271 static builtin_builder builtins;
   6272 static mtx_t builtins_lock = _MTX_INITIALIZER_NP;
   6273 
   6274 /**
   6275  * External API (exposing the built-in module to the rest of the compiler):
   6276  *  @{
   6277  */
   6278 void
   6279 _mesa_glsl_initialize_builtin_functions()
   6280 {
   6281    mtx_lock(&builtins_lock);
   6282    builtins.initialize();
   6283    mtx_unlock(&builtins_lock);
   6284 }
   6285 
   6286 void
   6287 _mesa_glsl_release_builtin_functions()
   6288 {
   6289    mtx_lock(&builtins_lock);
   6290    builtins.release();
   6291    mtx_unlock(&builtins_lock);
   6292 }
   6293 
   6294 ir_function_signature *
   6295 _mesa_glsl_find_builtin_function(_mesa_glsl_parse_state *state,
   6296                                  const char *name, exec_list *actual_parameters)
   6297 {
   6298    ir_function_signature *s;
   6299    mtx_lock(&builtins_lock);
   6300    s = builtins.find(state, name, actual_parameters);
   6301    mtx_unlock(&builtins_lock);
   6302 
   6303    return s;
   6304 }
   6305 
   6306 bool
   6307 _mesa_glsl_has_builtin_function(_mesa_glsl_parse_state *state, const char *name)
   6308 {
   6309    ir_function *f;
   6310    bool ret = false;
   6311    mtx_lock(&builtins_lock);
   6312    f = builtins.shader->symbols->get_function(name);
   6313    if (f != NULL) {
   6314       foreach_in_list(ir_function_signature, sig, &f->signatures) {
   6315          if (sig->is_builtin_available(state)) {
   6316             ret = true;
   6317             break;
   6318          }
   6319       }
   6320    }
   6321    mtx_unlock(&builtins_lock);
   6322 
   6323    return ret;
   6324 }
   6325 
   6326 gl_shader *
   6327 _mesa_glsl_get_builtin_function_shader()
   6328 {
   6329    return builtins.shader;
   6330 }
   6331 
   6332 
   6333 /**
   6334  * Get the function signature for main from a shader
   6335  */
   6336 ir_function_signature *
   6337 _mesa_get_main_function_signature(glsl_symbol_table *symbols)
   6338 {
   6339    ir_function *const f = symbols->get_function("main");
   6340    if (f != NULL) {
   6341       exec_list void_parameters;
   6342 
   6343       /* Look for the 'void main()' signature and ensure that it's defined.
   6344        * This keeps the linker from accidentally pick a shader that just
   6345        * contains a prototype for main.
   6346        *
   6347        * We don't have to check for multiple definitions of main (in multiple
   6348        * shaders) because that would have already been caught above.
   6349        */
   6350       ir_function_signature *sig =
   6351          f->matching_signature(NULL, &void_parameters, false);
   6352       if ((sig != NULL) && sig->is_defined) {
   6353          return sig;
   6354       }
   6355    }
   6356 
   6357    return NULL;
   6358 }
   6359 
   6360 /** @} */
   6361