2 * Copyright © 2018 Red Hat Inc.
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:
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
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 DEALINGS
25 #include "nir_builtin_builder.h"
28 nir_cross(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
30 unsigned yzx
[3] = { 1, 2, 0 };
31 unsigned zxy
[3] = { 2, 0, 1 };
33 return nir_fsub(b
, nir_fmul(b
, nir_swizzle(b
, x
, yzx
, 3, true),
34 nir_swizzle(b
, y
, zxy
, 3, true)),
35 nir_fmul(b
, nir_swizzle(b
, x
, zxy
, 3, true),
36 nir_swizzle(b
, y
, yzx
, 3, true)));
40 nir_fast_length(nir_builder
*b
, nir_ssa_def
*vec
)
42 switch (vec
->num_components
) {
43 case 1: return nir_fsqrt(b
, nir_fmul(b
, vec
, vec
));
44 case 2: return nir_fsqrt(b
, nir_fdot2(b
, vec
, vec
));
45 case 3: return nir_fsqrt(b
, nir_fdot3(b
, vec
, vec
));
46 case 4: return nir_fsqrt(b
, nir_fdot4(b
, vec
, vec
));
48 unreachable("Invalid number of components");
53 nir_smoothstep(nir_builder
*b
, nir_ssa_def
*edge0
, nir_ssa_def
*edge1
, nir_ssa_def
*x
)
55 nir_ssa_def
*f2
= nir_imm_floatN_t(b
, 2.0, x
->bit_size
);
56 nir_ssa_def
*f3
= nir_imm_floatN_t(b
, 3.0, x
->bit_size
);
58 /* t = clamp((x - edge0) / (edge1 - edge0), 0, 1) */
60 nir_fsat(b
, nir_fdiv(b
, nir_fsub(b
, x
, edge0
),
61 nir_fsub(b
, edge1
, edge0
)));
63 /* result = t * t * (3 - 2 * t) */
64 return nir_fmul(b
, t
, nir_fmul(b
, t
, nir_fsub(b
, f3
, nir_fmul(b
, f2
, t
))));