dataSource.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_DATASOURCE_H
25 #define PXR_IMAGING_HD_DATASOURCE_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/api.h"
30 #include "pxr/imaging/hd/dataSourceLocator.h"
31 
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/vt/value.h"
34 
35 #include <iosfwd>
36 #include <memory>
37 #include <vector>
38 #include <atomic>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
45 #define HD_DECLARE_DATASOURCE_ABSTRACT(type) \
46  using Handle = std::shared_ptr<type>; \
47  using AtomicHandle = Handle; \
48  static Handle AtomicLoad(AtomicHandle &ptr) { \
49  return std::atomic_load(&ptr); \
50  } \
51  static void AtomicStore(AtomicHandle &ptr, const Handle &v) { \
52  std::atomic_store(&ptr, v); \
53  } \
54  static Handle Cast(const HdDataSourceBase::Handle &v) { \
55  return std::dynamic_pointer_cast<type>(v); \
56  }
57 
64 #define HD_DECLARE_DATASOURCE(type) \
65  HD_DECLARE_DATASOURCE_ABSTRACT(type) \
66  template <typename ... Args> \
67  static Handle New(Args&& ... args) { \
68  return Handle(new type(std::forward<Args>(args) ... )); \
69  }
70 
71 #define HD_DECLARE_DATASOURCE_HANDLES(type) \
72  using type##Handle = type::Handle; \
73  using type##AtomicHandle = type::AtomicHandle;
74 
85 {
86 public:
87  HD_DECLARE_DATASOURCE_ABSTRACT(HdDataSourceBase)
88 
89  HD_API
90  virtual ~HdDataSourceBase() = 0;
91 };
92 
93 HD_DECLARE_DATASOURCE_HANDLES(HdDataSourceBase);
94 
103 {
104 public:
105  HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource);
106 
110  virtual bool Has(const TfToken &name) = 0;
111 
114  virtual TfTokenVector GetNames() = 0;
115 
118  virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
119 
124  HD_API
125  static HdDataSourceBaseHandle Get(
126  const Handle &container,
127  const HdDataSourceLocator &locator);
128 };
129 
130 HD_DECLARE_DATASOURCE_HANDLES(HdContainerDataSource);
131 
140 {
141 public:
142  HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource);
143 
146  virtual size_t GetNumElements() = 0;
147 
151  virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
152 };
153 
154 HD_DECLARE_DATASOURCE_HANDLES(HdVectorDataSource);
155 
162 {
163 public:
164  HD_DECLARE_DATASOURCE_ABSTRACT(HdSampledDataSource);
165  using Time = float;
166 
173  virtual VtValue GetValue(Time shutterOffset) = 0;
174 
189  Time startTime,
190  Time endTime,
191  std::vector<Time> * outSampleTimes) = 0;
192 };
193 
194 HD_DECLARE_DATASOURCE_HANDLES(HdSampledDataSource);
195 
200 template <typename T>
202 {
203 public:
204  HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource<T>);
205  using Type = T;
206 
209  virtual T GetTypedValue(Time shutterOffset) = 0;
210 };
211 
212 
221 {
222 public:
223  HD_DECLARE_DATASOURCE(HdBlockDataSource);
224 
226 };
227 
228 HD_DECLARE_DATASOURCE_HANDLES(HdBlockDataSource);
229 
230 // Utilities //////////////////////////////////////////////////////////////////
231 
233 HD_API
234 bool
235 HdGetMergedContributingSampleTimesForInterval(
236  size_t count,
237  const HdSampledDataSourceHandle *inputDataSources,
238  HdSampledDataSource::Time startTime,
239  HdSampledDataSource::Time endTime,
240  std::vector<HdSampledDataSource::Time> * outSampleTimes);
241 
243 HD_API
244 void
245 HdDebugPrintDataSource(
246  std::ostream &,
247  HdDataSourceBaseHandle,
248  int indentLevel = 0);
249 
251 HD_API
252 void
253 HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
254 
255 PXR_NAMESPACE_CLOSE_SCOPE
256 
257 #endif // PXR_IMAGING_HD_DATASOURCE_H
A datasource representing indexed data.
Definition: dataSource.h:139
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
Returns the child datasource of the given name.
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
Given a shutter window of interest (startTime and endTime relative to the current frame),...
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:102
virtual VtValue GetValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual HdDataSourceBaseHandle GetElement(size_t element)=0
Return the element at position element in this datasource.
Represents an object that can identify the location of a data source.
A datasource representing the absence of a datasource.
Definition: dataSource.h:220
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:201
virtual size_t GetNumElements()=0
Return the number of elements in this datasource.
virtual bool Has(const TfToken &name)=0
Returns true if the container has a child datasource of the given name, in which case Get(name) is ex...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
virtual T GetTypedValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset, as type T.
Represents an object which can produce scene data.
Definition: dataSource.h:84
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
virtual TfTokenVector GetNames()=0
Returns the list of names for which Get(...) is expected to return a non-null value.
A datasource representing time-sampled values.
Definition: dataSource.h:161
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...