glXFBEvaluator.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_XFB_EVALUATOR_H
26 #define OPENSUBDIV3_OSD_GL_XFB_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 
53 public:
55  Far::StencilTable const *stencilTable, void *deviceContext = NULL) {
56  (void)deviceContext; // unused
57  return new GLStencilTableTBO(stencilTable);
58  }
59 
61  Far::LimitStencilTable const *limitStencilTable,
62  void *deviceContext = NULL) {
63  (void)deviceContext; // unused
64  return new GLStencilTableTBO(limitStencilTable);
65  }
66 
67  explicit GLStencilTableTBO(Far::StencilTable const *stencilTable);
68  explicit GLStencilTableTBO(Far::LimitStencilTable const *limitStencilTable);
70 
71  // interfaces needed for GLSLTransformFeedbackKernel
72  GLuint GetSizesTexture() const { return _sizes; }
73  GLuint GetOffsetsTexture() const { return _offsets; }
74  GLuint GetIndicesTexture() const { return _indices; }
75  GLuint GetWeightsTexture() const { return _weights; }
76  GLuint GetDuWeightsTexture() const { return _duWeights; }
77  GLuint GetDvWeightsTexture() const { return _dvWeights; }
78  int GetNumStencils() const { return _numStencils; }
79 
80 private:
81  GLuint _sizes;
82  GLuint _offsets;
83  GLuint _indices;
84  GLuint _weights;
85  GLuint _duWeights;
86  GLuint _dvWeights;
87  int _numStencils;
88 };
89 
90 // ---------------------------------------------------------------------------
91 
93 public:
94  typedef bool Instantiatable;
95  static GLXFBEvaluator * Create(BufferDescriptor const &srcDesc,
96  BufferDescriptor const &dstDesc,
97  BufferDescriptor const &duDesc,
98  BufferDescriptor const &dvDesc,
99  void * deviceContext = NULL) {
100  (void)deviceContext; // not used
101  GLXFBEvaluator *instance = new GLXFBEvaluator();
102  if (instance->Compile(srcDesc, dstDesc, duDesc, dvDesc))
103  return instance;
104  delete instance;
105  return NULL;
106  }
107 
109  GLXFBEvaluator();
110 
112  ~GLXFBEvaluator();
113 
119 
147  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
148  static bool EvalStencils(
149  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
150  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
151  STENCIL_TABLE const *stencilTable,
152  GLXFBEvaluator const *instance,
153  void * deviceContext = NULL) {
154 
155  if (instance) {
156  return instance->EvalStencils(srcBuffer, srcDesc,
157  dstBuffer, dstDesc,
158  stencilTable);
159  } else {
160  // Create an instance on demand (slow)
161  (void)deviceContext; // unused
162  instance = Create(srcDesc, dstDesc,
164  BufferDescriptor());
165  if (instance) {
166  bool r = instance->EvalStencils(srcBuffer, srcDesc,
167  dstBuffer, dstDesc,
168  stencilTable);
169  delete instance;
170  return r;
171  }
172  return false;
173  }
174  }
175 
215  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
216  static bool EvalStencils(
217  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
218  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
219  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
220  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
221  STENCIL_TABLE const *stencilTable,
222  GLXFBEvaluator const *instance,
223  void * deviceContext = NULL) {
224 
225  if (instance) {
226  return instance->EvalStencils(srcBuffer, srcDesc,
227  dstBuffer, dstDesc,
228  duBuffer, duDesc,
229  dvBuffer, dvDesc,
230  stencilTable);
231  } else {
232  // Create an instance on demand (slow)
233  (void)deviceContext; // unused
234  instance = Create(srcDesc, dstDesc, duDesc, dvDesc);
235  if (instance) {
236  bool r = instance->EvalStencils(srcBuffer, srcDesc,
237  dstBuffer, dstDesc,
238  duBuffer, duDesc,
239  dvBuffer, dvDesc,
240  stencilTable);
241  delete instance;
242  return r;
243  }
244  return false;
245  }
246  }
247 
264  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
266  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
267  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
268  STENCIL_TABLE const *stencilTable) const {
269 
270  return EvalStencils(srcBuffer->BindVBO(), srcDesc,
271  dstBuffer->BindVBO(), dstDesc,
272  0, BufferDescriptor(),
273  0, BufferDescriptor(),
274  stencilTable->GetSizesTexture(),
275  stencilTable->GetOffsetsTexture(),
276  stencilTable->GetIndicesTexture(),
277  stencilTable->GetWeightsTexture(),
278  0,
279  0,
280  /* start = */ 0,
281  /* end = */ stencilTable->GetNumStencils());
282  }
283 
308  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
310  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
311  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
312  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
313  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
314  STENCIL_TABLE const *stencilTable) const {
315 
316  return EvalStencils(srcBuffer->BindVBO(), srcDesc,
317  dstBuffer->BindVBO(), dstDesc,
318  duBuffer->BindVBO(), duDesc,
319  dvBuffer->BindVBO(), dvDesc,
320  stencilTable->GetSizesTexture(),
321  stencilTable->GetOffsetsTexture(),
322  stencilTable->GetIndicesTexture(),
323  stencilTable->GetWeightsTexture(),
324  stencilTable->GetDuWeightsTexture(),
325  stencilTable->GetDvWeightsTexture(),
326  /* start = */ 0,
327  /* end = */ stencilTable->GetNumStencils());
328  }
329 
365  bool EvalStencils(GLuint srcBuffer, BufferDescriptor const &srcDesc,
366  GLuint dstBuffer, BufferDescriptor const &dstDesc,
367  GLuint duBuffer, BufferDescriptor const &duDesc,
368  GLuint dvBuffer, BufferDescriptor const &dvDesc,
369  GLuint sizesBuffer,
370  GLuint offsetsBuffer,
371  GLuint indicesBuffer,
372  GLuint weightsBuffer,
373  GLuint duWeightsBuffer,
374  GLuint dvWeightsBuffer,
375  int start,
376  int end) const;
377 
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  int numPatchCoords,
422  PATCHCOORD_BUFFER *patchCoords,
423  PATCH_TABLE *patchTable,
424  GLXFBEvaluator const *instance,
425  void * deviceContext = NULL) {
426 
427  if (instance) {
428  return instance->EvalPatches(srcBuffer, srcDesc,
429  dstBuffer, dstDesc,
430  numPatchCoords, patchCoords,
431  patchTable);
432  } else {
433  // Create an instance on demand (slow)
434  (void)deviceContext; // unused
435  instance = Create(srcDesc, dstDesc,
437  BufferDescriptor());
438  if (instance) {
439  bool r = instance->EvalPatches(srcBuffer, srcDesc,
440  dstBuffer, dstDesc,
441  numPatchCoords, patchCoords,
442  patchTable);
443  delete instance;
444  return r;
445  }
446  return false;
447  }
448  }
449 
490  template <typename SRC_BUFFER, typename DST_BUFFER,
491  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
492  static bool EvalPatches(
493  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
494  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
495  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
496  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
497  int numPatchCoords,
498  PATCHCOORD_BUFFER *patchCoords,
499  PATCH_TABLE *patchTable,
500  GLXFBEvaluator const *instance,
501  void * deviceContext = NULL) {
502 
503  if (instance) {
504  return instance->EvalPatches(srcBuffer, srcDesc,
505  dstBuffer, dstDesc,
506  duBuffer, duDesc,
507  dvBuffer, dvDesc,
508  numPatchCoords, patchCoords,
509  patchTable);
510  } else {
511  // Create an instance on demand (slow)
512  (void)deviceContext; // unused
513  instance = Create(srcDesc, dstDesc, duDesc, dvDesc);
514  if (instance) {
515  bool r = instance->EvalPatches(srcBuffer, srcDesc,
516  dstBuffer, dstDesc,
517  duBuffer, duDesc,
518  dvBuffer, dvDesc,
519  numPatchCoords, patchCoords,
520  patchTable);
521  delete instance;
522  return r;
523  }
524  return false;
525  }
526  }
527 
552  template <typename SRC_BUFFER, typename DST_BUFFER,
553  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
555  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
556  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
557  int numPatchCoords,
558  PATCHCOORD_BUFFER *patchCoords,
559  PATCH_TABLE *patchTable) const {
560 
561  return EvalPatches(srcBuffer->BindVBO(), srcDesc,
562  dstBuffer->BindVBO(), dstDesc,
563  0, BufferDescriptor(),
564  0, BufferDescriptor(),
565  numPatchCoords,
566  patchCoords->BindVBO(),
567  patchTable->GetPatchArrays(),
568  patchTable->GetPatchIndexTextureBuffer(),
569  patchTable->GetPatchParamTextureBuffer());
570  }
571 
606  template <typename SRC_BUFFER, typename DST_BUFFER,
607  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
609  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
610  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
611  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
612  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
613  int numPatchCoords,
614  PATCHCOORD_BUFFER *patchCoords,
615  PATCH_TABLE *patchTable) const {
616 
617  return EvalPatches(srcBuffer->BindVBO(), srcDesc,
618  dstBuffer->BindVBO(), dstDesc,
619  duBuffer->BindVBO(), duDesc,
620  dvBuffer->BindVBO(), dvDesc,
621  numPatchCoords,
622  patchCoords->BindVBO(),
623  patchTable->GetPatchArrays(),
624  patchTable->GetPatchIndexTextureBuffer(),
625  patchTable->GetPatchParamTextureBuffer());
626  }
627 
628  bool EvalPatches(GLuint srcBuffer, BufferDescriptor const &srcDesc,
629  GLuint dstBuffer, BufferDescriptor const &dstDesc,
630  GLuint duBuffer, BufferDescriptor const &duDesc,
631  GLuint dvBuffer, BufferDescriptor const &dvDesc,
632  int numPatchCoords,
633  GLuint patchCoordsBuffer,
634  const PatchArrayVector &patchArrays,
635  GLuint patchIndexBuffer,
636  GLuint patchParamsBuffer) const;
637 
643 
646  bool Compile(BufferDescriptor const &srcDesc,
647  BufferDescriptor const &dstDesc,
648  BufferDescriptor const &duDesc,
649  BufferDescriptor const &dvDesc);
650 
652  static void Synchronize(void *kernel);
653 
654 private:
655  GLuint _srcBufferTexture;
656 
657  struct _StencilKernel {
658  _StencilKernel();
659  ~_StencilKernel();
660  bool Compile(BufferDescriptor const &srcDesc,
661  BufferDescriptor const &dstDesc,
662  BufferDescriptor const &duDesc,
663  BufferDescriptor const &dvDesc);
664  GLuint program;
665  GLint uniformSrcBufferTexture;
666  GLint uniformSrcOffset; // src buffer offset (in elements)
667 
668  GLint uniformSizesTexture;
669  GLint uniformOffsetsTexture;
670  GLint uniformIndicesTexture;
671  GLint uniformWeightsTexture;
672  GLint uniformDuWeightsTexture;
673  GLint uniformDvWeightsTexture;
674  GLint uniformStart; // range
675  GLint uniformEnd;
676  } _stencilKernel;
677 
678  struct _PatchKernel {
679  _PatchKernel();
680  ~_PatchKernel();
681  bool Compile(BufferDescriptor const &srcDesc,
682  BufferDescriptor const &dstDesc,
683  BufferDescriptor const &duDesc,
684  BufferDescriptor const &dvDesc);
685  GLuint program;
686  GLint uniformSrcBufferTexture;
687  GLint uniformSrcOffset; // src buffer offset (in elements)
688 
689  GLint uniformPatchArray;
690  GLint uniformPatchParamTexture;
691  GLint uniformPatchIndexTexture;
692  } _patchKernel;
693 
694 };
695 
696 } // end namespace Osd
697 
698 } // end namespace OPENSUBDIV_VERSION
699 using namespace OPENSUBDIV_VERSION;
700 
701 } // end namespace OpenSubdiv
702 
703 
704 #endif // OPENSUBDIV3_OSD_GL_XFB_EVALUATOR_H
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, GLXFBEvaluator const *instance, void *deviceContext=NULL)
Generic static stencil function. This function has a same signature as other device kernels have so t...
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.
static GLStencilTableTBO * Create(Far::StencilTable const *stencilTable, void *deviceContext=NULL)
std::vector< PatchArray > PatchArrayVector
Definition: types.h:98
~GLXFBEvaluator()
Destructor. note that the GL context must be made current.
static GLStencilTableTBO * Create(Far::LimitStencilTable const *limitStencilTable, void *deviceContext=NULL)
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 ...
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
Generic eval stencils function with derivative evaluation.
static void Synchronize(void *kernel)
Wait the dispatched kernel finishes.
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, GLXFBEvaluator const *instance, void *deviceContext=NULL)
Generic static stencil function. This function has a same signature as other device kernels have so t...
static bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable, GLXFBEvaluator const *instance, void *deviceContext=NULL)
Generic limit eval function. This function has a same signature as other device kernels have so that ...
bool Compile(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc)
bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable) const
Generic eval stencils function.
GLStencilTableTBO(Far::StencilTable const *stencilTable)
static GLXFBEvaluator * Create(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc, void *deviceContext=NULL)
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, GLXFBEvaluator const *instance, void *deviceContext=NULL)
Generic limit eval function. This function has a same signature as other device kernels have so that ...