|
|
@ -3,6 +3,7 @@ |
|
|
|
#include "buffer.hpp" |
|
|
|
#include "buffer.hpp" |
|
|
|
#include "vertex.hpp" |
|
|
|
#include "vertex.hpp" |
|
|
|
#include "instance.hpp" |
|
|
|
#include "instance.hpp" |
|
|
|
|
|
|
|
#include "command_pool.hpp" |
|
|
|
|
|
|
|
|
|
|
|
const std::vector<Vertex> vertices = { |
|
|
|
const std::vector<Vertex> vertices = { |
|
|
|
{{0.0, -0.5}, {1, 0, 0}}, |
|
|
|
{{0.0, -0.5}, {1, 0, 0}}, |
|
|
@ -10,31 +11,26 @@ const std::vector<Vertex> vertices = { |
|
|
|
{{-0.5, 0.5}, {0, 0, 1}} |
|
|
|
{{-0.5, 0.5}, {0, 0, 1}} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Buffer::Buffer(Instance* instance) : instance(instance) { |
|
|
|
Buffer::Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : instance(instance), size(size){ |
|
|
|
VkBufferCreateInfo bufferInfo{}; |
|
|
|
VkBufferCreateInfo bufferInfo{}; |
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
|
|
|
bufferInfo.size = vertices.size() * sizeof(Vertex); |
|
|
|
bufferInfo.size = size; |
|
|
|
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; |
|
|
|
bufferInfo.usage = usage; |
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
|
|
|
|
|
|
|
|
|
|
|
vkCreateBuffer(instance->device, &bufferInfo, nullptr, &vertexHandle); |
|
|
|
vkCreateBuffer(instance->device, &bufferInfo, nullptr, &handle); |
|
|
|
|
|
|
|
|
|
|
|
VkMemoryRequirements memoryRequirements; |
|
|
|
VkMemoryRequirements memoryRequirements; |
|
|
|
vkGetBufferMemoryRequirements(instance->device, vertexHandle, &memoryRequirements); |
|
|
|
vkGetBufferMemoryRequirements(instance->device, handle, &memoryRequirements); |
|
|
|
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocateInfo {}; |
|
|
|
VkMemoryAllocateInfo allocateInfo {}; |
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
|
|
|
allocateInfo.allocationSize = memoryRequirements.size; |
|
|
|
allocateInfo.allocationSize = memoryRequirements.size; |
|
|
|
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
|
|
|
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); |
|
|
|
|
|
|
|
|
|
|
|
vkAllocateMemory(instance->device, &allocateInfo, nullptr, &vertexMemory); |
|
|
|
vkAllocateMemory(instance->device, &allocateInfo, nullptr, &memory); |
|
|
|
|
|
|
|
|
|
|
|
vkBindBufferMemory(instance->device, vertexHandle, vertexMemory, 0); |
|
|
|
vkBindBufferMemory(instance->device, handle, memory, 0); |
|
|
|
|
|
|
|
|
|
|
|
void* data; |
|
|
|
|
|
|
|
vkMapMemory(instance->device, vertexMemory, 0, bufferInfo.size, 0, &data); |
|
|
|
|
|
|
|
memcpy(data, vertices.data(), bufferInfo.size); |
|
|
|
|
|
|
|
vkUnmapMemory(instance->device, vertexMemory); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) { |
|
|
|
uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) { |
|
|
@ -51,6 +47,52 @@ uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags prope |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Buffer::~Buffer() { |
|
|
|
Buffer::~Buffer() { |
|
|
|
vkFreeMemory(instance->device, vertexMemory, nullptr); |
|
|
|
vkFreeMemory(instance->device, memory, nullptr); |
|
|
|
vkDestroyBuffer(instance->device, vertexHandle, nullptr); |
|
|
|
vkDestroyBuffer(instance->device, handle, nullptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Buffer* Buffer::createStagingBuffer(Instance* instance) { |
|
|
|
|
|
|
|
VkDeviceSize size = vertices.size() * sizeof(Vertex); |
|
|
|
|
|
|
|
VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
|
|
|
|
|
|
|
VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto stagingBuffer = new Buffer(instance, size, usage, properties); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* data; |
|
|
|
|
|
|
|
vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, &data); |
|
|
|
|
|
|
|
memcpy(data, vertices.data(), size); |
|
|
|
|
|
|
|
vkUnmapMemory(instance->device, stagingBuffer->memory); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return stagingBuffer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { |
|
|
|
|
|
|
|
VkCommandBufferAllocateInfo allocateInfo {}; |
|
|
|
|
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
|
|
|
|
|
|
|
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
|
|
|
|
|
|
|
allocateInfo.commandPool = commandPool->handle; |
|
|
|
|
|
|
|
allocateInfo.commandBufferCount = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkCommandBuffer commandBuffer; |
|
|
|
|
|
|
|
vkAllocateCommandBuffers(instance->device, &allocateInfo, &commandBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo); |
|
|
|
|
|
|
|
VkBufferCopy copyRegion {}; |
|
|
|
|
|
|
|
copyRegion.size = size; |
|
|
|
|
|
|
|
vkCmdCopyBuffer(commandBuffer, handle, dst->handle, 1, ©Region); |
|
|
|
|
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkSubmitInfo submitInfo {}; |
|
|
|
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
|
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkQueueSubmit(instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); |
|
|
|
|
|
|
|
vkDeviceWaitIdle(instance->device); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkFreeCommandBuffers(instance->device, commandPool->handle, 1, &commandBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|