All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clComputeController.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_CL_COMPUTE_CONTROLLER_H
26 #define OSD_CL_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/clComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 #include "../osd/opencl.h"
34 
35 #include <vector>
36 
37 namespace OpenSubdiv {
38 namespace OPENSUBDIV_VERSION {
39 
40 namespace Osd {
41 
42 class CLKernelBundle;
43 
55 public:
57 
64  CLComputeController(cl_context clContext, cl_command_queue queue);
65 
68 
88  template<class VERTEX_BUFFER, class VARYING_BUFFER>
89  void Compute( CLComputeContext const * context,
90  Far::KernelBatchVector const & batches,
91  VERTEX_BUFFER * vertexBuffer,
92  VARYING_BUFFER * varyingBuffer,
93  VertexBufferDescriptor const * vertexDesc=NULL,
94  VertexBufferDescriptor const * varyingDesc=NULL ){
95 
96  if (batches.empty()) return;
97 
98  bind(vertexBuffer, varyingBuffer, vertexDesc, varyingDesc);
99 
100  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
101 
102  unbind();
103  }
104 
114  template<class VERTEX_BUFFER>
115  void Compute(CLComputeContext const * context,
116  Far::KernelBatchVector const & batches,
117  VERTEX_BUFFER *vertexBuffer) {
118 
119  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
120  }
121 
123  void Synchronize();
124 
126  cl_context GetContext() const { return _clContext; }
127 
129  cl_command_queue GetCommandQueue() const { return _clQueue; }
130 
131 protected:
132 
134 
135  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
136  ComputeContext const *context);
137 
138  template<class VERTEX_BUFFER, class VARYING_BUFFER>
139  void bind( VERTEX_BUFFER * vertexBuffer,
140  VARYING_BUFFER * varyingBuffer,
141  VertexBufferDescriptor const * vertexDesc,
142  VertexBufferDescriptor const * varyingDesc ) {
143 
144  // if the vertex buffer descriptor is specified, use it.
145  // otherwise, assumes the data is tightly packed in the vertex buffer.
146  if (vertexDesc) {
147  _currentBindState.vertexDesc = *vertexDesc;
148  } else {
149  int numElements = vertexBuffer ? vertexBuffer->GetNumElements() : 0;
150  _currentBindState.vertexDesc =
151  VertexBufferDescriptor(0, numElements, numElements);
152  }
153 
154  if (varyingDesc) {
155  _currentBindState.varyingDesc = *varyingDesc;
156  } else {
157  int numElements = varyingBuffer ? varyingBuffer->GetNumElements() : 0;
158  _currentBindState.varyingDesc =
159  VertexBufferDescriptor(0, numElements, numElements);
160  }
161 
162  _currentBindState.vertexBuffer = vertexBuffer ?
163  vertexBuffer->BindCLBuffer(_clQueue) : 0;
164  _currentBindState.varyingBuffer = varyingBuffer ?
165  varyingBuffer->BindCLBuffer(_clQueue) : 0;
166  }
167 
168  void unbind() {
169  _currentBindState.Reset();
170  }
171 
172 
173 private:
174 
175  class KernelBundle;
176 
177  // Bind state is a transitional state during refinement.
178  // It doesn't take an ownership of the vertex buffers.
179  struct BindState {
180 
181  BindState() : vertexBuffer(0), varyingBuffer(0) { }
182 
183  void Reset() {
184  vertexBuffer = varyingBuffer = NULL;
185  vertexDesc.Reset();
186  varyingDesc.Reset();
187  }
188 
189  cl_mem vertexBuffer,
190  varyingBuffer;
191 
192  VertexBufferDescriptor vertexDesc,
193  varyingDesc;
194  };
195 
196  BindState _currentBindState;
197 
198  KernelBundle const * getKernel(VertexBufferDescriptor const &desc);
199 
200  typedef std::vector<KernelBundle *> KernelRegistry;
201 
202  KernelRegistry _kernelRegistry;
203 
204  cl_context _clContext;
205  cl_command_queue _clQueue;
206 };
207 
208 } // end namespace Osd
209 
210 } // end namespace OPENSUBDIV_VERSION
211 using namespace OPENSUBDIV_VERSION;
212 
213 } // end namespace OpenSubdiv
214 
215 #endif // OSD_COMPUTE_CONTROLLER_H
216 
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
void ApplyStencilTableKernel(Far::KernelBatch const &batch, ComputeContext const *context)
void bind(VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc)
Describes vertex elements in interleaved data buffers.
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
void Compute(CLComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
void Synchronize()
Waits until all running subdivision kernels finish.
cl_context GetContext() const
Returns CL context.
static void Apply(CONTROLLER *controller, CONTEXT *context, KernelBatchVector const &batches, int maxlevel)
Launches the processing of a vector of kernel batches this is a convenient API for controllers which ...
Compute controller for launching OpenCL Compute subdivision kernels.
void Compute(CLComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)
cl_command_queue GetCommandQueue() const
Returns CL command queue.
Subdivision refinement encapsulation layer.
CLComputeController(cl_context clContext, cl_command_queue queue)