Skip to content

Commit

Permalink
use vma for buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
kusma committed Aug 3, 2024
1 parent 1a0102e commit e14c5be
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static Texture3D loadFractalNoise(const std::string &filename, int width, int he

auto size = sizeof(float) * 4 * width * height * depth;
auto stagingBuffer = new StagingBuffer(size);
void *ptr = stagingBuffer->map(0, size);
void *ptr = stagingBuffer->map();

FILE *fp = fopen(filename.c_str(), "rb");
if (!fp)
Expand Down Expand Up @@ -280,7 +280,7 @@ Texture3D importCubeFile(const std::string &filename)

auto textureSize = sizeof(float) * 4 * size * size * size;
stagingBuffer = new StagingBuffer(textureSize);
ptr = static_cast<float *>(stagingBuffer->map(0, textureSize));
ptr = static_cast<float *>(stagingBuffer->map());

continue;
}
Expand Down Expand Up @@ -1131,7 +1131,7 @@ int main(int argc, char *argv[])
refractionUniforms.fade = float(sync_get_val(refractionFadeTrack, row));
refractionUniforms.refractiveIndex = float(sync_get_val(refractionIndexTrack, row));

auto ptr = refractionUniformBuffer->map(0, sizeof(refractionUniforms));
auto ptr = refractionUniformBuffer->map();
memcpy(ptr, &refractionUniforms, sizeof(refractionUniforms));
refractionUniformBuffer->unmap();

Expand All @@ -1152,7 +1152,7 @@ int main(int argc, char *argv[])
sync_get_val(wavePlaneScaleYTrack, row));
wavePlaneUniforms.time = float(sync_get_val(wavePlaneTimeTrack, row));

auto ptr = wavePlaneUniformBuffer->map(0, sizeof(wavePlaneUniforms));
auto ptr = wavePlaneUniformBuffer->map();
memcpy(ptr, &wavePlaneUniforms, sizeof(wavePlaneUniforms));
wavePlaneUniformBuffer->unmap();

Expand Down
28 changes: 12 additions & 16 deletions src/scene/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@

using namespace vkHelpers;

Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags) :
Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags usageFlags, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocFlags) :
size(size)
{
VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.size = size;
bufferCreateInfo.usage = usageFlags;

assumeSuccess(vkCreateBuffer(vkInstance::device, &bufferCreateInfo, nullptr, &buffer));

VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(vkInstance::device, buffer, &memoryRequirements);

auto memoryTypeIndex = getMemoryTypeIndex(vkInstance::deviceMemoryProperties, memoryRequirements, memoryPropertyFlags);
deviceMemory = allocateDeviceMemory(vkInstance::device, memoryRequirements.size, memoryTypeIndex);

assumeSuccess(vkBindBufferMemory(vkInstance::device, buffer, deviceMemory, 0));
VkBufferCreateInfo bufferCreateInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = usageFlags,
};
VmaAllocationCreateInfo allocInfo = {
.flags = allocFlags,
.usage = memoryUsage,
};
assumeSuccess(vmaCreateBuffer(vkInstance::allocator, &bufferCreateInfo, &allocInfo, &buffer, &allocation, nullptr));
}

Buffer::~Buffer()
{
vkDestroyBuffer(vkInstance::device, buffer, nullptr);
vkFreeMemory(vkInstance::device, deviceMemory, nullptr);
vmaFreeMemory(vkInstance::allocator, allocation);
}

void Buffer::uploadFromStagingBuffer(StagingBuffer *stagingBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size)
Expand Down
18 changes: 9 additions & 9 deletions src/scene/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ class StagingBuffer;

class Buffer {
public:
Buffer(VkDeviceSize size, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags);
Buffer(VkDeviceSize size, VkBufferUsageFlags usageFlags, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags allocFlags);
~Buffer();

void *map(VkDeviceSize offset, VkDeviceSize size)
void *map()
{
void *ret;
vkHelpers::assumeSuccess(vkMapMemory(vkInstance::device, deviceMemory, offset, size, 0, &ret));
vkHelpers::assumeSuccess(vmaMapMemory(vkInstance::allocator, allocation, &ret));
return ret;
}

void unmap()
{
vkUnmapMemory(vkInstance::device, deviceMemory);
vmaUnmapMemory(vkInstance::allocator, allocation);
}

void uploadMemory(VkDeviceSize offset, void *data, VkDeviceSize size)
void uploadMemory(void *data, VkDeviceSize size)
{
auto mappedUniformMemory = map(offset, size);
auto mappedUniformMemory = map();
memcpy(mappedUniformMemory, data, (size_t)size);
unmap();
}
Expand All @@ -49,19 +49,19 @@ class Buffer {
private:
VkBuffer buffer;
VkDeviceSize size;
VkDeviceMemory deviceMemory;
VmaAllocation allocation;
};

class StagingBuffer : public Buffer {
public:
StagingBuffer(VkDeviceSize size) : Buffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
StagingBuffer(VkDeviceSize size) : Buffer(size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT)
{
}
};

class UniformBuffer : public Buffer {
public:
UniformBuffer(VkDeviceSize size) : Buffer(size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
UniformBuffer(VkDeviceSize size) : Buffer(size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT)
{
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/scene/import-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static StagingBuffer *copyToStagingBuffer(FIBITMAP *dib)
auto size = pitch * height;

auto stagingBuffer = new StagingBuffer(size);
void *ptr = stagingBuffer->map(0, size);
void *ptr = stagingBuffer->map();

for (auto y = 0u; y < height; ++y) {
auto srcRow = FreeImage_GetScanLine(dib, y);
Expand Down
8 changes: 4 additions & 4 deletions src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ IndexedBatch meshToIndexedBatch(const Mesh &mesh)
auto indices = mesh.getIndices();

auto vertexStagingBuffer = new StagingBuffer(vertices.size());
vertexStagingBuffer->uploadMemory(0, vertices.data(), vertices.size());
vertexStagingBuffer->uploadMemory(vertices.data(), vertices.size());

auto vertexBuffer = new Buffer(vertices.size(), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
auto vertexBuffer = new Buffer(vertices.size(), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_AUTO, 0);
vertexBuffer->uploadFromStagingBuffer(vertexStagingBuffer, 0, 0, vertices.size());

auto indexStagingBuffer = new StagingBuffer(indices.size());
indexStagingBuffer->uploadMemory(0, indices.data(), indices.size());
indexStagingBuffer->uploadMemory(indices.data(), indices.size());

auto indexBuffer = new Buffer(indices.size(), VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
auto indexBuffer = new Buffer(indices.size(), VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_AUTO, 0);
indexBuffer->uploadFromStagingBuffer(indexStagingBuffer, 0, 0, indices.size());

VkIndexType indexType = VK_INDEX_TYPE_UINT16; // dummy
Expand Down
2 changes: 1 addition & 1 deletion src/scenerenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void SceneRenderer::draw(VkCommandBuffer commandBuffer, const glm::mat4 &viewMat
auto offset = 0u;
map<const Transform*, unsigned int> offsetMap;
auto transforms = scene->getTransforms();
auto ptr = uniformBuffer->map(0, uniformBufferSpacing * transforms.size());
auto ptr = uniformBuffer->map();

for (auto transform : transforms) {
auto modelMatrix = glm::mat4(1); // transform->getAbsoluteMatrix();
Expand Down
44 changes: 44 additions & 0 deletions src/vkinstance.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define VOLK_IMPLEMENTATION
#define VMA_IMPLEMENTATION

#include "vkinstance.h"

#include "core/core.h"
Expand Down Expand Up @@ -29,6 +31,7 @@ VkPhysicalDeviceMemoryProperties vkInstance::deviceMemoryProperties;
uint32_t vkInstance::graphicsQueueIndex = UINT32_MAX;
VkQueue vkInstance::graphicsQueue;
VkCommandPool vkInstance::setupCommandPool;
VmaAllocator vkInstance::allocator;
VkDebugReportCallbackEXT vkInstance::debugReportCallback;

#ifndef NDEBUG
Expand Down Expand Up @@ -151,12 +154,53 @@ void vkInstance::deviceInit(VkPhysicalDevice physicalDevice, function<bool(VkIns

const char *enabledExtensions[] = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
};
deviceCreateInfo.enabledExtensionCount = ARRAY_SIZE(enabledExtensions);
deviceCreateInfo.ppEnabledExtensionNames = enabledExtensions;

assumeSuccess(vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device));

VmaVulkanFunctions vulkanFunctions = {
#define P(x) .x = x,
P(vkGetInstanceProcAddr)
P(vkGetDeviceProcAddr)
P(vkGetPhysicalDeviceProperties)
P(vkGetPhysicalDeviceMemoryProperties)
P(vkAllocateMemory)
P(vkFreeMemory)
P(vkMapMemory)
P(vkUnmapMemory)
P(vkFlushMappedMemoryRanges)
P(vkInvalidateMappedMemoryRanges)
P(vkBindBufferMemory)
P(vkBindImageMemory)
P(vkGetBufferMemoryRequirements)
P(vkGetImageMemoryRequirements)
P(vkCreateBuffer)
P(vkDestroyBuffer)
P(vkCreateImage)
P(vkDestroyImage)
P(vkCmdCopyBuffer)
P(vkGetBufferMemoryRequirements2KHR)
P(vkGetImageMemoryRequirements2KHR)
P(vkBindBufferMemory2KHR)
P(vkBindImageMemory2KHR)
P(vkGetPhysicalDeviceMemoryProperties2KHR)
P(vkGetDeviceBufferMemoryRequirements)
P(vkGetDeviceImageMemoryRequirements)
#undef P
};

VmaAllocatorCreateInfo allocatorCreateInfo = {};
allocatorCreateInfo.flags = 0;
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_0;
allocatorCreateInfo.physicalDevice = physicalDevice;
allocatorCreateInfo.device = device;
allocatorCreateInfo.instance = instance;
allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;
vmaCreateAllocator(&allocatorCreateInfo, &vkInstance::allocator);

vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties);
vkGetDeviceQueue(device, graphicsQueueIndex, 0, &graphicsQueue);

Expand Down
5 changes: 5 additions & 0 deletions src/vkinstance.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <volk.h>

#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#include <vk_mem_alloc.h>

#include <vector>
#include <functional>

Expand All @@ -18,6 +22,7 @@ namespace vkInstance

extern VkCommandPool setupCommandPool;

extern VmaAllocator allocator;
extern VkDebugReportCallbackEXT debugReportCallback;

void instanceInit(const char *appName, const std::vector<const char *> &enabledExtensions);
Expand Down

0 comments on commit e14c5be

Please sign in to comment.