All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cudaComputeController.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_CUDA_COMPUTE_CONTROLLER_H
26 #define OSD_CUDA_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/cudaComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 
34 namespace OpenSubdiv {
35 namespace OPENSUBDIV_VERSION {
36 
37 namespace Osd {
38 
50 public:
52 
55 
58 
78  template<class VERTEX_BUFFER, class VARYING_BUFFER>
79  void Compute( CudaComputeContext const * context,
80  Far::KernelBatchVector const & batches,
81  VERTEX_BUFFER * vertexBuffer,
82  VARYING_BUFFER * varyingBuffer,
83  VertexBufferDescriptor const * vertexDesc=NULL,
84  VertexBufferDescriptor const * varyingDesc=NULL ){
85 
86  if (batches.empty()) return;
87 
88  bind(vertexBuffer, varyingBuffer, vertexDesc, varyingDesc);
89 
90  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
91 
92  unbind();
93  }
94 
104  template<class VERTEX_BUFFER>
105  void Compute(CudaComputeContext const * context,
106  Far::KernelBatchVector const & batches,
107  VERTEX_BUFFER *vertexBuffer) {
108 
109  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
110  }
111 
113  void Synchronize();
114 
115 protected:
116 
118 
119  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
120  ComputeContext const *context) const;
121 
122  template<class VERTEX_BUFFER, class VARYING_BUFFER>
123  void bind( VERTEX_BUFFER * vertexBuffer,
124  VARYING_BUFFER * varyingBuffer,
125  VertexBufferDescriptor const * vertexDesc,
126  VertexBufferDescriptor const * varyingDesc ) {
127 
128  // if the vertex buffer descriptor is specified, use it.
129  // otherwise, assumes the data is tightly packed in the vertex buffer.
130  if (vertexDesc) {
131  _currentBindState.vertexDesc = *vertexDesc;
132  } else {
133  int numElements = vertexBuffer ? vertexBuffer->GetNumElements() : 0;
134  _currentBindState.vertexDesc =
135  VertexBufferDescriptor(0, numElements, numElements);
136  }
137 
138  if (varyingDesc) {
139  _currentBindState.varyingDesc = *varyingDesc;
140  } else {
141  int numElements = varyingBuffer ? varyingBuffer->GetNumElements() : 0;
142  _currentBindState.varyingDesc =
143  VertexBufferDescriptor(0, numElements, numElements);
144  }
145 
146  _currentBindState.vertexBuffer = vertexBuffer ?
147  static_cast<float*>(vertexBuffer->BindCudaBuffer()) : 0;
148  _currentBindState.varyingBuffer = varyingBuffer ?
149  static_cast<float*>(varyingBuffer->BindCudaBuffer()) : 0;
150  }
151 
153  void unbind() {
154  _currentBindState.Reset();
155  }
156 
157 private:
158 
159  // Bind state is a transitional state during refinement.
160  // It doesn't take an ownership of the vertex buffers.
161  struct BindState {
162 
163  BindState() : vertexBuffer(NULL), varyingBuffer(NULL) {}
164 
165  void Reset() {
166  vertexBuffer = varyingBuffer = NULL;
167  vertexDesc.Reset();
168  varyingDesc.Reset();
169  }
170 
171  float *GetVertexBufferAtOffset() const {
172  return vertexBuffer ? vertexBuffer + vertexDesc.offset : 0;
173  }
174 
175  float *GetVaryingBufferAtOffset() const {
176  return varyingBuffer ? varyingBuffer + varyingDesc.offset : 0;
177  }
178 
179  float * vertexBuffer, // cuda buffers
180  * varyingBuffer;
181 
182  VertexBufferDescriptor vertexDesc,
183  varyingDesc;
184  };
185 
186  BindState _currentBindState;
187 };
188 
189 } // end namespace Osd
190 
191 } // end namespace OPENSUBDIV_VERSION
192 using namespace OPENSUBDIV_VERSION;
193 
194 } // end namespace OpenSubdiv
195 
196 #endif // OSD_CUDA_COMPUTE_CONTROLLER_H
197 
Compute controller for launching CUDA subdivision kernels.
A GP Compute Kernel descriptor.
Definition: kernelBatch.h:44
void bind(VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc, VertexBufferDescriptor const *varyingDesc)
void Compute(CudaComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
Describes vertex elements in interleaved data buffers.
void unbind()
Unbinds any previously bound vertex and varying data buffers.
std::vector< KernelBatch > KernelBatchVector
Definition: kernelBatch.h:73
void ApplyStencilTableKernel(Far::KernelBatch const &batch, ComputeContext const *context) const
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(CudaComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)
Subdivision refinement encapsulation layer.
void Synchronize()
Waits until all running subdivision kernels finish.