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