All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glslComputeController.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_GLSL_COMPUTE_CONTROLLER_H
26 #define OSD_GLSL_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/glslComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 
34 #include <vector>
35 
36 namespace OpenSubdiv {
37 namespace OPENSUBDIV_VERSION {
38 
39 namespace Osd {
40 
52 public:
54 
57 
60 
80  template<class VERTEX_BUFFER, class VARYING_BUFFER>
81  void Compute( GLSLComputeContext 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  if (vertexBuffer) {
91  bind(vertexBuffer, vertexDesc);
92 
93  context->BindVertexStencilTables();
94 
95  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
96  }
97 
98  if (varyingBuffer) {
99  bind(varyingBuffer, varyingDesc);
100 
101  context->BindVaryingStencilTables();
102 
103  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
104  }
105 
106  context->UnbindStencilTables();
107 
108  unbind();
109  }
110 
120  template<class VERTEX_BUFFER>
121  void Compute(GLSLComputeContext const * context,
122  Far::KernelBatchVector const & batches,
123  VERTEX_BUFFER *vertexBuffer) {
124 
125  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
126  }
127 
129  void Synchronize();
130 
131 protected:
132 
134 
135  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
136  ComputeContext const *context) const;
137 
138  template<class BUFFER>
139  void bind( BUFFER * buffer,
140  VertexBufferDescriptor const * desc ) {
141 
142  assert(buffer);
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 (desc) {
147  _currentBindState.desc = *desc;
148  } else {
149  int numElements = buffer ? buffer->GetNumElements() : 0;
150  _currentBindState.desc =
151  VertexBufferDescriptor(0, numElements, numElements);
152  }
153 
154  _currentBindState.buffer = buffer->BindVBO();
155 
156  _currentBindState.kernelBundle = getKernel(_currentBindState.desc);
157 
159  }
160 
161 
162  // Unbinds any previously bound vertex and varying data buffers.
163  void unbind() {
164  _currentBindState.Reset();
166  }
167 
168  // binds the primvar data buffer and compute program
169  void bindBufferAndProgram();
170 
171  // unbinds the primvar data buffer and compute program
172  void unbindBufferAndProgram();
173 
174 
175 private:
176 
177  class KernelBundle;
178 
179  // Bind state is a transitional state during refinement.
180  // It doesn't take an ownership of the vertex buffers.
181  struct BindState {
182 
183  BindState() : buffer(0), kernelBundle(0) { }
184 
185  void Reset() {
186  buffer = 0;
187  desc.Reset();
188  kernelBundle = 0;
189  }
190 
191  GLuint buffer;
192 
193  VertexBufferDescriptor desc;
194 
195  KernelBundle const * kernelBundle;
196  };
197 
198  BindState _currentBindState;
199 
200  typedef std::vector<KernelBundle *> KernelRegistry;
201 
202  KernelBundle const * getKernel(VertexBufferDescriptor const &desc);
203 
204  KernelRegistry _kernelRegistry;
205 };
206 
207 } // end namespace Osd
208 
209 } // end namespace OPENSUBDIV_VERSION
210 using namespace OPENSUBDIV_VERSION;
211 
212 } // end namespace OpenSubdiv
213 
214 #endif // OSD_GLSL_COMPUTE_CONTROLLER_H
215 
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
void Synchronize()
Waits until all running subdivision kernels finish.
Describes vertex elements in interleaved data buffers.
void ApplyStencilTableKernel(Far::KernelBatch const &batch, ComputeContext const *context) const
void BindVaryingStencilTables() const
Binds GL buffers containing stencils for &#39;varying&#39; interpolation.
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
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(GLSLComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)
void BindVertexStencilTables() const
Binds GL buffers containing stencils for &#39;vertex&#39; interpolation.
void bind(BUFFER *buffer, VertexBufferDescriptor const *desc)
Compute controller for launching GLSL Compute subdivision kernels.
void Compute(GLSLComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
Subdivision refinement encapsulation layer.
void UnbindStencilTables() const
Unbinds GL stencil buffers.