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 T Div = X / Y;
90 T Frc = frac(abs(Div));
91 return select(Div >= 0, Frc, (T)-Frc) * Y;
92#else
93 return __builtin_elementwise_fmod(X, Y);
94#endif
95}
96
97template <typename T> constexpr T smoothstep_impl(T Min, T Max, T X) {
98#if (__has_builtin(__builtin_spirv_smoothstep))
99 return __builtin_spirv_smoothstep(Min, Max, X);
100#else
101 T S = saturate((X - Min) / (Max - Min));
102 return (3 - 2 * S) * S * S;
103#endif
104}
105
106template <typename T> constexpr T step_impl(T Y, T X) {
107 return select(X < Y, (T)0, (T)1);
108}
109
110template <typename T> constexpr T lerp_impl(T X, T Y, T S) {
111 return X + S * (Y - X);
112}
113
114template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) {
115 bool DiffuseCond = NDotL < 0;
116 T Diffuse = select<T>(DiffuseCond, 0, NDotL);
117 vector<T, 4> Result = {1, Diffuse, 0, 1};
118 // clang-format off
119 bool SpecularCond = or(DiffuseCond, (NDotH < 0));
120 // clang-format on
121 T SpecularExp = exp(log(NDotH) * M);
122 Result[2] = select<T>(SpecularCond, 0, SpecularExp);
123 return Result;
124}
125
126template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
127 return select<T>(dot(I, Ng) < 0, N, -N);
128}
129
130template <typename K, typename T, int BitWidth>
131constexpr K firstbithigh_impl(T X) {
132 K FBH = __builtin_hlsl_elementwise_firstbithigh(X);
133#if defined(__DIRECTX__)
134 // The firstbithigh DXIL ops count bits from the wrong side, so we need to
135 // invert it for DirectX.
136 K Inversion = (BitWidth - 1) - FBH;
137 FBH = select(FBH == -1, FBH, Inversion);
138#endif
139 return FBH;
140}
141
142template <typename T> constexpr T ddx_impl(T input) {
143#if (__has_builtin(__builtin_spirv_ddx))
144 return __builtin_spirv_ddx(input);
145#else
146 return __builtin_hlsl_elementwise_ddx_coarse(input);
147#endif
148}
149
150template <typename T> constexpr T ddy_impl(T input) {
151#if (__has_builtin(__builtin_spirv_ddy))
152 return __builtin_spirv_ddy(input);
153#else
154 return __builtin_hlsl_elementwise_ddy_coarse(input);
155#endif
156}
157
158template <typename T> constexpr T fwidth_impl(T input) {
159#if (__has_builtin(__builtin_spirv_fwidth))
160 return __builtin_spirv_fwidth(input);
161#else
162 T derivCoarseX = ddx_coarse(input);
163 derivCoarseX = abs(derivCoarseX);
164 T derivCoarseY = ddy_coarse(input);
165 derivCoarseY = abs(derivCoarseY);
166 return derivCoarseX + derivCoarseY;
167#endif
168}
169
170} // namespace __detail
171} // namespace hlsl
172
173#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 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)
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 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