All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tbbComputeController.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_TBB_COMPUTE_CONTROLLER_H
26 #define OSD_TBB_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 namespace OpenSubdiv {
35 namespace OPENSUBDIV_VERSION {
36 
37 namespace Osd {
38 
50 public:
52 
58  explicit TbbComputeController(int numThreads=-1);
59 
60 
80  template<class VERTEX_BUFFER, class VARYING_BUFFER>
81  void Compute( CpuComputeContext const * context,
82  Far::KernelBatchVector const & batches,
83  VERTEX_BUFFER * vertexBuffer,
84  VARYING_BUFFER * varyingBuffer,
85  VertexBufferDescriptor const * vertexDesc=NULL,
86  VertexBufferDescriptor const * varyingDesc=NULL ){
87 
88  if (batches.empty()) return;
89 
90  bind(vertexBuffer, varyingBuffer, vertexDesc, varyingDesc);
91 
92  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
93 
94  unbind();
95  }
96 
106  template<class VERTEX_BUFFER>
107  void Compute(CpuComputeContext const * context,
108  Far::KernelBatchVector const & batches,
109  VERTEX_BUFFER *vertexBuffer) {
110 
111  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
112  }
113 
115  void Synchronize();
116 
117 protected:
118 
120 
121  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
122  ComputeContext const *context) const;
123 
124  template<class VERTEX_BUFFER, class VARYING_BUFFER>
125  void bind( VERTEX_BUFFER * vertexBuffer,
126  VARYING_BUFFER * varyingBuffer,
127  VertexBufferDescriptor const * vertexDesc,
128  VertexBufferDescriptor const * varyingDesc ) {
129 
130  // if the vertex buffer descriptor is specified, use it.
131  // otherwise, assumes the data is tightly packed in the vertex buffer.
132  if (vertexDesc) {
133  _currentBindState.vertexDesc = *vertexDesc;
134  } else {
135  int numElements = vertexBuffer ? vertexBuffer->GetNumElements() : 0;
136  _currentBindState.vertexDesc =
137  VertexBufferDescriptor(0, numElements, numElements);
138  }
139 
140  if (varyingDesc) {
141  _currentBindState.varyingDesc = *varyingDesc;
142  } else {
143  int numElements = varyingBuffer ? varyingBuffer->GetNumElements() : 0;
144  _currentBindState.varyingDesc =
145  VertexBufferDescriptor(0, numElements, numElements);
146  }
147 
148  _currentBindState.vertexBuffer = vertexBuffer ?
149  vertexBuffer->BindCpuBuffer(): 0;
150 
151  _currentBindState.varyingBuffer = varyingBuffer ?
152  varyingBuffer->BindCpuBuffer() : 0;
153  }
154 
155  void unbind() {
156  _currentBindState.Reset();
157  }
158 
159 private:
160 
161  // Bind state is a transitional state during refinement.
162  // It doesn't take an ownership of the vertex buffers.
163  struct BindState {
164 
165  BindState() : vertexBuffer(0), varyingBuffer(0) { }
166 
167  void Reset() {
168  vertexBuffer = varyingBuffer = 0;
169  vertexDesc.Reset();
170  varyingDesc.Reset();
171  }
172 
173  float * vertexBuffer,
174  * varyingBuffer;
175 
176  VertexBufferDescriptor vertexDesc,
177  varyingDesc;
178  };
179 
180  BindState _currentBindState;
181  int _numThreads;
182 };
183 
184 } // end namespace Osd
185 
186 } // end namespace OPENSUBDIV_VERSION
187 using namespace OPENSUBDIV_VERSION;
188 
189 } // end namespace OpenSubdiv
190 
191 #endif // OSD_TBB_COMPUTE_CONTROLLER_H
192 
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
Describes vertex elements in interleaved data buffers.
void Compute(CpuComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
void bind(VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc)
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
Compute controller for launching TBB subdivision kernels.
void Synchronize()
Waits until all running subdivision kernels finish.
Subdivision refinement encapsulation layer.
void Compute(CpuComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)