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  /// Fetch the buffer view assigned to this buffer, if any
128  VkBufferView getVkView()
129  {
130  return myView ? myView->getVkView()
131  : VK_NULL_HANDLE;
132  }
133  /// Return the vulkan format of the buffer.
135  {
136  return myView ? myView->getVkFormat()
138  }
139  /// Return the size of the buffer, in bytes
140  exint getSize() const { return myCreateInfo->size; }
141  /// return the vulkan usage of the buffer when it was created
142  VkBufferUsageFlags getUsage() const { return myCreateInfo->usage; }
143  /// return the current usage of this buffer
144  VkBufferUsageFlags getLastUsage() const { return myLastUsage; }
145 
146  /// Number of elements in this buffer (eg. RGB 32b is 1 element)
147  exint entries() const;
148  /// Return the RV type of the buffer
149  RV_GPUType getGPUType() const;
150  /// Return the vector size of the buffer (1-4)
151  int getVectorSize() const;
152  /// Return the device address of the buffer
153  VkDeviceAddress getDeviceAddress() const;
154 
155  /// @brief Upload data to the buffer
156  /// Upload data to the buffer and add a barrier based on its usage.
157  /// Returns true if the size+offset doesn't exceed the allocated size.
158  bool uploadData(RV_Render* r,
159  VkBufferUsageFlags new_usage,
160  const void* data,
161  exint data_size,
162  exint data_offset = 0);
163  /// @brief Upload data to the buffer
164  /// Upload data to the buffer. Returns true if the size+offset doesn't
165  /// exceed the allocated size.
166  bool uploadData(RV_Render* r,
167  const void* data,
168  exint data_size,
169  exint data_offset = 0);
170 
171  /// Span version of buffer upload
173  bool uploadData(RV_Render* r, const T &data)
174  {
175  // `T` must be a `UT_Span` or convertable into a `UT_Span`
177  return uploadData(r, span.data(), span.size_bytes(), 0);
178  }
179 
181  bool uploadData(RV_Render* r, VkBufferUsageFlags new_usage, const T &data)
182  {
183  // `T` must be a `UT_Span` or convertable into a `UT_Span`
185  return uploadData(r, new_usage, span.data(), span.size_bytes(), 0);
186  }
187 
188  /// return whether buffer was declared as CPU visible, and mapped
189  bool isMappedMemory() const;
190 
191  /// Get a CPU mapped ptr to the raw memory. Will return nullptr
192  /// if buffer wasn't declared as CPU visible
193  void* getMappedMemory();
194 
195  /// Download data from the buffer into `buf`
196  bool copyData(RV_Render* r,
197  RV_VKBuffer* buf,
198  exint src_offset = 0,
199  exint dst_offset = 0,
200  exint sublen = -1);
201 
202  /// Donwload data from the buffer to `data`. Returns false if the operation
203  /// would read data out-of-range or there was an error.
204  bool downloadData( RV_Render* r,
205  void* data,
206  exint data_size,
207  exint offset = 0,
208  exint sublen = -1);
209 
210  /// Clear the buffer with 0.
211  void clear(RV_Render *r);
212 
213  /// Add a barrier for using the buffer in the future
215  { addBarrier(r, myLastUsage, new_usage); }
216  /// Add a barrier for using the buffer in the future, changing its usage
217  void addBarrier(RV_Render* r, VkBufferUsageFlags old_usage,
218  VkBufferUsageFlags new_usage);
219 
220  virtual ~RV_VKBuffer();
221 
222  /// Debug output of the buffer (but not its data).
223  void print() const;
224 
225 private:
226  struct PassKey {};
227 public:
228  RV_VKBuffer(RV_Instance* inst,
229  const RV_VKBufferCreateInfo* info,
230  VkBuffer vk_buf,
231  RV_VKMemoryPtr mem,
232  RV_VKBufferViewPtr view,
233  const PassKey&);
234 protected:
235  RV_Instance* myInst = nullptr;
236 
237  VkBuffer myVkBuf = VK_NULL_HANDLE;
241 
245 
246 public:
247  RV_StageGroup myWaitingBarrierStage = RV_STAGE_NONE;
248  exint myWaitingBarrierGroupID = -1;
249 };
250 
251 #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:128
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:181
exint getSize() const
Return the size of the buffer, in bytes.
Definition: RV_VKBuffer.h:140
UT_UniquePtr< RV_VKMemory > myMemory
Definition: RV_VKBuffer.h:243
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:239
VkBufferUsageFlags myLastUsage
Definition: RV_VKBuffer.h:244
UT_UniquePtr< RV_VKBufferView > myView
Definition: RV_VKBuffer.h:242
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:240
#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:173
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:144
VkFormat getVkFormat() const
Return the vulkan format of the buffer.
Definition: RV_VKBuffer.h:134
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:238
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:142
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:214
Definition: format.h:1821