Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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. #include <stdarg.h>
  57. #include <stdio.h>
  58. #include "main/core.h" /* for struct gl_shader */
  59. #include "main/shaderobj.h"
  60. #include "ir_builder.h"
  61. #include "glsl_parser_extras.h"
  62. #include "program/prog_instruction.h"
  63. #include <math.h>
  64.  
  65. #define M_PIf   ((float) M_PI)
  66. #define M_PI_2f ((float) M_PI_2)
  67. #define M_PI_4f ((float) M_PI_4)
  68.  
  69. using namespace ir_builder;
  70.  
  71. /**
  72.  * Availability predicates:
  73.  *  @{
  74.  */
  75. static bool
  76. always_available(const _mesa_glsl_parse_state *)
  77. {
  78.    return true;
  79. }
  80.  
  81. static bool
  82. compatibility_vs_only(const _mesa_glsl_parse_state *state)
  83. {
  84.    return state->stage == MESA_SHADER_VERTEX &&
  85.           state->language_version <= 130 &&
  86.           !state->es_shader;
  87. }
  88.  
  89. static bool
  90. fs_only(const _mesa_glsl_parse_state *state)
  91. {
  92.    return state->stage == MESA_SHADER_FRAGMENT;
  93. }
  94.  
  95. static bool
  96. gs_only(const _mesa_glsl_parse_state *state)
  97. {
  98.    return state->stage == MESA_SHADER_GEOMETRY;
  99. }
  100.  
  101. static bool
  102. v110(const _mesa_glsl_parse_state *state)
  103. {
  104.    return !state->es_shader;
  105. }
  106.  
  107. static bool
  108. v110_fs_only(const _mesa_glsl_parse_state *state)
  109. {
  110.    return !state->es_shader && state->stage == MESA_SHADER_FRAGMENT;
  111. }
  112.  
  113. static bool
  114. v120(const _mesa_glsl_parse_state *state)
  115. {
  116.    return state->is_version(120, 300);
  117. }
  118.  
  119. static bool
  120. v130(const _mesa_glsl_parse_state *state)
  121. {
  122.    return state->is_version(130, 300);
  123. }
  124.  
  125. static bool
  126. v130_fs_only(const _mesa_glsl_parse_state *state)
  127. {
  128.    return state->is_version(130, 300) &&
  129.           state->stage == MESA_SHADER_FRAGMENT;
  130. }
  131.  
  132. static bool
  133. v140(const _mesa_glsl_parse_state *state)
  134. {
  135.    return state->is_version(140, 0);
  136. }
  137.  
  138. static bool
  139. es31(const _mesa_glsl_parse_state *state)
  140. {
  141.    return state->is_version(0, 310);
  142. }
  143.  
  144. static bool
  145. texture_rectangle(const _mesa_glsl_parse_state *state)
  146. {
  147.    return state->ARB_texture_rectangle_enable;
  148. }
  149.  
  150. static bool
  151. texture_external(const _mesa_glsl_parse_state *state)
  152. {
  153.    return state->OES_EGL_image_external_enable;
  154. }
  155.  
  156. /** True if texturing functions with explicit LOD are allowed. */
  157. static bool
  158. lod_exists_in_stage(const _mesa_glsl_parse_state *state)
  159. {
  160.    /* Texturing functions with "Lod" in their name exist:
  161.     * - In the vertex shader stage (for all languages)
  162.     * - In any stage for GLSL 1.30+ or GLSL ES 3.00
  163.     * - In any stage for desktop GLSL with ARB_shader_texture_lod enabled.
  164.     *
  165.     * Since ARB_shader_texture_lod can only be enabled on desktop GLSL, we
  166.     * don't need to explicitly check state->es_shader.
  167.     */
  168.    return state->stage == MESA_SHADER_VERTEX ||
  169.           state->is_version(130, 300) ||
  170.           state->ARB_shader_texture_lod_enable;
  171. }
  172.  
  173. static bool
  174. v110_lod(const _mesa_glsl_parse_state *state)
  175. {
  176.    return !state->es_shader && lod_exists_in_stage(state);
  177. }
  178.  
  179. static bool
  180. shader_texture_lod(const _mesa_glsl_parse_state *state)
  181. {
  182.    return state->ARB_shader_texture_lod_enable;
  183. }
  184.  
  185. static bool
  186. shader_texture_lod_and_rect(const _mesa_glsl_parse_state *state)
  187. {
  188.    return state->ARB_shader_texture_lod_enable &&
  189.           state->ARB_texture_rectangle_enable;
  190. }
  191.  
  192. static bool
  193. shader_bit_encoding(const _mesa_glsl_parse_state *state)
  194. {
  195.    return state->is_version(330, 300) ||
  196.           state->ARB_shader_bit_encoding_enable ||
  197.           state->ARB_gpu_shader5_enable;
  198. }
  199.  
  200. static bool
  201. shader_integer_mix(const _mesa_glsl_parse_state *state)
  202. {
  203.    return state->is_version(450, 310) ||
  204.           (v130(state) && state->EXT_shader_integer_mix_enable);
  205. }
  206.  
  207. static bool
  208. shader_packing_or_es3(const _mesa_glsl_parse_state *state)
  209. {
  210.    return state->ARB_shading_language_packing_enable ||
  211.           state->is_version(420, 300);
  212. }
  213.  
  214. static bool
  215. shader_packing_or_es3_or_gpu_shader5(const _mesa_glsl_parse_state *state)
  216. {
  217.    return state->ARB_shading_language_packing_enable ||
  218.           state->ARB_gpu_shader5_enable ||
  219.           state->is_version(400, 300);
  220. }
  221.  
  222. static bool
  223. gpu_shader5(const _mesa_glsl_parse_state *state)
  224. {
  225.    return state->is_version(400, 0) || state->ARB_gpu_shader5_enable;
  226. }
  227.  
  228. static bool
  229. gpu_shader5_or_es31(const _mesa_glsl_parse_state *state)
  230. {
  231.    return state->is_version(400, 310) || state->ARB_gpu_shader5_enable;
  232. }
  233.  
  234. static bool
  235. shader_packing_or_es31_or_gpu_shader5(const _mesa_glsl_parse_state *state)
  236. {
  237.    return state->ARB_shading_language_packing_enable ||
  238.           state->ARB_gpu_shader5_enable ||
  239.           state->is_version(400, 310);
  240. }
  241.  
  242. static bool
  243. fs_gpu_shader5(const _mesa_glsl_parse_state *state)
  244. {
  245.    return state->stage == MESA_SHADER_FRAGMENT &&
  246.           (state->is_version(400, 0) || state->ARB_gpu_shader5_enable);
  247. }
  248.  
  249.  
  250. static bool
  251. texture_array_lod(const _mesa_glsl_parse_state *state)
  252. {
  253.    return lod_exists_in_stage(state) &&
  254.           state->EXT_texture_array_enable;
  255. }
  256.  
  257. static bool
  258. fs_texture_array(const _mesa_glsl_parse_state *state)
  259. {
  260.    return state->stage == MESA_SHADER_FRAGMENT &&
  261.           state->EXT_texture_array_enable;
  262. }
  263.  
  264. static bool
  265. texture_array(const _mesa_glsl_parse_state *state)
  266. {
  267.    return state->EXT_texture_array_enable;
  268. }
  269.  
  270. static bool
  271. texture_multisample(const _mesa_glsl_parse_state *state)
  272. {
  273.    return state->is_version(150, 0) ||
  274.           state->ARB_texture_multisample_enable;
  275. }
  276.  
  277. static bool
  278. fs_texture_cube_map_array(const _mesa_glsl_parse_state *state)
  279. {
  280.    return state->stage == MESA_SHADER_FRAGMENT &&
  281.           (state->is_version(400, 0) ||
  282.            state->ARB_texture_cube_map_array_enable);
  283. }
  284.  
  285. static bool
  286. texture_cube_map_array(const _mesa_glsl_parse_state *state)
  287. {
  288.    return state->is_version(400, 0) ||
  289.           state->ARB_texture_cube_map_array_enable;
  290. }
  291.  
  292. static bool
  293. texture_query_levels(const _mesa_glsl_parse_state *state)
  294. {
  295.    return state->is_version(430, 0) ||
  296.           state->ARB_texture_query_levels_enable;
  297. }
  298.  
  299. static bool
  300. texture_query_lod(const _mesa_glsl_parse_state *state)
  301. {
  302.    return state->stage == MESA_SHADER_FRAGMENT &&
  303.           state->ARB_texture_query_lod_enable;
  304. }
  305.  
  306. static bool
  307. texture_gather(const _mesa_glsl_parse_state *state)
  308. {
  309.    return state->is_version(400, 0) ||
  310.           state->ARB_texture_gather_enable ||
  311.           state->ARB_gpu_shader5_enable;
  312. }
  313.  
  314. static bool
  315. texture_gather_or_es31(const _mesa_glsl_parse_state *state)
  316. {
  317.    return state->is_version(400, 310) ||
  318.           state->ARB_texture_gather_enable ||
  319.           state->ARB_gpu_shader5_enable;
  320. }
  321.  
  322. /* Only ARB_texture_gather but not GLSL 4.0 or ARB_gpu_shader5.
  323.  * used for relaxation of const offset requirements.
  324.  */
  325. static bool
  326. texture_gather_only_or_es31(const _mesa_glsl_parse_state *state)
  327. {
  328.    return !state->is_version(400, 0) &&
  329.           !state->ARB_gpu_shader5_enable &&
  330.           (state->ARB_texture_gather_enable ||
  331.            state->is_version(0, 310));
  332. }
  333.  
  334. /* Desktop GL or OES_standard_derivatives + fragment shader only */
  335. static bool
  336. fs_oes_derivatives(const _mesa_glsl_parse_state *state)
  337. {
  338.    return state->stage == MESA_SHADER_FRAGMENT &&
  339.           (state->is_version(110, 300) ||
  340.            state->OES_standard_derivatives_enable);
  341. }
  342.  
  343. static bool
  344. fs_derivative_control(const _mesa_glsl_parse_state *state)
  345. {
  346.    return state->stage == MESA_SHADER_FRAGMENT &&
  347.           (state->is_version(450, 0) ||
  348.            state->ARB_derivative_control_enable);
  349. }
  350.  
  351. static bool
  352. tex1d_lod(const _mesa_glsl_parse_state *state)
  353. {
  354.    return !state->es_shader && lod_exists_in_stage(state);
  355. }
  356.  
  357. /** True if sampler3D exists */
  358. static bool
  359. tex3d(const _mesa_glsl_parse_state *state)
  360. {
  361.    /* sampler3D exists in all desktop GLSL versions, GLSL ES 1.00 with the
  362.     * OES_texture_3D extension, and in GLSL ES 3.00.
  363.     */
  364.    return !state->es_shader ||
  365.           state->OES_texture_3D_enable ||
  366.           state->language_version >= 300;
  367. }
  368.  
  369. static bool
  370. fs_tex3d(const _mesa_glsl_parse_state *state)
  371. {
  372.    return state->stage == MESA_SHADER_FRAGMENT &&
  373.           (!state->es_shader || state->OES_texture_3D_enable);
  374. }
  375.  
  376. static bool
  377. tex3d_lod(const _mesa_glsl_parse_state *state)
  378. {
  379.    return tex3d(state) && lod_exists_in_stage(state);
  380. }
  381.  
  382. static bool
  383. shader_atomic_counters(const _mesa_glsl_parse_state *state)
  384. {
  385.    return state->has_atomic_counters();
  386. }
  387.  
  388. static bool
  389. shader_trinary_minmax(const _mesa_glsl_parse_state *state)
  390. {
  391.    return state->AMD_shader_trinary_minmax_enable;
  392. }
  393.  
  394. static bool
  395. shader_image_load_store(const _mesa_glsl_parse_state *state)
  396. {
  397.    return (state->is_version(420, 0) ||
  398.            state->ARB_shader_image_load_store_enable);
  399. }
  400.  
  401. static bool
  402. gs_streams(const _mesa_glsl_parse_state *state)
  403. {
  404.    return gpu_shader5(state) && gs_only(state);
  405. }
  406.  
  407. static bool
  408. fp64(const _mesa_glsl_parse_state *state)
  409. {
  410.    return state->has_double();
  411. }
  412.  
  413. /** @} */
  414.  
  415. /******************************************************************************/
  416.  
  417. namespace {
  418.  
  419. /**
  420.  * builtin_builder: A singleton object representing the core of the built-in
  421.  * function module.
  422.  *
  423.  * It generates IR for every built-in function signature, and organizes them
  424.  * into functions.
  425.  */
  426. class builtin_builder {
  427. public:
  428.    builtin_builder();
  429.    ~builtin_builder();
  430.  
  431.    void initialize();
  432.    void release();
  433.    ir_function_signature *find(_mesa_glsl_parse_state *state,
  434.                                const char *name, exec_list *actual_parameters);
  435.  
  436.    /**
  437.     * A shader to hold all the built-in signatures; created by this module.
  438.     *
  439.     * This includes signatures for every built-in, regardless of version or
  440.     * enabled extensions.  The availability predicate associated with each
  441.     * signature allows matching_signature() to filter out the irrelevant ones.
  442.     */
  443.    gl_shader *shader;
  444.  
  445. private:
  446.    void *mem_ctx;
  447.  
  448.    /** Global variables used by built-in functions. */
  449.    ir_variable *gl_ModelViewProjectionMatrix;
  450.    ir_variable *gl_Vertex;
  451.  
  452.    void create_shader();
  453.    void create_intrinsics();
  454.    void create_builtins();
  455.  
  456.    /**
  457.     * IR builder helpers:
  458.     *
  459.     * These convenience functions assist in emitting IR, but don't necessarily
  460.     * fit in ir_builder itself.  Many of them rely on having a mem_ctx class
  461.     * member available.
  462.     */
  463.    ir_variable *in_var(const glsl_type *type, const char *name);
  464.    ir_variable *out_var(const glsl_type *type, const char *name);
  465.    ir_constant *imm(float f, unsigned vector_elements=1);
  466.    ir_constant *imm(int i, unsigned vector_elements=1);
  467.    ir_constant *imm(unsigned u, unsigned vector_elements=1);
  468.    ir_constant *imm(double d, unsigned vector_elements=1);
  469.    ir_constant *imm(const glsl_type *type, const ir_constant_data &);
  470.    ir_dereference_variable *var_ref(ir_variable *var);
  471.    ir_dereference_array *array_ref(ir_variable *var, int i);
  472.    ir_swizzle *matrix_elt(ir_variable *var, int col, int row);
  473.  
  474.    ir_expression *asin_expr(ir_variable *x);
  475.    void do_atan(ir_factory &body, const glsl_type *type, ir_variable *res, operand y_over_x);
  476.  
  477.    /**
  478.     * Call function \param f with parameters specified as the linked
  479.     * list \param params of \c ir_variable objects.  \param ret should
  480.     * point to the ir_variable that will hold the function return
  481.     * value, or be \c NULL if the function has void return type.
  482.     */
  483.    ir_call *call(ir_function *f, ir_variable *ret, exec_list params);
  484.  
  485.    /** Create a new function and add the given signatures. */
  486.    void add_function(const char *name, ...);
  487.  
  488.    enum image_function_flags {
  489.       IMAGE_FUNCTION_EMIT_STUB = (1 << 0),
  490.       IMAGE_FUNCTION_RETURNS_VOID = (1 << 1),
  491.       IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE = (1 << 2),
  492.       IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE = (1 << 3),
  493.       IMAGE_FUNCTION_READ_ONLY = (1 << 4),
  494.       IMAGE_FUNCTION_WRITE_ONLY = (1 << 5)
  495.    };
  496.  
  497.    /**
  498.     * Create a new image built-in function for all known image types.
  499.     * \p flags is a bitfield of \c image_function_flags flags.
  500.     */
  501.    void add_image_function(const char *name,
  502.                            const char *intrinsic_name,
  503.                            unsigned num_arguments,
  504.                            unsigned flags);
  505.  
  506.    /**
  507.     * Create new functions for all known image built-ins and types.
  508.     * If \p glsl is \c true, use the GLSL built-in names and emit code
  509.     * to call into the actual compiler intrinsic.  If \p glsl is
  510.     * false, emit a function prototype with no body for each image
  511.     * intrinsic name.
  512.     */
  513.    void add_image_functions(bool glsl);
  514.  
  515.    ir_function_signature *new_sig(const glsl_type *return_type,
  516.                                   builtin_available_predicate avail,
  517.                                   int num_params, ...);
  518.  
  519.    /**
  520.     * Function signature generators:
  521.     *  @{
  522.     */
  523.    ir_function_signature *unop(builtin_available_predicate avail,
  524.                                ir_expression_operation opcode,
  525.                                const glsl_type *return_type,
  526.                                const glsl_type *param_type);
  527.    ir_function_signature *binop(ir_expression_operation opcode,
  528.                                 builtin_available_predicate avail,
  529.                                 const glsl_type *return_type,
  530.                                 const glsl_type *param0_type,
  531.                                 const glsl_type *param1_type);
  532.  
  533. #define B0(X) ir_function_signature *_##X();
  534. #define B1(X) ir_function_signature *_##X(const glsl_type *);
  535. #define B2(X) ir_function_signature *_##X(const glsl_type *, const glsl_type *);
  536. #define B3(X) ir_function_signature *_##X(const glsl_type *, const glsl_type *, const glsl_type *);
  537. #define BA1(X) ir_function_signature *_##X(builtin_available_predicate, const glsl_type *);
  538. #define BA2(X) ir_function_signature *_##X(builtin_available_predicate, const glsl_type *, const glsl_type *);
  539.    B1(radians)
  540.    B1(degrees)
  541.    B1(sin)
  542.    B1(cos)
  543.    B1(tan)
  544.    B1(asin)
  545.    B1(acos)
  546.    B1(atan2)
  547.    B1(atan)
  548.    B1(sinh)
  549.    B1(cosh)
  550.    B1(tanh)
  551.    B1(asinh)
  552.    B1(acosh)
  553.    B1(atanh)
  554.    B1(pow)
  555.    B1(exp)
  556.    B1(log)
  557.    B1(exp2)
  558.    B1(log2)
  559.    BA1(sqrt)
  560.    BA1(inversesqrt)
  561.    BA1(abs)
  562.    BA1(sign)
  563.    BA1(floor)
  564.    BA1(trunc)
  565.    BA1(round)
  566.    BA1(roundEven)
  567.    BA1(ceil)
  568.    BA1(fract)
  569.    B2(mod)
  570.    BA1(modf)
  571.    BA2(min)
  572.    BA2(max)
  573.    BA2(clamp)
  574.    BA2(mix_lrp)
  575.    ir_function_signature *_mix_sel(builtin_available_predicate avail,
  576.                                    const glsl_type *val_type,
  577.                                    const glsl_type *blend_type);
  578.    BA2(step)
  579.    BA2(smoothstep)
  580.    BA1(isnan)
  581.    BA1(isinf)
  582.    B1(floatBitsToInt)
  583.    B1(floatBitsToUint)
  584.    B1(intBitsToFloat)
  585.    B1(uintBitsToFloat)
  586.    ir_function_signature *_packUnorm2x16(builtin_available_predicate avail);
  587.    ir_function_signature *_packSnorm2x16(builtin_available_predicate avail);
  588.    ir_function_signature *_packUnorm4x8(builtin_available_predicate avail);
  589.    ir_function_signature *_packSnorm4x8(builtin_available_predicate avail);
  590.    ir_function_signature *_unpackUnorm2x16(builtin_available_predicate avail);
  591.    ir_function_signature *_unpackSnorm2x16(builtin_available_predicate avail);
  592.    ir_function_signature *_unpackUnorm4x8(builtin_available_predicate avail);
  593.    ir_function_signature *_unpackSnorm4x8(builtin_available_predicate avail);
  594.    ir_function_signature *_packHalf2x16(builtin_available_predicate avail);
  595.    ir_function_signature *_unpackHalf2x16(builtin_available_predicate avail);
  596.    ir_function_signature *_packDouble2x32(builtin_available_predicate avail);
  597.    ir_function_signature *_unpackDouble2x32(builtin_available_predicate avail);
  598.  
  599.    BA1(length)
  600.    BA1(distance);
  601.    BA1(dot);
  602.    BA1(cross);
  603.    BA1(normalize);
  604.    B0(ftransform);
  605.    BA1(faceforward);
  606.    BA1(reflect);
  607.    BA1(refract);
  608.    BA1(matrixCompMult);
  609.    BA1(outerProduct);
  610.    BA1(determinant_mat2);
  611.    BA1(determinant_mat3);
  612.    BA1(determinant_mat4);
  613.    BA1(inverse_mat2);
  614.    BA1(inverse_mat3);
  615.    BA1(inverse_mat4);
  616.    BA1(transpose);
  617.    BA1(lessThan);
  618.    BA1(lessThanEqual);
  619.    BA1(greaterThan);
  620.    BA1(greaterThanEqual);
  621.    BA1(equal);
  622.    BA1(notEqual);
  623.    B1(any);
  624.    B1(all);
  625.    B1(not);
  626.    B2(textureSize);
  627.    ir_function_signature *_textureSize(builtin_available_predicate avail,
  628.                                        const glsl_type *return_type,
  629.                                        const glsl_type *sampler_type);
  630.  
  631. /** Flags to _texture() */
  632. #define TEX_PROJECT 1
  633. #define TEX_OFFSET  2
  634. #define TEX_COMPONENT 4
  635. #define TEX_OFFSET_NONCONST 8
  636. #define TEX_OFFSET_ARRAY 16
  637.  
  638.    ir_function_signature *_texture(ir_texture_opcode opcode,
  639.                                    builtin_available_predicate avail,
  640.                                    const glsl_type *return_type,
  641.                                    const glsl_type *sampler_type,
  642.                                    const glsl_type *coord_type,
  643.                                    int flags = 0);
  644.    B0(textureCubeArrayShadow);
  645.    ir_function_signature *_texelFetch(builtin_available_predicate avail,
  646.                                       const glsl_type *return_type,
  647.                                       const glsl_type *sampler_type,
  648.                                       const glsl_type *coord_type,
  649.                                       const glsl_type *offset_type = NULL);
  650.  
  651.    B0(EmitVertex)
  652.    B0(EndPrimitive)
  653.    ir_function_signature *_EmitStreamVertex(builtin_available_predicate avail,
  654.                                             const glsl_type *stream_type);
  655.    ir_function_signature *_EndStreamPrimitive(builtin_available_predicate avail,
  656.                                               const glsl_type *stream_type);
  657.  
  658.    B2(textureQueryLod);
  659.    B1(textureQueryLevels);
  660.    B1(dFdx);
  661.    B1(dFdy);
  662.    B1(fwidth);
  663.    B1(dFdxCoarse);
  664.    B1(dFdyCoarse);
  665.    B1(fwidthCoarse);
  666.    B1(dFdxFine);
  667.    B1(dFdyFine);
  668.    B1(fwidthFine);
  669.    B1(noise1);
  670.    B1(noise2);
  671.    B1(noise3);
  672.    B1(noise4);
  673.  
  674.    B1(bitfieldExtract)
  675.    B1(bitfieldInsert)
  676.    B1(bitfieldReverse)
  677.    B1(bitCount)
  678.    B1(findLSB)
  679.    B1(findMSB)
  680.    BA1(fma)
  681.    B2(ldexp)
  682.    B2(frexp)
  683.    B2(dfrexp)
  684.    B1(uaddCarry)
  685.    B1(usubBorrow)
  686.    B1(mulExtended)
  687.    B1(interpolateAtCentroid)
  688.    B1(interpolateAtOffset)
  689.    B1(interpolateAtSample)
  690.  
  691.    ir_function_signature *_atomic_intrinsic(builtin_available_predicate avail);
  692.    ir_function_signature *_atomic_op(const char *intrinsic,
  693.                                      builtin_available_predicate avail);
  694.  
  695.    B1(min3)
  696.    B1(max3)
  697.    B1(mid3)
  698.  
  699.    ir_function_signature *_image_prototype(const glsl_type *image_type,
  700.                                            const char *intrinsic_name,
  701.                                            unsigned num_arguments,
  702.                                            unsigned flags);
  703.    ir_function_signature *_image(const glsl_type *image_type,
  704.                                  const char *intrinsic_name,
  705.                                  unsigned num_arguments,
  706.                                  unsigned flags);
  707.  
  708.    ir_function_signature *_memory_barrier_intrinsic(
  709.       builtin_available_predicate avail);
  710.    ir_function_signature *_memory_barrier(
  711.       builtin_available_predicate avail);
  712.  
  713. #undef B0
  714. #undef B1
  715. #undef B2
  716. #undef B3
  717. #undef BA1
  718. #undef BA2
  719.    /** @} */
  720. };
  721.  
  722. } /* anonymous namespace */
  723.  
  724. /**
  725.  * Core builtin_builder functionality:
  726.  *  @{
  727.  */
  728. builtin_builder::builtin_builder()
  729.    : shader(NULL),
  730.      gl_ModelViewProjectionMatrix(NULL),
  731.      gl_Vertex(NULL)
  732. {
  733.    mem_ctx = NULL;
  734. }
  735.  
  736. builtin_builder::~builtin_builder()
  737. {
  738.    ralloc_free(mem_ctx);
  739. }
  740.  
  741. ir_function_signature *
  742. builtin_builder::find(_mesa_glsl_parse_state *state,
  743.                       const char *name, exec_list *actual_parameters)
  744. {
  745.    /* The shader currently being compiled requested a built-in function;
  746.     * it needs to link against builtin_builder::shader in order to get them.
  747.     *
  748.     * Even if we don't find a matching signature, we still need to do this so
  749.     * that the "no matching signature" error will list potential candidates
  750.     * from the available built-ins.
  751.     */
  752.    state->uses_builtin_functions = true;
  753.  
  754.    ir_function *f = shader->symbols->get_function(name);
  755.    if (f == NULL)
  756.       return NULL;
  757.  
  758.    ir_function_signature *sig =
  759.       f->matching_signature(state, actual_parameters, true);
  760.    if (sig == NULL)
  761.       return NULL;
  762.  
  763.    return sig;
  764. }
  765.  
  766. void
  767. builtin_builder::initialize()
  768. {
  769.    /* If already initialized, don't do it again. */
  770.    if (mem_ctx != NULL)
  771.       return;
  772.  
  773.    mem_ctx = ralloc_context(NULL);
  774.    create_shader();
  775.    create_intrinsics();
  776.    create_builtins();
  777. }
  778.  
  779. void
  780. builtin_builder::release()
  781. {
  782.    ralloc_free(mem_ctx);
  783.    mem_ctx = NULL;
  784.  
  785.    ralloc_free(shader);
  786.    shader = NULL;
  787. }
  788.  
  789. void
  790. builtin_builder::create_shader()
  791. {
  792.    /* The target doesn't actually matter.  There's no target for generic
  793.     * GLSL utility code that could be linked against any stage, so just
  794.     * arbitrarily pick GL_VERTEX_SHADER.
  795.     */
  796.    shader = _mesa_new_shader(NULL, 0, GL_VERTEX_SHADER);
  797.    shader->symbols = new(mem_ctx) glsl_symbol_table;
  798.  
  799.    gl_ModelViewProjectionMatrix =
  800.       new(mem_ctx) ir_variable(glsl_type::mat4_type,
  801.                                "gl_ModelViewProjectionMatrix",
  802.                                ir_var_uniform);
  803.  
  804.    shader->symbols->add_variable(gl_ModelViewProjectionMatrix);
  805.  
  806.    gl_Vertex = in_var(glsl_type::vec4_type, "gl_Vertex");
  807.    shader->symbols->add_variable(gl_Vertex);
  808. }
  809.  
  810. /** @} */
  811.  
  812. /**
  813.  * Create ir_function and ir_function_signature objects for each
  814.  * intrinsic.
  815.  */
  816. void
  817. builtin_builder::create_intrinsics()
  818. {
  819.    add_function("__intrinsic_atomic_read",
  820.                 _atomic_intrinsic(shader_atomic_counters),
  821.                 NULL);
  822.    add_function("__intrinsic_atomic_increment",
  823.                 _atomic_intrinsic(shader_atomic_counters),
  824.                 NULL);
  825.    add_function("__intrinsic_atomic_predecrement",
  826.                 _atomic_intrinsic(shader_atomic_counters),
  827.                 NULL);
  828.  
  829.    add_image_functions(false);
  830.  
  831.    add_function("__intrinsic_memory_barrier",
  832.                 _memory_barrier_intrinsic(shader_image_load_store),
  833.                 NULL);
  834. }
  835.  
  836. /**
  837.  * Create ir_function and ir_function_signature objects for each built-in.
  838.  *
  839.  * Contains a list of every available built-in.
  840.  */
  841. void
  842. builtin_builder::create_builtins()
  843. {
  844. #define F(NAME)                                 \
  845.    add_function(#NAME,                          \
  846.                 _##NAME(glsl_type::float_type), \
  847.                 _##NAME(glsl_type::vec2_type),  \
  848.                 _##NAME(glsl_type::vec3_type),  \
  849.                 _##NAME(glsl_type::vec4_type),  \
  850.                 NULL);
  851.  
  852. #define FD(NAME)                                 \
  853.    add_function(#NAME,                          \
  854.                 _##NAME(always_available, glsl_type::float_type), \
  855.                 _##NAME(always_available, glsl_type::vec2_type),  \
  856.                 _##NAME(always_available, glsl_type::vec3_type),  \
  857.                 _##NAME(always_available, glsl_type::vec4_type),  \
  858.                 _##NAME(fp64, glsl_type::double_type),  \
  859.                 _##NAME(fp64, glsl_type::dvec2_type),    \
  860.                 _##NAME(fp64, glsl_type::dvec3_type),     \
  861.                 _##NAME(fp64, glsl_type::dvec4_type),      \
  862.                 NULL);
  863.  
  864. #define FD130(NAME)                                 \
  865.    add_function(#NAME,                          \
  866.                 _##NAME(v130, glsl_type::float_type), \
  867.                 _##NAME(v130, glsl_type::vec2_type),  \
  868.                 _##NAME(v130, glsl_type::vec3_type),                  \
  869.                 _##NAME(v130, glsl_type::vec4_type),  \
  870.                 _##NAME(fp64, glsl_type::double_type),  \
  871.                 _##NAME(fp64, glsl_type::dvec2_type),    \
  872.                 _##NAME(fp64, glsl_type::dvec3_type),     \
  873.                 _##NAME(fp64, glsl_type::dvec4_type),      \
  874.                 NULL);
  875.  
  876. #define FDGS5(NAME)                                 \
  877.    add_function(#NAME,                          \
  878.                 _##NAME(gpu_shader5, glsl_type::float_type), \
  879.                 _##NAME(gpu_shader5, glsl_type::vec2_type),  \
  880.                 _##NAME(gpu_shader5, glsl_type::vec3_type),                  \
  881.                 _##NAME(gpu_shader5, glsl_type::vec4_type),  \
  882.                 _##NAME(fp64, glsl_type::double_type),  \
  883.                 _##NAME(fp64, glsl_type::dvec2_type),    \
  884.                 _##NAME(fp64, glsl_type::dvec3_type),     \
  885.                 _##NAME(fp64, glsl_type::dvec4_type),      \
  886.                 NULL);
  887.  
  888. #define FI(NAME)                                \
  889.    add_function(#NAME,                          \
  890.                 _##NAME(glsl_type::float_type), \
  891.                 _##NAME(glsl_type::vec2_type),  \
  892.                 _##NAME(glsl_type::vec3_type),  \
  893.                 _##NAME(glsl_type::vec4_type),  \
  894.                 _##NAME(glsl_type::int_type),   \
  895.                 _##NAME(glsl_type::ivec2_type), \
  896.                 _##NAME(glsl_type::ivec3_type), \
  897.                 _##NAME(glsl_type::ivec4_type), \
  898.                 NULL);
  899.  
  900. #define FID(NAME)                                \
  901.    add_function(#NAME,                          \
  902.                 _##NAME(always_available, glsl_type::float_type), \
  903.                 _##NAME(always_available, glsl_type::vec2_type),  \
  904.                 _##NAME(always_available, glsl_type::vec3_type),  \
  905.                 _##NAME(always_available, glsl_type::vec4_type),  \
  906.                 _##NAME(always_available, glsl_type::int_type),   \
  907.                 _##NAME(always_available, glsl_type::ivec2_type), \
  908.                 _##NAME(always_available, glsl_type::ivec3_type), \
  909.                 _##NAME(always_available, glsl_type::ivec4_type), \
  910.                 _##NAME(fp64, glsl_type::double_type), \
  911.                 _##NAME(fp64, glsl_type::dvec2_type),  \
  912.                 _##NAME(fp64, glsl_type::dvec3_type),  \
  913.                 _##NAME(fp64, glsl_type::dvec4_type),  \
  914.                 NULL);
  915.  
  916. #define FIUD(NAME)                                                 \
  917.    add_function(#NAME,                                            \
  918.                 _##NAME(always_available, glsl_type::float_type), \
  919.                 _##NAME(always_available, glsl_type::vec2_type),  \
  920.                 _##NAME(always_available, glsl_type::vec3_type),  \
  921.                 _##NAME(always_available, glsl_type::vec4_type),  \
  922.                                                                   \
  923.                 _##NAME(always_available, glsl_type::int_type),   \
  924.                 _##NAME(always_available, glsl_type::ivec2_type), \
  925.                 _##NAME(always_available, glsl_type::ivec3_type), \
  926.                 _##NAME(always_available, glsl_type::ivec4_type), \
  927.                                                                   \
  928.                 _##NAME(v130, glsl_type::uint_type),              \
  929.                 _##NAME(v130, glsl_type::uvec2_type),             \
  930.                 _##NAME(v130, glsl_type::uvec3_type),             \
  931.                 _##NAME(v130, glsl_type::uvec4_type),             \
  932.                 _##NAME(fp64, glsl_type::double_type), \
  933.                 _##NAME(fp64, glsl_type::dvec2_type),  \
  934.                 _##NAME(fp64, glsl_type::dvec3_type),  \
  935.                 _##NAME(fp64, glsl_type::dvec4_type),  \
  936.                 NULL);
  937.  
  938. #define IU(NAME)                                \
  939.    add_function(#NAME,                          \
  940.                 _##NAME(glsl_type::int_type),   \
  941.                 _##NAME(glsl_type::ivec2_type), \
  942.                 _##NAME(glsl_type::ivec3_type), \
  943.                 _##NAME(glsl_type::ivec4_type), \
  944.                                                 \
  945.                 _##NAME(glsl_type::uint_type),  \
  946.                 _##NAME(glsl_type::uvec2_type), \
  947.                 _##NAME(glsl_type::uvec3_type), \
  948.                 _##NAME(glsl_type::uvec4_type), \
  949.                 NULL);
  950.  
  951. #define FIUBD(NAME)                                                \
  952.    add_function(#NAME,                                            \
  953.                 _##NAME(always_available, glsl_type::float_type), \
  954.                 _##NAME(always_available, glsl_type::vec2_type),  \
  955.                 _##NAME(always_available, glsl_type::vec3_type),  \
  956.                 _##NAME(always_available, glsl_type::vec4_type),  \
  957.                                                                   \
  958.                 _##NAME(always_available, glsl_type::int_type),   \
  959.                 _##NAME(always_available, glsl_type::ivec2_type), \
  960.                 _##NAME(always_available, glsl_type::ivec3_type), \
  961.                 _##NAME(always_available, glsl_type::ivec4_type), \
  962.                                                                   \
  963.                 _##NAME(v130, glsl_type::uint_type),              \
  964.                 _##NAME(v130, glsl_type::uvec2_type),             \
  965.                 _##NAME(v130, glsl_type::uvec3_type),             \
  966.                 _##NAME(v130, glsl_type::uvec4_type),             \
  967.                                                                   \
  968.                 _##NAME(always_available, glsl_type::bool_type),  \
  969.                 _##NAME(always_available, glsl_type::bvec2_type), \
  970.                 _##NAME(always_available, glsl_type::bvec3_type), \
  971.                 _##NAME(always_available, glsl_type::bvec4_type), \
  972.                                                                   \
  973.                 _##NAME(fp64, glsl_type::double_type),  \
  974.                 _##NAME(fp64, glsl_type::dvec2_type), \
  975.                 _##NAME(fp64, glsl_type::dvec3_type), \
  976.                 _##NAME(fp64, glsl_type::dvec4_type), \
  977.                 NULL);
  978.  
  979. #define FIUD2_MIXED(NAME)                                                                 \
  980.    add_function(#NAME,                                                                   \
  981.                 _##NAME(always_available, glsl_type::float_type, glsl_type::float_type), \
  982.                 _##NAME(always_available, glsl_type::vec2_type,  glsl_type::float_type), \
  983.                 _##NAME(always_available, glsl_type::vec3_type,  glsl_type::float_type), \
  984.                 _##NAME(always_available, glsl_type::vec4_type,  glsl_type::float_type), \
  985.                                                                                          \
  986.                 _##NAME(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),  \
  987.                 _##NAME(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),  \
  988.                 _##NAME(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),  \
  989.                                                                                          \
  990.                 _##NAME(always_available, glsl_type::int_type,   glsl_type::int_type),   \
  991.                 _##NAME(always_available, glsl_type::ivec2_type, glsl_type::int_type),   \
  992.                 _##NAME(always_available, glsl_type::ivec3_type, glsl_type::int_type),   \
  993.                 _##NAME(always_available, glsl_type::ivec4_type, glsl_type::int_type),   \
  994.                                                                                          \
  995.                 _##NAME(always_available, glsl_type::ivec2_type, glsl_type::ivec2_type), \
  996.                 _##NAME(always_available, glsl_type::ivec3_type, glsl_type::ivec3_type), \
  997.                 _##NAME(always_available, glsl_type::ivec4_type, glsl_type::ivec4_type), \
  998.                                                                                          \
  999.                 _##NAME(v130, glsl_type::uint_type,  glsl_type::uint_type),              \
  1000.                 _##NAME(v130, glsl_type::uvec2_type, glsl_type::uint_type),              \
  1001.                 _##NAME(v130, glsl_type::uvec3_type, glsl_type::uint_type),              \
  1002.                 _##NAME(v130, glsl_type::uvec4_type, glsl_type::uint_type),              \
  1003.                                                                                          \
  1004.                 _##NAME(v130, glsl_type::uvec2_type, glsl_type::uvec2_type),             \
  1005.                 _##NAME(v130, glsl_type::uvec3_type, glsl_type::uvec3_type),             \
  1006.                 _##NAME(v130, glsl_type::uvec4_type, glsl_type::uvec4_type),             \
  1007.                                                                                          \
  1008.                 _##NAME(fp64, glsl_type::double_type, glsl_type::double_type),           \
  1009.                 _##NAME(fp64, glsl_type::dvec2_type, glsl_type::double_type),           \
  1010.                 _##NAME(fp64, glsl_type::dvec3_type, glsl_type::double_type),           \
  1011.                 _##NAME(fp64, glsl_type::dvec4_type, glsl_type::double_type),           \
  1012.                 _##NAME(fp64, glsl_type::dvec2_type, glsl_type::dvec2_type),           \
  1013.                 _##NAME(fp64, glsl_type::dvec3_type, glsl_type::dvec3_type),           \
  1014.                 _##NAME(fp64, glsl_type::dvec4_type, glsl_type::dvec4_type),           \
  1015.                 NULL);
  1016.  
  1017.    F(radians)
  1018.    F(degrees)
  1019.    F(sin)
  1020.    F(cos)
  1021.    F(tan)
  1022.    F(asin)
  1023.    F(acos)
  1024.  
  1025.    add_function("atan",
  1026.                 _atan(glsl_type::float_type),
  1027.                 _atan(glsl_type::vec2_type),
  1028.                 _atan(glsl_type::vec3_type),
  1029.                 _atan(glsl_type::vec4_type),
  1030.                 _atan2(glsl_type::float_type),
  1031.                 _atan2(glsl_type::vec2_type),
  1032.                 _atan2(glsl_type::vec3_type),
  1033.                 _atan2(glsl_type::vec4_type),
  1034.                 NULL);
  1035.  
  1036.    F(sinh)
  1037.    F(cosh)
  1038.    F(tanh)
  1039.    F(asinh)
  1040.    F(acosh)
  1041.    F(atanh)
  1042.    F(pow)
  1043.    F(exp)
  1044.    F(log)
  1045.    F(exp2)
  1046.    F(log2)
  1047.    FD(sqrt)
  1048.    FD(inversesqrt)
  1049.    FID(abs)
  1050.    FID(sign)
  1051.    FD(floor)
  1052.    FD(trunc)
  1053.    FD(round)
  1054.    FD(roundEven)
  1055.    FD(ceil)
  1056.    FD(fract)
  1057.  
  1058.    add_function("mod",
  1059.                 _mod(glsl_type::float_type, glsl_type::float_type),
  1060.                 _mod(glsl_type::vec2_type,  glsl_type::float_type),
  1061.                 _mod(glsl_type::vec3_type,  glsl_type::float_type),
  1062.                 _mod(glsl_type::vec4_type,  glsl_type::float_type),
  1063.  
  1064.                 _mod(glsl_type::vec2_type,  glsl_type::vec2_type),
  1065.                 _mod(glsl_type::vec3_type,  glsl_type::vec3_type),
  1066.                 _mod(glsl_type::vec4_type,  glsl_type::vec4_type),
  1067.  
  1068.                 _mod(glsl_type::double_type, glsl_type::double_type),
  1069.                 _mod(glsl_type::dvec2_type,  glsl_type::double_type),
  1070.                 _mod(glsl_type::dvec3_type,  glsl_type::double_type),
  1071.                 _mod(glsl_type::dvec4_type,  glsl_type::double_type),
  1072.  
  1073.                 _mod(glsl_type::dvec2_type,  glsl_type::dvec2_type),
  1074.                 _mod(glsl_type::dvec3_type,  glsl_type::dvec3_type),
  1075.                 _mod(glsl_type::dvec4_type,  glsl_type::dvec4_type),
  1076.                 NULL);
  1077.  
  1078.    FD(modf)
  1079.  
  1080.    FIUD2_MIXED(min)
  1081.    FIUD2_MIXED(max)
  1082.    FIUD2_MIXED(clamp)
  1083.  
  1084.    add_function("mix",
  1085.                 _mix_lrp(always_available, glsl_type::float_type, glsl_type::float_type),
  1086.                 _mix_lrp(always_available, glsl_type::vec2_type,  glsl_type::float_type),
  1087.                 _mix_lrp(always_available, glsl_type::vec3_type,  glsl_type::float_type),
  1088.                 _mix_lrp(always_available, glsl_type::vec4_type,  glsl_type::float_type),
  1089.  
  1090.                 _mix_lrp(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
  1091.                 _mix_lrp(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
  1092.                 _mix_lrp(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
  1093.  
  1094.                 _mix_lrp(fp64, glsl_type::double_type, glsl_type::double_type),
  1095.                 _mix_lrp(fp64, glsl_type::dvec2_type,  glsl_type::double_type),
  1096.                 _mix_lrp(fp64, glsl_type::dvec3_type,  glsl_type::double_type),
  1097.                 _mix_lrp(fp64, glsl_type::dvec4_type,  glsl_type::double_type),
  1098.  
  1099.                 _mix_lrp(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
  1100.                 _mix_lrp(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
  1101.                 _mix_lrp(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
  1102.  
  1103.                 _mix_sel(v130, glsl_type::float_type, glsl_type::bool_type),
  1104.                 _mix_sel(v130, glsl_type::vec2_type,  glsl_type::bvec2_type),
  1105.                 _mix_sel(v130, glsl_type::vec3_type,  glsl_type::bvec3_type),
  1106.                 _mix_sel(v130, glsl_type::vec4_type,  glsl_type::bvec4_type),
  1107.  
  1108.                 _mix_sel(fp64, glsl_type::double_type, glsl_type::bool_type),
  1109.                 _mix_sel(fp64, glsl_type::dvec2_type,  glsl_type::bvec2_type),
  1110.                 _mix_sel(fp64, glsl_type::dvec3_type,  glsl_type::bvec3_type),
  1111.                 _mix_sel(fp64, glsl_type::dvec4_type,  glsl_type::bvec4_type),
  1112.  
  1113.                 _mix_sel(shader_integer_mix, glsl_type::int_type,   glsl_type::bool_type),
  1114.                 _mix_sel(shader_integer_mix, glsl_type::ivec2_type, glsl_type::bvec2_type),
  1115.                 _mix_sel(shader_integer_mix, glsl_type::ivec3_type, glsl_type::bvec3_type),
  1116.                 _mix_sel(shader_integer_mix, glsl_type::ivec4_type, glsl_type::bvec4_type),
  1117.  
  1118.                 _mix_sel(shader_integer_mix, glsl_type::uint_type,  glsl_type::bool_type),
  1119.                 _mix_sel(shader_integer_mix, glsl_type::uvec2_type, glsl_type::bvec2_type),
  1120.                 _mix_sel(shader_integer_mix, glsl_type::uvec3_type, glsl_type::bvec3_type),
  1121.                 _mix_sel(shader_integer_mix, glsl_type::uvec4_type, glsl_type::bvec4_type),
  1122.  
  1123.                 _mix_sel(shader_integer_mix, glsl_type::bool_type,  glsl_type::bool_type),
  1124.                 _mix_sel(shader_integer_mix, glsl_type::bvec2_type, glsl_type::bvec2_type),
  1125.                 _mix_sel(shader_integer_mix, glsl_type::bvec3_type, glsl_type::bvec3_type),
  1126.                 _mix_sel(shader_integer_mix, glsl_type::bvec4_type, glsl_type::bvec4_type),
  1127.                 NULL);
  1128.  
  1129.    add_function("step",
  1130.                 _step(always_available, glsl_type::float_type, glsl_type::float_type),
  1131.                 _step(always_available, glsl_type::float_type, glsl_type::vec2_type),
  1132.                 _step(always_available, glsl_type::float_type, glsl_type::vec3_type),
  1133.                 _step(always_available, glsl_type::float_type, glsl_type::vec4_type),
  1134.  
  1135.                 _step(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
  1136.                 _step(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
  1137.                 _step(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
  1138.                 _step(fp64, glsl_type::double_type, glsl_type::double_type),
  1139.                 _step(fp64, glsl_type::double_type, glsl_type::dvec2_type),
  1140.                 _step(fp64, glsl_type::double_type, glsl_type::dvec3_type),
  1141.                 _step(fp64, glsl_type::double_type, glsl_type::dvec4_type),
  1142.  
  1143.                 _step(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
  1144.                 _step(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
  1145.                 _step(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
  1146.                 NULL);
  1147.  
  1148.    add_function("smoothstep",
  1149.                 _smoothstep(always_available, glsl_type::float_type, glsl_type::float_type),
  1150.                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec2_type),
  1151.                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec3_type),
  1152.                 _smoothstep(always_available, glsl_type::float_type, glsl_type::vec4_type),
  1153.  
  1154.                 _smoothstep(always_available, glsl_type::vec2_type,  glsl_type::vec2_type),
  1155.                 _smoothstep(always_available, glsl_type::vec3_type,  glsl_type::vec3_type),
  1156.                 _smoothstep(always_available, glsl_type::vec4_type,  glsl_type::vec4_type),
  1157.                 _smoothstep(fp64, glsl_type::double_type, glsl_type::double_type),
  1158.                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec2_type),
  1159.                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec3_type),
  1160.                 _smoothstep(fp64, glsl_type::double_type, glsl_type::dvec4_type),
  1161.  
  1162.                 _smoothstep(fp64, glsl_type::dvec2_type,  glsl_type::dvec2_type),
  1163.                 _smoothstep(fp64, glsl_type::dvec3_type,  glsl_type::dvec3_type),
  1164.                 _smoothstep(fp64, glsl_type::dvec4_type,  glsl_type::dvec4_type),
  1165.                 NULL);
  1166.  
  1167.    FD130(isnan)
  1168.    FD130(isinf)
  1169.  
  1170.    F(floatBitsToInt)
  1171.    F(floatBitsToUint)
  1172.    add_function("intBitsToFloat",
  1173.                 _intBitsToFloat(glsl_type::int_type),
  1174.                 _intBitsToFloat(glsl_type::ivec2_type),
  1175.                 _intBitsToFloat(glsl_type::ivec3_type),
  1176.                 _intBitsToFloat(glsl_type::ivec4_type),
  1177.                 NULL);
  1178.    add_function("uintBitsToFloat",
  1179.                 _uintBitsToFloat(glsl_type::uint_type),
  1180.                 _uintBitsToFloat(glsl_type::uvec2_type),
  1181.                 _uintBitsToFloat(glsl_type::uvec3_type),
  1182.                 _uintBitsToFloat(glsl_type::uvec4_type),
  1183.                 NULL);
  1184.  
  1185.    add_function("packUnorm2x16",   _packUnorm2x16(shader_packing_or_es3_or_gpu_shader5),   NULL);
  1186.    add_function("packSnorm2x16",   _packSnorm2x16(shader_packing_or_es3),                  NULL);
  1187.    add_function("packUnorm4x8",    _packUnorm4x8(shader_packing_or_es31_or_gpu_shader5),   NULL);
  1188.    add_function("packSnorm4x8",    _packSnorm4x8(shader_packing_or_es31_or_gpu_shader5),   NULL);
  1189.    add_function("unpackUnorm2x16", _unpackUnorm2x16(shader_packing_or_es3_or_gpu_shader5), NULL);
  1190.    add_function("unpackSnorm2x16", _unpackSnorm2x16(shader_packing_or_es3),                NULL);
  1191.    add_function("unpackUnorm4x8",  _unpackUnorm4x8(shader_packing_or_es31_or_gpu_shader5), NULL);
  1192.    add_function("unpackSnorm4x8",  _unpackSnorm4x8(shader_packing_or_es31_or_gpu_shader5), NULL);
  1193.    add_function("packHalf2x16",    _packHalf2x16(shader_packing_or_es3),                   NULL);
  1194.    add_function("unpackHalf2x16",  _unpackHalf2x16(shader_packing_or_es3),                 NULL);
  1195.    add_function("packDouble2x32",    _packDouble2x32(fp64),                   NULL);
  1196.    add_function("unpackDouble2x32",  _unpackDouble2x32(fp64),                 NULL);
  1197.  
  1198.  
  1199.    FD(length)
  1200.    FD(distance)
  1201.    FD(dot)
  1202.  
  1203.    add_function("cross", _cross(always_available, glsl_type::vec3_type),
  1204.                 _cross(fp64, glsl_type::dvec3_type), NULL);
  1205.  
  1206.    FD(normalize)
  1207.    add_function("ftransform", _ftransform(), NULL);
  1208.    FD(faceforward)
  1209.    FD(reflect)
  1210.    FD(refract)
  1211.    // ...
  1212.    add_function("matrixCompMult",
  1213.                 _matrixCompMult(always_available, glsl_type::mat2_type),
  1214.                 _matrixCompMult(always_available, glsl_type::mat3_type),
  1215.                 _matrixCompMult(always_available, glsl_type::mat4_type),
  1216.                 _matrixCompMult(always_available, glsl_type::mat2x3_type),
  1217.                 _matrixCompMult(always_available, glsl_type::mat2x4_type),
  1218.                 _matrixCompMult(always_available, glsl_type::mat3x2_type),
  1219.                 _matrixCompMult(always_available, glsl_type::mat3x4_type),
  1220.                 _matrixCompMult(always_available, glsl_type::mat4x2_type),
  1221.                 _matrixCompMult(always_available, glsl_type::mat4x3_type),
  1222.                 _matrixCompMult(fp64, glsl_type::dmat2_type),
  1223.                 _matrixCompMult(fp64, glsl_type::dmat3_type),
  1224.                 _matrixCompMult(fp64, glsl_type::dmat4_type),
  1225.                 _matrixCompMult(fp64, glsl_type::dmat2x3_type),
  1226.                 _matrixCompMult(fp64, glsl_type::dmat2x4_type),
  1227.                 _matrixCompMult(fp64, glsl_type::dmat3x2_type),
  1228.                 _matrixCompMult(fp64, glsl_type::dmat3x4_type),
  1229.                 _matrixCompMult(fp64, glsl_type::dmat4x2_type),
  1230.                 _matrixCompMult(fp64, glsl_type::dmat4x3_type),
  1231.                 NULL);
  1232.    add_function("outerProduct",
  1233.                 _outerProduct(v120, glsl_type::mat2_type),
  1234.                 _outerProduct(v120, glsl_type::mat3_type),
  1235.                 _outerProduct(v120, glsl_type::mat4_type),
  1236.                 _outerProduct(v120, glsl_type::mat2x3_type),
  1237.                 _outerProduct(v120, glsl_type::mat2x4_type),
  1238.                 _outerProduct(v120, glsl_type::mat3x2_type),
  1239.                 _outerProduct(v120, glsl_type::mat3x4_type),
  1240.                 _outerProduct(v120, glsl_type::mat4x2_type),
  1241.                 _outerProduct(v120, glsl_type::mat4x3_type),
  1242.                 _outerProduct(fp64, glsl_type::dmat2_type),
  1243.                 _outerProduct(fp64, glsl_type::dmat3_type),
  1244.                 _outerProduct(fp64, glsl_type::dmat4_type),
  1245.                 _outerProduct(fp64, glsl_type::dmat2x3_type),
  1246.                 _outerProduct(fp64, glsl_type::dmat2x4_type),
  1247.                 _outerProduct(fp64, glsl_type::dmat3x2_type),
  1248.                 _outerProduct(fp64, glsl_type::dmat3x4_type),
  1249.                 _outerProduct(fp64, glsl_type::dmat4x2_type),
  1250.                 _outerProduct(fp64, glsl_type::dmat4x3_type),
  1251.                 NULL);
  1252.    add_function("determinant",
  1253.                 _determinant_mat2(v120, glsl_type::mat2_type),
  1254.                 _determinant_mat3(v120, glsl_type::mat3_type),
  1255.                 _determinant_mat4(v120, glsl_type::mat4_type),
  1256.                 _determinant_mat2(fp64, glsl_type::dmat2_type),
  1257.                 _determinant_mat3(fp64, glsl_type::dmat3_type),
  1258.                 _determinant_mat4(fp64, glsl_type::dmat4_type),
  1259.  
  1260.                 NULL);
  1261.    add_function("inverse",
  1262.                 _inverse_mat2(v120, glsl_type::mat2_type),
  1263.                 _inverse_mat3(v120, glsl_type::mat3_type),
  1264.                 _inverse_mat4(v120, glsl_type::mat4_type),
  1265.                 _inverse_mat2(fp64, glsl_type::dmat2_type),
  1266.                 _inverse_mat3(fp64, glsl_type::dmat3_type),
  1267.                 _inverse_mat4(fp64, glsl_type::dmat4_type),
  1268.                 NULL);
  1269.    add_function("transpose",
  1270.                 _transpose(v120, glsl_type::mat2_type),
  1271.                 _transpose(v120, glsl_type::mat3_type),
  1272.                 _transpose(v120, glsl_type::mat4_type),
  1273.                 _transpose(v120, glsl_type::mat2x3_type),
  1274.                 _transpose(v120, glsl_type::mat2x4_type),
  1275.                 _transpose(v120, glsl_type::mat3x2_type),
  1276.                 _transpose(v120, glsl_type::mat3x4_type),
  1277.                 _transpose(v120, glsl_type::mat4x2_type),
  1278.                 _transpose(v120, glsl_type::mat4x3_type),
  1279.                 _transpose(fp64, glsl_type::dmat2_type),
  1280.                 _transpose(fp64, glsl_type::dmat3_type),
  1281.                 _transpose(fp64, glsl_type::dmat4_type),
  1282.                 _transpose(fp64, glsl_type::dmat2x3_type),
  1283.                 _transpose(fp64, glsl_type::dmat2x4_type),
  1284.                 _transpose(fp64, glsl_type::dmat3x2_type),
  1285.                 _transpose(fp64, glsl_type::dmat3x4_type),
  1286.                 _transpose(fp64, glsl_type::dmat4x2_type),
  1287.                 _transpose(fp64, glsl_type::dmat4x3_type),
  1288.                 NULL);
  1289.    FIUD(lessThan)
  1290.    FIUD(lessThanEqual)
  1291.    FIUD(greaterThan)
  1292.    FIUD(greaterThanEqual)
  1293.    FIUBD(notEqual)
  1294.    FIUBD(equal)
  1295.  
  1296.    add_function("any",
  1297.                 _any(glsl_type::bvec2_type),
  1298.                 _any(glsl_type::bvec3_type),
  1299.                 _any(glsl_type::bvec4_type),
  1300.                 NULL);
  1301.  
  1302.    add_function("all",
  1303.                 _all(glsl_type::bvec2_type),
  1304.                 _all(glsl_type::bvec3_type),
  1305.                 _all(glsl_type::bvec4_type),
  1306.                 NULL);
  1307.  
  1308.    add_function("not",
  1309.                 _not(glsl_type::bvec2_type),
  1310.                 _not(glsl_type::bvec3_type),
  1311.                 _not(glsl_type::bvec4_type),
  1312.                 NULL);
  1313.  
  1314.    add_function("textureSize",
  1315.                 _textureSize(v130, glsl_type::int_type,   glsl_type::sampler1D_type),
  1316.                 _textureSize(v130, glsl_type::int_type,   glsl_type::isampler1D_type),
  1317.                 _textureSize(v130, glsl_type::int_type,   glsl_type::usampler1D_type),
  1318.  
  1319.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2D_type),
  1320.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler2D_type),
  1321.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler2D_type),
  1322.  
  1323.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler3D_type),
  1324.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::isampler3D_type),
  1325.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::usampler3D_type),
  1326.  
  1327.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::samplerCube_type),
  1328.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isamplerCube_type),
  1329.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usamplerCube_type),
  1330.  
  1331.                 _textureSize(v130, glsl_type::int_type,   glsl_type::sampler1DShadow_type),
  1332.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DShadow_type),
  1333.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::samplerCubeShadow_type),
  1334.  
  1335.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler1DArray_type),
  1336.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler1DArray_type),
  1337.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler1DArray_type),
  1338.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler2DArray_type),
  1339.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::isampler2DArray_type),
  1340.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::usampler2DArray_type),
  1341.  
  1342.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler1DArrayShadow_type),
  1343.                 _textureSize(v130, glsl_type::ivec3_type, glsl_type::sampler2DArrayShadow_type),
  1344.  
  1345.                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::samplerCubeArray_type),
  1346.                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::isamplerCubeArray_type),
  1347.                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::usamplerCubeArray_type),
  1348.                 _textureSize(texture_cube_map_array, glsl_type::ivec3_type, glsl_type::samplerCubeArrayShadow_type),
  1349.  
  1350.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DRect_type),
  1351.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::isampler2DRect_type),
  1352.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::usampler2DRect_type),
  1353.                 _textureSize(v130, glsl_type::ivec2_type, glsl_type::sampler2DRectShadow_type),
  1354.  
  1355.                 _textureSize(v140, glsl_type::int_type,   glsl_type::samplerBuffer_type),
  1356.                 _textureSize(v140, glsl_type::int_type,   glsl_type::isamplerBuffer_type),
  1357.                 _textureSize(v140, glsl_type::int_type,   glsl_type::usamplerBuffer_type),
  1358.                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::sampler2DMS_type),
  1359.                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::isampler2DMS_type),
  1360.                 _textureSize(texture_multisample, glsl_type::ivec2_type, glsl_type::usampler2DMS_type),
  1361.  
  1362.                 _textureSize(texture_multisample, glsl_type::ivec3_type, glsl_type::sampler2DMSArray_type),
  1363.                 _textureSize(texture_multisample, glsl_type::ivec3_type, glsl_type::isampler2DMSArray_type),
  1364.                 _textureSize(texture_multisample, glsl_type::ivec3_type, glsl_type::usampler2DMSArray_type),
  1365.                 NULL);
  1366.  
  1367.    add_function("texture",
  1368.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
  1369.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
  1370.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
  1371.  
  1372.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
  1373.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
  1374.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
  1375.  
  1376.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
  1377.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
  1378.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
  1379.  
  1380.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
  1381.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
  1382.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
  1383.  
  1384.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
  1385.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
  1386.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
  1387.  
  1388.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
  1389.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
  1390.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
  1391.  
  1392.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
  1393.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
  1394.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
  1395.  
  1396.                 _texture(ir_tex, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
  1397.                 _texture(ir_tex, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
  1398.                 _texture(ir_tex, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
  1399.  
  1400.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  1401.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
  1402.                 /* samplerCubeArrayShadow is special; it has an extra parameter
  1403.                  * for the shadow comparitor since there is no vec5 type.
  1404.                  */
  1405.                 _textureCubeArrayShadow(),
  1406.  
  1407.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type),
  1408.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
  1409.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
  1410.  
  1411.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
  1412.  
  1413.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
  1414.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
  1415.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
  1416.  
  1417.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
  1418.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
  1419.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
  1420.  
  1421.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
  1422.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
  1423.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
  1424.  
  1425.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
  1426.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
  1427.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
  1428.  
  1429.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
  1430.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
  1431.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
  1432.  
  1433.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
  1434.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
  1435.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
  1436.  
  1437.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
  1438.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
  1439.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
  1440.  
  1441.                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
  1442.                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
  1443.                 _texture(ir_txb, fs_texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
  1444.  
  1445.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  1446.                 NULL);
  1447.  
  1448.    add_function("textureLod",
  1449.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
  1450.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
  1451.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
  1452.  
  1453.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
  1454.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
  1455.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
  1456.  
  1457.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
  1458.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
  1459.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
  1460.  
  1461.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
  1462.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
  1463.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
  1464.  
  1465.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
  1466.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
  1467.  
  1468.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
  1469.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
  1470.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
  1471.  
  1472.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
  1473.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
  1474.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
  1475.  
  1476.                 _texture(ir_txl, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
  1477.                 _texture(ir_txl, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
  1478.                 _texture(ir_txl, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
  1479.  
  1480.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  1481.                 NULL);
  1482.  
  1483.    add_function("textureOffset",
  1484.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
  1485.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1486.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1487.  
  1488.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
  1489.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1490.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1491.  
  1492.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
  1493.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1494.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1495.  
  1496.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type, TEX_OFFSET),
  1497.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
  1498.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
  1499.  
  1500.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1501.  
  1502.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1503.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1504.  
  1505.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
  1506.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1507.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1508.  
  1509.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
  1510.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1511.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1512.  
  1513.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1514.  
  1515.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
  1516.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1517.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1518.  
  1519.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
  1520.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1521.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1522.  
  1523.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
  1524.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1525.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1526.  
  1527.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1528.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1529.  
  1530.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
  1531.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1532.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1533.  
  1534.                 _texture(ir_txb, v130_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
  1535.                 _texture(ir_txb, v130_fs_only, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1536.                 _texture(ir_txb, v130_fs_only, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1537.  
  1538.                 _texture(ir_txb, v130_fs_only, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1539.                 NULL);
  1540.  
  1541.    add_function("textureProj",
  1542.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
  1543.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1544.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1545.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1546.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1547.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1548.  
  1549.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
  1550.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1551.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1552.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1553.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1554.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1555.  
  1556.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1557.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1558.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1559.  
  1560.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1561.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1562.  
  1563.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT),
  1564.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  1565.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  1566.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT),
  1567.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  1568.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  1569.  
  1570.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1571.  
  1572.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
  1573.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1574.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1575.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1576.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1577.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1578.  
  1579.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
  1580.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1581.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1582.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1583.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1584.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1585.  
  1586.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1587.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1588.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1589.  
  1590.                 _texture(ir_txb, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1591.                 _texture(ir_txb, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1592.                 NULL);
  1593.  
  1594.    add_function("texelFetch",
  1595.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::int_type),
  1596.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::int_type),
  1597.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::int_type),
  1598.  
  1599.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::ivec2_type),
  1600.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::ivec2_type),
  1601.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::ivec2_type),
  1602.  
  1603.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::ivec3_type),
  1604.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::ivec3_type),
  1605.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::ivec3_type),
  1606.  
  1607.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::ivec2_type),
  1608.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::ivec2_type),
  1609.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::ivec2_type),
  1610.  
  1611.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::ivec2_type),
  1612.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::ivec2_type),
  1613.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::ivec2_type),
  1614.  
  1615.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::ivec3_type),
  1616.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::ivec3_type),
  1617.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::ivec3_type),
  1618.  
  1619.                 _texelFetch(v140, glsl_type::vec4_type,  glsl_type::samplerBuffer_type,  glsl_type::int_type),
  1620.                 _texelFetch(v140, glsl_type::ivec4_type, glsl_type::isamplerBuffer_type, glsl_type::int_type),
  1621.                 _texelFetch(v140, glsl_type::uvec4_type, glsl_type::usamplerBuffer_type, glsl_type::int_type),
  1622.  
  1623.                 _texelFetch(texture_multisample, glsl_type::vec4_type,  glsl_type::sampler2DMS_type,  glsl_type::ivec2_type),
  1624.                 _texelFetch(texture_multisample, glsl_type::ivec4_type, glsl_type::isampler2DMS_type, glsl_type::ivec2_type),
  1625.                 _texelFetch(texture_multisample, glsl_type::uvec4_type, glsl_type::usampler2DMS_type, glsl_type::ivec2_type),
  1626.  
  1627.                 _texelFetch(texture_multisample, glsl_type::vec4_type,  glsl_type::sampler2DMSArray_type,  glsl_type::ivec3_type),
  1628.                 _texelFetch(texture_multisample, glsl_type::ivec4_type, glsl_type::isampler2DMSArray_type, glsl_type::ivec3_type),
  1629.                 _texelFetch(texture_multisample, glsl_type::uvec4_type, glsl_type::usampler2DMSArray_type, glsl_type::ivec3_type),
  1630.                 NULL);
  1631.  
  1632.    add_function("texelFetchOffset",
  1633.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::int_type, glsl_type::int_type),
  1634.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::int_type, glsl_type::int_type),
  1635.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::int_type, glsl_type::int_type),
  1636.  
  1637.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::ivec2_type, glsl_type::ivec2_type),
  1638.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
  1639.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
  1640.  
  1641.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::ivec3_type, glsl_type::ivec3_type),
  1642.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::ivec3_type, glsl_type::ivec3_type),
  1643.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::ivec3_type, glsl_type::ivec3_type),
  1644.  
  1645.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::ivec2_type, glsl_type::ivec2_type),
  1646.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
  1647.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::ivec2_type, glsl_type::ivec2_type),
  1648.  
  1649.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::ivec2_type, glsl_type::int_type),
  1650.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::ivec2_type, glsl_type::int_type),
  1651.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::ivec2_type, glsl_type::int_type),
  1652.  
  1653.                 _texelFetch(v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::ivec3_type, glsl_type::ivec2_type),
  1654.                 _texelFetch(v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::ivec3_type, glsl_type::ivec2_type),
  1655.                 _texelFetch(v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::ivec3_type, glsl_type::ivec2_type),
  1656.  
  1657.                 NULL);
  1658.  
  1659.    add_function("textureProjOffset",
  1660.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1661.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1662.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1663.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1664.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1665.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1666.  
  1667.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1668.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1669.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1670.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1671.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1672.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1673.  
  1674.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1675.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1676.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1677.  
  1678.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1679.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1680.  
  1681.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1682.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1683.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1684.                 _texture(ir_tex, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1685.                 _texture(ir_tex, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1686.                 _texture(ir_tex, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1687.  
  1688.                 _texture(ir_tex, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1689.  
  1690.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1691.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1692.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1693.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1694.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1695.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1696.  
  1697.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1698.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1699.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1700.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1701.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1702.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1703.  
  1704.                 _texture(ir_txb, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1705.                 _texture(ir_txb, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1706.                 _texture(ir_txb, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1707.  
  1708.                 _texture(ir_txb, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1709.                 _texture(ir_txb, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1710.                 NULL);
  1711.  
  1712.    add_function("textureLodOffset",
  1713.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
  1714.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1715.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1716.  
  1717.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
  1718.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1719.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1720.  
  1721.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
  1722.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1723.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1724.  
  1725.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1726.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1727.  
  1728.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
  1729.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1730.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1731.  
  1732.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
  1733.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1734.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1735.  
  1736.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1737.                 NULL);
  1738.  
  1739.    add_function("textureProjLod",
  1740.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
  1741.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1742.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1743.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1744.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1745.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1746.  
  1747.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
  1748.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1749.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1750.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1751.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1752.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1753.  
  1754.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1755.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1756.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1757.  
  1758.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1759.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1760.                 NULL);
  1761.  
  1762.    add_function("textureProjLodOffset",
  1763.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1764.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1765.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1766.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1767.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1768.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1769.  
  1770.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1771.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1772.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1773.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1774.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1775.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1776.  
  1777.                 _texture(ir_txl, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1778.                 _texture(ir_txl, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1779.                 _texture(ir_txl, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1780.  
  1781.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1782.                 _texture(ir_txl, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1783.                 NULL);
  1784.  
  1785.    add_function("textureGrad",
  1786.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type),
  1787.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type),
  1788.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type),
  1789.  
  1790.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type),
  1791.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
  1792.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
  1793.  
  1794.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type),
  1795.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type),
  1796.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type),
  1797.  
  1798.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::samplerCube_type,  glsl_type::vec3_type),
  1799.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
  1800.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
  1801.  
  1802.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type),
  1803.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
  1804.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
  1805.  
  1806.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
  1807.  
  1808.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type,   glsl_type::vec3_type),
  1809.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type,   glsl_type::vec3_type),
  1810.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::samplerCubeShadow_type, glsl_type::vec4_type),
  1811.  
  1812.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type),
  1813.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type),
  1814.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type),
  1815.  
  1816.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type),
  1817.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
  1818.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
  1819.  
  1820.                 _texture(ir_txd, texture_cube_map_array, glsl_type::vec4_type,  glsl_type::samplerCubeArray_type,  glsl_type::vec4_type),
  1821.                 _texture(ir_txd, texture_cube_map_array, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
  1822.                 _texture(ir_txd, texture_cube_map_array, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
  1823.  
  1824.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  1825.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
  1826.                 NULL);
  1827.  
  1828.    add_function("textureGradOffset",
  1829.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::float_type, TEX_OFFSET),
  1830.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1831.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::float_type, TEX_OFFSET),
  1832.  
  1833.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec2_type, TEX_OFFSET),
  1834.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1835.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  1836.  
  1837.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec3_type, TEX_OFFSET),
  1838.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1839.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec3_type, TEX_OFFSET),
  1840.  
  1841.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec2_type, TEX_OFFSET),
  1842.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
  1843.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET),
  1844.  
  1845.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1846.  
  1847.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1848.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1849.  
  1850.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1DArray_type,  glsl_type::vec2_type, TEX_OFFSET),
  1851.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1852.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1DArray_type, glsl_type::vec2_type, TEX_OFFSET),
  1853.  
  1854.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DArray_type,  glsl_type::vec3_type, TEX_OFFSET),
  1855.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1856.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  1857.  
  1858.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  1859.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type, TEX_OFFSET),
  1860.                 NULL);
  1861.  
  1862.    add_function("textureProjGrad",
  1863.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT),
  1864.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1865.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  1866.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1867.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1868.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  1869.  
  1870.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT),
  1871.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1872.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  1873.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1874.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1875.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  1876.  
  1877.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT),
  1878.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1879.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  1880.  
  1881.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT),
  1882.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  1883.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  1884.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT),
  1885.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  1886.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  1887.  
  1888.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1889.  
  1890.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1891.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  1892.                 NULL);
  1893.  
  1894.    add_function("textureProjGradOffset",
  1895.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1896.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1897.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec2_type, TEX_PROJECT | TEX_OFFSET),
  1898.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler1D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1899.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1900.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler1D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1901.  
  1902.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1903.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1904.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1905.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1906.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1907.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1908.  
  1909.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler3D_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1910.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1911.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler3D_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1912.  
  1913.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1914.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1915.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT | TEX_OFFSET),
  1916.                 _texture(ir_txd, v130, glsl_type::vec4_type,  glsl_type::sampler2DRect_type,  glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1917.                 _texture(ir_txd, v130, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1918.                 _texture(ir_txd, v130, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1919.  
  1920.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1921.  
  1922.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1923.                 _texture(ir_txd, v130, glsl_type::float_type, glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT | TEX_OFFSET),
  1924.                 NULL);
  1925.  
  1926.    add_function("EmitVertex",   _EmitVertex(),   NULL);
  1927.    add_function("EndPrimitive", _EndPrimitive(), NULL);
  1928.    add_function("EmitStreamVertex",
  1929.                 _EmitStreamVertex(gs_streams, glsl_type::uint_type),
  1930.                 _EmitStreamVertex(gs_streams, glsl_type::int_type),
  1931.                 NULL);
  1932.    add_function("EndStreamPrimitive",
  1933.                 _EndStreamPrimitive(gs_streams, glsl_type::uint_type),
  1934.                 _EndStreamPrimitive(gs_streams, glsl_type::int_type),
  1935.                 NULL);
  1936.  
  1937.    add_function("textureQueryLOD",
  1938.                 _textureQueryLod(glsl_type::sampler1D_type,  glsl_type::float_type),
  1939.                 _textureQueryLod(glsl_type::isampler1D_type, glsl_type::float_type),
  1940.                 _textureQueryLod(glsl_type::usampler1D_type, glsl_type::float_type),
  1941.  
  1942.                 _textureQueryLod(glsl_type::sampler2D_type,  glsl_type::vec2_type),
  1943.                 _textureQueryLod(glsl_type::isampler2D_type, glsl_type::vec2_type),
  1944.                 _textureQueryLod(glsl_type::usampler2D_type, glsl_type::vec2_type),
  1945.  
  1946.                 _textureQueryLod(glsl_type::sampler3D_type,  glsl_type::vec3_type),
  1947.                 _textureQueryLod(glsl_type::isampler3D_type, glsl_type::vec3_type),
  1948.                 _textureQueryLod(glsl_type::usampler3D_type, glsl_type::vec3_type),
  1949.  
  1950.                 _textureQueryLod(glsl_type::samplerCube_type,  glsl_type::vec3_type),
  1951.                 _textureQueryLod(glsl_type::isamplerCube_type, glsl_type::vec3_type),
  1952.                 _textureQueryLod(glsl_type::usamplerCube_type, glsl_type::vec3_type),
  1953.  
  1954.                 _textureQueryLod(glsl_type::sampler1DArray_type,  glsl_type::float_type),
  1955.                 _textureQueryLod(glsl_type::isampler1DArray_type, glsl_type::float_type),
  1956.                 _textureQueryLod(glsl_type::usampler1DArray_type, glsl_type::float_type),
  1957.  
  1958.                 _textureQueryLod(glsl_type::sampler2DArray_type,  glsl_type::vec2_type),
  1959.                 _textureQueryLod(glsl_type::isampler2DArray_type, glsl_type::vec2_type),
  1960.                 _textureQueryLod(glsl_type::usampler2DArray_type, glsl_type::vec2_type),
  1961.  
  1962.                 _textureQueryLod(glsl_type::samplerCubeArray_type,  glsl_type::vec3_type),
  1963.                 _textureQueryLod(glsl_type::isamplerCubeArray_type, glsl_type::vec3_type),
  1964.                 _textureQueryLod(glsl_type::usamplerCubeArray_type, glsl_type::vec3_type),
  1965.  
  1966.                 _textureQueryLod(glsl_type::sampler1DShadow_type, glsl_type::float_type),
  1967.                 _textureQueryLod(glsl_type::sampler2DShadow_type, glsl_type::vec2_type),
  1968.                 _textureQueryLod(glsl_type::samplerCubeShadow_type, glsl_type::vec3_type),
  1969.                 _textureQueryLod(glsl_type::sampler1DArrayShadow_type, glsl_type::float_type),
  1970.                 _textureQueryLod(glsl_type::sampler2DArrayShadow_type, glsl_type::vec2_type),
  1971.                 _textureQueryLod(glsl_type::samplerCubeArrayShadow_type, glsl_type::vec3_type),
  1972.                 NULL);
  1973.  
  1974.    add_function("textureQueryLevels",
  1975.                 _textureQueryLevels(glsl_type::sampler1D_type),
  1976.                 _textureQueryLevels(glsl_type::sampler2D_type),
  1977.                 _textureQueryLevels(glsl_type::sampler3D_type),
  1978.                 _textureQueryLevels(glsl_type::samplerCube_type),
  1979.                 _textureQueryLevels(glsl_type::sampler1DArray_type),
  1980.                 _textureQueryLevels(glsl_type::sampler2DArray_type),
  1981.                 _textureQueryLevels(glsl_type::samplerCubeArray_type),
  1982.                 _textureQueryLevels(glsl_type::sampler1DShadow_type),
  1983.                 _textureQueryLevels(glsl_type::sampler2DShadow_type),
  1984.                 _textureQueryLevels(glsl_type::samplerCubeShadow_type),
  1985.                 _textureQueryLevels(glsl_type::sampler1DArrayShadow_type),
  1986.                 _textureQueryLevels(glsl_type::sampler2DArrayShadow_type),
  1987.                 _textureQueryLevels(glsl_type::samplerCubeArrayShadow_type),
  1988.  
  1989.                 _textureQueryLevels(glsl_type::isampler1D_type),
  1990.                 _textureQueryLevels(glsl_type::isampler2D_type),
  1991.                 _textureQueryLevels(glsl_type::isampler3D_type),
  1992.                 _textureQueryLevels(glsl_type::isamplerCube_type),
  1993.                 _textureQueryLevels(glsl_type::isampler1DArray_type),
  1994.                 _textureQueryLevels(glsl_type::isampler2DArray_type),
  1995.                 _textureQueryLevels(glsl_type::isamplerCubeArray_type),
  1996.  
  1997.                 _textureQueryLevels(glsl_type::usampler1D_type),
  1998.                 _textureQueryLevels(glsl_type::usampler2D_type),
  1999.                 _textureQueryLevels(glsl_type::usampler3D_type),
  2000.                 _textureQueryLevels(glsl_type::usamplerCube_type),
  2001.                 _textureQueryLevels(glsl_type::usampler1DArray_type),
  2002.                 _textureQueryLevels(glsl_type::usampler2DArray_type),
  2003.                 _textureQueryLevels(glsl_type::usamplerCubeArray_type),
  2004.  
  2005.                 NULL);
  2006.  
  2007.    add_function("texture1D",
  2008.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
  2009.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
  2010.                 NULL);
  2011.  
  2012.    add_function("texture1DArray",
  2013.                 _texture(ir_tex, texture_array,    glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
  2014.                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
  2015.                 NULL);
  2016.  
  2017.    add_function("texture1DProj",
  2018.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  2019.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  2020.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  2021.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  2022.                 NULL);
  2023.  
  2024.    add_function("texture1DLod",
  2025.                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
  2026.                 NULL);
  2027.  
  2028.    add_function("texture1DArrayLod",
  2029.                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler1DArray_type, glsl_type::vec2_type),
  2030.                 NULL);
  2031.  
  2032.    add_function("texture1DProjLod",
  2033.                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  2034.                 _texture(ir_txl, tex1d_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  2035.                 NULL);
  2036.  
  2037.    add_function("texture2D",
  2038.                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
  2039.                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
  2040.                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec2_type),
  2041.                 NULL);
  2042.  
  2043.    add_function("texture2DArray",
  2044.                 _texture(ir_tex, texture_array,    glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
  2045.                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
  2046.                 NULL);
  2047.  
  2048.    add_function("texture2DProj",
  2049.                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  2050.                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  2051.                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  2052.                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  2053.                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec3_type, TEX_PROJECT),
  2054.                 _texture(ir_tex, texture_external, glsl_type::vec4_type,  glsl_type::samplerExternalOES_type, glsl_type::vec4_type, TEX_PROJECT),
  2055.                 NULL);
  2056.  
  2057.    add_function("texture2DLod",
  2058.                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
  2059.                 NULL);
  2060.  
  2061.    add_function("texture2DArrayLod",
  2062.                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
  2063.                 NULL);
  2064.  
  2065.    add_function("texture2DProjLod",
  2066.                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  2067.                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  2068.                 NULL);
  2069.  
  2070.    add_function("texture3D",
  2071.                 _texture(ir_tex, tex3d,    glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
  2072.                 _texture(ir_txb, fs_tex3d, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
  2073.                 NULL);
  2074.  
  2075.    add_function("texture3DProj",
  2076.                 _texture(ir_tex, tex3d,    glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  2077.                 _texture(ir_txb, fs_tex3d, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  2078.                 NULL);
  2079.  
  2080.    add_function("texture3DLod",
  2081.                 _texture(ir_txl, tex3d_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
  2082.                 NULL);
  2083.  
  2084.    add_function("texture3DProjLod",
  2085.                 _texture(ir_txl, tex3d_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  2086.                 NULL);
  2087.  
  2088.    add_function("textureCube",
  2089.                 _texture(ir_tex, always_available, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
  2090.                 _texture(ir_txb, fs_only,          glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
  2091.                 NULL);
  2092.  
  2093.    add_function("textureCubeLod",
  2094.                 _texture(ir_txl, lod_exists_in_stage, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
  2095.                 NULL);
  2096.  
  2097.    add_function("texture2DRect",
  2098.                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec2_type),
  2099.                 NULL);
  2100.  
  2101.    add_function("texture2DRectProj",
  2102.                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  2103.                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  2104.                 NULL);
  2105.  
  2106.    add_function("shadow1D",
  2107.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
  2108.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
  2109.                 NULL);
  2110.  
  2111.    add_function("shadow1DArray",
  2112.                 _texture(ir_tex, texture_array,    glsl_type::vec4_type,  glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  2113.                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type,  glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  2114.                 NULL);
  2115.  
  2116.    add_function("shadow2D",
  2117.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
  2118.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
  2119.                 NULL);
  2120.  
  2121.    add_function("shadow2DArray",
  2122.                 _texture(ir_tex, texture_array,    glsl_type::vec4_type,  glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
  2123.                 _texture(ir_txb, fs_texture_array, glsl_type::vec4_type,  glsl_type::sampler2DArrayShadow_type, glsl_type::vec4_type),
  2124.                 NULL);
  2125.  
  2126.    add_function("shadow1DProj",
  2127.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2128.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2129.                 NULL);
  2130.  
  2131.    add_function("shadow2DProj",
  2132.                 _texture(ir_tex, v110,         glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2133.                 _texture(ir_txb, v110_fs_only, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2134.                 NULL);
  2135.  
  2136.    add_function("shadow1DLod",
  2137.                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
  2138.                 NULL);
  2139.  
  2140.    add_function("shadow2DLod",
  2141.                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
  2142.                 NULL);
  2143.  
  2144.    add_function("shadow1DArrayLod",
  2145.                 _texture(ir_txl, texture_array_lod, glsl_type::vec4_type, glsl_type::sampler1DArrayShadow_type, glsl_type::vec3_type),
  2146.                 NULL);
  2147.  
  2148.    add_function("shadow1DProjLod",
  2149.                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2150.                 NULL);
  2151.  
  2152.    add_function("shadow2DProjLod",
  2153.                 _texture(ir_txl, v110_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2154.                 NULL);
  2155.  
  2156.    add_function("shadow2DRect",
  2157.                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
  2158.                 NULL);
  2159.  
  2160.    add_function("shadow2DRectProj",
  2161.                 _texture(ir_tex, texture_rectangle, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2162.                 NULL);
  2163.  
  2164.    add_function("texture1DGradARB",
  2165.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::float_type),
  2166.                 NULL);
  2167.  
  2168.    add_function("texture1DProjGradARB",
  2169.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec2_type, TEX_PROJECT),
  2170.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1D_type, glsl_type::vec4_type, TEX_PROJECT),
  2171.                 NULL);
  2172.  
  2173.    add_function("texture2DGradARB",
  2174.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec2_type),
  2175.                 NULL);
  2176.  
  2177.    add_function("texture2DProjGradARB",
  2178.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec3_type, TEX_PROJECT),
  2179.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2D_type, glsl_type::vec4_type, TEX_PROJECT),
  2180.                 NULL);
  2181.  
  2182.    add_function("texture3DGradARB",
  2183.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec3_type),
  2184.                 NULL);
  2185.  
  2186.    add_function("texture3DProjGradARB",
  2187.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler3D_type, glsl_type::vec4_type, TEX_PROJECT),
  2188.                 NULL);
  2189.  
  2190.    add_function("textureCubeGradARB",
  2191.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::samplerCube_type, glsl_type::vec3_type),
  2192.                 NULL);
  2193.  
  2194.    add_function("shadow1DGradARB",
  2195.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec3_type),
  2196.                 NULL);
  2197.  
  2198.    add_function("shadow1DProjGradARB",
  2199.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler1DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2200.                 NULL);
  2201.  
  2202.    add_function("shadow2DGradARB",
  2203.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec3_type),
  2204.                 NULL);
  2205.  
  2206.    add_function("shadow2DProjGradARB",
  2207.                 _texture(ir_txd, shader_texture_lod, glsl_type::vec4_type,  glsl_type::sampler2DShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2208.                 NULL);
  2209.  
  2210.    add_function("texture2DRectGradARB",
  2211.                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec2_type),
  2212.                 NULL);
  2213.  
  2214.    add_function("texture2DRectProjGradARB",
  2215.                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec3_type, TEX_PROJECT),
  2216.                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRect_type, glsl_type::vec4_type, TEX_PROJECT),
  2217.                 NULL);
  2218.  
  2219.    add_function("shadow2DRectGradARB",
  2220.                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec3_type),
  2221.                 NULL);
  2222.  
  2223.    add_function("shadow2DRectProjGradARB",
  2224.                 _texture(ir_txd, shader_texture_lod_and_rect, glsl_type::vec4_type,  glsl_type::sampler2DRectShadow_type, glsl_type::vec4_type, TEX_PROJECT),
  2225.                 NULL);
  2226.  
  2227.    add_function("textureGather",
  2228.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type),
  2229.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type),
  2230.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type),
  2231.  
  2232.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type),
  2233.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type),
  2234.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type),
  2235.  
  2236.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type),
  2237.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type),
  2238.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type),
  2239.  
  2240.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::vec4_type, glsl_type::samplerCube_type, glsl_type::vec3_type),
  2241.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type),
  2242.                 _texture(ir_tg4, texture_gather_or_es31, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type),
  2243.  
  2244.                 _texture(ir_tg4, texture_gather, glsl_type::vec4_type, glsl_type::samplerCubeArray_type, glsl_type::vec4_type),
  2245.                 _texture(ir_tg4, texture_gather, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type),
  2246.                 _texture(ir_tg4, texture_gather, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type),
  2247.  
  2248.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
  2249.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
  2250.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_COMPONENT),
  2251.  
  2252.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
  2253.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
  2254.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_COMPONENT),
  2255.  
  2256.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
  2257.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
  2258.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_COMPONENT),
  2259.  
  2260.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::samplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
  2261.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::ivec4_type, glsl_type::isamplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
  2262.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::uvec4_type, glsl_type::usamplerCube_type, glsl_type::vec3_type, TEX_COMPONENT),
  2263.  
  2264.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::samplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
  2265.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isamplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
  2266.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usamplerCubeArray_type, glsl_type::vec4_type, TEX_COMPONENT),
  2267.  
  2268.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type),
  2269.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type),
  2270.                 _texture(ir_tg4, gpu_shader5_or_es31, glsl_type::vec4_type, glsl_type::samplerCubeShadow_type, glsl_type::vec3_type),
  2271.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::samplerCubeArrayShadow_type, glsl_type::vec4_type),
  2272.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type),
  2273.                 NULL);
  2274.  
  2275.    add_function("textureGatherOffset",
  2276.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  2277.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  2278.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET),
  2279.  
  2280.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  2281.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  2282.                 _texture(ir_tg4, texture_gather_only_or_es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET),
  2283.  
  2284.                 _texture(ir_tg4, es31, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
  2285.                 _texture(ir_tg4, es31, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
  2286.                 _texture(ir_tg4, es31, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET | TEX_COMPONENT),
  2287.  
  2288.                 _texture(ir_tg4, es31, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
  2289.                 _texture(ir_tg4, es31, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
  2290.                 _texture(ir_tg4, es31, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET | TEX_COMPONENT),
  2291.  
  2292.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2293.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2294.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2295.  
  2296.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
  2297.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
  2298.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
  2299.  
  2300.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2301.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2302.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2303.  
  2304.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2305.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2306.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2307.  
  2308.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2309.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2310.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2311.  
  2312.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2313.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2314.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST | TEX_COMPONENT),
  2315.  
  2316.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2317.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET_NONCONST),
  2318.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type, TEX_OFFSET_NONCONST),
  2319.  
  2320.                 _texture(ir_tg4, es31, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET),
  2321.                 _texture(ir_tg4, es31, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET),
  2322.                 NULL);
  2323.  
  2324.    add_function("textureGatherOffsets",
  2325.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2326.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2327.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2328.  
  2329.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2330.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2331.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2D_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2332.  
  2333.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
  2334.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
  2335.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
  2336.  
  2337.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2338.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2339.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DArray_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2340.  
  2341.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2342.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2343.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2344.  
  2345.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2346.                 _texture(ir_tg4, gpu_shader5, glsl_type::ivec4_type, glsl_type::isampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2347.                 _texture(ir_tg4, gpu_shader5, glsl_type::uvec4_type, glsl_type::usampler2DRect_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY | TEX_COMPONENT),
  2348.  
  2349.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DShadow_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2350.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DArrayShadow_type, glsl_type::vec3_type, TEX_OFFSET_ARRAY),
  2351.                 _texture(ir_tg4, gpu_shader5, glsl_type::vec4_type, glsl_type::sampler2DRectShadow_type, glsl_type::vec2_type, TEX_OFFSET_ARRAY),
  2352.                 NULL);
  2353.  
  2354.    F(dFdx)
  2355.    F(dFdy)
  2356.    F(fwidth)
  2357.    F(dFdxCoarse)
  2358.    F(dFdyCoarse)
  2359.    F(fwidthCoarse)
  2360.    F(dFdxFine)
  2361.    F(dFdyFine)
  2362.    F(fwidthFine)
  2363.    F(noise1)
  2364.    F(noise2)
  2365.    F(noise3)
  2366.    F(noise4)
  2367.  
  2368.    IU(bitfieldExtract)
  2369.    IU(bitfieldInsert)
  2370.    IU(bitfieldReverse)
  2371.    IU(bitCount)
  2372.    IU(findLSB)
  2373.    IU(findMSB)
  2374.    FDGS5(fma)
  2375.  
  2376.    add_function("ldexp",
  2377.                 _ldexp(glsl_type::float_type, glsl_type::int_type),
  2378.                 _ldexp(glsl_type::vec2_type,  glsl_type::ivec2_type),
  2379.                 _ldexp(glsl_type::vec3_type,  glsl_type::ivec3_type),
  2380.                 _ldexp(glsl_type::vec4_type,  glsl_type::ivec4_type),
  2381.                 _ldexp(glsl_type::double_type, glsl_type::int_type),
  2382.                 _ldexp(glsl_type::dvec2_type,  glsl_type::ivec2_type),
  2383.                 _ldexp(glsl_type::dvec3_type,  glsl_type::ivec3_type),
  2384.                 _ldexp(glsl_type::dvec4_type,  glsl_type::ivec4_type),
  2385.                 NULL);
  2386.  
  2387.    add_function("frexp",
  2388.                 _frexp(glsl_type::float_type, glsl_type::int_type),
  2389.                 _frexp(glsl_type::vec2_type,  glsl_type::ivec2_type),
  2390.                 _frexp(glsl_type::vec3_type,  glsl_type::ivec3_type),
  2391.                 _frexp(glsl_type::vec4_type,  glsl_type::ivec4_type),
  2392.                 _dfrexp(glsl_type::double_type, glsl_type::int_type),
  2393.                 _dfrexp(glsl_type::dvec2_type,  glsl_type::ivec2_type),
  2394.                 _dfrexp(glsl_type::dvec3_type,  glsl_type::ivec3_type),
  2395.                 _dfrexp(glsl_type::dvec4_type,  glsl_type::ivec4_type),
  2396.                 NULL);
  2397.    add_function("uaddCarry",
  2398.                 _uaddCarry(glsl_type::uint_type),
  2399.                 _uaddCarry(glsl_type::uvec2_type),
  2400.                 _uaddCarry(glsl_type::uvec3_type),
  2401.                 _uaddCarry(glsl_type::uvec4_type),
  2402.                 NULL);
  2403.    add_function("usubBorrow",
  2404.                 _usubBorrow(glsl_type::uint_type),
  2405.                 _usubBorrow(glsl_type::uvec2_type),
  2406.                 _usubBorrow(glsl_type::uvec3_type),
  2407.                 _usubBorrow(glsl_type::uvec4_type),
  2408.                 NULL);
  2409.    add_function("imulExtended",
  2410.                 _mulExtended(glsl_type::int_type),
  2411.                 _mulExtended(glsl_type::ivec2_type),
  2412.                 _mulExtended(glsl_type::ivec3_type),
  2413.                 _mulExtended(glsl_type::ivec4_type),
  2414.                 NULL);
  2415.    add_function("umulExtended",
  2416.                 _mulExtended(glsl_type::uint_type),
  2417.                 _mulExtended(glsl_type::uvec2_type),
  2418.                 _mulExtended(glsl_type::uvec3_type),
  2419.                 _mulExtended(glsl_type::uvec4_type),
  2420.                 NULL);
  2421.    add_function("interpolateAtCentroid",
  2422.                 _interpolateAtCentroid(glsl_type::float_type),
  2423.                 _interpolateAtCentroid(glsl_type::vec2_type),
  2424.                 _interpolateAtCentroid(glsl_type::vec3_type),
  2425.                 _interpolateAtCentroid(glsl_type::vec4_type),
  2426.                 NULL);
  2427.    add_function("interpolateAtOffset",
  2428.                 _interpolateAtOffset(glsl_type::float_type),
  2429.                 _interpolateAtOffset(glsl_type::vec2_type),
  2430.                 _interpolateAtOffset(glsl_type::vec3_type),
  2431.                 _interpolateAtOffset(glsl_type::vec4_type),
  2432.                 NULL);
  2433.    add_function("interpolateAtSample",
  2434.                 _interpolateAtSample(glsl_type::float_type),
  2435.                 _interpolateAtSample(glsl_type::vec2_type),
  2436.                 _interpolateAtSample(glsl_type::vec3_type),
  2437.                 _interpolateAtSample(glsl_type::vec4_type),
  2438.                 NULL);
  2439.  
  2440.    add_function("atomicCounter",
  2441.                 _atomic_op("__intrinsic_atomic_read",
  2442.                            shader_atomic_counters),
  2443.                 NULL);
  2444.    add_function("atomicCounterIncrement",
  2445.                 _atomic_op("__intrinsic_atomic_increment",
  2446.                            shader_atomic_counters),
  2447.                 NULL);
  2448.    add_function("atomicCounterDecrement",
  2449.                 _atomic_op("__intrinsic_atomic_predecrement",
  2450.                            shader_atomic_counters),
  2451.                 NULL);
  2452.  
  2453.    add_function("min3",
  2454.                 _min3(glsl_type::float_type),
  2455.                 _min3(glsl_type::vec2_type),
  2456.                 _min3(glsl_type::vec3_type),
  2457.                 _min3(glsl_type::vec4_type),
  2458.  
  2459.                 _min3(glsl_type::int_type),
  2460.                 _min3(glsl_type::ivec2_type),
  2461.                 _min3(glsl_type::ivec3_type),
  2462.                 _min3(glsl_type::ivec4_type),
  2463.  
  2464.                 _min3(glsl_type::uint_type),
  2465.                 _min3(glsl_type::uvec2_type),
  2466.                 _min3(glsl_type::uvec3_type),
  2467.                 _min3(glsl_type::uvec4_type),
  2468.                 NULL);
  2469.  
  2470.    add_function("max3",
  2471.                 _max3(glsl_type::float_type),
  2472.                 _max3(glsl_type::vec2_type),
  2473.                 _max3(glsl_type::vec3_type),
  2474.                 _max3(glsl_type::vec4_type),
  2475.  
  2476.                 _max3(glsl_type::int_type),
  2477.                 _max3(glsl_type::ivec2_type),
  2478.                 _max3(glsl_type::ivec3_type),
  2479.                 _max3(glsl_type::ivec4_type),
  2480.  
  2481.                 _max3(glsl_type::uint_type),
  2482.                 _max3(glsl_type::uvec2_type),
  2483.                 _max3(glsl_type::uvec3_type),
  2484.                 _max3(glsl_type::uvec4_type),
  2485.                 NULL);
  2486.  
  2487.    add_function("mid3",
  2488.                 _mid3(glsl_type::float_type),
  2489.                 _mid3(glsl_type::vec2_type),
  2490.                 _mid3(glsl_type::vec3_type),
  2491.                 _mid3(glsl_type::vec4_type),
  2492.  
  2493.                 _mid3(glsl_type::int_type),
  2494.                 _mid3(glsl_type::ivec2_type),
  2495.                 _mid3(glsl_type::ivec3_type),
  2496.                 _mid3(glsl_type::ivec4_type),
  2497.  
  2498.                 _mid3(glsl_type::uint_type),
  2499.                 _mid3(glsl_type::uvec2_type),
  2500.                 _mid3(glsl_type::uvec3_type),
  2501.                 _mid3(glsl_type::uvec4_type),
  2502.                 NULL);
  2503.  
  2504.    add_image_functions(true);
  2505.  
  2506.    add_function("memoryBarrier",
  2507.                 _memory_barrier(shader_image_load_store),
  2508.                 NULL);
  2509.  
  2510. #undef F
  2511. #undef FI
  2512. #undef FIUD
  2513. #undef FIUBD
  2514. #undef FIU2_MIXED
  2515. }
  2516.  
  2517. void
  2518. builtin_builder::add_function(const char *name, ...)
  2519. {
  2520.    va_list ap;
  2521.  
  2522.    ir_function *f = new(mem_ctx) ir_function(name);
  2523.  
  2524.    va_start(ap, name);
  2525.    while (true) {
  2526.       ir_function_signature *sig = va_arg(ap, ir_function_signature *);
  2527.       if (sig == NULL)
  2528.          break;
  2529.  
  2530.       if (false) {
  2531.          exec_list stuff;
  2532.          stuff.push_tail(sig);
  2533.          validate_ir_tree(&stuff);
  2534.       }
  2535.  
  2536.       f->add_signature(sig);
  2537.    }
  2538.    va_end(ap);
  2539.  
  2540.    shader->symbols->add_function(f);
  2541. }
  2542.  
  2543. void
  2544. builtin_builder::add_image_function(const char *name,
  2545.                                     const char *intrinsic_name,
  2546.                                     unsigned num_arguments,
  2547.                                     unsigned flags)
  2548. {
  2549.    static const glsl_type *const types[] = {
  2550.       glsl_type::image1D_type,
  2551.       glsl_type::image2D_type,
  2552.       glsl_type::image3D_type,
  2553.       glsl_type::image2DRect_type,
  2554.       glsl_type::imageCube_type,
  2555.       glsl_type::imageBuffer_type,
  2556.       glsl_type::image1DArray_type,
  2557.       glsl_type::image2DArray_type,
  2558.       glsl_type::imageCubeArray_type,
  2559.       glsl_type::image2DMS_type,
  2560.       glsl_type::image2DMSArray_type,
  2561.       glsl_type::iimage1D_type,
  2562.       glsl_type::iimage2D_type,
  2563.       glsl_type::iimage3D_type,
  2564.       glsl_type::iimage2DRect_type,
  2565.       glsl_type::iimageCube_type,
  2566.       glsl_type::iimageBuffer_type,
  2567.       glsl_type::iimage1DArray_type,
  2568.       glsl_type::iimage2DArray_type,
  2569.       glsl_type::iimageCubeArray_type,
  2570.       glsl_type::iimage2DMS_type,
  2571.       glsl_type::iimage2DMSArray_type,
  2572.       glsl_type::uimage1D_type,
  2573.       glsl_type::uimage2D_type,
  2574.       glsl_type::uimage3D_type,
  2575.       glsl_type::uimage2DRect_type,
  2576.       glsl_type::uimageCube_type,
  2577.       glsl_type::uimageBuffer_type,
  2578.       glsl_type::uimage1DArray_type,
  2579.       glsl_type::uimage2DArray_type,
  2580.       glsl_type::uimageCubeArray_type,
  2581.       glsl_type::uimage2DMS_type,
  2582.       glsl_type::uimage2DMSArray_type
  2583.    };
  2584.    ir_function *f = new(mem_ctx) ir_function(name);
  2585.  
  2586.    for (unsigned i = 0; i < ARRAY_SIZE(types); ++i) {
  2587.       if (types[i]->sampler_type != GLSL_TYPE_FLOAT ||
  2588.           (flags & IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE))
  2589.          f->add_signature(_image(types[i], intrinsic_name,
  2590.                                  num_arguments, flags));
  2591.    }
  2592.  
  2593.    shader->symbols->add_function(f);
  2594. }
  2595.  
  2596. void
  2597. builtin_builder::add_image_functions(bool glsl)
  2598. {
  2599.    const unsigned flags = (glsl ? IMAGE_FUNCTION_EMIT_STUB : 0);
  2600.  
  2601.    add_image_function(glsl ? "imageLoad" : "__intrinsic_image_load",
  2602.                       "__intrinsic_image_load", 0,
  2603.                       (flags | IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE |
  2604.                        IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE |
  2605.                        IMAGE_FUNCTION_READ_ONLY));
  2606.  
  2607.    add_image_function(glsl ? "imageStore" : "__intrinsic_image_store",
  2608.                       "__intrinsic_image_store", 1,
  2609.                       (flags | IMAGE_FUNCTION_RETURNS_VOID |
  2610.                        IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE |
  2611.                        IMAGE_FUNCTION_SUPPORTS_FLOAT_DATA_TYPE |
  2612.                        IMAGE_FUNCTION_WRITE_ONLY));
  2613.  
  2614.    add_image_function(glsl ? "imageAtomicAdd" : "__intrinsic_image_atomic_add",
  2615.                       "__intrinsic_image_atomic_add", 1, flags);
  2616.  
  2617.    add_image_function(glsl ? "imageAtomicMin" : "__intrinsic_image_atomic_min",
  2618.                       "__intrinsic_image_atomic_min", 1, flags);
  2619.  
  2620.    add_image_function(glsl ? "imageAtomicMax" : "__intrinsic_image_atomic_max",
  2621.                       "__intrinsic_image_atomic_max", 1, flags);
  2622.  
  2623.    add_image_function(glsl ? "imageAtomicAnd" : "__intrinsic_image_atomic_and",
  2624.                       "__intrinsic_image_atomic_and", 1, flags);
  2625.  
  2626.    add_image_function(glsl ? "imageAtomicOr" : "__intrinsic_image_atomic_or",
  2627.                       "__intrinsic_image_atomic_or", 1, flags);
  2628.  
  2629.    add_image_function(glsl ? "imageAtomicXor" : "__intrinsic_image_atomic_xor",
  2630.                       "__intrinsic_image_atomic_xor", 1, flags);
  2631.  
  2632.    add_image_function((glsl ? "imageAtomicExchange" :
  2633.                        "__intrinsic_image_atomic_exchange"),
  2634.                       "__intrinsic_image_atomic_exchange", 1, flags);
  2635.  
  2636.    add_image_function((glsl ? "imageAtomicCompSwap" :
  2637.                        "__intrinsic_image_atomic_comp_swap"),
  2638.                       "__intrinsic_image_atomic_comp_swap", 2, flags);
  2639. }
  2640.  
  2641. ir_variable *
  2642. builtin_builder::in_var(const glsl_type *type, const char *name)
  2643. {
  2644.    return new(mem_ctx) ir_variable(type, name, ir_var_function_in);
  2645. }
  2646.  
  2647. ir_variable *
  2648. builtin_builder::out_var(const glsl_type *type, const char *name)
  2649. {
  2650.    return new(mem_ctx) ir_variable(type, name, ir_var_function_out);
  2651. }
  2652.  
  2653. ir_constant *
  2654. builtin_builder::imm(float f, unsigned vector_elements)
  2655. {
  2656.    return new(mem_ctx) ir_constant(f, vector_elements);
  2657. }
  2658.  
  2659. ir_constant *
  2660. builtin_builder::imm(int i, unsigned vector_elements)
  2661. {
  2662.    return new(mem_ctx) ir_constant(i, vector_elements);
  2663. }
  2664.  
  2665. ir_constant *
  2666. builtin_builder::imm(unsigned u, unsigned vector_elements)
  2667. {
  2668.    return new(mem_ctx) ir_constant(u, vector_elements);
  2669. }
  2670.  
  2671. ir_constant *
  2672. builtin_builder::imm(double d, unsigned vector_elements)
  2673. {
  2674.    return new(mem_ctx) ir_constant(d, vector_elements);
  2675. }
  2676.  
  2677. ir_constant *
  2678. builtin_builder::imm(const glsl_type *type, const ir_constant_data &data)
  2679. {
  2680.    return new(mem_ctx) ir_constant(type, &data);
  2681. }
  2682.  
  2683. #define IMM_FP(type, val) (type->base_type == GLSL_TYPE_DOUBLE) ? imm(val) : imm((float)val)
  2684.  
  2685. ir_dereference_variable *
  2686. builtin_builder::var_ref(ir_variable *var)
  2687. {
  2688.    return new(mem_ctx) ir_dereference_variable(var);
  2689. }
  2690.  
  2691. ir_dereference_array *
  2692. builtin_builder::array_ref(ir_variable *var, int idx)
  2693. {
  2694.    return new(mem_ctx) ir_dereference_array(var, imm(idx));
  2695. }
  2696.  
  2697. /** Return an element of a matrix */
  2698. ir_swizzle *
  2699. builtin_builder::matrix_elt(ir_variable *var, int column, int row)
  2700. {
  2701.    return swizzle(array_ref(var, column), row, 1);
  2702. }
  2703.  
  2704. /**
  2705.  * Implementations of built-in functions:
  2706.  *  @{
  2707.  */
  2708. ir_function_signature *
  2709. builtin_builder::new_sig(const glsl_type *return_type,
  2710.                          builtin_available_predicate avail,
  2711.                          int num_params,
  2712.                          ...)
  2713. {
  2714.    va_list ap;
  2715.  
  2716.    ir_function_signature *sig =
  2717.       new(mem_ctx) ir_function_signature(return_type, avail);
  2718.  
  2719.    exec_list plist;
  2720.    va_start(ap, num_params);
  2721.    for (int i = 0; i < num_params; i++) {
  2722.       plist.push_tail(va_arg(ap, ir_variable *));
  2723.    }
  2724.    va_end(ap);
  2725.  
  2726.    sig->replace_parameters(&plist);
  2727.    return sig;
  2728. }
  2729.  
  2730. #define MAKE_SIG(return_type, avail, ...)  \
  2731.    ir_function_signature *sig =               \
  2732.       new_sig(return_type, avail, __VA_ARGS__);      \
  2733.    ir_factory body(&sig->body, mem_ctx);             \
  2734.    sig->is_defined = true;
  2735.  
  2736. #define MAKE_INTRINSIC(return_type, avail, ...)      \
  2737.    ir_function_signature *sig =                      \
  2738.       new_sig(return_type, avail, __VA_ARGS__);      \
  2739.    sig->is_intrinsic = true;
  2740.  
  2741. ir_function_signature *
  2742. builtin_builder::unop(builtin_available_predicate avail,
  2743.                       ir_expression_operation opcode,
  2744.                       const glsl_type *return_type,
  2745.                       const glsl_type *param_type)
  2746. {
  2747.    ir_variable *x = in_var(param_type, "x");
  2748.    MAKE_SIG(return_type, avail, 1, x);
  2749.    body.emit(ret(expr(opcode, x)));
  2750.    return sig;
  2751. }
  2752.  
  2753. #define UNOP(NAME, OPCODE, AVAIL)               \
  2754. ir_function_signature *                         \
  2755. builtin_builder::_##NAME(const glsl_type *type) \
  2756. {                                               \
  2757.    return unop(&AVAIL, OPCODE, type, type);     \
  2758. }
  2759.  
  2760. #define UNOPA(NAME, OPCODE)               \
  2761. ir_function_signature *                         \
  2762. builtin_builder::_##NAME(builtin_available_predicate avail, const glsl_type *type) \
  2763. {                                               \
  2764.    return unop(avail, OPCODE, type, type);     \
  2765. }
  2766.  
  2767. ir_function_signature *
  2768. builtin_builder::binop(ir_expression_operation opcode,
  2769.                        builtin_available_predicate avail,
  2770.                        const glsl_type *return_type,
  2771.                        const glsl_type *param0_type,
  2772.                        const glsl_type *param1_type)
  2773. {
  2774.    ir_variable *x = in_var(param0_type, "x");
  2775.    ir_variable *y = in_var(param1_type, "y");
  2776.    MAKE_SIG(return_type, avail, 2, x, y);
  2777.    body.emit(ret(expr(opcode, x, y)));
  2778.    return sig;
  2779. }
  2780.  
  2781. #define BINOP(NAME, OPCODE, AVAIL)                                      \
  2782. ir_function_signature *                                                 \
  2783. builtin_builder::_##NAME(const glsl_type *return_type,                  \
  2784.                          const glsl_type *param0_type,                  \
  2785.                          const glsl_type *param1_type)                  \
  2786. {                                                                       \
  2787.    return binop(&AVAIL, OPCODE, return_type, param0_type, param1_type); \
  2788. }
  2789.  
  2790. /**
  2791.  * Angle and Trigonometry Functions @{
  2792.  */
  2793.  
  2794. ir_function_signature *
  2795. builtin_builder::_radians(const glsl_type *type)
  2796. {
  2797.    ir_variable *degrees = in_var(type, "degrees");
  2798.    MAKE_SIG(type, always_available, 1, degrees);
  2799.    body.emit(ret(mul(degrees, imm(0.0174532925f))));
  2800.    return sig;
  2801. }
  2802.  
  2803. ir_function_signature *
  2804. builtin_builder::_degrees(const glsl_type *type)
  2805. {
  2806.    ir_variable *radians = in_var(type, "radians");
  2807.    MAKE_SIG(type, always_available, 1, radians);
  2808.    body.emit(ret(mul(radians, imm(57.29578f))));
  2809.    return sig;
  2810. }
  2811.  
  2812. UNOP(sin, ir_unop_sin, always_available)
  2813. UNOP(cos, ir_unop_cos, always_available)
  2814.  
  2815. ir_function_signature *
  2816. builtin_builder::_tan(const glsl_type *type)
  2817. {
  2818.    ir_variable *theta = in_var(type, "theta");
  2819.    MAKE_SIG(type, always_available, 1, theta);
  2820.    body.emit(ret(div(sin(theta), cos(theta))));
  2821.    return sig;
  2822. }
  2823.  
  2824. ir_expression *
  2825. builtin_builder::asin_expr(ir_variable *x)
  2826. {
  2827.    return mul(sign(x),
  2828.               sub(imm(M_PI_2f),
  2829.                   mul(sqrt(sub(imm(1.0f), abs(x))),
  2830.                       add(imm(M_PI_2f),
  2831.                           mul(abs(x),
  2832.                               add(imm(M_PI_4f - 1.0f),
  2833.                                   mul(abs(x),
  2834.                                       add(imm(0.086566724f),
  2835.                                           mul(abs(x), imm(-0.03102955f))))))))));
  2836. }
  2837.  
  2838. ir_call *
  2839. builtin_builder::call(ir_function *f, ir_variable *ret, exec_list params)
  2840. {
  2841.    exec_list actual_params;
  2842.  
  2843.    foreach_in_list(ir_variable, var, &params) {
  2844.       actual_params.push_tail(var_ref(var));
  2845.    }
  2846.  
  2847.    ir_function_signature *sig =
  2848.       f->exact_matching_signature(NULL, &actual_params);
  2849.    if (!sig)
  2850.       return NULL;
  2851.  
  2852.    ir_dereference_variable *deref =
  2853.       (sig->return_type->is_void() ? NULL : var_ref(ret));
  2854.  
  2855.    return new(mem_ctx) ir_call(sig, deref, &actual_params);
  2856. }
  2857.  
  2858. ir_function_signature *
  2859. builtin_builder::_asin(const glsl_type *type)
  2860. {
  2861.    ir_variable *x = in_var(type, "x");
  2862.    MAKE_SIG(type, always_available, 1, x);
  2863.  
  2864.    body.emit(ret(asin_expr(x)));
  2865.  
  2866.    return sig;
  2867. }
  2868.  
  2869. ir_function_signature *
  2870. builtin_builder::_acos(const glsl_type *type)
  2871. {
  2872.    ir_variable *x = in_var(type, "x");
  2873.    MAKE_SIG(type, always_available, 1, x);
  2874.  
  2875.    body.emit(ret(sub(imm(M_PI_2f), asin_expr(x))));
  2876.  
  2877.    return sig;
  2878. }
  2879.  
  2880. ir_function_signature *
  2881. builtin_builder::_atan2(const glsl_type *type)
  2882. {
  2883.    ir_variable *vec_y = in_var(type, "vec_y");
  2884.    ir_variable *vec_x = in_var(type, "vec_x");
  2885.    MAKE_SIG(type, always_available, 2, vec_y, vec_x);
  2886.  
  2887.    ir_variable *vec_result = body.make_temp(type, "vec_result");
  2888.    ir_variable *r = body.make_temp(glsl_type::float_type, "r");
  2889.    for (int i = 0; i < type->vector_elements; i++) {
  2890.       ir_variable *y = body.make_temp(glsl_type::float_type, "y");
  2891.       ir_variable *x = body.make_temp(glsl_type::float_type, "x");
  2892.       body.emit(assign(y, swizzle(vec_y, i, 1)));
  2893.       body.emit(assign(x, swizzle(vec_x, i, 1)));
  2894.  
  2895.       /* If |x| >= 1.0e-8 * |y|: */
  2896.       ir_if *outer_if =
  2897.          new(mem_ctx) ir_if(greater(abs(x), mul(imm(1.0e-8f), abs(y))));
  2898.  
  2899.       ir_factory outer_then(&outer_if->then_instructions, mem_ctx);
  2900.  
  2901.       /* Then...call atan(y/x) */
  2902.       do_atan(body, glsl_type::float_type, r, div(y, x));
  2903.  
  2904.       /*     ...and fix it up: */
  2905.       ir_if *inner_if = new(mem_ctx) ir_if(less(x, imm(0.0f)));
  2906.       inner_if->then_instructions.push_tail(
  2907.          if_tree(gequal(y, imm(0.0f)),
  2908.                  assign(r, add(r, imm(M_PIf))),
  2909.                  assign(r, sub(r, imm(M_PIf)))));
  2910.       outer_then.emit(inner_if);
  2911.  
  2912.       /* Else... */
  2913.       outer_if->else_instructions.push_tail(
  2914.          assign(r, mul(sign(y), imm(M_PI_2f))));
  2915.  
  2916.       body.emit(outer_if);
  2917.  
  2918.       body.emit(assign(vec_result, r, 1 << i));
  2919.    }
  2920.    body.emit(ret(vec_result));
  2921.  
  2922.    return sig;
  2923. }
  2924.  
  2925. void
  2926. builtin_builder::do_atan(ir_factory &body, const glsl_type *type, ir_variable *res, operand y_over_x)
  2927. {
  2928.    /*
  2929.     * range-reduction, first step:
  2930.     *
  2931.     *      / y_over_x         if |y_over_x| <= 1.0;
  2932.     * x = <
  2933.     *      \ 1.0 / y_over_x   otherwise
  2934.     */
  2935.    ir_variable *x = body.make_temp(type, "atan_x");
  2936.    body.emit(assign(x, div(min2(abs(y_over_x),
  2937.                                 imm(1.0f)),
  2938.                            max2(abs(y_over_x),
  2939.                                 imm(1.0f)))));
  2940.  
  2941.    /*
  2942.     * approximate atan by evaluating polynomial:
  2943.     *
  2944.     * x   * 0.9999793128310355 - x^3  * 0.3326756418091246 +
  2945.     * x^5 * 0.1938924977115610 - x^7  * 0.1173503194786851 +
  2946.     * x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
  2947.     */
  2948.    ir_variable *tmp = body.make_temp(type, "atan_tmp");
  2949.    body.emit(assign(tmp, mul(x, x)));
  2950.    body.emit(assign(tmp, mul(add(mul(sub(mul(add(mul(sub(mul(add(mul(imm(-0.0121323213173444f),
  2951.                                                                      tmp),
  2952.                                                                  imm(0.0536813784310406f)),
  2953.                                                              tmp),
  2954.                                                          imm(0.1173503194786851f)),
  2955.                                                      tmp),
  2956.                                                  imm(0.1938924977115610f)),
  2957.                                              tmp),
  2958.                                          imm(0.3326756418091246f)),
  2959.                                      tmp),
  2960.                                  imm(0.9999793128310355f)),
  2961.                              x)));
  2962.  
  2963.    /* range-reduction fixup */
  2964.    body.emit(assign(tmp, add(tmp,
  2965.                              mul(b2f(greater(abs(y_over_x),
  2966.                                           imm(1.0f, type->components()))),
  2967.                                   add(mul(tmp,
  2968.                                           imm(-2.0f)),
  2969.                                       imm(M_PI_2f))))));
  2970.  
  2971.    /* sign fixup */
  2972.    body.emit(assign(res, mul(tmp, sign(y_over_x))));
  2973. }
  2974.  
  2975. ir_function_signature *
  2976. builtin_builder::_atan(const glsl_type *type)
  2977. {
  2978.    ir_variable *y_over_x = in_var(type, "y_over_x");
  2979.    MAKE_SIG(type, always_available, 1, y_over_x);
  2980.  
  2981.    ir_variable *tmp = body.make_temp(type, "tmp");
  2982.    do_atan(body, type, tmp, y_over_x);
  2983.    body.emit(ret(tmp));
  2984.  
  2985.    return sig;
  2986. }
  2987.  
  2988. ir_function_signature *
  2989. builtin_builder::_sinh(const glsl_type *type)
  2990. {
  2991.    ir_variable *x = in_var(type, "x");
  2992.    MAKE_SIG(type, v130, 1, x);
  2993.  
  2994.    /* 0.5 * (e^x - e^(-x)) */
  2995.    body.emit(ret(mul(imm(0.5f), sub(exp(x), exp(neg(x))))));
  2996.  
  2997.    return sig;
  2998. }
  2999.  
  3000. ir_function_signature *
  3001. builtin_builder::_cosh(const glsl_type *type)
  3002. {
  3003.    ir_variable *x = in_var(type, "x");
  3004.    MAKE_SIG(type, v130, 1, x);
  3005.  
  3006.    /* 0.5 * (e^x + e^(-x)) */
  3007.    body.emit(ret(mul(imm(0.5f), add(exp(x), exp(neg(x))))));
  3008.  
  3009.    return sig;
  3010. }
  3011.  
  3012. ir_function_signature *
  3013. builtin_builder::_tanh(const glsl_type *type)
  3014. {
  3015.    ir_variable *x = in_var(type, "x");
  3016.    MAKE_SIG(type, v130, 1, x);
  3017.  
  3018.    /* (e^x - e^(-x)) / (e^x + e^(-x)) */
  3019.    body.emit(ret(div(sub(exp(x), exp(neg(x))),
  3020.                      add(exp(x), exp(neg(x))))));
  3021.  
  3022.    return sig;
  3023. }
  3024.  
  3025. ir_function_signature *
  3026. builtin_builder::_asinh(const glsl_type *type)
  3027. {
  3028.    ir_variable *x = in_var(type, "x");
  3029.    MAKE_SIG(type, v130, 1, x);
  3030.  
  3031.    body.emit(ret(mul(sign(x), log(add(abs(x), sqrt(add(mul(x, x),
  3032.                                                        imm(1.0f))))))));
  3033.    return sig;
  3034. }
  3035.  
  3036. ir_function_signature *
  3037. builtin_builder::_acosh(const glsl_type *type)
  3038. {
  3039.    ir_variable *x = in_var(type, "x");
  3040.    MAKE_SIG(type, v130, 1, x);
  3041.  
  3042.    body.emit(ret(log(add(x, sqrt(sub(mul(x, x), imm(1.0f)))))));
  3043.    return sig;
  3044. }
  3045.  
  3046. ir_function_signature *
  3047. builtin_builder::_atanh(const glsl_type *type)
  3048. {
  3049.    ir_variable *x = in_var(type, "x");
  3050.    MAKE_SIG(type, v130, 1, x);
  3051.  
  3052.    body.emit(ret(mul(imm(0.5f), log(div(add(imm(1.0f), x),
  3053.                                         sub(imm(1.0f), x))))));
  3054.    return sig;
  3055. }
  3056. /** @} */
  3057.  
  3058. /**
  3059.  * Exponential Functions @{
  3060.  */
  3061.  
  3062. ir_function_signature *
  3063. builtin_builder::_pow(const glsl_type *type)
  3064. {
  3065.    return binop(ir_binop_pow, always_available, type, type, type);
  3066. }
  3067.  
  3068. UNOP(exp,         ir_unop_exp,  always_available)
  3069. UNOP(log,         ir_unop_log,  always_available)
  3070. UNOP(exp2,        ir_unop_exp2, always_available)
  3071. UNOP(log2,        ir_unop_log2, always_available)
  3072. UNOPA(sqrt,        ir_unop_sqrt)
  3073. UNOPA(inversesqrt, ir_unop_rsq)
  3074.  
  3075. /** @} */
  3076.  
  3077. UNOPA(abs,       ir_unop_abs)
  3078. UNOPA(sign,      ir_unop_sign)
  3079. UNOPA(floor,     ir_unop_floor)
  3080. UNOPA(trunc,     ir_unop_trunc)
  3081. UNOPA(round,     ir_unop_round_even)
  3082. UNOPA(roundEven, ir_unop_round_even)
  3083. UNOPA(ceil,      ir_unop_ceil)
  3084. UNOPA(fract,     ir_unop_fract)
  3085.  
  3086. ir_function_signature *
  3087. builtin_builder::_mod(const glsl_type *x_type, const glsl_type *y_type)
  3088. {
  3089.    return binop(ir_binop_mod, always_available, x_type, x_type, y_type);
  3090. }
  3091.  
  3092. ir_function_signature *
  3093. builtin_builder::_modf(builtin_available_predicate avail, const glsl_type *type)
  3094. {
  3095.    ir_variable *x = in_var(type, "x");
  3096.    ir_variable *i = out_var(type, "i");
  3097.    MAKE_SIG(type, avail, 2, x, i);
  3098.  
  3099.    ir_variable *t = body.make_temp(type, "t");
  3100.    body.emit(assign(t, expr(ir_unop_trunc, x)));
  3101.    body.emit(assign(i, t));
  3102.    body.emit(ret(sub(x, t)));
  3103.  
  3104.    return sig;
  3105. }
  3106.  
  3107. ir_function_signature *
  3108. builtin_builder::_min(builtin_available_predicate avail,
  3109.                       const glsl_type *x_type, const glsl_type *y_type)
  3110. {
  3111.    return binop(ir_binop_min, avail, x_type, x_type, y_type);
  3112. }
  3113.  
  3114. ir_function_signature *
  3115. builtin_builder::_max(builtin_available_predicate avail,
  3116.                       const glsl_type *x_type, const glsl_type *y_type)
  3117. {
  3118.    return binop(ir_binop_max, avail, x_type, x_type, y_type);
  3119. }
  3120.  
  3121. ir_function_signature *
  3122. builtin_builder::_clamp(builtin_available_predicate avail,
  3123.                         const glsl_type *val_type, const glsl_type *bound_type)
  3124. {
  3125.    ir_variable *x = in_var(val_type, "x");
  3126.    ir_variable *minVal = in_var(bound_type, "minVal");
  3127.    ir_variable *maxVal = in_var(bound_type, "maxVal");
  3128.    MAKE_SIG(val_type, avail, 3, x, minVal, maxVal);
  3129.  
  3130.    body.emit(ret(clamp(x, minVal, maxVal)));
  3131.  
  3132.    return sig;
  3133. }
  3134.  
  3135. ir_function_signature *
  3136. builtin_builder::_mix_lrp(builtin_available_predicate avail, const glsl_type *val_type, const glsl_type *blend_type)
  3137. {
  3138.    ir_variable *x = in_var(val_type, "x");
  3139.    ir_variable *y = in_var(val_type, "y");
  3140.    ir_variable *a = in_var(blend_type, "a");
  3141.    MAKE_SIG(val_type, avail, 3, x, y, a);
  3142.  
  3143.    body.emit(ret(lrp(x, y, a)));
  3144.  
  3145.    return sig;
  3146. }
  3147.  
  3148. ir_function_signature *
  3149. builtin_builder::_mix_sel(builtin_available_predicate avail,
  3150.                           const glsl_type *val_type,
  3151.                           const glsl_type *blend_type)
  3152. {
  3153.    ir_variable *x = in_var(val_type, "x");
  3154.    ir_variable *y = in_var(val_type, "y");
  3155.    ir_variable *a = in_var(blend_type, "a");
  3156.    MAKE_SIG(val_type, avail, 3, x, y, a);
  3157.  
  3158.    /* csel matches the ternary operator in that a selector of true choses the
  3159.     * first argument. This differs from mix(x, y, false) which choses the
  3160.     * second argument (to remain consistent with the interpolating version of
  3161.     * mix() which takes a blend factor from 0.0 to 1.0 where 0.0 is only x.
  3162.     *
  3163.     * To handle the behavior mismatch, reverse the x and y arguments.
  3164.     */
  3165.    body.emit(ret(csel(a, y, x)));
  3166.  
  3167.    return sig;
  3168. }
  3169.  
  3170. ir_function_signature *
  3171. builtin_builder::_step(builtin_available_predicate avail, const glsl_type *edge_type, const glsl_type *x_type)
  3172. {
  3173.    ir_variable *edge = in_var(edge_type, "edge");
  3174.    ir_variable *x = in_var(x_type, "x");
  3175.    MAKE_SIG(x_type, avail, 2, edge, x);
  3176.  
  3177.    ir_variable *t = body.make_temp(x_type, "t");
  3178.    if (x_type->vector_elements == 1) {
  3179.       /* Both are floats */
  3180.       if (edge_type->base_type == GLSL_TYPE_DOUBLE)
  3181.          body.emit(assign(t, f2d(b2f(gequal(x, edge)))));
  3182.       else
  3183.          body.emit(assign(t, b2f(gequal(x, edge))));
  3184.    } else if (edge_type->vector_elements == 1) {
  3185.       /* x is a vector but edge is a float */
  3186.       for (int i = 0; i < x_type->vector_elements; i++) {
  3187.          if (edge_type->base_type == GLSL_TYPE_DOUBLE)
  3188.             body.emit(assign(t, f2d(b2f(gequal(swizzle(x, i, 1), edge))), 1 << i));
  3189.          else
  3190.             body.emit(assign(t, b2f(gequal(swizzle(x, i, 1), edge)), 1 << i));
  3191.       }
  3192.    } else {
  3193.       /* Both are vectors */
  3194.       for (int i = 0; i < x_type->vector_elements; i++) {
  3195.          if (edge_type->base_type == GLSL_TYPE_DOUBLE)
  3196.             body.emit(assign(t, f2d(b2f(gequal(swizzle(x, i, 1), swizzle(edge, i, 1)))),
  3197.                              1 << i));
  3198.          else
  3199.             body.emit(assign(t, b2f(gequal(swizzle(x, i, 1), swizzle(edge, i, 1))),
  3200.                              1 << i));
  3201.  
  3202.       }
  3203.    }
  3204.    body.emit(ret(t));
  3205.  
  3206.    return sig;
  3207. }
  3208.  
  3209. ir_function_signature *
  3210. builtin_builder::_smoothstep(builtin_available_predicate avail, const glsl_type *edge_type, const glsl_type *x_type)
  3211. {
  3212.    ir_variable *edge0 = in_var(edge_type, "edge0");
  3213.    ir_variable *edge1 = in_var(edge_type, "edge1");
  3214.    ir_variable *x = in_var(x_type, "x");
  3215.    MAKE_SIG(x_type, avail, 3, edge0, edge1, x);
  3216.  
  3217.    /* From the GLSL 1.10 specification:
  3218.     *
  3219.     *    genType t;
  3220.     *    t = clamp((x - edge0) / (edge1 - edge0), 0, 1);
  3221.     *    return t * t * (3 - 2 * t);
  3222.     */
  3223.  
  3224.    ir_variable *t = body.make_temp(x_type, "t");
  3225.    body.emit(assign(t, clamp(div(sub(x, edge0), sub(edge1, edge0)),
  3226.                              IMM_FP(x_type, 0.0), IMM_FP(x_type, 1.0))));
  3227.  
  3228.    body.emit(ret(mul(t, mul(t, sub(IMM_FP(x_type, 3.0), mul(IMM_FP(x_type, 2.0), t))))));
  3229.  
  3230.    return sig;
  3231. }
  3232.  
  3233. ir_function_signature *
  3234. builtin_builder::_isnan(builtin_available_predicate avail, const glsl_type *type)
  3235. {
  3236.    ir_variable *x = in_var(type, "x");
  3237.    MAKE_SIG(glsl_type::bvec(type->vector_elements), avail, 1, x);
  3238.  
  3239.    body.emit(ret(nequal(x, x)));
  3240.  
  3241.    return sig;
  3242. }
  3243.  
  3244. ir_function_signature *
  3245. builtin_builder::_isinf(builtin_available_predicate avail, const glsl_type *type)
  3246. {
  3247.    ir_variable *x = in_var(type, "x");
  3248.    MAKE_SIG(glsl_type::bvec(type->vector_elements), avail, 1, x);
  3249.  
  3250.    ir_constant_data infinities;
  3251.    for (int i = 0; i < type->vector_elements; i++) {
  3252.       infinities.f[i] = INFINITY;
  3253.    }
  3254.  
  3255.    body.emit(ret(equal(abs(x), imm(type, infinities))));
  3256.  
  3257.    return sig;
  3258. }
  3259.  
  3260. ir_function_signature *
  3261. builtin_builder::_floatBitsToInt(const glsl_type *type)
  3262. {
  3263.    ir_variable *x = in_var(type, "x");
  3264.    MAKE_SIG(glsl_type::ivec(type->vector_elements), shader_bit_encoding, 1, x);
  3265.    body.emit(ret(bitcast_f2i(x)));
  3266.    return sig;
  3267. }
  3268.  
  3269. ir_function_signature *
  3270. builtin_builder::_floatBitsToUint(const glsl_type *type)
  3271. {
  3272.    ir_variable *x = in_var(type, "x");
  3273.    MAKE_SIG(glsl_type::uvec(type->vector_elements), shader_bit_encoding, 1, x);
  3274.    body.emit(ret(bitcast_f2u(x)));
  3275.    return sig;
  3276. }
  3277.  
  3278. ir_function_signature *
  3279. builtin_builder::_intBitsToFloat(const glsl_type *type)
  3280. {
  3281.    ir_variable *x = in_var(type, "x");
  3282.    MAKE_SIG(glsl_type::vec(type->vector_elements), shader_bit_encoding, 1, x);
  3283.    body.emit(ret(bitcast_i2f(x)));
  3284.    return sig;
  3285. }
  3286.  
  3287. ir_function_signature *
  3288. builtin_builder::_uintBitsToFloat(const glsl_type *type)
  3289. {
  3290.    ir_variable *x = in_var(type, "x");
  3291.    MAKE_SIG(glsl_type::vec(type->vector_elements), shader_bit_encoding, 1, x);
  3292.    body.emit(ret(bitcast_u2f(x)));
  3293.    return sig;
  3294. }
  3295.  
  3296. ir_function_signature *
  3297. builtin_builder::_packUnorm2x16(builtin_available_predicate avail)
  3298. {
  3299.    ir_variable *v = in_var(glsl_type::vec2_type, "v");
  3300.    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
  3301.    body.emit(ret(expr(ir_unop_pack_unorm_2x16, v)));
  3302.    return sig;
  3303. }
  3304.  
  3305. ir_function_signature *
  3306. builtin_builder::_packSnorm2x16(builtin_available_predicate avail)
  3307. {
  3308.    ir_variable *v = in_var(glsl_type::vec2_type, "v");
  3309.    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
  3310.    body.emit(ret(expr(ir_unop_pack_snorm_2x16, v)));
  3311.    return sig;
  3312. }
  3313.  
  3314. ir_function_signature *
  3315. builtin_builder::_packUnorm4x8(builtin_available_predicate avail)
  3316. {
  3317.    ir_variable *v = in_var(glsl_type::vec4_type, "v");
  3318.    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
  3319.    body.emit(ret(expr(ir_unop_pack_unorm_4x8, v)));
  3320.    return sig;
  3321. }
  3322.  
  3323. ir_function_signature *
  3324. builtin_builder::_packSnorm4x8(builtin_available_predicate avail)
  3325. {
  3326.    ir_variable *v = in_var(glsl_type::vec4_type, "v");
  3327.    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
  3328.    body.emit(ret(expr(ir_unop_pack_snorm_4x8, v)));
  3329.    return sig;
  3330. }
  3331.  
  3332. ir_function_signature *
  3333. builtin_builder::_unpackUnorm2x16(builtin_available_predicate avail)
  3334. {
  3335.    ir_variable *p = in_var(glsl_type::uint_type, "p");
  3336.    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
  3337.    body.emit(ret(expr(ir_unop_unpack_unorm_2x16, p)));
  3338.    return sig;
  3339. }
  3340.  
  3341. ir_function_signature *
  3342. builtin_builder::_unpackSnorm2x16(builtin_available_predicate avail)
  3343. {
  3344.    ir_variable *p = in_var(glsl_type::uint_type, "p");
  3345.    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
  3346.    body.emit(ret(expr(ir_unop_unpack_snorm_2x16, p)));
  3347.    return sig;
  3348. }
  3349.  
  3350.  
  3351. ir_function_signature *
  3352. builtin_builder::_unpackUnorm4x8(builtin_available_predicate avail)
  3353. {
  3354.    ir_variable *p = in_var(glsl_type::uint_type, "p");
  3355.    MAKE_SIG(glsl_type::vec4_type, avail, 1, p);
  3356.    body.emit(ret(expr(ir_unop_unpack_unorm_4x8, p)));
  3357.    return sig;
  3358. }
  3359.  
  3360. ir_function_signature *
  3361. builtin_builder::_unpackSnorm4x8(builtin_available_predicate avail)
  3362. {
  3363.    ir_variable *p = in_var(glsl_type::uint_type, "p");
  3364.    MAKE_SIG(glsl_type::vec4_type, avail, 1, p);
  3365.    body.emit(ret(expr(ir_unop_unpack_snorm_4x8, p)));
  3366.    return sig;
  3367. }
  3368.  
  3369. ir_function_signature *
  3370. builtin_builder::_packHalf2x16(builtin_available_predicate avail)
  3371. {
  3372.    ir_variable *v = in_var(glsl_type::vec2_type, "v");
  3373.    MAKE_SIG(glsl_type::uint_type, avail, 1, v);
  3374.    body.emit(ret(expr(ir_unop_pack_half_2x16, v)));
  3375.    return sig;
  3376. }
  3377.  
  3378. ir_function_signature *
  3379. builtin_builder::_unpackHalf2x16(builtin_available_predicate avail)
  3380. {
  3381.    ir_variable *p = in_var(glsl_type::uint_type, "p");
  3382.    MAKE_SIG(glsl_type::vec2_type, avail, 1, p);
  3383.    body.emit(ret(expr(ir_unop_unpack_half_2x16, p)));
  3384.    return sig;
  3385. }
  3386.  
  3387. ir_function_signature *
  3388. builtin_builder::_packDouble2x32(builtin_available_predicate avail)
  3389. {
  3390.    ir_variable *v = in_var(glsl_type::uvec2_type, "v");
  3391.    MAKE_SIG(glsl_type::double_type, avail, 1, v);
  3392.    body.emit(ret(expr(ir_unop_pack_double_2x32, v)));
  3393.    return sig;
  3394. }
  3395.  
  3396. ir_function_signature *
  3397. builtin_builder::_unpackDouble2x32(builtin_available_predicate avail)
  3398. {
  3399.    ir_variable *p = in_var(glsl_type::double_type, "p");
  3400.    MAKE_SIG(glsl_type::uvec2_type, avail, 1, p);
  3401.    body.emit(ret(expr(ir_unop_unpack_double_2x32, p)));
  3402.    return sig;
  3403. }
  3404.  
  3405. ir_function_signature *
  3406. builtin_builder::_length(builtin_available_predicate avail, const glsl_type *type)
  3407. {
  3408.    ir_variable *x = in_var(type, "x");
  3409.    MAKE_SIG(type->get_base_type(), avail, 1, x);
  3410.  
  3411.    body.emit(ret(sqrt(dot(x, x))));
  3412.  
  3413.    return sig;
  3414. }
  3415.  
  3416. ir_function_signature *
  3417. builtin_builder::_distance(builtin_available_predicate avail, const glsl_type *type)
  3418. {
  3419.    ir_variable *p0 = in_var(type, "p0");
  3420.    ir_variable *p1 = in_var(type, "p1");
  3421.    MAKE_SIG(type->get_base_type(), avail, 2, p0, p1);
  3422.  
  3423.    if (type->vector_elements == 1) {
  3424.       body.emit(ret(abs(sub(p0, p1))));
  3425.    } else {
  3426.       ir_variable *p = body.make_temp(type, "p");
  3427.       body.emit(assign(p, sub(p0, p1)));
  3428.       body.emit(ret(sqrt(dot(p, p))));
  3429.    }
  3430.  
  3431.    return sig;
  3432. }
  3433.  
  3434. ir_function_signature *
  3435. builtin_builder::_dot(builtin_available_predicate avail, const glsl_type *type)
  3436. {
  3437.    if (type->vector_elements == 1)
  3438.       return binop(ir_binop_mul, avail, type, type, type);
  3439.  
  3440.    return binop(ir_binop_dot, avail,
  3441.                 type->get_base_type(), type, type);
  3442. }
  3443.  
  3444. ir_function_signature *
  3445. builtin_builder::_cross(builtin_available_predicate avail, const glsl_type *type)
  3446. {
  3447.    ir_variable *a = in_var(type, "a");
  3448.    ir_variable *b = in_var(type, "b");
  3449.    MAKE_SIG(type, avail, 2, a, b);
  3450.  
  3451.    int yzx = MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, 0);
  3452.    int zxy = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, 0);
  3453.  
  3454.    body.emit(ret(sub(mul(swizzle(a, yzx, 3), swizzle(b, zxy, 3)),
  3455.                      mul(swizzle(a, zxy, 3), swizzle(b, yzx, 3)))));
  3456.  
  3457.    return sig;
  3458. }
  3459.  
  3460. ir_function_signature *
  3461. builtin_builder::_normalize(builtin_available_predicate avail, const glsl_type *type)
  3462. {
  3463.    ir_variable *x = in_var(type, "x");
  3464.    MAKE_SIG(type, avail, 1, x);
  3465.  
  3466.    if (type->vector_elements == 1) {
  3467.       body.emit(ret(sign(x)));
  3468.    } else {
  3469.       body.emit(ret(mul(x, rsq(dot(x, x)))));
  3470.    }
  3471.  
  3472.    return sig;
  3473. }
  3474.  
  3475. ir_function_signature *
  3476. builtin_builder::_ftransform()
  3477. {
  3478.    MAKE_SIG(glsl_type::vec4_type, compatibility_vs_only, 0);
  3479.  
  3480.    body.emit(ret(new(mem_ctx) ir_expression(ir_binop_mul,
  3481.       glsl_type::vec4_type,
  3482.       var_ref(gl_ModelViewProjectionMatrix),
  3483.       var_ref(gl_Vertex))));
  3484.  
  3485.    /* FINISHME: Once the ir_expression() constructor handles type inference
  3486.     *           for matrix operations, we can simplify this to:
  3487.     *
  3488.     *    body.emit(ret(mul(gl_ModelViewProjectionMatrix, gl_Vertex)));
  3489.     */
  3490.    return sig;
  3491. }
  3492.  
  3493. ir_function_signature *
  3494. builtin_builder::_faceforward(builtin_available_predicate avail, const glsl_type *type)
  3495. {
  3496.    ir_variable *N = in_var(type, "N");
  3497.    ir_variable *I = in_var(type, "I");
  3498.    ir_variable *Nref = in_var(type, "Nref");
  3499.    MAKE_SIG(type, avail, 3, N, I, Nref);
  3500.  
  3501.    body.emit(if_tree(less(dot(Nref, I), IMM_FP(type, 0.0)),
  3502.                      ret(N), ret(neg(N))));
  3503.  
  3504.    return sig;
  3505. }
  3506.  
  3507. ir_function_signature *
  3508. builtin_builder::_reflect(builtin_available_predicate avail, const glsl_type *type)
  3509. {
  3510.    ir_variable *I = in_var(type, "I");
  3511.    ir_variable *N = in_var(type, "N");
  3512.    MAKE_SIG(type, avail, 2, I, N);
  3513.  
  3514.    /* I - 2 * dot(N, I) * N */
  3515.    body.emit(ret(sub(I, mul(IMM_FP(type, 2.0), mul(dot(N, I), N)))));
  3516.  
  3517.    return sig;
  3518. }
  3519.  
  3520. ir_function_signature *
  3521. builtin_builder::_refract(builtin_available_predicate avail, const glsl_type *type)
  3522. {
  3523.    ir_variable *I = in_var(type, "I");
  3524.    ir_variable *N = in_var(type, "N");
  3525.    ir_variable *eta = in_var(type->get_base_type(), "eta");
  3526.    MAKE_SIG(type, avail, 3, I, N, eta);
  3527.  
  3528.    ir_variable *n_dot_i = body.make_temp(type->get_base_type(), "n_dot_i");
  3529.    body.emit(assign(n_dot_i, dot(N, I)));
  3530.  
  3531.    /* From the GLSL 1.10 specification:
  3532.     * k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
  3533.     * if (k < 0.0)
  3534.     *    return genType(0.0)
  3535.     * else
  3536.     *    return eta * I - (eta * dot(N, I) + sqrt(k)) * N
  3537.     */
  3538.    ir_variable *k = body.make_temp(type->get_base_type(), "k");
  3539.    body.emit(assign(k, sub(IMM_FP(type, 1.0),
  3540.                            mul(eta, mul(eta, sub(IMM_FP(type, 1.0),
  3541.                                                  mul(n_dot_i, n_dot_i)))))));
  3542.    body.emit(if_tree(less(k, IMM_FP(type, 0.0)),
  3543.                      ret(ir_constant::zero(mem_ctx, type)),
  3544.                      ret(sub(mul(eta, I),
  3545.                              mul(add(mul(eta, n_dot_i), sqrt(k)), N)))));
  3546.  
  3547.    return sig;
  3548. }
  3549.  
  3550. ir_function_signature *
  3551. builtin_builder::_matrixCompMult(builtin_available_predicate avail, const glsl_type *type)
  3552. {
  3553.    ir_variable *x = in_var(type, "x");
  3554.    ir_variable *y = in_var(type, "y");
  3555.    MAKE_SIG(type, avail, 2, x, y);
  3556.  
  3557.    ir_variable *z = body.make_temp(type, "z");
  3558.    for (int i = 0; i < type->matrix_columns; i++) {
  3559.       body.emit(assign(array_ref(z, i), mul(array_ref(x, i), array_ref(y, i))));
  3560.    }
  3561.    body.emit(ret(z));
  3562.  
  3563.    return sig;
  3564. }
  3565.  
  3566. ir_function_signature *
  3567. builtin_builder::_outerProduct(builtin_available_predicate avail, const glsl_type *type)
  3568. {
  3569.    ir_variable *c;
  3570.    ir_variable *r;
  3571.  
  3572.    if (type->base_type == GLSL_TYPE_DOUBLE) {
  3573.       r = in_var(glsl_type::dvec(type->matrix_columns), "r");
  3574.       c = in_var(glsl_type::dvec(type->vector_elements), "c");
  3575.    } else {
  3576.       r = in_var(glsl_type::vec(type->matrix_columns), "r");
  3577.       c = in_var(glsl_type::vec(type->vector_elements), "c");
  3578.    }
  3579.    MAKE_SIG(type, avail, 2, c, r);
  3580.  
  3581.    ir_variable *m = body.make_temp(type, "m");
  3582.    for (int i = 0; i < type->matrix_columns; i++) {
  3583.       body.emit(assign(array_ref(m, i), mul(c, swizzle(r, i, 1))));
  3584.    }
  3585.    body.emit(ret(m));
  3586.  
  3587.    return sig;
  3588. }
  3589.  
  3590. ir_function_signature *
  3591. builtin_builder::_transpose(builtin_available_predicate avail, const glsl_type *orig_type)
  3592. {
  3593.    const glsl_type *transpose_type =
  3594.       glsl_type::get_instance(orig_type->base_type,
  3595.                               orig_type->matrix_columns,
  3596.                               orig_type->vector_elements);
  3597.  
  3598.    ir_variable *m = in_var(orig_type, "m");
  3599.    MAKE_SIG(transpose_type, avail, 1, m);
  3600.  
  3601.    ir_variable *t = body.make_temp(transpose_type, "t");
  3602.    for (int i = 0; i < orig_type->matrix_columns; i++) {
  3603.       for (int j = 0; j < orig_type->vector_elements; j++) {
  3604.          body.emit(assign(array_ref(t, j),
  3605.                           matrix_elt(m, i, j),
  3606.                           1 << i));
  3607.       }
  3608.    }
  3609.    body.emit(ret(t));
  3610.  
  3611.    return sig;
  3612. }
  3613.  
  3614. ir_function_signature *
  3615. builtin_builder::_determinant_mat2(builtin_available_predicate avail, const glsl_type *type)
  3616. {
  3617.    ir_variable *m = in_var(type, "m");
  3618.    MAKE_SIG(type->get_base_type(), avail, 1, m);
  3619.  
  3620.    body.emit(ret(sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
  3621.                      mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1)))));
  3622.  
  3623.    return sig;
  3624. }
  3625.  
  3626. ir_function_signature *
  3627. builtin_builder::_determinant_mat3(builtin_available_predicate avail, const glsl_type *type)
  3628. {
  3629.    ir_variable *m = in_var(type, "m");
  3630.    MAKE_SIG(type->get_base_type(), avail, 1, m);
  3631.  
  3632.    ir_expression *f1 =
  3633.       sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)),
  3634.           mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 1)));
  3635.  
  3636.    ir_expression *f2 =
  3637.       sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)),
  3638.           mul(matrix_elt(m, 1, 2), matrix_elt(m, 2, 0)));
  3639.  
  3640.    ir_expression *f3 =
  3641.       sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)),
  3642.           mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 0)));
  3643.  
  3644.    body.emit(ret(add(sub(mul(matrix_elt(m, 0, 0), f1),
  3645.                          mul(matrix_elt(m, 0, 1), f2)),
  3646.                      mul(matrix_elt(m, 0, 2), f3))));
  3647.  
  3648.    return sig;
  3649. }
  3650.  
  3651. ir_function_signature *
  3652. builtin_builder::_determinant_mat4(builtin_available_predicate avail, const glsl_type *type)
  3653. {
  3654.    ir_variable *m = in_var(type, "m");
  3655.    const glsl_type *btype = type->get_base_type();
  3656.    MAKE_SIG(btype, avail, 1, m);
  3657.  
  3658.    ir_variable *SubFactor00 = body.make_temp(btype, "SubFactor00");
  3659.    ir_variable *SubFactor01 = body.make_temp(btype, "SubFactor01");
  3660.    ir_variable *SubFactor02 = body.make_temp(btype, "SubFactor02");
  3661.    ir_variable *SubFactor03 = body.make_temp(btype, "SubFactor03");
  3662.    ir_variable *SubFactor04 = body.make_temp(btype, "SubFactor04");
  3663.    ir_variable *SubFactor05 = body.make_temp(btype, "SubFactor05");
  3664.    ir_variable *SubFactor06 = body.make_temp(btype, "SubFactor06");
  3665.    ir_variable *SubFactor07 = body.make_temp(btype, "SubFactor07");
  3666.    ir_variable *SubFactor08 = body.make_temp(btype, "SubFactor08");
  3667.    ir_variable *SubFactor09 = body.make_temp(btype, "SubFactor09");
  3668.    ir_variable *SubFactor10 = body.make_temp(btype, "SubFactor10");
  3669.    ir_variable *SubFactor11 = body.make_temp(btype, "SubFactor11");
  3670.    ir_variable *SubFactor12 = body.make_temp(btype, "SubFactor12");
  3671.    ir_variable *SubFactor13 = body.make_temp(btype, "SubFactor13");
  3672.    ir_variable *SubFactor14 = body.make_temp(btype, "SubFactor14");
  3673.    ir_variable *SubFactor15 = body.make_temp(btype, "SubFactor15");
  3674.    ir_variable *SubFactor16 = body.make_temp(btype, "SubFactor16");
  3675.    ir_variable *SubFactor17 = body.make_temp(btype, "SubFactor17");
  3676.    ir_variable *SubFactor18 = body.make_temp(btype, "SubFactor18");
  3677.  
  3678.    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)))));
  3679.    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)))));
  3680.    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)))));
  3681.    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)))));
  3682.    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)))));
  3683.    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)))));
  3684.    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)))));
  3685.    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)))));
  3686.    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)))));
  3687.    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)))));
  3688.    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)))));
  3689.    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)))));
  3690.    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)))));
  3691.    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)))));
  3692.    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)))));
  3693.    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)))));
  3694.    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)))));
  3695.    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)))));
  3696.    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)))));
  3697.  
  3698.    ir_variable *adj_0 = body.make_temp(btype == glsl_type::float_type ? glsl_type::vec4_type : glsl_type::dvec4_type, "adj_0");
  3699.  
  3700.    body.emit(assign(adj_0,
  3701.                     add(sub(mul(matrix_elt(m, 1, 1), SubFactor00),
  3702.                             mul(matrix_elt(m, 1, 2), SubFactor01)),
  3703.                         mul(matrix_elt(m, 1, 3), SubFactor02)),
  3704.                     WRITEMASK_X));
  3705.    body.emit(assign(adj_0, neg(
  3706.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor00),
  3707.                             mul(matrix_elt(m, 1, 2), SubFactor03)),
  3708.                         mul(matrix_elt(m, 1, 3), SubFactor04))),
  3709.                     WRITEMASK_Y));
  3710.    body.emit(assign(adj_0,
  3711.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor01),
  3712.                             mul(matrix_elt(m, 1, 1), SubFactor03)),
  3713.                         mul(matrix_elt(m, 1, 3), SubFactor05)),
  3714.                     WRITEMASK_Z));
  3715.    body.emit(assign(adj_0, neg(
  3716.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor02),
  3717.                             mul(matrix_elt(m, 1, 1), SubFactor04)),
  3718.                         mul(matrix_elt(m, 1, 2), SubFactor05))),
  3719.                     WRITEMASK_W));
  3720.  
  3721.    body.emit(ret(dot(array_ref(m, 0), adj_0)));
  3722.  
  3723.    return sig;
  3724. }
  3725.  
  3726. ir_function_signature *
  3727. builtin_builder::_inverse_mat2(builtin_available_predicate avail, const glsl_type *type)
  3728. {
  3729.    ir_variable *m = in_var(type, "m");
  3730.    MAKE_SIG(type, avail, 1, m);
  3731.  
  3732.    ir_variable *adj = body.make_temp(type, "adj");
  3733.    body.emit(assign(array_ref(adj, 0), matrix_elt(m, 1, 1), 1 << 0));
  3734.    body.emit(assign(array_ref(adj, 0), neg(matrix_elt(m, 0, 1)), 1 << 1));
  3735.    body.emit(assign(array_ref(adj, 1), neg(matrix_elt(m, 1, 0)), 1 << 0));
  3736.    body.emit(assign(array_ref(adj, 1), matrix_elt(m, 0, 0), 1 << 1));
  3737.  
  3738.    ir_expression *det =
  3739.       sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
  3740.           mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1)));
  3741.  
  3742.    body.emit(ret(div(adj, det)));
  3743.    return sig;
  3744. }
  3745.  
  3746. ir_function_signature *
  3747. builtin_builder::_inverse_mat3(builtin_available_predicate avail, const glsl_type *type)
  3748. {
  3749.    ir_variable *m = in_var(type, "m");
  3750.    const glsl_type *btype = type->get_base_type();
  3751.    MAKE_SIG(type, avail, 1, m);
  3752.  
  3753.    ir_variable *f11_22_21_12 = body.make_temp(btype, "f11_22_21_12");
  3754.    ir_variable *f10_22_20_12 = body.make_temp(btype, "f10_22_20_12");
  3755.    ir_variable *f10_21_20_11 = body.make_temp(btype, "f10_21_20_11");
  3756.  
  3757.    body.emit(assign(f11_22_21_12,
  3758.                     sub(mul(matrix_elt(m, 1, 1), matrix_elt(m, 2, 2)),
  3759.                         mul(matrix_elt(m, 2, 1), matrix_elt(m, 1, 2)))));
  3760.    body.emit(assign(f10_22_20_12,
  3761.                     sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 2)),
  3762.                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 2)))));
  3763.    body.emit(assign(f10_21_20_11,
  3764.                     sub(mul(matrix_elt(m, 1, 0), matrix_elt(m, 2, 1)),
  3765.                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 1, 1)))));
  3766.  
  3767.    ir_variable *adj = body.make_temp(type, "adj");
  3768.    body.emit(assign(array_ref(adj, 0), f11_22_21_12, WRITEMASK_X));
  3769.    body.emit(assign(array_ref(adj, 1), neg(f10_22_20_12), WRITEMASK_X));
  3770.    body.emit(assign(array_ref(adj, 2), f10_21_20_11, WRITEMASK_X));
  3771.  
  3772.    body.emit(assign(array_ref(adj, 0), neg(
  3773.                     sub(mul(matrix_elt(m, 0, 1), matrix_elt(m, 2, 2)),
  3774.                         mul(matrix_elt(m, 2, 1), matrix_elt(m, 0, 2)))),
  3775.                     WRITEMASK_Y));
  3776.    body.emit(assign(array_ref(adj, 1),
  3777.                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 2, 2)),
  3778.                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 0, 2))),
  3779.                     WRITEMASK_Y));
  3780.    body.emit(assign(array_ref(adj, 2), neg(
  3781.                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 2, 1)),
  3782.                         mul(matrix_elt(m, 2, 0), matrix_elt(m, 0, 1)))),
  3783.                     WRITEMASK_Y));
  3784.  
  3785.    body.emit(assign(array_ref(adj, 0),
  3786.                     sub(mul(matrix_elt(m, 0, 1), matrix_elt(m, 1, 2)),
  3787.                         mul(matrix_elt(m, 1, 1), matrix_elt(m, 0, 2))),
  3788.                     WRITEMASK_Z));
  3789.    body.emit(assign(array_ref(adj, 1), neg(
  3790.                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 2)),
  3791.                         mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 2)))),
  3792.                     WRITEMASK_Z));
  3793.    body.emit(assign(array_ref(adj, 2),
  3794.                     sub(mul(matrix_elt(m, 0, 0), matrix_elt(m, 1, 1)),
  3795.                         mul(matrix_elt(m, 1, 0), matrix_elt(m, 0, 1))),
  3796.                     WRITEMASK_Z));
  3797.  
  3798.    ir_expression *det =
  3799.       add(sub(mul(matrix_elt(m, 0, 0), f11_22_21_12),
  3800.               mul(matrix_elt(m, 0, 1), f10_22_20_12)),
  3801.           mul(matrix_elt(m, 0, 2), f10_21_20_11));
  3802.  
  3803.    body.emit(ret(div(adj, det)));
  3804.  
  3805.    return sig;
  3806. }
  3807.  
  3808. ir_function_signature *
  3809. builtin_builder::_inverse_mat4(builtin_available_predicate avail, const glsl_type *type)
  3810. {
  3811.    ir_variable *m = in_var(type, "m");
  3812.    const glsl_type *btype = type->get_base_type();
  3813.    MAKE_SIG(type, avail, 1, m);
  3814.  
  3815.    ir_variable *SubFactor00 = body.make_temp(btype, "SubFactor00");
  3816.    ir_variable *SubFactor01 = body.make_temp(btype, "SubFactor01");
  3817.    ir_variable *SubFactor02 = body.make_temp(btype, "SubFactor02");
  3818.    ir_variable *SubFactor03 = body.make_temp(btype, "SubFactor03");
  3819.    ir_variable *SubFactor04 = body.make_temp(btype, "SubFactor04");
  3820.    ir_variable *SubFactor05 = body.make_temp(btype, "SubFactor05");
  3821.    ir_variable *SubFactor06 = body.make_temp(btype, "SubFactor06");
  3822.    ir_variable *SubFactor07 = body.make_temp(btype, "SubFactor07");
  3823.    ir_variable *SubFactor08 = body.make_temp(btype, "SubFactor08");
  3824.    ir_variable *SubFactor09 = body.make_temp(btype, "SubFactor09");
  3825.    ir_variable *SubFactor10 = body.make_temp(btype, "SubFactor10");
  3826.    ir_variable *SubFactor11 = body.make_temp(btype, "SubFactor11");
  3827.    ir_variable *SubFactor12 = body.make_temp(btype, "SubFactor12");
  3828.    ir_variable *SubFactor13 = body.make_temp(btype, "SubFactor13");
  3829.    ir_variable *SubFactor14 = body.make_temp(btype, "SubFactor14");
  3830.    ir_variable *SubFactor15 = body.make_temp(btype, "SubFactor15");
  3831.    ir_variable *SubFactor16 = body.make_temp(btype, "SubFactor16");
  3832.    ir_variable *SubFactor17 = body.make_temp(btype, "SubFactor17");
  3833.    ir_variable *SubFactor18 = body.make_temp(btype, "SubFactor18");
  3834.  
  3835.    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)))));
  3836.    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)))));
  3837.    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)))));
  3838.    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)))));
  3839.    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)))));
  3840.    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)))));
  3841.    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)))));
  3842.    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)))));
  3843.    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)))));
  3844.    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)))));
  3845.    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)))));
  3846.    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)))));
  3847.    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)))));
  3848.    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)))));
  3849.    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)))));
  3850.    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)))));
  3851.    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)))));
  3852.    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)))));
  3853.    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)))));
  3854.  
  3855.    ir_variable *adj = body.make_temp(btype == glsl_type::float_type ? glsl_type::mat4_type : glsl_type::dmat4_type, "adj");
  3856.    body.emit(assign(array_ref(adj, 0),
  3857.                     add(sub(mul(matrix_elt(m, 1, 1), SubFactor00),
  3858.                             mul(matrix_elt(m, 1, 2), SubFactor01)),
  3859.                         mul(matrix_elt(m, 1, 3), SubFactor02)),
  3860.                     WRITEMASK_X));
  3861.    body.emit(assign(array_ref(adj, 1), neg(
  3862.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor00),
  3863.                             mul(matrix_elt(m, 1, 2), SubFactor03)),
  3864.                         mul(matrix_elt(m, 1, 3), SubFactor04))),
  3865.                     WRITEMASK_X));
  3866.    body.emit(assign(array_ref(adj, 2),
  3867.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor01),
  3868.                             mul(matrix_elt(m, 1, 1), SubFactor03)),
  3869.                         mul(matrix_elt(m, 1, 3), SubFactor05)),
  3870.                     WRITEMASK_X));
  3871.    body.emit(assign(array_ref(adj, 3), neg(
  3872.                     add(sub(mul(matrix_elt(m, 1, 0), SubFactor02),
  3873.                             mul(matrix_elt(m, 1, 1), SubFactor04)),
  3874.                         mul(matrix_elt(m, 1, 2), SubFactor05))),
  3875.                     WRITEMASK_X));
  3876.  
  3877.    body.emit(assign(array_ref(adj, 0), neg(
  3878.                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor00),
  3879.                             mul(matrix_elt(m, 0, 2), SubFactor01)),
  3880.                         mul(matrix_elt(m, 0, 3), SubFactor02))),
  3881.                     WRITEMASK_Y));
  3882.    body.emit(assign(array_ref(adj, 1),
  3883.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor00),
  3884.                             mul(matrix_elt(m, 0, 2), SubFactor03)),
  3885.                         mul(matrix_elt(m, 0, 3), SubFactor04)),
  3886.                     WRITEMASK_Y));
  3887.    body.emit(assign(array_ref(adj, 2), neg(
  3888.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor01),
  3889.                             mul(matrix_elt(m, 0, 1), SubFactor03)),
  3890.                         mul(matrix_elt(m, 0, 3), SubFactor05))),
  3891.                     WRITEMASK_Y));
  3892.    body.emit(assign(array_ref(adj, 3),
  3893.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor02),
  3894.                             mul(matrix_elt(m, 0, 1), SubFactor04)),
  3895.                         mul(matrix_elt(m, 0, 2), SubFactor05)),
  3896.                     WRITEMASK_Y));
  3897.  
  3898.    body.emit(assign(array_ref(adj, 0),
  3899.                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor06),
  3900.                             mul(matrix_elt(m, 0, 2), SubFactor07)),
  3901.                         mul(matrix_elt(m, 0, 3), SubFactor08)),
  3902.                     WRITEMASK_Z));
  3903.    body.emit(assign(array_ref(adj, 1), neg(
  3904.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor06),
  3905.                             mul(matrix_elt(m, 0, 2), SubFactor09)),
  3906.                         mul(matrix_elt(m, 0, 3), SubFactor10))),
  3907.                     WRITEMASK_Z));
  3908.    body.emit(assign(array_ref(adj, 2),
  3909.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor11),
  3910.                             mul(matrix_elt(m, 0, 1), SubFactor09)),
  3911.                         mul(matrix_elt(m, 0, 3), SubFactor12)),
  3912.                     WRITEMASK_Z));
  3913.    body.emit(assign(array_ref(adj, 3), neg(
  3914.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor08),
  3915.                             mul(matrix_elt(m, 0, 1), SubFactor10)),
  3916.                         mul(matrix_elt(m, 0, 2), SubFactor12))),
  3917.                     WRITEMASK_Z));
  3918.  
  3919.    body.emit(assign(array_ref(adj, 0), neg(
  3920.                     add(sub(mul(matrix_elt(m, 0, 1), SubFactor13),
  3921.                             mul(matrix_elt(m, 0, 2), SubFactor14)),
  3922.                         mul(matrix_elt(m, 0, 3), SubFactor15))),
  3923.                     WRITEMASK_W));
  3924.    body.emit(assign(array_ref(adj, 1),
  3925.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor13),
  3926.                             mul(matrix_elt(m, 0, 2), SubFactor16)),
  3927.                         mul(matrix_elt(m, 0, 3), SubFactor17)),
  3928.                     WRITEMASK_W));
  3929.    body.emit(assign(array_ref(adj, 2), neg(
  3930.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor14),
  3931.                             mul(matrix_elt(m, 0, 1), SubFactor16)),
  3932.                         mul(matrix_elt(m, 0, 3), SubFactor18))),
  3933.                     WRITEMASK_W));
  3934.    body.emit(assign(array_ref(adj, 3),
  3935.                     add(sub(mul(matrix_elt(m, 0, 0), SubFactor15),
  3936.                             mul(matrix_elt(m, 0, 1), SubFactor17)),
  3937.                         mul(matrix_elt(m, 0, 2), SubFactor18)),
  3938.                     WRITEMASK_W));
  3939.  
  3940.    ir_expression *det =
  3941.       add(mul(matrix_elt(m, 0, 0), matrix_elt(adj, 0, 0)),
  3942.           add(mul(matrix_elt(m, 0, 1), matrix_elt(adj, 1, 0)),
  3943.               add(mul(matrix_elt(m, 0, 2), matrix_elt(adj, 2, 0)),
  3944.                   mul(matrix_elt(m, 0, 3), matrix_elt(adj, 3, 0)))));
  3945.  
  3946.    body.emit(ret(div(adj, det)));
  3947.  
  3948.    return sig;
  3949. }
  3950.  
  3951.  
  3952. ir_function_signature *
  3953. builtin_builder::_lessThan(builtin_available_predicate avail,
  3954.                            const glsl_type *type)
  3955. {
  3956.    return binop(ir_binop_less, avail,
  3957.                 glsl_type::bvec(type->vector_elements), type, type);
  3958. }
  3959.  
  3960. ir_function_signature *
  3961. builtin_builder::_lessThanEqual(builtin_available_predicate avail,
  3962.                                 const glsl_type *type)
  3963. {
  3964.    return binop(ir_binop_lequal, avail,
  3965.                 glsl_type::bvec(type->vector_elements), type, type);
  3966. }
  3967.  
  3968. ir_function_signature *
  3969. builtin_builder::_greaterThan(builtin_available_predicate avail,
  3970.                               const glsl_type *type)
  3971. {
  3972.    return binop(ir_binop_greater, avail,
  3973.                 glsl_type::bvec(type->vector_elements), type, type);
  3974. }
  3975.  
  3976. ir_function_signature *
  3977. builtin_builder::_greaterThanEqual(builtin_available_predicate avail,
  3978.                                    const glsl_type *type)
  3979. {
  3980.    return binop(ir_binop_gequal, avail,
  3981.                 glsl_type::bvec(type->vector_elements), type, type);
  3982. }
  3983.  
  3984. ir_function_signature *
  3985. builtin_builder::_equal(builtin_available_predicate avail,
  3986.                         const glsl_type *type)
  3987. {
  3988.    return binop(ir_binop_equal, avail,
  3989.                 glsl_type::bvec(type->vector_elements), type, type);
  3990. }
  3991.  
  3992. ir_function_signature *
  3993. builtin_builder::_notEqual(builtin_available_predicate avail,
  3994.                            const glsl_type *type)
  3995. {
  3996.    return binop(ir_binop_nequal, avail,
  3997.                 glsl_type::bvec(type->vector_elements), type, type);
  3998. }
  3999.  
  4000. ir_function_signature *
  4001. builtin_builder::_any(const glsl_type *type)
  4002. {
  4003.    return unop(always_available, ir_unop_any, glsl_type::bool_type, type);
  4004. }
  4005.  
  4006. ir_function_signature *
  4007. builtin_builder::_all(const glsl_type *type)
  4008. {
  4009.    ir_variable *v = in_var(type, "v");
  4010.    MAKE_SIG(glsl_type::bool_type, always_available, 1, v);
  4011.  
  4012.    switch (type->vector_elements) {
  4013.    case 2:
  4014.       body.emit(ret(logic_and(swizzle_x(v), swizzle_y(v))));
  4015.       break;
  4016.    case 3:
  4017.       body.emit(ret(logic_and(logic_and(swizzle_x(v), swizzle_y(v)),
  4018.                               swizzle_z(v))));
  4019.       break;
  4020.    case 4:
  4021.       body.emit(ret(logic_and(logic_and(logic_and(swizzle_x(v), swizzle_y(v)),
  4022.                                         swizzle_z(v)),
  4023.                               swizzle_w(v))));
  4024.       break;
  4025.    }
  4026.  
  4027.    return sig;
  4028. }
  4029.  
  4030. UNOP(not, ir_unop_logic_not, always_available)
  4031.  
  4032. static bool
  4033. has_lod(const glsl_type *sampler_type)
  4034. {
  4035.    assert(sampler_type->is_sampler());
  4036.  
  4037.    switch (sampler_type->sampler_dimensionality) {
  4038.    case GLSL_SAMPLER_DIM_RECT:
  4039.    case GLSL_SAMPLER_DIM_BUF:
  4040.    case GLSL_SAMPLER_DIM_MS:
  4041.       return false;
  4042.    default:
  4043.       return true;
  4044.    }
  4045. }
  4046.  
  4047. ir_function_signature *
  4048. builtin_builder::_textureSize(builtin_available_predicate avail,
  4049.                               const glsl_type *return_type,
  4050.                               const glsl_type *sampler_type)
  4051. {
  4052.    ir_variable *s = in_var(sampler_type, "sampler");
  4053.    /* The sampler always exists; add optional lod later. */
  4054.    MAKE_SIG(return_type, avail, 1, s);
  4055.  
  4056.    ir_texture *tex = new(mem_ctx) ir_texture(ir_txs);
  4057.    tex->set_sampler(new(mem_ctx) ir_dereference_variable(s), return_type);
  4058.  
  4059.    if (has_lod(sampler_type)) {
  4060.       ir_variable *lod = in_var(glsl_type::int_type, "lod");
  4061.       sig->parameters.push_tail(lod);
  4062.       tex->lod_info.lod = var_ref(lod);
  4063.    } else {
  4064.       tex->lod_info.lod = imm(0u);
  4065.    }
  4066.  
  4067.    body.emit(ret(tex));
  4068.  
  4069.    return sig;
  4070. }
  4071.  
  4072. ir_function_signature *
  4073. builtin_builder::_texture(ir_texture_opcode opcode,
  4074.                           builtin_available_predicate avail,
  4075.                           const glsl_type *return_type,
  4076.                           const glsl_type *sampler_type,
  4077.                           const glsl_type *coord_type,
  4078.                           int flags)
  4079. {
  4080.    ir_variable *s = in_var(sampler_type, "sampler");
  4081.    ir_variable *P = in_var(coord_type, "P");
  4082.    /* The sampler and coordinate always exist; add optional parameters later. */
  4083.    MAKE_SIG(return_type, avail, 2, s, P);
  4084.  
  4085.    ir_texture *tex = new(mem_ctx) ir_texture(opcode);
  4086.    tex->set_sampler(var_ref(s), return_type);
  4087.  
  4088.    const int coord_size = sampler_type->coordinate_components();
  4089.  
  4090.    if (coord_size == coord_type->vector_elements) {
  4091.       tex->coordinate = var_ref(P);
  4092.    } else {
  4093.       /* The incoming coordinate also has the projector or shadow comparitor,
  4094.        * so we need to swizzle those away.
  4095.        */
  4096.       tex->coordinate = swizzle_for_size(P, coord_size);
  4097.    }
  4098.  
  4099.    /* The projector is always in the last component. */
  4100.    if (flags & TEX_PROJECT)
  4101.       tex->projector = swizzle(P, coord_type->vector_elements - 1, 1);
  4102.  
  4103.    if (sampler_type->sampler_shadow) {
  4104.       if (opcode == ir_tg4) {
  4105.          /* gather has refz as a separate parameter, immediately after the
  4106.           * coordinate
  4107.           */
  4108.          ir_variable *refz = in_var(glsl_type::float_type, "refz");
  4109.          sig->parameters.push_tail(refz);
  4110.          tex->shadow_comparitor = var_ref(refz);
  4111.       } else {
  4112.          /* The shadow comparitor is normally in the Z component, but a few types
  4113.           * have sufficiently large coordinates that it's in W.
  4114.           */
  4115.          tex->shadow_comparitor = swizzle(P, MAX2(coord_size, SWIZZLE_Z), 1);
  4116.       }
  4117.    }
  4118.  
  4119.    if (opcode == ir_txl) {
  4120.       ir_variable *lod = in_var(glsl_type::float_type, "lod");
  4121.       sig->parameters.push_tail(lod);
  4122.       tex->lod_info.lod = var_ref(lod);
  4123.    } else if (opcode == ir_txd) {
  4124.       int grad_size = coord_size - (sampler_type->sampler_array ? 1 : 0);
  4125.       ir_variable *dPdx = in_var(glsl_type::vec(grad_size), "dPdx");
  4126.       ir_variable *dPdy = in_var(glsl_type::vec(grad_size), "dPdy");
  4127.       sig->parameters.push_tail(dPdx);
  4128.       sig->parameters.push_tail(dPdy);
  4129.       tex->lod_info.grad.dPdx = var_ref(dPdx);
  4130.       tex->lod_info.grad.dPdy = var_ref(dPdy);
  4131.    }
  4132.  
  4133.    if (flags & (TEX_OFFSET | TEX_OFFSET_NONCONST)) {
  4134.       int offset_size = coord_size - (sampler_type->sampler_array ? 1 : 0);
  4135.       ir_variable *offset =
  4136.          new(mem_ctx) ir_variable(glsl_type::ivec(offset_size), "offset",
  4137.                                   (flags & TEX_OFFSET) ? ir_var_const_in : ir_var_function_in);
  4138.       sig->parameters.push_tail(offset);
  4139.       tex->offset = var_ref(offset);
  4140.    }
  4141.  
  4142.    if (flags & TEX_OFFSET_ARRAY) {
  4143.       ir_variable *offsets =
  4144.          new(mem_ctx) ir_variable(glsl_type::get_array_instance(glsl_type::ivec2_type, 4),
  4145.                                   "offsets", ir_var_const_in);
  4146.       sig->parameters.push_tail(offsets);
  4147.       tex->offset = var_ref(offsets);
  4148.    }
  4149.  
  4150.    if (opcode == ir_tg4) {
  4151.       if (flags & TEX_COMPONENT) {
  4152.          ir_variable *component =
  4153.             new(mem_ctx) ir_variable(glsl_type::int_type, "comp", ir_var_const_in);
  4154.          sig->parameters.push_tail(component);
  4155.          tex->lod_info.component = var_ref(component);
  4156.       }
  4157.       else {
  4158.          tex->lod_info.component = imm(0);
  4159.       }
  4160.    }
  4161.  
  4162.    /* The "bias" parameter comes /after/ the "offset" parameter, which is
  4163.     * inconsistent with both textureLodOffset and textureGradOffset.
  4164.     */
  4165.    if (opcode == ir_txb) {
  4166.       ir_variable *bias = in_var(glsl_type::float_type, "bias");
  4167.       sig->parameters.push_tail(bias);
  4168.       tex->lod_info.bias = var_ref(bias);
  4169.    }
  4170.  
  4171.    body.emit(ret(tex));
  4172.  
  4173.    return sig;
  4174. }
  4175.  
  4176. ir_function_signature *
  4177. builtin_builder::_textureCubeArrayShadow()
  4178. {
  4179.    ir_variable *s = in_var(glsl_type::samplerCubeArrayShadow_type, "sampler");
  4180.    ir_variable *P = in_var(glsl_type::vec4_type, "P");
  4181.    ir_variable *compare = in_var(glsl_type::float_type, "compare");
  4182.    MAKE_SIG(glsl_type::float_type, texture_cube_map_array, 3, s, P, compare);
  4183.  
  4184.    ir_texture *tex = new(mem_ctx) ir_texture(ir_tex);
  4185.    tex->set_sampler(var_ref(s), glsl_type::float_type);
  4186.  
  4187.    tex->coordinate = var_ref(P);
  4188.    tex->shadow_comparitor = var_ref(compare);
  4189.  
  4190.    body.emit(ret(tex));
  4191.  
  4192.    return sig;
  4193. }
  4194.  
  4195. ir_function_signature *
  4196. builtin_builder::_texelFetch(builtin_available_predicate avail,
  4197.                              const glsl_type *return_type,
  4198.                              const glsl_type *sampler_type,
  4199.                              const glsl_type *coord_type,
  4200.                              const glsl_type *offset_type)
  4201. {
  4202.    ir_variable *s = in_var(sampler_type, "sampler");
  4203.    ir_variable *P = in_var(coord_type, "P");
  4204.    /* The sampler and coordinate always exist; add optional parameters later. */
  4205.    MAKE_SIG(return_type, avail, 2, s, P);
  4206.  
  4207.    ir_texture *tex = new(mem_ctx) ir_texture(ir_txf);
  4208.    tex->coordinate = var_ref(P);
  4209.    tex->set_sampler(var_ref(s), return_type);
  4210.  
  4211.    if (sampler_type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
  4212.       ir_variable *sample = in_var(glsl_type::int_type, "sample");
  4213.       sig->parameters.push_tail(sample);
  4214.       tex->lod_info.sample_index = var_ref(sample);
  4215.       tex->op = ir_txf_ms;
  4216.    } else if (has_lod(sampler_type)) {
  4217.       ir_variable *lod = in_var(glsl_type::int_type, "lod");
  4218.       sig->parameters.push_tail(lod);
  4219.       tex->lod_info.lod = var_ref(lod);
  4220.    } else {
  4221.       tex->lod_info.lod = imm(0u);
  4222.    }
  4223.  
  4224.    if (offset_type != NULL) {
  4225.       ir_variable *offset =
  4226.          new(mem_ctx) ir_variable(offset_type, "offset", ir_var_const_in);
  4227.       sig->parameters.push_tail(offset);
  4228.       tex->offset = var_ref(offset);
  4229.    }
  4230.  
  4231.    body.emit(ret(tex));
  4232.  
  4233.    return sig;
  4234. }
  4235.  
  4236. ir_function_signature *
  4237. builtin_builder::_EmitVertex()
  4238. {
  4239.    MAKE_SIG(glsl_type::void_type, gs_only, 0);
  4240.  
  4241.    ir_rvalue *stream = new(mem_ctx) ir_constant(0, 1);
  4242.    body.emit(new(mem_ctx) ir_emit_vertex(stream));
  4243.  
  4244.    return sig;
  4245. }
  4246.  
  4247. ir_function_signature *
  4248. builtin_builder::_EmitStreamVertex(builtin_available_predicate avail,
  4249.                                    const glsl_type *stream_type)
  4250. {
  4251.    /* Section 8.12 (Geometry Shader Functions) of the GLSL 4.0 spec says:
  4252.     *
  4253.     *     "Emit the current values of output variables to the current output
  4254.     *     primitive on stream stream. The argument to stream must be a constant
  4255.     *     integral expression."
  4256.     */
  4257.    ir_variable *stream =
  4258.       new(mem_ctx) ir_variable(stream_type, "stream", ir_var_const_in);
  4259.  
  4260.    MAKE_SIG(glsl_type::void_type, avail, 1, stream);
  4261.  
  4262.    body.emit(new(mem_ctx) ir_emit_vertex(var_ref(stream)));
  4263.  
  4264.    return sig;
  4265. }
  4266.  
  4267. ir_function_signature *
  4268. builtin_builder::_EndPrimitive()
  4269. {
  4270.    MAKE_SIG(glsl_type::void_type, gs_only, 0);
  4271.  
  4272.    ir_rvalue *stream = new(mem_ctx) ir_constant(0, 1);
  4273.    body.emit(new(mem_ctx) ir_end_primitive(stream));
  4274.  
  4275.    return sig;
  4276. }
  4277.  
  4278. ir_function_signature *
  4279. builtin_builder::_EndStreamPrimitive(builtin_available_predicate avail,
  4280.                                      const glsl_type *stream_type)
  4281. {
  4282.    /* Section 8.12 (Geometry Shader Functions) of the GLSL 4.0 spec says:
  4283.     *
  4284.     *     "Completes the current output primitive on stream stream and starts
  4285.     *     a new one. The argument to stream must be a constant integral
  4286.     *     expression."
  4287.     */
  4288.    ir_variable *stream =
  4289.       new(mem_ctx) ir_variable(stream_type, "stream", ir_var_const_in);
  4290.  
  4291.    MAKE_SIG(glsl_type::void_type, avail, 1, stream);
  4292.  
  4293.    body.emit(new(mem_ctx) ir_end_primitive(var_ref(stream)));
  4294.  
  4295.    return sig;
  4296. }
  4297.  
  4298. ir_function_signature *
  4299. builtin_builder::_textureQueryLod(const glsl_type *sampler_type,
  4300.                                   const glsl_type *coord_type)
  4301. {
  4302.    ir_variable *s = in_var(sampler_type, "sampler");
  4303.    ir_variable *coord = in_var(coord_type, "coord");
  4304.    /* The sampler and coordinate always exist; add optional parameters later. */
  4305.    MAKE_SIG(glsl_type::vec2_type, texture_query_lod, 2, s, coord);
  4306.  
  4307.    ir_texture *tex = new(mem_ctx) ir_texture(ir_lod);
  4308.    tex->coordinate = var_ref(coord);
  4309.    tex->set_sampler(var_ref(s), glsl_type::vec2_type);
  4310.  
  4311.    body.emit(ret(tex));
  4312.  
  4313.    return sig;
  4314. }
  4315.  
  4316. ir_function_signature *
  4317. builtin_builder::_textureQueryLevels(const glsl_type *sampler_type)
  4318. {
  4319.    ir_variable *s = in_var(sampler_type, "sampler");
  4320.    const glsl_type *return_type = glsl_type::int_type;
  4321.    MAKE_SIG(return_type, texture_query_levels, 1, s);
  4322.  
  4323.    ir_texture *tex = new(mem_ctx) ir_texture(ir_query_levels);
  4324.    tex->set_sampler(var_ref(s), return_type);
  4325.  
  4326.    body.emit(ret(tex));
  4327.  
  4328.    return sig;
  4329. }
  4330.  
  4331. UNOP(dFdx, ir_unop_dFdx, fs_oes_derivatives)
  4332. UNOP(dFdxCoarse, ir_unop_dFdx_coarse, fs_derivative_control)
  4333. UNOP(dFdxFine, ir_unop_dFdx_fine, fs_derivative_control)
  4334. UNOP(dFdy, ir_unop_dFdy, fs_oes_derivatives)
  4335. UNOP(dFdyCoarse, ir_unop_dFdy_coarse, fs_derivative_control)
  4336. UNOP(dFdyFine, ir_unop_dFdy_fine, fs_derivative_control)
  4337.  
  4338. ir_function_signature *
  4339. builtin_builder::_fwidth(const glsl_type *type)
  4340. {
  4341.    ir_variable *p = in_var(type, "p");
  4342.    MAKE_SIG(type, fs_oes_derivatives, 1, p);
  4343.  
  4344.    body.emit(ret(add(abs(expr(ir_unop_dFdx, p)), abs(expr(ir_unop_dFdy, p)))));
  4345.  
  4346.    return sig;
  4347. }
  4348.  
  4349. ir_function_signature *
  4350. builtin_builder::_fwidthCoarse(const glsl_type *type)
  4351. {
  4352.    ir_variable *p = in_var(type, "p");
  4353.    MAKE_SIG(type, fs_derivative_control, 1, p);
  4354.  
  4355.    body.emit(ret(add(abs(expr(ir_unop_dFdx_coarse, p)),
  4356.                      abs(expr(ir_unop_dFdy_coarse, p)))));
  4357.  
  4358.    return sig;
  4359. }
  4360.  
  4361. ir_function_signature *
  4362. builtin_builder::_fwidthFine(const glsl_type *type)
  4363. {
  4364.    ir_variable *p = in_var(type, "p");
  4365.    MAKE_SIG(type, fs_derivative_control, 1, p);
  4366.  
  4367.    body.emit(ret(add(abs(expr(ir_unop_dFdx_fine, p)),
  4368.                      abs(expr(ir_unop_dFdy_fine, p)))));
  4369.  
  4370.    return sig;
  4371. }
  4372.  
  4373. ir_function_signature *
  4374. builtin_builder::_noise1(const glsl_type *type)
  4375. {
  4376.    return unop(v110, ir_unop_noise, glsl_type::float_type, type);
  4377. }
  4378.  
  4379. ir_function_signature *
  4380. builtin_builder::_noise2(const glsl_type *type)
  4381. {
  4382.    ir_variable *p = in_var(type, "p");
  4383.    MAKE_SIG(glsl_type::vec2_type, v110, 1, p);
  4384.  
  4385.    ir_constant_data b_offset;
  4386.    b_offset.f[0] = 601.0f;
  4387.    b_offset.f[1] = 313.0f;
  4388.    b_offset.f[2] = 29.0f;
  4389.    b_offset.f[3] = 277.0f;
  4390.  
  4391.    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
  4392.    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
  4393.    ir_variable *t = body.make_temp(glsl_type::vec2_type,  "t");
  4394.    body.emit(assign(a, expr(ir_unop_noise, p)));
  4395.    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, b_offset)))));
  4396.    body.emit(assign(t, a, WRITEMASK_X));
  4397.    body.emit(assign(t, b, WRITEMASK_Y));
  4398.    body.emit(ret(t));
  4399.  
  4400.    return sig;
  4401. }
  4402.  
  4403. ir_function_signature *
  4404. builtin_builder::_noise3(const glsl_type *type)
  4405. {
  4406.    ir_variable *p = in_var(type, "p");
  4407.    MAKE_SIG(glsl_type::vec3_type, v110, 1, p);
  4408.  
  4409.    ir_constant_data b_offset;
  4410.    b_offset.f[0] = 601.0f;
  4411.    b_offset.f[1] = 313.0f;
  4412.    b_offset.f[2] = 29.0f;
  4413.    b_offset.f[3] = 277.0f;
  4414.  
  4415.    ir_constant_data c_offset;
  4416.    c_offset.f[0] = 1559.0f;
  4417.    c_offset.f[1] = 113.0f;
  4418.    c_offset.f[2] = 1861.0f;
  4419.    c_offset.f[3] = 797.0f;
  4420.  
  4421.    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
  4422.    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
  4423.    ir_variable *c = body.make_temp(glsl_type::float_type, "c");
  4424.    ir_variable *t = body.make_temp(glsl_type::vec3_type,  "t");
  4425.    body.emit(assign(a, expr(ir_unop_noise, p)));
  4426.    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, b_offset)))));
  4427.    body.emit(assign(c, expr(ir_unop_noise, add(p, imm(type, c_offset)))));
  4428.    body.emit(assign(t, a, WRITEMASK_X));
  4429.    body.emit(assign(t, b, WRITEMASK_Y));
  4430.    body.emit(assign(t, c, WRITEMASK_Z));
  4431.    body.emit(ret(t));
  4432.  
  4433.    return sig;
  4434. }
  4435.  
  4436. ir_function_signature *
  4437. builtin_builder::_noise4(const glsl_type *type)
  4438. {
  4439.    ir_variable *p = in_var(type, "p");
  4440.    MAKE_SIG(glsl_type::vec4_type, v110, 1, p);
  4441.  
  4442.    ir_variable *_p = body.make_temp(type, "_p");
  4443.  
  4444.    ir_constant_data p_offset;
  4445.    p_offset.f[0] = 1559.0f;
  4446.    p_offset.f[1] = 113.0f;
  4447.    p_offset.f[2] = 1861.0f;
  4448.    p_offset.f[3] = 797.0f;
  4449.  
  4450.    body.emit(assign(_p, add(p, imm(type, p_offset))));
  4451.  
  4452.    ir_constant_data offset;
  4453.    offset.f[0] = 601.0f;
  4454.    offset.f[1] = 313.0f;
  4455.    offset.f[2] = 29.0f;
  4456.    offset.f[3] = 277.0f;
  4457.  
  4458.    ir_variable *a = body.make_temp(glsl_type::float_type, "a");
  4459.    ir_variable *b = body.make_temp(glsl_type::float_type, "b");
  4460.    ir_variable *c = body.make_temp(glsl_type::float_type, "c");
  4461.    ir_variable *d = body.make_temp(glsl_type::float_type, "d");
  4462.    ir_variable *t = body.make_temp(glsl_type::vec4_type,  "t");
  4463.    body.emit(assign(a, expr(ir_unop_noise, p)));
  4464.    body.emit(assign(b, expr(ir_unop_noise, add(p, imm(type, offset)))));
  4465.    body.emit(assign(c, expr(ir_unop_noise, _p)));
  4466.    body.emit(assign(d, expr(ir_unop_noise, add(_p, imm(type, offset)))));
  4467.    body.emit(assign(t, a, WRITEMASK_X));
  4468.    body.emit(assign(t, b, WRITEMASK_Y));
  4469.    body.emit(assign(t, c, WRITEMASK_Z));
  4470.    body.emit(assign(t, d, WRITEMASK_W));
  4471.    body.emit(ret(t));
  4472.  
  4473.    return sig;
  4474. }
  4475.  
  4476. ir_function_signature *
  4477. builtin_builder::_bitfieldExtract(const glsl_type *type)
  4478. {
  4479.    ir_variable *value  = in_var(type, "value");
  4480.    ir_variable *offset = in_var(glsl_type::int_type, "offset");
  4481.    ir_variable *bits   = in_var(glsl_type::int_type, "bits");
  4482.    MAKE_SIG(type, gpu_shader5_or_es31, 3, value, offset, bits);
  4483.  
  4484.    body.emit(ret(expr(ir_triop_bitfield_extract, value, offset, bits)));
  4485.  
  4486.    return sig;
  4487. }
  4488.  
  4489. ir_function_signature *
  4490. builtin_builder::_bitfieldInsert(const glsl_type *type)
  4491. {
  4492.    ir_variable *base   = in_var(type, "base");
  4493.    ir_variable *insert = in_var(type, "insert");
  4494.    ir_variable *offset = in_var(glsl_type::int_type, "offset");
  4495.    ir_variable *bits   = in_var(glsl_type::int_type, "bits");
  4496.    MAKE_SIG(type, gpu_shader5_or_es31, 4, base, insert, offset, bits);
  4497.  
  4498.    body.emit(ret(bitfield_insert(base, insert, offset, bits)));
  4499.  
  4500.    return sig;
  4501. }
  4502.  
  4503. UNOP(bitfieldReverse, ir_unop_bitfield_reverse, gpu_shader5_or_es31)
  4504.  
  4505. ir_function_signature *
  4506. builtin_builder::_bitCount(const glsl_type *type)
  4507. {
  4508.    return unop(gpu_shader5_or_es31, ir_unop_bit_count,
  4509.                glsl_type::ivec(type->vector_elements), type);
  4510. }
  4511.  
  4512. ir_function_signature *
  4513. builtin_builder::_findLSB(const glsl_type *type)
  4514. {
  4515.    return unop(gpu_shader5_or_es31, ir_unop_find_lsb,
  4516.                glsl_type::ivec(type->vector_elements), type);
  4517. }
  4518.  
  4519. ir_function_signature *
  4520. builtin_builder::_findMSB(const glsl_type *type)
  4521. {
  4522.    return unop(gpu_shader5_or_es31, ir_unop_find_msb,
  4523.                glsl_type::ivec(type->vector_elements), type);
  4524. }
  4525.  
  4526. ir_function_signature *
  4527. builtin_builder::_fma(builtin_available_predicate avail, const glsl_type *type)
  4528. {
  4529.    ir_variable *a = in_var(type, "a");
  4530.    ir_variable *b = in_var(type, "b");
  4531.    ir_variable *c = in_var(type, "c");
  4532.    MAKE_SIG(type, avail, 3, a, b, c);
  4533.  
  4534.    body.emit(ret(ir_builder::fma(a, b, c)));
  4535.  
  4536.    return sig;
  4537. }
  4538.  
  4539. ir_function_signature *
  4540. builtin_builder::_ldexp(const glsl_type *x_type, const glsl_type *exp_type)
  4541. {
  4542.    return binop(ir_binop_ldexp, x_type->base_type == GLSL_TYPE_DOUBLE ? fp64 : gpu_shader5_or_es31, x_type, x_type, exp_type);
  4543. }
  4544.  
  4545. ir_function_signature *
  4546. builtin_builder::_dfrexp(const glsl_type *x_type, const glsl_type *exp_type)
  4547. {
  4548.    ir_variable *x = in_var(x_type, "x");
  4549.    ir_variable *exponent = out_var(exp_type, "exp");
  4550.    MAKE_SIG(x_type, fp64, 2, x, exponent);
  4551.  
  4552.    body.emit(assign(exponent, expr(ir_unop_frexp_exp, x)));
  4553.  
  4554.    body.emit(ret(expr(ir_unop_frexp_sig, x)));
  4555.    return sig;
  4556. }
  4557.  
  4558. ir_function_signature *
  4559. builtin_builder::_frexp(const glsl_type *x_type, const glsl_type *exp_type)
  4560. {
  4561.    ir_variable *x = in_var(x_type, "x");
  4562.    ir_variable *exponent = out_var(exp_type, "exp");
  4563.    MAKE_SIG(x_type, gpu_shader5_or_es31, 2, x, exponent);
  4564.  
  4565.    const unsigned vec_elem = x_type->vector_elements;
  4566.    const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
  4567.    const glsl_type *uvec = glsl_type::get_instance(GLSL_TYPE_UINT, vec_elem, 1);
  4568.  
  4569.    /* Single-precision floating-point values are stored as
  4570.     *   1 sign bit;
  4571.     *   8 exponent bits;
  4572.     *   23 mantissa bits.
  4573.     *
  4574.     * An exponent shift of 23 will shift the mantissa out, leaving only the
  4575.     * exponent and sign bit (which itself may be zero, if the absolute value
  4576.     * was taken before the bitcast and shift.
  4577.     */
  4578.    ir_constant *exponent_shift = imm(23);
  4579.    ir_constant *exponent_bias = imm(-126, vec_elem);
  4580.  
  4581.    ir_constant *sign_mantissa_mask = imm(0x807fffffu, vec_elem);
  4582.  
  4583.    /* Exponent of floating-point values in the range [0.5, 1.0). */
  4584.    ir_constant *exponent_value = imm(0x3f000000u, vec_elem);
  4585.  
  4586.    ir_variable *is_not_zero = body.make_temp(bvec, "is_not_zero");
  4587.    body.emit(assign(is_not_zero, nequal(abs(x), imm(0.0f, vec_elem))));
  4588.  
  4589.    /* Since abs(x) ensures that the sign bit is zero, we don't need to bitcast
  4590.     * to unsigned integers to ensure that 1 bits aren't shifted in.
  4591.     */
  4592.    body.emit(assign(exponent, rshift(bitcast_f2i(abs(x)), exponent_shift)));
  4593.    body.emit(assign(exponent, add(exponent, csel(is_not_zero, exponent_bias,
  4594.                                                      imm(0, vec_elem)))));
  4595.  
  4596.    ir_variable *bits = body.make_temp(uvec, "bits");
  4597.    body.emit(assign(bits, bitcast_f2u(x)));
  4598.    body.emit(assign(bits, bit_and(bits, sign_mantissa_mask)));
  4599.    body.emit(assign(bits, bit_or(bits, csel(is_not_zero, exponent_value,
  4600.                                                 imm(0u, vec_elem)))));
  4601.    body.emit(ret(bitcast_u2f(bits)));
  4602.  
  4603.    return sig;
  4604. }
  4605.  
  4606. ir_function_signature *
  4607. builtin_builder::_uaddCarry(const glsl_type *type)
  4608. {
  4609.    ir_variable *x = in_var(type, "x");
  4610.    ir_variable *y = in_var(type, "y");
  4611.    ir_variable *carry = out_var(type, "carry");
  4612.    MAKE_SIG(type, gpu_shader5_or_es31, 3, x, y, carry);
  4613.  
  4614.    body.emit(assign(carry, ir_builder::carry(x, y)));
  4615.    body.emit(ret(add(x, y)));
  4616.  
  4617.    return sig;
  4618. }
  4619.  
  4620. ir_function_signature *
  4621. builtin_builder::_usubBorrow(const glsl_type *type)
  4622. {
  4623.    ir_variable *x = in_var(type, "x");
  4624.    ir_variable *y = in_var(type, "y");
  4625.    ir_variable *borrow = out_var(type, "borrow");
  4626.    MAKE_SIG(type, gpu_shader5_or_es31, 3, x, y, borrow);
  4627.  
  4628.    body.emit(assign(borrow, ir_builder::borrow(x, y)));
  4629.    body.emit(ret(sub(x, y)));
  4630.  
  4631.    return sig;
  4632. }
  4633.  
  4634. /**
  4635.  * For both imulExtended() and umulExtended() built-ins.
  4636.  */
  4637. ir_function_signature *
  4638. builtin_builder::_mulExtended(const glsl_type *type)
  4639. {
  4640.    ir_variable *x = in_var(type, "x");
  4641.    ir_variable *y = in_var(type, "y");
  4642.    ir_variable *msb = out_var(type, "msb");
  4643.    ir_variable *lsb = out_var(type, "lsb");
  4644.    MAKE_SIG(glsl_type::void_type, gpu_shader5_or_es31, 4, x, y, msb, lsb);
  4645.  
  4646.    body.emit(assign(msb, imul_high(x, y)));
  4647.    body.emit(assign(lsb, mul(x, y)));
  4648.  
  4649.    return sig;
  4650. }
  4651.  
  4652. ir_function_signature *
  4653. builtin_builder::_interpolateAtCentroid(const glsl_type *type)
  4654. {
  4655.    ir_variable *interpolant = in_var(type, "interpolant");
  4656.    interpolant->data.must_be_shader_input = 1;
  4657.    MAKE_SIG(type, fs_gpu_shader5, 1, interpolant);
  4658.  
  4659.    body.emit(ret(interpolate_at_centroid(interpolant)));
  4660.  
  4661.    return sig;
  4662. }
  4663.  
  4664. ir_function_signature *
  4665. builtin_builder::_interpolateAtOffset(const glsl_type *type)
  4666. {
  4667.    ir_variable *interpolant = in_var(type, "interpolant");
  4668.    interpolant->data.must_be_shader_input = 1;
  4669.    ir_variable *offset = in_var(glsl_type::vec2_type, "offset");
  4670.    MAKE_SIG(type, fs_gpu_shader5, 2, interpolant, offset);
  4671.  
  4672.    body.emit(ret(interpolate_at_offset(interpolant, offset)));
  4673.  
  4674.    return sig;
  4675. }
  4676.  
  4677. ir_function_signature *
  4678. builtin_builder::_interpolateAtSample(const glsl_type *type)
  4679. {
  4680.    ir_variable *interpolant = in_var(type, "interpolant");
  4681.    interpolant->data.must_be_shader_input = 1;
  4682.    ir_variable *sample_num = in_var(glsl_type::int_type, "sample_num");
  4683.    MAKE_SIG(type, fs_gpu_shader5, 2, interpolant, sample_num);
  4684.  
  4685.    body.emit(ret(interpolate_at_sample(interpolant, sample_num)));
  4686.  
  4687.    return sig;
  4688. }
  4689.  
  4690. ir_function_signature *
  4691. builtin_builder::_atomic_intrinsic(builtin_available_predicate avail)
  4692. {
  4693.    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "counter");
  4694.    MAKE_INTRINSIC(glsl_type::uint_type, avail, 1, counter);
  4695.    return sig;
  4696. }
  4697.  
  4698. ir_function_signature *
  4699. builtin_builder::_atomic_op(const char *intrinsic,
  4700.                             builtin_available_predicate avail)
  4701. {
  4702.    ir_variable *counter = in_var(glsl_type::atomic_uint_type, "atomic_counter");
  4703.    MAKE_SIG(glsl_type::uint_type, avail, 1, counter);
  4704.  
  4705.    ir_variable *retval = body.make_temp(glsl_type::uint_type, "atomic_retval");
  4706.    body.emit(call(shader->symbols->get_function(intrinsic), retval,
  4707.                   sig->parameters));
  4708.    body.emit(ret(retval));
  4709.    return sig;
  4710. }
  4711.  
  4712. ir_function_signature *
  4713. builtin_builder::_min3(const glsl_type *type)
  4714. {
  4715.    ir_variable *x = in_var(type, "x");
  4716.    ir_variable *y = in_var(type, "y");
  4717.    ir_variable *z = in_var(type, "z");
  4718.    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
  4719.  
  4720.    ir_expression *min3 = min2(x, min2(y,z));
  4721.    body.emit(ret(min3));
  4722.  
  4723.    return sig;
  4724. }
  4725.  
  4726. ir_function_signature *
  4727. builtin_builder::_max3(const glsl_type *type)
  4728. {
  4729.    ir_variable *x = in_var(type, "x");
  4730.    ir_variable *y = in_var(type, "y");
  4731.    ir_variable *z = in_var(type, "z");
  4732.    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
  4733.  
  4734.    ir_expression *max3 = max2(x, max2(y,z));
  4735.    body.emit(ret(max3));
  4736.  
  4737.    return sig;
  4738. }
  4739.  
  4740. ir_function_signature *
  4741. builtin_builder::_mid3(const glsl_type *type)
  4742. {
  4743.    ir_variable *x = in_var(type, "x");
  4744.    ir_variable *y = in_var(type, "y");
  4745.    ir_variable *z = in_var(type, "z");
  4746.    MAKE_SIG(type, shader_trinary_minmax, 3, x, y, z);
  4747.  
  4748.    ir_expression *mid3 = max2(min2(x, y), max2(min2(x, z), min2(y, z)));
  4749.    body.emit(ret(mid3));
  4750.  
  4751.    return sig;
  4752. }
  4753.  
  4754. ir_function_signature *
  4755. builtin_builder::_image_prototype(const glsl_type *image_type,
  4756.                                   const char *intrinsic_name,
  4757.                                   unsigned num_arguments,
  4758.                                   unsigned flags)
  4759. {
  4760.    const glsl_type *data_type = glsl_type::get_instance(
  4761.       image_type->sampler_type,
  4762.       (flags & IMAGE_FUNCTION_HAS_VECTOR_DATA_TYPE ? 4 : 1),
  4763.       1);
  4764.    const glsl_type *ret_type = (flags & IMAGE_FUNCTION_RETURNS_VOID ?
  4765.                                 glsl_type::void_type : data_type);
  4766.  
  4767.    /* Addressing arguments that are always present. */
  4768.    ir_variable *image = in_var(image_type, "image");
  4769.    ir_variable *coord = in_var(
  4770.       glsl_type::ivec(image_type->coordinate_components()), "coord");
  4771.  
  4772.    ir_function_signature *sig = new_sig(
  4773.       ret_type, shader_image_load_store, 2, image, coord);
  4774.  
  4775.    /* Sample index for multisample images. */
  4776.    if (image_type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS)
  4777.       sig->parameters.push_tail(in_var(glsl_type::int_type, "sample"));
  4778.  
  4779.    /* Data arguments. */
  4780.    for (unsigned i = 0; i < num_arguments; ++i) {
  4781.       char *arg_name = ralloc_asprintf(NULL, "arg%d", i);
  4782.       sig->parameters.push_tail(in_var(data_type, arg_name));
  4783.       ralloc_free(arg_name);
  4784.    }
  4785.  
  4786.    /* Set the maximal set of qualifiers allowed for this image
  4787.     * built-in.  Function calls with arguments having fewer
  4788.     * qualifiers than present in the prototype are allowed by the
  4789.     * spec, but not with more, i.e. this will make the compiler
  4790.     * accept everything that needs to be accepted, and reject cases
  4791.     * like loads from write-only or stores to read-only images.
  4792.     */
  4793.    image->data.image_read_only = (flags & IMAGE_FUNCTION_READ_ONLY) != 0;
  4794.    image->data.image_write_only = (flags & IMAGE_FUNCTION_WRITE_ONLY) != 0;
  4795.    image->data.image_coherent = true;
  4796.    image->data.image_volatile = true;
  4797.    image->data.image_restrict = true;
  4798.  
  4799.    return sig;
  4800. }
  4801.  
  4802. ir_function_signature *
  4803. builtin_builder::_image(const glsl_type *image_type,
  4804.                         const char *intrinsic_name,
  4805.                         unsigned num_arguments,
  4806.                         unsigned flags)
  4807. {
  4808.    ir_function_signature *sig = _image_prototype(image_type, intrinsic_name,
  4809.                                                  num_arguments, flags);
  4810.  
  4811.    if (flags & IMAGE_FUNCTION_EMIT_STUB) {
  4812.       ir_factory body(&sig->body, mem_ctx);
  4813.       ir_function *f = shader->symbols->get_function(intrinsic_name);
  4814.  
  4815.       if (flags & IMAGE_FUNCTION_RETURNS_VOID) {
  4816.          body.emit(call(f, NULL, sig->parameters));
  4817.       } else {
  4818.          ir_variable *ret_val =
  4819.             body.make_temp(sig->return_type, "_ret_val");
  4820.          body.emit(call(f, ret_val, sig->parameters));
  4821.          body.emit(ret(ret_val));
  4822.       }
  4823.  
  4824.       sig->is_defined = true;
  4825.  
  4826.    } else {
  4827.       sig->is_intrinsic = true;
  4828.    }
  4829.  
  4830.    return sig;
  4831. }
  4832.  
  4833. ir_function_signature *
  4834. builtin_builder::_memory_barrier_intrinsic(builtin_available_predicate avail)
  4835. {
  4836.    MAKE_INTRINSIC(glsl_type::void_type, avail, 0);
  4837.    return sig;
  4838. }
  4839.  
  4840. ir_function_signature *
  4841. builtin_builder::_memory_barrier(builtin_available_predicate avail)
  4842. {
  4843.    MAKE_SIG(glsl_type::void_type, avail, 0);
  4844.    body.emit(call(shader->symbols->get_function("__intrinsic_memory_barrier"),
  4845.                   NULL, sig->parameters));
  4846.    return sig;
  4847. }
  4848.  
  4849. /** @} */
  4850.  
  4851. /******************************************************************************/
  4852.  
  4853. /* The singleton instance of builtin_builder. */
  4854. static builtin_builder builtins;
  4855. static mtx_t builtins_lock = _MTX_INITIALIZER_NP;
  4856.  
  4857. /**
  4858.  * External API (exposing the built-in module to the rest of the compiler):
  4859.  *  @{
  4860.  */
  4861. void
  4862. _mesa_glsl_initialize_builtin_functions()
  4863. {
  4864.    mtx_lock(&builtins_lock);
  4865.    builtins.initialize();
  4866.    mtx_unlock(&builtins_lock);
  4867. }
  4868.  
  4869. void
  4870. _mesa_glsl_release_builtin_functions()
  4871. {
  4872.    mtx_lock(&builtins_lock);
  4873.    builtins.release();
  4874.    mtx_unlock(&builtins_lock);
  4875. }
  4876.  
  4877. ir_function_signature *
  4878. _mesa_glsl_find_builtin_function(_mesa_glsl_parse_state *state,
  4879.                                  const char *name, exec_list *actual_parameters)
  4880. {
  4881.    ir_function_signature * s;
  4882.    mtx_lock(&builtins_lock);
  4883.    s = builtins.find(state, name, actual_parameters);
  4884.    mtx_unlock(&builtins_lock);
  4885.    return s;
  4886. }
  4887.  
  4888. ir_function *
  4889. _mesa_glsl_find_builtin_function_by_name(_mesa_glsl_parse_state *state,
  4890.                                          const char *name)
  4891. {
  4892.    ir_function *f;
  4893.    mtx_lock(&builtins_lock);
  4894.    f = builtins.shader->symbols->get_function(name);
  4895.    mtx_unlock(&builtins_lock);
  4896.    return f;
  4897. }
  4898.  
  4899. gl_shader *
  4900. _mesa_glsl_get_builtin_function_shader()
  4901. {
  4902.    return builtins.shader;
  4903. }
  4904.  
  4905. /** @} */
  4906.