Skip to content

Commit 3f1db30

Browse files
committed
Moved some functions to tgmath.hlsl
1 parent 845707c commit 3f1db30

File tree

8 files changed

+134
-180
lines changed

8 files changed

+134
-180
lines changed

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -298,75 +298,6 @@ struct find_msb_return_type<vector<Integer, N> >
298298
template<typename Integer>
299299
using find_lsb_return_type = find_msb_return_type<Integer>;
300300

301-
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
302-
struct lerp_helper;
303-
304-
#ifdef __HLSL_VERSION
305-
#define MIX_FUNCTION spirv::fMix
306-
#else
307-
#define MIX_FUNCTION glm::mix
308-
#endif
309-
310-
#define DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(TYPE)\
311-
template<>\
312-
struct lerp_helper<TYPE, TYPE>\
313-
{\
314-
static inline TYPE lerp(NBL_CONST_REF_ARG(TYPE) x, NBL_CONST_REF_ARG(TYPE) y, NBL_CONST_REF_ARG(TYPE) a)\
315-
{\
316-
return MIX_FUNCTION(x, y, a);\
317-
}\
318-
};\
319-
\
320-
template<int N>\
321-
struct lerp_helper<vector<TYPE, N>, vector<TYPE, N> >\
322-
{\
323-
static inline vector<TYPE, N> lerp(NBL_CONST_REF_ARG(vector<TYPE, N>) x, NBL_CONST_REF_ARG(vector<TYPE, N>) y, NBL_CONST_REF_ARG(vector<TYPE, N>) a)\
324-
{\
325-
return MIX_FUNCTION(x, y, a);\
326-
}\
327-
};\
328-
\
329-
template<int N>\
330-
struct lerp_helper<vector<TYPE, N>, TYPE>\
331-
{\
332-
static inline vector<TYPE, N> lerp(NBL_CONST_REF_ARG(vector<TYPE, N>) x, NBL_CONST_REF_ARG(vector<TYPE, N>) y, NBL_CONST_REF_ARG(TYPE) a)\
333-
{\
334-
return MIX_FUNCTION(x, y, a);\
335-
}\
336-
};\
337-
338-
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float32_t)
339-
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float64_t)
340-
341-
#undef DEFINE_LERP_HELPER_COMMON_SPECIALIZATION
342-
#undef MIX_FUNCTION
343-
344-
template<typename T>
345-
struct lerp_helper<T, bool>
346-
{
347-
static inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(bool) a)
348-
{
349-
if (a)
350-
return y;
351-
else
352-
return x;
353-
}
354-
};
355-
356-
template<typename T, int N>
357-
struct lerp_helper<vector<T, N>, vector<bool, N> >
358-
{
359-
using output_vec_t = vector<T, N>;
360-
361-
static inline output_vec_t lerp(NBL_CONST_REF_ARG(output_vec_t) x, NBL_CONST_REF_ARG(output_vec_t) y, NBL_CONST_REF_ARG(vector<bool, N>) a)
362-
{
363-
output_vec_t retval;
364-
for (uint32_t i = 0; i < vector_traits<output_vec_t>::Dimension; i++)
365-
retval[i] = a[i] ? y[i] : x[i];
366-
return retval;
367-
}
368-
};
369-
370301
template<typename Matrix>
371302
struct transpose_helper;
372303

@@ -394,20 +325,6 @@ struct mul_helper
394325
}
395326
};
396327

397-
template<typename UnsignedInteger NBL_FUNC_REQUIRES(hlsl::is_integral_v<UnsignedInteger>&& hlsl::is_unsigned_v<UnsignedInteger>)
398-
inline bool isnan_uint_impl(UnsignedInteger val)
399-
{
400-
using AsFloat = typename float_of_size<sizeof(UnsignedInteger)>::type;
401-
return bool((ieee754::extractBiasedExponent<UnsignedInteger>(val) == ieee754::traits<AsFloat>::specialValueExp) && (val & ieee754::traits<AsFloat>::mantissaMask));
402-
}
403-
404-
template<typename UnsignedInteger NBL_FUNC_REQUIRES(hlsl::is_integral_v<UnsignedInteger>&& hlsl::is_unsigned_v<UnsignedInteger>)
405-
inline bool isinf_uint_impl(UnsignedInteger val)
406-
{
407-
using AsFloat = typename float_of_size<sizeof(UnsignedInteger)>::type;
408-
return (val & (~ieee754::traits<AsFloat>::signMask)) == ieee754::traits<AsFloat>::inf;
409-
}
410-
411328
}
412329
}
413330
}

include/nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ inline typename cpp_compat_intrinsics_impl::find_msb_return_type<Integer>::type
8989
return cpp_compat_intrinsics_impl::find_msb_helper<Integer>::findMSB(val);
9090
}
9191

92-
// TODO: some of the functions in this header should move to `tgmath`
93-
template<typename T>
94-
inline T floor(NBL_CONST_REF_ARG(T) val)
95-
{
96-
#ifdef __HLSL_VERSION
97-
return spirv::floor(val);
98-
#else
99-
return glm::floor(val);
100-
#endif
101-
102-
}
103-
10492
// TODO: for clearer error messages, use concepts to ensure that input type is a square matrix
10593
// inverse not defined cause its implemented via hidden friend
10694
template<typename T, uint16_t N>
@@ -113,12 +101,6 @@ inline matrix<T, N, N> inverse(NBL_CONST_REF_ARG(matrix<T, N, N>) m)
113101
#endif
114102
}
115103

116-
template<typename T, typename U>
117-
inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(U) a)
118-
{
119-
return cpp_compat_intrinsics_impl::lerp_helper<T, U>::lerp(x, y, a);
120-
}
121-
122104
// transpose not defined cause its implemented via hidden friend
123105
template<typename Matrix>
124106
inline typename matrix_traits<Matrix>::transposed_type transpose(NBL_CONST_REF_ARG(Matrix) m)
@@ -153,56 +135,6 @@ inline T max(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
153135
#endif
154136
}
155137

156-
template<typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint>)
157-
inline bool isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
158-
{
159-
#ifdef __HLSL_VERSION
160-
return spirv::isNan(val);
161-
#else
162-
// GCC and Clang will always return false with call to std::isnan when fast math is enabled,
163-
// this implementation will always return appropriate output regardless is fas math is enabled or not
164-
using AsUint = typename unsigned_integer_of_size<sizeof(FloatingPoint)>::type;
165-
return cpp_compat_intrinsics_impl::isnan_uint_impl(reinterpret_cast<const AsUint&>(val));
166-
#endif
167-
}
168-
169-
template<typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint>)
170-
inline FloatingPoint isinf(NBL_CONST_REF_ARG(FloatingPoint) val)
171-
{
172-
#ifdef __HLSL_VERSION
173-
return spirv::isInf(val);
174-
#else
175-
// GCC and Clang will always return false with call to std::isinf when fast math is enabled,
176-
// this implementation will always return appropriate output regardless is fas math is enabled or not
177-
using AsUint = typename unsigned_integer_of_size<sizeof(FloatingPoint)>::type;
178-
return cpp_compat_intrinsics_impl::isinf_uint_impl(reinterpret_cast<const AsUint&>(val));
179-
#endif
180-
}
181-
182-
template<typename T>
183-
inline T exp2(NBL_CONST_REF_ARG(T) val)
184-
{
185-
#ifdef __HLSL_VERSION
186-
return spirv::exp2(val);
187-
#else
188-
return std::exp2(val);
189-
#endif
190-
}
191-
192-
#define DEFINE_EXP2_SPECIALIZATION(TYPE)\
193-
template<>\
194-
inline TYPE exp2(NBL_CONST_REF_ARG(TYPE) val)\
195-
{\
196-
return _static_cast<TYPE>(1ull << val);\
197-
}\
198-
199-
DEFINE_EXP2_SPECIALIZATION(int16_t)
200-
DEFINE_EXP2_SPECIALIZATION(int32_t)
201-
DEFINE_EXP2_SPECIALIZATION(int64_t)
202-
DEFINE_EXP2_SPECIALIZATION(uint16_t)
203-
DEFINE_EXP2_SPECIALIZATION(uint32_t)
204-
DEFINE_EXP2_SPECIALIZATION(uint64_t)
205-
206138
template<typename FloatingPoint>
207139
inline FloatingPoint rsqrt(FloatingPoint x)
208140
{
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#ifndef _NBL_BUILTIN_HLSL_TGMATH_IMPL_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_TGMATH_IMPL_INCLUDED_
3+
4+
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
5+
#include <nbl/builtin/hlsl/concepts.hlsl>
6+
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
7+
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
8+
#include <nbl/builtin/hlsl/ieee754.hlsl>
9+
10+
namespace nbl
11+
{
12+
namespace hlsl
13+
{
14+
namespace tgmath_impl
15+
{
16+
17+
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
18+
struct lerp_helper;
19+
20+
#ifdef __HLSL_VERSION
21+
#define MIX_FUNCTION spirv::fMix
22+
#else
23+
#define MIX_FUNCTION glm::mix
24+
#endif
25+
26+
#define DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(TYPE)\
27+
template<>\
28+
struct lerp_helper<TYPE, TYPE>\
29+
{\
30+
static inline TYPE lerp(NBL_CONST_REF_ARG(TYPE) x, NBL_CONST_REF_ARG(TYPE) y, NBL_CONST_REF_ARG(TYPE) a)\
31+
{\
32+
return MIX_FUNCTION(x, y, a);\
33+
}\
34+
};\
35+
\
36+
template<int N>\
37+
struct lerp_helper<vector<TYPE, N>, vector<TYPE, N> >\
38+
{\
39+
static inline vector<TYPE, N> lerp(NBL_CONST_REF_ARG(vector<TYPE, N>) x, NBL_CONST_REF_ARG(vector<TYPE, N>) y, NBL_CONST_REF_ARG(vector<TYPE, N>) a)\
40+
{\
41+
return MIX_FUNCTION(x, y, a);\
42+
}\
43+
};\
44+
\
45+
template<int N>\
46+
struct lerp_helper<vector<TYPE, N>, TYPE>\
47+
{\
48+
static inline vector<TYPE, N> lerp(NBL_CONST_REF_ARG(vector<TYPE, N>) x, NBL_CONST_REF_ARG(vector<TYPE, N>) y, NBL_CONST_REF_ARG(TYPE) a)\
49+
{\
50+
return MIX_FUNCTION(x, y, a);\
51+
}\
52+
};\
53+
54+
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float32_t)
55+
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float64_t)
56+
57+
#undef DEFINE_LERP_HELPER_COMMON_SPECIALIZATION
58+
#undef MIX_FUNCTION
59+
60+
template<typename T>
61+
struct lerp_helper<T, bool>
62+
{
63+
static inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(bool) a)
64+
{
65+
if (a)
66+
return y;
67+
else
68+
return x;
69+
}
70+
};
71+
72+
template<typename T, int N>
73+
struct lerp_helper<vector<T, N>, vector<bool, N> >
74+
{
75+
using output_vec_t = vector<T, N>;
76+
77+
static inline output_vec_t lerp(NBL_CONST_REF_ARG(output_vec_t) x, NBL_CONST_REF_ARG(output_vec_t) y, NBL_CONST_REF_ARG(vector<bool, N>) a)
78+
{
79+
output_vec_t retval;
80+
for (uint32_t i = 0; i < vector_traits<output_vec_t>::Dimension; i++)
81+
retval[i] = a[i] ? y[i] : x[i];
82+
return retval;
83+
}
84+
};
85+
86+
template<typename UnsignedInteger NBL_FUNC_REQUIRES(hlsl::is_integral_v<UnsignedInteger>&& hlsl::is_unsigned_v<UnsignedInteger>)
87+
inline bool isnan_uint_impl(UnsignedInteger val)
88+
{
89+
using AsFloat = typename float_of_size<sizeof(UnsignedInteger)>::type;
90+
return bool((ieee754::extractBiasedExponent<UnsignedInteger>(val) == ieee754::traits<AsFloat>::specialValueExp) && (val & ieee754::traits<AsFloat>::mantissaMask));
91+
}
92+
93+
template<typename UnsignedInteger NBL_FUNC_REQUIRES(hlsl::is_integral_v<UnsignedInteger>&& hlsl::is_unsigned_v<UnsignedInteger>)
94+
inline bool isinf_uint_impl(UnsignedInteger val)
95+
{
96+
using AsFloat = typename float_of_size<sizeof(UnsignedInteger)>::type;
97+
return (val & (~ieee754::traits<AsFloat>::signMask)) == ieee754::traits<AsFloat>::inf;
98+
}
99+
100+
}
101+
}
102+
}
103+
104+
#endif

include/nbl/builtin/hlsl/math/equations/quartic.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <nbl/builtin/hlsl/math/equations/quadratic.hlsl>
99
#include <nbl/builtin/hlsl/math/equations/cubic.hlsl>
10+
#include <nbl/builtin/hlsl/tgmath.hlsl>
1011

1112
// TODO: Later include from correct hlsl header
1213
#ifndef nbl_hlsl_FLT_EPSILON
@@ -88,7 +89,7 @@ namespace equations
8889
float_t3 cubic = Cubic<float_t>::construct(1, -1.0 / 2 * p, -r, 1.0 / 2 * r * p - 1.0 / 8 * q * q).computeRoots();
8990
/* ... and take the one real solution ... */
9091
for (uint32_t i = 0; i < 3; i ++)
91-
if (!isnan(cubic[i]))
92+
if (!hlsl::isnan(cubic[i]))
9293
{
9394
z = cubic[i];
9495
break;

include/nbl/builtin/hlsl/spirv_intrinsics/core.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ enable_if_t<is_floating_point_v<FloatingPoint>, bool> isInf(FloatingPoint val);
244244

245245
template<typename Matrix>
246246
[[vk::ext_instruction( spv::OpTranspose )]]
247-
Matrix transpose(NBL_CONST_REF_ARG(Matrix) mat);
247+
Matrix transpose(Matrix mat);
248248

249249
}
250250

0 commit comments

Comments
 (0)