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_HDST_BINDING_H
25 #define PXR_IMAGING_HDST_BINDING_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 
30 #include "pxr/imaging/hd/bufferArrayRange.h"
31 #include "pxr/imaging/hd/bufferResource.h"
32 #include "pxr/imaging/hd/types.h"
33 
34 #include "pxr/base/tf/hash.h"
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 
39 using HdStBindingVector = std::vector<class HdStBinding>;
40 using HdStBindingRequestVector = std::vector<class HdStBindingRequest>;
41 
47 class HdStBinding {
48 public:
49  enum Type { // primvar, drawing coordinate and dispatch buffer bindings
50  // also shader fallback values
51  UNKNOWN,
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, //
59  BINDLESS_SSBO_RANGE, //
60  UBO, //
61  BINDLESS_UNIFORM, //
62  UNIFORM, //
63  UNIFORM_ARRAY, //
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 
127 public:
128 
129  HdStBindingRequest() = default;
130 
134  HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name)
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 
148  HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name,
149  HdType dataType)
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 
163  HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name,
164  HdBufferResourceSharedPtr 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 
181  HdStBindingRequest(HdStBinding::Type type, TfToken const& name,
182  HdBufferArrayRangeSharedPtr 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  // ---------------------------------------------------------------------- //
198  // ---------------------------------------------------------------------- //
199 
202  bool IsResource() const {
203  return bool(_resource);
204  }
205 
209  bool IsBufferArray() const {
210  return _bar && !_isInterleaved;
211  }
212 
218  return _bar && _isInterleaved;
219  }
220 
223  bool isWritable() const {
224  return _bar && _isWritable;
225  }
226 
229  bool IsTypeless() const {
230  return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
231  }
232 
233  // ---------------------------------------------------------------------- //
235  // ---------------------------------------------------------------------- //
236 
239  TfToken const& GetName() const {
240  return _name;
241  }
243  HdStBinding::Type GetBindingType() const {
244  return _bindingType;
245  }
248  HdBufferResourceSharedPtr const& GetResource() const {
249  return _resource;
250  }
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  }
263  HdBufferArrayRangeSharedPtr const& GetBar() const {
264  return _bar;
265  }
266 
268  HdType GetDataType() const {
269  return _dataType;
270  }
271 
273  size_t GetArraySize() const {
274  return _arraySize;
275  }
276 
279  bool ConcatenateNames() const {
280  return _concatenateNames;
281  }
282 
283  // ---------------------------------------------------------------------- //
285  // ---------------------------------------------------------------------- //
286  HDST_API
287  bool operator==(HdStBindingRequest const &other) const;
288 
289  HDST_API
290  bool operator!=(HdStBindingRequest const &other) const;
291 
292  // ---------------------------------------------------------------------- //
294  // ---------------------------------------------------------------------- //
295 
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  HdBufferResourceSharedPtr _resource;
326 
327  // Struct binding request
328  HdBufferArrayRangeSharedPtr _bar;
329  bool _isInterleaved;
330 
331  bool _isWritable;
332 
333  size_t _arraySize;
334 
335  bool _concatenateNames;
336 };
337 
338 
339 PXR_NAMESPACE_CLOSE_SCOPE
340 
341 #endif // PXR_IMAGING_HDST_BINDING_H
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:134
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:268
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdBufferResourceSharedPtr const &resource)
A buffer resource binding.
Definition: binding.h:163
HdStBinding::Type GetBindingType() const
Returns the HdStBinding type of this request.
Definition: binding.h:243
HdStBindingRequest(HdStBinding::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:181
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition: binding.h:209
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition: binding.h:217
Bindings are used for buffers or textures, it simply associates a binding type with a binding locatio...
Definition: binding.h:47
TfToken const & GetName() const
Returns the name of the binding point, if any; buffer arrays and structs need not be named.
Definition: binding.h:239
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition: binding.h:273
HDST_API size_t ComputeHash() const
Returns the hash corresponding to this buffer request.
bool ConcatenateNames() const
Returns whether the struct binding point and struct member names should be concatenated when codegen'...
Definition: binding.h:279
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition: binding.h:263
HdBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition: binding.h:248
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdType dataType)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:148
int GetByteOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:252
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition: binding.h:202
bool IsTypeless() const
This binding is typelss.
Definition: binding.h:229
bool isWritable() const
True when the resource is being bound so that it can be written to.
Definition: binding.h:223
BindingRequest allows externally allocated buffers to be bound at render time.
Definition: binding.h:126