Skip to content

Commit

Permalink
Move buffer creation to asset manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjaltesorgenfrei committed Mar 2, 2023
1 parent 430c779 commit 96a28f6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/Overv/VulkanTutorial/pull/320>
- [ ] 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
Expand Down
10 changes: 8 additions & 2 deletions src/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class AssetManager {
template <typename T>
[[nodiscard]] std::vector<std::shared_ptr<AllocatedBuffer>> createBuffers(std::span<T> data, vk::BufferUsageFlags bufferUsage, size_t count = 1);

template <typename T>
[[nodiscard]] std::shared_ptr<AllocatedBuffer> createBuffer(std::span<T> data, vk::BufferUsageFlags bufferUsage)
{
return createBuffers<T>(data, bufferUsage, 1)[0];
}

private:
std::shared_ptr<BehDevice> device;

Expand Down Expand Up @@ -95,7 +101,7 @@ inline std::vector<std::shared_ptr<AllocatedBuffer>> AssetManager::createBuffers
VkBufferCreateInfo createInfo {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = static_cast<VkBufferUsageFlags>(bufferUsage)
.usage = static_cast<VkBufferUsageFlags>(bufferUsage | vk::BufferUsageFlagBits::eTransferDst)
};

VmaAllocationCreateInfo allocInfo {
Expand Down Expand Up @@ -125,4 +131,4 @@ inline std::vector<std::shared_ptr<AllocatedBuffer>> AssetManager::createBuffers
});
cleanUpBuffer(stagedBuffer);
return buffers;
}
}
4 changes: 2 additions & 2 deletions src/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct Mesh {
std::vector<Vertex> _vertices;
std::vector<uint32_t> _indices;
std::vector<std::string> _texturePaths; // TODO: Fix this, it's not a good spot to have it saved.
AllocatedBuffer _vertexBuffer;
AllocatedBuffer _indexBuffer;
std::shared_ptr<AllocatedBuffer> _vertexBuffer;
std::shared_ptr<AllocatedBuffer> _indexBuffer;

static std::shared_ptr<Mesh> LoadFromObj(const char* filename);
};
4 changes: 2 additions & 2 deletions src/RenderObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ RenderObject::RenderObject(std::shared_ptr<Mesh> 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<uint32_t>(mesh->_indices.size()), 1, 0, 0, 0);
}
72 changes: 5 additions & 67 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ void Renderer::createComputeShaderBuffers() {
}

auto usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst;
shaderStorageBuffers = assetManager.createBuffers(static_cast<std::span<Particle>>(particles), usage, swapChainImages.size());
shaderStorageBuffers = assetManager.createBuffers<Particle>(particles, usage, swapChainImages.size());
}

void Renderer::createFramebuffers() {
Expand Down Expand Up @@ -766,74 +766,12 @@ uint32_t Renderer::findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags p

void Renderer::uploadMeshes(const std::vector<std::shared_ptr<RenderObject>> &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<Vertex>(model->mesh->_vertices, vk::BufferUsageFlagBits::eVertexBuffer);
model->mesh->_indexBuffer = assetManager.createBuffer<uint32_t>(model->mesh->_indices, vk::BufferUsageFlagBits::eIndexBuffer);
model->material = createMaterial(model->mesh->_texturePaths);
}
}

template<typename T>
AllocatedBuffer Renderer::uploadBuffer(std::vector<T> &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{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ class Renderer {
bool hasStencilComponent(vk::Format format);
vk::Format findSupportedFormat(const std::vector<vk::Format>& candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features);

template <typename T>
AllocatedBuffer uploadBuffer(std::vector<T>& meshData, VkBufferUsageFlags usage);

void transitionImageLayout(vk::Image image, vk::Format format, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, uint32_t mipLevels);

void createUniformBuffers();
Expand Down

0 comments on commit 96a28f6

Please sign in to comment.