All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bufferSource.h
1 //
2 // Copyright 2016 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 HD_BUFFER_SOURCE_H
25 #define HD_BUFFER_SOURCE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/bufferSpec.h"
31 #include "pxr/base/tf/diagnostic.h"
32 #include "pxr/base/tf/token.h"
33 
34 #include <atomic>
35 #include <vector>
36 #include <boost/noncopyable.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <boost/weak_ptr.hpp>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
43 class HdBufferSource;
44 typedef boost::shared_ptr<HdBufferSource> HdBufferSourceSharedPtr;
45 typedef boost::shared_ptr<HdBufferSource const> HdBufferSourceConstSharedPtr;
46 typedef std::vector<HdBufferSourceSharedPtr> HdBufferSourceVector;
47 typedef boost::weak_ptr<HdBufferSource> HdBufferSourceWeakPtr;
48 
57 class HdBufferSource : public boost::noncopyable {
58 public:
59  HdBufferSource() : _state(UNRESOLVED) { }
60 
61  HD_API
62  virtual ~HdBufferSource();
63 
65  virtual TfToken const &GetName() const = 0;
66 
69  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const = 0;
70 
72  HD_API
73  virtual size_t ComputeHash() const;
74 
83  virtual bool Resolve() = 0;
84 
86 
88  virtual void const* GetData() const = 0;
89 
91  virtual HdTupleType GetTupleType() const = 0;
92 
95  virtual size_t GetNumElements() const = 0;
96 
98  bool IsResolved() const {
99  return _state >= RESOLVED;
100  }
101 
103  bool HasResolveError() const {
104  return _state == RESOLVE_ERROR;
105  }
106 
121 
123  HD_API
124  virtual bool HasPreChainedBuffer() const;
125 
127  HD_API
128  virtual HdBufferSourceSharedPtr GetPreChainedBuffer() const;
129 
131  HD_API
132  virtual bool HasChainedBuffer() const;
133 
135  HD_API
136  virtual HdBufferSourceVector GetChainedBuffers() const;
137 
139 
144  HD_API
145  bool IsValid() const;
146 
147 protected:
150  void _SetResolved() {
151  TF_VERIFY(_state == BEING_RESOLVED);
152  _state = RESOLVED;
153  }
154 
165  TF_VERIFY(_state == BEING_RESOLVED);
166  _state = RESOLVE_ERROR;
167  }
168 
174  bool _TryLock() {
175  State oldState = UNRESOLVED;
176  return _state.compare_exchange_strong(oldState, BEING_RESOLVED);
177  }
178 
195  virtual bool _CheckValid() const = 0;
196 
197 private:
198  enum State { UNRESOLVED=0, BEING_RESOLVED, RESOLVED, RESOLVE_ERROR};
199  std::atomic<State> _state;
200 };
201 
211 public:
212  HD_API
213  virtual TfToken const &GetName() const override;
214  HD_API
215  virtual size_t ComputeHash() const override;
216  HD_API
217  virtual void const* GetData() const override;
218  HD_API
219  virtual HdTupleType GetTupleType() const override;
220  HD_API
221  virtual size_t GetNumElements() const override;
222 
223 protected:
224  void _SetResult(HdBufferSourceSharedPtr const &result) {
225  _result = result;
226  }
227 
228 private:
229  HdBufferSourceSharedPtr _result;
230 };
231 
236 public:
237  HD_API
238  virtual TfToken const &GetName() const override;
239  HD_API
240  virtual void const* GetData() const override;
241  HD_API
242  virtual size_t ComputeHash() const override;
243  HD_API
244  virtual size_t GetNumElements() const override;
245  HD_API
246  virtual HdTupleType GetTupleType() const override;
247  HD_API
248  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
249 };
250 
251 
252 PXR_NAMESPACE_CLOSE_SCOPE
253 
254 #endif //HD_BUFFER_SOURCE_H
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
void _SetResolveError()
Called during Resolve() to indicate an unrecoverable failure occurred and the results of the computat...
Definition: bufferSource.h:164
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
void _SetResolved()
Marks this buffer source as resolved.
Definition: bufferSource.h:150
virtual HD_API void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual void GetBufferSpecs(HdBufferSpecVector *specs) const =0
Add the buffer spec for this buffer source into given bufferspec vector.
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:239
bool HasResolveError() const
Returns true if an error occurred during resolve.
Definition: bufferSource.h:103
bool IsResolved() const
Returns true it this computation has already been resolved.
Definition: bufferSource.h:98
virtual void const * GetData() const =0
Following interfaces will be called after Resolve.
HD_API bool IsValid() const
Checks the validity of the source buffer.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual HD_API HdBufferSourceSharedPtr GetPreChainedBuffer() const
Returns the pre-chained buffer.
A abstract base class for pure cpu computation.
Definition: bufferSource.h:235
virtual HD_API bool HasPreChainedBuffer() const
Returns true if this buffer has a pre-chained buffer.
A transient buffer of data that has not yet been committed.
Definition: bufferSource.h:57
virtual HdTupleType GetTupleType() const =0
Returns the data type and count (array size) for this buffer source.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
virtual HD_API size_t ComputeHash() const
Computes and returns a hash value for the underlying data.
virtual bool _CheckValid() const =0
Checks the validity of the source buffer.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual HD_API bool HasChainedBuffer() const
Returns true if this buffer has any chained buffer(s)
virtual size_t GetNumElements() const =0
Returns the number of elements (e.g.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
bool _TryLock()
Non-blocking lock acquisition.
Definition: bufferSource.h:174
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
virtual HD_API HdBufferSourceVector GetChainedBuffers() const
Returns the vector of chained buffers.
virtual TfToken const & GetName() const =0
Return the name of this buffer source.
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
virtual bool Resolve()=0
Prepare the access of GetData().
A abstract base class for cpu computation followed by buffer transfer to the GPU. ...
Definition: bufferSource.h:210