-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUsdBridgedBaseObject.h
245 lines (213 loc) · 6.85 KB
/
UsdBridgedBaseObject.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
// Copyright 2020 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "UsdBaseObject.h"
#include "UsdBridge/UsdBridge.h"
#include "UsdDevice.h"
#include "UsdDataArray.h"
#include <cmath>
#include <utility>
enum class UsdEmptyComponents
{
};
template<typename T, typename D, typename H, typename C = UsdEmptyComponents>
class UsdBridgedBaseObject : public UsdParameterizedBaseObject<T, D>
{
protected:
using CType = C;
// Timevarying helper functions (classes workaround to allow for partial specialization)
template<typename IT, typename ID, typename IH, typename IC>
class TimeVaryingClass
{
public:
bool findTimeVarying(const char* name, IC& component)
{
for(auto& cmpName : IT::componentParamNames)
{
if(strEquals(name, cmpName.second))
{
component = cmpName.first;
return true;
}
}
return false;
}
void setTimeVarying(UsdBridgedBaseObject<IT,ID,IH,IC>* bridgedObj, IC component, bool value)
{
ID& params = bridgedObj->getWriteParams();
int bit = (1 << static_cast<int>(component));
params.timeVarying = value ? (params.timeVarying | bit) : (params.timeVarying & ~bit);
}
};
template<typename IT, typename ID, typename IH>
class TimeVaryingClass<IT, ID, IH, UsdEmptyComponents>
{
public:
bool findTimeVarying(const char* name, UsdEmptyComponents& component)
{
return false;
}
void setTimeVarying(UsdBridgedBaseObject<IT,ID,IH,UsdEmptyComponents>* bridgedObj, UsdEmptyComponents component, bool value)
{
}
};
bool setTimeVaryingParam(const char *name,
ANARIDataType type,
const void *mem,
UsdDevice* device)
{
static const char* paramName = "usd::timeVarying.";
bool value = *(reinterpret_cast<const bool*>(mem));
if (type == ANARI_BOOL)
{
if (strcmp(name, paramName) > 0)
{
const char* secondPart = name + strlen(paramName);
C component;
TimeVaryingClass<T,D,H,C> timevarHelper;
if(timevarHelper.findTimeVarying(secondPart, component))
{
timevarHelper.setTimeVarying(this, component, value);
return true;
}
}
}
return false;
}
bool isTimeVarying(C component) const
{
const D& params = this->getReadParams();
return params.timeVarying & (1 << static_cast<int>(component));
}
bool setRemovePrimParam(const char *name,
ANARIDataType type,
const void *mem,
UsdDevice* device)
{
if (type == ANARI_BOOL)
{
if (strcmp(name, "usd::removePrim") == 0)
{
removePrim = true;
return true;
}
}
return false;
}
public:
using ComponentPair = std::pair<C, const char*>; // Used to define a componentParamNames
UsdBridgedBaseObject(ANARIDataType t, const char* name, UsdDevice* device)
: UsdParameterizedBaseObject<T, D>(t, device)
, uniqueName(name)
{
}
H getUsdHandle() const { return usdHandle; }
const char* getName() const override { return this->getReadParams().usdName ? this->getReadParams().usdName->c_str() : uniqueName; }
void filterSetParam(const char *name,
ANARIDataType type,
const void *mem,
UsdDevice* device) override
{
if(!this->setTimeVaryingParam(name, type, mem, device))
if (!this->setNameParam(name, type, mem, device))
if(!this->setRemovePrimParam(name, type, mem, device))
this->setParam(name, type, mem, device);
}
int getProperty(const char *name,
ANARIDataType type,
void *mem,
uint64_t size,
UsdDevice* device) override
{
int nameResult = this->getNameProperty(name, type, mem, size, device);
if(!nameResult)
{
UsdBridge* usdBridge = device->getUsdBridge();
if(!usdBridge)
{
reportStatusThroughDevice(UsdLogInfo(device, this, ANARI_OBJECT, nullptr), ANARI_SEVERITY_WARNING, ANARI_STATUS_NO_ERROR,
"%s parameter '%s' cannot be read with getProperty(); it requires a succesful device parameter commit.", getName(), name);
}
if (type == ANARI_STRING && strEquals(name, "usd::primPath"))
{
const char* primPath = usdBridge->GetPrimPath(&usdHandle);
snprintf((char*)mem, size, "%s", primPath);
return 1;
}
else if (type == ANARI_UINT64 && strEquals(name, "usd::primPath.size"))
{
if (Assert64bitStringLengthProperty(size, UsdLogInfo(device, this, ANARI_OBJECT, this->getName()), "usd::primPath.size"))
{
const char* primPath = usdBridge->GetPrimPath(&usdHandle);
uint64_t nameLen = strlen(primPath)+1;
memcpy(mem, &nameLen, size);
}
return 1;
}
}
return nameResult;
}
virtual void commit(UsdDevice* device) override
{
#ifdef OBJECT_LIFETIME_EQUALS_USD_LIFETIME
cachedBridge = device->getUsdBridge();
#endif
UsdParameterizedBaseObject<T, D>::commit(device);
}
double selectObjTime(double objTimeStep, double worldTimeStep)
{
return
#ifdef VALUE_CLIP_RETIMING
!std::isnan(objTimeStep) ? objTimeStep :
#endif
worldTimeStep;
}
double selectRefTime(double refTimeStep, double objTimeStep, double worldTimeStep)
{
return
#ifdef VALUE_CLIP_RETIMING
!std::isnan(refTimeStep) ? refTimeStep :
(!std::isnan(objTimeStep) ? objTimeStep : worldTimeStep);
#else
worldTimeStep;
#endif
}
bool getRemovePrim() const { return removePrim; }
protected:
typedef UsdBridgedBaseObject<T,D,H,C> BridgedBaseObjectType;
typedef void (UsdBridge::*UsdBridgeMemFn)(H handle);
void applyRemoveFunc(UsdDevice* device, UsdBridgeMemFn func)
{
UsdBridge* usdBridge = device->getUsdBridge();
if(usdBridge && usdHandle.value)
(usdBridge->*func)(usdHandle);
}
const char* uniqueName;
H usdHandle;
bool removePrim = false;
#ifdef OBJECT_LIFETIME_EQUALS_USD_LIFETIME
UsdBridge* cachedBridge = nullptr;
#endif
};
template<typename T, typename D, typename H, typename C>
inline bool UsdObjectNotInitialized(const UsdBridgedBaseObject<T,D,H,C>* obj)
{
return obj && !obj->getUsdHandle().value;
}
template<typename T>
inline bool UsdObjectNotInitialized(UsdDataArray* objects)
{
if (!objects)
return false;
bool notInitialized = false;
if(anari::isObject(objects->getType()))
{
const T* const * object = reinterpret_cast<const T* const *>(objects->getData());
uint64_t numObjects = objects->getLayout().numItems1;
for(int i = 0; i < numObjects; ++i)
{
notInitialized = notInitialized || UsdObjectNotInitialized(object[i]);
}
}
return notInitialized;
}