All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glslTransformFeedbackComputeController.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_TRANSFORM_FEEDBACK_COMPUTE_CONTROLLER_H
26 #define OSD_GLSL_TRANSFORM_FEEDBACK_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/glslTransformFeedbackComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 
34 #include <vector>
35 
36 namespace OpenSubdiv {
37 namespace OPENSUBDIV_VERSION {
38 
39 namespace Osd {
40 
41 class GLSLTransformFeedbackKernelBundle;
42 
55 public:
57 
60 
63 
64 
85  template<class VERTEX_BUFFER, class VARYING_BUFFER>
87  Far::KernelBatchVector const & batches,
88  VERTEX_BUFFER * vertexBuffer,
89  VARYING_BUFFER * varyingBuffer,
90  VertexBufferDescriptor const * vertexDesc=NULL,
91  VertexBufferDescriptor const * varyingDesc=NULL ){
92 
93  if (batches.empty()) return;
94 
95  if (vertexBuffer) {
96 
97  bind(vertexBuffer, vertexDesc, _vertexTexture);
98 
99  bindContextStencilTables(context, false);
100 
101  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
102  }
103 
104  if (varyingBuffer) {
105 
106  bind(varyingBuffer, varyingDesc, _varyingTexture);
107 
108  bindContextStencilTables(context, true);
109 
110  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
111  }
112  unbind();
113  }
114 
125  template<class VERTEX_BUFFER>
127  Far::KernelBatchVector const & batches,
128  VERTEX_BUFFER *vertexBuffer) {
129 
130  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
131  }
132 
134  void Synchronize();
135 
136 protected:
137 
139 
140  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
141  ComputeContext const *context) const;
142 
143  template<class BUFFER>
144  void bind( BUFFER * buffer, VertexBufferDescriptor const * desc,
145  GLuint feedbackTexture ) {
146 
147  assert(buffer);
148 
149  // if the vertex buffer descriptor is specified, use it
150  // otherwise, assumes the data is tightly packed in the vertex buffer.
151  if (desc) {
152  _currentBindState.desc = *desc;
153  } else {
154  int numElements = buffer ? buffer->GetNumElements() : 0;
155  _currentBindState.desc =
156  VertexBufferDescriptor(0, numElements, numElements);
157  }
158 
159  _currentBindState.buffer = buffer->BindVBO();
160 
161  _currentBindState.kernelBundle = getKernel(_currentBindState.desc);
162 
163  bindBufferAndProgram(feedbackTexture);
164  }
165 
166  // Unbinds any previously bound vertex and varying data buffers.
167  void unbind() {
168  _currentBindState.Reset();
169  unbindResources();
170  }
171 
172  // binds the primvar data buffer and compute program
173  void bindBufferAndProgram(GLuint & texture);
174 
175  // binds the stencil tables for 'vertex' interpolation
176  void bindContextStencilTables(ComputeContext const *context, bool varying=false);
177 
178  // unbinds the primvar data buffer and compute program
179  void unbindResources();
180 
181 private:
182 
183  class KernelBundle;
184 
185  // Bind state is a transitional state during refinement.
186  // It doesn't take an ownership of the vertex buffers.
187  struct BindState {
188 
189  BindState() : buffer(0), kernelBundle(0) { }
190 
191  void Reset() {
192  buffer = 0;
193  desc.Reset();
194  kernelBundle = 0;
195  }
196 
197  GLuint buffer;
198 
199  VertexBufferDescriptor desc;
200 
201  KernelBundle const * kernelBundle;
202  };
203 
204  BindState _currentBindState;
205 
206  typedef std::vector<KernelBundle *> KernelRegistry;
207 
208  KernelBundle const * getKernel(VertexBufferDescriptor const &desc);
209 
210  KernelRegistry _kernelRegistry;
211 
212  GLuint _vertexTexture,
213  _varyingTexture,
214  _vao;
215 };
216 
217 } // end namespace Osd
218 
219 } // end namespace OPENSUBDIV_VERSION
220 using namespace OPENSUBDIV_VERSION;
221 
222 } // end namespace OpenSubdiv
223 
224 #endif // OSD_GLSL_TRANSFORM_FEEDBACK_COMPUTE_CONTROLLER_H
225 
void bind(BUFFER *buffer, VertexBufferDescriptor const *desc, GLuint feedbackTexture)
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
Describes vertex elements in interleaved data buffers.
Compute controller for launching GLSLTransformFeedback transform feedback subdivision kernels...
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
void Compute(GLSLTransformFeedbackComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
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 Synchronize()
Waits until all running subdivision kernels finish.
void bindContextStencilTables(ComputeContext const *context, bool varying=false)
void ApplyStencilTableKernel(Far::KernelBatch const &batch, ComputeContext const *context) const
Subdivision refinement encapsulation layer.
void Compute(GLSLTransformFeedbackComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)