HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataSource.h
Go to the documentation of this file.
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"
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 
41 
42 /// HD_DECLARE_DATASOURCE_ABSTRACT
43 /// Used for non-instantiable classes, this defines a set of functions
44 /// for manipulating handles to this type of datasource.
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 
58 /// HD_DECLARE_DATASOURCE
59 /// Used for instantiable classes, this defines functions for manipulating
60 /// and allocating handles to this type of datasource.
61 ///
62 /// Use of this macro in derived classes is important to make sure that
63 /// core and client code share the same handle type and allocator.
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 /// HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW
72 /// Used for declaring a `New` function for datasource types that have a
73 /// constructor that takes an initializer_list<T>.
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 
83 /// \class HdDataSourceBase
84 ///
85 /// Represents an object which can produce scene data.
86 /// \sa HdContainerDataSource HdVectorDataSource HdSampledDataSource
87 /// Note that most derived classes will have standard API for allocation
88 /// and handle manipulation. Derived classes that don't support instantiation
89 /// should use HD_DECLARE_DATASOURCE_ABSTRACT, which omits the
90 /// definition of ::New().
91 ///
93 {
94 public:
96 
97  HD_API
98  virtual ~HdDataSourceBase() = 0;
99 };
100 
102 
103 /// \class HdContainerDataSource
104 ///
105 /// A datasource representing structured (named, hierarchical) data, for
106 /// example a geometric primitive or a sub-object like a material definition.
107 /// Note that implementations are responsible for providing cache invalidation,
108 /// if necessary.
109 ///
111 {
112 public:
114 
115  /// Returns the list of names for which \p Get(...) is expected to return
116  /// a non-null value. This call is expected to be threadsafe.
117  virtual TfTokenVector GetNames() = 0;
118 
119  /// Returns the child datasource of the given name. This call is expected
120  /// to be threadsafe.
121  virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
122 
123  /// A convenience function: given \p container, return the descendant
124  /// identified by \p locator, which may be at any depth. Returns
125  /// \p container itself on an empty locator, or null if \p locator doesn't
126  /// identify a valid descendant.
127  HD_API
128  static HdDataSourceBaseHandle Get(
129  const Handle &container,
130  const HdDataSourceLocator &locator);
131 };
132 
134 
135 /// \class HdVectorDataSource
136 ///
137 /// A datasource representing indexed data. This should be used when a scene
138 /// index is expected to manipulate the indexing; for array-valued data, a
139 /// \p HdSampledDataSource can be used instead. Note that implementations are
140 /// responsible for providing cache invalidation, if necessary.
141 ///
143 {
144 public:
146 
147  /// Return the number of elements in this datasource. This call is
148  /// expected to be threadsafe.
149  virtual size_t GetNumElements() = 0;
150 
151  /// Return the element at position \p element in this datasource. This
152  /// is expected to return non-null for the range [0, \p numElements).
153  /// This call is expected to be threadsafe.
154  virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
155 };
156 
158 
159 /// \class HdSampledDataSource
160 ///
161 /// A datasource representing time-sampled values. Note that implementations
162 /// are responsible for providing cache invalidation, if necessary.
163 ///
165 {
166 public:
168  using Time = float;
169 
170  /// Returns the value of this data source at frame-relative time
171  /// \p shutterOffset. The caller does not track the frame; the scene
172  /// index producing this datasource is responsible for that, if applicable.
173  /// Note that, although this call returns a VtValue for each shutter
174  /// offset, the type of the held value is expected to be the same across
175  /// all shutter offsets. This call is expected to be threadsafe.
176  virtual VtValue GetValue(Time shutterOffset) = 0;
177 
178  /// Given a shutter window of interest (\p startTime and \p endTime
179  /// relative to the current frame), return a list of sample times for the
180  /// caller to query with GetValue such that the caller can reconstruct the
181  /// signal over the shutter window. For a sample-based attribute, this
182  /// might be a list of times when samples are defined. For a procedural
183  /// scene, this might be a generated distribution. Note that the returned
184  /// samples don't need to be within \p startTime and \p endTime; if
185  /// a boundary sample is outside of the window, implementers can return it,
186  /// and callers should expect it and interpolate to \p startTime or
187  /// \p endTime accordingly. If this call returns \p true, the caller is
188  /// expected to pass the list of \p outSampleTimes to \p GetValue. If this
189  /// call returns \p false, this value is uniform across the shutter window
190  /// and the caller should call \p GetValue(0) to get that uniform value.
192  Time startTime,
193  Time endTime,
194  std::vector<Time> * outSampleTimes) = 0;
195 };
196 
198 
199 /// \class HdTypedSampledDataSource
200 ///
201 /// A datasource representing a concretely-typed sampled value.
202 ///
203 template <typename T>
205 {
206 public:
208  using Type = T;
209 
210  /// Returns the value of this data source at frame-relative time
211  /// \p shutterOffset, as type \p T.
212  virtual T GetTypedValue(Time shutterOffset) = 0;
213 };
214 
215 
216 /// \class HdBlockDataSource
217 ///
218 /// A datasource representing the absence of a datasource. If a container has
219 /// a child datasource which is a block datasource, that's equivalent to that
220 /// child being null. This type is useful when composing containers, where a
221 /// block might shadow sampled data, and sampled data might shadow nullptr.
222 ///
224 {
225 public:
227 
229 };
230 
232 
233 // Utilities //////////////////////////////////////////////////////////////////
234 
235 /// Merges contributing sample times from several data sources.
236 HD_API
237 bool
239  size_t count,
240  const HdSampledDataSourceHandle *inputDataSources,
241  HdSampledDataSource::Time startTime,
243  std::vector<HdSampledDataSource::Time> * outSampleTimes);
244 
245 /// Print a datasource to a stream, for debugging/testing.
246 HD_API
247 void
249  std::ostream &,
250  HdDataSourceBaseHandle,
251  int indentLevel = 0);
252 
253 /// Print a datasource to stdout, for debugging/testing
254 HD_API
255 void
256 HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
257 
259 
260 #endif // PXR_IMAGING_HD_DATASOURCE_H
virtual HdDataSourceBaseHandle GetElement(size_t element)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource)
#define HD_API
Definition: api.h:40
virtual TfTokenVector GetNames()=0
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource< T >)
Definition: token.h:87
virtual size_t GetNumElements()=0
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
#define HD_DECLARE_DATASOURCE_HANDLES(type)
Definition: dataSource.h:79
GLuint const GLchar * name
Definition: glcorearb.h:786
HD_DECLARE_DATASOURCE_ABSTRACT(HdSampledDataSource)
HD_API bool HdGetMergedContributingSampleTimesForInterval(size_t count, const HdSampledDataSourceHandle *inputDataSources, HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes)
Merges contributing sample times from several data sources.
virtual VtValue GetValue(Time shutterOffset)=0
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource)
#define HD_DECLARE_DATASOURCE_ABSTRACT(type)
Definition: dataSource.h:45
virtual T GetTypedValue(Time shutterOffset)=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
virtual HD_API ~HdDataSourceBase()=0
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
HD_API void HdDebugPrintDataSource(std::ostream &, HdDataSourceBaseHandle, int indentLevel=0)
Print a datasource to a stream, for debugging/testing.
HD_DECLARE_DATASOURCE(HdBlockDataSource)
Definition: value.h:167
GLint GLsizei count
Definition: glcorearb.h:405
void * Handle
Definition: plugin.h:27