Skip to content

Commit 277b705

Browse files
author
devsh
committed
steal _static_cast from emulated_float64_t branch
1 parent 61b8e92 commit 277b705

File tree

4 files changed

+61
-49
lines changed

4 files changed

+61
-49
lines changed

include/nbl/builtin/hlsl/cpp_compat/basic.h

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33

44
#include <nbl/builtin/hlsl/macros.h>
55

6+
namespace nbl
7+
{
8+
namespace hlsl
9+
{
10+
namespace impl
11+
{
12+
template<typename To, typename From, typename Enabled = void>
13+
struct static_cast_helper
14+
{
15+
static inline To cast(From u)
16+
{
17+
#ifndef __HLSL_VERSION
18+
return static_cast<To>(u);
19+
#else
20+
return To(u);
21+
#endif
22+
}
23+
};
24+
}
25+
26+
template<typename To, typename From>
27+
inline To _static_cast(From v)
28+
{
29+
return impl::static_cast_helper<To, From>::cast(v);
30+
}
31+
32+
}
33+
}
34+
635
#ifndef __HLSL_VERSION
736
#include <type_traits>
837

@@ -11,16 +40,11 @@
1140
#define NBL_CONSTEXPR_FUNC constexpr
1241
#define NBL_CONSTEXPR_STATIC constexpr static
1342
#define NBL_CONSTEXPR_STATIC_INLINE constexpr static inline
43+
#define NBL_CONSTEXPR_INLINE_FUNC constexpr inline
1444
#define NBL_CONST_MEMBER_FUNC const
1545

1646
namespace nbl::hlsl
1747
{
18-
template<typename T, typename U>
19-
T _static_cast(U v)
20-
{
21-
return static_cast<T>(v);
22-
}
23-
2448
template<typename T>
2549
using add_reference = std::add_lvalue_reference<T>;
2650

@@ -41,32 +65,28 @@ namespace nbl::hlsl
4165
#define NBL_CONSTEXPR_FUNC
4266
#define NBL_CONSTEXPR_STATIC const static
4367
#define NBL_CONSTEXPR_STATIC_INLINE const static
68+
#define NBL_CONSTEXPR_INLINE_FUNC inline
4469
#define NBL_CONST_MEMBER_FUNC
4570

4671
namespace nbl
4772
{
48-
namespace hlsl
49-
{
50-
template<typename T, typename U>
51-
T _static_cast(U v)
52-
{
53-
return (T)v;
54-
}
73+
namespace hlsl
74+
{
5575

5676
#if 0 // TODO: for later
57-
template<typename T>
58-
struct add_reference
59-
{
60-
using type = ref<T>;
61-
};
62-
template<typename T>
63-
struct add_pointer
64-
{
65-
using type = ptr<T>;
66-
};
77+
template<typename T>
78+
struct add_reference
79+
{
80+
using type = ref<T>;
81+
};
82+
template<typename T>
83+
struct add_pointer
84+
{
85+
using type = ptr<T>;
86+
};
6787
#endif
6888

69-
}
89+
}
7090
}
7191

7292
#define NBL_REF_ARG(...) inout __VA_ARGS__

include/nbl/builtin/hlsl/format.hlsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,12 @@ namespace impl
397397
{
398398

399399
template<format::BlockViewClass ConstexprT>
400-
struct _static_cast_helper<format::view_class_traits<format::BlockViewClass::BVC_UNKNOWN>,format::view_class_traits<ConstexprT> >
400+
struct static_cast_helper<format::view_class_traits<format::BlockViewClass::BVC_UNKNOWN>,format::view_class_traits<ConstexprT> >
401401
{
402402
using T = format::view_class_traits<format::BlockViewClass::BVC_UNKNOWN>;
403403
using U = format::view_class_traits<ConstexprT>;
404404

405-
T operator()(U val)
405+
static inline T cast(U val)
406406
{
407407
T retval;
408408
retval.Class = U::Class;
@@ -414,12 +414,12 @@ struct _static_cast_helper<format::view_class_traits<format::BlockViewClass::BVC
414414
};
415415

416416
template<format::TexelBlockFormat ConstexprT>
417-
struct _static_cast_helper<format::block_traits<format::TexelBlockFormat::TBF_UNKNOWN>,format::block_traits<ConstexprT> >
417+
struct static_cast_helper<format::block_traits<format::TexelBlockFormat::TBF_UNKNOWN>,format::block_traits<ConstexprT> >
418418
{
419419
using T = format::block_traits<format::TexelBlockFormat::TBF_UNKNOWN>;
420420
using U = format::block_traits<ConstexprT>;
421421

422-
T operator()(U val)
422+
static inline T cast(U val)
423423
{
424424
T retval;
425425
retval.ClassTraits = _static_cast<format::view_class_traits<>,U::class_traits_t>();
@@ -440,7 +440,7 @@ struct _static_cast_helper<format::block_traits<format::TexelBlockFormat::TBF_UN
440440
namespace format
441441
{
442442

443-
view_class_traits<> getTraits(const BlockViewClass _class)
443+
inline view_class_traits<> getTraits(const BlockViewClass _class)
444444
{
445445
using dynamic_t = view_class_traits<>;
446446
dynamic_t retval;

include/nbl/builtin/hlsl/format/octahedral.hlsl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ struct octahedral// : enable_if_t<Bits*2>sizeof(UintT)||Bits*4<sizeof(UintT)> ne
2020

2121
NBL_CONSTEXPR_STATIC_INLINE uint16_t BitsUsed = Bits;
2222

23-
bool operator==(const this_t other)
23+
inline bool operator==(const this_t other)
2424
{
2525
return storage==other.storage;
2626
}
27-
bool operator!=(const this_t other)
27+
inline bool operator!=(const this_t other)
2828
{
2929
return storage==other.storage;
3030
}
@@ -37,18 +37,14 @@ struct octahedral// : enable_if_t<Bits*2>sizeof(UintT)||Bits*4<sizeof(UintT)> ne
3737
// https://www.shadertoy.com/view/Mtfyzl
3838
namespace impl
3939
{
40-
// TODO: remove after the `emulated_float` merge
41-
template<typename T, typename U>
42-
struct _static_cast_helper;
43-
4440
// decode
4541
template<typename float_t, typename UintT, uint16_t Bits>
46-
struct _static_cast_helper<vector<float_t,3>,format::octahedral<UintT,Bits> >
42+
struct static_cast_helper<vector<float_t,3>,format::octahedral<UintT,Bits> >
4743
{
4844
using T = vector<float_t,3>;
4945
using U = format::octahedral<UintT,Bits>;
5046

51-
T operator()(U val)
47+
static inline T cast(U val)
5248
{
5349
using storage_t = typename U::storage_t;
5450
const storage_t MaxVal = (storage_t(1)<<U::BitsUsed)-1u;
@@ -68,12 +64,12 @@ struct _static_cast_helper<vector<float_t,3>,format::octahedral<UintT,Bits> >
6864
};
6965
// encode
7066
template<typename UintT, uint16_t Bits, typename float_t>
71-
struct _static_cast_helper<format::octahedral<UintT,Bits>,vector<float_t,3> >
67+
struct static_cast_helper<format::octahedral<UintT,Bits>,vector<float_t,3> >
7268
{
7369
using T = format::octahedral<UintT,Bits>;
7470
using U = vector<float_t,3>;
7571

76-
T operator()(U nor)
72+
static inline T cast(U nor)
7773
{
7874
nor /= (abs(nor.x) + abs(nor.y) + abs(nor.z));
7975
if (nor.z<float_t(0)) // TODO: faster sign copy

include/nbl/builtin/hlsl/format/shared_exp.hlsl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ struct shared_exp// : enable_if_t<_ExponentBits<16> need a way to static_assert
2424
// Not even going to consider fp16 and fp64 dependence on device traits
2525
using decode_t = float32_t;
2626

27-
bool operator==(const this_t other)
27+
inline bool operator==(const this_t other)
2828
{
2929
return storage==other.storage;
3030
}
31-
bool operator!=(const this_t other)
31+
inline bool operator!=(const this_t other)
3232
{
3333
return storage==other.storage;
3434
}
@@ -97,23 +97,19 @@ struct numeric_limits<format::shared_exp<IntT,_Components,_ExponentBits> > : for
9797

9898
namespace impl
9999
{
100-
// TODO: remove after the `emulated_float` merge
101-
template<typename T, typename U>
102-
struct _static_cast_helper;
103-
104100
// TODO: versions for `float16_t`
105101

106102
// decode
107103
template<typename IntT, uint16_t _Components, uint16_t _ExponentBits>
108-
struct _static_cast_helper<
104+
struct static_cast_helper<
109105
vector<typename format::shared_exp<IntT,_Components,_ExponentBits>::decode_t,_Components>,
110106
format::shared_exp<IntT,_Components,_ExponentBits>
111107
>
112108
{
113109
using U = format::shared_exp<IntT,_Components,_ExponentBits>;
114110
using T = vector<typename U::decode_t,_Components>;
115111

116-
T operator()(U val)
112+
static inline T cast(U val)
117113
{
118114
using storage_t = typename U::storage_t;
119115
// DXC error: error: expression class 'DependentScopeDeclRefExpr' unimplemented, doesn't matter as decode_t is always float32_t for now
@@ -138,15 +134,15 @@ struct _static_cast_helper<
138134
};
139135
// encode (WARNING DOES NOT CHECK THAT INPUT IS IN THE RANGE!)
140136
template<typename IntT, uint16_t _Components, uint16_t _ExponentBits>
141-
struct _static_cast_helper<
137+
struct static_cast_helper<
142138
format::shared_exp<IntT,_Components,_ExponentBits>,
143139
vector<typename format::shared_exp<IntT,_Components,_ExponentBits>::decode_t,_Components>
144140
>
145141
{
146142
using T = format::shared_exp<IntT,_Components,_ExponentBits>;
147143
using U = vector<typename T::decode_t,_Components>;
148144

149-
T operator()(U val)
145+
static inline T cast(U val)
150146
{
151147
using storage_t = typename T::storage_t;
152148
// DXC error: error: expression class 'DependentScopeDeclRefExpr' unimplemented, doesn't matter as decode_t is always float32_t for now

0 commit comments

Comments
 (0)