buffer allocation
authorEleni Maria Stea <estea@igalia.com>
Sun, 11 Mar 2018 18:35:44 +0000 (20:35 +0200)
committerEleni Maria Stea <estea@igalia.com>
Sun, 11 Mar 2018 18:35:44 +0000 (20:35 +0200)
src/mesh.h
src/opengl/mesh-gl.cc
src/opengl/mesh-gl.h
src/vulkan/allocator.cc
src/vulkan/allocator.h
src/vulkan/mesh-vk.cc
src/vulkan/mesh-vk.h
src/vulkan/vk.cc
src/vulkan/vkutil.cc

index 84cbbcb..394f46a 100644 (file)
@@ -31,9 +31,9 @@ public:
 
        virtual void draw() const = 0;
        virtual void draw_normals(float scale) const = 0;
-       virtual void update_vertex_data() = 0;
+       virtual bool update_vertex_data() = 0;
        virtual void transform(const Mat4 &mat);
        virtual void invalidate();
 };
 
-#endif // MESH_H_
\ No newline at end of file
+#endif // MESH_H_
index 6175aae..531ac78 100644 (file)
@@ -69,6 +69,8 @@ MeshGL::~MeshGL()
 
        vertices.clear();
        normals.clear();
+       tex_coords.clear();
+       indices.clear();
 }
 
 void MeshGL::draw() const
@@ -115,9 +117,10 @@ void MeshGL::draw_normals(float scale) const
        glBindVertexArray(0);
 }
 
-void MeshGL::update_vertex_data()
+bool MeshGL::update_vertex_data()
 {
        update_vbo();
+       return true;
 }
 
 void MeshGL::update_vbo()
@@ -211,4 +214,4 @@ void MeshGL::destroy_vbo()
                glDeleteBuffers(1, &nvbo);
        if(nvao)
                glDeleteVertexArrays(1, &nvao);
-}
\ No newline at end of file
+}
index 833e2db..ca4c058 100644 (file)
@@ -31,9 +31,9 @@ public:
 
        virtual void draw() const override;
        virtual void draw_normals(float scale) const override;
-       virtual void update_vertex_data() override;
+       virtual bool update_vertex_data() override;
 
        void destroy_vbo();
 };
 
-#endif // MESH_GL_H_
\ No newline at end of file
+#endif // MESH_GL_H_
index 5228a72..9d71e9b 100644 (file)
@@ -5,7 +5,7 @@
 #include "vk.h"
 #include "vkutil.h"
 
-VkDeviceMemory vk_allocate(int size)
+bool vku_allocate(int size, DevMemBlock *block)
 {
        VkDeviceMemory gpu_mem;
 
@@ -15,9 +15,13 @@ VkDeviceMemory vk_allocate(int size)
        gpu_alloc_inf.allocationSize = size;
 
        if(vkAllocateMemory(vk_device, &gpu_alloc_inf, 0, &gpu_mem) != VK_SUCCESS) {
-               fprintf(stderr, "Failed to allocate device memory, mem size: %d\n");
-               return 0;
+               fprintf(stderr, "Failed to allocate device memory, mem size: %d\n", size);
+               return false;
        }
 
-       return gpu_mem;
+       block->dev_mem = gpu_mem;
+       block->offset = 0;
+       block->size = size;
+
+       return true;
 }
index dd730e3..ad4c87f 100644 (file)
@@ -3,7 +3,13 @@
 
 #include <vulkan/vulkan.h>
 
-VkDeviceMemory vk_allocate(int size);
-void vk_free(VkDeviceMemory gpu_memory);
+struct DevMemBlock {
+       VkDeviceMemory dev_mem;
+       int offset;
+       int size;
+};
+
+bool vku_allocate(int size, DevMemBlock *block);
+void vku_free(VkDeviceMemory gpu_memory);
 
 #endif // ALLOCATOR_H_
index 2f0c5fb..c190199 100644 (file)
@@ -1,12 +1,16 @@
 #include <vulkan/vulkan.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)
@@ -18,25 +22,40 @@ 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;
+               return false;
+       }
+
+       /* create vertex buffer */
+
+       if(!(vk_vertices = vku_create_buffer(vertices.size() * sizeof(Vec3),
+                                       VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
+               fprintf(stderr, "Failed to create vertex buffer.\n");
+               return false;
        }
 
-//     if(num_vertices != vertices.size()) {
-//     }
+       return true;
 }
 
 void MeshVK::draw() const
index 9b8c677..a4ad0bd 100644 (file)
@@ -2,15 +2,16 @@
 #define MESH_VK_H_
 
 #include "mesh.h"
+#include "vkutil.h"
 
 class MeshVK : public Mesh {
 private:
-       VkBuffer vk_vertices;
-       VkBuffer vk_normals;
-       VkBuffer vk_tex_coords;
-       VkBuffer vk_indices;
+       vku_buffer *vk_vertices;
+       vku_buffer *vk_normals;
+       vku_buffer *vk_tex_coords;
+       vku_buffer *vk_indices;
 
-       virtual void update_vertex_data() override;
+       virtual bool update_vertex_data() override;
 public:
     MeshVK();
        MeshVK(const MeshVK &mesh);
index a0f7751..4a23211 100644 (file)
@@ -354,12 +354,13 @@ static bool create_zbuffer()
        VkMemoryRequirements dmem_reqs;
        vkGetImageMemoryRequirements(vk_device, dimg, &dmem_reqs);
 
-       gpu_mem = vk_allocate(dmem_reqs.size);
-
-       if(!gpu_mem)
+       DevMemBlock block;
+       if(!vku_allocate(dmem_reqs.size, &block)) {
+               fprintf(stderr, "Failed to allocate zbuffer image.\n");
                return false;
+       }
 
-       vkBindImageMemory(vk_device, dimg, gpu_mem, 0);
+       vkBindImageMemory(vk_device, dimg, block.dev_mem, 0);
 
        if(!vk_image_set_layout(init_buf, dimg, VK_IMAGE_ASPECT_DEPTH_BIT,
                                VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
@@ -656,7 +657,6 @@ static bool begin_rendering_command_buffers(VkCommandBuffer *bufs, int count)
 
 void cleanup_vulkan()
 {
-       //TODO!!!
        free_rendering_command_buffers(rbufs, 2);
        if(win) {
                glfwDestroyWindow(win);
@@ -675,17 +675,6 @@ static void error_callback(int error, const char *description)
 
 static void reshape(int width, int height)
 {
-//     VkSwapchainKHR sc;
-//     if(!(sc = vku_create_swapchain(vk_surface, width, height, 2, VK_PRESENT_MODE_FIFO_KHR,
-//                                    vk_swapchain))) {
-//             fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", width, height);
-//             return;
-//     }
-//     vk_swapchain = sc;
-//
-//     delete [] vkswapchain_images;
-//     vkswapchain_images = vku_get_swapchain_images(sc, 0);
-//     vk_curr_swapchain_image = vku_get_next_image(vk_swapchain);
 }
 
 static void clear(float r, float g, float b)
index 526ec38..a64f86f 100644 (file)
@@ -7,6 +7,7 @@
 #include <string>
 #include <vector>
 
+#include "allocator.h"
 #include "vkutil.h"
 
 /* global variables */
@@ -443,7 +444,16 @@ struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
                fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
                return 0;
        }
-       // TODO back with memory
+
+       VkMemoryRequirements mr;
+       vkGetBufferMemoryRequirements(vk_device, buf->buf, &mr);
+
+       DevMemBlock block;
+       if(!vku_allocate(mr.size, &block))
+               return 0;
+
+       buf->mem_pool = block.dev_mem;
+
        return buf;
 }