2
* Copyright © 2018 Red Hat Inc.
3
* Copyright © 2015 Intel Corporation
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28
#include "nir_builtin_builder.h"
31
nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
33
unsigned yzx[3] = { 1, 2, 0 };
34
unsigned zxy[3] = { 2, 0, 1 };
36
return nir_ffma(b, nir_swizzle(b, x, yzx, 3),
37
nir_swizzle(b, y, zxy, 3),
38
nir_fneg(b, nir_fmul(b, nir_swizzle(b, x, zxy, 3),
39
nir_swizzle(b, y, yzx, 3))));
43
nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
45
nir_ssa_def *cross = nir_cross3(b, x, y);
48
nir_channel(b, cross, 0),
49
nir_channel(b, cross, 1),
50
nir_channel(b, cross, 2),
51
nir_imm_intN_t(b, 0, cross->bit_size));
55
nir_fast_length(nir_builder *b, nir_ssa_def *vec)
57
return nir_fsqrt(b, nir_fdot(b, vec, vec));
61
nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
63
nir_ssa_def *zero = nir_imm_intN_t(b, 0, x->bit_size);
64
nir_ssa_def *one = nir_imm_intN_t(b, 1, x->bit_size);
66
nir_ssa_def *condeq = nir_feq(b, x, y);
67
nir_ssa_def *conddir = nir_flt(b, x, y);
68
nir_ssa_def *condzero = nir_feq(b, x, zero);
70
uint64_t sign_mask = 1ull << (x->bit_size - 1);
73
if (nir_is_denorm_flush_to_zero(b->shader->info.float_controls_execution_mode, x->bit_size)) {
74
switch (x->bit_size) {
86
/* Flush denorm to zero to avoid returning a denorm when condeq is true. */
87
x = nir_fmul(b, x, nir_imm_floatN_t(b, 1.0, x->bit_size));
90
/* beware of: +/-0.0 - 1 == NaN */
94
nir_imm_intN_t(b, sign_mask | min_abs, x->bit_size),
97
/* beware of -0.0 + 1 == -0x1p-149 */
98
nir_ssa_def *xp = nir_bcsel(b, condzero,
99
nir_imm_intN_t(b, min_abs, x->bit_size),
100
nir_iadd(b, x, one));
102
/* nextafter can be implemented by just +/- 1 on the int value */
104
nir_bcsel(b, nir_ixor(b, conddir, nir_flt(b, x, zero)), xp, xn);
106
return nir_nan_check2(b, x, y, nir_bcsel(b, condeq, x, res));
110
nir_normalize(nir_builder *b, nir_ssa_def *vec)
112
if (vec->num_components == 1)
113
return nir_fsign(b, vec);
115
nir_ssa_def *f0 = nir_imm_floatN_t(b, 0.0, vec->bit_size);
116
nir_ssa_def *f1 = nir_imm_floatN_t(b, 1.0, vec->bit_size);
117
nir_ssa_def *finf = nir_imm_floatN_t(b, INFINITY, vec->bit_size);
119
/* scale the input to increase precision */
120
nir_ssa_def *maxc = nir_fmax_abs_vec_comp(b, vec);
121
nir_ssa_def *svec = nir_fdiv(b, vec, maxc);
123
nir_ssa_def *finfvec = nir_copysign(b, nir_bcsel(b, nir_feq(b, vec, finf), f1, f0), f1);
125
nir_ssa_def *temp = nir_bcsel(b, nir_feq(b, maxc, finf), finfvec, svec);
126
nir_ssa_def *res = nir_fmul(b, temp, nir_frsq(b, nir_fdot(b, temp, temp)));
128
return nir_bcsel(b, nir_feq(b, maxc, f0), vec, res);
132
nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_def *x)
134
nir_ssa_def *f2 = nir_imm_floatN_t(b, 2.0, x->bit_size);
135
nir_ssa_def *f3 = nir_imm_floatN_t(b, 3.0, x->bit_size);
137
/* t = clamp((x - edge0) / (edge1 - edge0), 0, 1) */
139
nir_fsat(b, nir_fdiv(b, nir_fsub(b, x, edge0),
140
nir_fsub(b, edge1, edge0)));
142
/* result = t * t * (3 - 2 * t) */
143
return nir_fmul(b, t, nir_fmul(b, t, nir_a_minus_bc(b, f3, f2, t)));
147
nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo)
149
assert(lo->num_components == hi->num_components);
150
assert(lo->bit_size == hi->bit_size);
152
nir_ssa_def *res[NIR_MAX_VEC_COMPONENTS];
153
for (unsigned i = 0; i < lo->num_components; ++i) {
154
nir_ssa_def *vec = nir_vec2(b, nir_channel(b, lo, i), nir_channel(b, hi, i));
155
res[i] = nir_pack_bits(b, vec, vec->bit_size * 2);
158
return nir_vec(b, res, lo->num_components);
162
* Compute xs[0] + xs[1] + xs[2] + ... using fadd.
165
build_fsum(nir_builder *b, nir_ssa_def **xs, int terms)
167
nir_ssa_def *accum = xs[0];
169
for (int i = 1; i < terms; i++)
170
accum = nir_fadd(b, accum, xs[i]);
176
nir_atan(nir_builder *b, nir_ssa_def *y_over_x)
178
const uint32_t bit_size = y_over_x->bit_size;
180
nir_ssa_def *abs_y_over_x = nir_fabs(b, y_over_x);
181
nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, bit_size);
184
* range-reduction, first step:
186
* / y_over_x if |y_over_x| <= 1.0;
188
* \ 1.0 / y_over_x otherwise
190
nir_ssa_def *x = nir_fdiv(b, nir_fmin(b, abs_y_over_x, one),
191
nir_fmax(b, abs_y_over_x, one));
194
* approximate atan by evaluating polynomial:
196
* x * 0.9999793128310355 - x^3 * 0.3326756418091246 +
197
* x^5 * 0.1938924977115610 - x^7 * 0.1173503194786851 +
198
* x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
200
nir_ssa_def *x_2 = nir_fmul(b, x, x);
201
nir_ssa_def *x_3 = nir_fmul(b, x_2, x);
202
nir_ssa_def *x_5 = nir_fmul(b, x_3, x_2);
203
nir_ssa_def *x_7 = nir_fmul(b, x_5, x_2);
204
nir_ssa_def *x_9 = nir_fmul(b, x_7, x_2);
205
nir_ssa_def *x_11 = nir_fmul(b, x_9, x_2);
207
nir_ssa_def *polynomial_terms[] = {
208
nir_fmul_imm(b, x, 0.9999793128310355f),
209
nir_fmul_imm(b, x_3, -0.3326756418091246f),
210
nir_fmul_imm(b, x_5, 0.1938924977115610f),
211
nir_fmul_imm(b, x_7, -0.1173503194786851f),
212
nir_fmul_imm(b, x_9, 0.0536813784310406f),
213
nir_fmul_imm(b, x_11, -0.0121323213173444f),
217
build_fsum(b, polynomial_terms, ARRAY_SIZE(polynomial_terms));
219
/* range-reduction fixup */
221
nir_b2f(b, nir_flt(b, one, abs_y_over_x), bit_size),
222
nir_ffma_imm12(b, tmp, -2.0f, M_PI_2),
226
nir_ssa_def *result = nir_fmul(b, tmp, nir_fsign(b, y_over_x));
228
/* The fmin and fmax above will filter out NaN values. This leads to
229
* non-NaN results for NaN inputs. Work around this by doing
231
* !isnan(y_over_x) ? ... : y_over_x;
234
nir_is_float_control_signed_zero_inf_nan_preserve(b->shader->info.float_controls_execution_mode, bit_size)) {
235
const bool exact = b->exact;
238
nir_ssa_def *is_not_nan = nir_feq(b, y_over_x, y_over_x);
241
/* The extra 1.0*y_over_x ensures that subnormal results are flushed to
244
result = nir_bcsel(b, is_not_nan, result, nir_fmul_imm(b, y_over_x, 1.0));
251
nir_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)
253
assert(y->bit_size == x->bit_size);
254
const uint32_t bit_size = x->bit_size;
256
nir_ssa_def *zero = nir_imm_floatN_t(b, 0, bit_size);
257
nir_ssa_def *one = nir_imm_floatN_t(b, 1, bit_size);
259
/* If we're on the left half-plane rotate the coordinates π/2 clock-wise
260
* for the y=0 discontinuity to end up aligned with the vertical
261
* discontinuity of atan(s/t) along t=0. This also makes sure that we
262
* don't attempt to divide by zero along the vertical line, which may give
263
* unspecified results on non-GLSL 4.1-capable hardware.
265
nir_ssa_def *flip = nir_fge(b, zero, x);
266
nir_ssa_def *s = nir_bcsel(b, flip, nir_fabs(b, x), y);
267
nir_ssa_def *t = nir_bcsel(b, flip, y, nir_fabs(b, x));
269
/* If the magnitude of the denominator exceeds some huge value, scale down
270
* the arguments in order to prevent the reciprocal operation from flushing
271
* its result to zero, which would cause precision problems, and for s
272
* infinite would cause us to return a NaN instead of the correct finite
275
* If fmin and fmax are respectively the smallest and largest positive
276
* normalized floating point values representable by the implementation,
277
* the constants below should be in agreement with:
280
* scale <= 1 / fmin / fmax (for |t| >= huge)
282
* In addition scale should be a negative power of two in order to avoid
283
* loss of precision. The values chosen below should work for most usual
284
* floating point representations with at least the dynamic range of ATI's
285
* 24-bit representation.
287
const double huge_val = bit_size >= 32 ? 1e18 : 16384;
288
nir_ssa_def *huge = nir_imm_floatN_t(b, huge_val, bit_size);
289
nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),
290
nir_imm_floatN_t(b, 0.25, bit_size), one);
291
nir_ssa_def *rcp_scaled_t = nir_frcp(b, nir_fmul(b, t, scale));
292
nir_ssa_def *s_over_t = nir_fmul(b, nir_fmul(b, s, scale), rcp_scaled_t);
294
/* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily
295
* that ∞/∞ = 1) in order to comply with the rather artificial rules
296
* inherited from IEEE 754-2008, namely:
298
* "atan2(±∞, −∞) is ±3π/4
299
* atan2(±∞, +∞) is ±π/4"
301
* Note that this is inconsistent with the rules for the neighborhood of
302
* zero that are based on iterated limits:
304
* "atan2(±0, −0) is ±π
305
* atan2(±0, +0) is ±0"
307
* but GLSL specifically allows implementations to deviate from IEEE rules
308
* at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as
311
nir_ssa_def *tan = nir_bcsel(b, nir_feq(b, nir_fabs(b, x), nir_fabs(b, y)),
312
one, nir_fabs(b, s_over_t));
314
/* Calculate the arctangent and fix up the result if we had flipped the
318
nir_ffma_imm1(b, nir_b2f(b, flip, bit_size), M_PI_2, nir_atan(b, tan));
320
/* Rather convoluted calculation of the sign of the result. When x < 0 we
321
* cannot use fsign because we need to be able to distinguish between
322
* negative and positive zero. We don't use bitwise arithmetic tricks for
323
* consistency with the GLSL front-end. When x >= 0 rcp_scaled_t will
324
* always be non-negative so this won't be able to distinguish between
325
* negative and positive zero, but we don't care because atan2 is
326
* continuous along the whole positive y = 0 half-line, so it won't affect
327
* the result significantly.
329
return nir_bcsel(b, nir_flt(b, nir_fmin(b, y, rcp_scaled_t), zero),
330
nir_fneg(b, arc), arc);
334
nir_get_texture_size(nir_builder *b, nir_tex_instr *tex)
336
b->cursor = nir_before_instr(&tex->instr);
340
unsigned num_srcs = 1; /* One for the LOD */
341
for (unsigned i = 0; i < tex->num_srcs; i++) {
342
if (tex->src[i].src_type == nir_tex_src_texture_deref ||
343
tex->src[i].src_type == nir_tex_src_sampler_deref ||
344
tex->src[i].src_type == nir_tex_src_texture_offset ||
345
tex->src[i].src_type == nir_tex_src_sampler_offset ||
346
tex->src[i].src_type == nir_tex_src_texture_handle ||
347
tex->src[i].src_type == nir_tex_src_sampler_handle)
351
txs = nir_tex_instr_create(b->shader, num_srcs);
352
txs->op = nir_texop_txs;
353
txs->sampler_dim = tex->sampler_dim;
354
txs->is_array = tex->is_array;
355
txs->is_shadow = tex->is_shadow;
356
txs->is_new_style_shadow = tex->is_new_style_shadow;
357
txs->texture_index = tex->texture_index;
358
txs->sampler_index = tex->sampler_index;
359
txs->dest_type = nir_type_int32;
362
for (unsigned i = 0; i < tex->num_srcs; i++) {
363
if (tex->src[i].src_type == nir_tex_src_texture_deref ||
364
tex->src[i].src_type == nir_tex_src_sampler_deref ||
365
tex->src[i].src_type == nir_tex_src_texture_offset ||
366
tex->src[i].src_type == nir_tex_src_sampler_offset ||
367
tex->src[i].src_type == nir_tex_src_texture_handle ||
368
tex->src[i].src_type == nir_tex_src_sampler_handle) {
369
nir_src_copy(&txs->src[idx].src, &tex->src[i].src);
370
txs->src[idx].src_type = tex->src[i].src_type;
374
/* Add in an LOD because some back-ends require it */
375
txs->src[idx].src = nir_src_for_ssa(nir_imm_int(b, 0));
376
txs->src[idx].src_type = nir_tex_src_lod;
378
nir_ssa_dest_init(&txs->instr, &txs->dest,
379
nir_tex_instr_dest_size(txs), 32, NULL);
380
nir_builder_instr_insert(b, &txs->instr);
382
return &txs->dest.ssa;
386
nir_get_texture_lod(nir_builder *b, nir_tex_instr *tex)
388
b->cursor = nir_before_instr(&tex->instr);
392
unsigned num_srcs = 0;
393
for (unsigned i = 0; i < tex->num_srcs; i++) {
394
if (tex->src[i].src_type == nir_tex_src_coord ||
395
tex->src[i].src_type == nir_tex_src_texture_deref ||
396
tex->src[i].src_type == nir_tex_src_sampler_deref ||
397
tex->src[i].src_type == nir_tex_src_texture_offset ||
398
tex->src[i].src_type == nir_tex_src_sampler_offset ||
399
tex->src[i].src_type == nir_tex_src_texture_handle ||
400
tex->src[i].src_type == nir_tex_src_sampler_handle)
404
tql = nir_tex_instr_create(b->shader, num_srcs);
405
tql->op = nir_texop_lod;
406
tql->coord_components = tex->coord_components;
407
tql->sampler_dim = tex->sampler_dim;
408
tql->is_array = tex->is_array;
409
tql->is_shadow = tex->is_shadow;
410
tql->is_new_style_shadow = tex->is_new_style_shadow;
411
tql->texture_index = tex->texture_index;
412
tql->sampler_index = tex->sampler_index;
413
tql->dest_type = nir_type_float32;
416
for (unsigned i = 0; i < tex->num_srcs; i++) {
417
if (tex->src[i].src_type == nir_tex_src_coord ||
418
tex->src[i].src_type == nir_tex_src_texture_deref ||
419
tex->src[i].src_type == nir_tex_src_sampler_deref ||
420
tex->src[i].src_type == nir_tex_src_texture_offset ||
421
tex->src[i].src_type == nir_tex_src_sampler_offset ||
422
tex->src[i].src_type == nir_tex_src_texture_handle ||
423
tex->src[i].src_type == nir_tex_src_sampler_handle) {
424
nir_src_copy(&tql->src[idx].src, &tex->src[i].src);
425
tql->src[idx].src_type = tex->src[i].src_type;
430
nir_ssa_dest_init(&tql->instr, &tql->dest, 2, 32, NULL);
431
nir_builder_instr_insert(b, &tql->instr);
433
/* The LOD is the y component of the result */
434
return nir_channel(b, &tql->dest.ssa, 1);