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 HgiShaderStage stageVisibility = HgiShaderStageAll)
171 : _bindingType(type)
172 , _dataType(HdTypeInvalid)
173 , _name(name)
174 , _resource(nullptr)
175 , _bar(bar)
176 , _isInterleaved(interleave)
177 , _isWritable(writable)
178 , _arraySize(arraySize)
179 , _concatenateNames(concatenateNames)
180 , _stageVisibility(stageVisibility)
181 {}
182
183 // ---------------------------------------------------------------------- //
185 // ---------------------------------------------------------------------- //
186
189 bool IsResource() const {
190 return bool(_resource);
191 }
192
196 bool IsBufferArray() const {
197 return _bar && !_isInterleaved;
198 }
199
205 return _bar && _isInterleaved;
206 }
207
210 bool isWritable() const {
211 return _bar && _isWritable;
212 }
213
216 bool IsTypeless() const {
217 return (!_bar) && (!_resource) && (_dataType == HdTypeInvalid);
218 }
219
220 // ---------------------------------------------------------------------- //
222 // ---------------------------------------------------------------------- //
223
226 TfToken const& GetName() const {
227 return _name;
228 }
230 HdStBinding::Type GetBindingType() const {
231 return _bindingType;
232 }
235 HdStBufferResourceSharedPtr const& GetResource() const {
236 return _resource;
237 }
239 int GetByteOffset() const {
240 // buffer resource binding
241 if (_resource) return _resource->GetOffset();
242
243 // named struct binding (interleaved) - the resource name doesn't matter
244 // since a single binding point is used.
245 if (_bar) return _bar->GetByteOffset(TfToken());
246 return 0;
247 }
250 HdBufferArrayRangeSharedPtr const& GetBar() const {
251 return _bar;
252 }
253
255 HdType GetDataType() const {
256 return _dataType;
257 }
258
260 size_t GetArraySize() const {
261 return _arraySize;
262 }
263
266 bool ConcatenateNames() const {
267 return _concatenateNames;
268 }
269
271 HgiShaderStage GetStageVisibility() const {
272 return _stageVisibility;
273 }
274
275 // ---------------------------------------------------------------------- //
277 // ---------------------------------------------------------------------- //
278 HDST_API
279 bool operator==(HdStBindingRequest const &other) const;
280
281 HDST_API
282 bool operator!=(HdStBindingRequest const &other) const;
283
284 // ---------------------------------------------------------------------- //
286 // ---------------------------------------------------------------------- //
287
293 HDST_API
294 size_t ComputeHash() const;
295
296 // TfHash support.
297 template <class HashState>
298 friend void TfHashAppend(HashState &h, HdStBindingRequest const &br) {
299 h.Append(br._name,
300 br._bindingType,
301 br._dataType,
302 br._isInterleaved);
303 }
304
305private:
306 // This class unfortunately represents several concepts packed into a single
307 // class. Ideally, we would break this out as one class per concept,
308 // however that would also require virtual dispatch, which is overkill for
309 // the current use cases.
310
311 // Named binding request
312 HdStBinding::Type _bindingType;
313 HdType _dataType;
314 TfToken _name;
315
316 // Resource binding request
317 HdStBufferResourceSharedPtr _resource;
318
319 // Struct binding request
320 HdBufferArrayRangeSharedPtr _bar;
321 bool _isInterleaved;
322
323 bool _isWritable;
324
325 size_t _arraySize;
326
327 bool _concatenateNames;
328
329 HgiShaderStage _stageVisibility;
330};
331
332
333PXR_NAMESPACE_CLOSE_SCOPE
334
335#endif // PXR_IMAGING_HDST_BINDING_H
virtual int GetByteOffset(TfToken const &resourceName) const =0
Returns the byte offset at which this range begins in the underlying buffer array for the given resou...
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:239
bool IsTypeless() const
This binding is typelss.
Definition binding.h:216
bool IsBufferArray() const
A buffer array binding has several buffers bundled together and each buffer will be bound individuall...
Definition binding.h:196
HgiShaderStage GetStageVisibility() const
Returns the stage visibility of the binding.
Definition binding.h:271
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:266
size_t GetArraySize() const
Array size if request is for an array of structs.
Definition binding.h:260
bool isWritable() const
True when the resource is being bound so that it can be written to.
Definition binding.h:210
HdStBufferResourceSharedPtr const & GetResource() const
Returns the single resource associated with this binding request or null when IsResource() returns fa...
Definition binding.h:235
bool IsInterleavedBufferArray() const
Like BufferArray binding requests, struct bindings have several buffers, however they must be allocat...
Definition binding.h:204
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:230
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:226
bool IsResource() const
Resource bingings have a single associated Hydra resource, but no buffer array.
Definition binding.h:189
HdType GetDataType() const
Return the data type of this request.
Definition binding.h:255
HdBufferArrayRangeSharedPtr const & GetBar() const
Returns the buffer array range associated with this binding request or null when IsBufferArrqay() ret...
Definition binding.h:250
HdStBindingRequest(HdStBinding::Type type, TfToken const &name, HdBufferArrayRangeSharedPtr bar, bool interleave, bool writable=false, size_t arraySize=0, bool concatenateNames=false, HgiShaderStage stageVisibility=HgiShaderStageAll)
A named struct binding.
Definition binding.h:166
int GetOffset() const
Returns the interleaved offset (in bytes) of the data.
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81