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