Skip to content

Commit 8414d65

Browse files
author
devsh
committed
provide ( a not yet enabled) fix for nbl::hlsl::bit_cast working like std::bit_cast
1 parent 0580adb commit 8414d65

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/nbl/builtin/hlsl/bit.hlsl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,33 @@ namespace nbl
3131
namespace hlsl
3232
{
3333

34+
#if 0 // enable this if you run into bit_cast not working for a non fundamental type
35+
template<class T, class U>
36+
enable_if_t<sizeof(T)==sizeof(U)&&(is_scalar_v<T>||is_vector_v<T>)==(is_scalar_v<U>||is_vector_v<U>),T> bit_cast(U val)
37+
{
38+
return spirv::bitcast<T,U>(val);
39+
}
40+
// unfortunately its impossible to deduce Storage Class right now,
41+
// also this function will only work as long as `inout` behaves as `__restrict &` in DXC
42+
template<class T, class U, uint32_t StorageClass>
43+
enable_if_t<sizeof(T)==sizeof(U),T> bit_cast(inout U val)
44+
{
45+
using ptr_u_t = spirv::pointer_t<U,StorageClass>;
46+
// get the address of U
47+
ptr_u_t ptr_u = spirv::copyObject<StorageClass,U>(val);
48+
using ptr_t_t = spirv::pointer_t<T,StorageClass>;
49+
// reinterpret cast the pointers
50+
ptr_t_t ptr_t = spirv::bitcast<ptr_t_t.ptr_u_t>(ptr_u);
51+
// actually load and return the value
52+
return spirv::load<T,ptr_t_t>(ptr_t);
53+
}
54+
#else
3455
template<class T, class U>
3556
enable_if_t<sizeof(T)==sizeof(U),T> bit_cast(U val)
3657
{
3758
return spirv::bitcast<T,U>(val);
3859
}
60+
#endif
3961

4062
template<typename T, typename S>
4163
T rotl(T x, S s);

0 commit comments

Comments
 (0)