All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
subdivision.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 HDST_SUBDIVISION_H
25 #define HDST_SUBDIVISION_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/version.h"
29 #include "pxr/imaging/hd/bufferSource.h"
30 #include "pxr/imaging/hd/computation.h"
31 #include "pxr/imaging/hd/tokens.h"
32 #include "pxr/imaging/hdSt/bufferResourceGL.h"
33 #include "pxr/imaging/hdSt/meshTopology.h"
34 #include "pxr/imaging/hf/perfLog.h"
35 #include "pxr/usd/sdf/path.h"
36 #include "pxr/base/tf/token.h"
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 
47 class HdSt_Subdivision {
48 public:
49  virtual ~HdSt_Subdivision();
50 
51  virtual int GetNumVertices() const = 0;
52 
53  virtual void RefineCPU(HdBufferSourceSharedPtr const &source,
54  bool varying,
55  void *vertexBuffer) = 0;
56  virtual void RefineGPU(HdBufferArrayRangeSharedPtr const &range,
57  TfToken const &name) = 0;
58 
59  // computation factory methods
60  virtual HdBufferSourceSharedPtr CreateTopologyComputation(
61  HdSt_MeshTopology *topology,
62  bool adaptive,
63  int level,
64  SdfPath const &id) = 0;
65 
66  virtual HdBufferSourceSharedPtr CreateIndexComputation(
67  HdSt_MeshTopology *topology,
68  HdBufferSourceSharedPtr const &osdTopology) = 0;
69 
70  virtual HdBufferSourceSharedPtr CreateRefineComputation(
71  HdSt_MeshTopology *topology,
72  HdBufferSourceSharedPtr const &source,
73  bool varying,
74  HdBufferSourceSharedPtr const &osdTopology) = 0;
75 
76  virtual HdComputationSharedPtr CreateRefineComputationGPU(
77  HdSt_MeshTopology *topology,
78  TfToken const &name,
79  HdType type) = 0;
80 
83  static bool RefinesToTriangles(TfToken const &scheme);
84 
86  static bool RefinesToBSplinePatches(TfToken const &scheme);
87 
90  static bool RefinesToBoxSplineTrianglePatches(TfToken const &scheme);
91 };
92 
93 // ---------------------------------------------------------------------------
99 class HdSt_OsdTopologyComputation : public HdComputedBufferSource {
100 public:
101  HdSt_OsdTopologyComputation(HdSt_MeshTopology *topology,
102  int level,
103  SdfPath const &id);
104 
106  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
107  virtual bool Resolve() = 0;
108 
109 protected:
110  HdSt_MeshTopology *_topology;
111  int _level;
112  SdfPath const _id;
113 };
114 
115 // ---------------------------------------------------------------------------
132 class HdSt_OsdIndexComputation : public HdComputedBufferSource {
133 public:
135  virtual bool HasChainedBuffer() const override;
136  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
137  virtual HdBufferSourceVector GetChainedBuffers() const override;
138  virtual bool Resolve() = 0;
139 
140 protected:
141  HdSt_OsdIndexComputation(HdSt_MeshTopology *topology,
142  HdBufferSourceSharedPtr const &osdTopology);
143 
144  virtual bool _CheckValid() const override;
145 
146  HdSt_MeshTopology *_topology;
147  HdBufferSourceSharedPtr _osdTopology;
148  HdBufferSourceSharedPtr _primitiveBuffer;
149  HdBufferSourceSharedPtr _edgeIndicesBuffer;
150 };
151 
152 // ---------------------------------------------------------------------------
160 template <typename VERTEX_BUFFER>
161 class HdSt_OsdRefineComputation : public HdBufferSource {
162 public:
163  HdSt_OsdRefineComputation(HdSt_MeshTopology *topology,
164  HdBufferSourceSharedPtr const &source,
165  bool varying,
166  HdBufferSourceSharedPtr const &osdTopology);
167  virtual ~HdSt_OsdRefineComputation();
168  virtual TfToken const &GetName() const override;
169  virtual size_t ComputeHash() const override;
170  virtual void const* GetData() const override;
171  virtual HdTupleType GetTupleType() const override;
172  virtual size_t GetNumElements() const override;
173  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
174  virtual bool Resolve() override;
175  virtual bool HasPreChainedBuffer() const override;
176  virtual HdBufferSourceSharedPtr GetPreChainedBuffer() const override;
177 
178 protected:
179  virtual bool _CheckValid() const override;
180 
181 private:
182  HdSt_MeshTopology *_topology;
183  HdBufferSourceSharedPtr _source;
184  HdBufferSourceSharedPtr _osdTopology;
185  VERTEX_BUFFER *_cpuVertexBuffer;
186  bool _varying;
187 };
188 
189 // ---------------------------------------------------------------------------
194 class HdSt_OsdRefineComputationGPU : public HdComputation {
195 public:
196  HdSt_OsdRefineComputationGPU(HdSt_MeshTopology *topology,
197  TfToken const &name,
198  HdType type);
199 
200  virtual void Execute(HdBufferArrayRangeSharedPtr const &range,
201  HdResourceRegistry *resourceRegistry) override;
202  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
203  virtual int GetNumOutputElements() const override;
204 
205  // A wrapper class to bridge between HdBufferResource and OpenSubdiv
206  // vertex buffer API.
207  //
208  class VertexBuffer {
209  public:
210  VertexBuffer(HdBufferResourceSharedPtr const &resource) {
211  _resource =
212  boost::static_pointer_cast<HdStBufferResourceGL> (resource);
213  }
214 
215  // bit confusing, osd expects 'GetNumElements()' returns the num
216  // components, in Storm sense
217  size_t GetNumElements() const {
218  return HdGetComponentCount(_resource->GetTupleType().type);
219  }
220  GLuint BindVBO() {
221  return _resource->GetId();
222  }
223  HdStBufferResourceGLSharedPtr _resource;
224  };
225 
226 private:
227  HdSt_MeshTopology *_topology;
228  TfToken _name;
229 };
230 
231 // ---------------------------------------------------------------------------
232 // template implementations
233 template <typename VERTEX_BUFFER>
234 HdSt_OsdRefineComputation<VERTEX_BUFFER>::HdSt_OsdRefineComputation(
235  HdSt_MeshTopology *topology,
236  HdBufferSourceSharedPtr const &source,
237  bool varying,
238  HdBufferSourceSharedPtr const &osdTopology)
239  : _topology(topology), _source(source), _osdTopology(osdTopology),
240  _cpuVertexBuffer(NULL), _varying(varying)
241 {
242 }
243 
244 template <typename VERTEX_BUFFER>
245 HdSt_OsdRefineComputation<VERTEX_BUFFER>::~HdSt_OsdRefineComputation()
246 {
247  delete _cpuVertexBuffer;
248 }
249 
250 template <typename VERTEX_BUFFER>
251 TfToken const &
252 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetName() const
253 {
254  return _source->GetName();
255 }
256 
257 template <typename VERTEX_BUFFER>
258 size_t
259 HdSt_OsdRefineComputation<VERTEX_BUFFER>::ComputeHash() const
260 {
261  return 0;
262 }
263 
264 template <typename VERTEX_BUFFER>
265 void const*
266 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetData() const
267 {
268  return _cpuVertexBuffer->BindCpuBuffer();
269 }
270 
271 template <typename VERTEX_BUFFER>
273 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetTupleType() const
274 {
275  return _source->GetTupleType();
276 }
277 
278 template <typename VERTEX_BUFFER>
279 size_t
280 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetNumElements() const
281 {
282  return _cpuVertexBuffer->GetNumVertices();
283 }
284 
285 template <typename VERTEX_BUFFER>
286 bool
287 HdSt_OsdRefineComputation<VERTEX_BUFFER>::Resolve()
288 {
289  if (_source && !_source->IsResolved()) return false;
290  if (_osdTopology && !_osdTopology->IsResolved()) return false;
291 
292  if (!_TryLock()) return false;
293 
294  HD_TRACE_FUNCTION();
295  HF_MALLOC_TAG_FUNCTION();
296 
297  HdSt_Subdivision *subdivision = _topology->GetSubdivision();
298  if (!TF_VERIFY(subdivision)) {
299  _SetResolved();
300  return true;
301  }
302 
303  // prepare cpu vertex buffer including refined vertices
304  TF_VERIFY(!_cpuVertexBuffer);
305  _cpuVertexBuffer = VERTEX_BUFFER::Create(
306  HdGetComponentCount(_source->GetTupleType().type),
307  subdivision->GetNumVertices());
308 
309  subdivision->RefineCPU(_source, _varying, _cpuVertexBuffer);
310 
311  HD_PERF_COUNTER_INCR(HdPerfTokens->subdivisionRefineCPU);
312 
313  _SetResolved();
314  return true;
315 }
316 
317 template <typename VERTEX_BUFFER>
318 bool
319 HdSt_OsdRefineComputation<VERTEX_BUFFER>::_CheckValid() const
320 {
321  bool valid = _source->IsValid();
322 
323  // _osdTopology is optional
324  valid &= _osdTopology ? _osdTopology->IsValid() : true;
325 
326  return valid;
327 }
328 
329 template <typename VERTEX_BUFFER>
330 void
331 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetBufferSpecs(HdBufferSpecVector *specs) const
332 {
333  // produces same spec buffer as source
334  _source->GetBufferSpecs(specs);
335 }
336 
337 template <typename VERTEX_BUFFER>
338 bool
339 HdSt_OsdRefineComputation<VERTEX_BUFFER>::HasPreChainedBuffer() const
340 {
341  return true;
342 }
343 
344 template <typename VERTEX_BUFFER>
345 HdBufferSourceSharedPtr
346 HdSt_OsdRefineComputation<VERTEX_BUFFER>::GetPreChainedBuffer() const
347 {
348  return _source;
349 }
350 
351 
352 PXR_NAMESPACE_CLOSE_SCOPE
353 
354 #endif // HDST_SUBDIVISION_H
virtual int GetNumOutputElements() const =0
Returns the size of its destination buffer (located by range argument of Execute()).
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
virtual void const * GetData() const =0
Following interfaces will be called after Resolve.
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.
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.
A central registry of all GPU resources.
virtual bool _CheckValid() const =0
Checks the validity of the source buffer.
virtual HD_API bool HasChainedBuffer() const
Returns true if this buffer has any chained buffer(s)
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
virtual void Execute(HdBufferArrayRangeSharedPtr const &range, HdResourceRegistry *resourceRegistry)=0
Execute computation.
virtual size_t GetNumElements() const =0
Returns the number of elements (e.g.
An interface class for GPU computation.
Definition: computation.h:50
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.
A specific type of HdBufferResource (GPU resource) representing an OpenGL buffer object.
virtual void GetBufferSpecs(HdBufferSpecVector *specs) const =0
Add the buffer spec for this computation into given bufferspec vector.
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