Skip to content

Commit

Permalink
Refactored old Buffer into new DataBuffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
janhsimon committed Dec 30, 2022
1 parent 4cae182 commit 2c92533
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 113 deletions.
34 changes: 0 additions & 34 deletions src/Buffer.h

This file was deleted.

6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ set(SHADER_SRC

set(SRC
Main.cpp

Buffer.cpp
Buffer.h

Context.cpp
Context.h

Controllers.cpp
Controllers.h

DataBuffer.cpp
DataBuffer.h

Headset.cpp
Headset.h

Expand Down
57 changes: 22 additions & 35 deletions src/Buffer.cpp → src/DataBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "Buffer.h"
#include "DataBuffer.h"

#include "Context.h"
#include "Util.h"

#include <sstream>

Buffer::Buffer(const VkDevice device,
const VkPhysicalDevice physicalDevice,
const VkBufferUsageFlags bufferUsageFlags,
const VkMemoryPropertyFlags memoryProperties,
const VkDeviceSize size)
: device(device), size(size)
DataBuffer::DataBuffer(const Context* context,
const VkBufferUsageFlags bufferUsageFlags,
const VkMemoryPropertyFlags memoryProperties,
const VkDeviceSize size)
: context(context), size(size)
{
const VkDevice device = context->getVkDevice();

VkBufferCreateInfo bufferCreateInfo{ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufferCreateInfo.size = size;
bufferCreateInfo.usage = bufferUsageFlags;
Expand All @@ -25,28 +27,12 @@ Buffer::Buffer(const VkDevice device,
VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memoryRequirements);

VkPhysicalDeviceMemoryProperties supportedMemoryProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &supportedMemoryProperties);

const VkMemoryPropertyFlags typeFilter = memoryRequirements.memoryTypeBits;
uint32_t suitableMemoryTypeIndex = 0u;
bool memoryTypeFound = false;
for (uint32_t memoryTypeIndex = 0u; memoryTypeIndex < supportedMemoryProperties.memoryTypeCount; ++memoryTypeIndex)
{
const VkMemoryPropertyFlags propertyFlags = supportedMemoryProperties.memoryTypes[memoryTypeIndex].propertyFlags;
if (typeFilter & (1 << memoryTypeIndex) && (propertyFlags & memoryProperties) == memoryProperties)
{
suitableMemoryTypeIndex = memoryTypeIndex;
memoryTypeFound = true;
break;
}
}

if (!memoryTypeFound)
if (!util::findSuitableMemoryTypeIndex(context->getVkPhysicalDevice(), memoryRequirements, memoryProperties,
suitableMemoryTypeIndex))
{
util::error(Error::FeatureNotSupported, "Suitable buffer memory type");
util::error(Error::FeatureNotSupported, "Suitable data buffer memory type");
valid = false;
return;
}

VkMemoryAllocateInfo memoryAllocateInfo{ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
Expand All @@ -69,8 +55,9 @@ Buffer::Buffer(const VkDevice device,
}
}

Buffer::~Buffer()
DataBuffer::~DataBuffer()
{
const VkDevice device = context->getVkDevice();
if (device)
{
if (deviceMemory)
Expand All @@ -85,7 +72,7 @@ Buffer::~Buffer()
}
}

bool Buffer::copyTo(const Buffer& target, VkCommandBuffer commandBuffer, VkQueue queue) const
bool DataBuffer::copyTo(const DataBuffer& target, VkCommandBuffer commandBuffer, VkQueue queue) const
{
VkCommandBufferBeginInfo beginInfo{ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
Expand All @@ -97,7 +84,7 @@ bool Buffer::copyTo(const Buffer& target, VkCommandBuffer commandBuffer, VkQueue

VkBufferCopy copyRegion{};
copyRegion.size = size;
vkCmdCopyBuffer(commandBuffer, buffer, target.getVkBuffer(), 1u, &copyRegion);
vkCmdCopyBuffer(commandBuffer, buffer, target.getBuffer(), 1u, &copyRegion);

if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
Expand All @@ -123,10 +110,10 @@ bool Buffer::copyTo(const Buffer& target, VkCommandBuffer commandBuffer, VkQueue
return true;
}

void* Buffer::map() const
void* DataBuffer::map() const
{
void* data;
VkResult result = vkMapMemory(device, deviceMemory, 0u, size, 0, &data);
const VkResult result = vkMapMemory(context->getVkDevice(), deviceMemory, 0u, size, 0, &data);
if (result != VK_SUCCESS)
{
util::error(Error::GenericVulkan);
Expand All @@ -136,17 +123,17 @@ void* Buffer::map() const
return data;
}

void Buffer::unmap() const
void DataBuffer::unmap() const
{
vkUnmapMemory(device, deviceMemory);
vkUnmapMemory(context->getVkDevice(), deviceMemory);
}

bool Buffer::isValid() const
bool DataBuffer::isValid() const
{
return valid;
}

VkBuffer Buffer::getVkBuffer() const
VkBuffer DataBuffer::getBuffer() const
{
return buffer;
}
35 changes: 35 additions & 0 deletions src/DataBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <vulkan/vulkan.h>

class Context;

/*
* The data buffer class is used to store Vulkan data buffers, namely the uniform buffer and the vertex/index buffer. It
* is unrelated to Vulkan image buffers used for the depth buffer for example. Note that is good for performance to keep
* Vulkan buffers mapped until destruction. This class offers functionality to do so, but doesn't enforce the principle.
*/
class DataBuffer final
{
public:
DataBuffer(const Context* context,
VkBufferUsageFlags bufferUsageFlags,
VkMemoryPropertyFlags memoryProperties,
VkDeviceSize size);
~DataBuffer();

bool copyTo(const DataBuffer& target, VkCommandBuffer commandBuffer, VkQueue queue) const;
void* map() const;
void unmap() const;

bool isValid() const;
VkBuffer getBuffer() const;

private:
bool valid = true;

const Context* context = nullptr;
VkBuffer buffer = nullptr;
VkDeviceMemory deviceMemory = nullptr;
VkDeviceSize size = 0u;
};
31 changes: 9 additions & 22 deletions src/ImageBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,24 @@ ImageBuffer::ImageBuffer(const Context* context,
return;
}

// Find a suitable memory type index
VkMemoryRequirements memoryRequirements;
vkGetImageMemoryRequirements(device, image, &memoryRequirements);

VkPhysicalDeviceMemoryProperties supportedMemoryProperties;
vkGetPhysicalDeviceMemoryProperties(context->getVkPhysicalDevice(), &supportedMemoryProperties);

const VkMemoryPropertyFlags memoryProperties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
const VkMemoryPropertyFlags typeFilter = memoryRequirements.memoryTypeBits;
uint32_t suitableMemoryTypeIndex = 0u;
bool memoryTypeFound = false;
for (uint32_t memoryTypeIndex = 0u; memoryTypeIndex < supportedMemoryProperties.memoryTypeCount; ++memoryTypeIndex)
{
const VkMemoryPropertyFlags propertyFlags = supportedMemoryProperties.memoryTypes[memoryTypeIndex].propertyFlags;
if (typeFilter & (1 << memoryTypeIndex) && (propertyFlags & memoryProperties) == memoryProperties)
{
suitableMemoryTypeIndex = memoryTypeIndex;
memoryTypeFound = true;
break;
}
}

if (!memoryTypeFound)
if (!util::findSuitableMemoryTypeIndex(context->getVkPhysicalDevice(), memoryRequirements,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, suitableMemoryTypeIndex))
{
util::error(Error::FeatureNotSupported, "Suitable image buffer memory type");
valid = false;
return;
}

// Allocate the device memory for the buffer
VkMemoryAllocateInfo memoryAllocateInfo{ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
memoryAllocateInfo.allocationSize = memoryRequirements.size;
memoryAllocateInfo.memoryTypeIndex = suitableMemoryTypeIndex;
if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &memory) != VK_SUCCESS)
if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &deviceMemory) != VK_SUCCESS)
{
std::stringstream s;
s << memoryRequirements.size << " bytes for image buffer";
Expand All @@ -76,7 +62,8 @@ ImageBuffer::ImageBuffer(const Context* context,
return;
}

if (vkBindImageMemory(device, image, memory, 0u) != VK_SUCCESS)
// Bind the image to the allocated device memory
if (vkBindImageMemory(device, image, deviceMemory, 0u) != VK_SUCCESS)
{
util::error(Error::GenericVulkan);
valid = false;
Expand Down Expand Up @@ -113,9 +100,9 @@ ImageBuffer::~ImageBuffer()
vkDestroyImageView(device, imageView, nullptr);
}

if (memory)
if (deviceMemory)
{
vkFreeMemory(device, memory, nullptr);
vkFreeMemory(device, deviceMemory, nullptr);
}

if (image)
Expand Down
3 changes: 1 addition & 2 deletions src/ImageBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class ImageBuffer final
bool valid = true;

const Context* context = nullptr;

VkImage image = nullptr;
VkDeviceMemory memory = nullptr;
VkDeviceMemory deviceMemory = nullptr;
VkImageView imageView = nullptr;
};
8 changes: 4 additions & 4 deletions src/RenderProcess.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "RenderProcess.h"

#include "Buffer.h"
#include "Context.h"
#include "DataBuffer.h"
#include "Util.h"

#include <cstring>
Expand Down Expand Up @@ -86,8 +86,8 @@ RenderProcess::RenderProcess(const Context* context,
// Create an empty uniform buffer
const VkDeviceSize uniformBufferSize = descriptorBufferInfos.at(2u).offset + descriptorBufferInfos.at(2u).range;
uniformBuffer =
new Buffer(device, context->getVkPhysicalDevice(), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBufferSize);
new DataBuffer(context, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBufferSize);
if (!uniformBuffer->isValid())
{
valid = false;
Expand Down Expand Up @@ -118,7 +118,7 @@ RenderProcess::RenderProcess(const Context* context,
// Associate the uniform buffer with each descriptor buffer info
for (VkDescriptorBufferInfo& descriptorBufferInfo : descriptorBufferInfos)
{
descriptorBufferInfo.buffer = uniformBuffer->getVkBuffer();
descriptorBufferInfo.buffer = uniformBuffer->getBuffer();
}

// Update the descriptor sets
Expand Down
4 changes: 2 additions & 2 deletions src/RenderProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <array>
#include <vector>

class Buffer;
class Context;
class DataBuffer;

/*
* The render process class consolidates all the resources that needs to be duplicated for each frame that can be
Expand Down Expand Up @@ -59,7 +59,7 @@ class RenderProcess final
VkCommandBuffer commandBuffer = nullptr;
VkSemaphore drawableSemaphore = nullptr, presentableSemaphore = nullptr;
VkFence busyFence = nullptr;
Buffer* uniformBuffer = nullptr;
DataBuffer* uniformBuffer = nullptr;
void* uniformBufferMemory = nullptr;
VkDescriptorSet descriptorSet = nullptr;
};
18 changes: 9 additions & 9 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Renderer.h"

#include "Buffer.h"
#include "Context.h"
#include "DataBuffer.h"
#include "Headset.h"
#include "MeshData.h"
#include "Model.h"
Expand Down Expand Up @@ -156,9 +156,9 @@ Renderer::Renderer(const Context* context,
{
// Create a staging buffer
const VkDeviceSize bufferSize = static_cast<VkDeviceSize>(meshData->getSize());
Buffer* stagingBuffer =
new Buffer(device, vkPhysicalDevice, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize);
DataBuffer* stagingBuffer =
new DataBuffer(context, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize);
if (!stagingBuffer->isValid())
{
valid = false;
Expand All @@ -177,10 +177,10 @@ Renderer::Renderer(const Context* context,
stagingBuffer->unmap();

// Create an empty target buffer
vertexIndexBuffer = new Buffer(device, vkPhysicalDevice,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, bufferSize);
vertexIndexBuffer = new DataBuffer(context,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, bufferSize);
if (!vertexIndexBuffer->isValid())
{
valid = false;
Expand Down Expand Up @@ -311,7 +311,7 @@ void Renderer::render(size_t swapchainImageIndex, float time)

// Bind the vertex section of the geometry buffer
VkDeviceSize vertexOffset = 0u;
const VkBuffer buffer = vertexIndexBuffer->getVkBuffer();
const VkBuffer buffer = vertexIndexBuffer->getBuffer();
vkCmdBindVertexBuffers(commandBuffer, 0u, 1u, &buffer, &vertexOffset);

// Bind the index section of the geometry buffer
Expand Down
Loading

0 comments on commit 2c92533

Please sign in to comment.