source: opengl-game/vulkan-buffer.hpp@ 6bac215

feature/imgui-sdl
Last change on this file since 6bac215 was 6bac215, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Rewrite a large portion of the VulkanBuffer class, start using it more
to resize buffers, and simplify resizeBufferSet() since the additions to
VulkanBuffer can now do some of what that function used to do

  • Property mode set to 100644
File size: 4.3 KB
Line 
1#ifndef _VULKAN_BUFFER_H
2#define _VULKAN_BUFFER_H
3
4/*
5* This class is intended to be used with Storage Buffers and Uniform Buffers.
6*/
7
8template<class T>
9class VulkanBuffer {
10
11 public:
12
13 // TODO: Make these private (maybe make a getter for numObjects)
14 // Externally, they are only used in resizeBufferSet
15 size_t capacity;
16
17 // temp field to help with ubo+ssbo resizing until they are added to this class
18 // See if I need a separate field for this or if I can use other fields to check for this
19 // Maybe compare uniform or storage buffer size to the size of the memory allocated here
20 bool resized;
21
22 VulkanBuffer();
23 VulkanBuffer(size_t capacity, size_t minOffsetAlignment);
24
25 VulkanBuffer(const VulkanBuffer<T>&) = delete;
26 VulkanBuffer(VulkanBuffer<T>&& other);
27
28 ~VulkanBuffer();
29
30 VulkanBuffer<T>& operator=(const VulkanBuffer<T>&) = delete;
31 VulkanBuffer<T>& operator=(VulkanBuffer<T>&& other) noexcept;
32
33 void resize();
34
35 void add(T obj);
36
37 // TODO: Add a resize function
38
39 private:
40
41 size_t alignment;
42 size_t numObjects;
43
44 T* srcData; // TODO: Rename this to something else probably and rename rawData to data
45 vector<T>* vData;
46
47 // Remember that this is a pointer to the mapped video memory
48 // Maybe rename it to mappedData or something to make that clearer
49 void* rawData;
50};
51
52// Currently, for SSBOs, I store the per-object values (usually just the model matrix), on each object, so they
53// are not in their own array and therefore cannot be pushed to the GPU as one block. The updates must happen
54// separately per object.
55
56// Since Sascha WIllems' dynamic UBO example works the same way (iirc), I can implement dynamic UBOs like that as well
57// for now. Would be nice to plan for potentially storing the ubo data on the CPU in a contiguous block in the future,
58// assuming that would make updates easier. Keep in mind that this only makes sense if all or most of the objects
59// in the ubo get updated every frame.
60
61// ============================= TODO: Also, check when it makes sense to have a staging buffer for copying data to the GPU
62// and see if I actually need to use it everywhere I currently am. I think this is mentioned in Sascha WIllems dubo example
63// or some other Vulkan website I recently bookmarked
64
65template<class T>
66VulkanBuffer<T>::VulkanBuffer()
67 : alignment(0)
68 , capacity(0)
69 , numObjects(0)
70 , resized(false)
71 , srcData(nullptr)
72 , rawData(nullptr)
73 , vData(nullptr) {
74}
75
76template<class T>
77VulkanBuffer<T>::VulkanBuffer(size_t capacity, size_t minOffsetAlignment)
78 : alignment(sizeof(T))
79 , capacity(capacity)
80 , numObjects(0)
81 , resized(false)
82 , srcData(nullptr)
83 , rawData(nullptr)
84 , vData(nullptr) {
85 if (minOffsetAlignment > 0) {
86 alignment = (alignment + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
87 }
88
89 srcData = (T*)malloc(capacity * alignment);
90}
91
92template<class T>
93VulkanBuffer<T>::VulkanBuffer(VulkanBuffer<T>&& other) {
94 // TODO: Implement
95}
96
97template<class T>
98VulkanBuffer<T>::~VulkanBuffer() {
99 if (srcData != nullptr) {
100 free(srcData);
101 }
102}
103
104template<class T>
105VulkanBuffer<T>& VulkanBuffer<T>::operator=(VulkanBuffer<T>&& other) noexcept {
106 if (this != &other) {
107 capacity = other.capacity;
108 numObjects = other.numObjects;
109 resized = other.resized;
110
111 alignment = other.alignment;
112
113 if (srcData != nullptr) {
114 free(srcData);
115 }
116
117 srcData = other.srcData;
118
119 other.capacity = 0;
120 other.numObjects = 0;
121 // TODO: Maybe set rnage to 0 as well
122
123 other.srcData = nullptr;
124 }
125
126 return *this;
127}
128
129template<class T>
130void VulkanBuffer<T>::resize() {
131 resized = false;
132}
133
134template<class T>
135void VulkanBuffer<T>::add(T obj) {
136 if (numObjects == capacity) {
137 // Once I add Vulkan buffer objects in here, make sure this doesn't overlap with resizeBufferSet
138 resized = true;
139
140 capacity *= 2;
141 }
142
143 numObjects++;
144}
145
146#endif // _VULKAN_BUFFER_H
Note: See TracBrowser for help on using the repository browser.