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