clEvaluator.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 OPENSUBDIV_OPENSUBDIV3_OSD_CL_EVALUATOR_H
26 #define OPENSUBDIV_OPENSUBDIV3_OSD_CL_EVALUATOR_H
27 
28 #include "../version.h"
29 
30 #include "../osd/opencl.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 StencilTable;
39 }
40 
41 namespace Osd {
42 
51 public:
52  template <typename DEVICE_CONTEXT>
53  static CLStencilTable *Create(Far::StencilTable const *stencilTable,
54  DEVICE_CONTEXT context) {
55  return new CLStencilTable(stencilTable, context->GetContext());
56  }
57 
58  template <typename DEVICE_CONTEXT>
60  Far::LimitStencilTable const *limitStencilTable,
61  DEVICE_CONTEXT context) {
62  return new CLStencilTable(limitStencilTable, context->GetContext());
63  }
64 
65  CLStencilTable(Far::StencilTable const *stencilTable,
66  cl_context clContext);
67  CLStencilTable(Far::LimitStencilTable const *limitStencilTable,
68  cl_context clContext);
70 
71  // interfaces needed for CLComputeKernel
72  cl_mem GetSizesBuffer() const { return _sizes; }
73  cl_mem GetOffsetsBuffer() const { return _offsets; }
74  cl_mem GetIndicesBuffer() const { return _indices; }
75  cl_mem GetWeightsBuffer() const { return _weights; }
76  cl_mem GetDuWeightsBuffer() const { return _duWeights; }
77  cl_mem GetDvWeightsBuffer() const { return _dvWeights; }
78  int GetNumStencils() const { return _numStencils; }
79 
80 private:
81  cl_mem _sizes;
82  cl_mem _offsets;
83  cl_mem _indices;
84  cl_mem _weights;
85  cl_mem _duWeights;
86  cl_mem _dvWeights;
87  int _numStencils;
88 };
89 
90 // ---------------------------------------------------------------------------
91 
92 class CLEvaluator {
93 public:
94  typedef bool Instantiatable;
96  CLEvaluator(cl_context context, cl_command_queue queue);
97 
99  ~CLEvaluator();
100 
102  template <typename DEVICE_CONTEXT>
103  static CLEvaluator *Create(BufferDescriptor const &srcDesc,
104  BufferDescriptor const &dstDesc,
105  BufferDescriptor const &duDesc,
106  BufferDescriptor const &dvDesc,
107  DEVICE_CONTEXT deviceContext) {
108  return Create(srcDesc, dstDesc, duDesc, dvDesc,
109  deviceContext->GetContext(),
110  deviceContext->GetCommandQueue());
111  }
112 
113  static CLEvaluator * Create(BufferDescriptor const &srcDesc,
114  BufferDescriptor const &dstDesc,
115  BufferDescriptor const &duDesc,
116  BufferDescriptor const &dvDesc,
117  cl_context clContext,
118  cl_command_queue clCommandQueue) {
119  CLEvaluator *kernel = new CLEvaluator(clContext, clCommandQueue);
120  if (kernel->Compile(srcDesc, dstDesc, duDesc, dvDesc)) return kernel;
121  delete kernel;
122  return NULL;
123  }
124 
130 
161  template <typename SRC_BUFFER, typename DST_BUFFER,
162  typename STENCIL_TABLE, typename DEVICE_CONTEXT>
163  static bool EvalStencils(
164  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
165  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
166  STENCIL_TABLE const *stencilTable,
167  CLEvaluator const *instance,
168  DEVICE_CONTEXT deviceContext) {
169 
170  if (instance) {
171  return instance->EvalStencils(srcBuffer, srcDesc,
172  dstBuffer, dstDesc,
173  stencilTable);
174  } else {
175  // Create an instance on demand (slow)
176  instance = Create(srcDesc, dstDesc,
179  deviceContext);
180  if (instance) {
181  bool r = instance->EvalStencils(srcBuffer, srcDesc,
182  dstBuffer, dstDesc,
183  stencilTable);
184  delete instance;
185  return r;
186  }
187  return false;
188  }
189  }
190 
233  template <typename SRC_BUFFER, typename DST_BUFFER,
234  typename STENCIL_TABLE, typename DEVICE_CONTEXT>
235  static bool EvalStencils(
236  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
237  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
238  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
239  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
240  STENCIL_TABLE const *stencilTable,
241  CLEvaluator const *instance,
242  DEVICE_CONTEXT deviceContext) {
243 
244  if (instance) {
245  return instance->EvalStencils(srcBuffer, srcDesc,
246  dstBuffer, dstDesc,
247  duBuffer, duDesc,
248  dvBuffer, dvDesc,
249  stencilTable);
250  } else {
251  // Create an instance on demand (slow)
252  instance = Create(srcDesc, dstDesc, duDesc, dvDesc,
253  deviceContext);
254  if (instance) {
255  bool r = instance->EvalStencils(srcBuffer, srcDesc,
256  dstBuffer, dstDesc,
257  duBuffer, duDesc,
258  dvBuffer, dvDesc,
259  stencilTable);
260  delete instance;
261  return r;
262  }
263  return false;
264  }
265  }
266 
270  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
272  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
273  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
274  STENCIL_TABLE const *stencilTable) const {
275  return EvalStencils(srcBuffer->BindCLBuffer(_clCommandQueue), srcDesc,
276  dstBuffer->BindCLBuffer(_clCommandQueue), dstDesc,
277  stencilTable->GetSizesBuffer(),
278  stencilTable->GetOffsetsBuffer(),
279  stencilTable->GetIndicesBuffer(),
280  stencilTable->GetWeightsBuffer(),
281  0,
282  stencilTable->GetNumStencils());
283  }
284 
288  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
290  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
291  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
292  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
293  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
294  STENCIL_TABLE const *stencilTable) const {
295  return EvalStencils(srcBuffer->BindCLBuffer(_clCommandQueue), srcDesc,
296  dstBuffer->BindCLBuffer(_clCommandQueue), dstDesc,
297  duBuffer->BindCLBuffer(_clCommandQueue), duDesc,
298  dvBuffer->BindCLBuffer(_clCommandQueue), dvDesc,
299  stencilTable->GetSizesBuffer(),
300  stencilTable->GetOffsetsBuffer(),
301  stencilTable->GetIndicesBuffer(),
302  stencilTable->GetWeightsBuffer(),
303  stencilTable->GetDuWeightsBuffer(),
304  stencilTable->GetDvWeightsBuffer(),
305  0,
306  stencilTable->GetNumStencils());
307  }
308 
311  bool EvalStencils(cl_mem src, BufferDescriptor const &srcDesc,
312  cl_mem dst, BufferDescriptor const &dstDesc,
313  cl_mem sizes,
314  cl_mem offsets,
315  cl_mem indices,
316  cl_mem weights,
317  int start,
318  int end) const;
319 
322  bool EvalStencils(cl_mem src, BufferDescriptor const &srcDesc,
323  cl_mem dst, BufferDescriptor const &dstDesc,
324  cl_mem du, BufferDescriptor const &duDesc,
325  cl_mem dv, BufferDescriptor const &dvDesc,
326  cl_mem sizes,
327  cl_mem offsets,
328  cl_mem indices,
329  cl_mem weights,
330  cl_mem duWeights,
331  cl_mem dvWeights,
332  int start,
333  int end) const;
334 
376  template <typename SRC_BUFFER, typename DST_BUFFER,
377  typename PATCHCOORD_BUFFER, typename PATCH_TABLE,
378  typename DEVICE_CONTEXT>
379  static bool EvalPatches(
380  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
381  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
382  int numPatchCoords,
383  PATCHCOORD_BUFFER *patchCoords,
384  PATCH_TABLE *patchTable,
385  CLEvaluator const *instance,
386  DEVICE_CONTEXT deviceContext) {
387 
388  if (instance) {
389  return instance->EvalPatches(srcBuffer, srcDesc,
390  dstBuffer, dstDesc,
391  numPatchCoords, patchCoords,
392  patchTable);
393  } else {
394  // Create an instance on demand (slow)
395  (void)deviceContext; // unused
396  instance = Create(srcDesc, dstDesc,
399  deviceContext);
400  if (instance) {
401  bool r = instance->EvalPatches(srcBuffer, srcDesc,
402  dstBuffer, dstDesc,
403  numPatchCoords, patchCoords,
404  patchTable);
405  delete instance;
406  return r;
407  }
408  return false;
409  }
410  }
411 
455  template <typename SRC_BUFFER, typename DST_BUFFER,
456  typename PATCHCOORD_BUFFER, typename PATCH_TABLE,
457  typename DEVICE_CONTEXT>
458  static bool EvalPatches(
459  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
460  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
461  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
462  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
463  int numPatchCoords,
464  PATCHCOORD_BUFFER *patchCoords,
465  PATCH_TABLE *patchTable,
466  CLEvaluator const *instance,
467  DEVICE_CONTEXT deviceContext) {
468 
469  if (instance) {
470  return instance->EvalPatches(srcBuffer, srcDesc,
471  dstBuffer, dstDesc,
472  duBuffer, duDesc,
473  dvBuffer, dvDesc,
474  numPatchCoords, patchCoords,
475  patchTable);
476  } else {
477  // Create an instance on demand (slow)
478  (void)deviceContext; // unused
479  instance = Create(srcDesc, dstDesc, duDesc, dvDesc, deviceContext);
480  if (instance) {
481  bool r = instance->EvalPatches(srcBuffer, srcDesc,
482  dstBuffer, dstDesc,
483  duBuffer, duDesc,
484  dvBuffer, dvDesc,
485  numPatchCoords, patchCoords,
486  patchTable);
487  delete instance;
488  return r;
489  }
490  return false;
491  }
492  }
493 
518  template <typename SRC_BUFFER, typename DST_BUFFER,
519  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
521  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
522  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
523  int numPatchCoords,
524  PATCHCOORD_BUFFER *patchCoords,
525  PATCH_TABLE *patchTable) const {
526 
527  return EvalPatches(srcBuffer->BindCLBuffer(_clCommandQueue), srcDesc,
528  dstBuffer->BindCLBuffer(_clCommandQueue), dstDesc,
529  0, BufferDescriptor(),
530  0, BufferDescriptor(),
531  numPatchCoords,
532  patchCoords->BindCLBuffer(_clCommandQueue),
533  patchTable->GetPatchArrayBuffer(),
534  patchTable->GetPatchIndexBuffer(),
535  patchTable->GetPatchParamBuffer());
536  }
537 
572  template <typename SRC_BUFFER, typename DST_BUFFER,
573  typename PATCHCOORD_BUFFER, typename PATCH_TABLE>
575  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
576  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
577  DST_BUFFER *duBuffer, BufferDescriptor const &duDesc,
578  DST_BUFFER *dvBuffer, BufferDescriptor const &dvDesc,
579  int numPatchCoords,
580  PATCHCOORD_BUFFER *patchCoords,
581  PATCH_TABLE *patchTable) const {
582 
583  return EvalPatches(srcBuffer->BindCLBuffer(_clCommandQueue), srcDesc,
584  dstBuffer->BindCLBuffer(_clCommandQueue), dstDesc,
585  duBuffer->BindCLBuffer(_clCommandQueue), duDesc,
586  dvBuffer->BindCLBuffer(_clCommandQueue), dvDesc,
587  numPatchCoords,
588  patchCoords->BindCLBuffer(_clCommandQueue),
589  patchTable->GetPatchArrayBuffer(),
590  patchTable->GetPatchIndexBuffer(),
591  patchTable->GetPatchParamBuffer());
592  }
593 
594  bool EvalPatches(cl_mem src, BufferDescriptor const &srcDesc,
595  cl_mem dst, BufferDescriptor const &dstDesc,
596  cl_mem du, BufferDescriptor const &duDesc,
597  cl_mem dv, BufferDescriptor const &dvDesc,
598  int numPatchCoords,
599  cl_mem patchCoordsBuffer,
600  cl_mem patchArrayBuffer,
601  cl_mem patchIndexBuffer,
602  cl_mem patchParamsBuffer) const;
603 
609 
612  bool Compile(BufferDescriptor const &srcDesc,
613  BufferDescriptor const &dstDesc,
614  BufferDescriptor const &duDesc,
615  BufferDescriptor const &dvDesc);
616 
618  template <typename DEVICE_CONTEXT>
619  static void Synchronize(DEVICE_CONTEXT deviceContext) {
620  Synchronize(deviceContext->GetCommandQueue());
621  }
622 
623  static void Synchronize(cl_command_queue queue);
624 
625 private:
626  cl_context _clContext;
627  cl_command_queue _clCommandQueue;
628  cl_program _program;
629  cl_kernel _stencilKernel;
630  cl_kernel _stencilDerivKernel;
631  cl_kernel _patchKernel;
632 };
633 
634 
635 } // end namespace Osd
636 
637 } // end namespace OPENSUBDIV_VERSION
638 using namespace OPENSUBDIV_VERSION;
639 
640 } // end namespace OpenSubdiv
641 
642 
643 #endif // OPENSUBDIV_OPENSUBDIV3_OSD_CL_EVALUATOR_H
static CLStencilTable * Create(Far::LimitStencilTable const *limitStencilTable, DEVICE_CONTEXT context)
Definition: clEvaluator.h:59
static CLEvaluator * Create(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc, DEVICE_CONTEXT deviceContext)
Generic creator template.
Definition: clEvaluator.h:103
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, CLEvaluator const *instance, DEVICE_CONTEXT deviceContext)
Generic static compute function. This function has a same signature as other device kernels have so t...
Definition: clEvaluator.h:235
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.
bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable) const
Definition: clEvaluator.h:271
static bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable, CLEvaluator const *instance, DEVICE_CONTEXT deviceContext)
Generic static compute function. This function has a same signature as other device kernels have so t...
Definition: clEvaluator.h:163
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, CLEvaluator const *instance, DEVICE_CONTEXT deviceContext)
Generic limit eval function. This function has a same signature as other device kernels have so that ...
Definition: clEvaluator.h:458
static CLStencilTable * Create(Far::StencilTable const *stencilTable, DEVICE_CONTEXT context)
Definition: clEvaluator.h:53
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...
Definition: clEvaluator.h:574
static void Synchronize(DEVICE_CONTEXT deviceContext)
Wait the OpenCL kernels finish.
Definition: clEvaluator.h:619
static CLEvaluator * Create(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc, cl_context clContext, cl_command_queue clCommandQueue)
Definition: clEvaluator.h:113
CLStencilTable(Far::StencilTable const *stencilTable, cl_context clContext)
CLEvaluator(cl_context context, cl_command_queue queue)
Constructor.
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
Definition: clEvaluator.h:289
static bool EvalPatches(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, int numPatchCoords, PATCHCOORD_BUFFER *patchCoords, PATCH_TABLE *patchTable, CLEvaluator const *instance, DEVICE_CONTEXT deviceContext)
Generic limit eval function. This function has a same signature as other device kernels have so that ...
Definition: clEvaluator.h:379
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 ...
Definition: clEvaluator.h:520
bool Compile(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc)
Table of limit subdivision stencils.
Definition: stencilTable.h:285