|
|
|
@ -5,31 +5,31 @@ |
|
|
|
|
#include "instance.hpp" |
|
|
|
|
#include "command_pool.hpp" |
|
|
|
|
|
|
|
|
|
Buffer::Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : instance(instance), size(size){ |
|
|
|
|
Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : size(size){ |
|
|
|
|
VkBufferCreateInfo bufferInfo{}; |
|
|
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
|
|
|
|
bufferInfo.size = size; |
|
|
|
|
bufferInfo.usage = usage; |
|
|
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
|
|
|
|
|
|
|
|
|
vkCreateBuffer(instance->device, &bufferInfo, nullptr, &handle); |
|
|
|
|
vkCreateBuffer(Instance::instance->device, &bufferInfo, nullptr, &handle); |
|
|
|
|
|
|
|
|
|
VkMemoryRequirements memoryRequirements; |
|
|
|
|
vkGetBufferMemoryRequirements(instance->device, handle, &memoryRequirements); |
|
|
|
|
vkGetBufferMemoryRequirements(Instance::instance->device, handle, &memoryRequirements); |
|
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo allocateInfo {}; |
|
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
|
|
|
|
allocateInfo.allocationSize = memoryRequirements.size; |
|
|
|
|
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); |
|
|
|
|
|
|
|
|
|
vkAllocateMemory(instance->device, &allocateInfo, nullptr, &memory); |
|
|
|
|
vkAllocateMemory(Instance::instance->device, &allocateInfo, nullptr, &memory); |
|
|
|
|
|
|
|
|
|
vkBindBufferMemory(instance->device, handle, memory, 0); |
|
|
|
|
vkBindBufferMemory(Instance::instance->device, handle, memory, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) { |
|
|
|
|
VkPhysicalDeviceMemoryProperties memoryProperties; |
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(instance->physicalDevice, &memoryProperties); |
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(Instance::instance->physicalDevice, &memoryProperties); |
|
|
|
|
|
|
|
|
|
for (uint32_t type = 0; type < memoryProperties.memoryTypeCount; type++){ |
|
|
|
|
if ((typeFilter & (1 << type)) && (memoryProperties.memoryTypes[type].propertyFlags & propertyFlags)){ |
|
|
|
@ -41,35 +41,48 @@ uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags prope |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Buffer::~Buffer() { |
|
|
|
|
vkFreeMemory(instance->device, memory, nullptr); |
|
|
|
|
vkDestroyBuffer(instance->device, handle, nullptr); |
|
|
|
|
vkFreeMemory(Instance::instance->device, memory, nullptr); |
|
|
|
|
vkDestroyBuffer(Instance::instance->device, handle, nullptr); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Buffer* Buffer::createStagedVertexBuffer(Instance* instance) { |
|
|
|
|
Buffer* Buffer::createStagedVertexBuffer() { |
|
|
|
|
std::vector<Vertex> vertices = { |
|
|
|
|
{{0.0, -0.5, 0}, {1, 0, 0}}, |
|
|
|
|
{{0.5, 0.5, 0}, {0, 1, 0}}, |
|
|
|
|
{{-0.5, 0.5, 0}, {0, 0, 1}}, |
|
|
|
|
{{0.8, -0.5, 0}, {1, 1, 1}} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
vertices.insert(vertices.end(), vertices.begin(), vertices.end()); |
|
|
|
|
|
|
|
|
|
for (size_t i = 4; i < vertices.size(); i++){ |
|
|
|
|
vertices[i].pos.z -= 0.5f; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
auto stagingBuffer = new Buffer(size, usage, properties); |
|
|
|
|
|
|
|
|
|
void* data; |
|
|
|
|
vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, &data); |
|
|
|
|
vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, &data); |
|
|
|
|
memcpy(data, vertices.data(), size); |
|
|
|
|
vkUnmapMemory(instance->device, stagingBuffer->memory); |
|
|
|
|
vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); |
|
|
|
|
|
|
|
|
|
return stagingBuffer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Buffer *Buffer::createStagedIndexBuffer(Instance *instance) { |
|
|
|
|
Buffer *Buffer::createStagedIndexBuffer() { |
|
|
|
|
VkDeviceSize size = indices.size() * sizeof(uint32_t); |
|
|
|
|
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); |
|
|
|
|
auto stagingBuffer = new Buffer(size, usage, properties); |
|
|
|
|
|
|
|
|
|
uint32_t *data; |
|
|
|
|
vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, reinterpret_cast<void**>(&data)); |
|
|
|
|
vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, reinterpret_cast<void**>(&data)); |
|
|
|
|
memcpy(data, indices.data(), sizeof(uint32_t) * indices.size()); |
|
|
|
|
vkUnmapMemory(instance->device, stagingBuffer->memory); |
|
|
|
|
vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); |
|
|
|
|
|
|
|
|
|
return stagingBuffer; |
|
|
|
|
} |
|
|
|
@ -82,7 +95,7 @@ void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { |
|
|
|
|
allocateInfo.commandBufferCount = 1; |
|
|
|
|
|
|
|
|
|
VkCommandBuffer commandBuffer; |
|
|
|
|
vkAllocateCommandBuffers(instance->device, &allocateInfo, &commandBuffer); |
|
|
|
|
vkAllocateCommandBuffers(Instance::instance->device, &allocateInfo, &commandBuffer); |
|
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
@ -99,8 +112,8 @@ void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { |
|
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
|
submitInfo.pCommandBuffers = &commandBuffer; |
|
|
|
|
|
|
|
|
|
vkQueueSubmit(instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); |
|
|
|
|
vkDeviceWaitIdle(instance->device); |
|
|
|
|
vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); |
|
|
|
|
vkDeviceWaitIdle(Instance::instance->device); |
|
|
|
|
|
|
|
|
|
vkFreeCommandBuffers(instance->device, commandPool->handle, 1, &commandBuffer); |
|
|
|
|
vkFreeCommandBuffers(Instance::instance->device, commandPool->handle, 1, &commandBuffer); |
|
|
|
|
} |
|
|
|
|