All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
d3d11Mesh.h
Go to the documentation of this file.
1 //
2 // Copyright 2013 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 
25 #ifndef OSD_D3D11MESH_H
26 #define OSD_D3D11MESH_H
27 
28 #include "../version.h"
29 
30 #include "../osd/mesh.h"
31 #include "../osd/d3d11ComputeController.h"
32 #include "../osd/d3d11DrawContext.h"
33 #include "../osd/d3d11VertexBuffer.h"
34 
35 namespace OpenSubdiv {
36 namespace OPENSUBDIV_VERSION {
37 
38 namespace Osd {
39 
41 
42 template <class VERTEX_BUFFER, class COMPUTE_CONTROLLER>
43 class Mesh<VERTEX_BUFFER, COMPUTE_CONTROLLER, D3D11DrawContext> : public D3D11MeshInterface {
44 public:
45  typedef VERTEX_BUFFER VertexBuffer;
46  typedef COMPUTE_CONTROLLER ComputeController;
47  typedef typename ComputeController::ComputeContext ComputeContext;
50 
51  Mesh(ComputeController * computeController,
52  Far::TopologyRefiner * refiner,
53  int numVertexElements,
54  int numVaryingElements,
55  int level,
56  MeshBitset bits,
57  ID3D11DeviceContext *d3d11DeviceContext) :
58 
59  _refiner(refiner),
60  _patchTables(0),
61  _vertexBuffer(0),
62  _varyingBuffer(0),
63  _computeContext(0),
64  _computeController(computeController),
65  _drawContext(0),
66  _d3d11DeviceContext(d3d11DeviceContext)
67  {
68  D3D11MeshInterface::refineMesh(*_refiner, level, bits.test(MeshAdaptive));
69 
70  int numElements =
71  initializeVertexBuffers(numVertexElements, numVaryingElements, bits);
72 
73  initializeComputeContext(numVertexElements, numVaryingElements);
74 
75  initializeDrawContext(numElements, bits);
76  }
77 
78  Mesh(ComputeController * computeController,
79  Far::TopologyRefiner * refiner,
80  VertexBuffer * vertexBuffer,
81  VertexBuffer * varyingBuffer,
82  ComputeContext * computeContext,
83  DrawContext * drawContext,
84  ID3D11DeviceContext *d3d11DeviceContext) :
85 
86  _refiner(refiner),
87  _vertexBuffer(vertexBuffer),
88  _varyingBuffer(varyingBuffer),
89  _computeContext(computeContext),
90  _computeController(computeController),
91  _drawContext(drawContext),
92  _d3d11DeviceContext(d3d11DeviceContext)
93  {
94  _drawContext->UpdateVertexTexture(_vertexBuffer, _d3d11DeviceContext);
95  }
96 
97  virtual ~Mesh() {
98  delete _refiner;
99  delete _patchTables;
100  delete _vertexBuffer;
101  delete _varyingBuffer;
102  delete _computeContext;
103  delete _drawContext;
104  }
105 
106  virtual int GetNumVertices() const {
107  assert(_refiner);
108  return D3D11MeshInterface::getNumVertices(*_refiner);
109  }
110 
111  virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts) {
112  _vertexBuffer->UpdateData(vertexData, startVertex, numVerts, _d3d11DeviceContext);
113  }
114  virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts) {
115  _varyingBuffer->UpdateData(varyingData, startVertex, numVerts, _d3d11DeviceContext);
116  }
117  virtual void Refine() {
118  _computeController->Compute(_computeContext, _kernelBatches, _vertexBuffer, _varyingBuffer);
119  }
120  virtual void Refine(VertexBufferDescriptor const *vertexDesc,
121  VertexBufferDescriptor const *varyingDesc,
122  bool interleaved) {
123  _computeController->Compute(_computeContext, _kernelBatches,
124  _vertexBuffer, (interleaved ? _vertexBuffer : _varyingBuffer),
125  vertexDesc, varyingDesc);
126  }
127  virtual void Synchronize() {
128  _computeController->Synchronize();
129  }
131  return _vertexBuffer->BindD3D11Buffer(_d3d11DeviceContext);
132  }
134  return _varyingBuffer->BindD3D11Buffer(_d3d11DeviceContext);
135  }
137  return _drawContext;
138  }
140  return _vertexBuffer;
141  }
143  return _varyingBuffer;
144  }
145 
146  virtual Far::TopologyRefiner const * GetTopologyRefiner() const {
147  return _refiner;
148  }
149 
150  virtual void SetFVarDataChannel(int fvarWidth,
151  std::vector<float> const & fvarData) {
152 
153  if (_patchTables and _drawContext and fvarWidth and (not fvarData.empty())) {
154  _drawContext->SetFVarDataTexture(*_patchTables,
155  _d3d11DeviceContext, fvarWidth, fvarData);
156  }
157  }
158 
159 private:
160 
161  void initializeComputeContext(int numVertexElements,
162  int numVaryingElements ) {
163 
164  assert(_refiner);
165 
167  options.generateOffsets=true;
168  options.generateAllLevels=_refiner->IsUniform() ? false : true;
169 
170  Far::StencilTables const * vertexStencils=0, * varyingStencils=0;
171 
172  if (numVertexElements>0) {
173 
174  vertexStencils = Far::StencilTablesFactory::Create(*_refiner, options);
175 
176  _kernelBatches.push_back(Far::StencilTablesFactory::Create(*vertexStencils));
177  }
178 
179  if (numVaryingElements>0) {
180 
182 
183  varyingStencils = Far::StencilTablesFactory::Create(*_refiner, options);
184  }
185 
186  _computeContext = ComputeContext::Create(vertexStencils, varyingStencils);
187 
188  delete vertexStencils;
189  delete varyingStencils;
190  }
191 
192  void initializeDrawContext(int numElements, MeshBitset bits) {
193 
194  assert(_refiner and _vertexBuffer);
195 
196  Far::PatchTablesFactory::Options options;
197  options.generateFVarTables = bits.test(MeshFVarData);
198 
199  _patchTables = Far::PatchTablesFactory::Create(*_refiner, options);
200 
201  _drawContext = DrawContext::Create(
202  _patchTables, _d3d11DeviceContext, numElements);
203 
204  _drawContext->UpdateVertexTexture(_vertexBuffer, _d3d11DeviceContext);
205  }
206 
207  int initializeVertexBuffers(int numVertexElements,
208  int numVaryingElements, MeshBitset bits) {
209 
210  ID3D11Device * pd3d11Device;
211  _d3d11DeviceContext->GetDevice(&pd3d11Device);
212 
213  int numVertices = D3D11MeshInterface::getNumVertices(*_refiner);
214 
215  int numElements = numVertexElements +
216  (bits.test(MeshInterleaveVarying) ? numVaryingElements : 0);
217 
218  if (numVertexElements) {
219 
220  _vertexBuffer =
221  VertexBuffer::Create(numElements, numVertices, pd3d11Device);
222  }
223 
224  if (numVaryingElements>0 and (not bits.test(MeshInterleaveVarying))) {
225  _varyingBuffer =
226  VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
227  }
228  return numElements;
229  }
230 
231  Far::TopologyRefiner * _refiner;
232  Far::PatchTables * _patchTables;
233  Far::KernelBatchVector _kernelBatches;
234 
235  VertexBuffer *_vertexBuffer;
236  VertexBuffer *_varyingBuffer;
237 
238  ComputeContext *_computeContext;
239  ComputeController *_computeController;
240  DrawContext *_drawContext;
241 
242  ID3D11DeviceContext *_d3d11DeviceContext;
243 };
244 
245 template <>
247 public:
253 
254  Mesh(ComputeController * computeController,
255  Far::TopologyRefiner * refiner,
256  int numVertexElements,
257  int numVaryingElements,
258  int level,
259  MeshBitset bits,
260  ID3D11DeviceContext *d3d11DeviceContext) :
261 
262  _refiner(refiner),
263  _patchTables(0),
264  _vertexBuffer(0),
265  _varyingBuffer(0),
266  _computeContext(0),
267  _computeController(computeController),
268  _drawContext(0),
269  _d3d11DeviceContext(d3d11DeviceContext)
270  {
271  D3D11MeshInterface::refineMesh(*_refiner, level, bits.test(MeshAdaptive));
272 
273  int numElements =
274  initializeVertexBuffers(numVertexElements, numVaryingElements, bits);
275 
276  initializeComputeContext(numVertexElements, numVaryingElements);
277 
278  initializeDrawContext(numElements, bits);
279  }
280 
281  Mesh(ComputeController * computeController,
282  Far::TopologyRefiner * refiner,
283  VertexBuffer * vertexBuffer,
284  VertexBuffer * varyingBuffer,
285  ComputeContext * computeContext,
286  DrawContext * drawContext,
287  ID3D11DeviceContext *d3d11DeviceContext) :
288 
289  _refiner(refiner),
290  _vertexBuffer(vertexBuffer),
291  _varyingBuffer(varyingBuffer),
292  _computeContext(computeContext),
293  _computeController(computeController),
294  _drawContext(drawContext),
295  _d3d11DeviceContext(d3d11DeviceContext)
296  {
297  _drawContext->UpdateVertexTexture(_vertexBuffer, _d3d11DeviceContext);
298  }
299 
300  virtual ~Mesh() {
301  delete _refiner;
302  delete _patchTables;
303  delete _vertexBuffer;
304  delete _varyingBuffer;
305  delete _computeContext;
306  delete _drawContext;
307  }
308 
309  virtual int GetNumVertices() const { return _refiner->GetNumVerticesTotal(); }
310 
311  virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts) {
312  _vertexBuffer->UpdateData(vertexData, startVertex, numVerts, _d3d11DeviceContext);
313  }
314  virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts) {
315  _varyingBuffer->UpdateData(varyingData, startVertex, numVerts, _d3d11DeviceContext);
316  }
317  virtual void Refine() {
318  _computeController->Compute(_computeContext, _kernelBatches, _vertexBuffer, _varyingBuffer);
319  }
320  virtual void Refine(VertexBufferDescriptor const *vertexDesc,
321  VertexBufferDescriptor const *varyingDesc,
322  bool interleaved) {
323  _computeController->Compute(_computeContext, _kernelBatches,
324  _vertexBuffer, (interleaved ? _vertexBuffer : _varyingBuffer),
325  vertexDesc, varyingDesc);
326  }
327  virtual void Synchronize() {
328  _computeController->Synchronize();
329  }
331  return _vertexBuffer->BindD3D11Buffer(_d3d11DeviceContext);
332  }
334  return _varyingBuffer->BindD3D11Buffer(_d3d11DeviceContext);
335  }
337  return _drawContext;
338  }
340  return _vertexBuffer;
341  }
343  return _varyingBuffer;
344  }
345 
346  virtual Far::TopologyRefiner const * GetTopologyRefiner() const {
347  return _refiner;
348  }
349 
350  virtual void SetFVarDataChannel(int fvarWidth,
351  std::vector<float> const & fvarData) {
352 
353  if (_patchTables and _drawContext and fvarWidth and (not fvarData.empty())) {
354  _drawContext->SetFVarDataTexture(*_patchTables,
355  _d3d11DeviceContext, fvarWidth, fvarData);
356  }
357  }
358 
359 private:
360 
361 
362  void initializeComputeContext(int numVertexElements,
363  int numVaryingElements ) {
364 
365  assert(_refiner);
366 
368  options.generateOffsets=true;
369  options.generateAllLevels=_refiner->IsUniform() ? false : true;
370 
371  Far::StencilTables const * vertexStencils=0, * varyingStencils=0;
372 
373  if (numVertexElements>0) {
374 
375  vertexStencils = Far::StencilTablesFactory::Create(*_refiner, options);
376 
377  _kernelBatches.push_back(Far::StencilTablesFactory::Create(*vertexStencils));
378  }
379 
380  if (numVaryingElements>0) {
381 
383 
384  varyingStencils = Far::StencilTablesFactory::Create(*_refiner, options);
385  }
386 
387  _computeContext =
388  ComputeContext::Create(_d3d11DeviceContext, vertexStencils, varyingStencils);
389 
390  delete vertexStencils;
391  delete varyingStencils;
392  }
393 
394  void initializeDrawContext(int numElements, MeshBitset bits) {
395 
396  assert(_refiner and _vertexBuffer);
397 
398  Far::PatchTablesFactory::Options options;
399  options.generateFVarTables = bits.test(MeshFVarData);
400 
401  _patchTables = Far::PatchTablesFactory::Create(*_refiner, options);
402 
403  _drawContext = DrawContext::Create(
404  _patchTables, _d3d11DeviceContext, numElements);
405 
406  _drawContext->UpdateVertexTexture(_vertexBuffer, _d3d11DeviceContext);
407  }
408 
409  int initializeVertexBuffers(int numVertexElements,
410  int numVaryingElements, MeshBitset bits) {
411 
412  ID3D11Device * pd3d11Device;
413  _d3d11DeviceContext->GetDevice(&pd3d11Device);
414 
415  int numVertices = D3D11MeshInterface::getNumVertices(*_refiner);
416 
417  int numElements = numVertexElements +
418  (bits.test(MeshInterleaveVarying) ? numVaryingElements : 0);
419 
420  if (numVertexElements) {
421  _vertexBuffer =
422  VertexBuffer::Create(numElements, numVertices, pd3d11Device);
423  }
424 
425  if (numVaryingElements>0 and (not bits.test(MeshInterleaveVarying))) {
426  _varyingBuffer =
427  VertexBuffer::Create(numVaryingElements, numVertices, pd3d11Device);
428  }
429  return numElements;
430  }
431 
432  Far::TopologyRefiner * _refiner;
433  Far::PatchTables * _patchTables;
434  Far::KernelBatchVector _kernelBatches;
435 
436  VertexBuffer *_vertexBuffer;
437  VertexBuffer *_varyingBuffer;
438 
439  ComputeContext *_computeContext;
440  ComputeController *_computeController;
441  DrawContext *_drawContext;
442 
443  ID3D11DeviceContext *_d3d11DeviceContext;
444 };
445 
446 
447 } // end namespace Osd
448 
449 } // end namespace OPENSUBDIV_VERSION
450 using namespace OPENSUBDIV_VERSION;
451 
452 } // end namespace OpenSubdiv
453 
454 #endif // OSD_D3D11MESH_H
455 
COMPUTE_CONTROLLER ComputeController
Definition: mesh.h:117
int generateOffsets
populate optional &quot;_offsets&quot; field
static PatchTables * Create(TopologyRefiner const &refiner, Options options=Options())
Factory constructor for PatchTables.
static void refineMesh(Far::TopologyRefiner &refiner, int level, bool adaptive)
Definition: mesh.h:99
virtual void SetFVarDataChannel(int fvarWidth, std::vector< float > const &fvarData)
Definition: d3d11Mesh.h:150
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts)
Definition: d3d11Mesh.h:111
Describes vertex elements in interleaved data buffers.
virtual void Refine(VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc, bool interleaved)
Definition: d3d11Mesh.h:320
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts)
Definition: d3d11Mesh.h:114
virtual void UpdateVertexBuffer(float const *vertexData, int startVertex, int numVerts)
Definition: d3d11Mesh.h:311
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
virtual void UpdateVaryingBuffer(float const *varyingData, int startVertex, int numVerts)
Definition: d3d11Mesh.h:314
virtual void Refine(VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc, bool interleaved)
Definition: d3d11Mesh.h:120
MeshInterface< D3D11DrawContext > D3D11MeshInterface
Definition: d3d11Mesh.h:40
int GetNumVerticesTotal() const
Returns the total number of vertices in all levels.
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, int numVertexElements, int numVaryingElements, int level, MeshBitset bits, ID3D11DeviceContext *d3d11DeviceContext)
Definition: d3d11Mesh.h:254
Compute controller for launching D3D11 Compute subdivision kernels.
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, VertexBuffer *vertexBuffer, VertexBuffer *varyingBuffer, ComputeContext *computeContext, DrawContext *drawContext, ID3D11DeviceContext *d3d11DeviceContext)
Definition: d3d11Mesh.h:78
std::bitset< NUM_MESH_BITS > MeshBitset
Definition: mesh.h:56
ComputeController::ComputeContext ComputeContext
Definition: mesh.h:118
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, int numVertexElements, int numVaryingElements, int level, MeshBitset bits, ID3D11DeviceContext *d3d11DeviceContext)
Definition: d3d11Mesh.h:51
Concrete vertex buffer class for DirectX subvision and DirectX drawing.
static StencilTables const * Create(TopologyRefiner const &refiner, Options options=Options())
Instantiates StencilTables from TopologyRefiner that have been refined uniformly or adaptively...
bool IsUniform() const
Returns true if uniform subdivision has been applied.
Stores topology data for a specified set of refinement options.
Mesh(ComputeController *computeController, Far::TopologyRefiner *refiner, VertexBuffer *vertexBuffer, VertexBuffer *varyingBuffer, ComputeContext *computeContext, DrawContext *drawContext, ID3D11DeviceContext *d3d11DeviceContext)
Definition: d3d11Mesh.h:281
static int getNumVertices(Far::TopologyRefiner const &refiner)
Definition: mesh.h:93
virtual void SetFVarDataChannel(int fvarWidth, std::vector< float > const &fvarData)
Definition: d3d11Mesh.h:350