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 BINDLESS_TEXTURE_2D, // bindless uv texture
63 BINDLESS_ARRAY_OF_TEXTURE_2D, // bindless array of uv textures
64 BINDLESS_TEXTURE_FIELD, // bindless field texture
65 // (see above)
66 BINDLESS_TEXTURE_UDIM_ARRAY, // bindless uv texture array
67 BINDLESS_TEXTURE_UDIM_LAYOUT, // bindless udim layout
68 BINDLESS_TEXTURE_PTEX_TEXEL, // bindless ptex texels
69 BINDLESS_TEXTURE_PTEX_LAYOUT, // bindless ptex layout
70 PRIMVAR_REDIRECT, // primvar redirection
71 FIELD_REDIRECT, // accesses a field texture by name and
72 // uses fallbackValue if no accessor for
73 // the texture exists.
74 TRANSFORM_2D // transform2d
75 };
76 enum Location {
77 // NOT_EXIST is a special value of location for a uniform
78 // which is assigned but optimized out after linking program.
79 NOT_EXIST = 0xffff
80 };
81 HdStBinding() : _typeAndLocation(-1) { }
82 HdStBinding(Type type, int location, int textureUnit=0) {
83 Set(type, location, textureUnit);
84 }
85 void Set(Type type, int location, int textureUnit) {
86 _typeAndLocation = (textureUnit << 24)|(location << 8)|(int)(type);
87 }
88 bool IsValid() const { return _typeAndLocation >= 0; }
89 Type GetType() const { return (Type)(_typeAndLocation & 0xff); }
90 int GetLocation() const { return (_typeAndLocation >> 8) & 0xffff; }
91 int GetTextureUnit() const { return (_typeAndLocation >> 24) & 0xff; }
92 int GetValue() const { return _typeAndLocation; }
93 bool operator < (HdStBinding const &b) const {
94 return (_typeAndLocation < b._typeAndLocation);
95 }
96private:
97 int _typeAndLocation;
98};
99
110public:
111
112 HdStBindingRequest() = default;
113
117 HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name)
118 : _bindingType(bindingType)
119 , _dataType(HdTypeInvalid)
120 , _name(name)
121 , _resource(nullptr)
122 , _bar(nullptr)
123 , _isInterleaved(false)
124 , _isWritable(false)
125 , _arraySize(0)
126 , _concatenateNames(false)
127 {}
128
131 HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name,
132 HdType dataType)
133 : _bindingType(bindingType)
134 , _dataType(dataType)
135 , _name(name)
136 , _resource(nullptr)
137 , _bar(nullptr)
138 , _isInterleaved(false)
139 , _isWritable(false)
140 , _arraySize(0)
141 , _concatenateNames(false)
142 {}
143
146 HdStBindingRequest(HdStBinding::Type bindingType, TfToken const& name,
147 HdStBufferResourceSharedPtr const& resource)
148 : _bindingType(bindingType)
149 , _dataType(resource->GetTupleType().type)
150 , _name(name)
151 , _resource(resource)
152 , _bar(nullptr)
153 , _isInterleaved(false)
154 , _isWritable(false)
155 , _arraySize(0)
156 , _concatenateNames(false)
157 {}
158
164 HdStBindingRequest(HdStBinding::Type type, TfToken const& name,
165 HdBufferArrayRangeSharedPtr bar,
166 bool interleave, bool writable = false,
167 size_t arraySize = 0, bool concatenateNames = false)
168 : _bindingType(type)
169 , _dataType(HdTypeInvalid)
170 , _name(name)
171 , _resource(nullptr)
172 , _bar(bar)
173 , _isInterleaved(interleave)
174 , _isWritable(writable)
175 , _arraySize(arraySize)
176 , _concatenateNames(concatenateNames)
177 {}
178
179 // ---------------------------------------------------------------------- //
181 // ---------------------------------------------------------------------- //
182
185 bool IsResource() const {
186 return bool(_resource);
187 }
188
192 bool IsBufferArray() const {
193 return _bar && !_isInterleaved;
194 }
195
201 return _bar && _isInterleaved;
202 }
203
206 bool isWritable() const {
207 return _bar && _isWritable;
208 }
209
212 bool IsTypeless() const {
213 return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
214 }
215
216 // ---------------------------------------------------------------------- //
218 // ---------------------------------------------------------------------- //
219
222 TfToken const& GetName() const {
223 return _name;
224 }
226 HdStBinding::Type GetBindingType() const {
227 return _bindingType;
228 }
231 HdStBufferResourceSharedPtr const& GetResource() const {
232 return _resource;
233 }
235 int GetByteOffset() const {
236 // buffer resource binding
237 if (_resource) return _resource->GetOffset();
238
239 // named struct binding (interleaved) - the resource name doesn't matter
240 // since a single binding point is used.
241 if (_bar) return _bar->GetByteOffset(TfToken());
242 return 0;
243 }
246 HdBufferArrayRangeSharedPtr const& GetBar() const {
247 return _bar;
248 }
249
251 HdType GetDataType() const {
252 return _dataType;
253 }
254
256 size_t GetArraySize() const {
257 return _arraySize;
258 }
259
262 bool ConcatenateNames() const {
263 return _concatenateNames;
264 }
265
266 // ---------------------------------------------------------------------- //
268 // ---------------------------------------------------------------------- //
269 HDST_API
270 bool operator==(HdStBindingRequest const &other) const;
271
272 HDST_API
273 bool operator!=(HdStBindingRequest const &other) const;
274
275 // ---------------------------------------------------------------------- //
277 // ---------------------------------------------------------------------- //
278
284 HDST_API
285 size_t ComputeHash() const;
286
287 // TfHash support.
288 template <class HashState>
289 friend void TfHashAppend(HashState &h, HdStBindingRequest const &br) {
290 h.Append(br._name,
291 br._bindingType,
292 br._dataType,
293 br._isInterleaved);
294 }
295
296private:
297 // This class unfortunately represents several concepts packed into a single
298 // class. Ideally, we would break this out as one class per concept,
299 // however that would also require virtual dispatch, which is overkill for
300 // the current use cases.
301
302 // Named binding request
303 HdStBinding::Type _bindingType;
304 HdType _dataType;
305 TfToken _name;
306
307 // Resource binding request
308 HdStBufferResourceSharedPtr _resource;
309
310 // Struct binding request
311 HdBufferArrayRangeSharedPtr _bar;
312 bool _isInterleaved;
313
314 bool _isWritable;
315
316 size_t _arraySize;
317
318 bool _concatenateNames;
319};
320
321
322PXR_NAMESPACE_CLOSE_SCOPE
323
324#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:109
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:117
int GetByteOffset() const
Returns the resource or buffer array range offset, defaults to zero.
Definition: binding.h:235
bool IsTypeless() const
This binding is typelss.
Definition: binding.h:212
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition: binding.h:192
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdType dataType)
A data binding, not backed by neither BufferArrayRange nor BufferResource.
Definition: binding.h:131
bool ConcatenateNames() const
Returns whether the struct binding point and struct member names should be concatenated when codegen'...
Definition: binding.h:262
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition: binding.h:256
bool isWritable() const
True when the resource is being bound so that it can be written to.
Definition: binding.h:206
HdStBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition: binding.h:231
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition: binding.h:200
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:226
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:164
HdStBindingRequest(HdStBinding::Type bindingType, TfToken const &name, HdStBufferResourceSharedPtr const &resource)
A buffer resource binding.
Definition: binding.h:146
TfToken const & GetName() const
Returns the name of the binding point, if any; buffer arrays and structs need not be named.
Definition: binding.h:222
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition: binding.h:185
HdType GetDataType() const
Return the data type of this request.
Definition: binding.h:251
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition: binding.h:246
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71