nir/builder: Move nir_atan and nir_atan2 from SPIR-V translator
Moves build_atan and build_atan2 into nir_builtin_builder. The goal is to be able to use this from the GLSL translator too. Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
This commit is contained in:
parent
075a96aa92
commit
2098ae16c8
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright © 2018 Red Hat Inc.
|
||||
* Copyright © 2015 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
|
@ -173,3 +174,154 @@ nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo)
|
|||
|
||||
return nir_vec(b, res, lo->num_components);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute xs[0] + xs[1] + xs[2] + ... using fadd.
|
||||
*/
|
||||
static nir_ssa_def *
|
||||
build_fsum(nir_builder *b, nir_ssa_def **xs, int terms)
|
||||
{
|
||||
nir_ssa_def *accum = xs[0];
|
||||
|
||||
for (int i = 1; i < terms; i++)
|
||||
accum = nir_fadd(b, accum, xs[i]);
|
||||
|
||||
return accum;
|
||||
}
|
||||
|
||||
nir_ssa_def *
|
||||
nir_atan(nir_builder *b, nir_ssa_def *y_over_x)
|
||||
{
|
||||
const uint32_t bit_size = y_over_x->bit_size;
|
||||
|
||||
nir_ssa_def *abs_y_over_x = nir_fabs(b, y_over_x);
|
||||
nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, bit_size);
|
||||
|
||||
/*
|
||||
* range-reduction, first step:
|
||||
*
|
||||
* / y_over_x if |y_over_x| <= 1.0;
|
||||
* x = <
|
||||
* \ 1.0 / y_over_x otherwise
|
||||
*/
|
||||
nir_ssa_def *x = nir_fdiv(b, nir_fmin(b, abs_y_over_x, one),
|
||||
nir_fmax(b, abs_y_over_x, one));
|
||||
|
||||
/*
|
||||
* approximate atan by evaluating polynomial:
|
||||
*
|
||||
* x * 0.9999793128310355 - x^3 * 0.3326756418091246 +
|
||||
* x^5 * 0.1938924977115610 - x^7 * 0.1173503194786851 +
|
||||
* x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
|
||||
*/
|
||||
nir_ssa_def *x_2 = nir_fmul(b, x, x);
|
||||
nir_ssa_def *x_3 = nir_fmul(b, x_2, x);
|
||||
nir_ssa_def *x_5 = nir_fmul(b, x_3, x_2);
|
||||
nir_ssa_def *x_7 = nir_fmul(b, x_5, x_2);
|
||||
nir_ssa_def *x_9 = nir_fmul(b, x_7, x_2);
|
||||
nir_ssa_def *x_11 = nir_fmul(b, x_9, x_2);
|
||||
|
||||
nir_ssa_def *polynomial_terms[] = {
|
||||
nir_fmul_imm(b, x, 0.9999793128310355f),
|
||||
nir_fmul_imm(b, x_3, -0.3326756418091246f),
|
||||
nir_fmul_imm(b, x_5, 0.1938924977115610f),
|
||||
nir_fmul_imm(b, x_7, -0.1173503194786851f),
|
||||
nir_fmul_imm(b, x_9, 0.0536813784310406f),
|
||||
nir_fmul_imm(b, x_11, -0.0121323213173444f),
|
||||
};
|
||||
|
||||
nir_ssa_def *tmp =
|
||||
build_fsum(b, polynomial_terms, ARRAY_SIZE(polynomial_terms));
|
||||
|
||||
/* range-reduction fixup */
|
||||
tmp = nir_fadd(b, tmp,
|
||||
nir_fmul(b, nir_b2f(b, nir_flt(b, one, abs_y_over_x), bit_size),
|
||||
nir_fadd_imm(b, nir_fmul_imm(b, tmp, -2.0f), M_PI_2)));
|
||||
|
||||
/* sign fixup */
|
||||
return nir_fmul(b, tmp, nir_fsign(b, y_over_x));
|
||||
}
|
||||
|
||||
nir_ssa_def *
|
||||
nir_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)
|
||||
{
|
||||
assert(y->bit_size == x->bit_size);
|
||||
const uint32_t bit_size = x->bit_size;
|
||||
|
||||
nir_ssa_def *zero = nir_imm_floatN_t(b, 0, bit_size);
|
||||
nir_ssa_def *one = nir_imm_floatN_t(b, 1, bit_size);
|
||||
|
||||
/* If we're on the left half-plane rotate the coordinates π/2 clock-wise
|
||||
* for the y=0 discontinuity to end up aligned with the vertical
|
||||
* discontinuity of atan(s/t) along t=0. This also makes sure that we
|
||||
* don't attempt to divide by zero along the vertical line, which may give
|
||||
* unspecified results on non-GLSL 4.1-capable hardware.
|
||||
*/
|
||||
nir_ssa_def *flip = nir_fge(b, zero, x);
|
||||
nir_ssa_def *s = nir_bcsel(b, flip, nir_fabs(b, x), y);
|
||||
nir_ssa_def *t = nir_bcsel(b, flip, y, nir_fabs(b, x));
|
||||
|
||||
/* If the magnitude of the denominator exceeds some huge value, scale down
|
||||
* the arguments in order to prevent the reciprocal operation from flushing
|
||||
* its result to zero, which would cause precision problems, and for s
|
||||
* infinite would cause us to return a NaN instead of the correct finite
|
||||
* value.
|
||||
*
|
||||
* If fmin and fmax are respectively the smallest and largest positive
|
||||
* normalized floating point values representable by the implementation,
|
||||
* the constants below should be in agreement with:
|
||||
*
|
||||
* huge <= 1 / fmin
|
||||
* scale <= 1 / fmin / fmax (for |t| >= huge)
|
||||
*
|
||||
* In addition scale should be a negative power of two in order to avoid
|
||||
* loss of precision. The values chosen below should work for most usual
|
||||
* floating point representations with at least the dynamic range of ATI's
|
||||
* 24-bit representation.
|
||||
*/
|
||||
const double huge_val = bit_size >= 32 ? 1e18 : 16384;
|
||||
nir_ssa_def *huge = nir_imm_floatN_t(b, huge_val, bit_size);
|
||||
nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),
|
||||
nir_imm_floatN_t(b, 0.25, bit_size), one);
|
||||
nir_ssa_def *rcp_scaled_t = nir_frcp(b, nir_fmul(b, t, scale));
|
||||
nir_ssa_def *s_over_t = nir_fmul(b, nir_fmul(b, s, scale), rcp_scaled_t);
|
||||
|
||||
/* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily
|
||||
* that ∞/∞ = 1) in order to comply with the rather artificial rules
|
||||
* inherited from IEEE 754-2008, namely:
|
||||
*
|
||||
* "atan2(±∞, −∞) is ±3π/4
|
||||
* atan2(±∞, +∞) is ±π/4"
|
||||
*
|
||||
* Note that this is inconsistent with the rules for the neighborhood of
|
||||
* zero that are based on iterated limits:
|
||||
*
|
||||
* "atan2(±0, −0) is ±π
|
||||
* atan2(±0, +0) is ±0"
|
||||
*
|
||||
* but GLSL specifically allows implementations to deviate from IEEE rules
|
||||
* at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as
|
||||
* well).
|
||||
*/
|
||||
nir_ssa_def *tan = nir_bcsel(b, nir_feq(b, nir_fabs(b, x), nir_fabs(b, y)),
|
||||
one, nir_fabs(b, s_over_t));
|
||||
|
||||
/* Calculate the arctangent and fix up the result if we had flipped the
|
||||
* coordinate system.
|
||||
*/
|
||||
nir_ssa_def *arc =
|
||||
nir_fadd(b, nir_fmul_imm(b, nir_b2f(b, flip, bit_size), M_PI_2),
|
||||
nir_atan(b, tan));
|
||||
|
||||
/* Rather convoluted calculation of the sign of the result. When x < 0 we
|
||||
* cannot use fsign because we need to be able to distinguish between
|
||||
* negative and positive zero. We don't use bitwise arithmetic tricks for
|
||||
* consistency with the GLSL front-end. When x >= 0 rcp_scaled_t will
|
||||
* always be non-negative so this won't be able to distinguish between
|
||||
* negative and positive zero, but we don't care because atan2 is
|
||||
* continuous along the whole positive y = 0 half-line, so it won't affect
|
||||
* the result significantly.
|
||||
*/
|
||||
return nir_bcsel(b, nir_flt(b, nir_fmin(b, y, rcp_scaled_t), zero),
|
||||
nir_fneg(b, arc), arc);
|
||||
}
|
||||
|
|
|
@ -41,6 +41,8 @@ nir_ssa_def* nir_rotate(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
|
|||
nir_ssa_def* nir_smoothstep(nir_builder *b, nir_ssa_def *edge0,
|
||||
nir_ssa_def *edge1, nir_ssa_def *x);
|
||||
nir_ssa_def* nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo);
|
||||
nir_ssa_def* nir_atan(nir_builder *b, nir_ssa_def *y_over_x);
|
||||
nir_ssa_def* nir_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x);
|
||||
|
||||
static inline nir_ssa_def *
|
||||
nir_nan_check2(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *res)
|
||||
|
|
|
@ -234,157 +234,6 @@ build_asin(nir_builder *b, nir_ssa_def *x, float p0, float p1)
|
|||
expr_tail)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute xs[0] + xs[1] + xs[2] + ... using fadd.
|
||||
*/
|
||||
static nir_ssa_def *
|
||||
build_fsum(nir_builder *b, nir_ssa_def **xs, int terms)
|
||||
{
|
||||
nir_ssa_def *accum = xs[0];
|
||||
|
||||
for (int i = 1; i < terms; i++)
|
||||
accum = nir_fadd(b, accum, xs[i]);
|
||||
|
||||
return accum;
|
||||
}
|
||||
|
||||
static nir_ssa_def *
|
||||
build_atan(nir_builder *b, nir_ssa_def *y_over_x)
|
||||
{
|
||||
const uint32_t bit_size = y_over_x->bit_size;
|
||||
|
||||
nir_ssa_def *abs_y_over_x = nir_fabs(b, y_over_x);
|
||||
nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, bit_size);
|
||||
|
||||
/*
|
||||
* range-reduction, first step:
|
||||
*
|
||||
* / y_over_x if |y_over_x| <= 1.0;
|
||||
* x = <
|
||||
* \ 1.0 / y_over_x otherwise
|
||||
*/
|
||||
nir_ssa_def *x = nir_fdiv(b, nir_fmin(b, abs_y_over_x, one),
|
||||
nir_fmax(b, abs_y_over_x, one));
|
||||
|
||||
/*
|
||||
* approximate atan by evaluating polynomial:
|
||||
*
|
||||
* x * 0.9999793128310355 - x^3 * 0.3326756418091246 +
|
||||
* x^5 * 0.1938924977115610 - x^7 * 0.1173503194786851 +
|
||||
* x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
|
||||
*/
|
||||
nir_ssa_def *x_2 = nir_fmul(b, x, x);
|
||||
nir_ssa_def *x_3 = nir_fmul(b, x_2, x);
|
||||
nir_ssa_def *x_5 = nir_fmul(b, x_3, x_2);
|
||||
nir_ssa_def *x_7 = nir_fmul(b, x_5, x_2);
|
||||
nir_ssa_def *x_9 = nir_fmul(b, x_7, x_2);
|
||||
nir_ssa_def *x_11 = nir_fmul(b, x_9, x_2);
|
||||
|
||||
nir_ssa_def *polynomial_terms[] = {
|
||||
nir_fmul_imm(b, x, 0.9999793128310355f),
|
||||
nir_fmul_imm(b, x_3, -0.3326756418091246f),
|
||||
nir_fmul_imm(b, x_5, 0.1938924977115610f),
|
||||
nir_fmul_imm(b, x_7, -0.1173503194786851f),
|
||||
nir_fmul_imm(b, x_9, 0.0536813784310406f),
|
||||
nir_fmul_imm(b, x_11, -0.0121323213173444f),
|
||||
};
|
||||
|
||||
nir_ssa_def *tmp =
|
||||
build_fsum(b, polynomial_terms, ARRAY_SIZE(polynomial_terms));
|
||||
|
||||
/* range-reduction fixup */
|
||||
tmp = nir_fadd(b, tmp,
|
||||
nir_fmul(b, nir_b2f(b, nir_flt(b, one, abs_y_over_x), bit_size),
|
||||
nir_fadd_imm(b, nir_fmul_imm(b, tmp, -2.0f), M_PI_2f)));
|
||||
|
||||
/* sign fixup */
|
||||
return nir_fmul(b, tmp, nir_fsign(b, y_over_x));
|
||||
}
|
||||
|
||||
static nir_ssa_def *
|
||||
build_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)
|
||||
{
|
||||
assert(y->bit_size == x->bit_size);
|
||||
const uint32_t bit_size = x->bit_size;
|
||||
|
||||
nir_ssa_def *zero = nir_imm_floatN_t(b, 0, bit_size);
|
||||
nir_ssa_def *one = nir_imm_floatN_t(b, 1, bit_size);
|
||||
|
||||
/* If we're on the left half-plane rotate the coordinates π/2 clock-wise
|
||||
* for the y=0 discontinuity to end up aligned with the vertical
|
||||
* discontinuity of atan(s/t) along t=0. This also makes sure that we
|
||||
* don't attempt to divide by zero along the vertical line, which may give
|
||||
* unspecified results on non-GLSL 4.1-capable hardware.
|
||||
*/
|
||||
nir_ssa_def *flip = nir_fge(b, zero, x);
|
||||
nir_ssa_def *s = nir_bcsel(b, flip, nir_fabs(b, x), y);
|
||||
nir_ssa_def *t = nir_bcsel(b, flip, y, nir_fabs(b, x));
|
||||
|
||||
/* If the magnitude of the denominator exceeds some huge value, scale down
|
||||
* the arguments in order to prevent the reciprocal operation from flushing
|
||||
* its result to zero, which would cause precision problems, and for s
|
||||
* infinite would cause us to return a NaN instead of the correct finite
|
||||
* value.
|
||||
*
|
||||
* If fmin and fmax are respectively the smallest and largest positive
|
||||
* normalized floating point values representable by the implementation,
|
||||
* the constants below should be in agreement with:
|
||||
*
|
||||
* huge <= 1 / fmin
|
||||
* scale <= 1 / fmin / fmax (for |t| >= huge)
|
||||
*
|
||||
* In addition scale should be a negative power of two in order to avoid
|
||||
* loss of precision. The values chosen below should work for most usual
|
||||
* floating point representations with at least the dynamic range of ATI's
|
||||
* 24-bit representation.
|
||||
*/
|
||||
const double huge_val = bit_size >= 32 ? 1e18 : 16384;
|
||||
nir_ssa_def *huge = nir_imm_floatN_t(b, huge_val, bit_size);
|
||||
nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),
|
||||
nir_imm_floatN_t(b, 0.25, bit_size), one);
|
||||
nir_ssa_def *rcp_scaled_t = nir_frcp(b, nir_fmul(b, t, scale));
|
||||
nir_ssa_def *s_over_t = nir_fmul(b, nir_fmul(b, s, scale), rcp_scaled_t);
|
||||
|
||||
/* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily
|
||||
* that ∞/∞ = 1) in order to comply with the rather artificial rules
|
||||
* inherited from IEEE 754-2008, namely:
|
||||
*
|
||||
* "atan2(±∞, −∞) is ±3π/4
|
||||
* atan2(±∞, +∞) is ±π/4"
|
||||
*
|
||||
* Note that this is inconsistent with the rules for the neighborhood of
|
||||
* zero that are based on iterated limits:
|
||||
*
|
||||
* "atan2(±0, −0) is ±π
|
||||
* atan2(±0, +0) is ±0"
|
||||
*
|
||||
* but GLSL specifically allows implementations to deviate from IEEE rules
|
||||
* at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as
|
||||
* well).
|
||||
*/
|
||||
nir_ssa_def *tan = nir_bcsel(b, nir_feq(b, nir_fabs(b, x), nir_fabs(b, y)),
|
||||
one, nir_fabs(b, s_over_t));
|
||||
|
||||
/* Calculate the arctangent and fix up the result if we had flipped the
|
||||
* coordinate system.
|
||||
*/
|
||||
nir_ssa_def *arc =
|
||||
nir_fadd(b, nir_fmul_imm(b, nir_b2f(b, flip, bit_size), M_PI_2f),
|
||||
build_atan(b, tan));
|
||||
|
||||
/* Rather convoluted calculation of the sign of the result. When x < 0 we
|
||||
* cannot use fsign because we need to be able to distinguish between
|
||||
* negative and positive zero. We don't use bitwise arithmetic tricks for
|
||||
* consistency with the GLSL front-end. When x >= 0 rcp_scaled_t will
|
||||
* always be non-negative so this won't be able to distinguish between
|
||||
* negative and positive zero, but we don't care because atan2 is
|
||||
* continuous along the whole positive y = 0 half-line, so it won't affect
|
||||
* the result significantly.
|
||||
*/
|
||||
return nir_bcsel(b, nir_flt(b, nir_fmin(b, y, rcp_scaled_t), zero),
|
||||
nir_fneg(b, arc), arc);
|
||||
}
|
||||
|
||||
static nir_op
|
||||
vtn_nir_alu_op_for_spirv_glsl_opcode(struct vtn_builder *b,
|
||||
enum GLSLstd450 opcode,
|
||||
|
@ -662,11 +511,11 @@ handle_glsl450_alu(struct vtn_builder *b, enum GLSLstd450 entrypoint,
|
|||
return;
|
||||
|
||||
case GLSLstd450Atan:
|
||||
val->ssa->def = build_atan(nb, src[0]);
|
||||
val->ssa->def = nir_atan(nb, src[0]);
|
||||
return;
|
||||
|
||||
case GLSLstd450Atan2:
|
||||
val->ssa->def = build_atan2(nb, src[0], src[1]);
|
||||
val->ssa->def = nir_atan2(nb, src[0], src[1]);
|
||||
return;
|
||||
|
||||
case GLSLstd450Frexp: {
|
||||
|
|
Loading…
Reference in New Issue