All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
d3d11ComputeEvaluator.h
Go to the documentation of this file.
1 //
2 // Copyright 2015 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 OPENSUBDIV3_OSD_D3D11_COMPUTE_EVALUATOR_H
26 #define OPENSUBDIV3_OSD_D3D11_COMPUTE_EVALUATOR_H
27 
28 #include "../version.h"
29 
30 struct ID3D11DeviceContext;
31 struct ID3D11Buffer;
32 struct ID3D11ComputeShader;
33 struct ID3D11ClassLinkage;
34 struct ID3D11ClassInstance;
35 struct ID3D11ShaderResourceView;
36 struct ID3D11UnorderedAccessView;
37 
38 #include "../osd/bufferDescriptor.h"
39 
40 namespace OpenSubdiv {
41 namespace OPENSUBDIV_VERSION {
42 
43 namespace Far {
44  class StencilTable;
45 }
46 
47 namespace Osd {
48 
57 public:
58  template <typename DEVICE_CONTEXT>
59  static D3D11StencilTable *Create(Far::StencilTable const *stencilTable,
60  DEVICE_CONTEXT context) {
61  return new D3D11StencilTable(stencilTable, context->GetDeviceContext());
62  }
63 
64  static D3D11StencilTable *Create(Far::StencilTable const *stencilTable,
65  ID3D11DeviceContext *deviceContext) {
66  return new D3D11StencilTable(stencilTable, deviceContext);
67  }
68 
69  D3D11StencilTable(Far::StencilTable const *stencilTable,
70  ID3D11DeviceContext *deviceContext);
71 
73 
74  // interfaces needed for D3D11ComputeEvaluator
75  ID3D11ShaderResourceView *GetSizesSRV() const { return _sizes; }
76  ID3D11ShaderResourceView *GetOffsetsSRV() const { return _offsets; }
77  ID3D11ShaderResourceView *GetIndicesSRV() const { return _indices; }
78  ID3D11ShaderResourceView *GetWeightsSRV() const { return _weights; }
79  int GetNumStencils() const { return _numStencils; }
80 
81 private:
82  ID3D11ShaderResourceView *_sizes;
83  ID3D11ShaderResourceView *_offsets;
84  ID3D11ShaderResourceView *_indices;
85  ID3D11ShaderResourceView *_weights;
86  ID3D11Buffer *_sizesBuffer;
87  ID3D11Buffer *_offsetsBuffer;
88  ID3D11Buffer *_indicesBuffer;
89  ID3D11Buffer *_weightsBuffer;
90 
91  int _numStencils;
92 };
93 
94 // ---------------------------------------------------------------------------
95 
97 public:
98  typedef bool Instantiatable;
99  static D3D11ComputeEvaluator * Create(BufferDescriptor const &srcDesc,
100  BufferDescriptor const &dstDesc,
101  BufferDescriptor const &duDesc,
102  BufferDescriptor const &dvDesc,
103  ID3D11DeviceContext *deviceContext);
104 
105  static D3D11ComputeEvaluator * Create(BufferDescriptor const &srcDesc,
106  BufferDescriptor const &dstDesc,
107  BufferDescriptor const &duDesc,
108  BufferDescriptor const &dvDesc,
109  BufferDescriptor const &duuDesc,
110  BufferDescriptor const &duvDesc,
111  BufferDescriptor const &dvvDesc,
112  ID3D11DeviceContext *deviceContext);
113 
116 
119 
147  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
148  static bool EvalStencils(
149  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
150  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
151  STENCIL_TABLE const *stencilTable,
152  D3D11ComputeEvaluator const *instance,
153  ID3D11DeviceContext * deviceContext) {
154  if (instance) {
155  return instance->EvalStencils(srcBuffer, srcDesc,
156  dstBuffer, dstDesc,
157  stencilTable,
158  deviceContext);
159  } else {
160  // Create an instance on demand (slow)
161  (void)deviceContext; // unused
162  instance = Create(srcDesc, dstDesc,
165  deviceContext);
166  if (instance) {
167  bool r = instance->EvalStencils(srcBuffer, srcDesc,
168  dstBuffer, dstDesc,
169  stencilTable,
170  deviceContext);
171  delete instance;
172  return r;
173  }
174  return false;
175  }
176  }
177 
180  template <typename SRC_BUFFER, typename DST_BUFFER, typename STENCIL_TABLE>
182  SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc,
183  DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc,
184  STENCIL_TABLE const *stencilTable,
185  ID3D11DeviceContext *deviceContext) const {
186  return EvalStencils(srcBuffer->BindD3D11UAV(deviceContext), srcDesc,
187  dstBuffer->BindD3D11UAV(deviceContext), dstDesc,
188  stencilTable->GetSizesSRV(),
189  stencilTable->GetOffsetsSRV(),
190  stencilTable->GetIndicesSRV(),
191  stencilTable->GetWeightsSRV(),
192  /* start = */ 0,
193  /* end = */ stencilTable->GetNumStencils(),
194  deviceContext);
195  }
196 
199  bool EvalStencils(ID3D11UnorderedAccessView *srcSRV,
200  BufferDescriptor const &srcDesc,
201  ID3D11UnorderedAccessView *dstUAV,
202  BufferDescriptor const &dstDesc,
203  ID3D11ShaderResourceView *sizesSRV,
204  ID3D11ShaderResourceView *offsetsSRV,
205  ID3D11ShaderResourceView *indicesSRV,
206  ID3D11ShaderResourceView *weightsSRV,
207  int start,
208  int end,
209  ID3D11DeviceContext *deviceContext) const;
210 
212  bool Compile(BufferDescriptor const &srcDesc,
213  BufferDescriptor const &dstDesc,
214  ID3D11DeviceContext *deviceContext);
215 
217  static void Synchronize(ID3D11DeviceContext *deviceContext);
218 
219 private:
220  ID3D11ComputeShader * _computeShader;
221  ID3D11ClassLinkage * _classLinkage;
222  ID3D11ClassInstance * _singleBufferKernel;
223  ID3D11ClassInstance * _separateBufferKernel;
224  ID3D11Buffer * _uniformArgs; // uniform parameters for kernels
225 
226  int _workGroupSize;
227 };
228 
229 } // end namespace Osd
230 
231 } // end namespace OPENSUBDIV_VERSION
232 using namespace OPENSUBDIV_VERSION;
233 
234 } // end namespace OpenSubdiv
235 
236 
237 #endif // OPENSUBDIV3_OSD_D3D11_COMPUTE_EVALUATOR_H
static bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable, D3D11ComputeEvaluator const *instance, ID3D11DeviceContext *deviceContext)
Generic static compute function. This function has a same signature as other device kernels have so t...
BufferDescriptor is a struct which describes buffer elements in interleaved data buffers. Almost all Osd Evaluator APIs take BufferDescriptors along with device-specific buffer objects.
bool Compile(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, ID3D11DeviceContext *deviceContext)
Configure DX kernel. Returns false if it fails to compile the kernel.
bool EvalStencils(SRC_BUFFER *srcBuffer, BufferDescriptor const &srcDesc, DST_BUFFER *dstBuffer, BufferDescriptor const &dstDesc, STENCIL_TABLE const *stencilTable, ID3D11DeviceContext *deviceContext) const
static D3D11StencilTable * Create(Far::StencilTable const *stencilTable, ID3D11DeviceContext *deviceContext)
static D3D11StencilTable * Create(Far::StencilTable const *stencilTable, DEVICE_CONTEXT context)
D3D11StencilTable(Far::StencilTable const *stencilTable, ID3D11DeviceContext *deviceContext)
Stencil table class wrapping the template for compatibility.
Definition: stencilTable.h:273
static void Synchronize(ID3D11DeviceContext *deviceContext)
Wait the dispatched kernel finishes.
static D3D11ComputeEvaluator * Create(BufferDescriptor const &srcDesc, BufferDescriptor const &dstDesc, BufferDescriptor const &duDesc, BufferDescriptor const &dvDesc, ID3D11DeviceContext *deviceContext)