HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
binding.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HDST_BINDING_H
8 #define PXR_IMAGING_HDST_BINDING_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
13 
15 #include "pxr/imaging/hd/types.h"
16 
17 #include "pxr/base/tf/hash.h"
18 
20 
21 
22 using HdStBindingVector = std::vector<class HdStBinding>;
23 using HdStBindingRequestVector = std::vector<class HdStBindingRequest>;
24 
25 /// \class HdStBinding
26 ///
27 /// Bindings are used for buffers or textures, it simply associates a binding
28 /// type with a binding location.
29 ///
30 class HdStBinding {
31 public:
32  enum Type { // primvar, drawing coordinate and dispatch buffer bindings
33  // also shader fallback values
35  DISPATCH, // GL_DRAW_INDIRECT_BUFFER
36  DRAW_INDEX, // per-drawcall. not instanced
37  DRAW_INDEX_INSTANCE, // per-drawcall. attribdivisor=on
38  DRAW_INDEX_INSTANCE_ARRAY, // per-drawcall. attribdivisor=on, array
39  VERTEX_ATTR, // vertex-attribute
40  INDEX_ATTR, // GL_ELEMENT_ARRAY_BUFFER
41  SSBO, //
43  UBO, //
45  UNIFORM, //
47 
48  // shader parameter bindings
49  FALLBACK, // fallback value
50  TEXTURE_2D, // non-bindless uv texture
51  ARRAY_OF_TEXTURE_2D, // non-bindless array of uv textures. Not
52  // to be confused with a texture array
53  // (what udim and ptex textures use).
54  TEXTURE_FIELD, // non-bindless field texture
55  // creates accessor that samples uvw
56  // texture after transforming coordinates
57  // by a sampling transform
58  TEXTURE_UDIM_ARRAY, // non-bindless udim texture array
59  TEXTURE_UDIM_LAYOUT, // non-bindless udim layout
60  TEXTURE_PTEX_TEXEL, // non-bindless ptex texels
61  TEXTURE_PTEX_LAYOUT, // non-bindless ptex layout
62  TEXTURE_CUBEMAP, // non-bindless cubemap texture
63  BINDLESS_TEXTURE_2D, // bindless uv texture
64  BINDLESS_ARRAY_OF_TEXTURE_2D, // bindless array of uv textures
65  BINDLESS_TEXTURE_FIELD, // bindless field texture
66  // (see above)
67  BINDLESS_TEXTURE_UDIM_ARRAY, // bindless uv texture array
68  BINDLESS_TEXTURE_UDIM_LAYOUT, // bindless udim layout
69  BINDLESS_TEXTURE_PTEX_TEXEL, // bindless ptex texels
70  BINDLESS_TEXTURE_PTEX_LAYOUT, // bindless ptex layout
71  BINDLESS_TEXTURE_CUBEMAP, // bindless cubemap texture
72  PRIMVAR_REDIRECT, // primvar redirection
73  FIELD_REDIRECT, // accesses a field texture by name and
74  // uses fallbackValue if no accessor for
75  // the texture exists.
76  TRANSFORM_2D // transform2d
77  };
78  enum Location {
79  // NOT_EXIST is a special value of location for a uniform
80  // which is assigned but optimized out after linking program.
81  NOT_EXIST = 0xffff
82  };
83  HdStBinding() : _typeAndLocation(-1) { }
84  HdStBinding(Type type, int location, int textureUnit=0) {
85  Set(type, location, textureUnit);
86  }
87  void Set(Type type, int location, int textureUnit) {
88  _typeAndLocation = (textureUnit << 24)|(location << 8)|(int)(type);
89  }
90  bool IsValid() const { return _typeAndLocation >= 0; }
91  Type GetType() const { return (Type)(_typeAndLocation & 0xff); }
92  int GetLocation() const { return (_typeAndLocation >> 8) & 0xffff; }
93  int GetTextureUnit() const { return (_typeAndLocation >> 24) & 0xff; }
94  int GetValue() const { return _typeAndLocation; }
95  bool operator < (HdStBinding const &b) const {
96  return (_typeAndLocation < b._typeAndLocation);
97  }
98 private:
99  int _typeAndLocation;
100 };
101 
102 /// BindingRequest allows externally allocated buffers to be bound at render
103 /// time. The different modes of binding discussed below allow the caller a
104 /// range of opt-in binding behaviors, from simply reserving a binding location
105 /// so it can be managed from client code, to fully generating buffer accessor
106 /// code at compile time (i.e. when using a BufferArrayRange or BufferResource).
107 ///
108 /// This is a "request" because the caller makes a request before bindings are
109 /// resolved. All requests are consulted and fulfilled during binding
110 /// resolution.
112 public:
113 
114  HdStBindingRequest() = default;
115 
116  /// A data binding, not backed by neither BufferArrayRange nor
117  /// BufferResource. This binding request simply
118  /// generates named metadata (#define HD_HAS_foo 1, #define HD_foo_Binding)
120  : _bindingType(bindingType)
121  , _dataType(HdTypeInvalid)
122  , _name(name)
123  , _resource(nullptr)
124  , _bar(nullptr)
125  , _isInterleaved(false)
126  , _isWritable(false)
127  , _arraySize(0)
128  , _concatenateNames(false)
129  {}
130 
131  /// A data binding, not backed by neither BufferArrayRange nor
132  /// BufferResource.
135  : _bindingType(bindingType)
136  , _dataType(dataType)
137  , _name(name)
138  , _resource(nullptr)
139  , _bar(nullptr)
140  , _isInterleaved(false)
141  , _isWritable(false)
142  , _arraySize(0)
143  , _concatenateNames(false)
144  {}
145 
146  /// A buffer resource binding. Binds a given buffer resource to a specified
147  /// name. The data type is set from the resource.
149  HdStBufferResourceSharedPtr const& resource)
150  : _bindingType(bindingType)
151  , _dataType(resource->GetTupleType().type)
152  , _name(name)
153  , _resource(resource)
154  , _bar(nullptr)
155  , _isInterleaved(false)
156  , _isWritable(false)
157  , _arraySize(0)
158  , _concatenateNames(false)
159  {}
160 
161  /// A named struct binding. From an interleaved BufferArray, an array of
162  /// structs will be generated, consuming a single binding point. Note that
163  /// all resources in the buffer array must have the same underlying
164  /// identifier, hence must be interleaved and bindable as a single resource.
165  /// Data types can be derived from each HdStBufferResource of bar.
168  bool interleave, bool writable = false,
169  size_t arraySize = 0, bool concatenateNames = false)
170  : _bindingType(type)
171  , _dataType(HdTypeInvalid)
172  , _name(name)
173  , _resource(nullptr)
174  , _bar(bar)
175  , _isInterleaved(interleave)
176  , _isWritable(writable)
177  , _arraySize(arraySize)
178  , _concatenateNames(concatenateNames)
179  {}
180 
181  // ---------------------------------------------------------------------- //
182  /// \name Discriminators
183  // ---------------------------------------------------------------------- //
184 
185  /// Resource bingings have a single associated Hydra resource, but no buffer
186  /// array.
187  bool IsResource() const {
188  return bool(_resource);
189  }
190 
191  /// A buffer array binding has several buffers bundled together and each
192  /// buffer will be bound individually and exposed as independent arrays in
193  /// the shader.
194  bool IsBufferArray() const {
195  return _bar && !_isInterleaved;
196  }
197 
198  /// Like BufferArray binding requests, struct bindings have several buffers,
199  /// however they must be allocated into a single resource and interleaved.
200  /// This type of binding request is exposed in the shader an array of
201  /// structs.
203  return _bar && _isInterleaved;
204  }
205 
206  /// True when the resource is being bound so that it can be written to.
207  /// This affects whether it will be declared 'const' or not.
208  bool isWritable() const {
209  return _bar && _isWritable;
210  }
211 
212  /// This binding is typelss. CodeGen only allocate location and
213  /// skip emitting declarations and accessors.
214  bool IsTypeless() const {
215  return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
216  }
217 
218  // ---------------------------------------------------------------------- //
219  /// \name Accessors
220  // ---------------------------------------------------------------------- //
221 
222  /// Returns the name of the binding point, if any; buffer arrays and structs
223  /// need not be named.
224  TfToken const& GetName() const {
225  return _name;
226  }
227  /// Returns the HdStBinding type of this request.
229  return _bindingType;
230  }
231  /// Returns the single resource associated with this binding request or
232  /// null when IsResource() returns false.
234  return _resource;
235  }
236  /// Returns the resource or buffer array range offset, defaults to zero.
237  int GetByteOffset() const {
238  // buffer resource binding
239  if (_resource) return _resource->GetOffset();
240 
241  // named struct binding (interleaved) - the resource name doesn't matter
242  // since a single binding point is used.
243  if (_bar) return _bar->GetByteOffset(TfToken());
244  return 0;
245  }
246  /// Returns the buffer array range associated with this binding request or
247  /// null when IsBufferArrqay() returns false.
249  return _bar;
250  }
251 
252  /// Return the data type of this request
253  HdType GetDataType() const {
254  return _dataType;
255  }
256 
257  /// Array size if request is for an array of structs.
258  size_t GetArraySize() const {
259  return _arraySize;
260  }
261 
262  /// Returns whether the struct binding point and struct member names
263  /// should be concatenated when codegen'ing the accessor.
264  bool ConcatenateNames() const {
265  return _concatenateNames;
266  }
267 
268  // ---------------------------------------------------------------------- //
269  /// \name Comparison
270  // ---------------------------------------------------------------------- //
271  HDST_API
272  bool operator==(HdStBindingRequest const &other) const;
273 
274  HDST_API
275  bool operator!=(HdStBindingRequest const &other) const;
276 
277  // ---------------------------------------------------------------------- //
278  /// \name Hash
279  // ---------------------------------------------------------------------- //
280 
281  /// Returns the hash corresponding to this buffer request.
282  ///
283  /// Note that this hash captures the structural state of the request, not
284  /// the contents. For example, buffer array versions/reallocations will not
285  /// affect hash, but changing the BAR pointer will.
286  HDST_API
287  size_t ComputeHash() const;
288 
289  // TfHash support.
290  template <class HashState>
291  friend void TfHashAppend(HashState &h, HdStBindingRequest const &br) {
292  h.Append(br._name,
293  br._bindingType,
294  br._dataType,
295  br._isInterleaved);
296  }
297 
298 private:
299  // This class unfortunately represents several concepts packed into a single
300  // class. Ideally, we would break this out as one class per concept,
301  // however that would also require virtual dispatch, which is overkill for
302  // the current use cases.
303 
304  // Named binding request
305  HdStBinding::Type _bindingType;
306  HdType _dataType;
307  TfToken _name;
308 
309  // Resource binding request
310  HdStBufferResourceSharedPtr _resource;
311 
312  // Struct binding request
314  bool _isInterleaved;
315 
316  bool _isWritable;
317 
318  size_t _arraySize;
319 
320  bool _concatenateNames;
321 };
322 
323 
325 
326 #endif // PXR_IMAGING_HDST_BINDING_H
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name)
Definition: binding.h:119
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition: binding.h:258
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdType dataType)
Definition: binding.h:133
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
friend void TfHashAppend(HashState &h, HdStBindingRequest const &br)
Definition: binding.h:291
std::vector< class HdStBindingRequest > HdStBindingRequestVector
Definition: binding.h:23
int GetTextureUnit() const
Definition: binding.h:93
HdStBinding::Type GetBindingType() const
Returns the HdStBinding type of this request.
Definition: binding.h:228
HDST_API bool operator==(HdStBindingRequest const &other) const
HdBufferArrayRangeSharedPtr const & GetBar() const
Definition: binding.h:248
OutGridT const XformOp bool bool
bool IsBufferArray() const
Definition: binding.h:194
HDST_API size_t ComputeHash() const
bool IsValid() const
Definition: binding.h:90
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: token.h:70
HdStBinding(Type type, int location, int textureUnit=0)
Definition: binding.h:84
HdStBinding()
Definition: binding.h:83
bool ConcatenateNames() const
Definition: binding.h:264
bool IsTypeless() const
Definition: binding.h:214
HdStBufferResourceSharedPtr const & GetResource() const
Definition: binding.h:233
HdStBindingRequest()=default
GLint location
Definition: glcorearb.h:805
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdStBufferResourceSharedPtr const &resource)
Definition: binding.h:148
bool IsResource() const
Definition: binding.h:187
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
Type GetType() const
Definition: binding.h:91
int GetByteOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:237
bool isWritable() const
Definition: binding.h:208
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
TfToken const & GetName() const
Definition: binding.h:224
bool IsInterleavedBufferArray() const
Definition: binding.h:202
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
void Set(Type type, int location, int textureUnit)
Definition: binding.h:87
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:253
HdStBindingRequest(HdStBinding::Type type, TfToken const &name, HdBufferArrayRangeSharedPtr bar, bool interleave, bool writable=false, size_t arraySize=0, bool concatenateNames=false)
Definition: binding.h:166
int GetValue() const
Definition: binding.h:94
std::vector< class HdStBinding > HdStBindingVector
Definition: binding.h:22
that also have some descendant prim *whose name begins with bar
HdType
Definition: types.h:272
HDST_API bool operator!=(HdStBindingRequest const &other) const
HUSD_API const char * dataType()
int GetLocation() const
Definition: binding.h:92
bool operator<(HdStBinding const &b) const
Definition: binding.h:95