Loading...
Searching...
No Matches
binding.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_HDST_BINDING_H
8#define PXR_IMAGING_HDST_BINDING_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hdSt/api.h"
12#include "pxr/imaging/hdSt/bufferResource.h"
13
14#include "pxr/imaging/hd/bufferArrayRange.h"
15#include "pxr/imaging/hd/types.h"
16
17#include "pxr/base/tf/hash.h"
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21
22using HdStBindingVector = std::vector<class HdStBinding>;
23using HdStBindingRequestVector = std::vector<class HdStBindingRequest>;
24
31public:
32 enum Type { // primvar, drawing coordinate and dispatch buffer bindings
33 // also shader fallback values
34 UNKNOWN,
35 DISPATCH, // GL_DRAW_INDIRECT_BUFFER
36 DRAW_INDEX, // per-drawcall. not instanced
37 DRAW_INDEX_INSTANCE, // per-drawcall. attribdivisor=on
38 DRAW_INDEX_INSTANCE_ARRAY, // per-drawcall. attribdivisor=on, array
39 VERTEX_ATTR, // vertex-attribute
40 INDEX_ATTR, // GL_ELEMENT_ARRAY_BUFFER
41 SSBO, //
42 BINDLESS_SSBO_RANGE, //
43 UBO, //
44 BINDLESS_UNIFORM, //
45 UNIFORM, //
46 UNIFORM_ARRAY, //
47
48 // shader parameter bindings
49 FALLBACK, // fallback value
50 TEXTURE_2D, // non-bindless uv texture
51 ARRAY_OF_TEXTURE_2D, // non-bindless array of uv textures. Not
52 // to be confused with a texture array
53 // (what udim and ptex textures use).
54 TEXTURE_FIELD, // non-bindless field texture
55 // creates accessor that samples uvw
56 // texture after transforming coordinates
57 // by a sampling transform
58 TEXTURE_UDIM_ARRAY, // non-bindless udim texture array
59 TEXTURE_UDIM_LAYOUT, // non-bindless udim layout
60 TEXTURE_PTEX_TEXEL, // non-bindless ptex texels
61 TEXTURE_PTEX_LAYOUT, // non-bindless ptex layout
62 TEXTURE_CUBEMAP, // non-bindless cubemap texture
63 BINDLESS_TEXTURE_2D, // bindless uv texture
64 BINDLESS_ARRAY_OF_TEXTURE_2D, // bindless array of uv textures
65 BINDLESS_TEXTURE_FIELD, // bindless field texture
66 // (see above)
67 BINDLESS_TEXTURE_UDIM_ARRAY, // bindless uv texture array
68 BINDLESS_TEXTURE_UDIM_LAYOUT, // bindless udim layout
69 BINDLESS_TEXTURE_PTEX_TEXEL, // bindless ptex texels
70 BINDLESS_TEXTURE_PTEX_LAYOUT, // bindless ptex layout
71 BINDLESS_TEXTURE_CUBEMAP, // bindless cubemap texture
72 PRIMVAR_REDIRECT, // primvar redirection
73 FIELD_REDIRECT, // accesses a field texture by name and
74 // uses fallbackValue if no accessor for
75 // the texture exists.
76 TRANSFORM_2D // transform2d
77 };
78 enum Location {
79 // NOT_EXIST is a special value of location for a uniform
80 // which is assigned but optimized out after linking program.
81 NOT_EXIST = 0xffff
82 };
83 HdStBinding() : _typeAndLocation(-1) { }
84 HdStBinding(Type type, int location, int textureUnit=0) {
85 Set(type, location, textureUnit);
86 }
87 void Set(Type type, int location, int textureUnit) {
88 _typeAndLocation = (textureUnit << 24)|(location << 8)|(int)(type);
89 }
90 bool IsValid() const { return _typeAndLocation >= 0; }
91 Type GetType() const { return (Type)(_typeAndLocation & 0xff); }
92 int GetLocation() const { return (_typeAndLocation >> 8) & 0xffff; }
93 int GetTextureUnit() const { return (_typeAndLocation >> 24) & 0xff; }
94 int GetValue() const { return _typeAndLocation; }
95 bool operator < (HdStBinding const &b) const {
96 return (_typeAndLocation < b._typeAndLocation);
97 }
98private:
99 int _typeAndLocation;
100};
101
112public:
113
114 HdStBindingRequest() = default;
115
119 HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name)
120 : _bindingType(bindingType)
121 , _dataType(HdTypeInvalid)
122 , _name(name)
123 , _resource(nullptr)
124 , _bar(nullptr)
125 , _isInterleaved(false)
126 , _isWritable(false)
127 , _arraySize(0)
128 , _concatenateNames(false)
129 {}
130
133 HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name,
134 HdType dataType)
135 : _bindingType(bindingType)
136 , _dataType(dataType)
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 HdStBufferResourceSharedPtr const& resource)
150 : _bindingType(bindingType)
151 , _dataType(resource->GetTupleType().type)
152 , _name(name)
153 , _resource(resource)
154 , _bar(nullptr)
155 , _isInterleaved(false)
156 , _isWritable(false)
157 , _arraySize(0)
158 , _concatenateNames(false)
159 {}
160
166 HdStBindingRequest(HdStBinding::Type type, TfToken const& name,
167 HdBufferArrayRangeSharedPtr bar,
168 bool interleave, bool writable = false,
169 size_t arraySize = 0, bool concatenateNames = false)
170 : _bindingType(type)
171 , _dataType(HdTypeInvalid)
172 , _name(name)
173 , _resource(nullptr)
174 , _bar(bar)
175 , _isInterleaved(interleave)
176 , _isWritable(writable)
177 , _arraySize(arraySize)
178 , _concatenateNames(concatenateNames)
179 {}
180
181 // ---------------------------------------------------------------------- //
183 // ---------------------------------------------------------------------- //
184
187 bool IsResource() const {
188 return bool(_resource);
189 }
190
194 bool IsBufferArray() const {
195 return _bar && !_isInterleaved;
196 }
197
203 return _bar && _isInterleaved;
204 }
205
208 bool isWritable() const {
209 return _bar && _isWritable;
210 }
211
214 bool IsTypeless() const {
215 return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
216 }
217
218 // ---------------------------------------------------------------------- //
220 // ---------------------------------------------------------------------- //
221
224 TfToken const& GetName() const {
225 return _name;
226 }
228 HdStBinding::Type GetBindingType() const {
229 return _bindingType;
230 }
233 HdStBufferResourceSharedPtr const& GetResource() const {
234 return _resource;
235 }
237 int GetByteOffset() const {
238 // buffer resource binding
239 if (_resource) return _resource->GetOffset();
240
241 // named struct binding (interleaved) - the resource name doesn't matter
242 // since a single binding point is used.
243 if (_bar) return _bar->GetByteOffset(TfToken());
244 return 0;
245 }
248 HdBufferArrayRangeSharedPtr const& GetBar() const {
249 return _bar;
250 }
251
253 HdType GetDataType() const {
254 return _dataType;
255 }
256
258 size_t GetArraySize() const {
259 return _arraySize;
260 }
261
264 bool ConcatenateNames() const {
265 return _concatenateNames;
266 }
267
268 // ---------------------------------------------------------------------- //
270 // ---------------------------------------------------------------------- //
271 HDST_API
272 bool operator==(HdStBindingRequest const &other) const;
273
274 HDST_API
275 bool operator!=(HdStBindingRequest const &other) const;
276
277 // ---------------------------------------------------------------------- //
279 // ---------------------------------------------------------------------- //
280
286 HDST_API
287 size_t ComputeHash() const;
288
289 // TfHash support.
290 template <class HashState>
291 friend void TfHashAppend(HashState &h, HdStBindingRequest const &br) {
292 h.Append(br._name,
293 br._bindingType,
294 br._dataType,
295 br._isInterleaved);
296 }
297
298private:
299 // This class unfortunately represents several concepts packed into a single
300 // class. Ideally, we would break this out as one class per concept,
301 // however that would also require virtual dispatch, which is overkill for
302 // the current use cases.
303
304 // Named binding request
305 HdStBinding::Type _bindingType;
306 HdType _dataType;
307 TfToken _name;
308
309 // Resource binding request
310 HdStBufferResourceSharedPtr _resource;
311
312 // Struct binding request
313 HdBufferArrayRangeSharedPtr _bar;
314 bool _isInterleaved;
315
316 bool _isWritable;
317
318 size_t _arraySize;
319
320 bool _concatenateNames;
321};
322
323
324PXR_NAMESPACE_CLOSE_SCOPE
325
326#endif // PXR_IMAGING_HDST_BINDING_H
Bindings are used for buffers or textures, it simply associates a binding type with a binding locatio...
Definition: binding.h:30
BindingRequest allows externally allocated buffers to be bound at render time.
Definition: binding.h:111
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:119
int GetByteOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:237
bool IsTypeless() const
This binding is typelss.
Definition: binding.h:214
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition: binding.h:194
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdType dataType)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:133
bool ConcatenateNames() const
Returns whether the struct binding point and struct member names should be concatenated when codegen'...
Definition: binding.h:264
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition: binding.h:258
bool isWritable() const
True when the resource is being bound so that it can be written to.
Definition: binding.h:208
HdStBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition: binding.h:233
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition: binding.h:202
HDST_API size_t ComputeHash() const
Returns the hash corresponding to this buffer request.
HdStBinding::Type GetBindingType() const
Returns the HdStBinding type of this request.
Definition: binding.h:228
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:166
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdStBufferResourceSharedPtr const &resource)
A buffer resource binding.
Definition: binding.h:148
TfToken const & GetName() const
Returns the name of the binding point, if any; buffer arrays and structs need not be named.
Definition: binding.h:224
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition: binding.h:187
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:253
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition: binding.h:248
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71