static nir_ssa_def *
build_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)
{
- nir_ssa_def *zero = nir_imm_float(b, 0.0f);
+ nir_ssa_def *zero = nir_imm_float(b, 0);
+ nir_ssa_def *one = nir_imm_float(b, 1);
+
+ /* 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.
+ */
+ nir_ssa_def *huge = nir_imm_float(b, 1e18f);
+ nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),
+ nir_imm_float(b, 0.25), 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));
- /* If |x| >= 1.0e-8 * |y|: */
- nir_ssa_def *condition =
- nir_fge(b, nir_fabs(b, x),
- nir_fmul(b, nir_imm_float(b, 1.0e-8f), nir_fabs(b, y)));
-
- /* Then...call atan(y/x) and fix it up: */
- nir_ssa_def *atan1 = build_atan(b, nir_fdiv(b, y, x));
- nir_ssa_def *r_then =
- nir_bcsel(b, nir_flt(b, x, zero),
- nir_fadd(b, atan1,
- nir_bcsel(b, nir_fge(b, y, zero),
- nir_imm_float(b, M_PIf),
- nir_imm_float(b, -M_PIf))),
- atan1);
-
- /* Else... */
- nir_ssa_def *r_else =
- nir_fmul(b, nir_fsign(b, y), nir_imm_float(b, M_PI_2f));
-
- return nir_bcsel(b, condition, r_then, r_else);
+ /* Calculate the arctangent and fix up the result if we had flipped the
+ * coordinate system.
+ */
+ nir_ssa_def *arc = nir_fadd(b, nir_fmul(b, nir_b2f(b, flip),
+ nir_imm_float(b, 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_ssa_def *
case GLSLstd450Log2: return nir_op_flog2;
case GLSLstd450Sqrt: return nir_op_fsqrt;
case GLSLstd450InverseSqrt: return nir_op_frsq;
+ case GLSLstd450NMin: return nir_op_fmin;
case GLSLstd450FMin: return nir_op_fmin;
case GLSLstd450UMin: return nir_op_umin;
case GLSLstd450SMin: return nir_op_imin;
+ case GLSLstd450NMax: return nir_op_fmax;
case GLSLstd450FMax: return nir_op_fmax;
case GLSLstd450UMax: return nir_op_umax;
case GLSLstd450SMax: return nir_op_imax;
case GLSLstd450PackSnorm2x16: return nir_op_pack_snorm_2x16;
case GLSLstd450PackUnorm2x16: return nir_op_pack_unorm_2x16;
case GLSLstd450PackHalf2x16: return nir_op_pack_half_2x16;
+ case GLSLstd450PackDouble2x32: return nir_op_pack_64_2x32;
case GLSLstd450UnpackSnorm4x8: return nir_op_unpack_snorm_4x8;
case GLSLstd450UnpackUnorm4x8: return nir_op_unpack_unorm_4x8;
case GLSLstd450UnpackSnorm2x16: return nir_op_unpack_snorm_2x16;
case GLSLstd450UnpackUnorm2x16: return nir_op_unpack_unorm_2x16;
case GLSLstd450UnpackHalf2x16: return nir_op_unpack_half_2x16;
+ case GLSLstd450UnpackDouble2x32: return nir_op_unpack_64_2x32;
default:
unreachable("No NIR equivalent");
}
}
+#define NIR_IMM_FP(n, v) (src[0]->bit_size == 64 ? nir_imm_double(n, v) : nir_imm_float(n, v))
+
static void
handle_glsl450_alu(struct vtn_builder *b, enum GLSLstd450 entrypoint,
const uint32_t *w, unsigned count)
/* Collect the various SSA sources */
unsigned num_inputs = count - 5;
nir_ssa_def *src[3] = { NULL, };
- for (unsigned i = 0; i < num_inputs; i++)
+ for (unsigned i = 0; i < num_inputs; i++) {
+ /* These are handled specially below */
+ if (vtn_untyped_value(b, w[i + 5])->value_type == vtn_value_type_pointer)
+ continue;
+
src[i] = vtn_ssa_value(b, w[i + 5])->def;
+ }
switch (entrypoint) {
case GLSLstd450Radians:
return;
case GLSLstd450FClamp:
+ case GLSLstd450NClamp:
val->ssa->def = build_fclamp(nb, src[0], src[1], src[2]);
return;
case GLSLstd450UClamp:
nir_ssa_def *t =
build_fclamp(nb, nir_fdiv(nb, nir_fsub(nb, src[2], src[0]),
nir_fsub(nb, src[1], src[0])),
- nir_imm_float(nb, 0.0), nir_imm_float(nb, 1.0));
+ NIR_IMM_FP(nb, 0.0), NIR_IMM_FP(nb, 1.0));
/* result = t * t * (3 - 2 * t) */
val->ssa->def =
nir_fmul(nb, t, nir_fmul(nb, t,
- nir_fsub(nb, nir_imm_float(nb, 3.0),
- nir_fmul(nb, nir_imm_float(nb, 2.0), t))));
+ nir_fsub(nb, NIR_IMM_FP(nb, 3.0),
+ nir_fmul(nb, NIR_IMM_FP(nb, 2.0), t))));
return;
}
build_exp(nb, nir_fneg(nb, src[0]))));
return;
- case GLSLstd450Tanh:
- /* (0.5 * (e^x - e^(-x))) / (0.5 * (e^x + e^(-x))) */
- val->ssa->def =
- nir_fdiv(nb, nir_fmul(nb, nir_imm_float(nb, 0.5f),
- nir_fsub(nb, build_exp(nb, src[0]),
- build_exp(nb, nir_fneg(nb, src[0])))),
- nir_fmul(nb, nir_imm_float(nb, 0.5f),
- nir_fadd(nb, build_exp(nb, src[0]),
- build_exp(nb, nir_fneg(nb, src[0])))));
+ case GLSLstd450Tanh: {
+ /* tanh(x) := (0.5 * (e^x - e^(-x))) / (0.5 * (e^x + e^(-x)))
+ *
+ * With a little algebra this reduces to (e^2x - 1) / (e^2x + 1)
+ *
+ * We clamp x to (-inf, +10] to avoid precision problems. When x > 10,
+ * e^2x is so much larger than 1.0 that 1.0 gets flushed to zero in the
+ * computation e^2x +/- 1 so it can be ignored.
+ */
+ nir_ssa_def *x = nir_fmin(nb, src[0], nir_imm_float(nb, 10));
+ nir_ssa_def *exp2x = build_exp(nb, nir_fmul(nb, x, nir_imm_float(nb, 2)));
+ val->ssa->def = nir_fdiv(nb, nir_fsub(nb, exp2x, nir_imm_float(nb, 1)),
+ nir_fadd(nb, exp2x, nir_imm_float(nb, 1)));
return;
+ }
case GLSLstd450Asinh:
val->ssa->def = nir_fmul(nb, nir_fsign(nb, src[0]),
}
}
+static void
+handle_glsl450_interpolation(struct vtn_builder *b, enum GLSLstd450 opcode,
+ const uint32_t *w, unsigned count)
+{
+ const struct glsl_type *dest_type =
+ vtn_value(b, w[1], vtn_value_type_type)->type->type;
+
+ struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
+ val->ssa = vtn_create_ssa_value(b, dest_type);
+
+ nir_intrinsic_op op;
+ switch (opcode) {
+ case GLSLstd450InterpolateAtCentroid:
+ op = nir_intrinsic_interp_var_at_centroid;
+ break;
+ case GLSLstd450InterpolateAtSample:
+ op = nir_intrinsic_interp_var_at_sample;
+ break;
+ case GLSLstd450InterpolateAtOffset:
+ op = nir_intrinsic_interp_var_at_offset;
+ break;
+ default:
+ unreachable("Invalid opcode");
+ }
+
+ nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->nb.shader, op);
+
+ nir_deref_var *deref = vtn_nir_deref(b, w[5]);
+ intrin->variables[0] = nir_deref_var_clone(deref, intrin);
+
+ switch (opcode) {
+ case GLSLstd450InterpolateAtCentroid:
+ break;
+ case GLSLstd450InterpolateAtSample:
+ case GLSLstd450InterpolateAtOffset:
+ intrin->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
+ break;
+ default:
+ unreachable("Invalid opcode");
+ }
+
+ intrin->num_components = glsl_get_vector_elements(dest_type);
+ nir_ssa_dest_init(&intrin->instr, &intrin->dest,
+ glsl_get_vector_elements(dest_type),
+ glsl_get_bit_size(dest_type), NULL);
+ val->ssa->def = &intrin->dest.ssa;
+
+ nir_builder_instr_insert(&b->nb, &intrin->instr);
+}
+
bool
vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
const uint32_t *w, unsigned count)
case GLSLstd450InterpolateAtCentroid:
case GLSLstd450InterpolateAtSample:
case GLSLstd450InterpolateAtOffset:
- unreachable("Unhandled opcode");
+ handle_glsl450_interpolation(b, ext_opcode, w, count);
+ break;
default:
handle_glsl450_alu(b, (enum GLSLstd450)ext_opcode, w, count);