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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
#include "ApexDefs.h"
#include "ApexAuthorableObject.h"
#include "ApexIsoMesh.h"
#include "ApexSubdivider.h"
#include "ApexSharedUtils.h"
#include "RenderMeshAssetIntl.h"
#include <limits.h>
#include <new>
#include "PxCudaContextManager.h"
#include "Factory.h"
#include "ModuleClothingImpl.h"
#include "ModuleClothingRegistration.h"
#include "ClothingAssetImpl.h"
#include "ClothingAssetAuthoringImpl.h"
#include "ClothingPhysicalMeshImpl.h"
#include "SceneIntl.h"
#include "ClothingScene.h"
#include "ModulePerfScope.h"
#include "CookingPhysX.h"
#include "Cooking.h"
#include "Simulation.h"
#include "ApexSDKIntl.h"
#include "ApexUsingNamespace.h"
#if APEX_CUDA_SUPPORT
#include "CuFactory.h"
#endif
#include "ApexPvdClient.h"
using namespace clothing;
using namespace physx::pvdsdk;
#define INIT_PVD_CLASSES_PARAMETERIZED( parameterizedClassName ) { \
pvdStream.createClass(NamespacedName(APEX_PVD_NAMESPACE, #parameterizedClassName)); \
parameterizedClassName* params = DYNAMIC_CAST(parameterizedClassName*)(GetInternalApexSDK()->getParameterizedTraits()->createNvParameterized(#parameterizedClassName)); \
client->initPvdClasses(*params->rootParameterDefinition(), #parameterizedClassName); \
params->destroy(); }
namespace nvidia
{
namespace apex
{
#if defined(_USRDLL) || PX_OSX || (PX_LINUX && APEX_LINUX_SHARED_LIBRARIES)
/* Modules don't have to link against the framework, they keep their own */
ApexSDKIntl* gApexSdk = 0;
ApexSDK* GetApexSDK()
{
return gApexSdk;
}
ApexSDKIntl* GetInternalApexSDK()
{
return gApexSdk;
}
APEX_API Module* CALL_CONV createModule(
ApexSDKIntl* inSdk,
ModuleIntl** niRef,
uint32_t APEXsdkVersion,
uint32_t PhysXsdkVersion,
ApexCreateError* errorCode)
{
if (APEXsdkVersion != APEX_SDK_VERSION)
{
if (errorCode)
{
*errorCode = APEX_CE_WRONG_VERSION;
}
return NULL;
}
if (PhysXsdkVersion != PX_PHYSICS_VERSION)
{
if (errorCode)
{
*errorCode = APEX_CE_WRONG_VERSION;
}
return NULL;
}
gApexSdk = inSdk;
clothing::ModuleClothingImpl* impl = PX_NEW(clothing::ModuleClothingImpl)(inSdk);
*niRef = (ModuleIntl*) impl;
return (Module*) impl;
}
#else // !defined(_USRDLL) && !PX_OSX && (!PX_LINUX || !APEX_LINUX_SHARED_LIBRARIES)
/* Statically linking entry function */
void instantiateModuleClothing()
{
using namespace clothing;
ApexSDKIntl* sdk = GetInternalApexSDK();
clothing::ModuleClothingImpl* impl = PX_NEW(clothing::ModuleClothingImpl)(sdk);
sdk->registerExternalModule((Module*) impl, (ModuleIntl*) impl);
}
#endif // !defined(_USRDLL) && !PX_OSX && (!PX_LINUX || !APEX_LINUX_SHARED_LIBRARIES)
}
namespace clothing
{
// This is needed to have an actor assigned to every PxActor, even if they currently don't belong to an ClothingActor
class DummyActor : public Actor, public UserAllocated, public ApexRWLockable
{
public:
APEX_RW_LOCKABLE_BOILERPLATE
DummyActor(Asset* owner) : mOwner(owner) {}
void release()
{
PX_DELETE(this);
}
Asset* getOwner() const
{
return mOwner;
}
void getLodRange(float& min, float& max, bool& intOnly) const
{
PX_UNUSED(min);
PX_UNUSED(max);
PX_UNUSED(intOnly);
}
float getActiveLod() const
{
return -1.0f;
}
void forceLod(float lod)
{
PX_UNUSED(lod);
}
/**
\brief Selectively enables/disables debug visualization of a specific APEX actor. Default value it true.
*/
virtual void setEnableDebugVisualization(bool state)
{
PX_UNUSED(state);
}
private:
Asset* mOwner;
};
// This is needed to for every dummy actor to point to an asset that in turn has the right object type id.
class DummyAsset : public Asset, public UserAllocated, public ApexRWLockable
{
public:
APEX_RW_LOCKABLE_BOILERPLATE
DummyAsset(AuthObjTypeID assetTypeID) : mAssetTypeID(assetTypeID) {};
void release()
{
PX_DELETE(this);
}
virtual const char* getName() const
{
return NULL;
}
virtual AuthObjTypeID getObjTypeID() const
{
return mAssetTypeID;
}
virtual const char* getObjTypeName() const
{
return NULL;
}
virtual uint32_t forceLoadAssets()
{
return 0;
}
virtual const NvParameterized::Interface* getAssetNvParameterized() const
{
return NULL;
}
NvParameterized::Interface* getDefaultActorDesc()
{
PX_ALWAYS_ASSERT();
return NULL;
};
NvParameterized::Interface* getDefaultAssetPreviewDesc()
{
PX_ALWAYS_ASSERT();
return NULL;
};
virtual Actor* createApexActor(const NvParameterized::Interface& /*parms*/, Scene& /*apexScene*/)
{
PX_ALWAYS_ASSERT();
return NULL;
}
virtual AssetPreview* createApexAssetPreview(const ::NvParameterized::Interface& /*params*/, AssetPreviewScene* /*previewScene*/)
{
PX_ALWAYS_ASSERT();
return NULL;
}
virtual bool isValidForActorCreation(const ::NvParameterized::Interface& /*parms*/, Scene& /*apexScene*/) const
{
return true; // TODO implement this method
}
virtual bool isDirty() const
{
return false;
}
/**
* \brief Releases the ApexAsset but returns the NvParameterized::Interface and *ownership* to the caller.
*/
virtual NvParameterized::Interface* releaseAndReturnNvParameterizedInterface(void)
{
return NULL;
}
private:
AuthObjTypeID mAssetTypeID;
};
ModuleClothingImpl::ModuleClothingImpl(ApexSDKIntl* inSdk)
: mDummyActor(NULL)
, mDummyAsset(NULL)
, mModuleParams(NULL)
, mApexClothingActorParams(NULL)
, mApexClothingPreviewParams(NULL)
, mCpuFactory(NULL)
, mCpuFactoryReferenceCount(0)
#if APEX_CUDA_SUPPORT
, mGpuDllHandle(NULL)
, mPxCreateCuFactoryFunc(NULL)
#endif
{
mInternalModuleParams.maxNumCompartments = 4;
mInternalModuleParams.maxUnusedPhysXResources = 5;
mInternalModuleParams.allowAsyncCooking = true;
mInternalModuleParams.avgSimFrequencyWindow = 60;
mInternalModuleParams.allowApexWorkBetweenSubsteps = true;
mInternalModuleParams.interCollisionDistance = 0.0f;
mInternalModuleParams.interCollisionStiffness = 1.0f;
mInternalModuleParams.interCollisionIterations = 1;
mInternalModuleParams.sparseSelfCollision = false;
mInternalModuleParams.maxTimeRenderProxyInPool = 100;
PX_COMPILE_TIME_ASSERT(sizeof(mInternalModuleParams) == 40); // don't forget to init the new param here (and then update this assert)
mName = "Clothing";
mSdk = inSdk;
mApiProxy = this;
NvParameterized::Traits* traits = mSdk->getParameterizedTraits();
if (traits)
{
ModuleClothingRegistration::invokeRegistration(traits);
mApexClothingActorParams = traits->createNvParameterized(ClothingActorParam::staticClassName());
mApexClothingPreviewParams = traits->createNvParameterized(ClothingPreviewParam::staticClassName());
}
#if APEX_CUDA_SUPPORT
// Since we split out the GPU code, we load the module and create the CuFactory ourselves
ApexSimpleString gpuClothingDllName;
PX_COMPILE_TIME_ASSERT(sizeof(HMODULE) == sizeof(mGpuDllHandle));
{
ModuleUpdateLoader moduleLoader(UPDATE_LOADER_DLL_NAME);
#define APEX_CLOTHING_GPU_DLL_PREFIX "APEX_ClothingGPU"
#ifdef PX_PHYSX_DLL_NAME_POSTFIX
# if PX_X86
static const char* gpuClothingDllPrefix = APEX_CLOTHING_GPU_DLL_PREFIX PX_STRINGIZE(PX_PHYSX_DLL_NAME_POSTFIX) "_x86";
# elif PX_X64
static const char* gpuClothingDllPrefix = APEX_CLOTHING_GPU_DLL_PREFIX PX_STRINGIZE(PX_PHYSX_DLL_NAME_POSTFIX) "_x64";
# endif
#else
# if PX_X86
static const char* gpuClothingDllPrefix = APEX_CLOTHING_GPU_DLL_PREFIX "_x86";
# elif PX_X64
static const char* gpuClothingDllPrefix = APEX_CLOTHING_GPU_DLL_PREFIX "_x64";
# endif
#endif
#undef APEX_CLOTHING_GPU_DLL_PREFIX
gpuClothingDllName = ApexSimpleString(gpuClothingDllPrefix);
// applications can append strings to the APEX DLL filenames, support this with getCustomDllNamePostfix()
gpuClothingDllName += ApexSimpleString(GetInternalApexSDK()->getCustomDllNamePostfix());
gpuClothingDllName += ApexSimpleString(".dll");
mGpuDllHandle = moduleLoader.loadModule(gpuClothingDllName.c_str(), GetInternalApexSDK()->getAppGuid());
}
if (mGpuDllHandle)
{
mPxCreateCuFactoryFunc = (PxCreateCuFactory_FUNC*)GetProcAddress((HMODULE)mGpuDllHandle, "PxCreateCuFactory");
if (mPxCreateCuFactoryFunc == NULL)
{
APEX_DEBUG_WARNING("Failed to find method PxCreateCuFactory in dll \'%s\'", gpuClothingDllName.c_str());
FreeLibrary((HMODULE)mGpuDllHandle);
mGpuDllHandle = NULL;
}
}
else if (!gpuClothingDllName.empty())
{
APEX_DEBUG_WARNING("Failed to load the GPU dll \'%s\'", gpuClothingDllName.c_str());
}
#endif
}
AuthObjTypeID ModuleClothingImpl::getModuleID() const
{
return ClothingAssetImpl::mAssetTypeID;
}
RenderableIterator* ModuleClothingImpl::createRenderableIterator(const Scene& apexScene)
{
ClothingScene* cs = getClothingScene(apexScene);
if (cs)
{
return cs->createRenderableIterator();
}
return NULL;
}
#ifdef WITHOUT_APEX_AUTHORING
class ClothingAssetDummyAuthoring : public AssetAuthoring, public UserAllocated
{
public:
ClothingAssetDummyAuthoring(ModuleClothingImpl* module, ResourceList& list, NvParameterized::Interface* params, const char* name)
{
PX_UNUSED(module);
PX_UNUSED(list);
PX_UNUSED(params);
PX_UNUSED(name);
}
ClothingAssetDummyAuthoring(ModuleClothingImpl* module, ResourceList& list, const char* name)
{
PX_UNUSED(module);
PX_UNUSED(list);
PX_UNUSED(name);
}
ClothingAssetDummyAuthoring(ModuleClothingImpl* module, ResourceList& list)
{
PX_UNUSED(module);
PX_UNUSED(list);
}
virtual ~ClothingAssetDummyAuthoring() {}
virtual void setToolString(const char* /*toolName*/, const char* /*toolVersion*/, uint32_t /*toolChangelist*/)
{
}
virtual void release()
{
destroy();
}
// internal
void destroy()
{
PX_DELETE(this);
}
/**
* \brief Returns the name of this APEX authorable object type
*/
virtual const char* getObjTypeName() const
{
return CLOTHING_AUTHORING_TYPE_NAME;
}
/**
* \brief Prepares a fully authored Asset Authoring object for a specified platform
*/
virtual bool prepareForPlatform(nvidia::apex::PlatformTag)
{
PX_ASSERT(0);
return false;
}
const char* getName(void) const
{
return NULL;
}
/**
* \brief Save asset's NvParameterized interface, may return NULL
*/
virtual NvParameterized::Interface* getNvParameterized() const
{
PX_ASSERT(0);
return NULL; //ClothingAssetImpl::getAssetNvParameterized();
}
virtual NvParameterized::Interface* releaseAndReturnNvParameterizedInterface(void)
{
PX_ALWAYS_ASSERT();
return NULL;
}
};
typedef ApexAuthorableObject<ModuleClothingImpl, ClothingAssetImpl, ClothingAssetDummyAuthoring> ClothingAO;
#else
typedef ApexAuthorableObject<ModuleClothingImpl, ClothingAssetImpl, ClothingAssetAuthoringImpl> ClothingAO;
#endif
NvParameterized::Interface* ModuleClothingImpl::getDefaultModuleDesc()
{
NvParameterized::Traits* traits = mSdk->getParameterizedTraits();
if (!mModuleParams)
{
mModuleParams = DYNAMIC_CAST(ClothingModuleParameters*)
(traits->createNvParameterized("ClothingModuleParameters"));
PX_ASSERT(mModuleParams);
}
else
{
mModuleParams->initDefaults();
}
const NvParameterized::Hint* hint = NULL;
NvParameterized::Handle h(mModuleParams);
h.getParameter("maxNumCompartments");
PX_ASSERT(h.isValid());
#if PX_WINDOWS_FAMILY
hint = h.parameterDefinition()->hint("defaultValueWindows");
#else
hint = h.parameterDefinition()->hint("defaultValueConsoles");
#endif
PX_ASSERT(hint);
if (hint)
{
mModuleParams->maxNumCompartments = (uint32_t)hint->asUInt();
}
return mModuleParams;
}
void ModuleClothingImpl::init(NvParameterized::Interface& desc)
{
if (::strcmp(desc.className(), ClothingModuleParameters::staticClassName()) == 0)
{
ClothingModuleParameters* params = DYNAMIC_CAST(ClothingModuleParameters*)(&desc);
mInternalModuleParams = *params;
}
else
{
APEX_INVALID_PARAMETER("The NvParameterized::Interface object is of the wrong type");
}
ClothingAO* AOClothingAsset = PX_NEW(ClothingAO)(this, mAssetAuthorableObjectFactories, ClothingAssetParameters::staticClassName());
ClothingAssetImpl::mAssetTypeID = AOClothingAsset->getResID();
registerBackendFactory(&mBackendFactory);
#ifndef WITHOUT_PVD
AOClothingAsset->mAssets.setupForPvd(mApiProxy, "ClothingAssets", "ClothingAsset");
// handle case if module is created after pvd connection
pvdsdk::ApexPvdClient* client = mSdk->getApexPvdClient();
if (client != NULL)
{
if (client->isConnected() && client->getPxPvd().getInstrumentationFlags() & PxPvdInstrumentationFlag::eDEBUG)
{
pvdsdk::PvdDataStream* pvdStream = client->getDataStream();
if (pvdStream != NULL)
{
pvdsdk::NamespacedName pvdModuleName(APEX_PVD_NAMESPACE, getName());
pvdStream->createClass(pvdModuleName);
initPvdClasses(*pvdStream);
ModuleBase* nxModule = static_cast<ModuleBase*>(this);
pvdStream->createInstance(pvdModuleName, nxModule);
pvdStream->pushBackObjectRef(mSdk, "Modules", nxModule);
initPvdInstances(*pvdStream);
}
}
}
#endif
}
ClothingPhysicalMesh* ModuleClothingImpl::createEmptyPhysicalMesh()
{
WRITE_ZONE();
return createPhysicalMeshInternal(NULL);
}
ClothingPhysicalMesh* ModuleClothingImpl::createSingleLayeredMesh(RenderMeshAssetAuthoring* asset, uint32_t subdivisionSize, bool mergeVertices, bool closeHoles, IProgressListener* progress)
{
WRITE_ZONE();
return createSingleLayeredMeshInternal(DYNAMIC_CAST(RenderMeshAssetAuthoringIntl*)(asset), subdivisionSize, mergeVertices, closeHoles, progress);
}
void ModuleClothingImpl::destroy()
{
mClothingSceneList.clear();
if (mApexClothingActorParams != NULL)
{
mApexClothingActorParams->destroy();
mApexClothingActorParams = NULL;
}
if (mApexClothingPreviewParams != NULL)
{
mApexClothingPreviewParams->destroy();
mApexClothingPreviewParams = NULL;
}
#if APEX_CUDA_SUPPORT
for (PxU32 i = 0; i < mGpuFactories.size(); i++)
{
//APEX_DEBUG_INFO("Release Gpu factory %d", i);
PX_DELETE(mGpuFactories[i].factoryGpu);
mGpuFactories.replaceWithLast(i);
}
#endif
PX_DELETE(mCpuFactory);
mCpuFactory = NULL;
#ifndef WITHOUT_PVD
destroyPvdInstances();
#endif
if (mModuleParams != NULL)
{
mModuleParams->destroy();
mModuleParams = NULL;
}
if (mDummyActor != NULL)
{
mDummyActor->release();
mDummyActor = NULL;
}
if (mDummyAsset != NULL)
{
mDummyAsset->release();
mDummyAsset = NULL;
}
NvParameterized::Traits* traits = mSdk->getParameterizedTraits();
ModuleBase::destroy();
mAssetAuthorableObjectFactories.clear(); // needs to be done before destructor!
#if APEX_CUDA_SUPPORT
if (mGpuDllHandle != NULL)
{
FreeLibrary((HMODULE)mGpuDllHandle);
}
#endif
if (traits)
{
ModuleClothingRegistration::invokeUnregistration(traits);
}
PX_DELETE(this);
}
ModuleSceneIntl* ModuleClothingImpl::createInternalModuleScene(SceneIntl& scene, RenderDebugInterface* renderDebug)
{
return PX_NEW(ClothingScene)(*this, scene, renderDebug, mClothingSceneList);
}
void ModuleClothingImpl::releaseModuleSceneIntl(ModuleSceneIntl& scene)
{
ClothingScene* clothingScene = DYNAMIC_CAST(ClothingScene*)(&scene);
clothingScene->destroy();
}
uint32_t ModuleClothingImpl::forceLoadAssets()
{
uint32_t loadedAssetCount = 0;
for (uint32_t i = 0; i < mAssetAuthorableObjectFactories.getSize(); i++)
{
AuthorableObjectIntl* ao = static_cast<AuthorableObjectIntl*>(mAssetAuthorableObjectFactories.getResource(i));
loadedAssetCount += ao->forceLoadAssets();
}
return loadedAssetCount;
}
#ifndef WITHOUT_PVD
void ModuleClothingImpl::initPvdClasses(pvdsdk::PvdDataStream& pvdStream)
{
NamespacedName objRef = getPvdNamespacedNameForType<ObjectRef>();
// ---------------------------------------
// Hierarchy
// ModuleBase holds ClothingAssets
pvdStream.createClass(NamespacedName(APEX_PVD_NAMESPACE, "ClothingAsset"));
pvdStream.createProperty(NamespacedName(APEX_PVD_NAMESPACE, getName()), "ClothingAssets", "children", objRef, PropertyType::Array);
// ClothingAsset holds ClothingActors
pvdStream.createClass(NamespacedName(APEX_PVD_NAMESPACE, "ClothingActor"));
pvdStream.createProperty(NamespacedName(APEX_PVD_NAMESPACE, "ClothingAsset"), "ClothingActors", "children", objRef, PropertyType::Array);
// ---------------------------------------
// NvParameterized
pvdsdk::ApexPvdClient* client = GetInternalApexSDK()->getApexPvdClient();
PX_ASSERT(client != NULL);
// ModuleBase Params
INIT_PVD_CLASSES_PARAMETERIZED(ClothingModuleParameters);
pvdStream.createProperty(NamespacedName(APEX_PVD_NAMESPACE, getName()), "ModuleParams", "", objRef, PropertyType::Scalar);
// Asset Params
INIT_PVD_CLASSES_PARAMETERIZED(ClothingPhysicalMeshParameters);
INIT_PVD_CLASSES_PARAMETERIZED(ClothingGraphicalLodParameters);
INIT_PVD_CLASSES_PARAMETERIZED(ClothingCookedParam);
INIT_PVD_CLASSES_PARAMETERIZED(ClothingCookedPhysX3Param);
INIT_PVD_CLASSES_PARAMETERIZED(ClothingMaterialLibraryParameters);
INIT_PVD_CLASSES_PARAMETERIZED(ClothingAssetParameters);
pvdStream.createProperty(NamespacedName(APEX_PVD_NAMESPACE, "ClothingAsset"), "AssetParams", "", objRef, PropertyType::Scalar);
// Actor Params
INIT_PVD_CLASSES_PARAMETERIZED(ClothingActorParam);
pvdStream.createProperty(NamespacedName(APEX_PVD_NAMESPACE, "ClothingActor"), "ActorParams", "", objRef, PropertyType::Scalar);
// ---------------------------------------
// Additional Properties
// ---------------------------------------
}
void ModuleClothingImpl::initPvdInstances(pvdsdk::PvdDataStream& pvdStream)
{
// if there's more than one AOFactory we don't know any more for sure that its a clothing asset factory, so we have to adapt the code below
PX_ASSERT(mAssetAuthorableObjectFactories.getSize() == 1 && "Adapt the code below");
pvdsdk::ApexPvdClient* client = GetInternalApexSDK()->getApexPvdClient();
PX_ASSERT(client != NULL);
// ModuleBase Params
pvdStream.createInstance(NamespacedName(APEX_PVD_NAMESPACE, "ClothingModuleParameters"), mModuleParams);
pvdStream.setPropertyValue(mApiProxy, "ModuleParams", DataRef<const uint8_t>((const uint8_t*)&mModuleParams, sizeof(ClothingModuleParameters*)), getPvdNamespacedNameForType<ObjectRef>());
// update module properties (should we do this per frame? if so, how?)
client->updatePvd(mModuleParams, *mModuleParams);
// prepare asset list and forward init calls
AuthorableObjectIntl* ao = static_cast<AuthorableObjectIntl*>(mAssetAuthorableObjectFactories.getResource(0));
ao->mAssets.initPvdInstances(pvdStream);
}
void ModuleClothingImpl::destroyPvdInstances()
{
pvdsdk::ApexPvdClient* client = GetInternalApexSDK()->getApexPvdClient();
if (client != NULL)
{
if (client->isConnected() && client->getPxPvd().getInstrumentationFlags() & PxPvdInstrumentationFlag::eDEBUG)
{
pvdsdk::PvdDataStream* pvdStream = client->getDataStream();
{
if (pvdStream != NULL)
{
client->updatePvd(mModuleParams, *mModuleParams, pvdsdk::PvdAction::DESTROY);
pvdStream->destroyInstance(mModuleParams);
}
}
}
}
}
#endif
ClothingScene* ModuleClothingImpl::getClothingScene(const Scene& apexScene)
{
const SceneIntl* niScene = DYNAMIC_CAST(const SceneIntl*)(&apexScene);
for (uint32_t i = 0 ; i < mClothingSceneList.getSize() ; i++)
{
ClothingScene* clothingScene = DYNAMIC_CAST(ClothingScene*)(mClothingSceneList.getResource(i));
if (clothingScene->mApexScene == niScene)
{
return clothingScene;
}
}
PX_ASSERT(!"Unable to locate an appropriate ClothingScene");
return NULL;
}
ClothingPhysicalMeshImpl* ModuleClothingImpl::createPhysicalMeshInternal(ClothingPhysicalMeshParameters* mesh)
{
ClothingPhysicalMeshImpl* result = PX_NEW(ClothingPhysicalMeshImpl)(this, mesh, &mPhysicalMeshes);
return result;
}
void ModuleClothingImpl::releasePhysicalMesh(ClothingPhysicalMeshImpl* physicalMesh)
{
physicalMesh->destroy();
}
void ModuleClothingImpl::unregisterAssetWithScenes(ClothingAssetImpl* asset)
{
for (uint32_t i = 0; i < mClothingSceneList.getSize(); i++)
{
ClothingScene* clothingScene = static_cast<ClothingScene*>(mClothingSceneList.getResource(i));
clothingScene->unregisterAsset(asset);
}
}
void ModuleClothingImpl::notifyReleaseGraphicalData(ClothingAssetImpl* asset)
{
for (uint32_t i = 0; i < mClothingSceneList.getSize(); i++)
{
ClothingScene* clothingScene = static_cast<ClothingScene*>(mClothingSceneList.getResource(i));
clothingScene->removeRenderProxies(asset);
}
}
Actor* ModuleClothingImpl::getDummyActor()
{
mDummyProtector.lock();
if (mDummyActor == NULL)
{
PX_ASSERT(mDummyAsset == NULL);
mDummyAsset = PX_NEW(DummyAsset)(getModuleID());
mDummyActor = PX_NEW(DummyActor)(mDummyAsset);
}
mDummyProtector.unlock();
return mDummyActor;
}
void ModuleClothingImpl::registerBackendFactory(BackendFactory* factory)
{
for (uint32_t i = 0; i < mBackendFactories.size(); i++)
{
if (::strcmp(mBackendFactories[i]->getName(), factory->getName()) == 0)
{
return;
}
}
mBackendFactories.pushBack(factory);
}
void ModuleClothingImpl::unregisterBackendFactory(BackendFactory* factory)
{
uint32_t read = 0, write = 0;
while (read < mBackendFactories.size())
{
mBackendFactories[write] = mBackendFactories[read];
if (mBackendFactories[read] == factory)
{
read++;
}
else
{
read++, write++;
}
}
while (read < write)
{
mBackendFactories.popBack();
}
}
BackendFactory* ModuleClothingImpl::getBackendFactory(const char* simulationBackend)
{
PX_ASSERT(simulationBackend != NULL);
for (uint32_t i = 0; i < mBackendFactories.size(); i++)
{
if (mBackendFactories[i]->isMatch(simulationBackend))
{
return mBackendFactories[i];
}
}
//APEX_INVALID_OPERATION("Simulation back end \'%s\' not found, using \'PhysX\' instead\n", simulationBackend);
PX_ASSERT(mBackendFactories.size() >= 1);
return mBackendFactories[0];
}
ClothFactory ModuleClothingImpl::createClothFactory(PxCudaContextManager* contextManager)
{
nvidia::Mutex::ScopedLock lock(mFactoryMutex);
#if APEX_CUDA_SUPPORT
if (contextManager != NULL && contextManager->supportsArchSM20())
{
for (uint32_t i = 0; i < mGpuFactories.size(); i++)
{
if (mGpuFactories[i].contextManager == contextManager)
{
mGpuFactories[i].referenceCount++;
//APEX_DEBUG_INFO("Found Gpu factory %d (ref = %d)", i, mGpuFactories[i].referenceCount);
return ClothFactory(mGpuFactories[i].factoryGpu, &mFactoryMutex);
}
}
// nothing found
if (mPxCreateCuFactoryFunc != NULL)
{
GpuFactoryEntry entry(mPxCreateCuFactoryFunc(contextManager), contextManager);
if (entry.factoryGpu != NULL)
{
//APEX_DEBUG_INFO("Create Gpu factory %d", mGpuFactories.size());
entry.referenceCount = 1;
mGpuFactories.pushBack(entry);
return ClothFactory(entry.factoryGpu, &mFactoryMutex);
}
}
return ClothFactory(NULL, &mFactoryMutex);
}
else
#else
PX_UNUSED(contextManager);
#endif
{
if (mCpuFactory == NULL)
{
mCpuFactory = cloth::Factory::createFactory(cloth::Factory::CPU);
//APEX_DEBUG_INFO("Create Cpu factory");
PX_ASSERT(mCpuFactoryReferenceCount == 0);
}
mCpuFactoryReferenceCount++;
//APEX_DEBUG_INFO("Get Cpu factory (ref = %d)", mCpuFactoryReferenceCount);
return ClothFactory(mCpuFactory, &mFactoryMutex);
}
}
void ModuleClothingImpl::releaseClothFactory(PxCudaContextManager* contextManager)
{
nvidia::Mutex::ScopedLock lock(mFactoryMutex);
#if APEX_CUDA_SUPPORT
if (contextManager != NULL)
{
for (uint32_t i = 0; i < mGpuFactories.size(); i++)
{
if (mGpuFactories[i].contextManager == contextManager)
{
PX_ASSERT(mGpuFactories[i].referenceCount > 0);
mGpuFactories[i].referenceCount--;
//APEX_DEBUG_INFO("Found Gpu factory %d (ref = %d)", i, mGpuFactories[i].referenceCount);
if (mGpuFactories[i].referenceCount == 0)
{
//APEX_DEBUG_INFO("Release Gpu factory %d", i);
PX_DELETE(mGpuFactories[i].factoryGpu);
mGpuFactories.replaceWithLast(i);
}
}
}
}
else
#else
PX_UNUSED(contextManager);
#endif
{
PX_ASSERT(mCpuFactoryReferenceCount > 0);
mCpuFactoryReferenceCount--;
//APEX_DEBUG_INFO("Release Cpu factory (ref = %d)", mCpuFactoryReferenceCount);
if (mCpuFactoryReferenceCount == 0)
{
PX_DELETE(mCpuFactory);
mCpuFactory = NULL;
}
}
}
ClothingPhysicalMesh* ModuleClothingImpl::createSingleLayeredMeshInternal(RenderMeshAssetAuthoringIntl* renderMeshAsset, uint32_t subdivisionSize,
bool mergeVertices, bool closeHoles, IProgressListener* progressListener)
{
if (renderMeshAsset->getPartCount() > 1)
{
APEX_INVALID_PARAMETER("RenderMeshAssetAuthoring has more than one part (%d)", renderMeshAsset->getPartCount());
return NULL;
}
if (subdivisionSize > 200)
{
APEX_INVALID_PARAMETER("subdivisionSize must be smaller or equal to 200 and has been clamped (was %d).", subdivisionSize);
subdivisionSize = 200;
}
HierarchicalProgressListener progress(100, progressListener);
uint32_t numGraphicalVertices = 0;
for (uint32_t i = 0; i < renderMeshAsset->getSubmeshCount(); i++)
{
const RenderSubmeshIntl& submesh = renderMeshAsset->getInternalSubmesh(i);
numGraphicalVertices += submesh.getVertexBuffer().getVertexCount();
}
ClothingPhysicalMeshImpl* physicalMesh = DYNAMIC_CAST(ClothingPhysicalMeshImpl*)(createEmptyPhysicalMesh());
// set time for registration, merge, close and subdivision
uint32_t times[4] = { 20, (uint32_t)(mergeVertices ? 20 : 0), (uint32_t)(closeHoles ? 30 : 0), (uint32_t)(subdivisionSize > 0 ? 30 : 0) };
uint32_t sum = times[0] + times[1] + times[2] + times[3];
for (uint32_t i = 0; i < 4; i++)
{
times[i] = 100 * times[i] / sum;
}
progress.setSubtaskWork((int32_t)times[0], "Creating single layered mesh");
ApexSubdivider subdivider;
Array<int32_t> old2New(numGraphicalVertices, -1);
uint32_t nbVertices = 0;
uint32_t vertexOffset = 0;
for (uint32_t submeshNr = 0; submeshNr < renderMeshAsset->getSubmeshCount(); submeshNr++)
{
const RenderSubmeshIntl& submesh = renderMeshAsset->getInternalSubmesh(submeshNr);
// used for physics?
const VertexFormat& vf = submesh.getVertexBuffer().getFormat();
uint32_t customIndex = (uint32_t)vf.getBufferIndexFromID(vf.getID("USED_FOR_PHYSICS"));
RenderDataFormat::Enum outFormat = vf.getBufferFormat(customIndex);
const uint8_t* usedForPhysics = NULL;
if (outFormat == RenderDataFormat::UBYTE1)
{
usedForPhysics = (const uint8_t*)submesh.getVertexBuffer().getBuffer(customIndex);
}
customIndex = (uint32_t)vf.getBufferIndexFromID(vf.getID("LATCH_TO_NEAREST_SLAVE"));
outFormat = vf.getBufferFormat(customIndex);
const uint32_t* latchToNearestSlave = outFormat != RenderDataFormat::UINT1 ? NULL : (uint32_t*)submesh.getVertexBuffer().getBuffer(customIndex);
customIndex = (uint32_t)vf.getBufferIndexFromID(vf.getID("LATCH_TO_NEAREST_MASTER"));
outFormat = vf.getBufferFormat(customIndex);
const uint32_t* latchToNearestMaster = outFormat != RenderDataFormat::UINT1 ? NULL : (uint32_t*)submesh.getVertexBuffer().getBuffer(customIndex);
PX_ASSERT((latchToNearestSlave != NULL) == (latchToNearestMaster != NULL)); // both NULL or not NULL
// triangles
const uint32_t* indices = submesh.getIndexBuffer(0); // only 1 part supported!
// vertices
RenderDataFormat::Enum format;
uint32_t bufferIndex = (uint32_t)vf.getBufferIndexFromID(vf.getSemanticID(RenderVertexSemantic::POSITION));
const PxVec3* positions = (const PxVec3*)submesh.getVertexBuffer().getBufferAndFormat(format, bufferIndex);
if (format != RenderDataFormat::FLOAT3)
{
PX_ALWAYS_ASSERT();
positions = NULL;
}
const uint32_t submeshIndices = submesh.getIndexCount(0);
for (uint32_t meshIndex = 0; meshIndex < submeshIndices; meshIndex += 3)
{
if (latchToNearestSlave != NULL)
{
uint32_t numVerticesOk = 0;
for (uint32_t i = 0; i < 3; i++)
{
const uint32_t index = indices[meshIndex + i];
numVerticesOk += latchToNearestSlave[index] == 0 ? 1u : 0u;
}
if (numVerticesOk < 3)
{
continue; // skip this triangle
}
}
else if (usedForPhysics != NULL)
{
uint32_t numVerticesOk = 0;
for (uint32_t i = 0; i < 3; i++)
{
const uint32_t index = indices[meshIndex + i];
numVerticesOk += usedForPhysics[index] == 0 ? 0u : 1u;
}
if (numVerticesOk < 3)
{
continue; // skip this triangle
}
}
// add triangle to subdivider
for (uint32_t i = 0; i < 3; i++)
{
const uint32_t localIndex = indices[meshIndex + i];
const uint32_t index = localIndex + vertexOffset;
if (old2New[index] == -1)
{
old2New[index] = (int32_t)nbVertices++;
uint32_t master = latchToNearestMaster != NULL ? latchToNearestMaster[localIndex] : 0xffffffffu;
subdivider.registerVertex(positions[localIndex], master);
}
}
const uint32_t i0 = (uint32_t)old2New[indices[meshIndex + 0] + vertexOffset];
const uint32_t i1 = (uint32_t)old2New[indices[meshIndex + 1] + vertexOffset];
const uint32_t i2 = (uint32_t)old2New[indices[meshIndex + 2] + vertexOffset];
subdivider.registerTriangle(i0, i1, i2);
}
vertexOffset += submesh.getVertexBuffer().getVertexCount();
}
subdivider.endRegistration();
progress.completeSubtask();
if (nbVertices == 0)
{
APEX_INVALID_PARAMETER("Mesh has no active vertices (see Physics on/off channel)");
return NULL;
}
// use subdivider
if (mergeVertices)
{
progress.setSubtaskWork((int32_t)times[1], "Merging");
subdivider.mergeVertices(&progress);
progress.completeSubtask();
}
if (closeHoles)
{
progress.setSubtaskWork((int32_t)times[2], "Closing holes");
subdivider.closeMesh(&progress);
progress.completeSubtask();
}
if (subdivisionSize > 0)
{
progress.setSubtaskWork((int32_t)times[3], "Subdividing");
subdivider.subdivide(subdivisionSize, &progress);
progress.completeSubtask();
}
Array<PxVec3> newVertices(subdivider.getNumVertices());
Array<uint32_t> newMasterValues(subdivider.getNumVertices());
for (uint32_t i = 0; i < newVertices.size(); i++)
{
subdivider.getVertex(i, newVertices[i], newMasterValues[i]);
}
Array<uint32_t> newIndices(subdivider.getNumTriangles() * 3);
for (uint32_t i = 0; i < newIndices.size(); i += 3)
{
subdivider.getTriangle(i / 3, newIndices[i], newIndices[i + 1], newIndices[i + 2]);
}
physicalMesh->setGeometry(false, newVertices.size(), sizeof(PxVec3), newVertices.begin(),
newMasterValues.begin(), newIndices.size(), sizeof(uint32_t), &newIndices[0]);
return physicalMesh;
}
bool ModuleClothingImpl::ClothingBackendFactory::isMatch(const char* simulationBackend)
{
if (::strcmp(getName(), simulationBackend) == 0)
{
return true;
}
if (::strcmp(ClothingCookedPhysX3Param::staticClassName(), simulationBackend) == 0
|| ::strcmp(ClothingCookedParam::staticClassName(), simulationBackend) == 0)
{
return true;
}
return false;
}
const char* ModuleClothingImpl::ClothingBackendFactory::getName()
{
return "Embedded";
}
uint32_t ModuleClothingImpl::ClothingBackendFactory::getCookingVersion()
{
return Cooking::getCookingVersion();
}
uint32_t ModuleClothingImpl::ClothingBackendFactory::getCookedDataVersion(const NvParameterized::Interface* cookedData)
{
if (cookedData != NULL && isMatch(cookedData->className()))
{
if (::strcmp(ClothingCookedParam::staticClassName(), cookedData->className()) == 0)
{
return static_cast<const ClothingCookedParam*>(cookedData)->cookedDataVersion;
}
else if (::strcmp(ClothingCookedPhysX3Param::staticClassName(), cookedData->className()) == 0)
{
return static_cast<const ClothingCookedPhysX3Param*>(cookedData)->cookedDataVersion;
}
else
{
PX_ALWAYS_ASSERT_MESSAGE("Mechanism to extract cooked data version is not defined and not implemented");
}
}
return 0;
}
CookingAbstract* ModuleClothingImpl::ClothingBackendFactory::createCookingJob()
{
bool withFibers = true;
return PX_NEW(Cooking)(withFibers);
}
void ModuleClothingImpl::ClothingBackendFactory::releaseCookedInstances(NvParameterized::Interface* cookedData)
{
Simulation::releaseFabric(cookedData);
}
SimulationAbstract* ModuleClothingImpl::ClothingBackendFactory::createSimulation(ClothingScene* clothingScene, bool useHW)
{
return PX_NEW(Simulation)(clothingScene, useHW);
}
}
} // namespace nvidia
|