clang 24.0.0git
hlsl_intrinsic_helpers.h
Go to the documentation of this file.
1//===----- hlsl_intrinsic_helpers.h - HLSL helpers intrinsics -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _HLSL_HLSL_INTRINSIC_HELPERS_H_
10#define _HLSL_HLSL_INTRINSIC_HELPERS_H_
11
12namespace hlsl {
13namespace __detail {
14
15template <typename T, int N> constexpr bool all_impl(vector<T, N> V) {
16 return __builtin_reduce_and(
17 __builtin_convertvector(V != static_cast<T>(0), vector<bool, N>));
18}
19
20template <typename T, int N> constexpr bool any_impl(vector<T, N> V) {
21 return __builtin_reduce_or(
22 __builtin_convertvector(V != static_cast<T>(0), vector<bool, N>));
23}
24
25template <typename T>
26constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
28 return abs(X);
29}
30
31template <typename T, int N>
32constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
33length_impl(vector<T, N> X) {
34#if (__has_builtin(__builtin_spirv_length))
35 return __builtin_spirv_length(X);
36#else
37 return sqrt(dot(X, X));
38#endif
39}
40
41constexpr float dot2add_impl(half2 a, half2 b, float c) {
42#if (__has_builtin(__builtin_dx_dot2add))
43 return __builtin_dx_dot2add(a, b, c);
44#else
45 return dot(a, b) + c;
46#endif
47}
48
49template <typename T, int N>
50constexpr enable_if_t<!is_same<double, T>::value, T>
51mul_vec_impl(vector<T, N> x, vector<T, N> y) {
52 return dot(x, y);
53}
54
55// Double vectors do not have a dot intrinsic, so expand manually.
56template <typename T, int N>
58 vector<T, N> y) {
59 T sum = x[0] * y[0];
60 [unroll] for (int i = 1; i < N; ++i) sum = mad(x[i], y[i], sum);
61 return sum;
62}
63
64template <typename T>
65constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value,
66 vector<T, 3>>
67cross_impl(vector<T, 3> x, vector<T, 3> y) {
68 return vector<T, 3>(x[1] * y[2] - y[1] * x[2], x[2] * y[0] - y[2] * x[0],
69 x[0] * y[1] - y[0] * x[1]);
70}
71
72template <typename T>
73constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
74reflect_impl(T I, T N) {
75 return I - 2 * N * I * N;
76}
77
78template <typename T, int L>
79constexpr vector<T, L> reflect_impl(vector<T, L> I, vector<T, L> N) {
80#if (__has_builtin(__builtin_spirv_reflect))
81 return __builtin_spirv_reflect(I, N);
82#else
83 return I - 2 * N * dot(I, N);
84#endif
85}
86
87template <typename T, typename U> constexpr T refract_impl(T I, T N, U Eta) {
88#if (__has_builtin(__builtin_spirv_refract))
89 return __builtin_spirv_refract(I, N, Eta);
90#endif
91 T Mul = dot(N, I);
92 T K = 1 - Eta * Eta * (1 - Mul * Mul);
93 T Result = (Eta * I - (Eta * Mul + sqrt(K)) * N);
94 return select<T>(K < 0, static_cast<T>(0), Result);
95}
96
97template <typename T> constexpr T fmod_impl(T X, T Y) {
98#if defined(__DIRECTX__)
99 T Div = X / Y;
100 T Frc = frac(abs(Div));
101 return select(Div >= 0, Frc, (T)-Frc) * Y;
102#else
103 return __builtin_elementwise_fmod(X, Y);
104#endif
105}
106
107template <typename T> constexpr T smoothstep_impl(T Min, T Max, T X) {
108#if (__has_builtin(__builtin_spirv_smoothstep))
109 return __builtin_spirv_smoothstep(Min, Max, X);
110#else
111 T S = saturate((X - Min) / (Max - Min));
112 return (3 - 2 * S) * S * S;
113#endif
114}
115
116template <typename T> constexpr T step_impl(T Y, T X) {
117 return select(X < Y, (T)(__detail::elem_type_t<T>)0,
119}
120
121template <typename T> constexpr T lerp_impl(T X, T Y, T S) {
122 return X + S * (Y - X);
123}
124
125template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) {
126 bool DiffuseCond = NDotL < 0;
127 T Diffuse = select<T>(DiffuseCond, 0, NDotL);
128 vector<T, 4> Result = {1, Diffuse, 0, 1};
129 // clang-format off
130 bool SpecularCond = or(DiffuseCond, (NDotH < 0));
131 // clang-format on
132 T SpecularExp = exp(log(NDotH) * M);
133 Result[2] = select<T>(SpecularCond, 0, SpecularExp);
134 return Result;
135}
136
137template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
138 return select<T>(dot(I, Ng) < 0, N, -N);
139}
140
141template <typename K, typename T, int BitWidth>
142constexpr K firstbithigh_impl(T X) {
143 K FBH = __builtin_hlsl_elementwise_firstbithigh(X);
144#if defined(__DIRECTX__)
145 // The firstbithigh DXIL ops count bits from the wrong side, so we need to
146 // invert it for DirectX.
147 K Inversion = (BitWidth - 1) - FBH;
148 FBH = select(FBH == -1, FBH, Inversion);
149#endif
150 return FBH;
151}
152
153template <typename T> constexpr T ddx_impl(T input) {
154#if (__has_builtin(__builtin_spirv_ddx))
155 return __builtin_spirv_ddx(input);
156#else
157 return __builtin_hlsl_elementwise_ddx_coarse(input);
158#endif
159}
160
161template <typename T> constexpr T ddy_impl(T input) {
162#if (__has_builtin(__builtin_spirv_ddy))
163 return __builtin_spirv_ddy(input);
164#else
165 return __builtin_hlsl_elementwise_ddy_coarse(input);
166#endif
167}
168
169template <typename T> constexpr T fwidth_impl(T input) {
170#if (__has_builtin(__builtin_spirv_fwidth))
171 return __builtin_spirv_fwidth(input);
172#else
173 T derivCoarseX = ddx_coarse(input);
174 derivCoarseX = abs(derivCoarseX);
175 T derivCoarseY = ddy_coarse(input);
176 derivCoarseY = abs(derivCoarseY);
177 return derivCoarseX + derivCoarseY;
178#endif
179}
180
181} // namespace __detail
182} // namespace hlsl
183
184#endif // _HLSL_HLSL_INTRINSIC_HELPERS_H_
#define V(N, I)
Result
Implement __builtin_bit_cast and related operations.
#define X(type, name)
Definition Value.h:97
__DEVICE__ long long abs(long long __n)
#define or
Definition iso646.h:24
constexpr T faceforward_impl(T N, T I, T Ng)
constexpr bool all_impl(vector< T, N > V)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > reflect_impl(T I, T N)
constexpr T fwidth_impl(T input)
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)
typename elem_type< T >::Type elem_type_t
Definition hlsl_detail.h:54
constexpr T step_impl(T Y, T X)
constexpr T fmod_impl(T X, T Y)
constexpr bool any_impl(vector< T, N > V)
typename enable_if< B, T >::Type enable_if_t
Definition hlsl_detail.h:33
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 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.
vector< half, 2 > half2
float __ovld __cnfn dot(float, float)
Compute dot product.
float __ovld __cnfn mad(float, float, float)
mad approximates a * b + c.
static const bool value
Definition hlsl_detail.h:19
#define sqrt(__x)
Definition tgmath.h:520
#define exp(__x)
Definition tgmath.h:431
#define log(__x)
Definition tgmath.h:460