9#ifndef _HLSL_HLSL_INTRINSIC_HELPERS_H_
10#define _HLSL_HLSL_INTRINSIC_HELPERS_H_
21template <
typename T,
int N>
24#if (__has_builtin(__builtin_spirv_length))
25 return __builtin_spirv_length(
X);
32#if (__has_builtin(__builtin_dx_dot2add))
33 return __builtin_dx_dot2add(a, b, c);
39template <
typename T,
int N>
40constexpr enable_if_t<!is_same<double, T>::value, T>
46template <
typename T,
int N>
50 [unroll]
for (
int i = 1; i < N; ++i) sum =
mad(x[i], y[i], sum);
58 return vector<T, 3>(x[1] * y[2] - y[1] * x[2], x[2] * y[0] - y[2] * x[0],
59 x[0] * y[1] - y[0] * x[1]);
65 return I - 2 * N * I * N;
68template <
typename T,
int L>
69constexpr vector<T, L>
reflect_impl(vector<T, L> I, vector<T, L> N) {
70#if (__has_builtin(__builtin_spirv_reflect))
71 return __builtin_spirv_reflect(I, N);
73 return I - 2 * N *
dot(I, N);
77template <
typename T,
typename U>
constexpr T
refract_impl(T I, T N,
U Eta) {
78#if (__has_builtin(__builtin_spirv_refract))
79 return __builtin_spirv_refract(I, N, Eta);
82 T K = 1 - Eta * Eta * (1 - Mul * Mul);
83 T
Result = (Eta * I - (Eta * Mul +
sqrt(K)) * N);
88#if !defined(__DIRECTX__)
89 return __builtin_elementwise_fmod(
X, Y);
93 T frc = frac(
abs(div));
98template <
typename T,
int N>
100#if !defined(__DIRECTX__)
101 return __builtin_elementwise_fmod(
X, Y);
103 vector<T, N> div =
X / Y;
104 vector<bool, N> ge = div >= 0;
105 vector<T, N> frc = frac(
abs(div));
111#if (__has_builtin(__builtin_spirv_smoothstep))
112 return __builtin_spirv_smoothstep(Min, Max,
X);
114 T S = saturate((
X - Min) / (Max - Min));
115 return (3 - 2 * S) * S * S;
120 return select(
X < Y, (T)0, (T)1);
124 return X + S * (Y -
X);
127template <
typename T>
constexpr vector<T, 4>
lit_impl(T NDotL, T NDotH, T M) {
128 bool DiffuseCond = NDotL < 0;
129 T Diffuse =
select<T>(DiffuseCond, 0, NDotL);
130 vector<T, 4>
Result = {1, Diffuse, 0, 1};
132 bool SpecularCond =
or(DiffuseCond, (NDotH < 0));
134 T SpecularExp =
exp(
log(NDotH) * M);
143template <
typename K,
typename T,
int BitW
idth>
145 K FBH = __builtin_hlsl_elementwise_firstbithigh(
X);
146#if defined(__DIRECTX__)
149 K Inversion = (BitWidth - 1) - FBH;
150 FBH =
select(FBH == -1, FBH, Inversion);
155template <
typename T>
constexpr T
ddx_impl(T input) {
156#if (__has_builtin(__builtin_spirv_ddx))
157 return __builtin_spirv_ddx(input);
159 return __builtin_hlsl_elementwise_ddx_coarse(input);
163template <
typename T>
constexpr T
ddy_impl(T input) {
164#if (__has_builtin(__builtin_spirv_ddy))
165 return __builtin_spirv_ddy(input);
167 return __builtin_hlsl_elementwise_ddy_coarse(input);
172#if (__has_builtin(__builtin_spirv_fwidth))
173 return __builtin_spirv_fwidth(input);
175 T derivCoarseX = ddx_coarse(input);
176 derivCoarseX =
abs(derivCoarseX);
177 T derivCoarseY = ddy_coarse(input);
178 derivCoarseY =
abs(derivCoarseY);
179 return derivCoarseX + derivCoarseY;
184 return Val * (T)(180.L /
Pi);
188 return Val * (T)(
Pi / 180.L);
Result
Implement __builtin_bit_cast and related operations.
__DEVICE__ long long abs(long long __n)
const FunctionProtoType * T
constexpr T faceforward_impl(T N, T I, T Ng)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > reflect_impl(T I, T N)
constexpr T degrees_impl(T Val)
constexpr T fwidth_impl(T input)
constexpr T radians_impl(T Val)
constexpr K firstbithigh_impl(T X)
constexpr T lerp_impl(T X, T Y, T S)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, vector< T, 3 > > cross_impl(vector< T, 3 > x, vector< T, 3 > y)
constexpr T step_impl(T Y, T X)
constexpr T fmod_impl(T X, T Y)
typename enable_if< B, T >::Type enable_if_t
constexpr T ddx_impl(T input)
constexpr T smoothstep_impl(T Min, T Max, T X)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > length_impl(T X)
constexpr T refract_impl(T I, T N, U Eta)
constexpr float dot2add_impl(half2 a, half2 b, float c)
constexpr vector< T, N > fmod_vec_impl(vector< T, N > X, vector< T, N > Y)
constexpr T ddy_impl(T input)
constexpr vector< T, 4 > lit_impl(T NDotL, T NDotH, T M)
constexpr enable_if_t<!is_same< double, T >::value, T > mul_vec_impl(vector< T, N > x, vector< T, N > y)
T select(bool, T, T)
ternary operator.
float __ovld __cnfn dot(float, float)
Compute dot product.
float __ovld __cnfn mad(float, float, float)
mad approximates a * b + c.