-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
changes.cpp
2054 lines (1835 loc) · 78.9 KB
/
changes.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 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
/// \file Changes.cpp
#include "pxr/pxr.h"
#include "pxr/usd/pcp/changes.h"
#include "pxr/usd/pcp/cache.h"
#include "pxr/usd/pcp/debugCodes.h"
#include "pxr/usd/pcp/dependencies.h"
#include "pxr/usd/pcp/instancing.h"
#include "pxr/usd/pcp/layerStack.h"
#include "pxr/usd/pcp/layerStackRegistry.h"
#include "pxr/usd/pcp/pathTranslation.h"
#include "pxr/usd/pcp/utils.h"
#include "pxr/usd/sdf/changeList.h"
#include "pxr/usd/sdf/layer.h"
#include "pxr/usd/ar/resolverContextBinder.h"
#include "pxr/base/trace/trace.h"
PXR_NAMESPACE_OPEN_SCOPE
static
void
Pcp_SubsumeDescendants(SdfPathSet* pathSet)
{
SdfPathSet::iterator prefixIt = pathSet->begin(), end = pathSet->end();
while (prefixIt != end) {
// Find the range of paths under path *prefixIt.
SdfPathSet::iterator first = prefixIt;
SdfPathSet::iterator last = ++first;
while (last != end && last->HasPrefix(*prefixIt)) {
++last;
}
// Remove the range.
pathSet->erase(first, last);
// Next path is not under previous path.
prefixIt = last;
}
}
void
Pcp_SubsumeDescendants(SdfPathSet* pathSet, const SdfPath& prefix)
{
// Start at first path in pathSet that is prefix or greater.
SdfPathSet::iterator first = pathSet->lower_bound(prefix);
// Scan for next path in pathSet that does not have prefix as a prefix.
SdfPathSet::iterator last = first;
SdfPathSet::iterator end = pathSet->end();
while (last != end && last->HasPrefix(prefix)) {
++last;
}
// Erase the paths in the range.
pathSet->erase(first, last);
}
PcpLifeboat::PcpLifeboat()
{
// Do nothing
}
PcpLifeboat::~PcpLifeboat()
{
// Do nothing
}
void
PcpLifeboat::Retain(const SdfLayerRefPtr& layer)
{
_layers.insert(layer);
}
void
PcpLifeboat::Retain(const PcpLayerStackRefPtr& layerStack)
{
_layerStacks.insert(layerStack);
}
const std::set<PcpLayerStackRefPtr>&
PcpLifeboat::GetLayerStacks() const
{
return _layerStacks;
}
void
PcpLifeboat::Swap(PcpLifeboat& other)
{
std::swap(_layers, other._layers);
std::swap(_layerStacks, other._layerStacks);
}
PcpChanges::PcpChanges()
{
// Do nothing
}
PcpChanges::~PcpChanges()
{
// Do nothing
}
#define PCP_APPEND_DEBUG(...) \
if (!debugSummary) ; else \
*debugSummary += TfStringPrintf(__VA_ARGS__)
enum Pcp_ChangesLayerStackChange {
Pcp_ChangesLayerStackChangeNone,
Pcp_ChangesLayerStackChangeSignificant,
Pcp_ChangesLayerStackChangeMaybeSignificant
};
static
Pcp_ChangesLayerStackChange
Pcp_EntryRequiresLayerStackChange(const SdfChangeList::Entry& entry)
{
// If a layer's content was entirely replaced, we must blow layer stacks.
if (entry.flags.didReplaceContent) {
return Pcp_ChangesLayerStackChangeSignificant;
}
// XXX: This only requires blowing the layer stacks using this
// identifier that haven't also been updated to use the new
// identifier.
if (entry.flags.didChangeIdentifier) {
return Pcp_ChangesLayerStackChangeSignificant;
}
// Order of layers in layer stack probably changed.
// XXX: Don't return true if these changes don't affect the
// layer tree order.
for (auto const &p: entry.infoChanged) {
if (p.first == SdfFieldKeys->Owner ||
p.first == SdfFieldKeys->SessionOwner ||
p.first == SdfFieldKeys->HasOwnedSubLayers) {
return Pcp_ChangesLayerStackChangeSignificant;
}
}
// Layer was added or removed.
TF_FOR_ALL(i, entry.subLayerChanges) {
if (i->second == SdfChangeList::SubLayerAdded ||
i->second == SdfChangeList::SubLayerRemoved) {
// Whether the change is significant depends on whether any
// added/removed layer is significant. To check that we need
// the help of each cache using this layer.
return Pcp_ChangesLayerStackChangeMaybeSignificant;
}
}
return Pcp_ChangesLayerStackChangeNone;
}
static
bool
Pcp_EntryRequiresLayerStackOffsetsChange(
const SdfLayerHandle &layer,
const SdfChangeList::Entry& entry,
bool *rootLayerStacksMayNeedTcpsRecompute)
{
// Check any changes to actual sublayer offsets.
for (const auto &it : entry.subLayerChanges) {
if (it.second == SdfChangeList::SubLayerOffset) {
return true;
}
}
// Check if the TCPS metadata field changed. Note that this encapsulates
// both changes to timeCodesPerSecond and framesPerSecond as the
// SdfChangeManager will send a send a FPS change as a change to TCPS as
// well when the FPS is relevant as a fallback for an unspecified TCPS.
auto it = entry.FindInfoChange(SdfFieldKeys->TimeCodesPerSecond);
if (it != entry.infoChanged.end()) {
// The old and new values in the entry already account for the
// "computed TCPS" when the FPS is used as a fallback. So we still have
// to check if the computed TCPS changed.
//
// We also have to account here for the case where both the FPS and
// TCPS are unspecified, either before or after the change, as the old
// or new entry value will be empty which is equivalent to specifying
// the TCPS fallback value from the SdfSchema.
const VtValue &oldComputedTcps = it->second.first;
const VtValue &newComputedTcps = it->second.second;
auto _MatchesFallback = [&layer](const VtValue &val) {
return layer->GetSchema().GetFallback(
SdfFieldKeys->TimeCodesPerSecond) == val;
};
// If the old and new TCPS values are the same, this indicates that
// either the old or new TCPS field is actually unauthored and is
// falling back to an authored FPS value. This is not a computed TCPS
// change for the layer itself and doesn't directly affect the offset
// for the layer relative to other layers.
//
// However, if this layer is the session or root layer of a cache's
// root layer stack, this change could still have an effect on the
// computed overall TCPS of that layer stack. That's why we still flag
// this change so we can check for this case after layer changes are
// processed.
//
// XXX: Note this requires an interpretation of the change information
// coming out of Sdf involving knowledge of specific implementation
// details of Sdf change management. Ideally Sdf should provide a
// notification differentiation between "authored TCPS" changed vs
// "computed TCPS" changed.
if (oldComputedTcps == newComputedTcps) {
if (rootLayerStacksMayNeedTcpsRecompute) {
*rootLayerStacksMayNeedTcpsRecompute = true;
}
return false;
}
// If either old or new value is empty, and the other value matches the
// fallback, then we don't have an effective TCPS change.
if ((oldComputedTcps.IsEmpty() && _MatchesFallback(newComputedTcps)) ||
(newComputedTcps.IsEmpty() && _MatchesFallback(oldComputedTcps))) {
return false;
}
return true;
}
return false;
}
static
bool
Pcp_EntryRequiresPrimIndexChange(const SdfChangeList::Entry& entry)
{
// Inherits, specializes, reference or variants changed.
if (entry.flags.didChangePrimInheritPaths ||
entry.flags.didChangePrimSpecializes ||
entry.flags.didChangePrimReferences ||
entry.flags.didChangePrimVariantSets) {
return true;
}
// Payload, permission or variant selection changed.
// XXX: We don't require a prim graph change if:
// we add/remove an unrequested payload;
// permissions change doesn't add/remove any specs
// that themselves require prim graph changes;
// variant selection was invalid and is still invalid.
for (auto const &p: entry.infoChanged) {
if (p.first == SdfFieldKeys->Payload ||
p.first == SdfFieldKeys->Permission ||
p.first == SdfFieldKeys->VariantSelection ||
p.first == SdfFieldKeys->Instanceable) {
return true;
}
}
return false;
}
enum {
Pcp_EntryChangeSpecsAddInert = 1,
Pcp_EntryChangeSpecsRemoveInert = 2,
Pcp_EntryChangeSpecsAddNonInert = 4,
Pcp_EntryChangeSpecsRemoveNonInert = 8,
Pcp_EntryChangeSpecsTargets = 16,
Pcp_EntryChangeSpecsConnections = 32,
Pcp_EntryChangeSpecsAdd =
Pcp_EntryChangeSpecsAddInert |
Pcp_EntryChangeSpecsAddNonInert,
Pcp_EntryChangeSpecsRemove =
Pcp_EntryChangeSpecsRemoveInert |
Pcp_EntryChangeSpecsRemoveNonInert,
Pcp_EntryChangeSpecsInert =
Pcp_EntryChangeSpecsAddInert |
Pcp_EntryChangeSpecsRemoveInert,
Pcp_EntryChangeSpecsNonInert =
Pcp_EntryChangeSpecsAddNonInert |
Pcp_EntryChangeSpecsRemoveNonInert
};
static int
Pcp_EntryRequiresPrimSpecsChange(const SdfChangeList::Entry& entry)
{
int result = 0;
result |= entry.flags.didAddInertPrim
? Pcp_EntryChangeSpecsAddInert : 0;
result |= entry.flags.didRemoveInertPrim
? Pcp_EntryChangeSpecsRemoveInert : 0;
result |= entry.flags.didAddNonInertPrim
? Pcp_EntryChangeSpecsAddNonInert : 0;
result |= entry.flags.didRemoveNonInertPrim
? Pcp_EntryChangeSpecsRemoveNonInert : 0;
return result;
}
static int
Pcp_EntryRequiresPropertySpecsChange(const SdfChangeList::Entry& entry)
{
int result = 0;
result |= entry.flags.didAddPropertyWithOnlyRequiredFields
? Pcp_EntryChangeSpecsAddInert : 0;
result |= entry.flags.didRemovePropertyWithOnlyRequiredFields
? Pcp_EntryChangeSpecsRemoveInert : 0;
result |= entry.flags.didAddProperty
? Pcp_EntryChangeSpecsAddNonInert : 0;
result |= entry.flags.didRemoveProperty
? Pcp_EntryChangeSpecsRemoveNonInert : 0;
if (entry.flags.didChangeRelationshipTargets) {
result |= Pcp_EntryChangeSpecsTargets;
}
if (entry.flags.didChangeAttributeConnection) {
result |= Pcp_EntryChangeSpecsConnections;
}
return result;
}
static bool
Pcp_EntryRequiresPropertyIndexChange(const SdfChangeList::Entry& entry)
{
for (auto const &p: entry.infoChanged) {
if (p.first == SdfFieldKeys->Permission) {
return true;
}
}
return false;
}
// Returns true if any changed info field in the changelist entry is a
// field that may be an input used to compute file format arguments for a
// dynamic file format used by a prim index in the cache. This is a minimal
// filtering by field name only, ignoring all other context.
static bool
Pcp_ChangeMayAffectDynamicFileFormatArguments(
const PcpCache* cache,
const SdfChangeList::Entry& entry,
std::string* debugSummary)
{
// Early out if the cache has no dynamic file format dependencies.
if (cache->HasAnyDynamicFileFormatArgumentDependencies()) {
for (const auto& change : entry.infoChanged) {
if (cache->IsPossibleDynamicFileFormatArgumentField(change.first)) {
PCP_APPEND_DEBUG(" Info change for field '%s' may affect "
"dynamic file format arguments\n",
change.first.GetText());
return true;
}
}
}
return false;
}
static bool
Pcp_PrimSpecOrDescendantHasRelocates(const SdfLayerHandle& layer,
const SdfPath& primPath)
{
TRACE_FUNCTION();
if (layer->HasField(primPath, SdfFieldKeys->Relocates)) {
return true;
}
TfTokenVector primChildNames;
if (layer->HasField(primPath, SdfChildrenKeys->PrimChildren,
&primChildNames)) {
for (const TfToken& name : primChildNames) {
if (Pcp_PrimSpecOrDescendantHasRelocates(
layer, primPath.AppendChild(name))) {
return true;
}
}
}
return false;
}
// Returns true if any of the info changed in the change list affects the file
// format arguments of for a dynamic file format under the prim index at path.
static bool
Pcp_DoesInfoChangeAffectFileFormatArguments(
PcpCache const *cache, const SdfPath& primIndexPath,
const SdfChangeList::Entry &changes,
std::string *debugSummary)
{
PCP_APPEND_DEBUG(
"Pcp_DoesInfoChangeAffectFileFormatArguments %s:%s?\n",
cache->GetLayerStackIdentifier().rootLayer->GetIdentifier().c_str(),
primIndexPath.GetText());
// Get the cached dynamic file format dependency data for the prim index.
// This will exist if the prim index exists and has any direct arcs that
// used a dynamic file format.
const PcpDynamicFileFormatDependencyData &depData =
cache->GetDynamicFileFormatArgumentDependencyData(primIndexPath);
if (depData.IsEmpty()) {
PCP_APPEND_DEBUG(" Prim index has no dynamic file format dependencies\n");
return false;
}
// For each info field ask the dependency data if the change can affect
// the file format args of any node in the prim index graph.
for (const auto& change : changes.infoChanged) {
const bool isRelevantChange =
depData.CanFieldChangeAffectFileFormatArguments(
change.first, change.second.first, change.second.second);
PCP_APPEND_DEBUG(" Field '%s' change: %s -> %s "
"%s relevant for prim index path '%s'\n",
change.first.GetText(),
TfStringify(change.second.first).c_str(),
TfStringify(change.second.second).c_str(),
isRelevantChange ? "IS" : "is NOT",
primIndexPath.GetText());
if (isRelevantChange) {
return true;
}
}
return false;
}
// DepFunc is a function type void (const PcpDependency &)
template <typename DepFunc>
static void
Pcp_DidChangeDependents(
const PcpCache* cache,
const SdfLayerHandle& layer,
const SdfPath& path,
bool processPrimDescendants,
bool onlyExistingDependentPaths,
const DepFunc &processDependencyFunc,
std::string* debugSummary)
{
// Don't want to put a trace here, as this function can get called many
// times during change processing.
// TRACE_FUNCTION();
// We don't recurse on site for property paths, only prim paths if
// necessary.
const bool recurseOnSite =
processPrimDescendants &&
(path == SdfPath::AbsoluteRootPath() ||
path.IsPrimOrPrimVariantSelectionPath());
PcpDependencyVector deps = cache->FindSiteDependencies(
layer, path, PcpDependencyTypeAnyIncludingVirtual, recurseOnSite,
/* recurseOnIndex */ false,
/* filter */ onlyExistingDependentPaths);
PCP_APPEND_DEBUG(
" Resync following in @%s@ %s due to Sdf site @%s@<%s>%s:\n",
cache->GetLayerStackIdentifier()
.rootLayer->GetIdentifier().c_str(),
recurseOnSite ?
"recurse on prim descendants" :
"do not recurse on prim descendants",
layer->GetIdentifier().c_str(), path.GetText(),
onlyExistingDependentPaths ?
" (restricted to existing caches)" :
" (not restricted to existing caches)");
// Run the process function on each found dependency.
for (const auto& dep: deps) {
PCP_APPEND_DEBUG(
" <%s> depends on <%s>\n",
dep.indexPath.GetText(),
dep.sitePath.GetText());
processDependencyFunc(dep);
}
PCP_APPEND_DEBUG(" Resync end\n");
}
void
PcpChanges::DidChange(const TfSpan<const PcpCache*>& caches,
const SdfLayerChangeListVec& changes)
{
// LayerStack changes
static const int LayerStackLayersChange = 1;
static const int LayerStackOffsetsChange = 2;
static const int LayerStackRelocatesChange = 4;
static const int LayerStackSignificantChange = 8;
static const int LayerStackResolvedPathChange = 16;
typedef int LayerStackChangeBitmask;
typedef std::map<PcpLayerStackPtr, LayerStackChangeBitmask>
LayerStackChangeMap;
// Path changes
static const int PathChangeSimple = 1;
static const int PathChangeTargets = 2;
static const int PathChangeConnections = 4;
typedef int PathChangeBitmask;
typedef std::map<SdfPath, PathChangeBitmask,
SdfPath::FastLessThan> PathChangeMap;
typedef PathChangeMap::value_type PathChangeValue;
// Spec changes
typedef int SpecChangeBitmask;
typedef std::map<SdfPath, SpecChangeBitmask,
SdfPath::FastLessThan> SpecChangesTypes;
// Payload decorator changes
typedef std::pair<const PcpCache*, SdfPath> CacheAndLayerPathPair;
typedef std::vector<CacheAndLayerPathPair> CacheAndLayerPathPairVector;
TRACE_FUNCTION();
SdfPathSet pathsWithSignificantChanges;
PathChangeMap pathsWithSpecChanges;
SpecChangesTypes pathsWithSpecChangesTypes;
SdfPathSet pathsWithRelocatesChanges;
SdfPathVector oldPaths, newPaths;
SdfPathSet fallbackToAncestorPaths;
CacheAndLayerPathPairVector fieldForFileFormatArgumentsChanges;
// As we process each layer below, we'll look for changes that
// affect entire layer stacks, then process those in one pass
// at the end.
LayerStackChangeMap layerStackChangesMap;
// Change debugging.
std::string summary;
std::string* debugSummary = TfDebug::IsEnabled(PCP_CHANGES) ? &summary : 0;
PCP_APPEND_DEBUG(" Caches:\n");
for (const PcpCache* cache: caches) {
PCP_APPEND_DEBUG(" %s\n",
TfStringify(cache->GetLayerStack()->GetIdentifier()).c_str());
}
const bool allCachesInUsdMode = std::all_of(
caches.begin(), caches.end(),
[](const PcpCache* cache) { return cache->IsUsd(); });
// Process all changes, first looping over all layers.
for (auto const &i: changes) {
const SdfLayerHandle& layer = i.first;
const SdfChangeList& changeList = i.second;
// PcpCaches in USD mode only cache prim indexes, so they only
// care about prim changes. We can do a pre-scan of the entries
// and bail early if none of the changes are for prims, skipping
// over unnecessary work.
if (allCachesInUsdMode) {
using _Entries = SdfChangeList::EntryList;
const _Entries& entries = changeList.GetEntryList();
const bool hasPrimChanges = std::find_if(
entries.begin(), entries.end(),
[](const _Entries::value_type& entry) {
return !entry.first.ContainsPropertyElements();
}) != entries.end();
if (!hasPrimChanges) {
PCP_APPEND_DEBUG(
" Layer @%s@ changed: skipping non-prim changes\n",
layer->GetIdentifier().c_str());
continue;
}
}
// Find every layer stack in every cache that includes 'layer'.
// If there aren't any such layer stacks, we can ignore this change.
typedef std::pair<const PcpCache*, PcpLayerStackPtrVector>
CacheLayerStacks;
typedef std::vector<CacheLayerStacks> CacheLayerStacksVector;
CacheLayerStacksVector cacheLayerStacks;
for (auto cache : caches) {
PcpLayerStackPtrVector stacks =
cache->FindAllLayerStacksUsingLayer(layer);
if (!stacks.empty()) {
cacheLayerStacks.emplace_back(cache, std::move(stacks));
}
}
if (cacheLayerStacks.empty()) {
PCP_APPEND_DEBUG(" Layer @%s@ changed: unused\n",
layer->GetIdentifier().c_str());
continue;
}
PCP_APPEND_DEBUG(" Changes to layer %s:\n%s",
layer->GetIdentifier().c_str(),
TfStringify(changeList).c_str());
// Reset state.
LayerStackChangeBitmask layerStackChangeMask = 0;
bool rootLayerStacksMayNeedTcpsRecompute = false;
pathsWithSignificantChanges.clear();
pathsWithSpecChanges.clear();
pathsWithSpecChangesTypes.clear();
pathsWithRelocatesChanges.clear();
oldPaths.clear();
newPaths.clear();
fallbackToAncestorPaths.clear();
fieldForFileFormatArgumentsChanges.clear();
// Loop over each entry on the layer.
for (auto const &j: changeList.GetEntryList()) {
const SdfPath& path = j.first;
const SdfChangeList::Entry& entry = j.second;
// Figure out for which paths we must fallback to an ancestor.
// These are the paths where a prim/property was added or
// removed and any descendant.
//
// When adding the first spec for a prim or property, there
// won't be any dependencies for that object yet, but we still
// need to figure out the locations that will be affected by
// the addition of this new object. Hence the need to fallback
// to an ancestor to synthesize dependencies.
//
// When removing a prim or property spec, the fallback ancestor
// is usually not needed because there should already be
// dependencies registered for that object. However, in the case
// where an object is renamed then removed in a single change
// block, we will need the fallback ancestor because the
// dependencies at the renamed path will not have been registered
// yet. The fallback ancestor code won't be run in the usual
// case anyway, so it's safe to just always set up the fallback
// ancestor path.
const bool fallbackToParent =
entry.flags.didAddInertPrim ||
entry.flags.didRemoveInertPrim ||
entry.flags.didAddNonInertPrim ||
entry.flags.didRemoveNonInertPrim ||
entry.flags.didAddProperty ||
entry.flags.didRemoveProperty ||
entry.flags.didAddPropertyWithOnlyRequiredFields ||
entry.flags.didRemovePropertyWithOnlyRequiredFields;
if (fallbackToParent) {
fallbackToAncestorPaths.insert(path);
}
if (path == SdfPath::AbsoluteRootPath()) {
if (entry.flags.didReplaceContent) {
pathsWithSignificantChanges.insert(path);
}
// Treat a change to DefaultPrim as a resync
// of that root prim path.
auto i = entry.FindInfoChange(SdfFieldKeys->DefaultPrim);
if (i != entry.infoChanged.end()) {
// old value.
TfToken token = i->second.first.GetWithDefault<TfToken>();
pathsWithSignificantChanges.insert(
SdfPath::IsValidIdentifier(token)
? SdfPath::AbsoluteRootPath().AppendChild(token)
: SdfPath::AbsoluteRootPath());
// new value.
token = i->second.second.GetWithDefault<TfToken>();
pathsWithSignificantChanges.insert(
SdfPath::IsValidIdentifier(token)
? SdfPath::AbsoluteRootPath().AppendChild(token)
: SdfPath::AbsoluteRootPath());
}
// Handle changes that require blowing the layer stack.
switch (Pcp_EntryRequiresLayerStackChange(entry)) {
case Pcp_ChangesLayerStackChangeMaybeSignificant:
layerStackChangeMask |= LayerStackLayersChange;
for (auto const &k: entry.subLayerChanges) {
if (k.second == SdfChangeList::SubLayerAdded ||
k.second == SdfChangeList::SubLayerRemoved) {
const std::string& sublayerPath = k.first;
const _SublayerChangeType sublayerChange =
k.second == SdfChangeList::SubLayerAdded ?
_SublayerAdded : _SublayerRemoved;
for (auto const &i: cacheLayerStacks) {
bool significant = false;
const SdfLayerRefPtr sublayer =
_LoadSublayerForChange(i.first,
layer,
sublayerPath,
sublayerChange);
PCP_APPEND_DEBUG(
" Layer @%s@ changed sublayers\n",
layer ?
layer->GetIdentifier().c_str() : "invalid");
_DidChangeSublayer(i.first /* cache */,
i.second /* stack */,
sublayerPath,
sublayer,
sublayerChange,
debugSummary,
&significant);
if (significant) {
layerStackChangeMask |=
LayerStackSignificantChange;
}
}
}
}
break;
case Pcp_ChangesLayerStackChangeSignificant:
// Must blow everything
layerStackChangeMask |= LayerStackLayersChange |
LayerStackSignificantChange;
pathsWithSignificantChanges.insert(path);
PCP_APPEND_DEBUG(" Layer @%s@ changed: significant\n",
layer->GetIdentifier().c_str());
break;
case Pcp_ChangesLayerStackChangeNone:
// Layer stack is okay. Handle changes that require
// blowing the layer stack offsets.
if (Pcp_EntryRequiresLayerStackOffsetsChange(layer, entry,
&rootLayerStacksMayNeedTcpsRecompute)) {
layerStackChangeMask |= LayerStackOffsetsChange;
// Layer offsets are folded into the map functions
// for arcs that originate from a layer. So, when
// offsets authored in a layer change, all indexes
// that depend on that layer must be significantly
// resync'd to regenerate those functions.
//
// XXX: If this becomes a performance issue, we could
// potentially apply the same incremental updating
// that's currently done for relocates.
pathsWithSignificantChanges.insert(path);
PCP_APPEND_DEBUG(" Layer @%s@ changed: "
"layer offsets (significant)\n",
layer->GetIdentifier().c_str());
}
break;
}
if (entry.flags.didChangeResolvedPath) {
layerStackChangeMask |= LayerStackResolvedPathChange;
}
}
// Handle changes that require a prim graph change.
else if (path.IsPrimOrPrimVariantSelectionPath()) {
if (entry.flags.didRename) {
// XXX: We don't have enough info to determine if
// the changes so far are a rename in layer
// stack space. Renames in Sd are only renames
// in a Pcp layer stack if all specs in the
// layer stack were renamed the same way (for
// and given old path, new path pair). But we
// don't know which layer stacks to check and
// which caches they belong to. For example,
// if we rename in a referenced layer stack we
// don't know here what caches are referencing
// that layer stack.
//
// In the future we'll probably avoid this
// problem altogether by requiring clients to
// do namespace edits through Csd if they want
// efficient handling. Csd will be able to
// tell its PcpChanges object about the
// renames directly and we won't do *any*
// *handling of renames in this method.
//
// For now we'll just treat renames as resyncs.
oldPaths.push_back(entry.oldPath);
newPaths.push_back(path);
PCP_APPEND_DEBUG(" Renamed @%s@<%s> to <%s>\n",
layer->GetIdentifier().c_str(),
entry.oldPath.GetText(), path.GetText());
}
if (int specChanges = Pcp_EntryRequiresPrimSpecsChange(entry)) {
pathsWithSpecChangesTypes[path] |= specChanges;
}
if (Pcp_EntryRequiresPrimIndexChange(entry)) {
pathsWithSignificantChanges.insert(path);
}
else {
for (const auto& c : cacheLayerStacks) {
const PcpCache* cache = c.first;
// Gather info changes that may affect dynamic file
// format arguments so we can check dependents on
// these changes later.
if (Pcp_ChangeMayAffectDynamicFileFormatArguments(
cache, entry, debugSummary)) {
PCP_APPEND_DEBUG(
" Info change on @%s@<%s> may affect file "
"format arguments in cache '%s'\n",
layer->GetIdentifier().c_str(),
path.GetText(),
cache->GetLayerStackIdentifier().rootLayer
->GetIdentifier().c_str());
fieldForFileFormatArgumentsChanges.push_back(
CacheAndLayerPathPair(cache, path));
}
}
}
if (entry.HasInfoChange(SdfFieldKeys->Relocates)) {
layerStackChangeMask |= LayerStackRelocatesChange;
}
}
else if (!allCachesInUsdMode) {
// See comment above regarding PcpCaches in USD mode.
// We also check for USD mode here to ensure we don't
// process any non-prim changes if the changelist had
// a mix of prim and non-prim changes.
if (path.IsPropertyPath()) {
if (entry.flags.didRename) {
// XXX: See the comment above regarding renaming
// prims.
oldPaths.push_back(entry.oldPath);
newPaths.push_back(path);
PCP_APPEND_DEBUG(" Renamed @%s@<%s> to <%s>\n",
layer->GetIdentifier().c_str(),
entry.oldPath.GetText(),path.GetText());
}
if (int specChanges =
Pcp_EntryRequiresPropertySpecsChange(entry)) {
pathsWithSpecChangesTypes[path] |= specChanges;
}
if (Pcp_EntryRequiresPropertyIndexChange(entry)) {
pathsWithSignificantChanges.insert(path);
}
}
else if (path.IsTargetPath()) {
if (entry.flags.didAddTarget) {
pathsWithSpecChangesTypes[path] |=
Pcp_EntryChangeSpecsAddInert;
}
if (entry.flags.didRemoveTarget) {
pathsWithSpecChangesTypes[path] |=
Pcp_EntryChangeSpecsRemoveInert;
}
}
}
} // end for all entries in changelist
// If we processed a change that may affect the TCPS of root layer
// stacks, we check that here.
if (rootLayerStacksMayNeedTcpsRecompute) {
for (const auto &i : cacheLayerStacks) {
const PcpCache *cache = i.first;
// We only need to check the root layer stacks of caches
// using this layer.
if (const PcpLayerStackPtr& layerStack =
cache->GetLayerStack()) {
// If the layer stack will need to recompute its TCPS
// because this layer changed, then mark that layer stack
// will have its layer offsets change.
if (Pcp_NeedToRecomputeLayerStackTimeCodesPerSecond(
layerStack, layer)) {
PCP_APPEND_DEBUG(" Layer @%s@ changed: "
"root layer stack TCPS (significant)\n",
layer->GetIdentifier().c_str());
layerStackChangesMap[layerStack] |=
LayerStackOffsetsChange;
// This is a significant change to all prim indexes.
DidChangeSignificantly(cache, SdfPath::AbsoluteRootPath());
}
}
}
}
// Push layer stack changes to all layer stacks using this layer.
if (layerStackChangeMask != 0) {
for (auto const &i: cacheLayerStacks) {
for (auto const &layerStack: i.second) {
layerStackChangesMap[layerStack] |= layerStackChangeMask;
}
}
}
// Handle spec changes. What we do depends on what changes happened
// and the cache at each path.
//
// Prim:
// Add/remove inert -- insignificant change (*)
// Add/remove non-inert -- significant change
//
// Property:
// Add/remove inert -- insignificant change
// Add/remove non-inert -- significant change
//
// (*) We may be adding the first prim spec or removing the last prim
// spec from a composed prim in this case. We'll check for this in
// DidChangeSpecs and upgrade to a significant change if we discover
// this is the case.
//
// Note that in the below code, the order of the if statements does
// matter, as a spec could be added, then removed (for example) within
// the same change.
for (const auto& value : pathsWithSpecChangesTypes) {
const SdfPath& path = value.first;
if (path.IsPrimOrPrimVariantSelectionPath()) {
if (value.second & Pcp_EntryChangeSpecsNonInert) {
pathsWithSignificantChanges.insert(path);
}
else if (value.second & Pcp_EntryChangeSpecsInert) {
pathsWithSpecChanges[path] |= PathChangeSimple;
}
}
else {
if (value.second & Pcp_EntryChangeSpecsNonInert) {
pathsWithSignificantChanges.insert(path);
}
else if (value.second & Pcp_EntryChangeSpecsInert) {
pathsWithSpecChanges[path] |= PathChangeSimple;
}
if (value.second & Pcp_EntryChangeSpecsTargets) {
pathsWithSpecChanges[path] |= PathChangeTargets;
}
if (value.second & Pcp_EntryChangeSpecsConnections) {
pathsWithSpecChanges[path] |= PathChangeConnections;
}
}
}
// For every path we've found on this layer that has a
// significant change, find all paths in the cache that use the
// spec at (layer, path) and mark them as affected.
for (const auto& path : pathsWithSignificantChanges) {
const bool onlyExistingDependentPaths =
fallbackToAncestorPaths.count(path) == 0;
for (auto cache : caches) {
// For significant changes to a prim (as opposed to property),
// we need to process its dependencies as well as dependencies
// on descendants of that prim.
//
// This is needed to accommodate relocates, specifically the
// case where a descendant of the changed prim was relocated out
// from beneath it. In this case, dependencies on that
// descendant will be in a different branch of namespace than
// the dependencies on the changed prim. We need to mark both
// sets of dependencies as being changed.
//
// We don't need to do this for significant property changes as
// properties can't be individually relocated.
Pcp_DidChangeDependents(
cache, layer, path, /*processPrimDescendants*/ true,
onlyExistingDependentPaths,
[this, &cache](const PcpDependency &dep) {
DidChangeSignificantly(cache, dep.indexPath);
},
debugSummary);
}
}
// For every (layer, path) site we've found that has a change
// to a field that a prim index that generates dynamic file format
// arguments cares about, find all paths in the cache that depend on
// that site and register a significant change if the file format says
// the field change affects how it generates arguments.
for (const auto& p : fieldForFileFormatArgumentsChanges) {
const bool onlyExistingDependentPaths =
fallbackToAncestorPaths.count(p.second) == 0;
const PcpCache* cache = p.first;
const SdfPath &changedPath = p.second;
SdfChangeList::Entry const &changes =
changeList.GetEntry(changedPath);
// We need to recurse on prim descendants for dynamic file format
// argument changes. This is to catch the case where there's
// a reference to a subroot prim who has an ancestor with a dynamic
// file format dependency. Changes that affect the ancestor may
// affect the descendant prim's prim index but that dependency will
// be stored with the descendant as the ancestor prim index is not
// itself cached when it is only used to compute subroot references.
Pcp_DidChangeDependents(
cache, layer, p.second, /*processPrimDescendants*/ true,
onlyExistingDependentPaths,
[this, &cache, &changes, &debugSummary](
const PcpDependency &dep) {
if (Pcp_DoesInfoChangeAffectFileFormatArguments(
cache, dep.indexPath, changes, debugSummary)) {
DidChangeSignificantly(cache, dep.indexPath);
}
},