From 96a28f6f254b2e7223a7b368c2284296a526e709 Mon Sep 17 00:00:00 2001 From: Hjalte Sorgenfrei Mac Dalland Date: Thu, 2 Mar 2023 15:53:25 +0100 Subject: [PATCH] Move buffer creation to asset manager --- README.md | 2 +- src/AssetManager.h | 10 ++++-- src/Mesh.h | 4 +-- src/RenderObject.cpp | 4 +-- src/Renderer.cpp | 72 +++----------------------------------------- src/Renderer.h | 3 -- 6 files changed, 18 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 4e59ce5..295a110 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Some ideas were also taken from [Zeux's blog](https://zeux.io/2020/02/27/writing - [x] use imageless framebuffer - [x] If the items in 4 and 5 are implemented, the resize function will not need to call vkDeviceWaitIdle() - [x] Implement Compute shaders -- [ ] Delete `Renderer::uploadBuffer` it should be handled by `AssetManager` +- [x] Delete `Renderer::uploadBuffer` it should be handled by `AssetManager` - [ ] Create multiple vk::Framebuffer swapChainFramebuffers. ### Descriptor Layout Idea diff --git a/src/AssetManager.h b/src/AssetManager.h index 98c0f3f..582a87b 100644 --- a/src/AssetManager.h +++ b/src/AssetManager.h @@ -33,6 +33,12 @@ class AssetManager { template [[nodiscard]] std::vector> createBuffers(std::span data, vk::BufferUsageFlags bufferUsage, size_t count = 1); + template + [[nodiscard]] std::shared_ptr createBuffer(std::span data, vk::BufferUsageFlags bufferUsage) + { + return createBuffers(data, bufferUsage, 1)[0]; + } + private: std::shared_ptr device; @@ -95,7 +101,7 @@ inline std::vector> AssetManager::createBuffers VkBufferCreateInfo createInfo { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size, - .usage = static_cast(bufferUsage) + .usage = static_cast(bufferUsage | vk::BufferUsageFlagBits::eTransferDst) }; VmaAllocationCreateInfo allocInfo { @@ -125,4 +131,4 @@ inline std::vector> AssetManager::createBuffers }); cleanUpBuffer(stagedBuffer); return buffers; -} \ No newline at end of file +} diff --git a/src/Mesh.h b/src/Mesh.h index 420bb1c..0b3aaf1 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -30,8 +30,8 @@ struct Mesh { std::vector _vertices; std::vector _indices; std::vector _texturePaths; // TODO: Fix this, it's not a good spot to have it saved. - AllocatedBuffer _vertexBuffer; - AllocatedBuffer _indexBuffer; + std::shared_ptr _vertexBuffer; + std::shared_ptr _indexBuffer; static std::shared_ptr LoadFromObj(const char* filename); }; \ No newline at end of file diff --git a/src/RenderObject.cpp b/src/RenderObject.cpp index 55daf92..9605266 100644 --- a/src/RenderObject.cpp +++ b/src/RenderObject.cpp @@ -7,11 +7,11 @@ RenderObject::RenderObject(std::shared_ptr mesh, Material material) { } void RenderObject::Draw(vk::CommandBuffer commandBuffer) { - vk::Buffer vertexBuffers[] = {mesh->_vertexBuffer._buffer}; + vk::Buffer vertexBuffers[] = {mesh->_vertexBuffer->_buffer}; vk::DeviceSize offsets[] = {0}; commandBuffer.bindVertexBuffers(0, 1, vertexBuffers, offsets); - commandBuffer.bindIndexBuffer(mesh->_indexBuffer._buffer, 0, vk::IndexType::eUint32); + commandBuffer.bindIndexBuffer(mesh->_indexBuffer->_buffer, 0, vk::IndexType::eUint32); commandBuffer.drawIndexed(static_cast(mesh->_indices.size()), 1, 0, 0, 0); } \ No newline at end of file diff --git a/src/Renderer.cpp b/src/Renderer.cpp index fe96e74..a4189c1 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -592,7 +592,7 @@ void Renderer::createComputeShaderBuffers() { } auto usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst; - shaderStorageBuffers = assetManager.createBuffers(static_cast>(particles), usage, swapChainImages.size()); + shaderStorageBuffers = assetManager.createBuffers(particles, usage, swapChainImages.size()); } void Renderer::createFramebuffers() { @@ -766,74 +766,12 @@ uint32_t Renderer::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags p void Renderer::uploadMeshes(const std::vector> &objects) { for (const auto &model: objects) { - model->mesh->_vertexBuffer = uploadBuffer(model->mesh->_vertices, - VkBufferUsageFlagBits::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); - model->mesh->_indexBuffer = uploadBuffer(model->mesh->_indices, - VkBufferUsageFlagBits::VK_BUFFER_USAGE_INDEX_BUFFER_BIT); - mainDeletionQueue.push_function([&, model]() { - vmaDestroyBuffer(device->allocator(), model->mesh->_vertexBuffer._buffer, - model->mesh->_vertexBuffer._allocation); - vmaDestroyBuffer(device->allocator(), model->mesh->_indexBuffer._buffer, - model->mesh->_indexBuffer._allocation); - }); + model->mesh->_vertexBuffer = assetManager.createBuffer(model->mesh->_vertices, vk::BufferUsageFlagBits::eVertexBuffer); + model->mesh->_indexBuffer = assetManager.createBuffer(model->mesh->_indices, vk::BufferUsageFlagBits::eIndexBuffer); model->material = createMaterial(model->mesh->_texturePaths); } } -template -AllocatedBuffer Renderer::uploadBuffer(std::vector &meshData, VkBufferUsageFlags usage) { - VkBufferCreateInfo stagingCreate{ - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .size = meshData.size() * sizeof(T), - .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT - }; - - VmaAllocationCreateInfo stagingAlloc{ - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, - .usage = VMA_MEMORY_USAGE_AUTO - }; - - VkBuffer stagingBuffer; - VmaAllocation stagingAllocation; - - if (vmaCreateBuffer(device->allocator(), &stagingCreate, &stagingAlloc, &stagingBuffer, &stagingAllocation, - nullptr) != VK_SUCCESS) { - throw std::runtime_error("Failed to uploadBuffer mesh vertices!"); - } - - void *data; - vmaMapMemory(device->allocator(), stagingAllocation, &data); - { - memcpy(data, meshData.data(), meshData.size() * sizeof(T)); - } - vmaUnmapMemory(device->allocator(), stagingAllocation); - - VkBufferCreateInfo bufferCreateInfo{ - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .size = meshData.size() * sizeof(T), - .usage = usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT - }; - - VmaAllocationCreateInfo vmaAllocInfo{ - .usage = VMA_MEMORY_USAGE_GPU_ONLY - }; - - VkBuffer buffer; - - AllocatedBuffer allocatedBuffer{}; - - if (vmaCreateBuffer(device->allocator(), &bufferCreateInfo, &vmaAllocInfo, &buffer, &allocatedBuffer._allocation, - nullptr) != VK_SUCCESS) { - throw std::runtime_error("Failed to upload buffer!"); - } - allocatedBuffer._buffer = buffer; - - copyBuffer(stagingBuffer, buffer, meshData.size() * sizeof(T)); - - vmaDestroyBuffer(device->allocator(), stagingBuffer, stagingAllocation); - return allocatedBuffer; -} - void Renderer::createUniformBuffers() { uniformBuffers.resize(swapChainImages.size()); VkBufferCreateInfo create{ @@ -1116,11 +1054,11 @@ void Renderer::recordCommandBuffer(vk::CommandBuffer &commandBuffer, size_t inde &descriptorSets[currentFrame], 0, nullptr); for (const auto &model: frameInfo.objects) { - vk::Buffer vertexBuffers[] = {model->mesh->_vertexBuffer._buffer}; + vk::Buffer vertexBuffers[] = {model->mesh->_vertexBuffer->_buffer}; vk::DeviceSize offsets[] = {0}; commandBuffer.bindVertexBuffers(0, 1, vertexBuffers, offsets); - commandBuffer.bindIndexBuffer(model->mesh->_indexBuffer._buffer, 0, vk::IndexType::eUint32); + commandBuffer.bindIndexBuffer(model->mesh->_indexBuffer->_buffer, 0, vk::IndexType::eUint32); commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 1, 1, &model->material.textureSet, 0, nullptr); diff --git a/src/Renderer.h b/src/Renderer.h index 0f2acf2..30e9b7a 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -155,9 +155,6 @@ class Renderer { bool hasStencilComponent(vk::Format format); vk::Format findSupportedFormat(const std::vector& candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features); - template - AllocatedBuffer uploadBuffer(std::vector& meshData, VkBufferUsageFlags usage); - void transitionImageLayout(vk::Image image, vk::Format format, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, uint32_t mipLevels); void createUniformBuffers();