Skip to content

Commit 41ca055

Browse files
Merge pull request #734 from Devsh-Graphics-Programming/more_bda_changes
Add some default barriers to BdaAccessor
2 parents a10ab3d + 568a4a1 commit 41ca055

File tree

8 files changed

+217
-46
lines changed

8 files changed

+217
-46
lines changed

include/nbl/builtin/hlsl/bda/bda_accessor.hlsl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,28 @@ namespace nbl
1212
namespace hlsl
1313
{
1414

15+
namespace impl {
16+
17+
struct BdaAccessorBase
18+
{
19+
// Note: Its a funny quirk of the SPIR-V Vulkan Env spec that `MemorySemanticsUniformMemoryMask` means SSBO as well :facepalm: (and probably BDA)
20+
void workgroupExecutionAndMemoryBarrier()
21+
{
22+
// we're only barriering the workgroup and trading memory within a workgroup
23+
spirv::controlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsAcquireReleaseMask | spv::MemorySemanticsUniformMemoryMask);
24+
}
25+
26+
void memoryBarrier()
27+
{
28+
// By default it's device-wide access to the buffer
29+
spirv::memoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAcquireReleaseMask | spv::MemorySemanticsUniformMemoryMask);
30+
}
31+
};
32+
33+
} //namespace impl
34+
1535
template<typename T>
16-
struct BdaAccessor
36+
struct BdaAccessor : impl::BdaAccessorBase
1737
{
1838
using type_t = T;
1939
static BdaAccessor<T> create(const bda::__ptr<T> ptr)
@@ -61,7 +81,7 @@ struct BdaAccessor
6181
};
6282

6383
template<typename T>
64-
struct DoubleBdaAccessor
84+
struct DoubleBdaAccessor : impl::BdaAccessorBase
6585
{
6686
using type_t = T;
6787
static DoubleBdaAccessor<T> create(const bda::__ptr<T> inputPtr, const bda::__ptr<T> outputPtr)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
// TODO: Remove all appearances of this class and refactor to use the real BDA once https://github.com/microsoft/DirectXShaderCompiler/issues/6541 is resolved
6+
// Then, delete this file altogether.
7+
8+
9+
#ifndef _NBL_BUILTIN_HLSL_LEGACY_BDA_ACCESSOR_INCLUDED_
10+
#define _NBL_BUILTIN_HLSL_LEGACY_BDA_ACCESSOR_INCLUDED_
11+
12+
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
13+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
14+
15+
16+
namespace nbl
17+
{
18+
namespace hlsl
19+
{
20+
21+
namespace impl {
22+
23+
struct LegacyBdaAccessorBase
24+
{
25+
// Note: Its a funny quirk of the SPIR-V Vulkan Env spec that `MemorySemanticsUniformMemoryMask` means SSBO as well :facepalm: (and probably BDA)
26+
void workgroupExecutionAndMemoryBarrier()
27+
{
28+
// we're only barriering the workgroup and trading memory within a workgroup
29+
spirv::controlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsAcquireReleaseMask | spv::MemorySemanticsUniformMemoryMask);
30+
}
31+
32+
void memoryBarrier()
33+
{
34+
// By default it's device-wide access to the buffer
35+
spirv::memoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAcquireReleaseMask | spv::MemorySemanticsUniformMemoryMask);
36+
}
37+
};
38+
39+
} //namespace impl
40+
41+
template<typename T>
42+
struct LegacyBdaAccessor : impl::LegacyBdaAccessorBase
43+
{
44+
using type_t = T;
45+
static LegacyBdaAccessor<T> create(const uint64_t address)
46+
{
47+
LegacyBdaAccessor<T> accessor;
48+
accessor.address = address;
49+
return accessor;
50+
}
51+
52+
T get(const uint64_t index)
53+
{
54+
return vk::RawBufferLoad<T>(address + index * sizeof(T));
55+
}
56+
57+
void get(const uint64_t index, NBL_REF_ARG(T) value)
58+
{
59+
value = vk::RawBufferLoad<T>(address + index * sizeof(T));
60+
}
61+
62+
void set(const uint64_t index, const T value)
63+
{
64+
vk::RawBufferStore<T>(address + index * sizeof(T), value);
65+
}
66+
67+
uint64_t address;
68+
};
69+
70+
template<typename T>
71+
struct DoubleLegacyBdaAccessor : impl::LegacyBdaAccessorBase
72+
{
73+
using type_t = T;
74+
static DoubleLegacyBdaAccessor<T> create(const uint64_t inputAddress, const uint64_t outputAddress)
75+
{
76+
DoubleLegacyBdaAccessor<T> accessor;
77+
accessor.inputAddress = inputAddress;
78+
accessor.outputAddress = outputAddress;
79+
return accessor;
80+
}
81+
82+
T get(const uint64_t index)
83+
{
84+
return vk::RawBufferLoad<T>(inputAddress + index * sizeof(T));
85+
}
86+
87+
void get(const uint64_t index, NBL_REF_ARG(T) value)
88+
{
89+
value = vk::RawBufferLoad<T>(inputAddress + index * sizeof(T));
90+
}
91+
92+
void set(const uint64_t index, const T value)
93+
{
94+
vk::RawBufferStore<T>(outputAddress + index * sizeof(T), value);
95+
}
96+
97+
uint64_t inputAddress, outputAddress;
98+
};
99+
100+
101+
}
102+
}
103+
104+
#endif

include/nbl/builtin/hlsl/complex.hlsl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,23 @@ NBL_REGISTER_OBJ_TYPE(complex_t<float64_t2>,::nbl::hlsl::alignment_of_v<float64_
378378
NBL_REGISTER_OBJ_TYPE(complex_t<float64_t3>,::nbl::hlsl::alignment_of_v<float64_t3>)
379379
NBL_REGISTER_OBJ_TYPE(complex_t<float64_t4>,::nbl::hlsl::alignment_of_v<float64_t4>)
380380

381+
382+
383+
--------------------------------------------- Some more functions that come in handy --------------------------------------
384+
// Fast mul by i
385+
template<typename Scalar>
386+
complex_t<Scalar> rotateLeft(NBL_CONST_REF_ARG(complex_t<Scalar>) value)
387+
{
388+
complex_t<Scalar> retVal = {- value.imag(), value.real()};
389+
return retVal;
390+
}
391+
392+
// Fast mul by -i
393+
template<typename Scalar>
394+
complex_t<Scalar> rotateRight(NBL_CONST_REF_ARG(complex_t<Scalar>) value)
395+
{
396+
complex_t<Scalar> retVal = {value.imag(), -value.real()};
397+
return retVal;
398+
}
399+
381400
#endif

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,13 @@ T bitfieldExtract( T val, uint32_t offsetBits, uint32_t numBits )
208208
return impl::bitfieldExtract<T, is_signed<T>::value, is_integral<T>::value>::__call(val,offsetBits,numBits);
209209
}
210210

211-
212211
namespace impl
213212
{
214213

215214
template<typename T>
216215
struct bitfieldInsert
217216
{
218-
static enable_if_t<is_integral_v<T>, T> __call( T base, T insert, uint32_t offset, uint32_t count )
217+
static T __call( T base, T insert, uint32_t offset, uint32_t count )
219218
{
220219
return spirv::bitFieldInsert<T>( base, insert, offset, count );
221220
}
@@ -224,9 +223,29 @@ struct bitfieldInsert
224223
} //namespace impl
225224

226225
template<typename T>
227-
T bitfieldInsert( T base, T insert, uint32_t offset, uint32_t count )
226+
T bitfieldInsert( T base, T insert, uint32_t offset, uint32_t bits )
227+
{
228+
return impl::bitfieldInsert<T>::__call(base, insert, offset, bits);
229+
}
230+
231+
namespace impl
232+
{
233+
234+
template<typename T>
235+
struct bitfieldReverse
236+
{
237+
static T __call( T base )
238+
{
239+
return spirv::bitFieldReverse<T>( base );
240+
}
241+
};
242+
243+
} //namespace impl
244+
245+
template<typename T>
246+
T bitfieldReverse( T value )
228247
{
229-
return impl::bitfieldInsert<T>::__call(base, insert, offset, count);
248+
return impl::bitfieldReverse<T>::__call(value);
230249
}
231250

232251
#endif

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ enable_if_t<is_signed_v<Signed>, Signed> bitFieldSExtract( Signed val, uint32_t
234234

235235
template<typename Integral>
236236
[[vk::ext_instruction( spv::OpBitFieldInsert )]]
237-
Integral bitFieldInsert( Integral base, Integral insert, uint32_t offset, uint32_t count );
237+
enable_if_t<is_integral_v<Integral>, Integral> bitFieldInsert( Integral base, Integral insert, uint32_t offset, uint32_t count );
238+
239+
template<typename Integral>
240+
[[vk::ext_instruction( spv::OpBitReverse )]]
241+
enable_if_t<is_integral_v<Integral>, Integral> bitFieldReverse( Integral base );
238242

239243
}
240244

include/nbl/builtin/hlsl/type_traits.hlsl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ template<class T>
306306
NBL_CONSTEXPR_STATIC_INLINE bool is_unsigned_v = is_unsigned<T>::value;
307307

308308
template<class T>
309-
struct is_integral : impl::base_type_forwarder<impl::is_integral, T> {};
309+
struct is_integral : impl::base_type_forwarder<impl::is_integral, typename remove_cv<T>::type> {};
310+
template<class T>
311+
NBL_CONSTEXPR_STATIC_INLINE bool is_integral_v = is_integral<T>::value;
310312

311313
template<class T>
312314
struct is_floating_point : impl::base_type_forwarder<impl::is_floating_point, typename remove_cv<T>::type> {};
@@ -321,6 +323,8 @@ struct is_scalar : bool_constant<
321323
impl::is_integral<typename remove_cv<T>::type>::value ||
322324
impl::is_floating_point<typename remove_cv<T>::type>::value
323325
> {};
326+
template<class T>
327+
NBL_CONSTEXPR_STATIC_INLINE bool is_scalar_v = is_scalar<T>::value;
324328

325329
template<class T>
326330
struct is_const : bool_constant<false> {};
@@ -395,12 +399,6 @@ struct enable_if<true, T> : type_identity<T> {};
395399
template<bool B, class T = void>
396400
using enable_if_t = typename enable_if<B, T>::type;
397401

398-
template<class T>
399-
NBL_CONSTEXPR_STATIC_INLINE bool is_integral_v = is_integral<T>::value;
400-
401-
template<class T>
402-
NBL_CONSTEXPR_STATIC_INLINE bool is_scalar_v = is_scalar<T>::value;
403-
404402
template<class T>
405403
struct alignment_of;
406404
template<class T>

0 commit comments

Comments
 (0)