Remove RAM stored mesh data

Remove RAM stored vertices and indices data. It is now stored only in
GPU memory.
This commit is contained in:
Piotr Krygier committed 2023-09-26 14:15:21 +02:00
1 parent 0ef475e57e
commit 940bc7e512
7 files changed
+38 -111

No files matched your search

+5 -35
View File
@@ -20,8 +20,8 @@ uint8_t g_instance_free_ids[MAX_MESH_NUMBER][MAX_INSTANCE_NUMBER] = {OBJECT_FREE
uint16_t create_mesh(struct rse_vertex_t* vertices, uint16_t create_mesh(struct rse_vertex_t* vertices,
uint16_t* indices, uint16_t* indices,
size_t vertices_num, size_t vertices_count,
size_t indices_num) size_t indices_count)
{ {
size_t iter = 0U; size_t iter = 0U;
@@ -40,25 +40,11 @@ uint16_t create_mesh(struct rse_vertex_t* vertices,
g_free_ids[iter] = OBJECT_TAKEN; g_free_ids[iter] = OBJECT_TAKEN;
g_meshes[iter].unique_id = iter; g_meshes[iter].unique_id = iter;
g_meshes[iter].vertices = (struct rse_vertex_t*)malloc(sizeof(struct rse_vertex_t) * vertices_num); g_meshes[iter].vertices_count = vertices_count;
g_meshes[iter].indices = (uint16_t*)malloc(sizeof(uint16_t) * indices_num); g_meshes[iter].indices_count = indices_count;
g_meshes[iter].instances = (struct rse_instance_data_t*)malloc(sizeof(struct rse_instance_data_t) * MAX_INSTANCE_NUMBER);
if(g_meshes[iter].vertices == NULL ||
g_meshes[iter].indices == NULL) {
LOGE(vulkan_error_messages[COMMON_MEMORY_ALLOCATION_FAILED]);
/* TODO: Add error handling */
return MAX_MESH_NUMBER;
}
g_meshes[iter].instances_count = 0; g_meshes[iter].instances_count = 0;
g_meshes[iter].indices_count = indices_num;
g_meshes[iter].vertices_count = vertices_num;
memcpy(g_meshes[iter].vertices, vertices, vertices_num);
memcpy(g_meshes[iter].indices, indices, indices_num);
update_mesh_buffers(vertices, indices, vertices_num, indices_num); update_mesh_buffers(vertices, indices, vertices_count, indices_count);
return iter; return iter;
} }
@@ -67,7 +53,6 @@ void create_mesh_instance(uint16_t mesh_id,
struct rse_instance_data_t instance_data) struct rse_instance_data_t instance_data)
{ {
size_t iter = 0U; size_t iter = 0U;
struct rse_instance_data_t* instances = g_meshes[mesh_id].instances;
uint8_t* free_instance_ids = g_instance_free_ids[mesh_id]; uint8_t* free_instance_ids = g_instance_free_ids[mesh_id];
for(iter = 0U; iter < MAX_INSTANCE_NUMBER; ++iter) { for(iter = 0U; iter < MAX_INSTANCE_NUMBER; ++iter) {
@@ -83,7 +68,6 @@ void create_mesh_instance(uint16_t mesh_id,
} }
free_instance_ids[iter] = OBJECT_TAKEN; free_instance_ids[iter] = OBJECT_TAKEN;
instances[iter] = instance_data;
if(iter >= g_meshes[mesh_id].instances_count) { if(iter >= g_meshes[mesh_id].instances_count) {
g_meshes[mesh_id].instances_count++; g_meshes[mesh_id].instances_count++;
@@ -92,20 +76,6 @@ void create_mesh_instance(uint16_t mesh_id,
update_mesh_instances(&instance_data); update_mesh_instances(&instance_data);
} }
struct rse_instance_data_t* get_instance_data_for_mesh(uint16_t mesh_id)
{
return g_meshes[mesh_id].instances;
}
struct rse_vertex_t* get_mesh_vertices(uint16_t mesh_id) {
return g_meshes[mesh_id].vertices;
}
uint16_t* get_mesh_indices(uint16_t mesh_id) {
return g_meshes[mesh_id].indices;
}
size_t get_vertices_count(uint16_t mesh_id) size_t get_vertices_count(uint16_t mesh_id)
{ {
return g_meshes[mesh_id].vertices_count; return g_meshes[mesh_id].vertices_count;
+2 -28
View File
@@ -22,12 +22,9 @@
struct rse_mesh_t struct rse_mesh_t
{ {
uint32_t unique_id; uint32_t unique_id;
size_t instances_count;
size_t indices_count;
size_t vertices_count; size_t vertices_count;
struct rse_vertex_t* vertices; size_t indices_count;
uint16_t* indices; size_t instances_count;
struct rse_instance_data_t* instances;
}; };
/** /**
@@ -76,29 +73,6 @@ size_t get_indices_count(uint16_t mesh_id);
*/ */
size_t get_instances_count(uint16_t mesh_id); size_t get_instances_count(uint16_t mesh_id);
/**
* @brief Get pointer to vertices of mesh
*
* @param mesh_id Mesh identifier
* @return struct rse_vertex_t* Vertex data
*/
struct rse_vertex_t* get_mesh_vertices(uint16_t mesh_id);
/**
* @brief Get pointer to indices of mesh
*
* @param mesh_id Mesh identifier
* @return uint16_t* Indices data
*/
uint16_t* get_mesh_indices(uint16_t mesh_id);
/**
* @brief Get the instance data for mesh object.
*
* @param mesh_id Mesh identifier
* @return struct rse_instance_data_t* Pointer to instances array
*/
struct rse_instance_data_t* get_instance_data_for_mesh(uint16_t mesh_id);
#endif /* RSE_MESH_CONTROLLER_H */ #endif /* RSE_MESH_CONTROLLER_H */
+10 -43
View File
@@ -13,15 +13,6 @@
#define MAX_VULKAN_BUFFERS_COUNT 1 #define MAX_VULKAN_BUFFERS_COUNT 1
// // FIXME: Temporary array of vertices, for testing purposes
// const std::vector<Vertex> vertices = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}},
// {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 1.0f}},
// {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
// {{-0.5f, 0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}};
// // FIXME: Temporary array of vertices, for testing purposes
// std::vector<uint16_t> indices = {0, 1, 2, 2, 3, 0};
/* GPU visible buffer */ /* GPU visible buffer */
struct rse_vulkan_buffer_t g_vertex_buffers[MAX_VULKAN_BUFFERS_COUNT]; struct rse_vulkan_buffer_t g_vertex_buffers[MAX_VULKAN_BUFFERS_COUNT];
struct rse_vulkan_buffer_t g_index_buffers[MAX_VULKAN_BUFFERS_COUNT]; struct rse_vulkan_buffer_t g_index_buffers[MAX_VULKAN_BUFFERS_COUNT];
@@ -29,23 +20,6 @@ struct rse_vulkan_buffer_t g_instance_buffers[MAX_VULKAN_BUFFERS_COUNT];
VkCommandPool g_command_pool = VK_NULL_HANDLE; VkCommandPool g_command_pool = VK_NULL_HANDLE;
static uint8_t create_buffer(const VkDeviceSize size,
VkBufferUsageFlags buffer_usage,
VmaMemoryUsage memory_usage,
const VmaAllocationCreateFlags allocation_flags,
struct rse_vulkan_buffer_t* buffer);
static uint8_t copy_buffer(VkBuffer src,
VkBuffer dst,
VkDeviceSize size,
VkDeviceSize dest_offset);
static uint8_t create_command_pools();
static uint8_t create_vertex_buffer();
static uint8_t create_index_buffer();
static uint8_t create_uniform_buffer();
static uint8_t allocate_command_buffers();
/** /**
* @brief Helper function. Creates a buffer object * @brief Helper function. Creates a buffer object
* *
@@ -57,7 +31,7 @@ static uint8_t allocate_command_buffers();
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED
*/ */
uint8_t create_buffer(const VkDeviceSize size, static uint8_t create_buffer(const VkDeviceSize size,
VkBufferUsageFlags buffer_usage, VkBufferUsageFlags buffer_usage,
VmaMemoryUsage memory_usage, VmaMemoryUsage memory_usage,
const VmaAllocationCreateFlags allocation_flags, const VmaAllocationCreateFlags allocation_flags,
@@ -106,7 +80,7 @@ uint8_t create_buffer(const VkDeviceSize size,
* @param size Size of buffer to copy * @param size Size of buffer to copy
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset) static uint8_t copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset)
{ {
/* Vulkan buffers can only be copied using command buffers */ /* Vulkan buffers can only be copied using command buffers */
VkResult status = VK_FALSE; VkResult status = VK_FALSE;
@@ -165,7 +139,7 @@ uint8_t copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED * VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED
*/ */
uint8_t create_command_pools() static uint8_t create_command_pools()
{ {
VkCommandPoolCreateInfo create_info; VkCommandPoolCreateInfo create_info;
@@ -190,7 +164,7 @@ uint8_t create_command_pools()
* VULKAN_ERROR_BUFFER_CREATION_FAILED * VULKAN_ERROR_BUFFER_CREATION_FAILED
* VULKAN_ERROR_VERTEX_BUFFER_MAPPING_FAILED * VULKAN_ERROR_VERTEX_BUFFER_MAPPING_FAILED
*/ */
uint8_t create_vertex_buffer() static uint8_t create_vertex_buffer()
{ {
VkDeviceSize buffer_size; VkDeviceSize buffer_size;
@@ -216,7 +190,7 @@ uint8_t create_vertex_buffer()
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_BUFFER_CREATION_FAILED * VULKAN_ERROR_BUFFER_CREATION_FAILED
*/ */
uint8_t create_index_buffer() static uint8_t create_index_buffer()
{ {
VkDeviceSize buffer_size; VkDeviceSize buffer_size;
@@ -235,7 +209,7 @@ uint8_t create_index_buffer()
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
uint8_t createInstanceBuffer() static uint8_t create_instance_buffer()
{ {
VkDeviceSize buffer_size; VkDeviceSize buffer_size;
@@ -261,7 +235,7 @@ uint8_t createInstanceBuffer()
* *
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
*/ */
uint8_t create_uniform_buffer() static uint8_t create_uniform_buffer()
{ {
VkDeviceSize buffer_size = sizeof(struct rse_uniform_buffer_object_t); VkDeviceSize buffer_size = sizeof(struct rse_uniform_buffer_object_t);
@@ -280,7 +254,7 @@ uint8_t create_uniform_buffer()
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
* VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED
*/ */
uint8_t allocate_command_buffers() static uint8_t allocate_command_buffers()
{ {
VkCommandBufferAllocateInfo allocate_info; VkCommandBufferAllocateInfo allocate_info;
@@ -300,10 +274,6 @@ uint8_t allocate_command_buffers()
return VULKAN_ERROR_NO_ERROR; return VULKAN_ERROR_NO_ERROR;
} }
/**
* @brief Update uniform buffers
*
*/
void update_uniform_buffers() void update_uniform_buffers()
{ {
struct rse_uniform_buffer_object_t ubo = {}; struct rse_uniform_buffer_object_t ubo = {};
@@ -336,7 +306,6 @@ uint8_t update_mesh_buffers(const struct rse_vertex_t* vertices,
size_t vertices_num, size_t vertices_num,
size_t indices_num) size_t indices_num)
{ {
// void* mapped_data;
struct rse_vulkan_buffer_t staging_buffer; struct rse_vulkan_buffer_t staging_buffer;
size_t vertices_size = sizeof(vertices[0]) * vertices_num; size_t vertices_size = sizeof(vertices[0]) * vertices_num;
size_t indices_size = sizeof(indices[0]) * indices_num; size_t indices_size = sizeof(indices[0]) * indices_num;
@@ -408,7 +377,7 @@ uint8_t update_mesh_instances(struct rse_instance_data_t* instance_data)
} }
/** /**
* @brief Records commands for provided command buffer * @brief Records commands for provided swapchain framebuffer
* *
* @param command_buffer Command buffer, that will hold commands * @param command_buffer Command buffer, that will hold commands
* @param image_index Image index * @param image_index Image index
@@ -464,8 +433,6 @@ uint8_t record_command_buffer(uint32_t image_index)
VkBuffer vertex_buffers[] = {g_vertex_buffers[i].buffer}; VkBuffer vertex_buffers[] = {g_vertex_buffers[i].buffer};
VkBuffer index_buffers = {g_index_buffers[i].buffer}; VkBuffer index_buffers = {g_index_buffers[i].buffer};
VkBuffer instance_buffers[] = {g_instance_buffers[i].buffer}; VkBuffer instance_buffers[] = {g_instance_buffers[i].buffer};
uint16_t* indices = get_mesh_indices(0); /* TODO: We need to take care of ALL meshes */
struct rse_instance_data_t* instances = get_instance_data_for_mesh(0);
/* TODO: Figure out the offset */ /* TODO: Figure out the offset */
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offsets); vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offsets);
@@ -494,7 +461,7 @@ uint8_t create_buffers()
STATUS_CHECK(create_command_pools()); STATUS_CHECK(create_command_pools());
STATUS_CHECK(create_vertex_buffer()); STATUS_CHECK(create_vertex_buffer());
STATUS_CHECK(create_index_buffer()); STATUS_CHECK(create_index_buffer());
STATUS_CHECK(createInstanceBuffer()); STATUS_CHECK(create_instance_buffer());
STATUS_CHECK(create_uniform_buffer()); STATUS_CHECK(create_uniform_buffer());
STATUS_CHECK(allocate_command_buffers()); STATUS_CHECK(allocate_command_buffers());
+17 -3
View File
@@ -10,7 +10,6 @@
/** /**
* @brief Create buffers needed by vulkan pipeline * @brief Create buffers needed by vulkan pipeline
* *
* @param vulkan_state
* @return uint8_t * @return uint8_t
*/ */
uint8_t create_buffers(); uint8_t create_buffers();
@@ -18,22 +17,37 @@ uint8_t create_buffers();
/** /**
* @brief Update uniform buffers * @brief Update uniform buffers
* *
* @param vulkan_state
*/ */
void update_uniform_buffers(); void update_uniform_buffers();
/**
* @brief Add vertices and indices to vulkan buffers
*
* @param vertices Vertices to add
* @param indices Indices to add
* @param vertices_num Number of vertices to add
* @param indices_num Number of indices to add
* @return uint8_t VULKAN_ERROR_NO_ERROR on success
*/
uint8_t update_mesh_buffers(const struct rse_vertex_t* vertices, uint8_t update_mesh_buffers(const struct rse_vertex_t* vertices,
const uint16_t* indices, const uint16_t* indices,
size_t vertices_num, size_t vertices_num,
size_t indices_num); size_t indices_num);
/**
* @brief Add instance data to vulkan buffers
*
* @param instance_data Instance data
* @return uint8_t VULKAN_ERROR_NO_ERROR on success
*/
uint8_t update_mesh_instances(struct rse_instance_data_t* instance_data); uint8_t update_mesh_instances(struct rse_instance_data_t* instance_data);
/** /**
* @brief Record commands for given image index * @brief Record commands for given image index
* *
* @param vulkan_state * @param vulkan_state
* @param imageIndex * @param imageIndex
* @return uint8_t * @return uint8_t VULKAN_ERROR_NO_ERROR on success
*/ */
uint8_t record_command_buffer(uint32_t imageIndex); uint8_t record_command_buffer(uint32_t imageIndex);
+1 -1
View File
@@ -20,7 +20,7 @@ void rse_update_time()
g_float_delta_time = (float)difftime(g_this_frame_time, g_last_frame_time); g_float_delta_time = (float)difftime(g_this_frame_time, g_last_frame_time);
} }
float inline rse_get_time_diff() float rse_get_time_diff()
{ {
return g_float_delta_time; return g_float_delta_time;
} }
+1 -1
View File
@@ -81,6 +81,6 @@ struct rse_instance_data_t {
void rse_init_time(); void rse_init_time();
void rse_update_time(); void rse_update_time();
float inline rse_get_time_diff(); float rse_get_time_diff();
#endif /* RSE_VULKAN_GLOBAL_H */ #endif /* RSE_VULKAN_GLOBAL_H */
+2
View File
@@ -534,6 +534,8 @@ void free_dynamic_pipeline_state_info(VkPipelineDynamicStateCreateInfo* dynamic_
*/ */
void fill_vertex_input_info(VkPipelineVertexInputStateCreateInfo* vertex_input_info) void fill_vertex_input_info(VkPipelineVertexInputStateCreateInfo* vertex_input_info)
{ {
/* This is clusterfuck with some magic numbers here and there and unneeded malloc and memcpy stuff. Change,
* so you won't feel ashamed. */
VkVertexInputBindingDescription vertex_binding_description = get_binding_description(); VkVertexInputBindingDescription vertex_binding_description = get_binding_description();
VkVertexInputAttributeDescription* vertex_attribute_descriptions = get_attribute_descriptions(); VkVertexInputAttributeDescription* vertex_attribute_descriptions = get_attribute_descriptions();
VkVertexInputBindingDescription* bindingDescriptions = malloc(sizeof(VkVertexInputBindingDescription) * 2); VkVertexInputBindingDescription* bindingDescriptions = malloc(sizeof(VkVertexInputBindingDescription) * 2);