-
Notifications
You must be signed in to change notification settings - Fork 10
/
UsdSampler.cpp
204 lines (167 loc) · 6.96 KB
/
UsdSampler.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
// Copyright 2020 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#include "UsdSampler.h"
#include "UsdAnari.h"
#include "UsdDevice.h"
#include "UsdDataArray.h"
DEFINE_PARAMETER_MAP(UsdSampler,
REGISTER_PARAMETER_MACRO("name", ANARI_STRING, name)
REGISTER_PARAMETER_MACRO("usd::name", ANARI_STRING, usdName)
REGISTER_PARAMETER_MACRO("usd::time", ANARI_FLOAT64, timeStep)
REGISTER_PARAMETER_MACRO("usd::timeVarying", ANARI_INT32, timeVarying)
REGISTER_PARAMETER_MACRO("usd::imageUrl", ANARI_STRING, imageUrl)
REGISTER_PARAMETER_MACRO("inAttribute", ANARI_STRING, inAttribute)
REGISTER_PARAMETER_MACRO("image", ANARI_ARRAY, imageData)
REGISTER_PARAMETER_MACRO("wrapMode", ANARI_STRING, wrapS)
REGISTER_PARAMETER_MACRO("wrapMode1", ANARI_STRING, wrapS)
REGISTER_PARAMETER_MACRO("wrapMode2", ANARI_STRING, wrapT)
REGISTER_PARAMETER_MACRO("wrapMode3", ANARI_STRING, wrapR)
)
constexpr UsdSampler::ComponentPair UsdSampler::componentParamNames[]; // Workaround for C++14's lack of inlining constexpr arrays
namespace
{
UsdBridgeSamplerData::WrapMode ANARIToUsdBridgeWrapMode(const char* anariWrapMode)
{
UsdBridgeSamplerData::WrapMode usdWrapMode = UsdBridgeSamplerData::WrapMode::BLACK;
if(anariWrapMode)
{
if (strEquals(anariWrapMode, "clampToEdge"))
{
usdWrapMode = UsdBridgeSamplerData::WrapMode::CLAMP;
}
else if (strEquals(anariWrapMode, "repeat"))
{
usdWrapMode = UsdBridgeSamplerData::WrapMode::REPEAT;
}
else if (strEquals(anariWrapMode, "mirrorRepeat"))
{
usdWrapMode = UsdBridgeSamplerData::WrapMode::MIRROR;
}
}
return usdWrapMode;
}
}
UsdSampler::UsdSampler(const char* name, const char* type, UsdDevice* device)
: BridgedBaseObjectType(ANARI_SAMPLER, name, device)
{
if (strEquals(type, "image1D"))
samplerType = SAMPLER_1D;
else if (strEquals(type, "image2D"))
samplerType = SAMPLER_2D;
else if (strEquals(type, "image3D"))
samplerType = SAMPLER_3D;
else
device->reportStatus(this, ANARI_SAMPLER, ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_ARGUMENT, "UsdSampler '%s' construction failed: type %s not supported", getName(), name);
}
UsdSampler::~UsdSampler()
{
#ifdef OBJECT_LIFETIME_EQUALS_USD_LIFETIME
if(cachedBridge)
cachedBridge->DeleteSampler(usdHandle);
#endif
}
void UsdSampler::remove(UsdDevice* device)
{
applyRemoveFunc(device, &UsdBridge::DeleteSampler);
}
void UsdSampler::updateBoundParameters(bool boundToInstance, const UsdSharedString*const* attribNames, size_t numAttribNames, UsdDevice* device)
{
UsdBridge* usdBridge = device->getUsdBridge();
if(!usdHandle.value)
return;
perInstance = boundToInstance;
UsdLogInfo logInfo(device, this, ANARI_SAMPLER, getName());
// Fix up the position attribute
const UsdSamplerData& paramData = getReadParams();
const char* inAttribName = UsdSharedString::c_str(paramData.inAttribute);
auto [hasMeshDependentAttribs, meshDependentAttribName] =
GetGeomDependentAttributeName(inAttribName, perInstance, attribNames, numAttribNames, logInfo);
if(hasMeshDependentAttribs)
{
double worldTimeStep = device->getReadParams().timeStep;
double dataTimeStep = selectObjTime(paramData.timeStep, worldTimeStep);
UsdBridgeSamplerData::DataMemberId timeVarying;
setSamplerTimeVarying(timeVarying);
usdBridge->ChangeInAttribute(usdHandle, meshDependentAttribName, dataTimeStep, timeVarying);
}
}
bool UsdSampler::deferCommit(UsdDevice* device)
{
return false;
}
bool UsdSampler::doCommitData(UsdDevice* device)
{
UsdBridge* usdBridge = device->getUsdBridge();
if(!device->getReadParams().outputMaterial ||
samplerType == SAMPLER_UNKNOWN)
return false;
const UsdSamplerData& paramData = getReadParams();
UsdBridgeSamplerData::SamplerType type =
(samplerType == SAMPLER_1D ? UsdBridgeSamplerData::SamplerType::SAMPLER_1D :
(samplerType == SAMPLER_2D ? UsdBridgeSamplerData::SamplerType::SAMPLER_2D :
UsdBridgeSamplerData::SamplerType::SAMPLER_3D
)
);
bool isNew = false;
if (!usdHandle.value)
isNew = usdBridge->CreateSampler(getName(), usdHandle, type);
if (paramChanged || isNew)
{
if (paramData.inAttribute && (std::strlen(UsdSharedString::c_str(paramData.inAttribute)) > 0)
&& (paramData.imageUrl || paramData.imageData))
{
bool supportedImage = true;
int numComponents = 0;
if(paramData.imageData)
{
numComponents = static_cast<int>(anari::componentsOf(paramData.imageData->getType()));
if(numComponents > 4)
device->reportStatus(this, ANARI_SAMPLER, ANARI_SEVERITY_WARNING, ANARI_STATUS_INVALID_ARGUMENT,
"UsdSampler '%s' image data has more than 4 components. Anything above the 4th component will be ignored.", paramData.imageData->getName());
}
if(supportedImage)
{
UsdLogInfo logInfo(device, this, ANARI_SAMPLER, getName());
UsdBridgeSamplerData samplerData;
samplerData.Type = type;
double worldTimeStep = device->getReadParams().timeStep;
double dataTimeStep = selectObjTime(paramData.timeStep, worldTimeStep);
samplerData.InAttribute = AnariAttributeToUsdName(UsdSharedString::c_str(paramData.inAttribute), perInstance, logInfo);
if(paramData.imageUrl)
{
samplerData.ImageUrl = UsdSharedString::c_str(paramData.imageUrl);
}
if(paramData.imageData)
{
samplerData.Data = paramData.imageData->getData();
samplerData.ImageName = paramData.imageData->getName();
samplerData.ImageNumComponents = numComponents;
samplerData.DataType = AnariToUsdBridgeType(paramData.imageData->getType());
paramData.imageData->getLayout().copyDims(samplerData.ImageDims);
paramData.imageData->getLayout().copyStride(samplerData.ImageStride);
}
samplerData.WrapS = ANARIToUsdBridgeWrapMode(UsdSharedString::c_str(paramData.wrapS));
samplerData.WrapT = ANARIToUsdBridgeWrapMode(UsdSharedString::c_str(paramData.wrapT));
samplerData.WrapR = ANARIToUsdBridgeWrapMode(UsdSharedString::c_str(paramData.wrapR));
setSamplerTimeVarying(samplerData.TimeVarying);
usdBridge->SetSamplerData(usdHandle, samplerData, dataTimeStep);
}
}
else
{
device->reportStatus(this, ANARI_SAMPLER, ANARI_SEVERITY_ERROR, ANARI_STATUS_INVALID_ARGUMENT,
"UsdSampler '%s' commit failed: missing either the 'inAttribute', or both the 'image' and 'usd::imageUrl' parameter", getName());
}
paramChanged = false;
}
return false;
}
void UsdSampler::setSamplerTimeVarying(UsdBridgeSamplerData::DataMemberId& timeVarying)
{
typedef UsdBridgeSamplerData::DataMemberId DMI;
timeVarying = DMI::ALL
& (isTimeVarying(CType::DATA) ? DMI::ALL : ~DMI::DATA)
& (isTimeVarying(CType::WRAPS) ? DMI::ALL : ~DMI::WRAPS)
& (isTimeVarying(CType::WRAPT) ? DMI::ALL : ~DMI::WRAPT)
& (isTimeVarying(CType::WRAPR) ? DMI::ALL : ~DMI::WRAPR);
}