writing mesh data to GPU
[demo] / src / vulkan / mesh-vk.cc
index 4bd4791..494389b 100644 (file)
@@ -1,11 +1,17 @@
+#include <vulkan/vulkan.h>
+#include "allocator.h"
 #include "mesh-vk.h"
 
-MeshVK::MeshVK() {}
+MeshVK::MeshVK()
+{
+}
+
 MeshVK::MeshVK(const MeshVK &mesh)
 {
        indices = mesh.indices;
        vertices = mesh.vertices;
        normals = mesh.normals;
+       tex_coords = mesh.tex_coords;
 }
 
 MeshVK &MeshVK::operator=(const MeshVK &mesh)
@@ -17,20 +23,98 @@ MeshVK &MeshVK::operator=(const MeshVK &mesh)
        indices = mesh.indices;
        vertices = mesh.vertices;
        normals = mesh.normals;
+       tex_coords = mesh.tex_coords;
 
        return *this;
 }
 
 MeshVK::~MeshVK()
 {
+       vku_destroy_buffer(vk_vertices);
+       vku_destroy_buffer(vk_normals);
+       vku_destroy_buffer(vk_tex_coords);
+       vku_destroy_buffer(vk_indices);
+
        vertices.clear();
        normals.clear();
+       tex_coords.clear();
+       indices.clear();
 }
 
-void MeshVK::update_vertex_data()
+bool MeshVK::update_vertex_data()
 {
+       if(vertices.empty()) {
+               printf("empty vertices!\n");
+               return false;
+       }
+
+       /* create the buffers */
+
+       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;
+       }
+
+       int nsz = normals.size() * sizeof(Vec3);
+       if(!(vk_normals = vku_create_buffer(nsz,
+                                       VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
+               vku_destroy_buffer(vk_vertices);
+
+               fprintf(stderr, "Failed to create the buffer for the normals.\n");
+               return false;
+       }
+
+       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);
+
+               fprintf(stderr,
+                               "Failed to create the buffer for the texture coordinates.\n");
+               return false;
+       }
+
+       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);
+               vku_destroy_buffer(vk_tex_coords);
+
+               fprintf(stderr, "Failed to create the indices buffer.\n");
+               return false;
+       }
+
+       /* write the buffers */
+
+       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;
 }
 
 void MeshVK::draw() const
 {
-}
\ No newline at end of file
+}
+
+void MeshVK::draw_normals(float scale) const
+{
+}