Loading...
Searching...
No Matches
bufferSource.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_HD_BUFFER_SOURCE_H
8#define PXR_IMAGING_HD_BUFFER_SOURCE_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hd/api.h"
12#include "pxr/imaging/hd/version.h"
13#include "pxr/imaging/hd/bufferSpec.h"
15#include "pxr/base/tf/token.h"
16
17#include <atomic>
18#include <memory>
19#include <vector>
20
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class HdBufferSource;
25using HdBufferSourceSharedPtr = std::shared_ptr<HdBufferSource>;
26using HdBufferSourceConstSharedPtr = std::shared_ptr<HdBufferSource const>;
27using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
28using HdBufferSourceWeakPtr = std::weak_ptr<HdBufferSource>;
29
39{
40public:
41 HdBufferSource() : _state(UNRESOLVED) { }
42
43 HD_API
44 virtual ~HdBufferSource();
45
47 virtual TfToken const &GetName() const = 0;
48
51 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const = 0;
52
54 HD_API
55 virtual size_t ComputeHash() const;
56
65 virtual bool Resolve() = 0;
66
68
70 virtual void const* GetData() const = 0;
71
73 virtual HdTupleType GetTupleType() const = 0;
74
77 virtual size_t GetNumElements() const = 0;
78
80 bool IsResolved() const {
81 return _state >= RESOLVED;
82 }
83
85 bool HasResolveError() const {
86 return _state == RESOLVE_ERROR;
87 }
88
103
105 HD_API
106 virtual bool HasPreChainedBuffer() const;
107
109 HD_API
110 virtual HdBufferSourceSharedPtr GetPreChainedBuffer() const;
111
113 HD_API
114 virtual bool HasChainedBuffer() const;
115
117 HD_API
118 virtual HdBufferSourceSharedPtrVector GetChainedBuffers() const;
119
121
126 HD_API
127 bool IsValid() const;
128
129protected:
133 TF_VERIFY(_state == BEING_RESOLVED);
134 _state = RESOLVED;
135 }
136
147 TF_VERIFY(_state == BEING_RESOLVED);
148 _state = RESOLVE_ERROR;
149 }
150
156 bool _TryLock() {
157 State oldState = UNRESOLVED;
158 return _state.compare_exchange_strong(oldState, BEING_RESOLVED);
159 }
160
177 virtual bool _CheckValid() const = 0;
178
179protected:
180 enum State { UNRESOLVED=0, BEING_RESOLVED, RESOLVED, RESOLVE_ERROR};
181 HdBufferSource(State state) : _state(state) { }
182
183private:
184 // Don't allow copies
185 HdBufferSource(const HdBufferSource &) = delete;
186 HdBufferSource &operator=(const HdBufferSource &) = delete;
187
188 std::atomic<State> _state;
189};
190
194public:
196
197 HD_API
198 bool Resolve() override { return true; }
199};
200
210public:
211 HD_API
212 virtual TfToken const &GetName() const override;
213 HD_API
214 virtual size_t ComputeHash() const override;
215 HD_API
216 virtual void const* GetData() const override;
217 HD_API
218 virtual HdTupleType GetTupleType() const override;
219 HD_API
220 virtual size_t GetNumElements() const override;
221
222protected:
223 void _SetResult(HdBufferSourceSharedPtr const &result) {
224 _result = result;
225 }
226
227private:
228 HdBufferSourceSharedPtr _result;
229};
230
235public:
236 HD_API
237 virtual TfToken const &GetName() const override;
238 HD_API
239 virtual void const* GetData() const override;
240 HD_API
241 virtual size_t ComputeHash() const override;
242 HD_API
243 virtual size_t GetNumElements() const override;
244 HD_API
245 virtual HdTupleType GetTupleType() const override;
246 HD_API
247 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
248};
249
250
251PXR_NAMESPACE_CLOSE_SCOPE
252
253#endif //PXR_IMAGING_HD_BUFFER_SOURCE_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
A transient buffer of data that has not yet been committed.
virtual void const * GetData() const =0
Following interfaces will be called after Resolve.
virtual bool _CheckValid() const =0
Checks the validity of the source buffer.
virtual size_t GetNumElements() const =0
Returns the number of elements (e.g.
virtual TfToken const & GetName() const =0
Return the name of this buffer source.
bool IsResolved() const
Returns true it this computation has already been resolved.
virtual bool Resolve()=0
Prepare the access of GetData().
virtual HdTupleType GetTupleType() const =0
Returns the data type and count (array size) for this buffer source.
HD_API bool IsValid() const
Checks the validity of the source buffer.
virtual HD_API HdBufferSourceSharedPtr GetPreChainedBuffer() const
Returns the pre-chained buffer.
virtual HD_API bool HasPreChainedBuffer() const
Returns true if this buffer has a pre-chained buffer.
virtual HD_API bool HasChainedBuffer() const
Returns true if this buffer has any chained buffer(s)
virtual void GetBufferSpecs(HdBufferSpecVector *specs) const =0
Add the buffer spec for this buffer source into given bufferspec vector.
bool _TryLock()
Non-blocking lock acquisition.
virtual HD_API size_t ComputeHash() const
Computes and returns a hash value for the underlying data.
virtual HD_API HdBufferSourceSharedPtrVector GetChainedBuffers() const
Returns the vector of chained buffers.
void _SetResolveError()
Called during Resolve() to indicate an unrecoverable failure occurred and the results of the computat...
void _SetResolved()
Marks this buffer source as resolved.
bool HasResolveError() const
Returns true if an error occurred during resolve.
An abstract base class for cpu computation followed by buffer transfer to the GPU.
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
An abstract base class for pure cpu computation.
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
virtual HD_API void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
An abstract base class for a plain old data buffer source that doesn't need to be resolved.
HD_API bool Resolve() override
Prepare the access of GetData().
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:71
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition diagnostic.h:266
HdTupleType represents zero, one, or more values of the same HdType.
Definition types.h:343
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...