introduce index buffer (doesn't optimize anything yet)

This commit is contained in:
jacekpoz 2024-09-08 17:08:11 +02:00
parent af7e527ff2
commit 35cf6c593d
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 73 additions and 14 deletions

View file

@ -11,6 +11,7 @@ static PtkHandle m_root_component;
static PTK_LIST(PtkHandle) m_components;
PTK_LIST(Vertex) g_vertices;
PTK_LIST(uint32_t) g_indices;
static const PtkVec2 uvs[] = {
{ .x = 0.0f, .y = 0.0f },
@ -22,7 +23,8 @@ static const PtkVec2 uvs[] = {
void vk_init_vertices(void) {
m_components = PTK_LIST_NEW(PtkHandle, 1);
g_vertices = PTK_LIST_NEW(Vertex, 1);
g_vertices = PTK_LIST_NEW(Vertex, 0);
g_indices = PTK_LIST_NEW(uint32_t, 0);
}
void triangle(const PtkTriangle *triangle, const size_t uvs_offset) {
@ -34,7 +36,9 @@ void triangle(const PtkTriangle *triangle, const size_t uvs_offset) {
.uv = uvs[i + uvs_offset],
};
uint32_t old_vertex_count = g_vertices.size;
PTK_LIST_ADD(Vertex, g_vertices, v);
PTK_LIST_ADD(uint32_t, g_indices, old_vertex_count);
}
}

View file

@ -14,8 +14,10 @@ typedef struct {
} Vertex;
PTK_LIST_DEFINE(Vertex);
PTK_LIST_DEFINE(uint32_t);
extern PTK_LIST(Vertex) g_vertices;
extern PTK_LIST(uint32_t) g_indices;
void vk_init_vertices(void);

View file

@ -124,6 +124,8 @@ static PTK_ARRAY(VkVertexInputAttributeDescription) m_vertex_attribute_descripti
static VkBuffer m_vertex_buffer;
static VkDeviceMemory m_vertex_buffer_memory;
static VkBuffer m_index_buffer;
static VkDeviceMemory m_index_buffer_memory;
typedef struct {
PtkSize initial_window_size;
@ -1099,15 +1101,8 @@ bool copy_buffer(const VkBuffer src, const VkBuffer dst, const VkDeviceSize size
return true;
}
bool vk_transfer_vertex_data(void) {
PTK_DEBUG("vertices updated!");
PTK_LIST_FOR_EACH_E(const Vertex, g_vertices, i, current, {
PTK_DEBUG("v[%d]:", i);
PTK_DEBUG(" pos: (%f, %f)", current.pos.x, current.pos.y);
PTK_DEBUG(" color: (%f, %f, %f)", current.color.r, current.color.g, current.color.b);
})
PTK_DEBUG("transferring vertices to gpu…");
const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
bool transfer_to_buffer(void *src, size_t src_size, VkBuffer buffer) {
const VkDeviceSize buffer_size = src_size;
VkBuffer staging_buffer;
VkDeviceMemory staging_buffer_memory;
@ -1120,16 +1115,16 @@ bool vk_transfer_vertex_data(void) {
&staging_buffer_memory
)
) {
PTK_ERR("failed creating staging buffer");
PTK_ERR("failed creating staging vertex buffer");
return false;
}
void *data;
vkMapMemory(g_dev, staging_buffer_memory, 0, buffer_size, 0, &data);
memcpy(data, g_vertices.data, (size_t)buffer_size);
memcpy(data, src, (size_t)buffer_size);
vkUnmapMemory(g_dev, staging_buffer_memory);
if (!copy_buffer(staging_buffer, m_vertex_buffer, buffer_size)) {
if (!copy_buffer(staging_buffer, buffer, buffer_size)) {
PTK_ERR("failed copying staging buffer to vertex buffer");
return false;
}
@ -1140,6 +1135,36 @@ bool vk_transfer_vertex_data(void) {
return true;
}
bool vk_transfer_vertex_data(void) {
PTK_DEBUG("vertices updated!");
PTK_LIST_FOR_EACH_E(const Vertex, g_vertices, i, current, {
PTK_DEBUG("v[%d]:", i);
PTK_DEBUG(" pos: (%f, %f)", current.pos.x, current.pos.y);
PTK_DEBUG(" color: (%f, %f, %f)", current.color.r, current.color.g, current.color.b);
})
PTK_DEBUG("transferring vertices to gpu…");
const size_t vertices_size = sizeof(g_vertices.data[0]) * g_vertices.size;
if (!transfer_to_buffer(g_vertices.data, vertices_size, m_vertex_buffer)) {
PTK_ERR("failed transferring vertices");
return false;
}
PTK_DEBUG("indices updated!");
PTK_LIST_FOR_EACH_E(const uint32_t, g_indices, i, index, {
PTK_DEBUG("i[%d]: %d", i, index);
})
PTK_DEBUG("transferring indices to gpu…");
const size_t indices_size = sizeof(g_indices.data[0]) * g_indices.size;
if (!transfer_to_buffer(g_indices.data, indices_size, m_index_buffer)) {
PTK_ERR("failed transferring indices");
return false;
}
return true;
}
bool create_vertex_buffer(void) {
// const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
const VkDeviceSize buffer_size = 65536;
@ -1159,6 +1184,24 @@ bool create_vertex_buffer(void) {
return true;
}
bool create_index_buffer(void) {
const VkDeviceSize buffer_size = 65536;
if (
!create_buffer(
buffer_size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&m_index_buffer,
&m_index_buffer_memory
)
) {
PTK_ERR("failed creating index buffer");
return false;
}
return true;
}
bool create_uniform_buffers(void) {
const VkDeviceSize buffer_size = sizeof(UniformBufferObject);
@ -1326,9 +1369,11 @@ bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32
vkCmdBindVertexBuffers(command_buffer, 0, 1, (VkBuffer []){m_vertex_buffer}, (VkDeviceSize []){0});
vkCmdBindIndexBuffer(command_buffer, m_index_buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline_layout, 0, 1, &m_descriptor_sets.data[g_current_frame], 0, NULL);
vkCmdDraw(command_buffer, g_vertices.size, 1, 0, 0);
vkCmdDrawIndexed(command_buffer, g_indices.size, 1, 0, 0, 0);
vkCmdEndRenderPass(command_buffer);
@ -1493,6 +1538,11 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const
return false;
}
if (!create_index_buffer()) {
PTK_ERR("failed creating index buffer");
return false;
}
if (!create_uniform_buffers()) {
PTK_ERR("failed creating uniform buffers");
return false;
@ -1540,6 +1590,9 @@ void vk_cleanup(void) {
vkDestroyDescriptorPool(g_dev, m_descriptor_pool, NULL);
vkDestroyDescriptorSetLayout(g_dev, m_descriptor_set_layout, NULL);
vkDestroyBuffer(g_dev, m_index_buffer, NULL);
vkFreeMemory(g_dev, m_index_buffer_memory, NULL);
vkDestroyBuffer(g_dev, m_vertex_buffer, NULL);
vkFreeMemory(g_dev, m_vertex_buffer_memory, NULL);