All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
d3d11ComputeController.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_D3D11_COMPUTE_CONTROLLER_H
26 #define OSD_D3D11_COMPUTE_CONTROLLER_H
27 
28 #include "../version.h"
29 
30 #include "../far/kernelBatchDispatcher.h"
31 #include "../osd/d3d11ComputeContext.h"
32 #include "../osd/vertexDescriptor.h"
33 
34 #include <vector>
35 
36 struct ID3D11DeviceContext;
37 struct ID3D11Query;
38 struct ID3D11UnorderedAccessView;
39 
40 namespace OpenSubdiv {
41 namespace OPENSUBDIV_VERSION {
42 
43 namespace Osd {
44 
56 public:
58 
63  D3D11ComputeController(ID3D11DeviceContext *deviceContext);
64 
67 
87  template<class VERTEX_BUFFER, class VARYING_BUFFER>
88  void Compute( D3D11ComputeContext const * context,
89  Far::KernelBatchVector const & batches,
90  VERTEX_BUFFER * vertexBuffer,
91  VARYING_BUFFER * varyingBuffer,
92  VertexBufferDescriptor const * vertexDesc=NULL,
93  VertexBufferDescriptor const * varyingDesc=NULL ){
94 
95  if (batches.empty()) return;
96 
97  if (vertexBuffer) {
98  bind(vertexBuffer, vertexDesc);
99 
100  context->BindVertexStencilTables(_deviceContext);
101 
102  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
103  }
104 
105  if (varyingBuffer) {
106  bind(varyingBuffer, varyingDesc);
107 
108  context->BindVaryingStencilTables(_deviceContext);
109 
110  Far::KernelBatchDispatcher::Apply(this, context, batches, /*maxlevel*/ -1);
111  }
112 
113  context->UnbindStencilTables(_deviceContext);
114 
115  unbind();
116  }
117 
127  template<class VERTEX_BUFFER>
128  void Compute(D3D11ComputeContext const * context,
129  Far::KernelBatchVector const & batches,
130  VERTEX_BUFFER *vertexBuffer) {
131 
132  Compute<VERTEX_BUFFER>(context, batches, vertexBuffer, (VERTEX_BUFFER*)0);
133  }
134 
136  void Synchronize();
137 
138 protected:
139 
141 
142  void ApplyStencilTableKernel(Far::KernelBatch const &batch,
143  ComputeContext const *context) const;
144 
145  template<class BUFFER>
146  void bind( BUFFER * buffer,
147  VertexBufferDescriptor const * desc ) {
148 
149  assert(buffer);
150 
151  // if the vertex buffer descriptor is specified, use it
152  // otherwise, assumes the data is tightly packed in the vertex buffer.
153  if (desc) {
154  _currentBindState.desc = *desc;
155  } else {
156  int numElements = buffer ? buffer->GetNumElements() : 0;
157  _currentBindState.desc =
158  VertexBufferDescriptor(0, numElements, numElements);
159  }
160 
161  _currentBindState.buffer = buffer->BindD3D11UAV(_deviceContext);
162 
163  _currentBindState.kernelBundle = getKernel(_currentBindState.desc);
164 
165  bindBuffer();
166  }
167 
168 
169  // Unbinds any previously bound vertex and varying data buffers.
170  void unbind() {
171  _currentBindState.Reset();
172  unbindBuffer();
173  }
174 
175  // binds the primvar data buffer
176  void bindBuffer();
177 
178  // unbinds the primvar data buffer
179  void unbindBuffer();
180 
181 
182 private:
183 
184  ID3D11DeviceContext *_deviceContext;
185  ID3D11Query *_query;
186 
187  class KernelBundle;
188 
189  // Bind state is a transitional state during refinement.
190  // It doesn't take an ownership of the vertex buffers.
191  struct BindState {
192 
193  BindState() : buffer(0), kernelBundle(0) { }
194 
195  void Reset() {
196  buffer = 0;
197  desc.Reset();
198  kernelBundle = 0;
199  }
200 
201  ID3D11UnorderedAccessView * buffer;
202 
203  VertexBufferDescriptor desc;
204 
205  KernelBundle const * kernelBundle;
206  };
207 
208  BindState _currentBindState;
209 
210  typedef std::vector<KernelBundle *> KernelRegistry;
211 
212  KernelBundle const * getKernel(VertexBufferDescriptor const &desc);
213 
214  KernelRegistry _kernelRegistry;
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_D3D11_COMPUTE_CONTROLLER_H
225 
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 BindVaryingStencilTables(ID3D11DeviceContext *deviceContext) const
void Compute(D3D11ComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer, VARYING_BUFFER *varyingBuffer, VertexBufferDescriptor const *vertexDesc=NULL, VertexBufferDescriptor const *varyingDesc=NULL)
void bind(BUFFER *buffer, VertexBufferDescriptor const *desc)
D3D11ComputeController(ID3D11DeviceContext *deviceContext)
void Compute(D3D11ComputeContext const *context, Far::KernelBatchVector const &batches, VERTEX_BUFFER *vertexBuffer)
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 ...
Compute controller for launching D3D11 Compute subdivision kernels.
void BindVertexStencilTables(ID3D11DeviceContext *deviceContext) const
void UnbindStencilTables(ID3D11DeviceContext *deviceContext) const
void Synchronize()
Waits until all running subdivision kernels finish.
Subdivision refinement encapsulation layer.