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