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
|
// 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-2020 NVIDIA Corporation. All rights reserved.
#include "TkBaseTest.h"
#include "NvBlastExtSync.h"
#include "NvBlastTkEvent.h"
#include <map>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ExtSync Tests
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Base
{
public:
Base(TkTestStrict* test) : m_test(test)
{
}
void run(std::stringstream& finalState)
{
//////// initial setup ////////
m_test->createTestAssets();
TkFramework* fwk = NvBlastTkFrameworkGet();
TkGroupDesc gdesc;
gdesc.workerCount = m_test->m_taskman->getCpuDispatcher()->getWorkerCount();
m_group = fwk->createGroup(gdesc);
EXPECT_TRUE(m_group != nullptr);
TkActorDesc adesc(m_test->testAssets[0]);
NvBlastID id;
TkActor* actor0 = fwk->createActor(adesc);
EXPECT_TRUE(actor0 != nullptr);
families[0] = &actor0->getFamily();
memcpy(id.data, "Mumble Jumble Bumble", sizeof(NvBlastID)); // Stuffing an arbitrary 16 bytes (The prefix of the given string)
families[0]->setID(id);
m_group->addActor(*actor0);
TkActor* actor1 = fwk->createActor(adesc);
EXPECT_TRUE(actor1 != nullptr);
families[1] = &actor1->getFamily();
memcpy(id.data, "buzzkillerdiller", sizeof(NvBlastID)); // Stuffing an arbitrary 16 bytes (The prefix of the given string)
families[1]->setID(id);
m_group->addActor(*actor1);
m_test->m_groupTM->setGroup(m_group);
//////// server/client specific impl ////////
impl();
//////// write out framework final state ////////
finalState.clear();
for (auto family : families)
{
std::vector<TkActor*> actors(family->getActorCount());
family->getActors(actors.data(), static_cast<uint32_t>(actors.size()));
for (auto actor : actors)
{
finalState << actor->getVisibleChunkCount();
finalState << actor->getGraphNodeCount();
std::vector<uint32_t> chunkIndices(actor->getGraphNodeCount());
actor->getVisibleChunkIndices(chunkIndices.data(), (uint32_t)chunkIndices.size());
for (uint32_t chunkIndex : chunkIndices)
finalState << chunkIndex;
const float* bondHealths = actor->getBondHealths();
for (uint32_t i = 0; i < actor->getAsset()->getBondCount(); ++i)
finalState << bondHealths[i];
}
}
//////// release ////////
m_group->release();
for (auto family : families)
{
family->release();
}
m_test->releaseTestAssets();
}
protected:
virtual void impl() = 0;
TkTestStrict* m_test;
TkGroup* m_group;
TkFamily* families[2];
};
class Server : public Base
{
public:
Server(TkTestStrict* test, std::vector<ExtSyncEvent*>& syncBuffer) : Base(test), m_syncBuffer(syncBuffer) {}
protected:
virtual void impl() override
{
// create sync ext
ExtSync* sync = ExtSync::create();
// add sync as listener to family #1
families[1]->addListener(*sync);
// damage params
CSParams cs0(1, 0.0f);
NvBlastExtProgramParams csParams0 = { &cs0, nullptr };
NvBlastExtRadialDamageDesc radialDamage0 = m_test->getRadialDamageDesc(0, 0, 0);
NvBlastExtProgramParams radialParams0 = { &radialDamage0, nullptr };
NvBlastExtRadialDamageDesc radialDamage1 = m_test->getRadialDamageDesc(0, 0, 0, 10.0f, 10.0f, 0.1f);
NvBlastExtProgramParams radialParams1 = { &radialDamage1, nullptr };
// damage family #0 (make it split)
{
TkActor* actor;
families[0]->getActors(&actor, 1);
actor->damage(m_test->getCubeSlicerProgram(), &csParams0);
}
// process
m_test->m_groupTM->process();
m_test->m_groupTM->wait();
EXPECT_EQ(families[0]->getActorCount(), 2);
// sync family #0
sync->syncFamily(*families[0]);
// add sync as listener to family #0
families[0]->addListener(*sync);
// damage family #0 (make it split fully)
{
TkActor* actor;
families[0]->getActors(&actor, 1, 1);
actor->damage(m_test->getFalloffProgram(), &radialParams0);
}
// damage family 1 (just damage bonds health)
{
TkActor* actor;
families[1]->getActors(&actor, 1);
NvBlastExtRadialDamageDesc radialDamage = m_test->getRadialDamageDesc(0, 0, 0, 10.0f, 10.0f, 0.1f);
actor->damage(m_test->getFalloffProgram(), &radialParams1);
}
// process
m_test->m_groupTM->process();
m_test->m_groupTM->wait();
EXPECT_EQ(families[0]->getActorCount(), 5);
EXPECT_EQ(families[1]->getActorCount(), 1);
// take sync buffer from sync
{
const ExtSyncEvent*const* buffer;
uint32_t size;
sync->acquireSyncBuffer(buffer, size);
m_syncBuffer.resize(size);
for (size_t i = 0; i < size; ++i)
{
m_syncBuffer[i] = buffer[i]->clone();
}
sync->releaseSyncBuffer();
}
//
families[0]->removeListener(*sync);
families[1]->removeListener(*sync);
//
sync->release();
}
private:
std::vector<ExtSyncEvent*>& m_syncBuffer;
};
class Client : public Base, public TkEventListener
{
public:
Client(TkTestStrict* test, std::vector<ExtSyncEvent*>& syncBuffer) : Base(test), m_syncBuffer(syncBuffer) {}
protected:
virtual void impl() override
{
ExtSync* sync = ExtSync::create();
// fill map
for (auto& family : families)
{
std::vector<TkActor*> actors(family->getActorCount());
family->getActors(actors.data(), static_cast<uint32_t>(actors.size()));
auto& actorsSet = m_actorsPerFamily[family];
for (auto actor : actors)
EXPECT_TRUE(actorsSet.insert(actor->getIndex()).second);
}
// subscribe
for (auto& family : families)
{
family->addListener(*this);
}
// apply sync buffer
sync->applySyncBuffer(*NvBlastTkFrameworkGet(), (const Nv::Blast::ExtSyncEvent**)m_syncBuffer.data(), static_cast<uint32_t>(m_syncBuffer.size()), m_group);
// check map
for (auto& family : families)
{
std::vector<TkActor*> actors(family->getActorCount());
family->getActors(actors.data(), static_cast<uint32_t>(actors.size()));
std::set<uint32_t> actorsSet;
for (auto actor : actors)
EXPECT_TRUE(actorsSet.insert(actor->getIndex()).second);
EXPECT_TRUE(m_actorsPerFamily[family] == actorsSet);
}
// unsubscribe
for (auto& family : families)
{
family->removeListener(*this);
}
m_test->m_groupTM->process();
m_test->m_groupTM->wait();
sync->release();
}
// listen for Split event and update actors map
virtual void receive(const TkEvent* events, uint32_t eventCount) override
{
for (size_t i = 0; i < eventCount; ++i)
{
const TkEvent& e = events[i];
switch (e.type)
{
case (TkEvent::Split) :
{
const TkSplitEvent* splitEvent = e.getPayload<TkSplitEvent>();
auto& actorsSet = m_actorsPerFamily[splitEvent->parentData.family];
if (!isInvalidIndex(splitEvent->parentData.index))
{
EXPECT_EQ((size_t)1, actorsSet.erase(splitEvent->parentData.index));
}
for (size_t i = 0; i < splitEvent->numChildren; ++i)
{
TkActor* a = splitEvent->children[i];
EXPECT_TRUE(actorsSet.insert(a->getIndex()).second);
}
break;
}
case (TkEvent::FractureCommand) :
{
break;
}
case (TkEvent::JointUpdate) :
{
FAIL();
break;
}
default:
break;
}
}
}
private:
std::map<TkFamily*, std::set<uint32_t>> m_actorsPerFamily;
std::vector<ExtSyncEvent*>& m_syncBuffer;
};
TEST_F(TkTestStrict, SyncTest1)
{
this->createFramework();
std::vector<ExtSyncEvent*> syncBuffer;
std::stringstream serverFinalState;
{
Server s(this, syncBuffer);
s.run(serverFinalState);
}
EXPECT_TRUE(syncBuffer.size() > 0);
std::stringstream clientFinalState;
{
Client c(this, syncBuffer);
c.run(clientFinalState);
}
for (auto e : syncBuffer)
{
e->release();
}
syncBuffer.clear();
EXPECT_EQ(serverFinalState.str(), clientFinalState.str());
this->releaseFramework();
}
|