binding.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 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 
32 #include "pxr/imaging/hd/bufferResource.h"
33 #include "pxr/imaging/hd/bufferArrayRange.h"
34 
35 #include "pxr/base/tf/hash.h"
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 
40 typedef std::vector<class HdBinding> HdBindingVector;
41 typedef std::vector<class HdBindingRequest> HdBindingRequestVector;
42 
48 class HdBinding {
49 public:
50  enum Type { // primvar, drawing coordinate and dispatch buffer bindings
51  // also shader fallback values
52  UNKNOWN,
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, //
60  BINDLESS_SSBO_RANGE, //
61  UBO, //
62  BINDLESS_UNIFORM, //
63  UNIFORM, //
64  UNIFORM_ARRAY, //
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 
128 public:
129 
130  HdBindingRequest() = default;
131 
135  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name)
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 
149  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name,
150  HdType dataType)
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 
164  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name,
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 
182  HdBindingRequest(HdBinding::Type type, TfToken const& name,
183  HdBufferArrayRangeSharedPtr 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  // ---------------------------------------------------------------------- //
199  // ---------------------------------------------------------------------- //
200 
203  bool IsResource() const {
204  return bool(_resource);
205  }
206 
210  bool IsBufferArray() const {
211  return _bar && !_isInterleaved;
212  }
213 
219  return _bar && _isInterleaved;
220  }
221 
224  bool isWritable() const {
225  return _bar && _isWritable;
226  }
227 
230  bool IsTypeless() const {
231  return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
232  }
233 
234  // ---------------------------------------------------------------------- //
236  // ---------------------------------------------------------------------- //
237 
240  TfToken const& GetName() const {
241  return _name;
242  }
244  HdBinding::Type GetBindingType() const {
245  return _bindingType;
246  }
249  HdBufferResourceSharedPtr const& GetResource() const {
250  return _resource;
251  }
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  }
264  HdBufferArrayRangeSharedPtr const& GetBar() const {
265  return _bar;
266  }
267 
269  HdType GetDataType() const {
270  return _dataType;
271  }
272 
274  size_t GetArraySize() const {
275  return _arraySize;
276  }
277 
280  bool ConcatenateNames() const {
281  return _concatenateNames;
282  }
283 
284  // ---------------------------------------------------------------------- //
286  // ---------------------------------------------------------------------- //
287  HD_API
288  bool operator==(HdBindingRequest const &other) const;
289 
290  HD_API
291  bool operator!=(HdBindingRequest const &other) const;
292 
293  // ---------------------------------------------------------------------- //
295  // ---------------------------------------------------------------------- //
296 
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
329  HdBufferArrayRangeSharedPtr _bar;
330  bool _isInterleaved;
331 
332  bool _isWritable;
333 
334  size_t _arraySize;
335 
336  bool _concatenateNames;
337 };
338 
339 
340 PXR_NAMESPACE_CLOSE_SCOPE
341 
342 #endif // PXR_IMAGING_HD_BINDING_H
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name, HdBufferResourceSharedPtr const &resource)
A buffer resource binding.
Definition: binding.h:164
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition: binding.h:274
bool IsTypeless() const
This binding is typelss.
Definition: binding.h:230
Bindings are used for buffers or textures, it simple associates a binding type with a binding locatio...
Definition: binding.h:48
bool isWritable() const
True when the resource is being bound so that it can be written to.
Definition: binding.h:224
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name, HdType dataType)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:149
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition: binding.h:203
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition: binding.h:264
HD_API size_t ComputeHash() const
Returns the hash corresponding to this buffer request.
TfToken const & GetName() const
Returns the name of the binding point, if any; buffer arrays and structs need not be named.
Definition: binding.h:240
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:135
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition: binding.h:210
int GetByteOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:253
bool ConcatenateNames() const
Returns whether the struct binding point and struct member names should be concatenated when codegen'...
Definition: binding.h:280
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition: binding.h:218
HdBinding::Type GetBindingType() const
Returns the HdBinding type of this request.
Definition: binding.h:244
BindingRequest allows externally allocated buffers to be bound at render time.
Definition: binding.h:127
HdBindingRequest(HdBinding::Type type, TfToken const &name, HdBufferArrayRangeSharedPtr bar, bool interleave, bool writable=false, size_t arraySize=0, bool concatenateNames=false)
A named struct binding.
Definition: binding.h:182
HdBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition: binding.h:249
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:269