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