Skip to content

Commit b5194ef

Browse files
committed
Merge branch 'master' into hlsl_path_tracer
2 parents 96c7497 + 91dc3af commit b5194ef

File tree

13 files changed

+179
-98
lines changed

13 files changed

+179
-98
lines changed

05_StreamingAndBufferDeviceAddressApp/app_resources/common.hlsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
22

3-
// Unfortunately not every piece of C++14 metaprogramming syntax is available in HLSL 202x
4-
// https://github.com/microsoft/DirectXShaderCompiler/issues/5751#issuecomment-1800847954
5-
typedef nbl::hlsl::float32_t3 input_t;
6-
typedef nbl::hlsl::float32_t output_t;
3+
//
4+
using input_t = nbl::hlsl::float32_t3;
5+
using output_t = nbl::hlsl::float32_t;
76

87
NBL_CONSTEXPR_STATIC_INLINE uint32_t MaxPossibleElementCount = 1 << 20;
98

11_FFT/app_resources/shader.comp.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct Accessor
4343
return accessor;
4444
}
4545

46+
// TODO: can't use our own BDA yet, because it doesn't support the types `workgroup::FFT` will invoke these templates with
4647
template <typename AccessType>
4748
void get(const uint32_t index, NBL_REF_ARG(AccessType) value)
4849
{

21_LRUCacheUnitTest/main.cpp

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
3333
inline TextureReference& operator=(uint64_t semamphoreVal) { lastUsedSemaphoreValue = semamphoreVal; return *this; }
3434
};
3535

36-
using TextureLRUCache = core::LRUCache<uint32_t, TextureReference>;
36+
using TextureLRUCache = core::ResizableLRUCache<uint32_t, TextureReference>;
3737

3838
// we stuff all our work here because its a "single shot" app
3939
bool onAppInitialized(smart_refctd_ptr<ISystem>&& system) override
@@ -44,12 +44,18 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
4444

4545
m_logger->log("LRU cache unit test");
4646
m_logger->log("Testing large cache...");
47-
LRUCache<int, char> hugeCache(50000000u);
47+
ResizableLRUCache<int, char> hugeCache(50000000u);
4848
hugeCache.insert(0, '0');
4949
hugeCache.print(m_logger);
5050

51-
52-
LRUCache<int, char> cache(5u);
51+
// Use this to ensure the disposal function is properly called on every element of the first cache
52+
ResizableLRUCache<int, char>::disposal_func_t df([&](std::pair<int, char> a)
53+
{
54+
std::ostringstream tmp;
55+
tmp << "Disposal function called on element (" << a.first << ", " << a.second << ")";
56+
m_logger->log(tmp.str());
57+
});
58+
ResizableLRUCache<int, char> cache(5u, std::move(df));
5359

5460
m_logger->log("Testing insert with const key, const val...");
5561
//const, const
@@ -80,8 +86,11 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
8086
//non const, const
8187
int i = 0;
8288
cache.insert(++i, '1');
89+
assert(cache.getState() == "{12, e}, {10, c}, {11, d}, {13, f}, {1, 1}");
8390
cache.insert(++i, '2');
91+
assert(cache.getState() == "{10, c}, {11, d}, {13, f}, {1, 1}, {2, 2}");
8492
cache.insert(++i, '3');
93+
assert(cache.getState() == "{11, d}, {13, f}, {1, 1}, {2, 2}, {3, 3}");
8594
returned = *(cache.get(1));
8695
assert(returned == '1');
8796

@@ -111,7 +120,17 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
111120
auto peekedNullptr = cache.peek(5);
112121
assert(peekedNullptr == nullptr);
113122

114-
core::LRUCache<int, std::string> cache2(5u);
123+
// Try clearing the cache
124+
m_logger->log("Clearing test");
125+
m_logger->log("Print contents before clearing");
126+
cache.print(m_logger);
127+
assert(cache.getState() == "{2, 2}, {3, 3}, {1, 1}, {4, N}, {6, Y}");
128+
cache.clear();
129+
m_logger->log("Print contents after clearing");
130+
cache.print(m_logger);
131+
assert(cache.getState() == "");
132+
133+
ResizableLRUCache<int, std::string> cache2(5u);
115134

116135
cache2.insert(500, "five hundred"); //inserts at addr = 0
117136
cache2.insert(510, "five hundred and ten"); //inserts at addr = 472
@@ -124,6 +143,82 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
124143
cache2.print(m_logger);
125144
cache2.insert(++i, "key is 112");
126145

146+
// Grow test - try growing the cache
147+
m_logger->log("Growing test");
148+
auto previousState = cache2.getState();
149+
// Grow cache
150+
assert(cache2.grow(10));
151+
// Cache state should be the same
152+
assert(cache2.getState() == previousState);
153+
cache2.print(m_logger);
154+
cache2.insert(++i, "key is 113");
155+
cache2.insert(++i, "key is 114");
156+
cache2.insert(++i, "key is 115");
157+
cache2.insert(++i, "key is 116");
158+
cache2.insert(++i, "key is 117");
159+
cache2.print(m_logger);
160+
// Should evict key 52
161+
cache2.insert(++i, "key is 118");
162+
cache2.print(m_logger);
163+
const auto latestState = cache2.getState();
164+
assert(latestState == "{21, key is 21}, {22, key is 22}, {23, key is 23}, {112, key is 112}, {113, key is 113}, {114, key is 114}, {115, key is 115}, {116, key is 116}, {117, key is 117}, {118, key is 118}");
165+
// Invalid grow should fail
166+
assert(!cache2.grow(5));
167+
assert(!cache2.grow(10));
168+
// Call a bunch of grows that shouldn't fail and some others that should
169+
for (auto i = 1u; i < 50; i++)
170+
{
171+
assert(cache2.grow(50 * i));
172+
assert(!cache2.grow(25 * i));
173+
assert(!cache2.grow(50 * i));
174+
assert(cache2.getState() == latestState);
175+
}
176+
177+
// Single element cache test - checking for edge cases
178+
ResizableLRUCache<int, std::string> cache3(1u);
179+
cache3.insert(0, "foo");
180+
cache3.insert(1, "bar");
181+
cache3.clear();
182+
183+
// Besides the disposal function that gets called when evicting, we need to check that the Cache properly destroys all resident `Key,Value` pairs when destroyed
184+
struct Foo
185+
{
186+
int* destroyCounter;
187+
188+
Foo(int* _destroyCounter) : destroyCounter(_destroyCounter){}
189+
190+
void operator=(Foo&& other)
191+
{
192+
destroyCounter = other.destroyCounter;
193+
other.destroyCounter = nullptr;
194+
}
195+
196+
Foo(Foo&& other)
197+
{
198+
operator=(std::move(other));
199+
}
200+
201+
~Foo()
202+
{
203+
// Only count destructions of objects resident in Cache and not ones that happen right after moving out of
204+
if (destroyCounter)
205+
(*destroyCounter)++;
206+
}
207+
};
208+
209+
int destroyCounter = 0;
210+
{
211+
ResizableLRUCache<int, Foo> cache4(10u);
212+
for (int i = 0; i < 10; i++)
213+
cache4.insert(i, Foo(&destroyCounter));
214+
int x = 0;
215+
}
216+
217+
assert(destroyCounter == 10);
218+
219+
220+
m_logger->log("all good");
221+
127222
m_textureLRUCache = std::unique_ptr<TextureLRUCache>(new TextureLRUCache(1024u));
128223
{
129224
SIntendedSubmitInfo intendedNextSubmit = {};
@@ -142,11 +237,6 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
142237
TextureReference* inserted = m_textureLRUCache->insert(69420, nextSemaSignal.value, evictionCallback);
143238
}
144239

145-
#ifdef _NBL_DEBUG
146-
cache2.print(m_logger);
147-
#endif
148-
m_logger->log("all good");
149-
150240
constexpr uint32_t InvalidIdx = ~0u;
151241
struct TextureReference
152242
{
@@ -168,7 +258,7 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
168258
// In LRU Cache `insert` function, in case of cache hit, we need to assign semaphore value to TextureReference without changing `alloc_idx`
169259
inline TextureReference& operator=(uint64_t semamphoreVal) { lastUsedSemaphoreValue = semamphoreVal; return *this; }
170260
};
171-
using TextureLRUCache = LRUCache<uint32_t, TextureReference>;
261+
using TextureLRUCache = ResizableLRUCache<uint32_t, TextureReference>;
172262

173263
TextureLRUCache textureCache = TextureLRUCache(3u);
174264

22_CppCompat/app_resources/common.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define _NBL_EXAMPLES_TESTS_22_CPP_COMPAT_COMMON_INCLUDED_
77

88
// because DXC doesn't properly support `_Static_assert`
9+
// TODO: add a message, and move to macros.h or cpp_compat
910
#define STATIC_ASSERT(...) { nbl::hlsl::conditional<__VA_ARGS__, int, void>::type a = 0; }
1011

1112
#include <boost/preprocessor.hpp>

22_CppCompat/app_resources/test.comp.hlsl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33
//// For conditions of distribution and use, see copyright notice in nabla.h
44
#include "app_resources/common.hlsl"
55

6-
#define SHADER_CRASHING_ASSERT(expr) \
7-
{ \
8-
bool con = (expr); \
9-
do { \
10-
[branch] if (!con) \
11-
vk::RawBufferStore<uint32_t>(0xdeadbeefBADC0FFbull,0x45u,4u); \
12-
} while(!con); \
13-
}
14-
156
template<typename L, typename R>
167
const static bool is_same_v = nbl::hlsl::is_same_v<L,R>;
178

@@ -155,10 +146,10 @@ void main(uint3 invocationID : SV_DispatchThreadID)
155146
// uint32_t rotrResult0 = nbl::hlsl::mpl::rotr<uint32_t, 2u, 1>::value;
156147
// uint32_t rotrResult1 = nbl::hlsl::mpl::rotr<uint32_t, 2u, -1>::value;
157148

158-
// SHADER_CRASHING_ASSERT(rotlResult0 == mplRotlResult0);
159-
// SHADER_CRASHING_ASSERT(rotlResult1 == mplRotlResult1);
160-
// SHADER_CRASHING_ASSERT(rotrResult0 == mplRotrResult0);
161-
// SHADER_CRASHING_ASSERT(rotrResult1 == mplRotrResult1);
149+
// assert(rotlResult0 == mplRotlResult0);
150+
// assert(rotlResult1 == mplRotlResult1);
151+
// assert(rotrResult0 == mplRotrResult0);
152+
// assert(rotrResult1 == mplRotrResult1);
162153

163154
// TODO: more tests and compare with cpp version as well
164155
fill(invocationID, 5);
@@ -175,27 +166,27 @@ void main(uint3 invocationID : SV_DispatchThreadID)
175166
uint16_t compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint16_t, TEST_VALUE_0>::value;
176167
uint16_t runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_0);
177168
fill(invocationID, float4(5.1, compileTimeCountLZero, runTimeCountLZero, 0));
178-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
169+
assert(compileTimeCountLZero == runTimeCountLZero);
179170

180171
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_1>::value;
181172
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_1);
182173
fill(invocationID, float4(5.2, compileTimeCountLZero, runTimeCountLZero, 0));
183-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
174+
assert(compileTimeCountLZero == runTimeCountLZero);
184175

185176
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_2>::value;
186177
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_2);
187178
fill(invocationID, float4(5.3, compileTimeCountLZero, runTimeCountLZero, 0));
188-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
179+
assert(compileTimeCountLZero == runTimeCountLZero);
189180

190181
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_3>::value;
191182
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_3);
192183
fill(invocationID, float4(5.4, compileTimeCountLZero, runTimeCountLZero, 0));
193-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
184+
assert(compileTimeCountLZero == runTimeCountLZero);
194185

195186
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_4>::value;
196187
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_4);
197188
fill(invocationID, float4(5.5, compileTimeCountLZero, runTimeCountLZero, 0));
198-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
189+
assert(compileTimeCountLZero == runTimeCountLZero);
199190
}
200191

201192
{
@@ -211,7 +202,7 @@ void main(uint3 invocationID : SV_DispatchThreadID)
211202
{
212203
float4 v;
213204
fill(invocationID, float4(alignof(v.x), alignof(v), 0, 0));
214-
SHADER_CRASHING_ASSERT(alignof(v.x) == alignof(v));
205+
assert(alignof(v.x) == alignof(v));
215206
}
216207

217208
{

22_CppCompat/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ void cpu_tests()
686686

687687
// countl_zero test
688688
mpl::countl_zero<uint32_t, 5>::value;
689+
// TODO: fix warning about nodiscard
689690
std::countl_zero(5u);
690691
nbl::hlsl::countl_zero(5u);
691692

22_CppCompat/test.hlsl.orig

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
#define STATIC_ASSERT(C) { nbl::hlsl::conditional<C, int, void>::type a = 0; }
88

99
#define IS_SAME(L,R) nbl::hlsl::is_same<L,R>::value
10-
#define SHADER_CRASHING_ASSERT(expr) \
11-
{ \
12-
bool con = (expr); \
13-
do { \
14-
[branch] if (!con) \
15-
vk::RawBufferStore<uint32_t>(0xdeadbeefBADC0FFbull,0x45u,4u); \
16-
} while(!con); \
17-
}
1810

1911
#include <nbl/builtin/hlsl/cmath.hlsl>
2012

@@ -145,10 +137,10 @@ void main(uint3 invocationID : SV_DispatchThreadID)
145137
// uint32_t rotrResult0 = nbl::hlsl::mpl::rotr<uint32_t, 2u, 1>::value;
146138
// uint32_t rotrResult1 = nbl::hlsl::mpl::rotr<uint32_t, 2u, -1>::value;
147139

148-
// SHADER_CRASHING_ASSERT(rotlResult0 == mplRotlResult0);
149-
// SHADER_CRASHING_ASSERT(rotlResult1 == mplRotlResult1);
150-
// SHADER_CRASHING_ASSERT(rotrResult0 == mplRotrResult0);
151-
// SHADER_CRASHING_ASSERT(rotrResult1 == mplRotrResult1);
140+
// assert(rotlResult0 == mplRotlResult0);
141+
// assert(rotlResult1 == mplRotlResult1);
142+
// assert(rotrResult0 == mplRotrResult0);
143+
// assert(rotrResult1 == mplRotrResult1);
152144

153145
// TODO: more tests and compare with cpp version as well
154146
fill(invocationID, 5);
@@ -165,27 +157,27 @@ void main(uint3 invocationID : SV_DispatchThreadID)
165157
uint16_t compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint16_t, TEST_VALUE_0>::value;
166158
uint16_t runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_0);
167159
fill(invocationID, float4(5.1, compileTimeCountLZero, runTimeCountLZero, 0));
168-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
160+
assert(compileTimeCountLZero == runTimeCountLZero);
169161

170162
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_1>::value;
171163
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_1);
172164
fill(invocationID, float4(5.2, compileTimeCountLZero, runTimeCountLZero, 0));
173-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
165+
assert(compileTimeCountLZero == runTimeCountLZero);
174166

175167
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_2>::value;
176168
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_2);
177169
fill(invocationID, float4(5.3, compileTimeCountLZero, runTimeCountLZero, 0));
178-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
170+
assert(compileTimeCountLZero == runTimeCountLZero);
179171

180172
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_3>::value;
181173
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_3);
182174
fill(invocationID, float4(5.4, compileTimeCountLZero, runTimeCountLZero, 0));
183-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
175+
assert(compileTimeCountLZero == runTimeCountLZero);
184176

185177
compileTimeCountLZero = nbl::hlsl::mpl::countl_zero<uint32_t, TEST_VALUE_4>::value;
186178
runTimeCountLZero = nbl::hlsl::countl_zero(TEST_VALUE_4);
187179
fill(invocationID, float4(5.5, compileTimeCountLZero, runTimeCountLZero, 0));
188-
SHADER_CRASHING_ASSERT(compileTimeCountLZero == runTimeCountLZero);
180+
assert(compileTimeCountLZero == runTimeCountLZero);
189181
}
190182

191183
{
@@ -201,7 +193,7 @@ void main(uint3 invocationID : SV_DispatchThreadID)
201193
{
202194
float4 v;
203195
fill(invocationID, float4(alignof(v.x), alignof(v), 0, 0));
204-
SHADER_CRASHING_ASSERT(alignof(v.x) == alignof(v));
196+
assert(alignof(v.x) == alignof(v));
205197
}
206198

207199
{
@@ -266,7 +258,7 @@ void main(uint3 invocationID : SV_DispatchThreadID)
266258
decltype(fn(__VA_ARGS__)) lhs = nbl::hlsl::fn(__VA_ARGS__); \
267259
decltype(lhs) rhs = fn(__VA_ARGS__); \
268260
fill(invocationID, float4(idx, lhs, rhs)); \
269-
SHADER_CRASHING_ASSERT(abs(lhs - rhs) < nbl::hlsl::numeric_limits<decltype(lhs)>::epsilon) \
261+
assert(abs(lhs - rhs) < nbl::hlsl::numeric_limits<decltype(lhs)>::epsilon) \
270262
}
271263
TEST_FN(float2(10, 1), cos, 1.44f)
272264
TEST_FN(float2(10, 2), sin, 1.44f)

67_RayQueryGeometry/app_resources/common.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
77

8+
// we need bitfield support in NBL_HLSL_DECLARE_STRUCT it seems
89
struct SGeomInfo
910
{
1011
uint64_t vertexBufferAddress;

0 commit comments

Comments
 (0)