From c3d7c1b7f8a7f4bcfc7661b6baf166ce0648083a Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sun, 11 Mar 2018 22:08:05 +0200 Subject: [PATCH] writing mesh data to GPU --- src/vulkan/allocator.cc | 15 +++++++++++++++ src/vulkan/allocator.h | 3 +-- src/vulkan/mesh-vk.cc | 34 +++++++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/vulkan/allocator.cc b/src/vulkan/allocator.cc index 714b7cc..ba1b47b 100644 --- a/src/vulkan/allocator.cc +++ b/src/vulkan/allocator.cc @@ -30,3 +30,18 @@ void vku_free(VkDeviceMemory gpu_memory) { vkFreeMemory(vk_device, gpu_memory, 0); } + +bool vku_write_memory(VkDeviceMemory gpu_mem, int size, void *data) +{ + uint8_t *pdata; + VkResult res = vkMapMemory(vk_device, gpu_mem, 0, size, 0, (void**)&pdata); + if(res != VK_SUCCESS) { + fprintf(stderr, "Failed to map memory to write data.\n"); + return false; + } + + memcpy(pdata, data, size); + vkUnmapMemory(vk_device, gpu_mem); + + return true; +} diff --git a/src/vulkan/allocator.h b/src/vulkan/allocator.h index 83ccee9..4f4fd53 100644 --- a/src/vulkan/allocator.h +++ b/src/vulkan/allocator.h @@ -12,7 +12,6 @@ struct DevMemBlock { bool vku_allocate(int size, DevMemBlock *block); void vku_free(VkDeviceMemory gpu_memory); -bool vku_map_memory(VkDeviceMemory gpu_mem, int size, void *data); -void vku_unmap_memory(VkDeviceMemory gpu_mem); +bool vku_write_memory(VkDeviceMemory gpu_mem, int size, void *data); #endif // ALLOCATOR_H_ diff --git a/src/vulkan/mesh-vk.cc b/src/vulkan/mesh-vk.cc index 97ce2f0..494389b 100644 --- a/src/vulkan/mesh-vk.cc +++ b/src/vulkan/mesh-vk.cc @@ -1,4 +1,5 @@ #include +#include "allocator.h" #include "mesh-vk.h" MeshVK::MeshVK() @@ -49,13 +50,15 @@ bool MeshVK::update_vertex_data() /* create the buffers */ - if(!(vk_vertices = vku_create_buffer(vertices.size() * sizeof(Vec3), + int vsz = vertices.size() * sizeof(Vec3); + if(!(vk_vertices = vku_create_buffer(vsz, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) { fprintf(stderr, "Failed to create the buffer for the vertices.\n"); return false; } - if(!(vk_normals = vku_create_buffer(normals.size() * sizeof(Vec3), + int nsz = normals.size() * sizeof(Vec3); + if(!(vk_normals = vku_create_buffer(nsz, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) { vku_destroy_buffer(vk_vertices); @@ -63,7 +66,8 @@ bool MeshVK::update_vertex_data() return false; } - if(!(vk_tex_coords = vku_create_buffer(tex_coords.size() * sizeof(Vec2), + int tsz = tex_coords.size() * sizeof(Vec2); + if(!(vk_tex_coords = vku_create_buffer(tsz, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) { vku_destroy_buffer(vk_vertices); vku_destroy_buffer(vk_normals); @@ -73,7 +77,8 @@ bool MeshVK::update_vertex_data() return false; } - if(!(vk_indices = vku_create_buffer(indices.size() * 2, + int isz = indices.size() * 2; + if(!(vk_indices = vku_create_buffer(isz, VK_BUFFER_USAGE_INDEX_BUFFER_BIT))) { vku_destroy_buffer(vk_vertices); vku_destroy_buffer(vk_normals); @@ -83,10 +88,25 @@ bool MeshVK::update_vertex_data() return false; } - /* map the buffers */ + /* write the buffers */ - //TODO this is going to change when we allocate one mem block for all data, - //for the moment we map each buffer memory block and we set the data + if(!vku_write_memory(vk_vertices->mem_pool, vsz, (void*)vertices.data())) { + fprintf(stderr, "Failed to write the vertices on GPU.\n"); + return false; + } + if(!vku_write_memory(vk_normals->mem_pool, nsz, (void*)normals.data())) { + fprintf(stderr, "Failed to write the normalson GPU.\n"); + return false; + } + if(!vku_write_memory(vk_tex_coords->mem_pool, tsz, + (void*)tex_coords.data())) { + fprintf(stderr, "Failed to write the texture coordinates on GPU.\n"); + return false; + } + if(!vku_write_memory(vk_indices->mem_pool, isz, (void*)indices.data())) { + fprintf(stderr, "Failed to write the indices on GPU.\n"); + return false; + } return true; } -- 1.7.10.4