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 "RV_VK.h"
21 #include "RV_Instance.h"
22 #include "RV_Type.h"
23 
24 class RV_Render;
25 class RV_VKMemory;
26 
27 // TODO: make into class ?
29 
30 /// RAII wrapper class for VkBufferView
32 {
33 public:
34  VkBufferView getVkView() { return myVkView; }
35  VkFormat getVkFormat() const { return myVkFormat; }
36 
37  // CAN have null state
39  : myInst(nullptr)
40  , myVkView(VK_NULL_HANDLE)
41  , myVkFormat(VK_FORMAT_UNDEFINED)
42  {}
43  RV_VKBufferView(RV_Instance* inst, VkBufferView vk_view, VkFormat vk_format)
44  : myInst(inst), myVkView(vk_view), myVkFormat(vk_format)
45  {}
46 
47  // disable copy semantics
48  RV_VKBufferView(const RV_VKBufferView&) = delete;
50 
51  // enable move semantics
52  RV_VKBufferView(RV_VKBufferView&& other) noexcept
53  : myInst(other.myInst)
54  , myVkView(other.myVkView)
55  , myVkFormat(other.myVkFormat)
56  {
57  other.myInst = nullptr;
58  other.myVkView = VK_NULL_HANDLE;
59  other.myVkFormat = VK_FORMAT_UNDEFINED;
60  }
61  RV_VKBufferView& operator=(RV_VKBufferView&& other) noexcept = delete;
62 
63  // Destroy Resource Handle on destruction
65  {
66  if (myVkView != VK_NULL_HANDLE)
67  {
68  VkDevice vk_dev = myInst->getDevice();
69  ::vkDestroyBufferView(vk_dev, myVkView, nullptr);
70  }
71  }
72 
73 private:
74  RV_Instance* myInst = nullptr;
75  VkBufferView myVkView = VK_NULL_HANDLE;
76  VkFormat myVkFormat = VK_FORMAT_UNDEFINED;
77 };
78 
79 /// @brief A vulkan buffer object
81 {
82 public:
83 
84  /// @brief Allocate a new Buffer object
85  /// Allocate a vulkan buffer
86  /// `is_staging` - if true, buffer will be CPU visible
87  ///
88  /// `vk_format` - if provided, a BufferView is only created --
89  /// only needed for texel buffer usage
90  static RV_VKBuffer* allocate( RV_Instance* inst,
91  exint size,
93  bool is_staging,
94  VkFormat vk_format = VK_FORMAT_UNDEFINED,
95  const char* name = nullptr);
96 
97  /// Get the raw vulkan buffer handle
98  VkBuffer getVkBuf() { return myVkBuf; }
99  /// Get the create info the buffer was created with
100  const RV_VKBufferCreateInfo& getInfo() { return *myCreateInfo; }
101  /// Get the memory the buffer is backed by
102  RV_VKMemory& getMemory() { return *myMemory; }
103 
104  /// Fetch the buffer view assigned to this buffer, if any
105  VkBufferView getVkView()
106  {
107  return myView ? myView->getVkView()
108  : VK_NULL_HANDLE;
109  }
110  /// Return the vulkan format of the buffer.
112  {
113  return myView ? myView->getVkFormat()
115  }
116  /// Return the size of the buffer, in bytes
117  exint getSize() const { return myCreateInfo->size; }
118  /// return the vulkan usage of the buffer when it was created
119  VkBufferUsageFlags getUsage() const { return myCreateInfo->usage; }
120  /// return the current usage of this buffer
121  VkBufferUsageFlags getLastUsage() const { return myLastUsage; }
122 
123  /// Number of elements in this buffer (eg. RGB 32b is 1 element)
124  exint entries() const;
125  /// Return the RV type of the buffer
126  RV_GPUType getGPUType() const;
127  /// Return the vector size of the buffer (1-4)
128  int getVectorSize() const;
129 
130  /// @brief Upload data to the buffer
131  /// Upload data to the buffer and add a barrier based on its usage.
132  /// Returns true if the size+offset doesn't exceed the allocated size.
133  bool uploadData(RV_Render* r,
134  VkBufferUsageFlags new_usage,
135  const void* data,
136  exint data_size,
137  exint data_offset = 0);
138  /// @brief Upload data to the buffer
139  /// Upload data to the buffer. Returns true if the size+offset doesn't
140  /// exceed the allocated size.
141  bool uploadData(RV_Render* r,
142  const void* data,
143  exint data_size,
144  exint data_offset = 0);
145 
146  /// Span version of buffer upload
148  bool uploadData(RV_Render* r, const T &data, exint offset = 0)
149  {
150  // `T` must be a `UT_Span` or convertable into a `UT_Span`
152  return uploadData(r, span.data(), span.size_bytes(), offset);
153  }
154 
155  /// Donwload data from the buffer to `data`. Returns false if the operation
156  /// would read data out-of-range or there was an error.
157  bool downloadData( RV_Render* r,
158  void* data,
159  exint data_size,
160  exint offset = 0,
161  exint sublen = -1);
162 
163  /// Add a barrier for using the buffer in the future
165  { addBarrier(r, myLastUsage, new_usage); }
166  /// Add a barrier for using the buffer in the future, changing its usage
167  void addBarrier(RV_Render* r, VkBufferUsageFlags old_usage,
168  VkBufferUsageFlags new_usage);
169 
170  virtual ~RV_VKBuffer();
171 
172  /// Debug output of the buffer (but not its data).
173  void print() const;
174 
175 protected:
176  RV_VKBuffer( RV_Instance* inst,
177  const RV_VKBufferCreateInfo* info,
178  VkBuffer vk_buf,
179  RV_VKMemory* mem,
181 
182  RV_Instance* myInst = nullptr;
183 
184  VkBuffer myVkBuf = VK_NULL_HANDLE;
186 
190 
191 public:
192  RV_StageGroup myWaitingBarrierStage = RV_STAGE_NONE;
193 
194 };
195 
196 #endif
VkFormat getVkFormat() const
Definition: RV_VKBuffer.h:35
VkBuffer getVkBuf()
Get the raw vulkan buffer handle.
Definition: RV_VKBuffer.h:98
#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
const RV_VKBufferCreateInfo & getInfo()
Get the create info the buffer was created with.
Definition: RV_VKBuffer.h:100
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Definition: span.h:73
int64 exint
Definition: SYS_Types.h:125
RV_StageGroup
Definition: RV_Type.h:429
VkBufferView getVkView()
Fetch the buffer view assigned to this buffer, if any.
Definition: RV_VKBuffer.h:105
exint getSize() const
Return the size of the buffer, in bytes.
Definition: RV_VKBuffer.h:117
UT_UniquePtr< RV_VKMemory > myMemory
Definition: RV_VKBuffer.h:188
VkBufferView getVkView()
Definition: RV_VKBuffer.h:34
bool uploadData(RV_Render *r, const T &data, exint offset=0)
Span version of buffer upload.
Definition: RV_VKBuffer.h:148
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:189
UT_UniquePtr< RV_VKBufferView > myView
Definition: RV_VKBuffer.h:187
GLintptr offset
Definition: glcorearb.h:665
UT_UniquePtr< const RV_VKBufferCreateInfo > myCreateInfo
Definition: RV_VKBuffer.h:185
#define RV_API
Definition: RV_API.h:10
RV_VKMemory & getMemory()
Get the memory the buffer is backed by.
Definition: RV_VKBuffer.h:102
GLuint const GLchar * name
Definition: glcorearb.h:786
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:36
RV_VKBufferView(RV_VKBufferView &&other) noexcept
Definition: RV_VKBuffer.h:52
VkBufferUsageFlags getLastUsage() const
return the current usage of this buffer
Definition: RV_VKBuffer.h:121
VkFormat getVkFormat() const
Return the vulkan format of the buffer.
Definition: RV_VKBuffer.h:111
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:119
Definition: core.h:982
VkDevice getDevice()
Get the raw vulkan device assocated with this instance.
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:80
RAII wrapper class for VkBufferView.
Definition: RV_VKBuffer.h:31
RV_VKBufferView & operator=(RV_VKBufferView &)=delete
VkBufferCreateInfo RV_VKBufferCreateInfo
Definition: RV_VKBuffer.h:25
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:43
void addBarrier(RV_Render *r, VkBufferUsageFlags new_usage)
Add a barrier for using the buffer in the future.
Definition: RV_VKBuffer.h:164
Definition: format.h:895