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>
18 return abs(X);
19}
20
21template <typename T, int N>
22constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
23length_impl(vector<T, N> X) {
24#if (__has_builtin(__builtin_spirv_length))
25 return __builtin_spirv_length(X);
26#else
27 return sqrt(dot(X, X));
28#endif
29}
30
31constexpr float dot2add_impl(half2 a, half2 b, float c) {
32#if (__has_builtin(__builtin_dx_dot2add))
33 return __builtin_dx_dot2add(a, b, c);
34#else
35 return dot(a, b) + c;
36#endif
37}
38
39template <typename T, int N>
40constexpr enable_if_t<!is_same<double, T>::value, T>
41mul_vec_impl(vector<T, N> x, vector<T, N> y) {
42 return dot(x, y);
43}
44
45// Double vectors do not have a dot intrinsic, so expand manually.
46template <typename T, int N>
48 vector<T, N> y) {
49 T sum = x[0] * y[0];
50 [unroll] for (int i = 1; i < N; ++i) sum = mad(x[i], y[i], sum);
51 return sum;
52}
53
54template <typename T>
55constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value,
56 vector<T, 3>>
57cross_impl(vector<T, 3> x, vector<T, 3> y) {
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]);
60}
61
62template <typename T>
63constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
64reflect_impl(T I, T N) {
65 return I - 2 * N * I * N;
66}
67
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);
72#else
73 return I - 2 * N * dot(I, N);
74#endif
75}
76
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);
80#endif
81 T Mul = dot(N, I);
82 T K = 1 - Eta * Eta * (1 - Mul * Mul);
83 T Result = (Eta * I - (Eta * Mul + sqrt(K)) * N);
84 return select<T>(K < 0, static_cast<T>(0), Result);
85}
86
87template <typename T> constexpr T fmod_impl(T X, T Y) {
88#if !defined(__DIRECTX__)
89 return __builtin_elementwise_fmod(X, Y);
90#else
91 T div = X / Y;
92 bool ge = div >= 0;
93 T frc = frac(abs(div));
94 return select<T>(ge, frc, -frc) * Y;
95#endif
96}
97
98template <typename T, int N>
99constexpr vector<T, N> fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
100#if !defined(__DIRECTX__)
101 return __builtin_elementwise_fmod(X, Y);
102#else
103 vector<T, N> div = X / Y;
104 vector<bool, N> ge = div >= 0;
105 vector<T, N> frc = frac(abs(div));
106 return select<T>(ge, frc, -frc) * Y;
107#endif
108}
109
110template <typename T> constexpr T smoothstep_impl(T Min, T Max, T X) {
111#if (__has_builtin(__builtin_spirv_smoothstep))
112 return __builtin_spirv_smoothstep(Min, Max, X);
113#else
114 T S = saturate((X - Min) / (Max - Min));
115 return (3 - 2 * S) * S * S;
116#endif
117}
118
119template <typename T> constexpr T step_impl(T Y, T X) {
120 return select(X < Y, (T)0, (T)1);
121}
122
123template <typename T> constexpr T lerp_impl(T X, T Y, T S) {
124 return X + S * (Y - X);
125}
126
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};
131 // clang-format off
132 bool SpecularCond = or(DiffuseCond, (NDotH < 0));
133 // clang-format on
134 T SpecularExp = exp(log(NDotH) * M);
135 Result[2] = select<T>(SpecularCond, 0, SpecularExp);
136 return Result;
137}
138
139template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
140 return select<T>(dot(I, Ng) < 0, N, -N);
141}
142
143template <typename K, typename T, int BitWidth>
144constexpr K firstbithigh_impl(T X) {
145 K FBH = __builtin_hlsl_elementwise_firstbithigh(X);
146#if defined(__DIRECTX__)
147 // The firstbithigh DXIL ops count bits from the wrong side, so we need to
148 // invert it for DirectX.
149 K Inversion = (BitWidth - 1) - FBH;
150 FBH = select(FBH == -1, FBH, Inversion);
151#endif
152 return FBH;
153}
154
155template <typename T> constexpr T ddx_impl(T input) {
156#if (__has_builtin(__builtin_spirv_ddx))
157 return __builtin_spirv_ddx(input);
158#else
159 return __builtin_hlsl_elementwise_ddx_coarse(input);
160#endif
161}
162
163template <typename T> constexpr T ddy_impl(T input) {
164#if (__has_builtin(__builtin_spirv_ddy))
165 return __builtin_spirv_ddy(input);
166#else
167 return __builtin_hlsl_elementwise_ddy_coarse(input);
168#endif
169}
170
171template <typename T> constexpr T fwidth_impl(T input) {
172#if (__has_builtin(__builtin_spirv_fwidth))
173 return __builtin_spirv_fwidth(input);
174#else
175 T derivCoarseX = ddx_coarse(input);
176 derivCoarseX = abs(derivCoarseX);
177 T derivCoarseY = ddy_coarse(input);
178 derivCoarseY = abs(derivCoarseY);
179 return derivCoarseX + derivCoarseY;
180#endif
181}
182
183template <typename T> constexpr T degrees_impl(T Val) {
184 return Val * (T)(180.L / Pi);
185}
186
187template <typename T> constexpr T radians_impl(T Val) {
188 return Val * (T)(Pi / 180.L);
189}
190
191} // namespace __detail
192} // namespace hlsl
193
194#endif // _HLSL_HLSL_INTRINSIC_HELPERS_H_
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
const FunctionProtoType * T
constexpr T faceforward_impl(T N, T I, T Ng)
constexpr double Pi
Definition hlsl_detail.h:16
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
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 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.
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