primDataSourceOverlayCache.h
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_PRIM_DATA_SOURCE_OVERLAY_CACHE_H
25 #define PXR_IMAGING_HD_PRIM_DATA_SOURCE_OVERLAY_CACHE_H
26 
27 #include "pxr/imaging/hd/sceneIndex.h"
28 #include "pxr/imaging/hd/dataSource.h"
29 
30 #include "pxr/usd/sdf/pathTable.h"
31 
32 PXR_NAMESPACE_OPEN_SCOPE
33 
34 // ----------------------------------------------------------------------------
35 
36 // A utility class to handle caching of datasource overlays, along with
37 // invalidation functions to clear the cache.
38 
39 class HdPrimDataSourceOverlayCache :
40  public std::enable_shared_from_this<HdPrimDataSourceOverlayCache>
41 {
42 public:
43  virtual ~HdPrimDataSourceOverlayCache();
44 
45  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const;
46 
47  void HandlePrimsAdded(
49  const HdSceneIndexBaseRefPtr &source);
50  void HandlePrimsRemoved(
52  void HandlePrimsDirtied(
54  HdSceneIndexObserver::DirtiedPrimEntries *additionalDirtied);
55 
56 protected:
57  HdPrimDataSourceOverlayCache() = default;
58 
59  // This struct provides a way for users of the cache to describe the
60  // structure of synthetic attributes. For example, if you compute
61  // "xformInverse" from "xform", the topology would look like:
62  // OverlayTopology topo = { "xformInverse" ->
63  // { .onPrim = { "xform" }, .onParent = { }, false }
64  // }
65  // ... notably, this tells us what attributes we're adding (xformInverse,
66  // but only when xform is present); and also to dirty xformInverse when
67  // xform is dirty.
68  //
69  // For attributes we always want to add (even if their dependents are not
70  // present), dependenciesOptional lets us say as much.
71  //
72  // Note: _ComputeOverlayDataSource should respect this topology, or behavior
73  // is undefined...
74  //
75  // XXX: the "onParent" dependencies here are to support eventual inherited
76  // attribute caching, but this feature hasn't been implemented yet.
77  struct _OverlayDependencies
78  {
79  _OverlayDependencies()
80  : onPrim(), onParent(), dependenciesOptional(false) {}
81 
83  HdDataSourceLocatorSet onParent;
84  bool dependenciesOptional;
85  };
86  using _OverlayTopology = std::map<TfToken, _OverlayDependencies>;
87 
88 
89  // Topology should be set once, from the derived class constructor.
90  void _SetOverlayTopology(const _OverlayTopology &topology) {
91  _overlayTopology = topology;
92  }
93 
94  // Compute the named datasource. Note that inputDataSource comes from the
95  // source scene index, while parentOverlayDataSource comes from the cache
96  // and is consequently recursively composed.
97  //
98  // XXX: the "parentOverlayDataSource" is here to support eventual inherited
99  // attribute caching, but this feature hasn't been implemented yet. For now,
100  // it will always be null.
101  virtual HdDataSourceBaseHandle _ComputeOverlayDataSource(
102  const TfToken &name,
103  HdContainerDataSourceHandle inputDataSource,
104  HdContainerDataSourceHandle parentOverlayDataSource) const = 0;
105 
106 private:
107  class _HdPrimDataSourceOverlay : public HdContainerDataSource
108  {
109  public:
110  HD_DECLARE_DATASOURCE(_HdPrimDataSourceOverlay);
111 
112  _HdPrimDataSourceOverlay(
113  HdContainerDataSourceHandle inputDataSource,
114  HdContainerDataSourceHandle parentOverlayDataSource,
115  const std::weak_ptr<const HdPrimDataSourceOverlayCache> cache);
116 
117  void UpdateInputDataSource(HdContainerDataSourceHandle inputDataSource);
118 
119  void PrimDirtied(const HdDataSourceLocatorSet &dirtyAttributes);
120 
121  TfTokenVector GetNames() override;
122  HdDataSourceBaseHandle Get(const TfToken &name) override;
123 
124  private:
125  HdContainerDataSourceHandle _inputDataSource;
126  HdContainerDataSourceHandle _parentOverlayDataSource;
127  const std::weak_ptr<const HdPrimDataSourceOverlayCache> _cache;
128 
129  using _OverlayMap = std::map<TfToken, HdDataSourceBaseHandle>;
130 
131  _OverlayMap _overlayMap;
132  };
133 
135  _OverlayTopology _overlayTopology;
136 };
137 
138 PXR_NAMESPACE_CLOSE_SCOPE
139 
140 #endif
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:110
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:49
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Represents a set of data source locators closed under descendancy.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
static HD_API HdDataSourceBaseHandle Get(const Handle &container, const HdDataSourceLocator &locator)
A convenience function: given container, return the descendant identified by locator,...