OSDN Git Service

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