clang 23.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, T>
56reflect_impl(T I, T N) {
57 return I - 2 * N * I * N;
58}
59
60template <typename T, int L>
61constexpr vector<T, L> reflect_impl(vector<T, L> I, vector<T, L> N) {
62#if (__has_builtin(__builtin_spirv_reflect))
63 return __builtin_spirv_reflect(I, N);
64#else
65 return I - 2 * N * dot(I, N);
66#endif
67}
68
69template <typename T, typename U> constexpr T refract_impl(T I, T N, U Eta) {
70#if (__has_builtin(__builtin_spirv_refract))
71 return __builtin_spirv_refract(I, N, Eta);
72#endif
73 T Mul = dot(N, I);
74 T K = 1 - Eta * Eta * (1 - Mul * Mul);
75 T Result = (Eta * I - (Eta * Mul + sqrt(K)) * N);
76 return select<T>(K < 0, static_cast<T>(0), Result);
77}
78
79template <typename T> constexpr T fmod_impl(T X, T Y) {
80#if !defined(__DIRECTX__)
81 return __builtin_elementwise_fmod(X, Y);
82#else
83 T div = X / Y;
84 bool ge = div >= 0;
85 T frc = frac(abs(div));
86 return select<T>(ge, frc, -frc) * Y;
87#endif
88}
89
90template <typename T, int N>
91constexpr vector<T, N> fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
92#if !defined(__DIRECTX__)
93 return __builtin_elementwise_fmod(X, Y);
94#else
95 vector<T, N> div = X / Y;
96 vector<bool, N> ge = div >= 0;
97 vector<T, N> frc = frac(abs(div));
98 return select<T>(ge, frc, -frc) * Y;
99#endif
100}
101
102template <typename T> constexpr T smoothstep_impl(T Min, T Max, T X) {
103#if (__has_builtin(__builtin_spirv_smoothstep))
104 return __builtin_spirv_smoothstep(Min, Max, X);
105#else
106 T S = saturate((X - Min) / (Max - Min));
107 return (3 - 2 * S) * S * S;
108#endif
109}
110
111template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) {
112 bool DiffuseCond = NDotL < 0;
113 T Diffuse = select<T>(DiffuseCond, 0, NDotL);
114 vector<T, 4> Result = {1, Diffuse, 0, 1};
115 // clang-format off
116 bool SpecularCond = or(DiffuseCond, (NDotH < 0));
117 // clang-format on
118 T SpecularExp = exp(log(NDotH) * M);
119 Result[2] = select<T>(SpecularCond, 0, SpecularExp);
120 return Result;
121}
122
123template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
124 return select<T>(dot(I, Ng) < 0, N, -N);
125}
126
127template <typename K, typename T, int BitWidth>
128constexpr K firstbithigh_impl(T X) {
129 K FBH = __builtin_hlsl_elementwise_firstbithigh(X);
130#if defined(__DIRECTX__)
131 // The firstbithigh DXIL ops count bits from the wrong side, so we need to
132 // invert it for DirectX.
133 K Inversion = (BitWidth - 1) - FBH;
134 FBH = select(FBH == -1, FBH, Inversion);
135#endif
136 return FBH;
137}
138
139template <typename T> constexpr T ddx_impl(T input) {
140#if (__has_builtin(__builtin_spirv_ddx))
141 return __builtin_spirv_ddx(input);
142#else
143 return __builtin_hlsl_elementwise_ddx_coarse(input);
144#endif
145}
146
147template <typename T> constexpr T ddy_impl(T input) {
148#if (__has_builtin(__builtin_spirv_ddy))
149 return __builtin_spirv_ddy(input);
150#else
151 return __builtin_hlsl_elementwise_ddy_coarse(input);
152#endif
153}
154
155template <typename T> constexpr T fwidth_impl(T input) {
156#if (__has_builtin(__builtin_spirv_fwidth))
157 return __builtin_spirv_fwidth(input);
158#else
159 T derivCoarseX = ddx_coarse(input);
160 derivCoarseX = abs(derivCoarseX);
161 T derivCoarseY = ddy_coarse(input);
162 derivCoarseY = abs(derivCoarseY);
163 return derivCoarseX + derivCoarseY;
164#endif
165}
166
167} // namespace __detail
168} // namespace hlsl
169
170#endif // _HLSL_HLSL_INTRINSIC_HELPERS_H_
#define X(type, name)
Definition Value.h:97
__DEVICE__ long long abs(long long __n)
__device__ __2f16 b
__device__ __2f16 float c
#define or
Definition iso646.h:24
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 fmod_impl(T X, T Y)
typename enable_if< B, T >::Type enable_if_t
Definition hlsl_detail.h:31
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:17
#define sqrt(__x)
Definition tgmath.h:520
#define exp(__x)
Definition tgmath.h:431
#define log(__x)
Definition tgmath.h:460