Skip to content

Commit 279774c

Browse files
committed
Intrinsics.hlsl refactor
1 parent 03c9042 commit 279774c

File tree

9 files changed

+132
-167
lines changed

9 files changed

+132
-167
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
55
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
66
#include <nbl/builtin/hlsl/concepts.hlsl>
7+
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
8+
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
79

810
namespace nbl
911
{
@@ -21,7 +23,7 @@ struct dot_helper
2123
static array_get<T, scalar_type> getter;
2224
scalar_type retval = getter(lhs, 0) * getter(rhs, 0);
2325

24-
static const uint32_t ArrayDim = sizeof(T) / sizeof(scalar_type);
26+
static const uint32_t ArrayDim = vector_traits<T>::Dimension;
2527
for (uint32_t i = 1; i < ArrayDim; ++i)
2628
retval = retval + getter(lhs, i) * getter(rhs, i);
2729

@@ -381,6 +383,15 @@ struct transpose_helper<matrix<T, N, M> >
381383
}
382384
};
383385

386+
template<typename LhsT, typename RhsT>
387+
struct mul_helper
388+
{
389+
static inline RhsT multiply(LhsT lhs, RhsT rhs)
390+
{
391+
return mul(lhs, rhs);
392+
}
393+
};
394+
384395
}
385396
}
386397
}

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
#include <nbl/builtin/hlsl/type_traits.hlsl>
66
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
77
#include <nbl/builtin/hlsl/array_accessors.hlsl>
8-
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
9-
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
108
#include <nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl>
119
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
10+
#include <nbl/builtin/hlsl/ieee754.hlsl>
1211

1312
#ifndef __HLSL_VERSION
1413
#include <algorithm>
@@ -128,6 +127,13 @@ inline typename matrix_traits<Matrix>::transposed_type transpose(NBL_CONST_REF_A
128127
return cpp_compat_intrinsics_impl::transpose_helper<Matrix>::transpose(m);
129128
}
130129

130+
// TODO: concepts, to ensure that MatT is a matrix and VecT is a vector type
131+
template<typename MatT, typename VecT>
132+
VecT mul(MatT mat, VecT vec)
133+
{
134+
return cpp_compat_intrinsics_impl::mul_helper<MatT, VecT>::multiply(mat, vec);
135+
}
136+
131137
template<typename T>
132138
inline T min(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
133139
{
@@ -148,8 +154,8 @@ inline T max(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
148154
#endif
149155
}
150156

151-
template<typename FloatingPoint>
152-
inline FloatingPoint isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
157+
template<typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint>)
158+
inline bool isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
153159
{
154160
#ifdef __HLSL_VERSION
155161
return spirv::isNan(val);
@@ -158,7 +164,17 @@ inline FloatingPoint isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
158164
#endif
159165
}
160166

161-
template<typename FloatingPoint>
167+
template <typename Integer NBL_FUNC_REQUIRES(hlsl::is_integral_v<Integer>)
168+
inline bool isnan(Integer val)
169+
{
170+
using AsUint = typename unsigned_integer_of_size<sizeof(Integer)>::type;
171+
using AsFloat = typename float_of_size<sizeof(Integer)>::type;
172+
173+
AsUint asUint = bit_cast<AsUint, Integer>(val);
174+
return bool((ieee754::extractBiasedExponent<Integer>(val) == ieee754::traits<AsFloat>::specialValueExp) && (asUint & ieee754::traits<AsFloat>::mantissaMask));
175+
}
176+
177+
template<typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint>)
162178
inline FloatingPoint isinf(NBL_CONST_REF_ARG(FloatingPoint) val)
163179
{
164180
#ifdef __HLSL_VERSION
@@ -168,6 +184,16 @@ inline FloatingPoint isinf(NBL_CONST_REF_ARG(FloatingPoint) val)
168184
#endif
169185
}
170186

187+
template<typename Integer NBL_FUNC_REQUIRES(hlsl::is_integral_v<Integer>)
188+
inline bool isinf(Integer val)
189+
{
190+
using AsUint = typename unsigned_integer_of_size<sizeof(Integer)>::type;
191+
using AsFloat = typename float_of_size<sizeof(Integer)>::type;
192+
193+
AsUint tmp = bit_cast<AsUint>(val);
194+
return (tmp & (~ieee754::traits<AsFloat>::signMask)) == ieee754::traits<AsFloat>::inf;
195+
}
196+
171197
template<typename T>
172198
inline T exp2(NBL_CONST_REF_ARG(T) val)
173199
{

include/nbl/builtin/hlsl/emulated/float64_t.hlsl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ namespace hlsl
9696
{
9797
if(!FastMath)
9898
{
99-
const bool isRhsInf = tgmath::isInf(rhs.data);
100-
if (tgmath::isInf(data))
99+
const bool isRhsInf = hlsl::isinf(rhs.data);
100+
if (hlsl::isinf(data))
101101
{
102102
if (isRhsInf && ((data ^ rhs.data) & ieee754::traits<float64_t>::signMask))
103103
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN);
@@ -115,7 +115,7 @@ namespace hlsl
115115

116116
if(!FastMath)
117117
{
118-
if (tgmath::isInf(data))
118+
if (hlsl::isinf(data))
119119
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | ieee754::extractSignPreserveBitPattern(max(data, rhs.data)));
120120
}
121121

@@ -222,9 +222,9 @@ namespace hlsl
222222
uint64_t sign = (data ^ rhs.data) & ieee754::traits<float64_t>::signMask;
223223
if (!FastMath)
224224
{
225-
if (tgmath::isNaN(data) || tgmath::isNaN(rhs.data))
225+
if (hlsl::isnan(data) || hlsl::isnan(rhs.data))
226226
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | sign);
227-
if (tgmath::isInf(data) || tgmath::isInf(rhs.data))
227+
if (hlsl::isinf(data) || hlsl::isinf(rhs.data))
228228
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
229229
if (emulated_float64_t_impl::isZero(data) || emulated_float64_t_impl::isZero(rhs.data))
230230
return bit_cast<this_t>(sign);
@@ -285,17 +285,17 @@ namespace hlsl
285285

286286
if(!FastMath)
287287
{
288-
if (tgmath::isNaN<uint64_t>(data) || tgmath::isNaN<uint64_t>(rhs.data))
288+
if (hlsl::isnan<uint64_t>(data) || hlsl::isnan<uint64_t>(rhs.data))
289289
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN);
290290
if (emulated_float64_t_impl::areBothZero(data, rhs.data))
291291
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | sign);
292292
if (emulated_float64_t_impl::isZero(rhs.data))
293293
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
294294
if (emulated_float64_t_impl::areBothInfinity(data, rhs.data))
295295
return bit_cast<this_t>(ieee754::traits<float64_t>::quietNaN | ieee754::traits<float64_t>::signMask);
296-
if (tgmath::isInf(data))
296+
if (hlsl::isinf(data))
297297
return bit_cast<this_t>(ieee754::traits<float64_t>::inf | sign);
298-
if (tgmath::isInf(rhs.data))
298+
if (hlsl::isinf(rhs.data))
299299
return bit_cast<this_t>(sign);
300300
}
301301

@@ -342,7 +342,7 @@ namespace hlsl
342342
{
343343
if (!FastMath)
344344
{
345-
if (tgmath::isNaN<uint64_t>(data) || tgmath::isNaN<uint64_t>(rhs.data))
345+
if (hlsl::isnan<uint64_t>(data) || hlsl::isnan<uint64_t>(rhs.data))
346346
return false;
347347
if (emulated_float64_t_impl::areBothZero(data, rhs.data))
348348
return true;
@@ -352,29 +352,29 @@ namespace hlsl
352352
}
353353
bool operator!=(emulated_float64_t rhs) NBL_CONST_MEMBER_FUNC
354354
{
355-
if (!FastMath && (tgmath::isNaN<uint64_t>(data) || tgmath::isNaN<uint64_t>(rhs.data)))
355+
if (!FastMath && (hlsl::isnan<uint64_t>(data) || hlsl::isnan<uint64_t>(rhs.data)))
356356
return false;
357357

358358
return !(bit_cast<this_t>(data) == rhs);
359359
}
360360
bool operator<(emulated_float64_t rhs) NBL_CONST_MEMBER_FUNC
361361
{
362-
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, emulated_float64_t_impl::OperatorType::LESS>(data, rhs.data);
362+
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, hlsl::less<uint64_t> >(data, rhs.data);
363363
}
364364
bool operator>(emulated_float64_t rhs) NBL_CONST_MEMBER_FUNC
365365
{
366-
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, emulated_float64_t_impl::OperatorType::GREATER>(data, rhs.data);
366+
return emulated_float64_t_impl::operatorLessAndGreaterCommonImplementation<FastMath, hlsl::greater<uint64_t> >(data, rhs.data);
367367
}
368368
bool operator<=(emulated_float64_t rhs) NBL_CONST_MEMBER_FUNC
369369
{
370-
if (!FastMath && (tgmath::isNaN<uint64_t>(data) || tgmath::isNaN<uint64_t>(rhs.data)))
370+
if (!FastMath && (hlsl::isnan<uint64_t>(data) || hlsl::isnan<uint64_t>(rhs.data)))
371371
return false;
372372

373373
return !(bit_cast<this_t>(data) > bit_cast<this_t>(rhs.data));
374374
}
375375
bool operator>=(emulated_float64_t rhs)
376376
{
377-
if (!FastMath && (tgmath::isNaN<uint64_t>(data) || tgmath::isNaN<uint64_t>(rhs.data)))
377+
if (!FastMath && (hlsl::isnan<uint64_t>(data) || hlsl::isnan<uint64_t>(rhs.data)))
378378
return false;
379379

380380
return !(bit_cast<this_t>(data) < bit_cast<this_t>(rhs.data));
@@ -494,7 +494,7 @@ struct static_cast_helper<To,emulated_float64_t<FastMath,FlushDenormToZero>,void
494494
return bit_cast<To>(ieee754::traits<ToAsFloat>::inf);
495495
if (exponent < ieee754::traits<ToAsFloat>::exponentMin)
496496
return bit_cast<To>(-ieee754::traits<ToAsFloat>::inf);
497-
if (tgmath::isNaN(v.data))
497+
if (hlsl::isnan(v.data))
498498
return bit_cast<To>(ieee754::traits<ToAsFloat>::quietNaN);
499499
}
500500

include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
55
#include <nbl/builtin/hlsl/ieee754.hlsl>
66
#include <nbl/builtin/hlsl/algorithm.hlsl>
7-
#include <nbl/builtin/hlsl/tgmath.hlsl>
87
#include <nbl/builtin/hlsl/glsl_compat/core.hlsl>
98

109
// TODO: when it will be possible, use this unions wherever they fit:
@@ -56,7 +55,7 @@ inline uint64_t castFloat32ToStorageType(float32_t val)
5655
if (FlushDenormToZero)
5756
{
5857
const uint64_t sign = uint64_t(ieee754::extractSign(val)) << 63;
59-
if (tgmath::isInf(val))
58+
if (hlsl::isinf(val))
6059
return ieee754::traits<float64_t>::inf | sign;
6160
uint32_t asUint = ieee754::impl::bitCastToUintType(val);
6261
const int f32BiasedExp = int(ieee754::extractBiasedExponent(val));
@@ -161,18 +160,12 @@ NBL_CONSTEXPR_INLINE_FUNC bool areBothSameSignZero(uint64_t lhs, uint64_t rhs)
161160
return !bool((lhs) << 1) && (lhs == rhs);
162161
}
163162

164-
enum OperatorType
165-
{
166-
LESS,
167-
GREATER
168-
};
169-
170-
template<bool FastMath, OperatorType OpType>
163+
template<bool FastMath, typename Op>
171164
NBL_CONSTEXPR_INLINE_FUNC bool operatorLessAndGreaterCommonImplementation(uint64_t lhs, uint64_t rhs)
172165
{
173166
if (!FastMath)
174167
{
175-
if (tgmath::isNaN<uint64_t>(lhs) || tgmath::isNaN<uint64_t>(rhs))
168+
if (hlsl::isnan<uint64_t>(lhs) || hlsl::isnan<uint64_t>(rhs))
176169
return false;
177170
if (emulated_float64_t_impl::areBothInfinity(lhs, rhs))
178171
{
@@ -182,26 +175,26 @@ NBL_CONSTEXPR_INLINE_FUNC bool operatorLessAndGreaterCommonImplementation(uint64
182175
if (lhsSign == rhsSign)
183176
return false;
184177

185-
if (OpType == OperatorType::LESS)
186-
return lhs < rhs;
187-
else
188-
return lhs > rhs;
178+
Op compare;
179+
return compare(lhs, rhs);
189180
}
190181
if (emulated_float64_t_impl::areBothZero(lhs, rhs))
191182
return false;
192183
}
193184

194-
const uint64_t lhsSign = ieee754::extractSign(lhs);
195-
const uint64_t rhsSign = ieee754::extractSign(rhs);
185+
const uint64_t lhsSign = ieee754::extractSignPreserveBitPattern(lhs);
186+
const uint64_t rhsSign = ieee754::extractSignPreserveBitPattern(rhs);
196187

197188
// flip bits of negative numbers and flip signs of all numbers
198-
uint64_t lhsFlipped = lhs ^ ((0x7FFFFFFFFFFFFFFFull * lhsSign) | ieee754::traits<float64_t>::signMask);
199-
uint64_t rhsFlipped = rhs ^ ((0x7FFFFFFFFFFFFFFFull * rhsSign) | ieee754::traits<float64_t>::signMask);
200-
201-
if (OpType == OperatorType::LESS)
202-
return lhsFlipped < rhsFlipped;
203-
else
204-
return lhsFlipped > rhsFlipped;
189+
if (lhsSign)
190+
lhs ^= 0x7FFFFFFFFFFFFFFFull;
191+
if (rhsSign)
192+
rhs ^= 0x7FFFFFFFFFFFFFFFull;
193+
lhs ^= ieee754::traits<float64_t>::signMask;
194+
rhs ^= ieee754::traits<float64_t>::signMask;
195+
196+
Op compare;
197+
return compare(lhs, rhs);
205198
}
206199

207200
// TODO: remove, use Newton-Raphson instead

include/nbl/builtin/hlsl/emulated/matrix_t.hlsl

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(3, 4)
6868

6969
#undef DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION
7070

71-
namespace emulated_matrix_impl
71+
namespace cpp_compat_intrinsics_impl
7272
{
73-
// TODO: move to cpp_compat/impl/intrinsics_impl.hlsl
74-
template<typename LhsT, typename RhsT>
75-
struct mul_helper
73+
template<typename T, int N, int M>
74+
struct transpose_helper<emulated_matrix<T, N, M> >
7675
{
77-
static inline RhsT multiply(LhsT lhs, RhsT rhs)
78-
{
79-
return mul(lhs, rhs);
80-
}
76+
using transposed_t = typename matrix_traits<emulated_matrix<T, N, M> >::transposed_type;
77+
78+
static transposed_t transpose(NBL_CONST_REF_ARG(emulated_matrix<T, N, M>) m)
79+
{
80+
return m.getTransposed();
81+
}
8182
};
8283

8384
template<typename ComponentT, uint16_t RowCount, uint16_t ColumnCount>
@@ -99,29 +100,6 @@ struct mul_helper<emulated_matrix<ComponentT, RowCount, ColumnCount>, emulated_v
99100
return output;
100101
}
101102
};
102-
103-
}
104-
105-
namespace cpp_compat_intrinsics_impl
106-
{
107-
template<typename T, int N, int M>
108-
struct transpose_helper<emulated_matrix<T, N, M> >
109-
{
110-
using transposed_t = typename matrix_traits<emulated_matrix<T, N, M> >::transposed_type;
111-
112-
static transposed_t transpose(NBL_CONST_REF_ARG(emulated_matrix<T, N, M>) m)
113-
{
114-
return m.getTransposed();
115-
}
116-
};
117-
}
118-
119-
// TODO: move to cpp_compat/intrinsics.hlsl
120-
// TODO: concepts, to ensure that LhsT is a matrix and RhsT is a vector type
121-
template<typename MatT, typename VecT>
122-
VecT mul(MatT mat, VecT vec)
123-
{
124-
return emulated_matrix_impl::mul_helper<MatT, VecT>::multiply(mat, vec);
125103
}
126104

127105
}

include/nbl/builtin/hlsl/glsl_compat/core.hlsl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#ifndef _NBL_BUILTIN_HLSL_GLSL_COMPAT_CORE_INCLUDED_
55
#define _NBL_BUILTIN_HLSL_GLSL_COMPAT_CORE_INCLUDED_
66

7-
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
7+
#include "nbl/builtin/hlsl/cpp_compat/basic.h"
88
#include "nbl/builtin/hlsl/spirv_intrinsics/core.hlsl"
99
#include "nbl/builtin/hlsl/type_traits.hlsl"
1010
#include "nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl"
@@ -16,12 +16,6 @@ namespace hlsl
1616
namespace glsl
1717
{
1818

19-
template<typename T>
20-
NBL_CONSTEXPR_INLINE_FUNC typename enable_if<is_fundamental<T>::value, T>::type mix(T a, T b, bool c)
21-
{
22-
return c ? b : a;
23-
}
24-
2519
#ifndef __HLSL_VERSION
2620

2721
// GLM Aliases

0 commit comments

Comments
 (0)