1 //===----- detail.h - HLSL definitions for 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_DETAILS_H_ 10 #define _HLSL_HLSL_DETAILS_H_ 11 12 namespace hlsl { 13 14 namespace __detail { 15 16 template <typename T, typename U> struct is_same { 17 static const bool value = false; 18 }; 19 20 template <typename T> struct is_same<T, T> { 21 static const bool value = true; 22 }; 23 24 template <bool B, typename T> struct enable_if {}; 25 26 template <typename T> struct enable_if<true, T> { 27 using Type = T; 28 }; 29 30 template <bool B, class T = void> 31 using enable_if_t = typename enable_if<B, T>::Type; 32 33 template <typename U, typename T, int N> 34 constexpr enable_if_t<sizeof(U) == sizeof(T), vector<U, N>> 35 bit_cast(vector<T, N> V) { 36 return __builtin_bit_cast(vector<U, N>, V); 37 } 38 39 template <typename U, typename T> 40 constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) { 41 return __builtin_bit_cast(U, F); 42 } 43 44 constexpr vector<uint, 4> d3d_color_to_ubyte4_impl(vector<float, 4> V) { 45 // Use the same scaling factor used by FXC, and DXC for DXIL 46 // (i.e., 255.001953) 47 // https://github.com/microsoft/DirectXShaderCompiler/blob/070d0d5a2beacef9eeb51037a9b04665716fd6f3/lib/HLSL/HLOperationLower.cpp#L666C1-L697C2 48 // The DXC implementation refers to a comment on the following stackoverflow 49 // discussion to justify the scaling factor: "Built-in rounding, necessary 50 // because of truncation. 0.001953 * 256 = 0.5" 51 // https://stackoverflow.com/questions/52103720/why-does-d3dcolortoubyte4-multiplies-components-by-255-001953f 52 return V.zyxw * 255.001953f; 53 } 54 55 template <typename T> 56 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T> 57 length_impl(T X) { 58 return __builtin_elementwise_abs(X); 59 } 60 61 template <typename T, int N> 62 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T> 63 length_vec_impl(vector<T, N> X) { 64 #if (__has_builtin(__builtin_spirv_length)) 65 return __builtin_spirv_length(X); 66 #else 67 return __builtin_elementwise_sqrt(__builtin_hlsl_dot(X, X)); 68 #endif 69 } 70 71 template <typename T> 72 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T> 73 distance_impl(T X, T Y) { 74 return length_impl(X - Y); 75 } 76 77 template <typename T, int N> 78 constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T> 79 distance_vec_impl(vector<T, N> X, vector<T, N> Y) { 80 return length_vec_impl(X - Y); 81 } 82 } // namespace __detail 83 } // namespace hlsl 84 #endif //_HLSL_HLSL_DETAILS_H_ 85