#include "hk_model.hpp" #include #include namespace hk{ Model::Model(Device &device, const std::vector &vertices) : device{device} { createVertexBuffers(vertices); } Model::~Model() { vkDestroyBuffer(device.device(), vertexBuffer, nullptr); vkFreeMemory(device.device(), vertexBufferMemory, nullptr); } void Model::createVertexBuffers(const std::vector &vertices) { vertexCount = static_cast(vertices.size()); assert(vertexCount >= 3 && "Vertex count must be at least 3"); VkDeviceSize bufferSize = sizeof(vertices[0]) * vertexCount; device.createBuffer( bufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexBuffer, vertexBufferMemory ); void *data; vkMapMemory(device.device(), vertexBufferMemory, 0, bufferSize, 0, &data); memcpy(data, vertices.data(), static_cast(bufferSize)); vkUnmapMemory(device.device(), vertexBufferMemory); } void Model::bind(VkCommandBuffer commandBuffer) { VkBuffer buffers[] = {vertexBuffer}; VkDeviceSize offsets[] = {0}; vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets); } void Model::draw(VkCommandBuffer commandBuffer) { vkCmdDraw(commandBuffer, vertexCount, 1, 0, 0); } std::vector Model::Vertex::getBindingDescriptions() { std::vector bindingDescriptions(1); bindingDescriptions[0].binding = 0; bindingDescriptions[0].stride = sizeof(Vertex); bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; return bindingDescriptions; } std::vector Model::Vertex::getAttributeDescriptions() { std::vector attributeDescriptions(2); attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; attributeDescriptions[0].offset = offsetof(Vertex, posision); attributeDescriptions[1].binding = 0; attributeDescriptions[1].location = 1; attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; attributeDescriptions[1].offset = offsetof(Vertex, color); return attributeDescriptions; } }