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_
vertices.clear();
normals.clear();
+ tex_coords.clear();
+ indices.clear();
}
void MeshGL::draw() const
glBindVertexArray(0);
}
-void MeshGL::update_vertex_data()
+bool MeshGL::update_vertex_data()
{
update_vbo();
+ return true;
}
void MeshGL::update_vbo()
glDeleteBuffers(1, &nvbo);
if(nvao)
glDeleteVertexArrays(1, &nvao);
-}
\ No newline at end of file
+}
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_
#include "vk.h"
#include "vkutil.h"
-VkDeviceMemory vk_allocate(int size)
+bool vku_allocate(int size, DevMemBlock *block)
{
VkDeviceMemory gpu_mem;
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;
}
#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_
#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)
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
#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);
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,
void cleanup_vulkan()
{
- //TODO!!!
free_rendering_command_buffers(rbufs, 2);
if(win) {
glfwDestroyWindow(win);
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)
#include <string>
#include <vector>
+#include "allocator.h"
#include "vkutil.h"
/* global variables */
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;
}