All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
resourceBinder.h
1 //
2 // Copyright 2016 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 #ifndef HDST_RESOURCE_BINDER_H
25 #define HDST_RESOURCE_BINDER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hd/version.h"
30 
31 #include "pxr/imaging/hd/binding.h"
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/tf/stl.h"
34 
35 #include "pxr/base/tf/hashmap.h"
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 
40 class HdStDrawItem;
41 class HdStShaderCode;
42 class HdStResourceGL;
43 
44 typedef boost::shared_ptr<class HdStBufferResourceGL> HdStBufferResourceGLSharedPtr;
45 typedef boost::shared_ptr<class HdStBufferArrayRangeGL> HdStBufferArrayRangeGLSharedPtr;
46 
47 typedef boost::shared_ptr<class HdStShaderCode> HdStShaderCodeSharedPtr;
48 typedef std::vector<HdStShaderCodeSharedPtr> HdStShaderCodeSharedPtrVector;
49 typedef std::vector<class HdBindingRequest> HdBindingRequestVector;
50 
61 // [SSBO, BindlessUniform]
66 // [SSBO, BindlessUniform]
132 class HdSt_ResourceBinder {
133 public:
135  class MetaData {
136  public:
137  MetaData() : instancerNumLevels(0) {}
138 
139  typedef size_t ID;
141  HDST_API
142  ID ComputeHash() const;
143 
144  // -------------------------------------------------------------------
145  // for a primvar in interleaved buffer array (Constant, ShaderData)
146  struct StructEntry {
147  StructEntry(TfToken const &name,
148  TfToken const &dataType,
149  int offset, int arraySize)
150  : name(name)
151  , dataType(dataType)
152  , offset(offset)
153  , arraySize(arraySize)
154  {}
155 
156  TfToken name;
157  TfToken dataType;
158  int offset;
159  int arraySize;
160 
161  bool operator < (StructEntry const &other) const {
162  return offset < other.offset;
163  }
164  };
165  struct StructBlock {
166  StructBlock(TfToken const &name)
167  : blockName(name) {}
168  TfToken blockName;
169  std::vector<StructEntry> entries;
170  };
171  typedef std::map<HdBinding, StructBlock> StructBlockBinding;
172 
173  // -------------------------------------------------------------------
174  // for a primvar in non-interleaved buffer array (Vertex, Element, ...)
175  struct Primvar {
176  Primvar() {}
177  Primvar(TfToken const &name, TfToken const &dataType)
178  : name(name), dataType(dataType) {}
179  TfToken name;
180  TfToken dataType;
181  };
182  typedef std::map<HdBinding, Primvar> PrimvarBinding;
183 
184  // -------------------------------------------------------------------
185  // for instance primvars
186  struct NestedPrimvar {
187  NestedPrimvar() {}
188  NestedPrimvar(TfToken const &name, TfToken const &dataType,
189  int level)
190  : name(name), dataType(dataType), level(level) {}
191  TfToken name;
192  TfToken dataType;
193  int level;
194  };
195  typedef std::map<HdBinding, NestedPrimvar> NestedPrimvarBinding;
196 
197  // -------------------------------------------------------------------
198  // for shader parameter accessors
199  struct ShaderParameterAccessor {
200  ShaderParameterAccessor() {}
201  ShaderParameterAccessor(TfToken const &name,
202  TfToken const &dataType,
203  TfTokenVector const &inPrimvars=TfTokenVector())
204  : name(name), dataType(dataType), inPrimvars(inPrimvars) {}
205  TfToken name; // e.g. Kd
206  TfToken dataType; // e.g. vec4
207  TfTokenVector inPrimvars; // for primvar renaming and texture coordinates,
208  };
209  typedef std::map<HdBinding, ShaderParameterAccessor> ShaderParameterBinding;
210 
211  // -------------------------------------------------------------------
212  // for accessor with fallback value sampling a 3d texture at
213  // coordinates transformed by an associated matrix
214  //
215  // The accessor will be named NAME (i.e., vec3 HdGet_NAME(vec3 p)) and
216  // will sample FIELDNAMETexture at the coordinate obtained by transforming
217  // p by FIELDNAMESamplineTransform. If FIELDNAMETexture does not exist,
218  // NAMEFallback is used.
219  struct FieldRedirectAccessor {
220  FieldRedirectAccessor() {}
221  FieldRedirectAccessor(TfToken const &name,
222  TfToken const &fieldName)
223  : name(name), fieldName(fieldName) {}
224  TfToken name;
225  TfToken fieldName;
226  };
227  typedef std::map<HdBinding, FieldRedirectAccessor> FieldRedirectBinding;
228 
229  // -------------------------------------------------------------------
230  // for specific buffer array (drawing coordinate, instance indices)
231  struct BindingDeclaration {
232  BindingDeclaration() {}
233  BindingDeclaration(TfToken const &name,
234  TfToken const &dataType,
235  HdBinding binding)
236  : name(name), dataType(dataType), binding(binding) {}
237  TfToken name;
238  TfToken dataType;
239  HdBinding binding;
240  };
241 
242  // -------------------------------------------------------------------
243 
244  StructBlockBinding constantData;
245  StructBlockBinding shaderData;
246  StructBlockBinding topologyVisibilityData;
247  PrimvarBinding elementData;
248  PrimvarBinding vertexData;
249  PrimvarBinding fvarData;
250  PrimvarBinding computeReadWriteData;
251  PrimvarBinding computeReadOnlyData;
252  NestedPrimvarBinding instanceData;
253  int instancerNumLevels;
254 
255  ShaderParameterBinding shaderParameterBinding;
256  FieldRedirectBinding fieldRedirectBinding;
257 
258  BindingDeclaration drawingCoord0Binding;
259  BindingDeclaration drawingCoord1Binding;
260  BindingDeclaration drawingCoord2Binding;
261  BindingDeclaration drawingCoordIBinding;
262  BindingDeclaration instanceIndexArrayBinding;
263  BindingDeclaration culledInstanceIndexArrayBinding;
264  BindingDeclaration instanceIndexBaseBinding;
265  BindingDeclaration primitiveParamBinding;
266  BindingDeclaration edgeIndexBinding;
267 
268  StructBlockBinding customInterleavedBindings;
269  std::vector<BindingDeclaration> customBindings;
270  };
271 
273  HDST_API
274  HdSt_ResourceBinder();
275 
278  HDST_API
279  void ResolveBindings(HdStDrawItem const *drawItem,
280  HdStShaderCodeSharedPtrVector const &shaders,
281  MetaData *metaDataOut,
282  bool indirect,
283  bool instanceDraw,
284  HdBindingRequestVector const &customBindings);
285 
288  HDST_API
289  void ResolveComputeBindings(HdBufferSpecVector const &readWriteBufferSpecs,
290  HdBufferSpecVector const &readOnlyBufferSpecs,
291  HdStShaderCodeSharedPtrVector const &shaders,
292  MetaData *metaDataOut);
293 
297  HDST_API
298  void IntrospectBindings(HdStResourceGL const & programResource);
299 
300  HDST_API
301  void Bind(HdBindingRequest const& req) const;
302  HDST_API
303  void Unbind(HdBindingRequest const& req) const;
304 
306  HDST_API
307  void BindBufferArray(HdStBufferArrayRangeGLSharedPtr const &bar) const;
308  HDST_API
309  void UnbindBufferArray(HdStBufferArrayRangeGLSharedPtr const &bar) const;
310 
312  HDST_API
313  void BindConstantBuffer(
314  HdStBufferArrayRangeGLSharedPtr const & constantBar) const;
315  HDST_API
316  void UnbindConstantBuffer(
317  HdStBufferArrayRangeGLSharedPtr const &constantBar) const;
318 
320  HDST_API
321  void BindInterleavedBuffer(
322  HdStBufferArrayRangeGLSharedPtr const & constantBar,
323  TfToken const &name) const;
324  HDST_API
325  void UnbindInterleavedBuffer(
326  HdStBufferArrayRangeGLSharedPtr const &constantBar,
327  TfToken const &name) const;
328 
330  HDST_API
331  void BindInstanceBufferArray(
332  HdStBufferArrayRangeGLSharedPtr const &bar, int level) const;
333  HDST_API
334  void UnbindInstanceBufferArray(
335  HdStBufferArrayRangeGLSharedPtr const &bar, int level) const;
336 
338  HDST_API
339  void BindShaderResources(HdStShaderCode const *shader) const;
340  HDST_API
341  void UnbindShaderResources(HdStShaderCode const *shader) const;
342 
345  HDST_API
346  void BindBuffer(TfToken const &name,
347  HdStBufferResourceGLSharedPtr const &resource) const;
348  HDST_API
349  void BindBuffer(TfToken const &name,
350  HdStBufferResourceGLSharedPtr const &resource,
351  int offset, int level=-1) const;
352  HDST_API
353  void UnbindBuffer(TfToken const &name,
354  HdStBufferResourceGLSharedPtr const &resource,
355  int level=-1) const;
356 
358  HDST_API
359  void BindUniformui(TfToken const &name, int count,
360  const unsigned int *value) const;
361 
363  HDST_API
364  void BindUniformi(TfToken const &name, int count, const int *value) const;
365 
367  HDST_API
368  void BindUniformArrayi(TfToken const &name, int count, const int *value) const;
369 
371  HDST_API
372  void BindUniformf(TfToken const &name, int count, const float *value) const;
373 
377  HdBinding GetBinding(TfToken const &name, int level=-1) const {
378  HdBinding binding;
379  TfMapLookup(_bindingMap, NameAndLevel(name, level), &binding);
380  return binding;
381  }
382 
383  int GetNumReservedTextureUnits() const {
384  return _numReservedTextureUnits;
385  }
386 
387 private:
388  // for batch execution
389  struct NameAndLevel {
390  NameAndLevel(TfToken const &n, int lv=-1) :
391  name(n), level(lv) {}
392  TfToken name;
393  int level;
394 
395  bool operator < (NameAndLevel const &other) const {
396  return name < other.name ||
397  (name == other.name && level < other.level);
398  }
399  };
400  typedef std::map<NameAndLevel, HdBinding> _BindingMap;
401  _BindingMap _bindingMap;
402  int _numReservedTextureUnits;
403 };
404 
405 
406 PXR_NAMESPACE_CLOSE_SCOPE
407 
408 #endif // HDST_RESOURCE_BINDER_H
Bindings are used for buffers or textures, it simple associates a binding type with a binding locatio...
Definition: binding.h:46
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
bool TfMapLookup(Container const &map, Key const &key, Result *valuePtr)
Checks if an item exists in a map or a TfHashMap.
Definition: stl.h:88
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition: shaderCode.h:59
BindingRequest allows externally allocated buffers to be bound at render time.
Definition: binding.h:116
Base class for simple OpenGL resource objects.
Definition: resourceGL.h:47