aboutsummaryrefslogtreecommitdiff
path: root/test/src/unit/MultithreadingTests.cpp
blob: da07f98f8e86a5a4dbd93adf3f8b7cd638e36a98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2016-2017 NVIDIA Corporation. All rights reserved.


#include "BlastBaseTest.h"
#include "AssetGenerator.h"

#include <iostream>
#include <memory>
#include "TaskDispatcher.h"

#include "NvBlastActor.h"
#include "NvBlastExtDamageShaders.h"

#if NV_XBOXONE
#undef min
#undef max
#endif

typedef std::function<void(const Nv::Blast::Actor&, NvBlastLog)> ActorTestFunction;
typedef std::function<void(std::vector<NvBlastActor*>&, NvBlastLog)> PostDamageTestFunction;


static void blast(std::set<NvBlastActor*>& actorsToDamage, GeneratorAsset* testAsset, GeneratorAsset::Vec3 localPos, float minRadius, float maxRadius, float compressiveDamage)
{
	std::vector<NvBlastChunkFractureData> chunkEvents; /* num lower-support chunks + bonds */
	std::vector<NvBlastBondFractureData> bondEvents; /* num lower-support chunks + bonds */
	chunkEvents.resize(testAsset->solverChunks.size());
	bondEvents.resize(testAsset->solverBonds.size());

	NvBlastFractureBuffers events = { static_cast<uint32_t>(bondEvents.size()), static_cast<uint32_t>(chunkEvents.size()), bondEvents.data(), chunkEvents.data() };

	std::vector<float> scratch(chunkEvents.size() + bondEvents.size(), 0.0f);

	std::vector<char> splitScratch;
	std::vector<NvBlastActor*> newActorsBuffer(testAsset->solverChunks.size());

	NvBlastExtRadialDamageDesc damage = {
		compressiveDamage,
		{ localPos.x, localPos.y, localPos.z },
		minRadius,
		maxRadius
	};

	NvBlastExtProgramParams programParams =
	{
		&damage,
		nullptr
	};

	NvBlastDamageProgram program = {
		NvBlastExtFalloffGraphShader,
		nullptr
	};

	size_t totalNewActorsCount = 0;
	for (std::set<NvBlastActor*>::iterator k = actorsToDamage.begin(); k != actorsToDamage.end();)
	{
		NvBlastActor* actor = *k;

		NvBlastActorGenerateFracture(&events, actor, program, &programParams, nullptr, nullptr);
		NvBlastActorApplyFracture(&events, actor, &events, nullptr, nullptr);

		bool removeActor = false;

		if (events.bondFractureCount + events.chunkFractureCount > 0)
		{
			NvBlastActorSplitEvent splitEvent;
			splitEvent.newActors = &newActorsBuffer.data()[totalNewActorsCount];
			uint32_t newActorSize = (uint32_t)(newActorsBuffer.size() - totalNewActorsCount);

			splitScratch.resize((size_t)NvBlastActorGetRequiredScratchForSplit(actor, nullptr));
			const size_t newActorsCount = NvBlastActorSplit(&splitEvent, actor, newActorSize, splitScratch.data(), nullptr, nullptr);
			totalNewActorsCount += newActorsCount;
			removeActor = splitEvent.deletedActor != NULL;
		}

		if (removeActor)
		{
			k = actorsToDamage.erase(k);
		}
		else
		{
			++k;
		}
	}
	
	for (size_t i = 0; i < totalNewActorsCount; ++i)
	{
		actorsToDamage.insert(newActorsBuffer[i]);
	}
}


template<int FailLevel, int Verbosity>
class MultithreadingTest : public BlastBaseTest<FailLevel, Verbosity>
{
public:
	MultithreadingTest()
	{

	}

	static void messageLog(int type, const char* msg, const char* file, int line)
	{
		BlastBaseTest<FailLevel, Verbosity>::messageLog(type, msg, file, line);
	}

	static void* alloc(size_t size)
	{
		return BlastBaseTest<FailLevel, Verbosity>::alignedZeroedAlloc(size);
	}

	static void free(void* mem)
	{
		BlastBaseTest<FailLevel, Verbosity>::alignedFree(mem);
	}

	static void testActorVisibleChunks(const Nv::Blast::Actor& actor, NvBlastLog)
	{
		const Nv::Blast::Asset& asset = *actor.getAsset();
		const NvBlastChunk* chunks = asset.getChunks();

		if (actor.isSubSupportChunk())
		{
			EXPECT_EQ(1, actor.getVisibleChunkCount());

			const uint32_t firstVisibleChunkIndex = (uint32_t)Nv::Blast::Actor::VisibleChunkIt(actor);

			EXPECT_EQ(actor.getIndex() - asset.m_graph.m_nodeCount, firstVisibleChunkIndex - asset.m_firstSubsupportChunkIndex);

			// Make sure the visible chunk is subsupport
			// Array of support flags
			std::vector<bool> isSupport(asset.m_chunkCount, false);
			for (uint32_t i = 0; i < asset.m_graph.m_nodeCount; ++i)
			{
				isSupport[asset.m_graph.getChunkIndices()[i]] = true;
			}

			// Climb hierarchy to find support chunk
			uint32_t chunkIndex = firstVisibleChunkIndex;
			while (chunkIndex != Nv::Blast::invalidIndex<uint32_t>())
			{
				if (isSupport[chunkIndex])
				{
					break;
				}
				chunkIndex = chunks[chunkIndex].parentChunkIndex;
			}

			EXPECT_FALSE(Nv::Blast::isInvalidIndex(chunkIndex));
		}
		else
		{
			// Array of visibility flags
			std::vector<bool> isVisible(asset.m_chunkCount, false);
			for (Nv::Blast::Actor::VisibleChunkIt i = actor; (bool)i; ++i)
			{
				isVisible[(uint32_t)i] = true;
			}

			// Mark visible nodes representing graph chunks
			std::vector<bool> visibleChunkFound(asset.m_chunkCount, false);

			// Make sure every graph chunk is represented by a visible chunk
			for (Nv::Blast::Actor::GraphNodeIt i = actor; (bool)i; ++i)
			{
				const uint32_t graphNodeIndex = (uint32_t)i;
				uint32_t chunkIndex = asset.m_graph.getChunkIndices()[graphNodeIndex];
				// Climb hierarchy to find visible chunk
				while (chunkIndex != Nv::Blast::invalidIndex<uint32_t>())
				{
					// Check that chunk owners are accurate
					EXPECT_EQ(actor.getIndex(), actor.getFamilyHeader()->getChunkActorIndices()[chunkIndex]);
					if (isVisible[chunkIndex])
					{
						visibleChunkFound[chunkIndex] = true;
						break;
					}
					chunkIndex = chunks[chunkIndex].parentChunkIndex;
				}
				EXPECT_FALSE(Nv::Blast::isInvalidIndex(chunkIndex));
			}

			// Check that all visible chunks are accounted for
			for (uint32_t i = 0; i < asset.m_chunkCount; ++i)
			{
				EXPECT_EQ(visibleChunkFound[i], isVisible[i]);
			}

			// Make sure that, if all siblings are intact, they are invisible
			for (uint32_t i = 0; i < asset.m_chunkCount; ++i)
			{
				bool allIntact = true;
				bool noneVisible = true;
				if (chunks[i].firstChildIndex < asset.getUpperSupportChunkCount()) // Do not check subsupport
				{
					for (uint32_t j = chunks[i].firstChildIndex; j < chunks[i].childIndexStop; ++j)
					{
						allIntact = allIntact && actor.getFamilyHeader()->getChunkActorIndices()[j] == actor.getIndex();
						noneVisible = noneVisible && !isVisible[j];
					}
					EXPECT_TRUE(!allIntact || noneVisible);
				}
			}
		}
	}

	class DamageActorTask : public TaskDispatcher::Task
	{
	public:
		DamageActorTask(NvBlastActor* actor, GeneratorAsset* asset, GeneratorAsset::Vec3 localPos, float minRadius, float maxRadius, float compressiveDamage, ActorTestFunction testFunction)
			: m_asset(asset)
			, m_localPos(localPos)
			, m_minRadius(minRadius)
			, m_maxRadius(maxRadius)
			, m_compressiveDamage(compressiveDamage)
			, m_testFunction(testFunction)
		{
			m_actors.insert(actor);
		}

		virtual void process()
		{
			blast(m_actors, m_asset, m_localPos, m_minRadius, m_maxRadius, m_compressiveDamage);

			// Test individual actors
			if (m_testFunction != nullptr)
			{
				for (std::set<NvBlastActor*>::iterator k = m_actors.begin(); k != m_actors.end(); ++k)
				{
					m_testFunction(*static_cast<Nv::Blast::Actor*>(*k), messageLog);
				}
			}
		}

		const std::set<NvBlastActor*>& getResult() const { return m_actors; }

	private:
		std::set<NvBlastActor*>			 m_actors;
		GeneratorAsset*                  m_asset;
		GeneratorAsset::Vec3             m_localPos;
		float                            m_minRadius;
		float                            m_maxRadius;
		float                            m_compressiveDamage;
		ActorTestFunction                m_testFunction;

		std::vector<NvBlastActor*> m_resultActors;
	};

	void damageLeafSupportActorsParallelized
	(
		uint32_t assetCount,
		uint32_t minChunkCount,
		uint32_t damageCount,
		uint32_t threadCount,
		ActorTestFunction actorTestFunction,
		PostDamageTestFunction postDamageTestFunction
	)
	{
		const float relativeDamageRadius = 0.05f;
		const float compressiveDamage = 1.0f;

		srand(0);

		std::cout << "Asset # (out of " << assetCount << "): ";
		for (uint32_t assetNum = 0; assetNum < assetCount; ++assetNum)
		{
			std::cout << assetNum + 1 << ".. ";
			CubeAssetGenerator::Settings settings;
			settings.extents = GeneratorAsset::Vec3(1, 1, 1);
			CubeAssetGenerator::DepthInfo depthInfo;
			depthInfo.slicesPerAxis = GeneratorAsset::Vec3(1, 1, 1);
			depthInfo.flag = NvBlastChunkDesc::Flags::NoFlags;
			settings.depths.push_back(depthInfo);
			uint32_t chunkCount = 1;
			while (chunkCount < minChunkCount)
			{
				uint32_t chunkMul;
				do
				{
					depthInfo.slicesPerAxis = GeneratorAsset::Vec3((float)(1 + rand() % 4), (float)(1 + rand() % 4), (float)(1 + rand() % 4));
					chunkMul = (uint32_t)(depthInfo.slicesPerAxis.x * depthInfo.slicesPerAxis.y * depthInfo.slicesPerAxis.z);
				} while (chunkMul == 1);
				chunkCount *= chunkMul;
				settings.depths.push_back(depthInfo);
				settings.extents = settings.extents * depthInfo.slicesPerAxis;
			}
			settings.depths.back().flag = NvBlastChunkDesc::SupportFlag;	// Leaves are support

			// Make largest direction unit size
			settings.extents = settings.extents * (1.0f / std::max(settings.extents.x, std::max(settings.extents.y, settings.extents.z)));

			// Create asset
			GeneratorAsset testAsset;
			CubeAssetGenerator::generate(testAsset, settings);

			NvBlastAssetDesc desc;
			desc.chunkDescs = &testAsset.solverChunks[0];
			desc.chunkCount = (uint32_t)testAsset.solverChunks.size();
			desc.bondDescs = testAsset.solverBonds.data();
			desc.bondCount = (uint32_t)testAsset.solverBonds.size();

			std::vector<char> scratch;
			scratch.resize((size_t)NvBlastGetRequiredScratchForCreateAsset(&desc, messageLog));
			void* mem = alloc(NvBlastGetAssetMemorySize(&desc, messageLog));
			NvBlastAsset* asset = NvBlastCreateAsset(mem, &desc, scratch.data(), messageLog);
			EXPECT_TRUE(asset != nullptr);

			NvBlastActorDesc actorDesc;
			actorDesc.initialBondHealths = actorDesc.initialSupportChunkHealths = nullptr;
			actorDesc.uniformInitialBondHealth = actorDesc.uniformInitialLowerSupportChunkHealth = 1.0f;
			void* fmem = alloc(NvBlastAssetGetFamilyMemorySize(asset, messageLog));
			NvBlastFamily* family = NvBlastAssetCreateFamily(fmem, asset, nullptr);	// Using zeroingAlloc in case actorTest compares memory blocks
			scratch.resize((size_t)NvBlastFamilyGetRequiredScratchForCreateFirstActor(family, messageLog));
			NvBlastActor* actor = NvBlastFamilyCreateFirstActor(family, &actorDesc, scratch.data(), messageLog);
			EXPECT_TRUE(actor != nullptr);

			// Run parallelized damage through TaskDispatcher
			std::set<NvBlastActor*> resultActors;
			{
				uint32_t damageNum = 0;

				// create DamageActorTask and it to dispatcher helper function
				auto addDamageTaskFunction = [&](TaskDispatcher& dispatcher, NvBlastActor* actor)
				{
					GeneratorAsset::Vec3 localPos = settings.extents*GeneratorAsset::Vec3((float)rand() / RAND_MAX - 0.5f, (float)rand() / RAND_MAX - 0.5f, (float)rand() / RAND_MAX - 0.5f);
					auto newTask = std::unique_ptr<DamageActorTask>(new DamageActorTask(actor, &testAsset, localPos, relativeDamageRadius, relativeDamageRadius*1.2f, compressiveDamage, actorTestFunction));
					dispatcher.addTask(std::move(newTask));
				};

				// on task finished function for dispatcher (main thread)
				TaskDispatcher::OnTaskFinishedFunction onTaskFinishedFunction = [&](TaskDispatcher& dispatcher, std::unique_ptr<TaskDispatcher::Task> task) {
					const DamageActorTask* damageTask = static_cast<const DamageActorTask*>(task.get());
					const std::set<NvBlastActor*>& actors = damageTask->getResult();
					for (NvBlastActor* actor : actors)
					{
						if (damageNum >= damageCount)
						{
							resultActors.insert(actor);
						}
						else
						{
							damageNum++;
							addDamageTaskFunction(dispatcher, actor);
						}
					}
				};

				// create dispatcher, add first task and run 
				TaskDispatcher dispatcher(threadCount, onTaskFinishedFunction);
				addDamageTaskFunction(dispatcher, actor);
				dispatcher.process();
			}

			// Test fractured actor set
			if (postDamageTestFunction)
			{
				std::vector<NvBlastActor*> actorArray(resultActors.begin(), resultActors.end());
				postDamageTestFunction(actorArray, messageLog);
			}

			// Release remaining actors
			for (std::set<NvBlastActor*>::iterator k = resultActors.begin(); k != resultActors.end(); ++k)
			{
				NvBlastActorDeactivate(*k, messageLog);
			}
			resultActors.clear();

			const uint32_t actorCount = NvBlastFamilyGetActorCount(family, messageLog);
			EXPECT_TRUE(actorCount == 0);

			free(family);

			// Release asset data
			free(asset);
		}
		std::cout << "done.\n";
	}
};


// Specializations
typedef MultithreadingTest<NvBlastMessage::Error, 1> MultithreadingTestAllowWarnings;
typedef MultithreadingTest<NvBlastMessage::Error, 1> MultithreadingTestStrict;


TEST_F(MultithreadingTestStrict, MultithreadingTestDamageLeafSupportActorsTestVisibility)
{
	damageLeafSupportActorsParallelized(1, 1000, 50, 4, testActorVisibleChunks, nullptr);
}

TEST_F(MultithreadingTestStrict, MultithreadingTestDamageLeafSupportActors)
{
	damageLeafSupportActorsParallelized(1, 3000, 1000, 4, nullptr, nullptr);
}