9#ifndef _HLSL_HLSL_INTRINSIC_HELPERS_H_
10#define _HLSL_HLSL_INTRINSIC_HELPERS_H_
23 return V.zyxw * 255.001953f;
28template <
typename T,
int N>
31#if (__has_builtin(__builtin_spirv_length))
32 return __builtin_spirv_length(
X);
39constexpr vector<T, 4>
dst_impl(vector<T, 4> Src0, vector<T, 4> Src1) {
40 return {1, Src0[1] * Src1[1], Src0[2], Src1[3]};
47template <
typename T,
int N>
54#if (__has_builtin(__builtin_dx_dot2add))
55 return __builtin_dx_dot2add(a,
b,
c);
61template <
typename T,
int N>
62constexpr enable_if_t<!is_same<double, T>::value, T>
68template <
typename T,
int N>
72 [unroll]
for (
int i = 1; i < N; ++i) sum =
mad(x[i], y[i], sum);
77 return I - 2 * N * I * N;
80template <
typename T,
int L>
82#if (__has_builtin(__builtin_spirv_reflect))
83 return __builtin_spirv_reflect(I, N);
85 return I - 2 * N *
dot(I, N);
89template <
typename T,
typename U>
constexpr T
refract_impl(T I, T N,
U Eta) {
90#if (__has_builtin(__builtin_spirv_refract))
91 return __builtin_spirv_refract(I, N, Eta);
94 T K = 1 - Eta * Eta * (1 - Mul * Mul);
95 T Result = (Eta * I - (Eta * Mul +
sqrt(K)) * N);
96 return select<T>(K < 0,
static_cast<T
>(0), Result);
100#if !defined(__DIRECTX__)
101 return __builtin_elementwise_fmod(
X, Y);
110template <
typename T,
int N>
112#if !defined(__DIRECTX__)
113 return __builtin_elementwise_fmod(
X, Y);
115 vector<T, N> div =
X / Y;
116 vector<bool, N> ge = div >= 0;
117 vector<T, N> frc =
frac(
abs(div));
123#if (__has_builtin(__builtin_spirv_smoothstep))
124 return __builtin_spirv_smoothstep(Min, Max,
X);
127 return (3 - 2 * S) * S * S;
131template <
typename T,
int N>
134#if (__has_builtin(__builtin_spirv_smoothstep))
135 return __builtin_spirv_smoothstep(Min, Max,
X);
137 vector<T, N> S =
saturate((
X - Min) / (Max - Min));
138 return (3 - 2 * S) * S * S;
142template <
typename T>
constexpr vector<T, 4>
lit_impl(T NDotL, T NDotH, T M) {
143 bool DiffuseCond = NDotL < 0;
144 T Diffuse =
select<T>(DiffuseCond, 0, NDotL);
145 vector<T, 4> Result = {1, Diffuse, 0, 1};
147 bool SpecularCond =
or(DiffuseCond, (NDotH < 0));
149 T SpecularExp =
exp(
log(NDotH) * M);
150 Result[2] =
select<T>(SpecularCond, 0, SpecularExp);
159 return exp2(Exp) *
X;
162template <
typename K,
typename T,
int BitW
idth>
164 K FBH = __builtin_hlsl_elementwise_firstbithigh(
X);
165#if defined(__DIRECTX__)
168 K Inversion = (BitWidth - 1) - FBH;
169 FBH =
select(FBH == -1, FBH, Inversion);
174template <
typename T>
constexpr T
ddx_impl(T input) {
175#if (__has_builtin(__builtin_spirv_ddx))
176 return __builtin_spirv_ddx(input);
178 return __builtin_hlsl_elementwise_ddx_coarse(input);
182template <
typename T>
constexpr T
ddy_impl(T input) {
183#if (__has_builtin(__builtin_spirv_ddy))
184 return __builtin_spirv_ddy(input);
186 return __builtin_hlsl_elementwise_ddy_coarse(input);
191#if (__has_builtin(__builtin_spirv_fwidth))
192 return __builtin_spirv_fwidth(input);
195 derivCoarseX =
abs(derivCoarseX);
197 derivCoarseY =
abs(derivCoarseY);
198 return derivCoarseX + derivCoarseY;
__device__ __2f16 float c
constexpr vector< T, N > smoothstep_vec_impl(vector< T, N > Min, vector< T, N > Max, vector< T, N > X)
constexpr T length_impl(T X)
constexpr vector< T, 4 > dst_impl(vector< T, 4 > Src0, vector< T, 4 > Src1)
constexpr T faceforward_impl(T N, T I, T Ng)
constexpr T fwidth_impl(T input)
constexpr vector< T, L > reflect_vec_impl(vector< T, L > I, vector< T, L > N)
constexpr T distance_impl(T X, T Y)
constexpr K firstbithigh_impl(T X)
constexpr T reflect_impl(T I, T N)
constexpr T ldexp_impl(T X, T Exp)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > distance_vec_impl(vector< T, N > X, vector< T, N > Y)
constexpr T fmod_impl(T X, T Y)
typename enable_if< B, T >::Type enable_if_t
constexpr T ddx_impl(T input)
constexpr int4 d3d_color_to_ubyte4_impl(float4 V)
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_vec_impl(vector< T, N > 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)
half mad(half, half, half)
T select(bool, T, T)
ternary operator.
vector< float, 4 > float4