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 
74 #define HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW(type, T) \
75  static Handle New(std::initializer_list<T> initList) { \
76  return Handle(new type(initList)); \
77  }
78 
79 #define HD_DECLARE_DATASOURCE_HANDLES(type) \
80  using type##Handle = type::Handle; \
81  using type##AtomicHandle = type::AtomicHandle;
82 
93 {
94 public:
95  HD_DECLARE_DATASOURCE_ABSTRACT(HdDataSourceBase)
96 
97  HD_API
98  virtual ~HdDataSourceBase() = 0;
99 };
100 
101 HD_DECLARE_DATASOURCE_HANDLES(HdDataSourceBase);
102 
111 {
112 public:
113  HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource);
114 
117  virtual TfTokenVector GetNames() = 0;
118 
121  virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
122 
127  HD_API
128  static HdDataSourceBaseHandle Get(
129  const Handle &container,
130  const HdDataSourceLocator &locator);
131 };
132 
133 HD_DECLARE_DATASOURCE_HANDLES(HdContainerDataSource);
134 
143 {
144 public:
145  HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource);
146 
149  virtual size_t GetNumElements() = 0;
150 
154  virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
155 };
156 
157 HD_DECLARE_DATASOURCE_HANDLES(HdVectorDataSource);
158 
165 {
166 public:
167  HD_DECLARE_DATASOURCE_ABSTRACT(HdSampledDataSource);
168  using Time = float;
169 
176  virtual VtValue GetValue(Time shutterOffset) = 0;
177 
192  Time startTime,
193  Time endTime,
194  std::vector<Time> * outSampleTimes) = 0;
195 };
196 
197 HD_DECLARE_DATASOURCE_HANDLES(HdSampledDataSource);
198 
203 template <typename T>
205 {
206 public:
207  HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource<T>);
208  using Type = T;
209 
212  virtual T GetTypedValue(Time shutterOffset) = 0;
213 };
214 
215 
224 {
225 public:
226  HD_DECLARE_DATASOURCE(HdBlockDataSource);
227 
229 };
230 
231 HD_DECLARE_DATASOURCE_HANDLES(HdBlockDataSource);
232 
233 // Utilities //////////////////////////////////////////////////////////////////
234 
236 HD_API
237 bool
238 HdGetMergedContributingSampleTimesForInterval(
239  size_t count,
240  const HdSampledDataSourceHandle *inputDataSources,
241  HdSampledDataSource::Time startTime,
242  HdSampledDataSource::Time endTime,
243  std::vector<HdSampledDataSource::Time> * outSampleTimes);
244 
246 HD_API
247 void
248 HdDebugPrintDataSource(
249  std::ostream &,
250  HdDataSourceBaseHandle,
251  int indentLevel = 0);
252 
254 HD_API
255 void
256 HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
257 
258 PXR_NAMESPACE_CLOSE_SCOPE
259 
260 #endif // PXR_IMAGING_HD_DATASOURCE_H
A datasource representing indexed data.
Definition: dataSource.h:142
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:110
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:223
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:204
virtual size_t GetNumElements()=0
Return the number of elements in this datasource.
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:92
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:164
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...