All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ompComputeController.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_OMP_COMPUTE_CONTROLLER_H
26 #define OSD_OMP_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/cpuComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 
34 #ifdef OPENSUBDIV_HAS_OPENMP
35  #include <omp.h>
36 #endif
37 
38 namespace OpenSubdiv {
39 namespace OPENSUBDIV_VERSION {
40 
41 namespace Osd {
42 
54 public:
56 
62  explicit OmpComputeController(int numThreads=-1);
63 
64 
84  template<class VERTEX_BUFFER, class VARYING_BUFFER>
85  void Compute( CpuComputeContext const * context,
86  Far::KernelBatchVector const & batches,
87  VERTEX_BUFFER * vertexBuffer,
88  VARYING_BUFFER * varyingBuffer,
89  VertexBufferDescriptor const * vertexDesc=NULL,
90  VertexBufferDescriptor const * varyingDesc=NULL ){
91 
92  if (batches.empty()) return;
93 
94  omp_set_num_threads(_numThreads);
95 
96  bind(vertexBuffer, varyingBuffer, vertexDesc, varyingDesc);
97 
98  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
99 
100  unbind();
101  }
102 
112  template<class VERTEX_BUFFER>
113  void Compute(CpuComputeContext const * context,
114  Far::KernelBatchVector const & batches,
115  VERTEX_BUFFER *vertexBuffer) {
116 
117  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
118  }
119 
121  void Synchronize();
122 
123 protected:
124 
126 
127  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
128  ComputeContext const *context) const;
129 
130  template<class VERTEX_BUFFER, class VARYING_BUFFER>
131  void bind( VERTEX_BUFFER * vertexBuffer,
132  VARYING_BUFFER * varyingBuffer,
133  VertexBufferDescriptor const * vertexDesc,
134  VertexBufferDescriptor const * varyingDesc ) {
135 
136  // if the vertex buffer descriptor is specified, use it.
137  // otherwise, assumes the data is tightly packed in the vertex buffer.
138  if (vertexDesc) {
139  _currentBindState.vertexDesc = *vertexDesc;
140  } else {
141  int numElements = vertexBuffer ? vertexBuffer->GetNumElements() : 0;
142  _currentBindState.vertexDesc =
143  VertexBufferDescriptor(0, numElements, numElements);
144  }
145 
146  if (varyingDesc) {
147  _currentBindState.varyingDesc = *varyingDesc;
148  } else {
149  int numElements = varyingBuffer ? varyingBuffer->GetNumElements() : 0;
150  _currentBindState.varyingDesc =
151  VertexBufferDescriptor(0, numElements, numElements);
152  }
153 
154  _currentBindState.vertexBuffer = vertexBuffer ?
155  vertexBuffer->BindCpuBuffer() : 0;
156 
157  _currentBindState.varyingBuffer = varyingBuffer ?
158  varyingBuffer->BindCpuBuffer() : 0;
159  }
160 
161 
162  void unbind() {
163  _currentBindState.Reset();
164  }
165 
166 private:
167 
168  // Bind state is a transitional state during refinement.
169  // It doesn't take an ownership of the vertex buffers.
170  struct BindState {
171 
172  BindState() : vertexBuffer(0), varyingBuffer(0) { }
173 
174  void Reset() {
175  vertexBuffer = varyingBuffer = 0;
176  vertexDesc.Reset();
177  varyingDesc.Reset();
178  }
179 
180  float * vertexBuffer,
181  * varyingBuffer;
182 
183  VertexBufferDescriptor vertexDesc,
184  varyingDesc;
185  };
186 
187  BindState _currentBindState;
188  int _numThreads;
189 };
190 
191 } // end namespace Osd
192 
193 } // end namespace OPENSUBDIV_VERSION
194 using namespace OPENSUBDIV_VERSION;
195 
196 } // end namespace OpenSubdiv
197 
198 #endif // OSD_OMP_COMPUTE_CONTROLLER_H
199 
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
void Compute(CpuComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)
Describes vertex elements in interleaved data buffers.
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
void Compute(CpuComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
void bind(VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc)
void Synchronize()
Waits until all running subdivision kernels finish.
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 ...
void ApplyStencilTableKernel(Far::KernelBatch const &batch, ComputeContext const *context) const
Subdivision refinement encapsulation layer.
Compute controller for launching OpenMP subdivision kernels.