-
Notifications
You must be signed in to change notification settings - Fork 8
/
UsdDevice.cpp
1037 lines (883 loc) · 26.6 KB
/
UsdDevice.cpp
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
// Copyright 2020 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#include "UsdDevice.h"
#include "UsdBridgedBaseObject.h"
#include "UsdDataArray.h"
#include "UsdGeometry.h"
#include "UsdSpatialField.h"
#include "UsdSurface.h"
#include "UsdVolume.h"
#include "UsdInstance.h"
#include "UsdGroup.h"
#include "UsdMaterial.h"
#include "UsdSampler.h"
#include "UsdWorld.h"
#include "UsdRenderer.h"
#include "UsdFrame.h"
#include "UsdLight.h"
#include "UsdCamera.h"
#include "UsdDeviceQueries.h"
#include <cstdarg>
#include <cstdio>
#include <set>
#include <memory>
#include <sstream>
#include <algorithm>
#include <limits>
static char deviceName[] = "usd";
class UsdDeviceInternals
{
public:
UsdDeviceInternals()
{
}
bool CreateNewBridge(const UsdDeviceData& deviceParams, UsdBridgeLogCallback bridgeStatusFunc, void* userData)
{
if (bridge.get())
bridge->CloseSession();
UsdBridgeSettings bridgeSettings = {
UsdSharedString::c_str(deviceParams.hostName),
outputLocation.c_str(),
deviceParams.createNewSession,
deviceParams.outputBinary,
deviceParams.outputPreviewSurfaceShader,
deviceParams.outputMdlShader
};
bridge = std::make_unique<UsdBridge>(bridgeSettings);
bridge->SetExternalSceneStage(externalSceneStage);
bridge->SetEnableSaving(this->enableSaving);
bridgeStatusFunc(UsdBridgeLogLevel::STATUS, userData, "Initializing UsdBridge Session");
bool createSuccess = bridge->OpenSession(bridgeStatusFunc, userData);
if (!createSuccess)
{
bridge = nullptr;
bridgeStatusFunc(UsdBridgeLogLevel::STATUS, userData, "UsdBridge Session initialization failed.");
}
else
{
bridgeStatusFunc(UsdBridgeLogLevel::STATUS, userData, "UsdBridge Session initialization successful.");
}
return createSuccess;
}
std::string outputLocation;
bool enableSaving = true;
std::unique_ptr<UsdBridge> bridge;
SceneStagePtr externalSceneStage{nullptr};
std::set<std::string> uniqueNames;
};
//---- Make sure to update clearDeviceParameters() on refcounted objects
DEFINE_PARAMETER_MAP(UsdDevice,
REGISTER_PARAMETER_MACRO("usd::serialize.hostName", ANARI_STRING, hostName)
REGISTER_PARAMETER_MACRO("usd::serialize.location", ANARI_STRING, outputPath)
REGISTER_PARAMETER_MACRO("usd::serialize.newSession", ANARI_BOOL, createNewSession)
REGISTER_PARAMETER_MACRO("usd::serialize.outputBinary", ANARI_BOOL, outputBinary)
REGISTER_PARAMETER_MACRO("usd::time", ANARI_FLOAT64, timeStep)
REGISTER_PARAMETER_MACRO("usd::writeAtCommit", ANARI_BOOL, writeAtCommit)
REGISTER_PARAMETER_MACRO("usd::output.material", ANARI_BOOL, outputMaterial)
REGISTER_PARAMETER_MACRO("usd::output.previewSurfaceShader", ANARI_BOOL, outputPreviewSurfaceShader)
REGISTER_PARAMETER_MACRO("usd::output.mdlShader", ANARI_BOOL, outputMdlShader)
)
void UsdDevice::clearDeviceParameters()
{
filterResetParam("usd::serialize.hostName");
filterResetParam("usd::serialize.location");
transferWriteToReadParams();
}
//----
UsdDevice::UsdDevice()
: UsdParameterizedBaseObject<UsdDevice, UsdDeviceData>(ANARI_DEVICE)
, internals(std::make_unique<UsdDeviceInternals>())
{}
UsdDevice::UsdDevice(ANARILibrary library)
: DeviceImpl(library)
, UsdParameterizedBaseObject<UsdDevice, UsdDeviceData>(ANARI_DEVICE)
, internals(std::make_unique<UsdDeviceInternals>())
{}
UsdDevice::~UsdDevice()
{
// Make sure no more references are held before cleaning up the device (and checking for memleaks)
clearCommitList();
clearDeviceParameters(); // Release device parameters with object references
clearResourceStringList(); // Do the same for resource string references
//internals->bridge->SaveScene(); //Uncomment to test cleanup of usd files.
#ifdef CHECK_MEMLEAKS
if(!allocatedObjects.empty() || !allocatedStrings.empty() || !allocatedRawMemory.empty())
{
std::stringstream errstream;
errstream << "USD Device memleak reported for: ";
for(auto ptr : allocatedObjects)
errstream << "Object ptr: 0x" << std::hex << ptr << " of type: " << std::dec << ptr->getType() << "; ";
for(auto ptr : allocatedStrings)
errstream << "String ptr: 0x" << std::hex << ptr << " with content: " << std::dec << ptr->c_str() << "; ";
for(auto ptr : allocatedRawMemory)
errstream << "Raw ptr: 0x" << std::hex << ptr << std::dec << "; ";
reportStatus(this, ANARI_DEVICE, ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_OPERATION, errstream.str().c_str());
}
else
{
reportStatus(this, ANARI_DEVICE, ANARI_SEVERITY_INFO, ANARI_STATUS_NO_ERROR, "Reference memleak check complete, no issues found.");
}
//assert(allocatedObjects.empty());
#endif
}
void UsdDevice::reportStatus(void* source,
ANARIDataType sourceType,
ANARIStatusSeverity severity,
ANARIStatusCode statusCode,
const char *format, ...)
{
va_list arglist;
va_start(arglist, format);
reportStatus(source, sourceType, severity, statusCode, format, arglist);
va_end(arglist);
}
static void reportBridgeStatus(UsdBridgeLogLevel level, void* device, const char *message)
{
ANARIStatusSeverity severity = UsdBridgeLogLevelToAnariSeverity(level);
va_list arglist;
((UsdDevice*)device)->reportStatus(nullptr, ANARI_UNKNOWN, severity, ANARI_STATUS_NO_ERROR, message, arglist);
}
void UsdDevice::reportStatus(void* source,
ANARIDataType sourceType,
ANARIStatusSeverity severity,
ANARIStatusCode statusCode,
const char *format,
va_list& arglist)
{
va_list arglist_copy;
va_copy(arglist_copy, arglist);
int count = std::vsnprintf(nullptr, 0, format, arglist);
lastStatusMessage.resize(count + 1);
std::vsnprintf(lastStatusMessage.data(), count + 1, format, arglist_copy);
va_end(arglist_copy);
if (statusFunc != nullptr)
{
statusFunc(
statusUserData,
(ANARIDevice)this,
(ANARIObject)source,
sourceType,
severity,
statusCode,
lastStatusMessage.data());
}
}
void UsdDevice::filterSetParam(
const char *name,
ANARIDataType type,
const void *mem,
UsdDevice* device)
{
if (strEquals(name, "usd::garbageCollect"))
{
// Perform garbage collection on usd objects (needs to move into the user interface)
if(internals->bridge)
internals->bridge->GarbageCollect();
}
else if(strEquals(name, "usd::removeUnusedNames"))
{
internals->uniqueNames.clear();
}
else if (strEquals(name, "usd::connection.logVerbosity")) // 0 <= verbosity <= USDBRIDGE_MAX_LOG_VERBOSITY, with USDBRIDGE_MAX_LOG_VERBOSITY being the loudest
{
if(type == ANARI_INT32)
UsdBridge::SetConnectionLogVerbosity(*(reinterpret_cast<const int*>(mem)));
}
else if(strEquals(name, "usd::sceneStage"))
{
if(type == ANARI_VOID_POINTER)
internals->externalSceneStage = const_cast<void *>(mem);
}
else if (strEquals(name, "usd::enableSaving"))
{
if(type == ANARI_BOOL)
{
internals->enableSaving = *(reinterpret_cast<const bool*>(mem));
if(internals->bridge)
internals->bridge->SetEnableSaving(internals->enableSaving);
}
}
else if (strEquals(name, "statusCallback") && type == ANARI_STATUS_CALLBACK)
{
userSetStatusFunc = (ANARIStatusCallback)mem;
}
else if (strEquals(name, "statusCallbackUserData") && type == ANARI_VOID_POINTER)
{
userSetStatusUserData = const_cast<void *>(mem);
}
else
{
setParam(name, type, mem, this);
}
}
void UsdDevice::filterResetParam(const char * name)
{
if (strEquals(name, "statusCallback"))
{
userSetStatusFunc = nullptr;
}
else if (strEquals(name, "statusCallbackUserData"))
{
userSetStatusUserData = nullptr;
}
else if (!strEquals(name, "usd::garbageCollect")
&& !strEquals(name, "usd::removeUnusedNames"))
{
resetParam(name);
}
}
void UsdDevice::commit(UsdDevice* device)
{
transferWriteToReadParams();
if(!bridgeInitAttempt)
{
initializeBridge();
}
else
{
const UsdDeviceData& paramData = getReadParams();
internals->bridge->UpdateBeginEndTime(paramData.timeStep);
}
}
void UsdDevice::initializeBridge()
{
const UsdDeviceData& paramData = getReadParams();
bridgeInitAttempt = true;
statusFunc = userSetStatusFunc ? userSetStatusFunc : defaultStatusCallback();
statusUserData = userSetStatusUserData ? userSetStatusUserData : defaultStatusCallbackUserPtr();
if(paramData.outputPath)
internals->outputLocation = paramData.outputPath->c_str();
if(internals->outputLocation.empty()) {
auto *envLocation = getenv("ANARI_USD_SERIALIZE_LOCATION");
if (envLocation) {
internals->outputLocation = envLocation;
reportStatus(this, ANARI_DEVICE, ANARI_SEVERITY_INFO, ANARI_STATUS_NO_ERROR,
"Usd Device parameter 'usd::serialize.location' using ANARI_USD_SERIALIZE_LOCATION value");
}
}
if (internals->outputLocation.empty())
{
reportStatus(this, ANARI_DEVICE, ANARI_SEVERITY_WARNING, ANARI_STATUS_INVALID_ARGUMENT,
"Usd Device parameter 'usd::serialize.location' not set, defaulting to './'");
internals->outputLocation = "./";
}
if (!internals->CreateNewBridge(paramData, &reportBridgeStatus, this))
{
reportStatus(this, ANARI_DEVICE, ANARI_SEVERITY_ERROR, ANARI_STATUS_UNKNOWN_ERROR, "Usd Bridge failed to load");
}
}
ANARIArray UsdDevice::CreateDataArray(const void *appMemory,
ANARIMemoryDeleter deleter,
const void *userData,
ANARIDataType dataType,
uint64_t numItems1,
int64_t byteStride1,
uint64_t numItems2,
int64_t byteStride2,
uint64_t numItems3,
int64_t byteStride3)
{
if (!appMemory)
{
UsdDataArray* object = new UsdDataArray(dataType, numItems1, numItems2, numItems3, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIArray)(object);
}
else
{
UsdDataArray* object = new UsdDataArray(appMemory, deleter, userData,
dataType, numItems1, byteStride1, numItems2, byteStride2, numItems3, byteStride3,
this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIArray)(object);
}
}
ANARIArray1D UsdDevice::newArray1D(const void *appMemory,
ANARIMemoryDeleter deleter,
const void *userData,
ANARIDataType type,
uint64_t numItems)
{
return (ANARIArray1D)CreateDataArray(appMemory, deleter, userData,
type, numItems, 0, 1, 0, 1, 0);
}
ANARIArray2D UsdDevice::newArray2D(const void *appMemory,
ANARIMemoryDeleter deleter,
const void *userData,
ANARIDataType type,
uint64_t numItems1,
uint64_t numItems2)
{
return (ANARIArray2D)CreateDataArray(appMemory, deleter, userData,
type, numItems1, 0, numItems2, 0, 1, 0);
}
ANARIArray3D UsdDevice::newArray3D(const void *appMemory,
ANARIMemoryDeleter deleter,
const void *userData,
ANARIDataType type,
uint64_t numItems1,
uint64_t numItems2,
uint64_t numItems3)
{
return (ANARIArray3D)CreateDataArray(appMemory, deleter, userData,
type, numItems1, 0, numItems2, 0, numItems3, 0);
}
void * UsdDevice::mapArray(ANARIArray array)
{
return array ? AnariToUsdObjectPtr(array)->map(this) : nullptr;
}
void UsdDevice::unmapArray(ANARIArray array)
{
if(array)
AnariToUsdObjectPtr(array)->unmap(this);
}
ANARISampler UsdDevice::newSampler(const char *type)
{
const char* name = makeUniqueName("Sampler");
UsdSampler* object = new UsdSampler(name, type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARISampler)(object);
}
ANARIMaterial UsdDevice::newMaterial(const char *material_type)
{
const char* name = makeUniqueName("Material");
UsdMaterial* object = new UsdMaterial(name, material_type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIMaterial)(object);
}
ANARIGeometry UsdDevice::newGeometry(const char *type)
{
const char* name = makeUniqueName("Geometry");
UsdGeometry* object = new UsdGeometry(name, type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIGeometry)(object);
}
ANARISpatialField UsdDevice::newSpatialField(const char * type)
{
const char* name = makeUniqueName("SpatialField");
UsdSpatialField* object = new UsdSpatialField(name, type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARISpatialField)(object);
}
ANARISurface UsdDevice::newSurface()
{
const char* name = makeUniqueName("Surface");
UsdSurface* object = new UsdSurface(name, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARISurface)(object);
}
ANARIVolume UsdDevice::newVolume(const char *type)
{
const char* name = makeUniqueName("Volume");
UsdVolume* object = new UsdVolume(name, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIVolume)(object);
}
ANARIGroup UsdDevice::newGroup()
{
const char* name = makeUniqueName("Group");
UsdGroup* object = new UsdGroup(name, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIGroup)(object);
}
ANARIInstance UsdDevice::newInstance(const char */*type*/)
{
const char* name = makeUniqueName("Instance");
UsdInstance* object = new UsdInstance(name, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIInstance)(object);
}
ANARIWorld UsdDevice::newWorld()
{
const char* name = makeUniqueName("World");
UsdWorld* object = new UsdWorld(name, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIWorld)(object);
}
ANARILight UsdDevice::newLight(const char *type)
{
const char* name = makeUniqueName("Light");
UsdLight* object = new UsdLight(name, type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARILight)(object);
}
ANARICamera UsdDevice::newCamera(const char *type)
{
const char* name = makeUniqueName("Camera");
UsdCamera* object = new UsdCamera(name, type, this);
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARICamera)(object);
}
const char **UsdDevice::getObjectSubtypes(ANARIDataType objectType)
{
return anari::usd::query_object_types(objectType);
}
const void *UsdDevice::getObjectInfo(ANARIDataType objectType,
const char *objectSubtype,
const char *infoName,
ANARIDataType infoType)
{
return anari::usd::query_object_info(
objectType, objectSubtype, infoName, infoType);
}
const void *UsdDevice::getParameterInfo(ANARIDataType objectType,
const char *objectSubtype,
const char *parameterName,
ANARIDataType parameterType,
const char *infoName,
ANARIDataType infoType)
{
return anari::usd::query_param_info(objectType,
objectSubtype,
parameterName,
parameterType,
infoName,
infoType);
}
ANARIRenderer UsdDevice::newRenderer(const char *type)
{
UsdRenderer* object = new UsdRenderer();
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIRenderer)(object);
}
UsdBridge* UsdDevice::getUsdBridge()
{
return internals->bridge.get();
}
void UsdDevice::renderFrame(ANARIFrame frame)
{
// Always commit device changes if not initialized, otherwise no conversion can be performed.
if(!bridgeInitAttempt)
initializeBridge();
if(!isInitialized())
return;
flushCommitList();
internals->bridge->ResetResourceUpdateState(); // Reset the modified flags for committed shared resources
if(frame)
AnariToUsdObjectPtr(frame)->saveUsd(this);
}
const char* UsdDevice::makeUniqueName(const char* name)
{
std::string proposedBaseName(name);
proposedBaseName.append("_");
int postFix = 0;
std::string proposedName = proposedBaseName + std::to_string(postFix);
auto empRes = internals->uniqueNames.emplace(proposedName);
while (!empRes.second)
{
++postFix;
proposedName = proposedBaseName + std::to_string(postFix);
empRes = internals->uniqueNames.emplace(proposedName);
}
return empRes.first->c_str();
}
bool UsdDevice::nameExists(const char* name)
{
return internals->uniqueNames.find(name) != internals->uniqueNames.end();
}
void UsdDevice::addToCommitList(UsdBaseObject* object, bool commitData)
{
if(!object)
return;
if(lockCommitList)
{
this->reportStatus(object, object->getType(), ANARI_SEVERITY_FATAL_ERROR, ANARI_STATUS_INVALID_OPERATION,
"Usd device internal error; addToCommitList called while list is locked");
}
else
{
auto it = std::find_if(commitList.begin(), commitList.end(),
[&object](const CommitListType& entry) -> bool { return entry.first.ptr == object; });
if(it == commitList.end())
commitList.emplace_back(CommitListType(object, commitData));
}
}
void UsdDevice::clearCommitList()
{
removePrimsFromUsd(true); // removeList pointers are taken from commitlist
#ifdef CHECK_MEMLEAKS
for(auto& commitEntry : commitList)
{
logObjDeallocation(commitEntry.first.ptr);
}
#endif
commitList.resize(0);
}
void UsdDevice::flushCommitList()
{
lockCommitList = true;
writeTypeToUsd<(int)ANARI_SAMPLER>();
writeTypeToUsd<(int)ANARI_SPATIAL_FIELD>();
writeTypeToUsd<(int)ANARI_GEOMETRY>();
writeTypeToUsd<(int)ANARI_LIGHT>();
writeTypeToUsd<(int)ANARI_MATERIAL>();
writeTypeToUsd<(int)ANARI_SURFACE>();
writeTypeToUsd<(int)ANARI_VOLUME>();
writeTypeToUsd<(int)ANARI_GROUP>();
writeTypeToUsd<(int)ANARI_INSTANCE>();
writeTypeToUsd<(int)ANARI_WORLD>();
writeTypeToUsd<(int)ANARI_CAMERA>();
removePrimsFromUsd();
clearCommitList();
lockCommitList = false;
}
void UsdDevice::addToVolumeList(UsdVolume* volume)
{
auto it = std::find(volumeList.begin(), volumeList.end(), volume);
if(it == volumeList.end())
volumeList.emplace_back(volume);
}
void UsdDevice::addToResourceStringList(UsdSharedString* string)
{
resourceStringList.push_back(helium::IntrusivePtr<UsdSharedString>(string));
}
void UsdDevice::clearResourceStringList()
{
resourceStringList.resize(0);
}
void UsdDevice::removeFromVolumeList(UsdVolume* volume)
{
auto it = std::find(volumeList.begin(), volumeList.end(), volume);
if(it == volumeList.end())
{
*it = volumeList.back();
volumeList.pop_back();
}
}
template<int typeInt>
void UsdDevice::writeTypeToUsd()
{
for(auto objCommitPair : commitList)
{
auto object = objCommitPair.first;
bool commitData = objCommitPair.second;
if((int)object->getType() == typeInt)
{
using ObjectType = typename AnariToUsdBridgedObject<typeInt>::Type;
ObjectType* typedObj = reinterpret_cast<ObjectType*>(object.ptr);
if(!object->deferCommit(this))
{
bool commitRefs = true;
if(commitData)
commitRefs = object->doCommitData(this);
if(commitRefs)
object->doCommitRefs(this);
}
else
{
this->reportStatus(object.ptr, object->getType(), ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_OPERATION,
"User forgot to at least once commit an ANARI child object of parent object '%s'", typedObj->getName());
}
if(typedObj->getRemovePrim())
{
removeList.push_back(object.ptr); // Just raw pointer, removeList is purged upon commitList clear
}
}
}
}
void UsdDevice::removePrimsFromUsd(bool onlyRemoveHandles)
{
if(!onlyRemoveHandles)
{
for(auto baseObj : removeList)
{
baseObj->remove(this);
}
}
removeList.resize(0);
}
int UsdDevice::getProperty(ANARIObject object,
const char *name,
ANARIDataType type,
void *mem,
uint64_t size,
uint32_t mask)
{
if ((void *)object == (void *)this)
{
if (strEquals(name, "version") && type == ANARI_INT32)
{
writeToVoidP(mem, DEVICE_VERSION_BUILD);
return 1;
}
if (strEquals(name, "version.major") && type == ANARI_INT32)
{
writeToVoidP(mem, DEVICE_VERSION_MAJOR);
return 1;
}
if (strEquals(name, "version.minor") && type == ANARI_INT32)
{
writeToVoidP(mem, DEVICE_VERSION_MINOR);
return 1;
}
if (strEquals(name, "version.patch") && type == ANARI_INT32)
{
writeToVoidP(mem, DEVICE_VERSION_PATCH);
return 1;
}
if (strEquals(name, "version.name") && type == ANARI_STRING)
{
snprintf((char*)mem, size, "%s", DEVICE_VERSION_NAME);
return 1;
}
else if (strEquals(name, "version.name.size") && type == ANARI_UINT64)
{
if (Assert64bitStringLengthProperty(size, UsdLogInfo(this, this, ANARI_DEVICE, "UsdDevice"), "version.name.size"))
{
uint64_t nameLen = strlen(DEVICE_VERSION_NAME)+1;
memcpy(mem, &nameLen, size);
}
return 1;
}
else if (strEquals(name, "geometryMaxIndex") && type == ANARI_UINT64)
{
uint64_t maxIndex = std::numeric_limits<uint64_t>::max(); // Only restricted to int for UsdGeomMesh: GetFaceVertexIndicesAttr() takes a VtArray<int>
writeToVoidP(mem, maxIndex);
return 1;
}
else if (strEquals(name, "extension") && type == ANARI_STRING_LIST)
{
writeToVoidP(mem, anari::usd::query_extensions());
return 1;
}
}
else if(object)
return AnariToUsdObjectPtr(object)->getProperty(name, type, mem, size, this);
return 0;
}
ANARIFrame UsdDevice::newFrame()
{
UsdFrame* object = new UsdFrame(internals->bridge.get());
#ifdef CHECK_MEMLEAKS
logObjAllocation(object);
#endif
return (ANARIFrame)(object);
}
const void * UsdDevice::frameBufferMap(
ANARIFrame fb,
const char *channel,
uint32_t *width,
uint32_t *height,
ANARIDataType *pixelType)
{
if (fb)
return AnariToUsdObjectPtr(fb)->mapBuffer(channel, width, height, pixelType);
return nullptr;
}
void UsdDevice::frameBufferUnmap(ANARIFrame fb, const char *channel)
{
if (fb)
return AnariToUsdObjectPtr(fb)->unmapBuffer(channel);
}
UsdBaseObject* UsdDevice::getBaseObjectPtr(ANARIObject object)
{
return handleIsDevice(object) ? this : (UsdBaseObject*)object;
}
void UsdDevice::setParameter(ANARIObject object,
const char *name,
ANARIDataType type,
const void *mem)
{
if(object)
getBaseObjectPtr(object)->filterSetParam(name, type, mem, this);
}
void UsdDevice::unsetParameter(ANARIObject object, const char * name)
{
if(object)
getBaseObjectPtr(object)->filterResetParam(name);
}
void UsdDevice::unsetAllParameters(ANARIObject object)
{
if(object)
getBaseObjectPtr(object)->resetAllParams();
}
void *UsdDevice::mapParameterArray1D(ANARIObject object,
const char *name,
ANARIDataType dataType,
uint64_t numElements1,
uint64_t *elementStride)
{
auto array = newArray1D(nullptr, nullptr, nullptr, dataType, numElements1);
setParameter(object, name, ANARI_ARRAY1D, &array);
*elementStride = anari::sizeOf(dataType);
AnariToUsdObjectPtr(array)->refDec(helium::RefType::PUBLIC);
return mapArray(array);
}
void *UsdDevice::mapParameterArray2D(ANARIObject object,
const char *name,
ANARIDataType dataType,
uint64_t numElements1,
uint64_t numElements2,
uint64_t *elementStride)
{
auto array = newArray2D(nullptr, nullptr, nullptr, dataType, numElements1, numElements2);
setParameter(object, name, ANARI_ARRAY2D, &array);
*elementStride = anari::sizeOf(dataType);
AnariToUsdObjectPtr(array)->refDec(helium::RefType::PUBLIC);
return mapArray(array);
}
void *UsdDevice::mapParameterArray3D(ANARIObject object,
const char *name,
ANARIDataType dataType,
uint64_t numElements1,
uint64_t numElements2,
uint64_t numElements3,
uint64_t *elementStride)
{
auto array = newArray3D(nullptr,
nullptr,
nullptr,
dataType,
numElements1,
numElements2,
numElements3);
setParameter(object, name, ANARI_ARRAY3D, &array);
*elementStride = anari::sizeOf(dataType);
AnariToUsdObjectPtr(array)->refDec(helium::RefType::PUBLIC);
return mapArray(array);
}
void UsdDevice::unmapParameterArray(ANARIObject object, const char *name)
{
if(!object)
return;
ANARIDataType paramType = ANARI_UNKNOWN;
void* paramAddress = getBaseObjectPtr(object)->getParameter(name, paramType);
if(paramAddress && anari::isArray(paramType))
{
auto arrayAddress = (ANARIArray*)paramAddress;
if(*arrayAddress)
AnariToUsdObjectPtr(*arrayAddress)->unmap(this);
}
}
void UsdDevice::release(ANARIObject object)
{
if(!object)
return;
UsdBaseObject* baseObject = getBaseObjectPtr(object);
bool privatizeArray = anari::isArray(baseObject->getType())
&& baseObject->useCount(helium::RefType::INTERNAL) > 0
&& baseObject->useCount(helium::RefType::PUBLIC) == 1;
#ifdef CHECK_MEMLEAKS
if(!handleIsDevice(object))
logObjDeallocation(baseObject);
#endif
if (baseObject)
baseObject->refDec(helium::RefType::PUBLIC);
if (privatizeArray)
AnariToUsdObjectPtr((ANARIArray)object)->privatize();
}
void UsdDevice::retain(ANARIObject object)
{
if(object)
getBaseObjectPtr(object)->refInc(helium::RefType::PUBLIC);
}
void UsdDevice::commitParameters(ANARIObject object)
{
if(object)
getBaseObjectPtr(object)->commit(this);
}
#ifdef CHECK_MEMLEAKS
namespace
{
template<typename T>
void SharedLogDeallocation(const T* ptr, std::vector<const T*>& allocations, UsdDevice* device)
{
if (ptr)
{
auto it = std::find(allocations.begin(), allocations.end(), ptr);
if(it == allocations.end())
{
std::stringstream errstream;
errstream << "USD Device release of nonexisting or already released/deleted object: 0x" << std::hex << ptr;
device->reportStatus(device, ANARI_DEVICE, ANARI_SEVERITY_FATAL_ERROR, ANARI_STATUS_INVALID_OPERATION, errstream.str().c_str());
}
if(ptr->useCount() == 1)
{
assert(it != allocations.end());
allocations.erase(it);
}
}
}
template<typename T>
bool isAllocated(const T* ptr, const std::vector<const T*>& allocations)
{
auto it = std::find(allocations.begin(), allocations.end(), ptr);
return it != allocations.end();
}
}
void UsdDevice::logObjAllocation(const UsdBaseObject* ptr)
{
allocatedObjects.push_back(ptr);
}
void UsdDevice::logObjDeallocation(const UsdBaseObject* ptr)
{
SharedLogDeallocation(ptr, allocatedObjects, this);
}
void UsdDevice::logStrAllocation(const UsdSharedString* ptr)
{
allocatedStrings.push_back(ptr);
}
void UsdDevice::logStrDeallocation(const UsdSharedString* ptr)
{
SharedLogDeallocation(ptr, allocatedStrings, this);
}
void UsdDevice::logRawAllocation(const void* ptr)
{