HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_VKBuffer.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RV_Instance.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Class to manage Vulkan Buffer object and allocated memory
10  */
11 
12 #ifndef RV_VKBuffer_h
13 #define RV_VKBuffer_h
14 
15 #include "RV_API.h"
16 
17 #include <UT/UT_UniquePtr.h>
18 #include <UT/UT_Span.h>
19 
20 #include <VE/VE_VK.h>
21 #include "RV_Instance.h"
22 #include "RV_Type.h"
23 #include "RV_TypePtrs.h"
24 
25 class RV_Render;
26 class RV_VKMemory;
27 
28 // TODO: make into class ?
30 
31 /// RAII wrapper class for VkBufferView
33 {
34 public:
35  VkBufferView getVkView() { return myVkView; }
36  VkFormat getVkFormat() const { return myVkFormat; }
37 
38  // CAN have null state
40  : myInst(nullptr)
41  , myVkView(VK_NULL_HANDLE)
42  , myVkFormat(VK_FORMAT_UNDEFINED)
43  {}
44  RV_VKBufferView(RV_Instance* inst, VkBufferView vk_view, VkFormat vk_format)
45  : myInst(inst), myVkView(vk_view), myVkFormat(vk_format)
46  {}
47 
48  // disable copy semantics
49  RV_VKBufferView(const RV_VKBufferView&) = delete;
51 
52  // enable move semantics
53  RV_VKBufferView(RV_VKBufferView&& other) noexcept
54  : myInst(other.myInst)
55  , myVkView(other.myVkView)
56  , myVkFormat(other.myVkFormat)
57  {
58  other.myInst = nullptr;
59  other.myVkView = VK_NULL_HANDLE;
60  other.myVkFormat = VK_FORMAT_UNDEFINED;
61  }
62  RV_VKBufferView& operator=(RV_VKBufferView&& other) noexcept = delete;
63 
64  // Destroy Resource Handle on destruction
66  {
67  if (myVkView != VK_NULL_HANDLE)
68  {
69  VkDevice vk_dev = myInst->getDevice();
70  ::vkDestroyBufferView(vk_dev, myVkView, nullptr);
71  }
72  }
73 
74 private:
75  RV_Instance* myInst = nullptr;
76  VkBufferView myVkView = VK_NULL_HANDLE;
77  VkFormat myVkFormat = VK_FORMAT_UNDEFINED;
78 };
79 
80 /// @brief A vulkan buffer object
82 {
83 public:
84 
85  /// @brief Allocate a new Buffer object
86  /// Allocate a vulkan buffer
87  /// `is_staging` - if true, buffer will be CPU visible
88  ///
89  /// `vk_format` - if provided, a BufferView is only created --
90  /// only needed for texel buffer usage
91  static RV_VKBufferPtr allocate( RV_Instance* inst,
92  exint size,
94  bool is_staging,
95  VkFormat vk_format = VK_FORMAT_UNDEFINED,
96  const char* name = nullptr,
97  exint alignment = 0);
98 
99  /// @brief Allocate a new Buffer object
100  /// Allocate a vulkan buffer
101  /// `mem_type` - Memory type to use
102  ///
103  /// `vk_format` - if provided, a BufferView is only created --
104  /// only needed for texel buffer usage
105  static RV_VKBufferPtr allocate( RV_Instance* inst,
106  exint size,
107  VkBufferUsageFlags usage,
108  RV_MemType mem_type,
109  VkFormat vk_format = VK_FORMAT_UNDEFINED,
110  const char* name = nullptr,
111  exint alignment = 0);
112 
113  /// Get the raw vulkan buffer handle
114  VkBuffer getVkBuf() { return myVkBuf; }
115  /// Get the create info the buffer was created with
116  const RV_VKBufferCreateInfo& getInfo() { return *myCreateInfo; }
117  /// Get the memory the buffer is backed by
118  RV_VKMemory& getMemory() { return *myMemory; }
119  const RV_VKMemory& getMemory() const { return *myMemory; }
120  /// Return the memory size this buffer uses, in bytes
121  exint gpuMemoryUsage() const;
122  /// Return unique ID for buffer object
123  const RV_ResourceID &getID() const { return myId; }
124  /// Return label for buffer object
125  const UT_StringHolder &getName() const { return myName; }
126 
127  /// Allocate owned buffer view for use as texel Buffer. Does nothing
128  /// if it is already allocated
129  void generateVkView(RV_Instance* inst);
130 
131  /// Fetch the buffer view assigned to this buffer, if any
132  VkBufferView getVkView()
133  {
134  return myView ? myView->getVkView()
135  : VK_NULL_HANDLE;
136  }
137  /// Return the vulkan format of the buffer.
139  {
140  return myView ? myView->getVkFormat()
142  }
143  /// Return the size of the buffer, in bytes
144  exint getSize() const { return myCreateInfo->size; }
145  /// return the vulkan usage of the buffer when it was created
146  VkBufferUsageFlags getUsage() const { return myCreateInfo->usage; }
147  /// return the current usage of this buffer
148  VkBufferUsageFlags getLastUsage() const { return myLastUsage; }
149 
150  /// Number of elements in this buffer (eg. RGB 32b is 1 element)
151  exint entries() const;
152  /// Return the RV type of the buffer
153  RV_GPUType getGPUType() const;
154  /// Return the vector size of the buffer (1-4)
155  int getVectorSize() const;
156  /// Return the device address of the buffer
157  VkDeviceAddress getDeviceAddress() const;
158 
159  /// @brief Upload data to the buffer
160  /// Upload data to the buffer and add a barrier based on its usage.
161  /// Returns true if the size+offset doesn't exceed the allocated size.
162  bool uploadData(RV_Render* r,
163  VkBufferUsageFlags new_usage,
164  const void* data,
165  exint data_size,
166  exint data_offset = 0);
167  /// @brief Upload data to the buffer
168  /// Upload data to the buffer. Returns true if the size+offset doesn't
169  /// exceed the allocated size.
170  bool uploadData(RV_Render* r,
171  const void* data,
172  exint data_size,
173  exint data_offset = 0);
174 
175  /// Span version of buffer upload
177  bool uploadData(RV_Render* r, const T &data)
178  {
179  // `T` must be a `UT_Span` or convertable into a `UT_Span`
181  return uploadData(r, span.data(), span.size_bytes(), 0);
182  }
183 
185  bool uploadData(RV_Render* r, VkBufferUsageFlags new_usage, const T &data)
186  {
187  // `T` must be a `UT_Span` or convertable into a `UT_Span`
189  return uploadData(r, new_usage, span.data(), span.size_bytes(), 0);
190  }
191 
192  /// return whether buffer was declared as CPU visible, and mapped
193  bool isMappedMemory() const;
194 
195  /// Get a CPU mapped ptr to the raw memory. Will return nullptr
196  /// if buffer wasn't declared as CPU visible
197  void* getMappedMemory();
198 
199  /// Download data from the buffer into `buf`
200  bool copyData(RV_Render* r,
201  RV_VKBuffer* buf,
202  exint src_offset = 0,
203  exint dst_offset = 0,
204  exint sublen = -1);
205 
206  /// Donwload data from the buffer to `data`. Returns false if the operation
207  /// would read data out-of-range or there was an error.
208  bool downloadData( RV_Render* r,
209  void* data,
210  exint data_size,
211  exint offset = 0,
212  exint sublen = -1);
213 
214  /// Clear the buffer with 0.
215  void clear(RV_Render *r);
216 
217  /// Add a barrier for using the buffer in the future
219  { addBarrier(r, myLastUsage, new_usage); }
220  /// Add a barrier for using the buffer in the future, changing its usage
221  void addBarrier(RV_Render* r, VkBufferUsageFlags old_usage,
222  VkBufferUsageFlags new_usage);
223 
224  virtual ~RV_VKBuffer();
225 
226  /// Debug output of the buffer (but not its data).
227  void print() const;
228 
229 private:
230  struct PassKey {};
231 public:
232  RV_VKBuffer(RV_Instance* inst,
233  const RV_VKBufferCreateInfo* info,
234  VkBuffer vk_buf,
235  RV_VKMemoryPtr mem,
236  RV_VKBufferViewPtr view,
237  const PassKey&);
238 protected:
239  RV_Instance* myInst = nullptr;
240 
241  VkBuffer myVkBuf = VK_NULL_HANDLE;
245 
249 
250 public:
251  RV_StageGroup myWaitingBarrierStage = RV_STAGE_NONE;
252  exint myWaitingBarrierGroupID = -1;
253 };
254 
255 #endif
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
VkFormat getVkFormat() const
Definition: RV_VKBuffer.h:36
VkBuffer getVkBuf()
Get the raw vulkan buffer handle.
Definition: RV_VKBuffer.h:114
#define VK_NULL_HANDLE
Definition: vulkan_core.h:45
VkFlags VkBufferUsageFlags
Definition: vulkan_core.h:2515
constexpr span< ElementType, Extent > make_span(span< ElementType, Extent > s) noexcept
Definition: UT_Span.h:559
UT_UniquePtr< RV_VKBuffer > RV_VKBufferPtr
Definition: RV_TypePtrs.h:63
const RV_VKBufferCreateInfo & getInfo()
Get the create info the buffer was created with.
Definition: RV_VKBuffer.h:116
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Definition: span.h:74
int64 exint
Definition: SYS_Types.h:125
RV_StageGroup
Definition: RV_Type.h:508
VkBufferView getVkView()
Fetch the buffer view assigned to this buffer, if any.
Definition: RV_VKBuffer.h:132
UT_UniquePtr< RV_VKMemory > RV_VKMemoryPtr
Definition: RV_TypePtrs.h:50
bool uploadData(RV_Render *r, VkBufferUsageFlags new_usage, const T &data)
Definition: RV_VKBuffer.h:185
exint getSize() const
Return the size of the buffer, in bytes.
Definition: RV_VKBuffer.h:144
UT_UniquePtr< RV_VKMemory > myMemory
Definition: RV_VKBuffer.h:247
VkBufferView getVkView()
Definition: RV_VKBuffer.h:35
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
UT_StringHolder myName
Definition: RV_VKBuffer.h:243
VkBufferUsageFlags myLastUsage
Definition: RV_VKBuffer.h:248
UT_UniquePtr< RV_VKBufferView > myView
Definition: RV_VKBuffer.h:246
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
GLintptr offset
Definition: glcorearb.h:665
UT_UniquePtr< const RV_VKBufferCreateInfo > myCreateInfo
Definition: RV_VKBuffer.h:244
#define RV_API
Definition: RV_API.h:10
const UT_StringHolder & getName() const
Return label for buffer object.
Definition: RV_VKBuffer.h:125
bool uploadData(RV_Render *r, const T &data)
Span version of buffer upload.
Definition: RV_VKBuffer.h:177
RV_VKMemory & getMemory()
Get the memory the buffer is backed by.
Definition: RV_VKBuffer.h:118
const RV_ResourceID & getID() const
Return unique ID for buffer object.
Definition: RV_VKBuffer.h:123
GLuint const GLchar * name
Definition: glcorearb.h:786
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:44
RV_VKBufferView(RV_VKBufferView &&other) noexcept
Definition: RV_VKBuffer.h:53
VkBufferUsageFlags getLastUsage() const
return the current usage of this buffer
Definition: RV_VKBuffer.h:148
VkFormat getVkFormat() const
Return the vulkan format of the buffer.
Definition: RV_VKBuffer.h:138
GLsizeiptr size
Definition: glcorearb.h:664
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
VKAPI_ATTR void VKAPI_CALL vkDestroyBufferView(VkDevice device, VkBufferView bufferView, const VkAllocationCallbacks *pAllocator)
RV_ResourceID myId
Definition: RV_VKBuffer.h:242
RV_GPUType
Definition: RV_Type.h:40
constexpr size_type size_bytes() const noexcept
Definition: span.h:187
VkFormat
Definition: vulkan_core.h:1386
VkBufferUsageFlags getUsage() const
return the vulkan usage of the buffer when it was created
Definition: RV_VKBuffer.h:146
VkDevice getDevice()
Get the raw vulkan device assocated with this instance.
RV_MemType
Definition: RV_Type.h:111
GLboolean r
Definition: glcorearb.h:1222
UT_UniquePtr< RV_VKBufferView > RV_VKBufferViewPtr
Definition: RV_TypePtrs.h:64
A vulkan buffer object.
Definition: RV_VKBuffer.h:81
RAII wrapper class for VkBufferView.
Definition: RV_VKBuffer.h:32
const RV_VKMemory & getMemory() const
Definition: RV_VKBuffer.h:119
RV_VKBufferView & operator=(RV_VKBufferView &)=delete
VkBufferCreateInfo RV_VKBufferCreateInfo
Definition: RV_VKBuffer.h:26
FMT_INLINE void print(format_string< T...> fmt, T &&...args)
Definition: core.h:2903
uint64_t VkDeviceAddress
Definition: vulkan_core.h:94
constexpr pointer data() const noexcept
Definition: span.h:190
RV_VKBufferView(RV_Instance *inst, VkBufferView vk_view, VkFormat vk_format)
Definition: RV_VKBuffer.h:44
void addBarrier(RV_Render *r, VkBufferUsageFlags new_usage)
Add a barrier for using the buffer in the future.
Definition: RV_VKBuffer.h:218
Definition: format.h:1821