All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 HD_BINDING_H
25 #define 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 PXR_NAMESPACE_OPEN_SCOPE
36 
37 
38 typedef std::vector<class HdBinding> HdBindingVector;
39 typedef std::vector<class HdBindingRequest> HdBindingRequestVector;
40 
46 class HdBinding {
47 public:
48  enum Type { // primvar, drawing coordinate and dispatch buffer bindings
49  // also shader fallback values
50  UNKNOWN,
51  DISPATCH, // GL_DRAW_INDIRECT_BUFFER
52  DRAW_INDEX, // per-drawcall. not instanced
53  DRAW_INDEX_INSTANCE, // per-drawcall. attribdivisor=on
54  DRAW_INDEX_INSTANCE_ARRAY, // per-drawcall. attribdivisor=on, array
55  VERTEX_ATTR, // vertex-attribute
56  INDEX_ATTR, // GL_ELEMENT_ARRAY_BUFFER
57  SSBO, //
58  BINDLESS_SSBO_RANGE, //
59  UBO, //
60  BINDLESS_UNIFORM, //
61  UNIFORM, //
62  UNIFORM_ARRAY, //
63  TBO, //
64 
65  // shader parameter bindings
66  FALLBACK, // fallback value
67  TEXTURE_2D, // non-bindless uv texture
68  TEXTURE_3D,
69  TEXTURE_UDIM_ARRAY, // non-bindless udim texture array
70  TEXTURE_UDIM_LAYOUT, // non-bindless udim layout
71  TEXTURE_PTEX_TEXEL, // non-bindless ptex texels
72  TEXTURE_PTEX_LAYOUT, // non-bindless ptex layout
73  BINDLESS_TEXTURE_2D, // bindless uv texture
74  BINDLESS_TEXTURE_3D,
75  BINDLESS_TEXTURE_UDIM_ARRAY, // bindless uv texture array
76  BINDLESS_TEXTURE_UDIM_LAYOUT, // bindless udim layout
77  BINDLESS_TEXTURE_PTEX_TEXEL, // bindless ptex texels
78  BINDLESS_TEXTURE_PTEX_LAYOUT, // bindless ptex layout
79  PRIMVAR_REDIRECT, // primvar redirection
80  FIELD_REDIRECT // accesses 3d texture with potential
81  // transform and fallback under different name
82  };
83  enum Location {
84  // NOT_EXIST is a special value of location for a uniform
85  // which is assigned but optimized out after linking program.
86  NOT_EXIST = 0xffff
87  };
88  HdBinding() : _typeAndLocation(-1) { }
89  HdBinding(Type type, int location, int textureUnit=0) {
90  Set(type, location, textureUnit);
91  }
92  void Set(Type type, int location, int textureUnit) {
93  _typeAndLocation = (textureUnit << 24)|(location << 8)|(int)(type);
94  }
95  bool IsValid() const { return _typeAndLocation >= 0; }
96  Type GetType() const { return (Type)(_typeAndLocation & 0xff); }
97  int GetLocation() const { return (_typeAndLocation >> 8) & 0xffff; }
98  int GetTextureUnit() const { return (_typeAndLocation >> 24) & 0xff; }
99  int GetValue() const { return _typeAndLocation; }
100  bool operator < (HdBinding const &b) const {
101  return (_typeAndLocation < b._typeAndLocation);
102  }
103 private:
104  int _typeAndLocation;
105 };
106 
117 public:
118 
119  HdBindingRequest() = default;
120 
124  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name)
125  : _bindingType(bindingType)
126  , _dataType(HdTypeInvalid)
127  , _name(name)
128  , _resource(nullptr)
129  , _bar(nullptr)
130  , _isInterleaved(false)
131  {}
132 
135  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name,
136  HdType dataType)
137  : _bindingType(bindingType)
138  , _dataType(dataType)
139  , _name(name)
140  , _resource(nullptr)
141  , _bar(nullptr)
142  , _isInterleaved(false)
143  {}
144 
147  HdBindingRequest(HdBinding::Type bindingType, TfToken const& name,
148  HdBufferResourceSharedPtr const& resource)
149  : _bindingType(bindingType)
150  , _dataType(resource->GetTupleType().type)
151  , _name(name)
152  , _resource(resource)
153  , _bar(nullptr)
154  , _isInterleaved(false)
155  {}
156 
162  HdBindingRequest(HdBinding::Type type, TfToken const& name,
163  HdBufferArrayRangeSharedPtr bar,
164  bool interleave)
165  : _bindingType(type)
166  , _dataType(HdTypeInvalid)
167  , _name(name)
168  , _resource(nullptr)
169  , _bar(bar)
170  , _isInterleaved(interleave)
171  {}
172 
173  // ---------------------------------------------------------------------- //
175  // ---------------------------------------------------------------------- //
176 
179  bool IsResource() const {
180  return bool(_resource);
181  }
182 
186  bool IsBufferArray() const {
187  return _bar && !_isInterleaved;
188  }
189 
195  return _bar && _isInterleaved;
196  }
197 
200  bool IsTypeless() const {
201  return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
202  }
203 
204  // ---------------------------------------------------------------------- //
206  // ---------------------------------------------------------------------- //
207 
210  TfToken const& GetName() const {
211  return _name;
212  }
214  HdBinding::Type GetBindingType() const {
215  return _bindingType;
216  }
219  HdBufferResourceSharedPtr const& GetResource() const {
220  return _resource;
221  }
223  int GetOffset() const {
224  if (_resource) return _resource->GetOffset();
225  if (_bar) return _bar->GetOffset();
226  return 0;
227  }
230  HdBufferArrayRangeSharedPtr const& GetBar() const {
231  return _bar;
232  }
233 
235  HdType GetDataType() const {
236  return _dataType;
237  }
238 
244  HD_API
245  size_t ComputeHash() const;
246 
247 private:
248  // This class unfortunately represents several concepts packed into a single
249  // class. Ideally, we would break this out as one class per concept,
250  // however that would also require virtual dispatch, which is overkill for
251  // the current use cases.
252 
253  // Named binding request
254  HdBinding::Type _bindingType;
255  HdType _dataType;
256  TfToken _name;
257 
258  // Resource binding request
259  HdBufferResourceSharedPtr _resource;
260 
261  // Struct binding request
262  HdBufferArrayRangeSharedPtr _bar;
263  bool _isInterleaved;
264 
265 };
266 
267 
268 PXR_NAMESPACE_CLOSE_SCOPE
269 
270 #endif // HD_BINDING_H
HdBinding::Type GetBindingType() const
Returns the HdBinding type of this request.
Definition: binding.h:214
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name, HdBufferResourceSharedPtr const &resource)
A buffer resource binding.
Definition: binding.h:147
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition: binding.h:186
HdBindingRequest(HdBinding::Type type, TfToken const &name, HdBufferArrayRangeSharedPtr bar, bool interleave)
A named struct binding.
Definition: binding.h:162
Bindings are used for buffers or textures, it simple associates a binding type with a binding locatio...
Definition: binding.h:46
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name, HdType dataType)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:135
TfToken const & GetName() const
Returns the name of the binding point, if any; buffer arrays and structs need not be named...
Definition: binding.h:210
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:235
HdBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition: binding.h:219
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HdBindingRequest(HdBinding::Type bindingType, TfToken const &name)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:124
bool IsTypeless() const
This binding is typelss.
Definition: binding.h:200
int GetOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:223
HD_API size_t ComputeHash() const
Returns the hash corresponding to this buffer request.
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition: binding.h:179
BindingRequest allows externally allocated buffers to be bound at render time.
Definition: binding.h:116
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition: binding.h:230
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition: binding.h:194