HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_ShaderVariableSet.h
Go to the documentation of this file.
1 
2 /*
3  * PROPRIETARY INFORMATION. This software is proprietary to
4  * Side Effects Software Inc., and is not to be reproduced,
5  * transmitted, or disclosed in any way without written permission.
6  *
7  * NAME: RV_ShaderVariableSet.h ( RV Library, C++)
8  *
9  * COMMENTS:
10  * Class to handle creating a set of Variables to be bound alongisde a
11  * shader, i.e. a Descriptor Set
12  *
13  * TODO: maybe better name? RV_ShaderParamSet ?
14  *
15  */
16 
17 #ifndef RV_ShaderVariableSet_h
18 #define RV_ShaderVariableSet_h
19 
20 #include "RV_API.h"
21 
22 #include <UT/UT_Array.h>
23 #include <UT/UT_ArrayStringMap.h>
24 #include <UT/UT_UniquePtr.h>
25 #include <UT/UT_StringHolder.h>
26 #include <UT/UT_StringMap.h>
27 
28 #include <utility>
29 
30 #include "RV_VKDescriptorSet.h"
31 
32 class RV_Instance;
33 class RV_Render;
34 class RV_ShaderBlock;
36 class RV_VKCommandBuffer;
37 class RV_VKBuffer;
38 class RV_VKImage;
39 
40 // ~~~~~~~~~~
41 // RV_ShaderVariableSet
42 //
43 // High level class for variable bindings used in shaders (i.e. desciptor sets)
44 // Stores the layout, the values.
45 //
46 // Mutable and not thread safe
47 
48 /// A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
50 {
51 public:
53  RV_Instance* inst,
54  const RV_VKDescriptorSetInfo* layout,
56  const char* name = nullptr);
58 
60  RV_Instance* inst,
61  const RV_VKDescriptorSetInfo* layout,
62  const RV_ShaderVariableSet* opt_base = nullptr,
63  const char* name = nullptr);
64 
65  /// Returns true if binding named 'name' exists
66  bool hasBinding(const UT_StringRef& name) const;
67  /// The list of bindings
68  const UT_Array<RV_VKDescriptorBinding>& getBindings() const;
69  /// Return a single binding by index
70  const RV_VKDescriptorBinding& getBinding(uint32_t binding_num) const;
71  /// Return a single binding by name
72  RV_VKDescriptorBinding getBinding(const UT_StringRef& name) const;
73 
74  /// The raw Vulkan descriptor set which this set represents.
75  VkDescriptorSet getVkDescriptorSet();
76  /// The raw Vulkan descritor set create info
78  { return *myLayout.get(); }
79 
80  // --------------------------------
81  // Bound Resources
82  // TODO: make return value ptr, instead of output var?
83 
84  /// Fetch a bound UBO or SSBO with the given name (may be null)
85  /// Return false if the binding doesn't exist or isn't a buffer object.
86  bool getBufferBlock(const UT_StringRef& name, RV_ShaderBlock*& out_block);
87  /// Fetch a bound UBO or SSBO with the given name (may be null)
88  /// Return false if the binding doesn't exist or isn't a buffer object.
89  bool getBufferBlock(const UT_StringRef& name, const RV_ShaderBlock*&) const;
90  /// Fetch a bound UBO or SSBO by index (may be null).
91  /// Return false if the binding doesn't exist or isn't a buffer object.
92  bool getBufferBlock(int binding_num, RV_ShaderBlock*& out_block);
93  /// Fetch a bound UBO or SSBO by index (may be null).
94  /// Return false if the binding doesn't exist or isn't a buffer object.
95  bool getBufferBlock(int binding_num, const RV_ShaderBlock*& out_block)
96  const;
97 
98  /// Fetch a bound texture with the given name (may be null)
99  /// Return false if the binding doesn't exist or isn't a texture.
100  bool getTexture(const UT_StringRef& name, RV_VKImage*& out_image);
101  /// Fetch a bound texture with the given name (may be null)
102  /// Return false if the binding doesn't exist or isn't a texture.
103  bool getTexture(const UT_StringRef& name, const RV_VKImage*&) const;
104  /// Fetch a bound texture by index (may be null)
105  /// Return false if the binding doesn't exist or isn't a texture.
106  bool getTexture(int binding_num, RV_VKImage*& out_image);
107  /// Fetch a bound texture by index (may be null)
108  /// Return false if the binding doesn't exist or isn't a texture.
109  bool getTexture(int binding_num, const RV_VKImage*& out_image) const;
110 
111  /// Fetch a bound buffer view (TBO in GL) by the given name (may be null)
112  /// Return false if the binding doesn't exist or isn't a buffer view.
113  bool getBufferView(const UT_StringRef& name, RV_VKBuffer*& out_buffer);
114  /// Fetch a bound buffer view (TBO in GL) by the given name (may be null)
115  /// Return false if the binding doesn't exist or isn't a buffer view.
116  bool getBufferView(const UT_StringRef& name, const RV_VKBuffer*&) const;
117  /// Fetch a bound buffer view (TBO in GL) by index (may be null)
118  /// Return false if the binding doesn't exist or isn't a buffer view.
119  bool getBufferView(int binding_num, RV_VKBuffer*& out_buffer);
120  /// Fetch a bound buffer view (TBO in GL) by index (may be null)
121  /// Return false if the binding doesn't exist or isn't a buffer view.
122  bool getBufferView(int binding_num, const RV_VKBuffer*& out_buffer) const;
123 
124  // Must attach buffers before using to draw
125  // Marks the buffer as used by the Descriptor Set
126  // Will wait until used for a draw to actually update the set
127 
128  /// Attach a UBO or SSBO to this variable set. Returns false if the binding
129  /// name doesn't exist, the block is null, or it isn't compatible with the
130  /// layout of the binding.
131  bool attachBufferBlock(
132  RV_Instance* inst,
133  const UT_StringRef& name,
134  RV_ShaderBlock* buffer_block,
135  int index = 0);
136  /// Attach a UBO or SSBO to this variable set. Returns false if the binding
137  /// index doesn't exist, the block is null, or it isn't compatible with the
138  /// layout of the binding.
139  bool attachBufferBlock(
140  RV_Instance* inst,
141  int binding_num,
142  RV_ShaderBlock* buffer_block,
143  int index = 0);
144 
145  /// Attach a buffer view (TBO in GL) to this variable set. Returns false if
146  /// the binding name doesn't exist, the buffer is null, or it isn't
147  /// compatible with the layout of the binding.
148  bool attachBufferView(
149  RV_Instance* inst,
150  const UT_StringRef& name,
152  int index = 0);
153  /// Attach a buffer view (TBO in GL) to this variable set. Returns false if
154  /// the binding index doesn't exist, the buffer is null, or it isn't
155  /// compatible with the layout of the binding.
156  bool attachBufferView(
157  RV_Instance* inst,
158  int binding_num,
160  int index = 0);
161 
162  /// Attach a texture or image to this variable set. Returns false if
163  /// the binding name doesn't exist, the image is null, or it isn't
164  /// compatible with the sampler type of the binding.
165  bool attachTexture(
166  RV_Instance* inst,
167  const UT_StringRef& name,
168  RV_VKImage* image,
169  int index = 0);
170  /// Attach a texture or image to this variable set. Returns false if
171  /// the binding index doesn't exist, the image is null, or it isn't
172  /// compatible with the sampler type of the binding.
173  bool attachTexture(
174  RV_Instance* inst,
175  int binding_num,
176  RV_VKImage* image,
177  int index = 0);
178 
179  /// Clear a bound UBO, SSBO, buffer view, or texture from binding 'name'
180  void clearBinding(RV_Render* inst, const UT_StringRef& name);
181  /// Clear a bound UBO, SSBO, buffer view, or texture from the binding index
182  void clearBinding(RV_Render* inst, int binding_num);
183 
184  /// Take any pending bindings and write them to the descriptor set
185  void commitBindings(RV_Instance* inst, RV_VKCommandBuffer* cb);
186 
187  // check that all bindings have been assigned
188  // -- also must check that all buffers/images are valid (have memory backing them, have not been deleted)
189  //
190 
191  /// Return true if all bindings have been assigned to the set
192  bool isReady(RV_Instance* inst) const;
193 
194  /// Return true if the set has been modified since the last commit
195  inline bool isDirty() const;
196 
197  // Shader Layout info
198 
199  /// Return if this variable set is compatible with the given pipeline binding
200  /// layout of the shader.
201  bool isCompatibleToBind(const RV_VKDescriptorSetInfo& pipe_layout) const;
202 
203 
204  /// Return the binding number of the set that this set was created with.
205  inline int getSetNumber() const
206  { return mySetNumber; }
207 
208  // -------------------------------------------------------------------
209  // DEBUG
210 
211  /// Debug dump to stderr all information about the bindings
212  void print() const;
213 
214 private:
215  friend class RV_Render;
216 
217  bool privGetBinding(
218  const UT_StringRef& name,
219  int& out_binding_num,
220  const RV_VKDescriptorBinding*& out_binding) const;
221 
223  privWriteNewSet(
224  RV_Instance* inst,
225  const RV_VKDescriptorSet* opt_base);
226 
227 
228  // Only called by RV_Render
229  bool bindSet(
230  RV_Instance* r,
231  RV_VKCommandBuffer* cb,
233 
234  /// Pipeline Layout for shader
236  UT_StringHolder myName;
237  int mySetNumber;
238 
239  // Uniform Blocks:
240  /// Mapping of uniform names, to indices in myUniforms
241  UT_ArrayStringMap<int> myUniformTable;
242  UT_Array<const RV_Uniform*> myUniforms;
243 
244  // Bindings:
245  // Mapping of Binding Names to Binding number in the layout
246  UT_ArrayStringMap<int> myBindingTable;
247  UT_Array<int> myBindingResourceIdx;
248 
252 
253  enum RV_BindingType
254  {
255  RV_BINDING_UNKNOWN = 0,
256  RV_BINDING_BLOCK,
257  RV_BINDING_BUFFER,
258  RV_BINDING_IMAGE,
259  };
260 
261  // Simple variant/union-like struct
262  struct BindingResource
263  {
264  BindingResource(RV_BindingType type,
265  RV_ShaderBlock* block,
268  : myType(type)
269  , myBlock(block)
270  , myBuffer(buffer)
271  , myTexture(texture)
272  {}
273 
274  BindingResource(RV_ShaderBlock* v)
275  : myType(RV_BINDING_BLOCK)
276  , myBlock(v)
277  {}
278 
279  BindingResource(RV_VKBuffer* v)
280  : myType(RV_BINDING_BUFFER)
281  , myBuffer(v)
282  {}
283 
284  BindingResource(RV_VKImage* v)
285  : myType(RV_BINDING_IMAGE)
286  , myTexture(v)
287  {}
288 
289  RV_BindingType myType = RV_BINDING_UNKNOWN;
290  RV_ShaderBlock* myBlock = nullptr;
291  RV_VKBuffer* myBuffer = nullptr;
292  RV_VKImage* myTexture = nullptr;
293  };
294  // Extra Bindings -- Attached to the set by name but not
295  // part of the layout, so will not be bound as part of our set
296  // keep to copy into other sets if we transfer our resources
297  void privAttachExtraResource(const UT_StringHolder& name,
298  const BindingResource &res);
299  UT_StringMap<BindingResource> myExtraBindings;
300 
301  // Descriptor Sets:
302  // - DescriptorSets: Current descriptor sets
303  // - PendingUpdates: Updates to set that will be commited before use
304 
305  /// Our current up-to-date VK_DescriptorSet object
306  UT_UniquePtr<RV_VKDescriptorSet> myDescriptorSet;
307  UT_UniquePtr<RV_VKDescriptorLayout> myDescriptorLayout;
308 
309  /// pending writes for descriptor set
311  bool myIsSetDirty = false;
312 };
313 #endif
314 
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
const GLdouble * v
Definition: glcorearb.h:837
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
const RV_VKDescriptorSetInfo & getSetInfo() const
The raw Vulkan descritor set create info.
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
Definition: core.h:760
#define RV_API
Definition: RV_API.h:10
GLuint const GLchar * name
Definition: glcorearb.h:786
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:36
int getSetNumber() const
Return the binding number of the set that this set was created with.
GLuint shader
Definition: glcorearb.h:785
GLuint index
Definition: glcorearb.h:786
GLuint texture
Definition: glcorearb.h:415
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:80
type
Definition: core.h:1059
FMT_INLINE void print(format_string< T...> fmt, T &&...args)
Definition: core.h:2976
bool bindSet(RV_ShaderVariableSet *set, const RV_ShaderProgramBase *shr)
Bind a variable set to the specific shader program.