-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
lightAdapter.cpp
325 lines (282 loc) · 11.1 KB
/
lightAdapter.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
//
// Copyright 2017 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/usdImaging/usdImaging/lightAdapter.h"
#include "pxr/usdImaging/usdImaging/dataSourcePrim.h"
#include "pxr/usdImaging/usdImaging/delegate.h"
#include "pxr/usdImaging/usdImaging/indexProxy.h"
#include "pxr/usdImaging/usdImaging/materialParamUtils.h"
#include "pxr/usdImaging/usdImaging/tokens.h"
#include "pxr/imaging/hd/light.h"
#include "pxr/imaging/hd/material.h"
#include "pxr/usd/ar/resolverScopedCache.h"
#include "pxr/usd/ar/resolverContextBinder.h"
#include "pxr/usd/usdLux/lightAPI.h"
#include "pxr/base/tf/envSetting.h"
PXR_NAMESPACE_OPEN_SCOPE
TF_REGISTRY_FUNCTION(TfType)
{
typedef UsdImagingLightAdapter Adapter;
TfType t = TfType::Define<Adapter, TfType::Bases<Adapter::BaseAdapter> >();
t.SetFactory< UsdImagingPrimAdapterFactory<Adapter> >();
}
TF_DEFINE_ENV_SETTING(USDIMAGING_ENABLE_SCENE_LIGHTS, 1,
"Enable loading scene lights.");
/*static*/
bool UsdImagingLightAdapter::IsEnabledSceneLights() {
static bool _v = TfGetEnvSetting(USDIMAGING_ENABLE_SCENE_LIGHTS) == 1;
return _v;
}
HdContainerDataSourceHandle
UsdImagingLightAdapter::GetImagingSubprimData(
UsdPrim const& prim,
TfToken const& subprim,
const UsdImagingDataSourceStageGlobals &stageGlobals)
{
if (subprim.IsEmpty()) {
return UsdImagingDataSourcePrim::New(
prim.GetPath(), prim, stageGlobals);
}
return nullptr;
}
HdDataSourceLocatorSet
UsdImagingLightAdapter::InvalidateImagingSubprim(
UsdPrim const& prim,
TfToken const& subprim,
TfTokenVector const& properties,
UsdImagingPropertyInvalidationType invalidationType)
{
return UsdImagingDataSourcePrim::Invalidate(
prim, subprim, properties, invalidationType);
}
bool
UsdImagingLightAdapter::IsSupported(UsdImagingIndexProxy const* index) const
{
return IsEnabledSceneLights() &&
index->IsSprimTypeSupported(HdPrimTypeTokens->light);
}
SdfPath
UsdImagingLightAdapter::Populate(UsdPrim const& prim,
UsdImagingIndexProxy* index,
UsdImagingInstancerContext const* instancerContext)
{
return _AddSprim(HdPrimTypeTokens->light, prim, index, instancerContext);
}
void
UsdImagingLightAdapter::_RemovePrim(SdfPath const& cachePath,
UsdImagingIndexProxy* index)
{
_UnregisterLightCollections(cachePath);
index->RemoveSprim(HdPrimTypeTokens->domeLight, cachePath);
}
void
UsdImagingLightAdapter::MarkCollectionsDirty(UsdPrim const& prim,
SdfPath const& cachePath,
UsdImagingIndexProxy* index)
{
index->MarkSprimDirty(cachePath, HdLight::DirtyCollection);
}
bool
UsdImagingLightAdapter::_UpdateCollectionsChanged(UsdPrim const& prim) const {
UsdImaging_CollectionCache &collectionCache = _GetCollectionCache();
UsdLuxLightAPI light(prim);
bool lightColChanged = collectionCache.UpdateCollection(light.GetLightLinkCollectionAPI());
bool shadowColChanged = collectionCache.UpdateCollection(light.GetShadowLinkCollectionAPI());
return lightColChanged || shadowColChanged;
}
void
UsdImagingLightAdapter::_UnregisterLightCollections(SdfPath const& cachePath) {
UsdImaging_CollectionCache &collectionCache = _GetCollectionCache();
SdfPath lightLinkPath = cachePath.AppendProperty(UsdImagingTokens->collectionLightLink);
collectionCache.RemoveCollection(_GetStage(), lightLinkPath);
SdfPath shadowLinkPath = cachePath.AppendProperty(UsdImagingTokens->collectionShadowLink);
collectionCache.RemoveCollection(_GetStage(), shadowLinkPath);
}
void
UsdImagingLightAdapter::_RegisterLightCollections(UsdPrim const& prim) {
_UpdateCollectionsChanged(prim);
}
void
UsdImagingLightAdapter::TrackVariability(UsdPrim const& prim,
SdfPath const& cachePath,
HdDirtyBits* timeVaryingBits,
UsdImagingInstancerContext const*
instancerContext) const
{
// Discover time-varying transforms.
_IsTransformVarying(prim,
HdLight::DirtyBits::DirtyTransform,
UsdImagingTokens->usdVaryingXform,
timeVaryingBits);
// Discover time-varying visibility.
_IsVarying(prim,
UsdGeomTokens->visibility,
HdLight::DirtyBits::DirtyParams,
UsdImagingTokens->usdVaryingVisibility,
timeVaryingBits,
true);
// Determine if the light material network is time varying.
if (UsdImagingIsHdMaterialNetworkTimeVarying(prim)) {
*timeVaryingBits |= HdLight::DirtyBits::DirtyResource;
}
// If any of the light attributes is time varying
// we will assume all light params are time-varying.
const std::vector<UsdAttribute> &attrs = prim.GetAttributes();
for (UsdAttribute const& attr : attrs) {
// Don't double-count transform attrs.
if (UsdGeomXformable::IsTransformationAffectedByAttrNamed(
attr.GetName())) {
continue;
}
if (attr.GetNumTimeSamples()>1){
*timeVaryingBits |= HdLight::DirtyBits::DirtyParams;
break;
}
}
UsdImagingPrimvarDescCache* primvarDescCache = _GetPrimvarDescCache();
// XXX Cache primvars for lights.
{
// Establish a primvar desc cache entry.
HdPrimvarDescriptorVector& vPrimvars =
primvarDescCache->GetPrimvars(cachePath);
// Compile a list of primvars to check.
std::vector<UsdGeomPrimvar> primvars;
UsdImaging_InheritedPrimvarStrategy::value_type inheritedPrimvarRecord =
_GetInheritedPrimvars(prim.GetParent());
if (inheritedPrimvarRecord) {
primvars = inheritedPrimvarRecord->primvars;
}
UsdGeomPrimvarsAPI primvarsAPI(prim);
std::vector<UsdGeomPrimvar> local = primvarsAPI.GetPrimvarsWithValues();
primvars.insert(primvars.end(), local.begin(), local.end());
for (auto const &pv : primvars) {
_ComputeAndMergePrimvar(prim, pv, UsdTimeCode(), &vPrimvars);
}
}
}
// Thread safe.
// * Populate dirty bits for the given \p time.
void
UsdImagingLightAdapter::UpdateForTime(UsdPrim const& prim,
SdfPath const& cachePath,
UsdTimeCode time,
HdDirtyBits requestedBits,
UsdImagingInstancerContext const*
instancerContext) const
{
}
HdDirtyBits
UsdImagingLightAdapter::ProcessPropertyChange(UsdPrim const& prim,
SdfPath const& cachePath,
TfToken const& propertyName)
{
if (UsdGeomXformable::IsTransformationAffectedByAttrNamed(propertyName)) {
return HdLight::DirtyBits::DirtyTransform;
}
if (TfStringStartsWith(propertyName.GetString(), UsdImagingTokens->collectionShadowLink.GetString()) ||
TfStringStartsWith(propertyName.GetString(), UsdImagingTokens->collectionLightLink.GetString())) {
if (_UpdateCollectionsChanged(prim)) {
return HdLight::DirtyBits::DirtyCollection;
}
}
// "DirtyParam" is the catch-all bit for light params.
return HdLight::DirtyBits::DirtyParams;
}
void
UsdImagingLightAdapter::MarkDirty(UsdPrim const& prim,
SdfPath const& cachePath,
HdDirtyBits dirty,
UsdImagingIndexProxy* index)
{
index->MarkSprimDirty(cachePath, dirty);
}
void
UsdImagingLightAdapter::MarkTransformDirty(UsdPrim const& prim,
SdfPath const& cachePath,
UsdImagingIndexProxy* index)
{
static const HdDirtyBits transformDirty = HdLight::DirtyTransform;
index->MarkSprimDirty(cachePath, transformDirty);
}
void
UsdImagingLightAdapter::MarkVisibilityDirty(UsdPrim const& prim,
SdfPath const& cachePath,
UsdImagingIndexProxy* index)
{
static const HdDirtyBits paramsDirty = HdLight::DirtyParams;
index->MarkSprimDirty(cachePath, paramsDirty);
}
void
UsdImagingLightAdapter::MarkLightParamsDirty(UsdPrim const& prim,
SdfPath const& cachePath,
UsdImagingIndexProxy* index)
{
static const HdDirtyBits paramsDirty = HdLight::DirtyParams;
index->MarkSprimDirty(cachePath, paramsDirty);
}
VtValue
UsdImagingLightAdapter::GetMaterialResource(UsdPrim const &prim,
SdfPath const& cachePath,
UsdTimeCode time) const
{
if (!prim.HasAPI<UsdLuxLightAPI>()) {
TF_RUNTIME_ERROR("Expected light prim at <%s> to have an applied API "
"of type 'UsdLuxLightAPI'; ignoring",
prim.GetPath().GetText());
return VtValue();
}
// Bind the usd stage's resolver context for correct asset resolution.
ArResolverContextBinder binder(prim.GetStage()->GetPathResolverContext());
ArResolverScopedCache resolverCache;
HdMaterialNetworkMap networkMap;
UsdImagingBuildHdMaterialNetworkFromTerminal(
prim,
HdMaterialTerminalTokens->light,
_GetShaderSourceTypes(),
_GetMaterialRenderContexts(),
&networkMap,
time);
if (!_GetSceneLightsEnabled()) {
// When scene lights are disabeled we need to mark them as disabled
// by setting the intensity value to 0. This parameter is found on
// the terminal node, which is the last node in the light network.
networkMap.map[HdMaterialTerminalTokens->light].nodes.back().
parameters[HdLightTokens->intensity] = 0.0f;
}
return VtValue(networkMap);
}
SdfPath
UsdImagingLightAdapter::_AddSprim(
const TfToken& primType,
const UsdPrim& usdPrim,
UsdImagingIndexProxy* index,
const UsdImagingInstancerContext* instancerContext)
{
SdfPath cachePath = ResolveCachePath(usdPrim.GetPath(), instancerContext);
UsdPrim proxyPrim = _GetPrim(ResolveProxyPrimPath(
cachePath, instancerContext));
if (instancerContext != nullptr) {
index->InsertSprim(
primType, cachePath, proxyPrim, instancerContext->instancerAdapter);
index->RemovePrimInfoDependency(cachePath);
index->AddDependency(cachePath, usdPrim);
} else {
index->InsertSprim(primType, cachePath, proxyPrim);
}
HD_PERF_COUNTER_INCR(UsdImagingTokens->usdPopulatedPrimCount);
_RegisterLightCollections(proxyPrim);
return cachePath;
}
void
UsdImagingLightAdapter::_RemoveSprim(
const TfToken& primType,
const SdfPath& cachePath,
UsdImagingIndexProxy* index)
{
_UnregisterLightCollections(cachePath);
index->RemoveSprim(primType, cachePath);
}
PXR_NAMESPACE_CLOSE_SCOPE