-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertex_buffer.cppm
More file actions
133 lines (109 loc) · 5.44 KB
/
vertex_buffer.cppm
File metadata and controls
133 lines (109 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module;
#include <vulkan/vulkan.h>
#include <span>
#include <array>
export module vk:vertex_buffer;
export import :types;
export import :utilities;
export import :command_buffer;
export import :buffer_streams;
export namespace vk {
inline namespace v1 {
/**
* @brief vulkan implementation for loading in vertices to a vulkan buffer handle
*
* This implementation automates handle in loading the vertices and its memories for it
*/
class vertex_buffer {
public:
vertex_buffer() = default;
vertex_buffer(const VkDevice& p_device,
const vertex_params& p_vertex_info) : m_device(p_device) {
m_size = p_vertex_info.vertices.size();
m_size_bytes = p_vertex_info.vertices.size_bytes();
VkBufferUsageFlags usage =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
// 1. creating staging buffer
uint32_t property_flags =
memory_property::host_visible_bit | memory_property::host_cached_bit;
buffer_parameters new_staging_buffer_settings = {
.device_size = m_size_bytes,
.physical_memory_properties =
p_vertex_info.phsyical_memory_properties,
.property_flags = (memory_property)property_flags,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.debug_name = p_vertex_info.debug_name.c_str(),
.vkSetDebugUtilsObjectNameEXT = p_vertex_info.vkSetDebugUtilsObjectNameEXT
};
buffer_stream staging_buffer(m_device, new_staging_buffer_settings);
std::span<const vertex_input> vertices = p_vertex_info.vertices;
staging_buffer.write(vertices);
// 3.) Now creating our actual vertex buffer handler
buffer_parameters vertex_params = {
.device_size = m_size_bytes,
.physical_memory_properties =
p_vertex_info.phsyical_memory_properties,
.property_flags = memory_property::device_local_bit,
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
};
m_vertex_handler = buffer_stream(m_device, vertex_params);
// 4. Copy data from staging buffer to the actual vertex buffer itself!
buffer_copy_info info = { .src = staging_buffer,
.dst = m_vertex_handler };
// copy(m_device, info, m_size_bytes);
// 1. Retrieve the first queue
// TODO: Use vk::device_queue for this
VkQueue temp_graphics_queue = nullptr;
uint32_t queue_family_index = 0;
uint32_t queue_index = 0;
vkGetDeviceQueue(
p_device, queue_family_index, queue_index, &temp_graphics_queue);
// command_buffer_info
command_params enumerate_command_info = {
.levels = command_levels::primary,
.queue_index = 0,
};
command_buffer copy_command_buffer(p_device, enumerate_command_info);
copy_command_buffer.begin(command_usage::one_time_submit);
// VkBufferCopy copy_region{};
// copy_region.size = (VkDeviceSize)m_size_bytes;
// vkCmdCopyBuffer(
// copy_command_buffer, staging_buffer, m_vertex_handler, 1, ©_region);
copy_command_buffer.copy_buffer(staging_buffer, m_vertex_handler, m_size_bytes);
copy_command_buffer.end();
VkCommandBuffer temp = copy_command_buffer;
VkSubmitInfo submit_info{};
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &temp;
vkQueueSubmit(temp_graphics_queue, 1, &submit_info, nullptr);
vkQueueWaitIdle(temp_graphics_queue);
// vkFreeCommandBuffers(, command_pool, 1, ©_cmd_buffer);
// vkDestroyCommandPool(driver, command_pool, nullptr);
copy_command_buffer.destroy();
// 5. cleanup staging buffer -- no longer used
staging_buffer.destroy();
}
[[nodiscard]] uint32_t size_bytes() const { return m_size_bytes; }
[[nodiscard]] uint32_t size() const { return m_size; }
[[nodiscard]] bool alive() const { return m_vertex_handler; }
void bind(const VkCommandBuffer& p_current) {
std::array<VkBuffer, 1> handlers = { m_vertex_handler };
VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(p_current, 0, 1, handlers.data(), offsets);
}
operator VkBuffer() const { return m_vertex_handler; }
operator VkBuffer() { return m_vertex_handler; }
void destroy() {
m_vertex_handler.destroy();
}
private:
VkDevice m_device = nullptr;
uint32_t m_size_bytes = 0;
uint32_t m_size = 0;
buffer_stream m_vertex_handler;
};
};
};