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