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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
// 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) 2008-2017 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include "NvCloth/DxContextManagerCallback.h"
#include "DxFactory.h"
#include "DxFabric.h"
#include "DxCloth.h"
#include "DxSolver.h"
#include "DxCheckSuccess.h"
#include "DxContextLock.h"
#include "../ClothImpl.h"
#if NV_CLOTH_ENABLE_DX11
#include "DxSolverKernelBlob.h"
using namespace physx;
using namespace nv;
namespace nv
{
namespace cloth
{
// defined in Factory.cpp
uint32_t getNextFabricId();
typedef Vec4T<uint32_t> Vec4u;
}
}
void cloth::checkSuccessImpl(HRESULT err, const char* file, const int line)
{
if (err != S_OK)
{
NV_CLOTH_LOG_ERROR("direct compute error: %u at %s:%d", err, file, line);
}
}
namespace
{
// returns max threads as specified by launch bounds in DxSolverKernel.hlsl
uint32_t getMaxThreadsPerBlock()
{
return 1024;
}
}
cloth::DxFactory::DxFactory(DxContextManagerCallback* contextManager)
: mContextManager(contextManager)
, mStagingBuffer(0)
, mSolverKernelComputeShader(nullptr)
, mNumThreadsPerBlock(getMaxThreadsPerBlock())
, mMaxThreadsPerBlock(mNumThreadsPerBlock)
, mConstraints(mContextManager)
, mConstraintsHostCopy(mContextManager, DxStagingBufferPolicy())
, mStiffnessValues(mContextManager)
, mTethers(mContextManager)
, mParticles(mContextManager, DxDefaultRawBufferPolicy())
, mParticlesHostCopy(mContextManager, DxStagingBufferPolicy())
, mParticleAccelerations(mContextManager)
, mParticleAccelerationsHostCopy(mContextManager, DxStagingBufferPolicy())
, mPhaseConfigs(mContextManager)
, mCapsuleIndices(mContextManager, DxStagingBufferPolicy())
, mCapsuleIndicesDeviceCopy(mContextManager)
, mCollisionSpheres(mContextManager, DxStagingBufferPolicy())
, mCollisionSpheresDeviceCopy(mContextManager)
, mConvexMasks(mContextManager, DxStagingBufferPolicy())
, mConvexMasksDeviceCopy(mContextManager)
, mCollisionPlanes(mContextManager, DxStagingBufferPolicy())
, mCollisionPlanesDeviceCopy(mContextManager)
, mCollisionTriangles(mContextManager, DxStagingBufferPolicy())
, mCollisionTrianglesDeviceCopy(mContextManager)
, mMotionConstraints(mContextManager)
, mSeparationConstraints(mContextManager)
, mRestPositions(mContextManager, DxStagingBufferPolicy())
, mRestPositionsDeviceCopy(mContextManager)
, mSelfCollisionIndices(mContextManager)
, mSelfCollisionParticles(mContextManager)
, mSelfCollisionData(mContextManager)
, mTriangles(mContextManager)
{
if (mContextManager->synchronizeResources())
{
// allow particle interop with other device
mParticles.mBuffer.mMiscFlag =
D3D11_RESOURCE_MISC_FLAG(mParticles.mBuffer.mMiscFlag | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX);
}
}
cloth::DxFactory::~DxFactory()
{
DxContextLock(*this);
NV_CLOTH_ASSERT(("All fabrics created by this factory need to be deleted before this factory is destroyed.",mFabrics.size() == 0));
if (mStagingBuffer)
mStagingBuffer->Release();
if (mSolverKernelComputeShader)
mSolverKernelComputeShader->Release();
}
cloth::Fabric* cloth::DxFactory::createFabric(uint32_t numParticles, Range<const uint32_t> phaseIndices,
Range<const uint32_t> sets, Range<const float> restvalues, Range<const float> stiffnessValues,
Range<const uint32_t> indices, Range<const uint32_t> anchors,
Range<const float> tetherLengths, Range<const uint32_t> triangles)
{
return NV_CLOTH_NEW(DxFabric)(*this, numParticles, phaseIndices, sets, restvalues, stiffnessValues, indices, anchors, tetherLengths, triangles,
getNextFabricId());
}
cloth::Cloth* cloth::DxFactory::createCloth(Range<const PxVec4> particles, Fabric& fabric)
{
return NV_CLOTH_NEW(DxCloth)(*this, static_cast<DxFabric&>(fabric), particles);
}
cloth::Solver* cloth::DxFactory::createSolver()
{
CompileComputeShaders(); //Make sure our compute shaders are ready
DxSolver* solver = NV_CLOTH_NEW(DxSolver)(*this);
if (solver->hasError())
{
NV_CLOTH_DELETE(solver);
return NULL;
}
return solver;
}
// DxFactory::clone() implemented in DxClothClone.cpp
void cloth::DxFactory::copyToHost(void* dst, ID3D11Buffer* srcBuffer, uint32_t offset, uint32_t size) const
{
if (!size)
return;
DxContextLock contextLock(*this);
const_cast<DxFactory*>(this)->reserveStagingBuffer(size);
CD3D11_BOX box(offset, 0, 0, offset + size, 1, 1);
mContextManager->getContext()->CopySubresourceRegion(mStagingBuffer, 0, 0, 0, 0, srcBuffer, 0, &box);
void* mapIt = mapStagingBuffer(D3D11_MAP_READ);
memcpy(dst, mapIt, size);
unmapStagingBuffer();
}
void cloth::DxFactory::extractFabricData(const Fabric& fabric, Range<uint32_t> phaseIndices, Range<uint32_t> sets,
Range<float> restvalues, Range<float> stiffnessValues, Range<uint32_t> indices, Range<uint32_t> anchors,
Range<float> tetherLengths, Range<uint32_t> triangles) const
{
DxContextLock contextLock(*this);
const DxFabric& dxFabric = static_cast<const DxFabric&>(fabric);
if (!phaseIndices.empty())
{
NV_CLOTH_ASSERT(phaseIndices.size() == dxFabric.mPhases.size());
memcpy(phaseIndices.begin(), dxFabric.mPhases.begin(), phaseIndices.size() * sizeof(uint32_t));
}
if (!restvalues.empty())
{
NV_CLOTH_ASSERT(restvalues.size() == dxFabric.mConstraints.size());
Vector<DxConstraint>::Type hostConstraints(restvalues.size());
copyToHost(hostConstraints.begin(), dxFabric.mConstraints.buffer(), dxFabric.mConstraints.mOffset * sizeof(DxConstraint),
uint32_t(hostConstraints.size() * sizeof(DxConstraint)));
for (uint32_t i = 0, n = restvalues.size(); i < n; ++i)
restvalues[i] = hostConstraints[i].mRestvalue;
}
if (!stiffnessValues.empty())
{
NV_CLOTH_ASSERT(stiffnessValues.size() == dxFabric.mStiffnessValues.size());
Vector<float>::Type hostStiffnessValues(stiffnessValues.size());
copyToHost(hostStiffnessValues.begin(), dxFabric.mStiffnessValues.buffer(), dxFabric.mStiffnessValues.mOffset * sizeof(float),
uint32_t(hostStiffnessValues.size() * sizeof(float)));
for (uint32_t i = 0, n = stiffnessValues.size(); i < n; ++i)
stiffnessValues[i] = hostStiffnessValues[i];
}
if (!sets.empty())
{
// need to skip copying the first element
NV_CLOTH_ASSERT(sets.size() == dxFabric.mSets.size());
memcpy(sets.begin(), dxFabric.mSets.begin(), sets.size() * sizeof(uint32_t));
}
if (!indices.empty())
{
NV_CLOTH_ASSERT(indices.size() == dxFabric.mConstraints.size()*2);
Vector<DxConstraint>::Type hostConstraints(dxFabric.mConstraints.size());
copyToHost(hostConstraints.begin(), dxFabric.mConstraints.buffer(), dxFabric.mConstraints.mOffset * sizeof(DxConstraint),
uint32_t(hostConstraints.size() * sizeof(DxConstraint)));
auto cIt = hostConstraints.begin(), cEnd = hostConstraints.end();
for (uint32_t* iIt = indices.begin(); cIt != cEnd; ++cIt)
{
*iIt++ = cIt->mFirstIndex;
*iIt++ = cIt->mSecondIndex;
}
}
if (!anchors.empty() || !tetherLengths.empty())
{
uint32_t numTethers = uint32_t(dxFabric.mTethers.size());
Vector<DxTether>::Type tethers(numTethers, DxTether(0, 0));
copyToHost(tethers.begin(), dxFabric.mTethers.buffer(), dxFabric.mTethers.mOffset * sizeof(DxTether),
uint32_t(tethers.size() * sizeof(DxTether)));
NV_CLOTH_ASSERT(anchors.empty() || anchors.size() == tethers.size());
for (uint32_t i = 0; !anchors.empty(); ++i, anchors.popFront())
anchors.front() = tethers[i].mAnchor;
NV_CLOTH_ASSERT(tetherLengths.empty() || tetherLengths.size() == tethers.size());
for (uint32_t i = 0; !tetherLengths.empty(); ++i, tetherLengths.popFront())
tetherLengths.front() = tethers[i].mLength * dxFabric.mTetherLengthScale;
}
if (!triangles.empty())
{
// todo triangles
}
}
void cloth::DxFactory::extractCollisionData(const Cloth& cloth, Range<PxVec4> spheres, Range<uint32_t> capsules,
Range<PxVec4> planes, Range<uint32_t> convexes, Range<PxVec3> triangles) const
{
NV_CLOTH_ASSERT(&cloth.getFactory() == this);
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
NV_CLOTH_ASSERT(spheres.empty() || spheres.size() == dxCloth.mStartCollisionSpheres.size());
NV_CLOTH_ASSERT(capsules.empty() || capsules.size() == dxCloth.mCapsuleIndices.size() * 2);
NV_CLOTH_ASSERT(planes.empty() || planes.size() == dxCloth.mStartCollisionPlanes.size());
NV_CLOTH_ASSERT(convexes.empty() || convexes.size() == dxCloth.mConvexMasks.size());
NV_CLOTH_ASSERT(triangles.empty() || triangles.size() == dxCloth.mStartCollisionTriangles.size());
// collision spheres are in pinned memory, so memcpy directly
if (!dxCloth.mStartCollisionSpheres.empty() && !spheres.empty())
{
memcpy(spheres.begin(),
DxCloth::MappedVec4fVectorType(const_cast<DxCloth&>(dxCloth).mStartCollisionSpheres).begin(),
spheres.size() * sizeof(PxVec4));
}
if (!dxCloth.mCapsuleIndices.empty() && !capsules.empty())
{
memcpy(capsules.begin(), DxCloth::MappedIndexVectorType(const_cast<DxCloth&>(dxCloth).mCapsuleIndices).begin(),
capsules.size() * sizeof(uint32_t));
}
if (!dxCloth.mStartCollisionPlanes.empty() && !planes.empty())
{
memcpy(planes.begin(), DxCloth::MappedVec4fVectorType(const_cast<DxCloth&>(dxCloth).mStartCollisionPlanes).begin(),
dxCloth.mStartCollisionPlanes.size() * sizeof(PxVec4));
}
if (!dxCloth.mConvexMasks.empty() && !convexes.empty())
{
memcpy(convexes.begin(), DxCloth::MappedMaskVectorType(const_cast<DxCloth&>(dxCloth).mConvexMasks).begin(),
dxCloth.mConvexMasks.size() * sizeof(uint32_t));
}
if (!dxCloth.mStartCollisionTriangles.empty() && !triangles.empty())
{
memcpy(triangles.begin(), DxCloth::MappedVec3fVectorType(const_cast<DxCloth&>(dxCloth).mStartCollisionTriangles).begin(),
dxCloth.mStartCollisionTriangles.size() * sizeof(PxVec3));
}
}
void cloth::DxFactory::extractMotionConstraints(const Cloth& cloth, Range<PxVec4> destConstraints) const
{
NV_CLOTH_ASSERT(&cloth.getFactory() == this);
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
if (dxCloth.mMotionConstraints.mHostCopy.size())
{
NV_CLOTH_ASSERT(destConstraints.size() == dxCloth.mMotionConstraints.mHostCopy.size());
memcpy(destConstraints.begin(), dxCloth.mMotionConstraints.mHostCopy.begin(),
sizeof(PxVec4) * dxCloth.mMotionConstraints.mHostCopy.size());
}
else
{
DxContextLock contextLock(*this);
DxBatchedVector<PxVec4> const& srcConstraints = !dxCloth.mMotionConstraints.mTarget.empty()
? dxCloth.mMotionConstraints.mTarget
: dxCloth.mMotionConstraints.mStart;
NV_CLOTH_ASSERT(destConstraints.size() == srcConstraints.size());
copyToHost(destConstraints.begin(), srcConstraints.buffer(), 0, destConstraints.size() * sizeof(PxVec4));
}
}
void cloth::DxFactory::extractSeparationConstraints(const Cloth& cloth, Range<PxVec4> destConstraints) const
{
NV_CLOTH_ASSERT(&cloth.getFactory() == this);
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
if (dxCloth.mSeparationConstraints.mHostCopy.size())
{
NV_CLOTH_ASSERT(destConstraints.size() == dxCloth.mSeparationConstraints.mHostCopy.size());
memcpy(destConstraints.begin(), dxCloth.mSeparationConstraints.mHostCopy.begin(),
sizeof(PxVec4) * dxCloth.mSeparationConstraints.mHostCopy.size());
}
else
{
DxContextLock contextLock(*this);
DxBatchedVector<PxVec4> const& srcConstraints = !dxCloth.mSeparationConstraints.mTarget.empty()
? dxCloth.mSeparationConstraints.mTarget
: dxCloth.mSeparationConstraints.mStart;
NV_CLOTH_ASSERT(destConstraints.size() == srcConstraints.size());
copyToHost(destConstraints.begin(), srcConstraints.buffer(), 0, destConstraints.size() * sizeof(PxVec4));
}
}
void cloth::DxFactory::extractParticleAccelerations(const Cloth& cloth, Range<PxVec4> destAccelerations) const
{
/*
NV_CLOTH_ASSERT(&cloth.getFactory() == this);
const DxCloth& dxCloth = static_cast<const DxClothImpl&>(cloth).mCloth;
if (dxCloth.mParticleAccelerationsHostCopy.size())
{
NV_CLOTH_ASSERT(dxCloth.mParticleAccelerationsHostCopy.size());
memcpy(destAccelerations.begin(), dxCloth.mParticleAccelerationsHostCopy.begin(),
sizeof(PxVec4) * dxCloth.mParticleAccelerationsHostCopy.size());
}
else
{
DxContextLock contextLock(*this);
DxBatchedVector<PxVec4> const& srcAccelerations = dxCloth.mParticleAccelerations;
NV_CLOTH_ASSERT(destAccelerations.size() == srcAccelerations.size());
copyToHost(destAccelerations.begin(), srcAccelerations.buffer(), 0, destAccelerations.size() * sizeof(PxVec4));
}
*/
PX_UNUSED(&cloth);
PX_UNUSED(&destAccelerations);
NV_CLOTH_ASSERT(0);
}
void cloth::DxFactory::extractVirtualParticles(const Cloth& cloth, Range<uint32_t[4]> destIndices,
Range<PxVec3> destWeights) const
{
NV_CLOTH_ASSERT(&cloth.getFactory() == this);
DxContextLock contextLock(*this);
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
if (destWeights.size() > 0)
{
uint32_t numWeights = cloth.getNumVirtualParticleWeights();
Vector<PxVec4>::Type hostWeights(numWeights, PxVec4(0.0f));
copyToHost(hostWeights.begin(), dxCloth.mVirtualParticleWeights.mBuffer.mBuffer, 0,
hostWeights.size() * sizeof(PxVec4));
// convert weights to Vec3f
PxVec3* destIt = reinterpret_cast<PxVec3*>(destWeights.begin());
Vector<PxVec4>::Type::ConstIterator srcIt = hostWeights.begin();
Vector<PxVec4>::Type::ConstIterator srcEnd = srcIt + numWeights;
for (; srcIt != srcEnd; ++srcIt, ++destIt)
*destIt = reinterpret_cast<const PxVec3&>(*srcIt);
NV_CLOTH_ASSERT(destIt <= destWeights.end());
}
if (destIndices.size() > 0)
{
uint32_t numIndices = cloth.getNumVirtualParticles();
Vector<Vec4us>::Type hostIndices(numIndices);
copyToHost(hostIndices.begin(), dxCloth.mVirtualParticleIndices.mBuffer.mBuffer, 0,
hostIndices.size() * sizeof(Vec4us));
// convert indices to 32 bit
Vec4u* destIt = reinterpret_cast<Vec4u*>(destIndices.begin());
Vector<Vec4us>::Type::ConstIterator srcIt = hostIndices.begin();
Vector<Vec4us>::Type::ConstIterator srcEnd = srcIt + numIndices;
for (; srcIt != srcEnd; ++srcIt, ++destIt)
*destIt = Vec4u(*srcIt);
NV_CLOTH_ASSERT(&array(*destIt) <= destIndices.end());
}
}
void cloth::DxFactory::extractSelfCollisionIndices(const Cloth& cloth, Range<uint32_t> destIndices) const
{
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
NV_CLOTH_ASSERT(destIndices.size() == dxCloth.mSelfCollisionIndices.size());
intrinsics::memCopy(destIndices.begin(), dxCloth.mSelfCollisionIndicesHost.begin(),
destIndices.size() * sizeof(uint32_t));
}
void cloth::DxFactory::extractRestPositions(const Cloth& cloth, Range<PxVec4> destRestPositions) const
{
const DxCloth& dxCloth = static_cast<const DxCloth&>(cloth);
NV_CLOTH_ASSERT(destRestPositions.size() == dxCloth.mRestPositions.size());
intrinsics::memCopy(destRestPositions.begin(), DxCloth::MappedVec4fVectorType(const_cast<DxCloth&>(dxCloth).mRestPositions).begin(),
destRestPositions.size() * sizeof(PxVec4));
}
void cloth::DxFactory::reserveStagingBuffer(uint32_t size)
{
if (mStagingBuffer)
{
D3D11_BUFFER_DESC desc;
mStagingBuffer->GetDesc(&desc);
if (desc.ByteWidth >= size)
return;
mStagingBuffer->Release();
}
CD3D11_BUFFER_DESC desc(size, 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
mContextManager->getDevice()->CreateBuffer(&desc, 0, &mStagingBuffer);
}
void* cloth::DxFactory::mapStagingBuffer(D3D11_MAP mapType) const
{
D3D11_MAPPED_SUBRESOURCE mapped;
mContextManager->getContext()->Map(mStagingBuffer, 0, mapType, 0, &mapped);
return mapped.pData;
}
void cloth::DxFactory::unmapStagingBuffer() const
{
mContextManager->getContext()->Unmap(mStagingBuffer, 0);
}
void cloth::DxFactory::CompileComputeShaders()
{
if (mSolverKernelComputeShader == nullptr)
{
DxContextLock(*this);
ID3D11Device* device = mContextManager->getDevice();
device->CreateComputeShader(gDxSolverKernel, sizeof(gDxSolverKernel), NULL, &mSolverKernelComputeShader);
}
}
#endif // NV_CLOTH_ENABLE_DX11
|