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 
98  /// Get the raw vulkan buffer handle
99  VkBuffer getVkBuf() { return myVkBuf; }
100  /// Get the create info the buffer was created with
101  const RV_VKBufferCreateInfo& getInfo() { return *myCreateInfo; }
102  /// Get the memory the buffer is backed by
103  RV_VKMemory& getMemory() { return *myMemory; }
104  /// Return unique ID for buffer object
105  exint getID() const { return myId; }
106 
107  /// Fetch the buffer view assigned to this buffer, if any
108  VkBufferView getVkView()
109  {
110  return myView ? myView->getVkView()
111  : VK_NULL_HANDLE;
112  }
113  /// Return the vulkan format of the buffer.
115  {
116  return myView ? myView->getVkFormat()
118  }
119  /// Return the size of the buffer, in bytes
120  exint getSize() const { return myCreateInfo->size; }
121  /// return the vulkan usage of the buffer when it was created
122  VkBufferUsageFlags getUsage() const { return myCreateInfo->usage; }
123  /// return the current usage of this buffer
124  VkBufferUsageFlags getLastUsage() const { return myLastUsage; }
125 
126  /// Number of elements in this buffer (eg. RGB 32b is 1 element)
127  exint entries() const;
128  /// Return the RV type of the buffer
129  RV_GPUType getGPUType() const;
130  /// Return the vector size of the buffer (1-4)
131  int getVectorSize() const;
132 
133  /// @brief Upload data to the buffer
134  /// Upload data to the buffer and add a barrier based on its usage.
135  /// Returns true if the size+offset doesn't exceed the allocated size.
136  bool uploadData(RV_Render* r,
137  VkBufferUsageFlags new_usage,
138  const void* data,
139  exint data_size,
140  exint data_offset = 0);
141  /// @brief Upload data to the buffer
142  /// Upload data to the buffer. Returns true if the size+offset doesn't
143  /// exceed the allocated size.
144  bool uploadData(RV_Render* r,
145  const void* data,
146  exint data_size,
147  exint data_offset = 0);
148 
149  /// Span version of buffer upload
151  bool uploadData(RV_Render* r, const T &data)
152  {
153  // `T` must be a `UT_Span` or convertable into a `UT_Span`
155  return uploadData(r, span.data(), span.size_bytes(), 0);
156  }
157 
159  bool uploadData(RV_Render* r, VkBufferUsageFlags new_usage, const T &data)
160  {
161  // `T` must be a `UT_Span` or convertable into a `UT_Span`
163  return uploadData(r, new_usage, span.data(), span.size_bytes(), 0);
164  }
165 
166  /// return whether buffer was declared as CPU visible, and mapped
167  bool isMappedMemory() const;
168 
169  /// Get a CPU mapped ptr to the raw memory. Will return nullptr
170  /// if buffer wasn't declared as CPU visible
171  void* getMappedMemory();
172 
173  /// Download data from the buffer into `buf`
174  bool copyData(RV_Render* r,
175  RV_VKBuffer* buf,
176  exint src_offset = 0,
177  exint dst_offset = 0,
178  exint sublen = -1);
179 
180  /// Donwload data from the buffer to `data`. Returns false if the operation
181  /// would read data out-of-range or there was an error.
182  bool downloadData( RV_Render* r,
183  void* data,
184  exint data_size,
185  exint offset = 0,
186  exint sublen = -1);
187 
188  /// Add a barrier for using the buffer in the future
190  { addBarrier(r, myLastUsage, new_usage); }
191  /// Add a barrier for using the buffer in the future, changing its usage
192  void addBarrier(RV_Render* r, VkBufferUsageFlags old_usage,
193  VkBufferUsageFlags new_usage);
194 
195  virtual ~RV_VKBuffer();
196 
197  /// Debug output of the buffer (but not its data).
198  void print() const;
199 
200 private:
201  struct PassKey {};
202 public:
203  RV_VKBuffer(RV_Instance* inst,
204  const RV_VKBufferCreateInfo* info,
205  VkBuffer vk_buf,
206  RV_VKMemoryPtr mem,
208  const PassKey&);
209 
210 protected:
211  RV_Instance* myInst = nullptr;
212 
213  VkBuffer myVkBuf = VK_NULL_HANDLE;
214  exint myId = 0;
216 
220 
221 public:
222  RV_StageGroup myWaitingBarrierStage = RV_STAGE_NONE;
223  exint myWaitingBarrierGroupID = -1;
224 
225 };
226 
227 #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:99
#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:55
const RV_VKBufferCreateInfo & getInfo()
Get the create info the buffer was created with.
Definition: RV_VKBuffer.h:101
GLsizei const GLfloat * value
Definition: glcorearb.h:824
exint getID() const
Return unique ID for buffer object.
Definition: RV_VKBuffer.h:105
Definition: span.h:73
int64 exint
Definition: SYS_Types.h:125
RV_StageGroup
Definition: RV_Type.h:451
VkBufferView getVkView()
Fetch the buffer view assigned to this buffer, if any.
Definition: RV_VKBuffer.h:108
UT_UniquePtr< RV_VKMemory > RV_VKMemoryPtr
Definition: RV_TypePtrs.h:47
bool uploadData(RV_Render *r, VkBufferUsageFlags new_usage, const T &data)
Definition: RV_VKBuffer.h:159
exint getSize() const
Return the size of the buffer, in bytes.
Definition: RV_VKBuffer.h:120
UT_UniquePtr< RV_VKMemory > myMemory
Definition: RV_VKBuffer.h:218
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
VkBufferUsageFlags myLastUsage
Definition: RV_VKBuffer.h:219
UT_UniquePtr< RV_VKBufferView > myView
Definition: RV_VKBuffer.h:217
GLintptr offset
Definition: glcorearb.h:665
UT_UniquePtr< const RV_VKBufferCreateInfo > myCreateInfo
Definition: RV_VKBuffer.h:215
#define RV_API
Definition: RV_API.h:10
bool uploadData(RV_Render *r, const T &data)
Span version of buffer upload.
Definition: RV_VKBuffer.h:151
RV_VKMemory & getMemory()
Get the memory the buffer is backed by.
Definition: RV_VKBuffer.h:103
GLuint const GLchar * name
Definition: glcorearb.h:786
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:38
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:124
VkFormat getVkFormat() const
Return the vulkan format of the buffer.
Definition: RV_VKBuffer.h:114
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_GPUType
Definition: RV_Type.h:37
constexpr size_type size_bytes() const noexcept
Definition: span.h:186
VkFormat
Definition: vulkan_core.h:1386
VkBufferUsageFlags getUsage() const
return the vulkan usage of the buffer when it was created
Definition: RV_VKBuffer.h:122
Definition: core.h:982
VkDevice getDevice()
Get the raw vulkan device assocated with this instance.
GLboolean r
Definition: glcorearb.h:1222
UT_UniquePtr< RV_VKBufferView > RV_VKBufferViewPtr
Definition: RV_TypePtrs.h:56
A vulkan buffer object.
Definition: RV_VKBuffer.h:81
RAII wrapper class for VkBufferView.
Definition: RV_VKBuffer.h:32
RV_VKBufferView & operator=(RV_VKBufferView &)=delete
VkBufferCreateInfo RV_VKBufferCreateInfo
Definition: RV_VKBuffer.h:26
type
Definition: core.h:1059
FMT_INLINE void print(format_string< T...> fmt, T &&...args)
Definition: core.h:2976
constexpr pointer data() const noexcept
Definition: span.h:189
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:189
Definition: format.h:895