-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUsdParameterizedObject.h
437 lines (366 loc) · 16.1 KB
/
UsdParameterizedObject.h
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
// Copyright 2020 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <map>
#include <string>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>
#include "UsdAnari.h"
#include "UsdSharedObjects.h"
#include "UsdMultiTypeParameter.h"
#include "helium/utility/IntrusivePtr.h"
#include "anari/frontend/type_utility.h"
class UsdDevice;
class UsdBridge;
class UsdBaseObject;
// When deriving from UsdParameterizedObject<T>, define a a struct T::Data and
// a static void T::registerParams() that registers any member of T::Data using REGISTER_PARAMETER_MACRO()
template<typename T, typename D>
class UsdParameterizedObject
{
public:
struct UsdAnariDataTypeStore
{
ANARIDataType type0 = ANARI_UNKNOWN;
ANARIDataType type1 = ANARI_UNKNOWN;
ANARIDataType type2 = ANARI_UNKNOWN;
ANARIDataType singleType() const { return type0; }
bool isMultiType() const { return type1 != ANARI_UNKNOWN; }
bool typeMatches(ANARIDataType inType) const
{
return inType == type0 ||
(isMultiType() && (inType == type1 || inType == type2));
}
};
struct ParamTypeInfo
{
size_t dataOffset = 0; // offset of data, within paramDataSet D
size_t typeOffset = 0; // offset of type, from data
size_t size = 0; // Total size of data+type
UsdAnariDataTypeStore types;
};
using ParameterizedClassType = UsdParameterizedObject<T, D>;
using ParamContainer = std::map<std::string, ParamTypeInfo>;
void* getParam(const char* name, ANARIDataType& returnType)
{
// Check if name registered
typename ParamContainer::iterator it = registeredParams->find(name);
if (it != registeredParams->end())
{
const ParamTypeInfo& typeInfo = it->second;
void* destAddress = nullptr;
getParamTypeAndAddress(paramDataSets[paramWriteIdx], typeInfo,
returnType, destAddress);
return destAddress;
}
return nullptr;
}
protected:
helium::RefCounted** ptrToRefCountedPtr(void* address) { return reinterpret_cast<helium::RefCounted**>(address); }
UsdBaseObject* toBaseObjectPtr(void* address) { return *reinterpret_cast<UsdBaseObject**>(address); }
ANARIDataType* toAnariDataTypePtr(void* address) { return reinterpret_cast<ANARIDataType*>(address); }
bool isRefCounted(ANARIDataType type) const { return anari::isObject(type) || type == ANARI_STRING; }
void safeRefInc(void* paramPtr, ANARIDataType paramType, bool onWriteParams) // Pointer to the parameter address which holds a helium::RefCounted*
{
helium::RefCounted** refCountedPP = ptrToRefCountedPtr(paramPtr);
if (*refCountedPP)
{
(*refCountedPP)->refInc(helium::RefType::INTERNAL);
if(anari::isObject(paramType))
onParamRefChanged(toBaseObjectPtr(paramPtr), true, onWriteParams);
}
}
void safeRefDec(void* paramPtr, ANARIDataType paramType, bool onWriteParams) // Pointer to the parameter address which holds a helium::RefCounted*
{
helium::RefCounted** refCountedPP = ptrToRefCountedPtr(paramPtr);
if (*refCountedPP)
{
if(anari::isObject(paramType))
onParamRefChanged(toBaseObjectPtr(paramPtr), false, onWriteParams);
helium::RefCounted*& refCountedP = *refCountedPP;
#ifdef CHECK_MEMLEAKS
logDeallocationThroughDevice(allocDevice, refCountedP, paramType);
#endif
assert(refCountedP->useCount(helium::RefType::INTERNAL) > 0);
refCountedP->refDec(helium::RefType::INTERNAL);
refCountedP = nullptr; // Explicitly clear the pointer (see destructor)
}
}
virtual void onParamRefChanged(UsdBaseObject* paramObject, bool incRef, bool onWriteParams) {}
void* paramAddress(D& paramData, const ParamTypeInfo& typeInfo)
{
return reinterpret_cast<char*>(¶mData) + typeInfo.dataOffset;
}
ANARIDataType paramType(void* paramAddress, const ParamTypeInfo& typeInfo)
{
if(typeInfo.types.isMultiType())
return *toAnariDataTypePtr(static_cast<char*>(paramAddress) + typeInfo.typeOffset);
else
return typeInfo.types.singleType();
}
void setMultiParamType(void* paramAddress, const ParamTypeInfo& typeInfo, ANARIDataType newType)
{
if(typeInfo.types.isMultiType())
*toAnariDataTypePtr(static_cast<char*>(paramAddress) + typeInfo.typeOffset) = newType;
}
void getParamTypeAndAddress(D& paramData, const ParamTypeInfo& typeInfo,
ANARIDataType& returnType, void*& returnAddress)
{
returnAddress = paramAddress(paramData, typeInfo);
returnType = paramType(returnAddress, typeInfo);
}
// Convenience function for usd-compatible parameters
void formatUsdName(UsdSharedString* nameStr)
{
char* name = const_cast<char*>(UsdSharedString::c_str(nameStr));
assert(strlen(name) > 0);
auto letter = [](unsigned c) { return ((c - 'A') < 26) || ((c - 'a') < 26); };
auto number = [](unsigned c) { return (c - '0') < 10; };
auto under = [](unsigned c) { return c == '_'; };
unsigned x = *name;
if (!letter(x) && !under(x)) { *name = '_'; }
x = *(++name);
while (x != '\0')
{
if(!letter(x) && !number(x) && !under(x))
*name = '_';
x = *(++name);
};
}
public:
UsdParameterizedObject()
{
static ParamContainer* reg = ParameterizedClassType::registerParams();
registeredParams = reg;
}
~UsdParameterizedObject()
{
// Manually decrease the references on all objects in the read and writeparam datasets
// (since the pointers are relinquished)
auto it = registeredParams->begin();
while (it != registeredParams->end())
{
const ParamTypeInfo& typeInfo = it->second;
ANARIDataType readParamType, writeParamType;
void* readParamAddress = nullptr;
void* writeParamAddress = nullptr;
getParamTypeAndAddress(paramDataSets[paramReadIdx], typeInfo,
readParamType, readParamAddress);
getParamTypeAndAddress(paramDataSets[paramWriteIdx], typeInfo,
writeParamType, writeParamAddress);
// Works even if two parameters point to the same paramdata address, as the content at that address (object pointers) are set to null
if(isRefCounted(readParamType))
safeRefDec(readParamAddress, readParamType, false);
if(isRefCounted(writeParamType))
safeRefDec(writeParamAddress, writeParamType, true);
++it;
}
}
const D& getReadParams() const { return paramDataSets[paramReadIdx]; }
D& getWriteParams() { return paramDataSets[paramWriteIdx]; }
protected:
void setParam(const char* name, ANARIDataType srcType, const void* rawSrc, UsdDevice* device)
{
#ifdef CHECK_MEMLEAKS
allocDevice = device;
#endif
if(srcType == ANARI_UNKNOWN)
{
reportStatusThroughDevice(UsdLogInfo(device, this, ANARI_OBJECT, nullptr), ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_ARGUMENT,
"Attempting to set param %s with type %s", name, AnariTypeToString(srcType));
return;
}
else if(anari::isArray(srcType))
{
// Flatten the source type in case of array
srcType = ANARI_ARRAY;
}
// Check if name registered
typename ParamContainer::iterator it = registeredParams->find(name);
if (it != registeredParams->end())
{
const ParamTypeInfo& typeInfo = it->second;
// Check if type matches
if (typeInfo.types.typeMatches(srcType))
{
ANARIDataType destType;
void* destAddress = nullptr;
getParamTypeAndAddress(paramDataSets[paramWriteIdx], typeInfo,
destType, destAddress);
const void* srcAddress = rawSrc; //temporary src
size_t numBytes = anari::sizeOf(srcType); // Size is determined purely by source data
bool contentUpdate = srcType != destType; // Always do a content update if types differ (in case of multitype params)
// Update data for all the different types
UsdSharedString* sharedStr = nullptr;
if (srcType == ANARI_STRING)
{
// Wrap strings to make them refcounted,
// from that point they are considered normal RefCounteds.
UsdSharedString* destStr = reinterpret_cast<UsdSharedString*>(*ptrToRefCountedPtr(destAddress));
const char* srcCstr = reinterpret_cast<const char*>(srcAddress);
contentUpdate = contentUpdate || !destStr || !strEquals(destStr->c_str(), srcCstr); // Note that execution of strEquals => (srcType == destType)
if(contentUpdate)
{
sharedStr = new UsdSharedString(srcCstr); // Remember to refdec
numBytes = sizeof(void*);
srcAddress = &sharedStr;
#ifdef CHECK_MEMLEAKS
logAllocationThroughDevice(allocDevice, sharedStr, ANARI_STRING);
#endif
}
}
else
contentUpdate = contentUpdate || bool(std::memcmp(destAddress, srcAddress, numBytes));
if(contentUpdate)
{
if(isRefCounted(destType))
safeRefDec(destAddress, destType, true);
std::memcpy(destAddress, srcAddress, numBytes);
if(isRefCounted(srcType))
safeRefInc(destAddress, destType, true);
}
// If a string object has been created, decrease its public refcount (1 at creation)
if (sharedStr)
{
assert(sharedStr->useCount() == 2); // Single public and internal reference
sharedStr->refDec();
}
// Update the type for multitype params (so far only data has been updated)
if(contentUpdate)
setMultiParamType(destAddress, typeInfo, srcType);
if(!strEquals(name, "usd::time")) // Allow for re-use of object as reference at different timestep, without triggering a full re-commit of the referenced object
{
#ifdef TIME_BASED_CACHING
paramChanged = true; //For time-varying parameters, comparisons between content of potentially different timesteps is meaningless
#else
paramChanged = paramChanged || contentUpdate;
#endif
}
}
else
reportStatusThroughDevice(UsdLogInfo(device, this, ANARI_OBJECT, nullptr), ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_ARGUMENT,
"Param %s is not of an accepted type. For example, use %s instead.", name, AnariTypeToString(typeInfo.types.type0));
}
}
void resetParam(const ParamTypeInfo& typeInfo)
{
size_t paramSize = typeInfo.size;
// Copy to existing write param location
ANARIDataType destType;
void* destAddress = nullptr;
getParamTypeAndAddress(paramDataSets[paramWriteIdx], typeInfo,
destType, destAddress);
// Create temporary default-constructed parameter set and find source param address ((multi-)type is of no concern)
D defaultParamData;
void* srcAddress = paramAddress(defaultParamData, typeInfo);
// Make sure to dec existing ptr, as it will be relinquished
if(isRefCounted(destType))
safeRefDec(destAddress, destType, true);
// Just replace contents of the whole parameter structure, single or multiparam
std::memcpy(destAddress, srcAddress, paramSize);
}
void resetParam(const char* name)
{
typename ParamContainer::iterator it = registeredParams->find(name);
if (it != registeredParams->end())
{
const ParamTypeInfo& typeInfo = it->second;
resetParam(typeInfo);
if(!strEquals(name, "usd::time"))
{
paramChanged = true;
}
}
}
void resetParams()
{
auto it = registeredParams->begin();
while (it != registeredParams->end())
{
resetParam(it->second);
++it;
}
paramChanged = true;
}
void transferWriteToReadParams()
{
// Make sure object references are removed for
// the overwritten readparams, and increased for the source writeparams
auto it = registeredParams->begin();
while (it != registeredParams->end())
{
const ParamTypeInfo& typeInfo = it->second;
ANARIDataType srcType, destType;
void* srcAddress = nullptr;
void* destAddress = nullptr;
getParamTypeAndAddress(paramDataSets[paramWriteIdx], typeInfo,
srcType, srcAddress);
getParamTypeAndAddress(paramDataSets[paramReadIdx], typeInfo,
destType, destAddress);
// SrcAddress and destAddress are not the same, but they may specifically contain the same object pointers.
// Branch out on that situation (may also be disabled, which results in a superfluous inc/dec)
if(std::memcmp(destAddress, srcAddress, typeInfo.size))
{
// First inc, then dec (in case branch is taken out and the pointed to object is the same)
if (isRefCounted(srcType))
safeRefInc(srcAddress, srcType, false); // count as increase of readparams ref due to subsequent copy
if (isRefCounted(destType))
safeRefDec(destAddress, destType, false);
// Perform assignment immediately; there may be multiple parameters with the same object target,
// which will be branched out at the compare the second time around
std::memcpy(destAddress, srcAddress, typeInfo.size);
}
++it;
}
}
static ParamContainer* registerParams();
ParamContainer* registeredParams;
typedef T DerivedClassType;
typedef D DataType;
D paramDataSets[2];
constexpr static unsigned int paramReadIdx = 0;
constexpr static unsigned int paramWriteIdx = 1;
bool paramChanged = false;
#ifdef CHECK_MEMLEAKS
UsdDevice* allocDevice = nullptr;
#endif
};
#define DEFINE_PARAMETER_MAP(DefClass, Params) template<> UsdParameterizedObject<DefClass,DefClass::DataType>::ParamContainer* UsdParameterizedObject<DefClass,DefClass::DataType>::registerParams() { static ParamContainer registeredParams; Params return ®isteredParams; }
#define REGISTER_PARAMETER_MACRO(ParamName, ParamType, ParamData) \
registeredParams.emplace( std::make_pair<std::string, ParamTypeInfo>( \
std::string(ParamName), \
{offsetof(DataType, ParamData), 0, sizeof(DataType::ParamData), {ParamType, ANARI_UNKNOWN, ANARI_UNKNOWN}} \
)); \
static_assert(AssertParamDataType<decltype(DataType::ParamData), ParamType>::value, "ANARI type " #ParamType " of member '" #ParamData "' does not correspond to member type");
#define REGISTER_PARAMETER_MULTITYPE_MACRO(ParamName, ParamType0, ParamType1, ParamType2, ParamData) \
{ \
using multitype_t = decltype(DataType::ParamData); \
static_assert(AssertParamDataType<multitype_t::CDataType0, ParamType0>::value, "MultiTypeParams registration: ParamType0 " #ParamType0 " of member '" #ParamData "' doesn't match AnariType0"); \
static_assert(AssertParamDataType<multitype_t::CDataType1, ParamType1>::value, "MultiTypeParams registration: ParamType1 " #ParamType1 " of member '" #ParamData "' doesn't match AnariType1"); \
static_assert(AssertParamDataType<multitype_t::CDataType2, ParamType2>::value, "MultiTypeParams registration: ParamType2 " #ParamType2 " of member '" #ParamData "' doesn't match AnariType2"); \
size_t dataOffset = offsetof(DataType, ParamData); \
size_t typeOffset = offsetof(DataType, ParamData.type); \
registeredParams.emplace( std::make_pair<std::string, ParamTypeInfo>( \
std::string(ParamName), \
{dataOffset, typeOffset - dataOffset, sizeof(DataType::ParamData), {ParamType0, ParamType1, ParamType2}} \
)); \
}
// Static assert explainer: gets the element type of the array via the decltype of *std::begin(), which in turn accepts an array
#define REGISTER_PARAMETER_ARRAY_MACRO(ParamName, ParamNameSuffix, ParamType, ParamData, NumEntries) \
{ \
using element_type_t = std::remove_reference_t<decltype(*std::begin(std::declval<decltype(DataType::ParamData)&>()))>; \
static_assert(AssertParamDataType<element_type_t, ParamType>::value, "ANARI type " #ParamType " of member '" #ParamData "' does not correspond to member type"); \
static_assert(sizeof(decltype(DataType::ParamData)) == sizeof(element_type_t)*NumEntries, "Number of elements of member '" #ParamData "' does not correspond with member declaration."); \
size_t offset0 = offsetof(DataType, ParamData[0]); \
size_t offset1 = offsetof(DataType, ParamData[1]); \
size_t paramSize = offset1-offset0; \
for(int i = 0; i < NumEntries; ++i) \
{ \
registeredParams.emplace( std::make_pair<std::string, ParamTypeInfo>( \
ParamName + std::to_string(i) + ParamNameSuffix, \
{offset0+paramSize*i, 0, paramSize, {ParamType, ANARI_UNKNOWN, ANARI_UNKNOWN}} \
)); \
} \
}