Debian Codegraph @

clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h

raw
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
12 namespace hlsl {
13 namespace __detail {
14
15 constexpr int4 d3d_color_to_ubyte4_impl(float4 V) {
16 // Use the same scaling factor used by FXC, and DXC for DXIL
17 // (i.e., 255.001953)
18 // https://github.com/microsoft/DirectXShaderCompiler/blob/070d0d5a2beacef9eeb51037a9b04665716fd6f3/lib/HLSL/HLOperationLower.cpp#L666C1-L697C2
19 // The DXC implementation refers to a comment on the following stackoverflow
20 // discussion to justify the scaling factor: "Built-in rounding, necessary
21 // because of truncation. 0.001953 * 256 = 0.5"
22 // https://stackoverflow.com/questions/52103720/why-does-d3dcolortoubyte4-multiplies-components-by-255-001953f
23 return V.zyxw * 255.001953f;
24 }
25
26 template <typename T> constexpr T length_impl(T X) { return abs(X); }
27
28 template <typename T, int N>
29 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
30 length_vec_impl(vector<T, N> X) {
31 #if (__has_builtin(__builtin_spirv_length))
32 return __builtin_spirv_length(X);
33 #else
34 return sqrt(dot(X, X));
35 #endif
36 }
37
38 template <typename T>
39 constexpr vector<T, 4> dst_impl(vector<T, 4> Src0, vector<T, 4> Src1) {
40 return {1, Src0[1] * Src1[1], Src0[2], Src1[3]};
41 }
42
43 template <typename T> constexpr T distance_impl(T X, T Y) {
44 return length_impl(X - Y);
45 }
46
47 template <typename T, int N>
48 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
49 distance_vec_impl(vector<T, N> X, vector<T, N> Y) {
50 return length_vec_impl(X - Y);
51 }
52
53 constexpr float dot2add_impl(half2 a, half2 b, float c) {
54 #if (__has_builtin(__builtin_dx_dot2add))
55 return __builtin_dx_dot2add(a, b, c);
56 #else
57 return dot(a, b) + c;
58 #endif
59 }
60
61 template <typename T> constexpr T reflect_impl(T I, T N) {
62 return I - 2 * N * I * N;
63 }
64
65 template <typename T, int L>
66 constexpr vector<T, L> reflect_vec_impl(vector<T, L> I, vector<T, L> N) {
67 #if (__has_builtin(__builtin_spirv_reflect))
68 return __builtin_spirv_reflect(I, N);
69 #else
70 return I - 2 * N * dot(I, N);
71 #endif
72 }
73
74 template <typename T, typename U> constexpr T refract_impl(T I, T N, U Eta) {
75 #if (__has_builtin(__builtin_spirv_refract))
76 return __builtin_spirv_refract(I, N, Eta);
77 #endif
78 T Mul = dot(N, I);
79 T K = 1 - Eta * Eta * (1 - Mul * Mul);
80 T Result = (Eta * I - (Eta * Mul + sqrt(K)) * N);
81 return select<T>(K < 0, static_cast<T>(0), Result);
82 }
83
84 template <typename T> constexpr T fmod_impl(T X, T Y) {
85 #if !defined(__DIRECTX__)
86 return __builtin_elementwise_fmod(X, Y);
87 #else
88 T div = X / Y;
89 bool ge = div >= 0;
90 T frc = frac(abs(div));
91 return select<T>(ge, frc, -frc) * Y;
92 #endif
93 }
94
95 template <typename T, int N>
96 constexpr vector<T, N> fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
97 #if !defined(__DIRECTX__)
98 return __builtin_elementwise_fmod(X, Y);
99 #else
100 vector<T, N> div = X / Y;
101 vector<bool, N> ge = div >= 0;
102 vector<T, N> frc = frac(abs(div));
103 return select<T>(ge, frc, -frc) * Y;
104 #endif
105 }
106
107 template <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
116 template <typename T, int N>
117 constexpr vector<T, N> smoothstep_vec_impl(vector<T, N> Min, vector<T, N> Max,
118 vector<T, N> X) {
119 #if (__has_builtin(__builtin_spirv_smoothstep))
120 return __builtin_spirv_smoothstep(Min, Max, X);
121 #else
122 vector<T, N> S = saturate((X - Min) / (Max - Min));
123 return (3 - 2 * S) * S * S;
124 #endif
125 }
126
127 template <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
139 template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
140 return select(dot(I, Ng) < 0, N, -N);
141 }
142
143 template <typename T> constexpr T ldexp_impl(T X, T Exp) {
144 return exp2(Exp) * X;
145 }
146
147 template <typename K, typename T, int BitWidth>
148 constexpr K firstbithigh_impl(T X) {
149 K FBH = __builtin_hlsl_elementwise_firstbithigh(X);
150 #if defined(__DIRECTX__)
151 // The firstbithigh DXIL ops count bits from the wrong side, so we need to
152 // invert it for DirectX.
153 K Inversion = (BitWidth - 1) - FBH;
154 FBH = select(FBH == -1, FBH, Inversion);
155 #endif
156 return FBH;
157 }
158
159 template <typename T> constexpr T ddx_impl(T input) {
160 #if (__has_builtin(__builtin_spirv_ddx))
161 return __builtin_spirv_ddx(input);
162 #else
163 return __builtin_hlsl_elementwise_ddx_coarse(input);
164 #endif
165 }
166
167 template <typename T> constexpr T ddy_impl(T input) {
168 #if (__has_builtin(__builtin_spirv_ddy))
169 return __builtin_spirv_ddy(input);
170 #else
171 return __builtin_hlsl_elementwise_ddy_coarse(input);
172 #endif
173 }
174
175 template <typename T> constexpr T fwidth_impl(T input) {
176 #if (__has_builtin(__builtin_spirv_fwidth))
177 return __builtin_spirv_fwidth(input);
178 #else
179 T derivCoarseX = ddx_coarse(input);
180 derivCoarseX = abs(derivCoarseX);
181 T derivCoarseY = ddy_coarse(input);
182 derivCoarseY = abs(derivCoarseY);
183 return derivCoarseX + derivCoarseY;
184 #endif
185 }
186
187 } // namespace __detail
188 } // namespace hlsl
189
190 #endif // _HLSL_HLSL_INTRINSIC_HELPERS_H_