retainedDataSource.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_RETAINEDDATASOURCE_H
25 #define PXR_IMAGING_HD_RETAINEDDATASOURCE_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/api.h"
30 #include "pxr/imaging/hd/dataSource.h"
31 
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
46 {
47 public:
48  HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedContainerDataSource);
49 
50  HD_API
51  static Handle New();
52 
53  HD_API
54  static Handle New(
55  size_t count,
56  const TfToken *names,
57  const HdDataSourceBaseHandle *values);
58 
59  HD_API
60  static Handle New(
61  const TfToken &name1,
62  const HdDataSourceBaseHandle &value1);
63 
64  HD_API
65  static Handle New(
66  const TfToken &name1,
67  const HdDataSourceBaseHandle &value1,
68  const TfToken &name2,
69  const HdDataSourceBaseHandle &value2);
70 
71  HD_API
72  static Handle New(
73  const TfToken &name1,
74  const HdDataSourceBaseHandle &value1,
75  const TfToken &name2,
76  const HdDataSourceBaseHandle &value2,
77  const TfToken &name3,
78  const HdDataSourceBaseHandle &value3);
79 
80  HD_API
81  static Handle New(
82  const TfToken &name1,
83  const HdDataSourceBaseHandle &value1,
84  const TfToken &name2,
85  const HdDataSourceBaseHandle &value2,
86  const TfToken &name3,
87  const HdDataSourceBaseHandle &value3,
88  const TfToken &name4,
89  const HdDataSourceBaseHandle &value4);
90 
91  HD_API
92  static Handle New(
93  const TfToken &name1,
94  const HdDataSourceBaseHandle &value1,
95  const TfToken &name2,
96  const HdDataSourceBaseHandle &value2,
97  const TfToken &name3,
98  const HdDataSourceBaseHandle &value3,
99  const TfToken &name4,
100  const HdDataSourceBaseHandle &value4,
101  const TfToken &name5,
102  const HdDataSourceBaseHandle &value5);
103 
104  HD_API
105  static Handle New(
106  const TfToken &name1,
107  const HdDataSourceBaseHandle &value1,
108  const TfToken &name2,
109  const HdDataSourceBaseHandle &value2,
110  const TfToken &name3,
111  const HdDataSourceBaseHandle &value3,
112  const TfToken &name4,
113  const HdDataSourceBaseHandle &value4,
114  const TfToken &name5,
115  const HdDataSourceBaseHandle &value5,
116  const TfToken &name6,
117  const HdDataSourceBaseHandle &value6);
118 };
119 
120 HD_DECLARE_DATASOURCE_HANDLES(HdRetainedContainerDataSource);
121 
122 //-----------------------------------------------------------------------------
123 
130 {
131 public:
132  HD_DECLARE_DATASOURCE(HdRetainedSampledDataSource);
133 
135  : _value(value) {}
136 
138  HdSampledDataSource::Time startTime,
139  HdSampledDataSource::Time endTime,
140  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
141  {
142  return false;
143  }
144 
145  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
146  {
147  return _value;
148  }
149 
150 private:
151  VtValue _value;
152 };
153 
154 HD_DECLARE_DATASOURCE_HANDLES(HdRetainedSampledDataSource);
155 
156 //-----------------------------------------------------------------------------
157 
163 template <typename T>
165 {
166 public:
167  //abstract to implement New outside in service of specialization
168  HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedTypedSampledDataSource<T>);
169 
170  HdRetainedTypedSampledDataSource(const T &value)
171  : _value(value) {}
172 
174  HdSampledDataSource::Time startTime,
175  HdSampledDataSource::Time endTime,
176  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
177  {
178  return false;
179  }
180 
181  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
182  {
183  return VtValue(_value);
184  }
185 
186  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
187  {
188  return _value;
189  }
190 
191  static
192  typename HdRetainedTypedSampledDataSource<T>::Handle New(const T &value);
193 
194 protected:
195  T _value;
196 };
197 
198 // New is specializable for cases where instances may be shared for efficiency
199 template <typename T>
202 {
205 }
206 
207 template <>
210 
211 //-----------------------------------------------------------------------------
212 
218 template <typename T>
219 class HdRetainedTypedMultisampledDataSource : public HdTypedSampledDataSource<T>
220 {
221 public:
222  HD_DECLARE_DATASOURCE(HdRetainedTypedMultisampledDataSource<T>);
223 
224  HdRetainedTypedMultisampledDataSource(
225  size_t count,
226  HdSampledDataSource::Time *sampleTimes,
227  T *sampleValues);
228 
230  HdSampledDataSource::Time startTime,
231  HdSampledDataSource::Time endTime,
232  std::vector<HdSampledDataSource::Time> *outSampleTimes) override;
233 
234  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
235  {
236  return VtValue(GetTypedValue(shutterOffset));
237  }
238 
239  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override;
240 
241 private:
242  typedef std::pair<HdSampledDataSource::Time, T> _SamplePair;
243  TfSmallVector<_SamplePair, 6> _sampledValues;
244 };
245 
246 template <typename T>
247 HdRetainedTypedMultisampledDataSource<T>::HdRetainedTypedMultisampledDataSource(
248  size_t count,
249  HdSampledDataSource::Time *sampleTimes,
250  T *sampleValues)
251 {
252  _sampledValues.reserve(count);
253 
254  // XXX: For now, assume that sample times are ordered.
255  // We could sort them if needed.
256  for (size_t i = 0; i < count; ++i) {
257  _sampledValues.emplace_back(sampleTimes[i], sampleValues[i]);
258  }
259 }
260 
261 template <typename T>
262 bool
263 HdRetainedTypedMultisampledDataSource<T>::GetContributingSampleTimesForInterval(
264  HdSampledDataSource::Time startTime,
265  HdSampledDataSource::Time endTime,
266  std::vector<HdSampledDataSource::Time> *outSampleTimes)
267 {
268  if (_sampledValues.size() < 2) {
269  return false;
270  }
271 
272  if (outSampleTimes) {
273  outSampleTimes->clear();
274 
275  // XXX: Include all stored samples for now.
276  outSampleTimes->reserve(_sampledValues.size());
277 
278  for (const auto & iter : _sampledValues) {
279  outSampleTimes->push_back(iter.first);
280  }
281  }
282 
283  return true;
284 }
285 
286 template <typename T>
287 T
288 HdRetainedTypedMultisampledDataSource<T>::GetTypedValue(
289  HdSampledDataSource::Time shutterOffset)
290 {
291  if (_sampledValues.empty()) {
292  return T();
293  }
294 
295  const HdSampledDataSource::Time epsilon = 0.0001;
296 
297  for (size_t i = 0, e = _sampledValues.size(); i < e; ++i) {
298 
299  const HdSampledDataSource::Time & sampleTime = _sampledValues[i].first;
300 
301  if (sampleTime > shutterOffset) {
302 
303  // If we're first and we're already bigger, return us.
304  if (i < 1) {
305  return _sampledValues[i].second;
306  } else {
307 
308  // This will always be positive
309  const HdSampledDataSource::Time delta =
310  sampleTime - shutterOffset;
311 
312  // If we're kinda equal, go for it
313  if (delta < epsilon) {
314  return _sampledValues[i].second;
315  }
316 
317  // Since we're already over the requested time, let's see
318  // if it's closer, use it instead of me. In the case of a
319  // tie, use the earlier.
320  const HdSampledDataSource::Time previousDelta =
321  shutterOffset - _sampledValues[i - 1].first;
322 
323  if (previousDelta <= delta) {
324  return _sampledValues[i - 1].second;
325  } else {
326  return _sampledValues[i].second;
327  }
328  }
329  } else {
330  if (fabs(sampleTime - shutterOffset) < epsilon) {
331  return _sampledValues[i].second;
332  }
333  }
334  }
335 
336  // Never were in range, return our last sample
337  return _sampledValues.back().second;
338 }
339 
340 //-----------------------------------------------------------------------------
341 
350 {
351 public:
352  HD_DECLARE_DATASOURCE(HdRetainedSmallVectorDataSource);
353 
354  HD_API
356  size_t count,
357  HdDataSourceBaseHandle *values);
358 
359  HD_API
360  size_t GetNumElements() override;
361 
362  HD_API
363  HdDataSourceBaseHandle GetElement(size_t element) override;
364 
365 private:
367 };
368 
369 HD_DECLARE_DATASOURCE_HANDLES(HdRetainedSmallVectorDataSource);
370 
371 // Utilities //////////////////////////////////////////////////////////////////
372 
375 HD_API
376 HdSampledDataSourceHandle
377 HdCreateTypedRetainedDataSource(VtValue const &v);
378 
379 PXR_NAMESPACE_CLOSE_SCOPE
380 
381 #endif // PXR_IMAGING_HD_RETAINEDDATASOURCE_H
A retained container data source is a data source whose data are available locally,...
A datasource representing indexed data.
Definition: dataSource.h:142
HD_API size_t GetNumElements() override
Return the number of elements in this datasource.
void reserve(size_type newCapacity)
Reserve storage for newCapacity entries.
Definition: smallVector.h:443
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),...
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:110
A retained data source version of HdVectorDataSource.
virtual VtValue GetValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset.
HD_API HdDataSourceBaseHandle GetElement(size_t element) override
Return the element at position element in this datasource.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
A retained data source for sampled data.
Similar to HdRetainedSampledDataSource but provides strongly typed semantics.
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:204
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset, as type T.
virtual T GetTypedValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset, as type T.
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
A datasource representing time-sampled values.
Definition: dataSource.h:164