glComputeEvaluator.h
Go to the documentation of this file.
1 //
2 // Copyright 2015 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 OPENSUBDIV3_OSD_GL_COMPUTE_EVALUATOR_H
26 #define OPENSUBDIV3_OSD_GL_COMPUTE_EVALUATOR_H
27 
28 #include "../version.h"
29 
30 #include "../osd/opengl.h"
31 #include "../osd/types.h"
32 #include "../osd/bufferDescriptor.h"
33 
34 namespace OpenSubdiv {
35 namespace OPENSUBDIV_VERSION {
36 
37 namespace Far {
38  class PatchTable;
39  class StencilTable;
40  class LimitStencilTable;
41 }
42 
43 namespace Osd {
44 
52 public:
53  static GLStencilTableSSBO *Create(Far::StencilTable const *stencilTable,
54  void *deviceContext = NULL) {
55  (void)deviceContext; // unused
56  return new GLStencilTableSSBO(stencilTable);
57  }
59  Far::LimitStencilTable const *limitStencilTable,
60  void *deviceContext = NULL) {
61  (void)deviceContext; // unused
62  return new GLStencilTableSSBO(limitStencilTable);
63  }
64 
65  explicit GLStencilTableSSBO(Far::StencilTable const *stencilTable);
66  explicit GLStencilTableSSBO(Far::LimitStencilTable const *limitStencilTable);
68 
69  // interfaces needed for GLSLComputeKernel
70  GLuint GetSizesBuffer() const { return _sizes; }
71  GLuint GetOffsetsBuffer() const { return _offsets; }
72  GLuint GetIndicesBuffer() const { return _indices; }
73  GLuint GetWeightsBuffer() const { return _weights; }
74  GLuint GetDuWeightsBuffer() const { return _duWeights; }
75  GLuint GetDvWeightsBuffer() const { return _dvWeights; }
76  int GetNumStencils() const { return _numStencils; }
77 
78 private:
79  GLuint _sizes;
80  GLuint _offsets;
81  GLuint _indices;
82  GLuint _weights;
83  GLuint _duWeights;
84  GLuint _dvWeights;
85  int _numStencils;
86 };
87 
88 // ---------------------------------------------------------------------------
89 
91 public:
92  typedef bool Instantiatable;
93  static GLComputeEvaluator * Create(BufferDescriptor const &srcDesc,
94  BufferDescriptor const &dstDesc,
95  BufferDescriptor const &duDesc,
96  BufferDescriptor const &dvDesc,
97  void * deviceContext = NULL) {
98  (void)deviceContext; // not used
99  GLComputeEvaluator *instance = new GLComputeEvaluator();
100  if (instance->Compile(srcDesc, dstDesc, duDesc, dvDesc)) return instance;
101  delete instance;
102  return NULL;
103  }
104 
107 
110 
116 
144  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
145  static bool EvalStencils(
146  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
147  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
148  STENCIL_TABLE const *stencilTable,
149  GLComputeEvaluator const *instance,
150  void * deviceContext = NULL) {
151 
152  if (instance) {
153  return instance->EvalStencils(srcBuffer, srcDesc,
154  dstBuffer, dstDesc,
155  stencilTable);
156  } else {
157  // Create a kernel on demand (slow)
158  (void)deviceContext; // unused
159  instance = Create(srcDesc, dstDesc,
161  BufferDescriptor());
162  if (instance) {
163  bool r = instance->EvalStencils(srcBuffer, srcDesc,
164  dstBuffer, dstDesc,
165  stencilTable);
166  delete instance;
167  return r;
168  }
169  return false;
170  }
171  }
172 
212  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
213  static bool EvalStencils(
214  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
215  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
216  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
217  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
218  STENCIL_TABLE const *stencilTable,
219  GLComputeEvaluator const *instance,
220  void * deviceContext = NULL) {
221 
222  if (instance) {
223  return instance->EvalStencils(srcBuffer, srcDesc,
224  dstBuffer, dstDesc,
225  duBuffer, duDesc,
226  dvBuffer, dvDesc,
227  stencilTable);
228  } else {
229  // Create a kernel on demand (slow)
230  (void)deviceContext; // unused
231  instance = Create(srcDesc, dstDesc, duDesc, dvDesc);
232  if (instance) {
233  bool r = instance->EvalStencils(srcBuffer, srcDesc,
234  dstBuffer, dstDesc,
235  duBuffer, duDesc,
236  dvBuffer, dvDesc,
237  stencilTable);
238  delete instance;
239  return r;
240  }
241  return false;
242  }
243  }
244 
247  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
249  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
250  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
251  STENCIL_TABLE const *stencilTable) const {
252  return EvalStencils(srcBuffer->BindVBO(), srcDesc,
253  dstBuffer->BindVBO(), dstDesc,
254  0, BufferDescriptor(),
255  0, BufferDescriptor(),
256  stencilTable->GetSizesBuffer(),
257  stencilTable->GetOffsetsBuffer(),
258  stencilTable->GetIndicesBuffer(),
259  stencilTable->GetWeightsBuffer(),
260  0,
261  0,
262  /* start = */ 0,
263  /* end = */ stencilTable->GetNumStencils());
264  }
265 
268  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
270  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
271  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
272  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
273  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
274  STENCIL_TABLE const *stencilTable) const {
275  return EvalStencils(srcBuffer->BindVBO(), srcDesc,
276  dstBuffer->BindVBO(), dstDesc,
277  duBuffer->BindVBO(), duDesc,
278  dvBuffer->BindVBO(), dvDesc,
279  stencilTable->GetSizesBuffer(),
280  stencilTable->GetOffsetsBuffer(),
281  stencilTable->GetIndicesBuffer(),
282  stencilTable->GetWeightsBuffer(),
283  stencilTable->GetDuWeightsBuffer(),
284  stencilTable->GetDvWeightsBuffer(),
285  /* start = */ 0,
286  /* end = */ stencilTable->GetNumStencils());
287  }
288 
291  bool EvalStencils(GLuint srcBuffer, BufferDescriptor const &srcDesc,
292  GLuint dstBuffer, BufferDescriptor const &dstDesc,
293  GLuint duBuffer, BufferDescriptor const &duDesc,
294  GLuint dvBuffer, BufferDescriptor const &dvDesc,
295  GLuint sizesBuffer,
296  GLuint offsetsBuffer,
297  GLuint indicesBuffer,
298  GLuint weightsBuffer,
299  GLuint duWeightsBuffer,
300  GLuint dvWeightsBuffer,
301  int start,
302  int end) const;
303 
342  template <typename SRC_BUFFER, typename DST_BUFFER,
343  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
344  static bool EvalPatches(
345  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
346  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
347  int numPatchCoords,
348  PATCHCOORD_BUFFER *patchCoords,
349  PATCH_TABLE *patchTable,
350  GLComputeEvaluator const *instance,
351  void * deviceContext = NULL) {
352 
353  if (instance) {
354  return instance->EvalPatches(srcBuffer, srcDesc,
355  dstBuffer, dstDesc,
356  numPatchCoords, patchCoords,
357  patchTable);
358  } else {
359  // Create an instance on demand (slow)
360  (void)deviceContext; // unused
361  instance = Create(srcDesc, dstDesc,
363  BufferDescriptor());
364  if (instance) {
365  bool r = instance->EvalPatches(srcBuffer, srcDesc,
366  dstBuffer, dstDesc,
367  numPatchCoords, patchCoords,
368  patchTable);
369  delete instance;
370  return r;
371  }
372  return false;
373  }
374  }
375 
416  template <typename SRC_BUFFER, typename DST_BUFFER,
417  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
418  static bool EvalPatches(
419  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
420  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
421  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
422  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
423  int numPatchCoords,
424  PATCHCOORD_BUFFER *patchCoords,
425  PATCH_TABLE *patchTable,
426  GLComputeEvaluator const *instance,
427  void * deviceContext = NULL) {
428 
429  if (instance) {
430  return instance->EvalPatches(srcBuffer, srcDesc,
431  dstBuffer, dstDesc,
432  duBuffer, duDesc,
433  dvBuffer, dvDesc,
434  numPatchCoords, patchCoords,
435  patchTable);
436  } else {
437  // Create an instance on demand (slow)
438  (void)deviceContext; // unused
439  instance = Create(srcDesc, dstDesc, duDesc, dvDesc);
440  if (instance) {
441  bool r = instance->EvalPatches(srcBuffer, srcDesc,
442  dstBuffer, dstDesc,
443  duBuffer, duDesc,
444  dvBuffer, dvDesc,
445  numPatchCoords, patchCoords,
446  patchTable);
447  delete instance;
448  return r;
449  }
450  return false;
451  }
452  }
453 
478  template <typename SRC_BUFFER, typename DST_BUFFER,
479  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
481  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
482  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
483  int numPatchCoords,
484  PATCHCOORD_BUFFER *patchCoords,
485  PATCH_TABLE *patchTable) const {
486 
487  return EvalPatches(srcBuffer->BindVBO(), srcDesc,
488  dstBuffer->BindVBO(), dstDesc,
489  0, BufferDescriptor(),
490  0, BufferDescriptor(),
491  numPatchCoords,
492  patchCoords->BindVBO(),
493  patchTable->GetPatchArrays(),
494  patchTable->GetPatchIndexBuffer(),
495  patchTable->GetPatchParamBuffer());
496  }
497 
532  template <typename SRC_BUFFER, typename DST_BUFFER,
533  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
535  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
536  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
537  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
538  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
539  int numPatchCoords,
540  PATCHCOORD_BUFFER *patchCoords,
541  PATCH_TABLE *patchTable) const {
542 
543  return EvalPatches(srcBuffer->BindVBO(), srcDesc,
544  dstBuffer->BindVBO(), dstDesc,
545  duBuffer->BindVBO(), duDesc,
546  dvBuffer->BindVBO(), dvDesc,
547  numPatchCoords,
548  patchCoords->BindVBO(),
549  patchTable->GetPatchArrays(),
550  patchTable->GetPatchIndexBuffer(),
551  patchTable->GetPatchParamBuffer());
552  }
553 
554  bool EvalPatches(GLuint srcBuffer, BufferDescriptor const &srcDesc,
555  GLuint dstBuffer, BufferDescriptor const &dstDesc,
556  GLuint duBuffer, BufferDescriptor const &duDesc,
557  GLuint dvBuffer, BufferDescriptor const &dvDesc,
558  int numPatchCoords,
559  GLuint patchCoordsBuffer,
560  const PatchArrayVector &patchArrays,
561  GLuint patchIndexBuffer,
562  GLuint patchParamsBuffer) const;
563 
569 
572  bool Compile(BufferDescriptor const &srcDesc,
573  BufferDescriptor const &dstDesc,
574  BufferDescriptor const &duDesc,
575  BufferDescriptor const &dvDesc);
576 
578  static void Synchronize(void *deviceContext);
579 
580 private:
581  struct _StencilKernel {
582  _StencilKernel();
583  ~_StencilKernel();
584  bool Compile(BufferDescriptor const &srcDesc,
585  BufferDescriptor const &dstDesc,
586  BufferDescriptor const &duDesc,
587  BufferDescriptor const &dvDesc,
588  int workGroupSize);
589  GLuint program;
590  GLuint uniformStart;
591  GLuint uniformEnd;
592  GLuint uniformSrcOffset;
593  GLuint uniformDstOffset;
594  GLuint uniformDuDesc;
595  GLuint uniformDvDesc;
596  } _stencilKernel;
597 
598  struct _PatchKernel {
599  _PatchKernel();
600  ~_PatchKernel();
601  bool Compile(BufferDescriptor const &srcDesc,
602  BufferDescriptor const &dstDesc,
603  BufferDescriptor const &duDesc,
604  BufferDescriptor const &dvDesc,
605  int workGroupSize);
606  GLuint program;
607  GLuint uniformSrcOffset;
608  GLuint uniformDstOffset;
609  GLuint uniformPatchArray;
610  GLuint uniformDuDesc;
611  GLuint uniformDvDesc;
612 
613  } _patchKernel;
614 
615  int _workGroupSize;
616 };
617 
618 } // end namespace Osd
619 
620 } // end namespace OPENSUBDIV_VERSION
621 using namespace OPENSUBDIV_VERSION;
622 
623 } // end namespace OpenSubdiv
624 
625 
626 #endif // OPENSUBDIV3_OSD_GL_COMPUTE_EVALUATOR_H
bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable) const
Generic limit eval function. This function has a same signature as other device kernels have so that ...
static GLStencilTableSSBO * Create(Far::LimitStencilTable const *limitStencilTable, void *deviceContext=NULL)
BufferDescriptor is a struct which describes buffer elements in interleaved data buffers. Almost all Osd Evaluator APIs take BufferDescriptors along with device-specific buffer objects.
GLStencilTableSSBO(Far::StencilTable const *stencilTable)
std::vector< PatchArray > PatchArrayVector
Definition: types.h:98
bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, DST_BUFFER *duBuffer, BufferDescriptor const &duDesc, DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc, STENCIL_TABLE const *stencilTable) const
static bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable, GLComputeEvaluator const *instance, void *deviceContext=NULL)
Generic limit eval function. This function has a same signature as other device kernels have so that ...
static GLComputeEvaluator * Create(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc, void *deviceContext=NULL)
static void Synchronize(void *deviceContext)
Wait the dispatched kernel finishes.
GL stencil table (Shader Storage buffer)
static GLStencilTableSSBO * Create(Far::StencilTable const *stencilTable, void *deviceContext=NULL)
bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable) const
bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, DST_BUFFER *duBuffer, BufferDescriptor const &duDesc, DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable) const
Generic limit eval function with derivatives. This function has a same signature as other device kern...
static bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable, GLComputeEvaluator const *instance, void *deviceContext=NULL)
Generic static compute function. This function has a same signature as other device kernels have so t...
bool Compile(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc)
static bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, DST_BUFFER *duBuffer, BufferDescriptor const &duDesc, DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc, STENCIL_TABLE const *stencilTable, GLComputeEvaluator const *instance, void *deviceContext=NULL)
Generic static compute function. This function has a same signature as other device kernels have so t...
~GLComputeEvaluator()
Destructor. note that the GL context must be made current.
Table of limit subdivision stencils.
Definition: stencilTable.h:285
static bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, DST_BUFFER *duBuffer, BufferDescriptor const &duDesc, DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable, GLComputeEvaluator const *instance, void *deviceContext=NULL)
Generic limit eval function. This function has a same signature as other device kernels have so that ...