This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
179private:
180 // Don't allow copies
181 HdBufferSource(const HdBufferSource &) = delete;
182 HdBufferSource &operator=(const HdBufferSource &) = delete;
183
184 enum State { UNRESOLVED=0, BEING_RESOLVED, RESOLVED, RESOLVE_ERROR};
185 std::atomic<State> _state;
186};
187
197public:
198 HD_API
199 virtual TfToken const &GetName() const override;
200 HD_API
201 virtual size_t ComputeHash() const override;
202 HD_API
203 virtual void const* GetData() const override;
204 HD_API
205 virtual HdTupleType GetTupleType() const override;
206 HD_API
207 virtual size_t GetNumElements() const override;
208
209protected:
210 void _SetResult(HdBufferSourceSharedPtr const &result) {
211 _result = result;
212 }
213
214private:
215 HdBufferSourceSharedPtr _result;
216};
217
222public:
223 HD_API
224 virtual TfToken const &GetName() const override;
225 HD_API
226 virtual void const* GetData() const override;
227 HD_API
228 virtual size_t ComputeHash() const override;
229 HD_API
230 virtual size_t GetNumElements() const override;
231 HD_API
232 virtual HdTupleType GetTupleType() const override;
233 HD_API
234 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
235};
236
237
238PXR_NAMESPACE_CLOSE_SCOPE
239
240#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.
Definition: bufferSource.h:39
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.
Definition: bufferSource.h:80
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.
Definition: bufferSource.h:156
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...
Definition: bufferSource.h:146
void _SetResolved()
Marks this buffer source as resolved.
Definition: bufferSource.h:132
bool HasResolveError() const
Returns true if an error occurred during resolve.
Definition: bufferSource.h:85
A abstract base class for cpu computation followed by buffer transfer to the GPU.
Definition: bufferSource.h:196
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.
A abstract base class for pure cpu computation.
Definition: bufferSource.h:221
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.
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...