Skip to content

Commit f0c217f

Browse files
committed
Resolved conflicts, merged cpp_intrinsics_improvements
2 parents 14e1010 + 3f1db30 commit f0c217f

File tree

13 files changed

+412
-98
lines changed

13 files changed

+412
-98
lines changed

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

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
#define _NBL_BUILTIN_HLSL_CPP_COMPAT_IMPL_INTRINSICS_IMPL_INCLUDED_
33

44
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
5+
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
6+
#include <nbl/builtin/hlsl/matrix_utils/mul_output_t.hlsl>
57
#include <nbl/builtin/hlsl/concepts.hlsl>
8+
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
9+
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
10+
#include <nbl/builtin/hlsl/ieee754.hlsl>
611

712
namespace nbl
813
{
@@ -20,7 +25,7 @@ struct dot_helper
2025
static array_get<T, scalar_type> getter;
2126
scalar_type retval = getter(lhs, 0) * getter(rhs, 0);
2227

23-
static const uint32_t ArrayDim = sizeof(T) / sizeof(scalar_type);
28+
static const uint32_t ArrayDim = vector_traits<T>::Dimension;
2429
for (uint32_t i = 1; i < ArrayDim; ++i)
2530
retval = retval + getter(lhs, i) * getter(rhs, i);
2631

@@ -293,72 +298,30 @@ struct find_msb_return_type<vector<Integer, N> >
293298
template<typename Integer>
294299
using find_lsb_return_type = find_msb_return_type<Integer>;
295300

296-
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
297-
struct lerp_helper;
301+
template<typename Matrix>
302+
struct transpose_helper;
298303

304+
template<typename T, int N, int M>
305+
struct transpose_helper<matrix<T, N, M> >
306+
{
307+
using transposed_t = typename matrix_traits<matrix<T, N, M> >::transposed_type;
308+
309+
static transposed_t transpose(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
310+
{
299311
#ifdef __HLSL_VERSION
300-
#define MIX_FUNCTION spirv::fMix
312+
return spirv::transpose(m);
301313
#else
302-
#define MIX_FUNCTION glm::mix
314+
return reinterpret_cast<transposed_t&>(glm::transpose(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m)));
303315
#endif
304-
305-
#define DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(TYPE)\
306-
template<>\
307-
struct lerp_helper<TYPE, TYPE>\
308-
{\
309-
static inline TYPE lerp(NBL_CONST_REF_ARG(TYPE) x, NBL_CONST_REF_ARG(TYPE) y, NBL_CONST_REF_ARG(TYPE) a)\
310-
{\
311-
return MIX_FUNCTION(x, y, a);\
312-
}\
313-
};\
314-
\
315-
template<int N>\
316-
struct lerp_helper<vector<TYPE, N>, vector<TYPE, N> >\
317-
{\
318-
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)\
319-
{\
320-
return MIX_FUNCTION(x, y, a);\
321-
}\
322-
};\
323-
\
324-
template<int N>\
325-
struct lerp_helper<vector<TYPE, N>, TYPE>\
326-
{\
327-
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)\
328-
{\
329-
return MIX_FUNCTION(x, y, a);\
330-
}\
331-
};\
332-
333-
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float32_t)
334-
DEFINE_LERP_HELPER_COMMON_SPECIALIZATION(float64_t)
335-
336-
#undef DEFINE_LERP_HELPER_COMMON_SPECIALIZATION
337-
#undef MIX_FUNCTION
338-
339-
template<typename T>
340-
struct lerp_helper<T, bool>
341-
{
342-
static inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(bool) a)
343-
{
344-
if (a)
345-
return y;
346-
else
347-
return x;
348316
}
349317
};
350318

351-
template<typename T, int N>
352-
struct lerp_helper<vector<T, N>, vector<bool, N> >
319+
template<typename LhsT, typename RhsT>
320+
struct mul_helper
353321
{
354-
using output_vec_t = vector<T, N>;
355-
356-
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)
322+
static inline mul_output_t<LhsT, RhsT> multiply(LhsT lhs, RhsT rhs)
357323
{
358-
output_vec_t retval;
359-
for (uint32_t i = 0; i < vector_traits<output_vec_t>::Dimension; i++)
360-
retval[i] = a[i] ? y[i] : x[i];
361-
return retval;
324+
return mul(lhs, rhs);
362325
}
363326
};
364327

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
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>
9+
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
1110

1211
#ifndef __HLSL_VERSION
1312
#include <algorithm>
@@ -103,14 +102,17 @@ inline matrix<T, N, N> inverse(NBL_CONST_REF_ARG(matrix<T, N, N>) m)
103102
}
104103

105104
// transpose not defined cause its implemented via hidden friend
106-
template<typename T, uint16_t N, uint16_t M>
107-
inline matrix<T, M, N> transpose(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
105+
template<typename Matrix>
106+
inline typename matrix_traits<Matrix>::transposed_type transpose(NBL_CONST_REF_ARG(Matrix) m)
108107
{
109-
#ifdef __HLSL_VERSION
110-
return spirv::transpose(m);
111-
#else
112-
return reinterpret_cast<matrix<T, M, N>&>(glm::transpose(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m)));
113-
#endif
108+
return cpp_compat_intrinsics_impl::transpose_helper<Matrix>::transpose(m);
109+
}
110+
111+
// TODO: concepts, to ensure that MatT is a matrix and VecT is a vector type
112+
template<typename LhsT, typename RhsT>
113+
mul_output_t<LhsT, RhsT> mul(LhsT mat, RhsT vec)
114+
{
115+
return cpp_compat_intrinsics_impl::mul_helper<LhsT, RhsT>::multiply(mat, vec);
114116
}
115117

116118
template<typename T>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
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"
10+
#include "nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl"
1011

1112
namespace nbl
1213
{

include/nbl/builtin/hlsl/ieee754.hlsl

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#ifndef _NBL_BUILTIN_HLSL_IEE754_HLSL_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_IEE754_HLSL_INCLUDED_
3+
4+
#include <nbl/builtin/hlsl/ieee754/impl.hlsl>
5+
6+
namespace nbl
7+
{
8+
namespace hlsl
9+
{
10+
namespace ieee754
11+
{
12+
13+
template<typename Float>
14+
struct traits_base
15+
{
16+
static_assert(is_same<Float, float16_t>::value || is_same<Float, float32_t>::value || is_same<Float, float64_t>::value);
17+
NBL_CONSTEXPR_STATIC_INLINE int16_t exponentBitCnt = int16_t(0xbeef);
18+
NBL_CONSTEXPR_STATIC_INLINE int16_t mantissaBitCnt = int16_t(0xbeef);
19+
};
20+
21+
template<>
22+
struct traits_base<float16_t>
23+
{
24+
NBL_CONSTEXPR_STATIC_INLINE int16_t exponentBitCnt = 5;
25+
NBL_CONSTEXPR_STATIC_INLINE int16_t mantissaBitCnt = 10;
26+
};
27+
28+
template<>
29+
struct traits_base<float32_t>
30+
{
31+
NBL_CONSTEXPR_STATIC_INLINE int16_t exponentBitCnt = 8;
32+
NBL_CONSTEXPR_STATIC_INLINE int16_t mantissaBitCnt = 23;
33+
};
34+
35+
template<>
36+
struct traits_base<float64_t>
37+
{
38+
NBL_CONSTEXPR_STATIC_INLINE int16_t exponentBitCnt = 11;
39+
NBL_CONSTEXPR_STATIC_INLINE int16_t mantissaBitCnt = 52;
40+
};
41+
42+
template<typename Float>
43+
struct traits : traits_base<Float>
44+
{
45+
//static_assert(is_same_v<Float, float16_t> || is_same_v<Float, float32_t> || is_same_v<Float, float64_t>);
46+
47+
using bit_rep_t = typename unsigned_integer_of_size<sizeof(Float)>::type;
48+
using base_t = traits_base<Float>;
49+
50+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t signMask = bit_rep_t(0x1ull) << (sizeof(Float) * 8 - 1);
51+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t exponentMask = ((~bit_rep_t(0)) << base_t::mantissaBitCnt) ^ signMask;
52+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t mantissaMask = (bit_rep_t(0x1u) << base_t::mantissaBitCnt) - 1;
53+
NBL_CONSTEXPR_STATIC_INLINE int exponentBias = (int(0x1) << (base_t::exponentBitCnt - 1)) - 1;
54+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t inf = exponentMask;
55+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t specialValueExp = (1ull << base_t::exponentBitCnt) - 1;
56+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t quietNaN = exponentMask | (1ull << (base_t::mantissaBitCnt - 1));
57+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t max = ((1ull << (sizeof(Float) * 8 - 1)) - 1) & (~(1ull << base_t::mantissaBitCnt));
58+
NBL_CONSTEXPR_STATIC_INLINE bit_rep_t min = 1ull << base_t::mantissaBitCnt;
59+
NBL_CONSTEXPR_STATIC_INLINE int exponentMax = exponentBias;
60+
NBL_CONSTEXPR_STATIC_INLINE int exponentMin = -(exponentBias - 1);
61+
};
62+
63+
template <typename T>
64+
inline uint32_t extractBiasedExponent(T x)
65+
{
66+
using AsUint = typename unsigned_integer_of_size<sizeof(T)>::type;
67+
return glsl::bitfieldExtract<AsUint>(ieee754::impl::bitCastToUintType(x), traits<typename float_of_size<sizeof(T)>::type>::mantissaBitCnt, traits<typename float_of_size<sizeof(T)>::type>::exponentBitCnt);
68+
}
69+
70+
template<>
71+
inline uint32_t extractBiasedExponent(uint64_t x)
72+
{
73+
uint64_t output = (x >> traits<float64_t>::mantissaBitCnt) & (traits<float64_t>::exponentMask >> traits<float64_t>::mantissaBitCnt);
74+
return uint32_t(output);
75+
}
76+
77+
template<>
78+
inline uint32_t extractBiasedExponent(float64_t x)
79+
{
80+
return extractBiasedExponent<uint64_t>(ieee754::impl::bitCastToUintType(x));
81+
}
82+
83+
template <typename T>
84+
inline int extractExponent(T x)
85+
{
86+
using AsFloat = typename float_of_size<sizeof(T)>::type;
87+
return int(extractBiasedExponent(x)) - traits<AsFloat>::exponentBias;
88+
}
89+
90+
template <typename T>
91+
NBL_CONSTEXPR_INLINE_FUNC T replaceBiasedExponent(T x, typename unsigned_integer_of_size<sizeof(T)>::type biasedExp)
92+
{
93+
using AsFloat = typename float_of_size<sizeof(T)>::type;
94+
return impl::castBackToFloatType<T>(glsl::bitfieldInsert(ieee754::impl::bitCastToUintType(x), biasedExp, traits<AsFloat>::mantissaBitCnt, traits<AsFloat>::exponentBitCnt));
95+
}
96+
97+
// performs no overflow tests, returns x*exp2(n)
98+
template <typename T>
99+
NBL_CONSTEXPR_INLINE_FUNC T fastMulExp2(T x, int n)
100+
{
101+
return replaceBiasedExponent(x, extractBiasedExponent(x) + uint32_t(n));
102+
}
103+
104+
template <typename T>
105+
NBL_CONSTEXPR_INLINE_FUNC typename unsigned_integer_of_size<sizeof(T)>::type extractMantissa(T x)
106+
{
107+
using AsUint = typename unsigned_integer_of_size<sizeof(T)>::type;
108+
return ieee754::impl::bitCastToUintType(x) & traits<typename float_of_size<sizeof(T)>::type>::mantissaMask;
109+
}
110+
111+
template <typename T>
112+
NBL_CONSTEXPR_INLINE_FUNC typename unsigned_integer_of_size<sizeof(T)>::type extractNormalizeMantissa(T x)
113+
{
114+
using AsUint = typename unsigned_integer_of_size<sizeof(T)>::type;
115+
using AsFloat = typename float_of_size<sizeof(T)>::type;
116+
return extractMantissa(x) | (AsUint(1) << traits<AsFloat>::mantissaBitCnt);
117+
}
118+
119+
template <typename T>
120+
NBL_CONSTEXPR_INLINE_FUNC typename unsigned_integer_of_size<sizeof(T)>::type extractSign(T x)
121+
{
122+
using AsFloat = typename float_of_size<sizeof(T)>::type;
123+
return (ieee754::impl::bitCastToUintType(x) & traits<AsFloat>::signMask) >> ((sizeof(T) * 8) - 1);
124+
}
125+
126+
template <typename T>
127+
NBL_CONSTEXPR_INLINE_FUNC typename unsigned_integer_of_size<sizeof(T)>::type extractSignPreserveBitPattern(T x)
128+
{
129+
using AsFloat = typename float_of_size<sizeof(T)>::type;
130+
return ieee754::impl::bitCastToUintType(x) & traits<AsFloat>::signMask;
131+
}
132+
133+
}
134+
}
135+
}
136+
137+
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef _NBL_BUILTIN_HLSL_IEE754_IMPL_HLSL_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_IEE754_IMPL_HLSL_INCLUDED_
3+
4+
#include <nbl/builtin/hlsl/type_traits.hlsl>
5+
#include <nbl/builtin/hlsl/glsl_compat/core.hlsl>
6+
#include <nbl/builtin/hlsl/bit.hlsl>
7+
8+
namespace nbl
9+
{
10+
namespace hlsl
11+
{
12+
namespace ieee754
13+
{
14+
15+
namespace impl
16+
{
17+
template <typename T>
18+
NBL_CONSTEXPR_INLINE_FUNC unsigned_integer_of_size_t<sizeof(T)> bitCastToUintType(T x)
19+
{
20+
using AsUint = unsigned_integer_of_size_t<sizeof(T)>;
21+
return bit_cast<AsUint, T>(x);
22+
}
23+
// to avoid bit cast from uintN_t to uintN_t
24+
template <> NBL_CONSTEXPR_INLINE_FUNC unsigned_integer_of_size_t<2> bitCastToUintType(uint16_t x) { return x; }
25+
template <> NBL_CONSTEXPR_INLINE_FUNC unsigned_integer_of_size_t<4> bitCastToUintType(uint32_t x) { return x; }
26+
template <> NBL_CONSTEXPR_INLINE_FUNC unsigned_integer_of_size_t<8> bitCastToUintType(uint64_t x) { return x; }
27+
28+
template <typename T>
29+
NBL_CONSTEXPR_INLINE_FUNC T castBackToFloatType(T x)
30+
{
31+
using AsFloat = typename float_of_size<sizeof(T)>::type;
32+
return bit_cast<AsFloat, T>(x);
33+
}
34+
template<> NBL_CONSTEXPR_INLINE_FUNC uint16_t castBackToFloatType(uint16_t x) { return x; }
35+
template<> NBL_CONSTEXPR_INLINE_FUNC uint32_t castBackToFloatType(uint32_t x) { return x; }
36+
template<> NBL_CONSTEXPR_INLINE_FUNC uint64_t castBackToFloatType(uint64_t x) { return x; }
37+
}
38+
39+
}
40+
}
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)