Skip to content

Commit

Permalink
vulkan: implement defragmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeinikei committed Oct 12, 2024
1 parent f20b413 commit 4251b9d
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 53 deletions.
60 changes: 49 additions & 11 deletions src/modules/graphics/vulkan/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Buffer::Buffer(love::graphics::Graphics *gfx, const Settings &settings, const st
, initialData(data)
, vgfx(dynamic_cast<Graphics*>(gfx))
, usageFlags(settings.usageFlags)
, graphicsResource(GRAPHICSRESOURCETYPE_BUFFER, this)
{
// All buffers can be copied to and from.
barrierDstAccessFlags = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
Expand Down Expand Up @@ -99,7 +100,7 @@ bool Buffer::loadVolatile()
{
allocator = vgfx->getVmaAllocator();

VkBufferCreateInfo bufferInfo{};
memset(&bufferInfo, 0, sizeof(bufferInfo));
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = getSize();
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | getVulkanUsageFlags(usageFlags);
Expand All @@ -113,6 +114,8 @@ bool Buffer::loadVolatile()
if (result != VK_SUCCESS)
throw love::Exception("failed to create buffer");

vmaSetAllocationUserData(allocator, allocation, &graphicsResource);

if (zeroInitialize)
{
auto cmd = vgfx->getCommandBufferForDataTransfer();
Expand All @@ -124,16 +127,7 @@ bool Buffer::loadVolatile()
fill(0, size, initialData);

if (usageFlags & BUFFERUSAGEFLAG_TEXEL)
{
VkBufferViewCreateInfo bufferViewInfo{};
bufferViewInfo.buffer = buffer;
bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
bufferViewInfo.format = Vulkan::getVulkanVertexFormat(getDataMember(0).decl.format);
bufferViewInfo.range = VK_WHOLE_SIZE;

if (vkCreateBufferView(vgfx->getDevice(), &bufferViewInfo, nullptr, &bufferView) != VK_SUCCESS)
throw love::Exception("failed to create texel buffer view");
}
createBufferView();

VkMemoryPropertyFlags memoryProperties;
vmaGetAllocationMemoryProperties(allocator, allocation, &memoryProperties);
Expand All @@ -154,6 +148,8 @@ bool Buffer::loadVolatile()
vkSetDebugUtilsObjectNameEXT(device, &nameInfo);
}

vgfx->requestDefragmentation();

return true;
}

Expand All @@ -162,6 +158,8 @@ void Buffer::unloadVolatile()
if (buffer == VK_NULL_HANDLE)
return;

vmaSetAllocationUserData(allocator, allocation, nullptr);

auto device = vgfx->getDevice();

vgfx->queueCleanUp(
Expand All @@ -170,6 +168,7 @@ void Buffer::unloadVolatile()
vmaDestroyBuffer(allocator, buffer, allocation);
if (bufferView)
vkDestroyBufferView(device, bufferView, nullptr);
return true;
});

buffer = VK_NULL_HANDLE;
Expand Down Expand Up @@ -276,6 +275,7 @@ bool Buffer::fill(size_t offset, size_t size, const void *data)

vgfx->queueCleanUp([allocator = allocator, fillBuffer = fillBuffer, fillAllocation = fillAllocation]() {
vmaDestroyBuffer(allocator, fillBuffer, fillAllocation);
return true;
});

return true;
Expand All @@ -302,6 +302,7 @@ void Buffer::unmap(size_t usedoffset, size_t usedsize)

vgfx->queueCleanUp([allocator = allocator, stagingBuffer = stagingBuffer, stagingAllocation = stagingAllocation]() {
vmaDestroyBuffer(allocator, stagingBuffer, stagingAllocation);
return true;
});
}
}
Expand Down Expand Up @@ -337,6 +338,43 @@ void Buffer::postGPUWriteBarrier(VkCommandBuffer cmd)
vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, barrierDstStageFlags, 0, 1, &barrier, 0, nullptr, 0, nullptr);
}

void Buffer::createBufferView()
{
VkBufferViewCreateInfo bufferViewInfo{};
bufferViewInfo.buffer = buffer;
bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
bufferViewInfo.format = Vulkan::getVulkanVertexFormat(getDataMember(0).decl.format);
bufferViewInfo.range = VK_WHOLE_SIZE;

if (vkCreateBufferView(vgfx->getDevice(), &bufferViewInfo, nullptr, &bufferView) != VK_SUCCESS)
throw love::Exception("failed to create texel buffer view");
}

VkBuffer Buffer::performDefragmentationMove(VkCommandBuffer commandBuffer, VmaAllocator allocator, VmaAllocation dstAllocation)
{
VkDevice device = vgfx->getDevice();
VkBuffer oldBuffer = buffer;

if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
throw love::Exception("could not recreate stream buffer");

if (vmaBindBufferMemory(allocator, dstAllocation, buffer))
throw love::Exception("could not bind the buffer memory");

VkBufferCopy region{};
region.size = bufferInfo.size;

vkCmdCopyBuffer(commandBuffer, oldBuffer, buffer, 1, &region);

if (usageFlags & BUFFERUSAGEFLAG_TEXEL)
{
vkDestroyBufferView(device, bufferView, nullptr);
createBufferView();
}

return oldBuffer;
}

} // vulkan
} // graphics
} // love
7 changes: 6 additions & 1 deletion src/modules/graphics/vulkan/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "graphics/Volatile.h"

#include "VulkanWrapper.h"
#include "Vulkan.h"


namespace love
Expand Down Expand Up @@ -60,12 +61,15 @@ class Buffer final
VkAccessFlags getBarrierDstAccessFlags() const { return barrierDstAccessFlags; }
VkPipelineStageFlags getBarrierDstStageFlags() const { return barrierDstStageFlags; }

private:
VkBuffer performDefragmentationMove(VkCommandBuffer commandBuffer, VmaAllocator allocator, VmaAllocation dstAllocation);

private:
void createBufferView();
void clearInternal(size_t offset, size_t size) override;

bool zeroInitialize;
const void *initialData;
VkBufferCreateInfo bufferInfo;
VkBuffer buffer = VK_NULL_HANDLE;
VkBuffer stagingBuffer = VK_NULL_HANDLE;
VkBufferView bufferView = VK_NULL_HANDLE;
Expand All @@ -78,6 +82,7 @@ class Buffer final
BufferUsageFlags usageFlags;
Range mappedRange;
bool coherent;
GraphicsResource graphicsResource;

VkAccessFlags barrierDstAccessFlags = 0;
VkPipelineStageFlags barrierDstStageFlags = 0;
Expand Down
137 changes: 133 additions & 4 deletions src/modules/graphics/vulkan/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ void Graphics::submitGpuCommands(SubmitMode submitMode, void *screenshotCallback
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, fence) != VK_SUCCESS)
throw love::Exception("failed to submit draw command buffer");

if (submitMode == SUBMIT_NOPRESENT || submitMode == SUBMIT_RESTART || screenshotBuffer != VK_NULL_HANDLE)
if (submitMode == SUBMIT_NOPRESENT || submitMode == SUBMIT_RESTART || screenshotBuffer != VK_NULL_HANDLE || defragmentationRequested)
{
vkQueueWaitIdle(graphicsQueue);

Expand Down Expand Up @@ -532,6 +532,9 @@ void Graphics::submitGpuCommands(SubmitMode submitMode, void *screenshotCallback
pendingScreenshotCallbacks.clear();
}

if (defragmentationRequested)
runDefragmentation();

if (submitMode == SUBMIT_RESTART)
startRecordingGraphicsCommands();
}
Expand Down Expand Up @@ -1372,8 +1375,11 @@ void Graphics::beginFrame()
readbackCallback();
readbackCallbacks.at(currentFrame).clear();

for (auto &cleanUpFn : cleanUpFunctions.at(currentFrame))
cleanUpFn();
for (auto& cleanUpFn : cleanUpFunctions.at(currentFrame))
{
if (cleanUpFn())
defragmentationRequested = true;
}
cleanUpFunctions.at(currentFrame).clear();

startRecordingGraphicsCommands();
Expand Down Expand Up @@ -1471,7 +1477,7 @@ VkCommandBuffer Graphics::getCommandBufferForDataTransfer()
return commandBuffers.at(currentFrame);
}

void Graphics::queueCleanUp(std::function<void()> cleanUp)
void Graphics::queueCleanUp(std::function<bool()> cleanUp)
{
cleanUpFunctions.at(currentFrame).push_back(cleanUp);
}
Expand Down Expand Up @@ -2759,6 +2765,107 @@ void Graphics::requestSwapchainRecreation()
}
}

void Graphics::runDefragmentation()
{
VmaDefragmentationInfo defragInfo{};
defragInfo.flags = VMA_DEFRAGMENTATION_FLAG_ALGORITHM_FAST_BIT;

VmaDefragmentationContext defragCtx;
if (vmaBeginDefragmentation(vmaAllocator, &defragInfo, &defragCtx) != VK_SUCCESS)
throw love::Exception("could not create defragmentation context");

VkResult res;
for (;;)
{
VmaDefragmentationPassMoveInfo pass;
res = vmaBeginDefragmentationPass(vmaAllocator, defragCtx, &pass);
if (res == VK_SUCCESS)
break;
else if (res != VK_INCOMPLETE)
throw love::Exception("could not begin defragmentation pass");

std::cout << "defragmentation pass, #moves = " << pass.moveCount << std::endl;

VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;

if (vkBeginCommandBuffer(defragmentationCommandBuffer, &beginInfo) != VK_SUCCESS)
throw love::Exception("could not start defragmentation command buffer");

std::vector<VkBuffer> buffers;
std::vector<VkImage> images;

std::vector<std::function<void()>> postMoves;

for (uint32_t i = 0; i < pass.moveCount; i++)
{
VmaAllocationInfo srcAllocInfo;
vmaGetAllocationInfo(vmaAllocator, pass.pMoves[i].srcAllocation, &srcAllocInfo);
if (srcAllocInfo.pUserData == nullptr)
pass.pMoves[i].operation = VMA_DEFRAGMENTATION_MOVE_OPERATION_IGNORE;
else
{
GraphicsResource *resource = (GraphicsResource*)srcAllocInfo.pUserData;
switch (resource->type) {
case GRAPHICSRESOURCETYPE_STREAMBUFFER: {
auto streamBuffer = static_cast<StreamBuffer*>(resource->object);
buffers.push_back(streamBuffer->performDefragmentationMove(defragmentationCommandBuffer, vmaAllocator, pass.pMoves[i].dstTmpAllocation));
break;
}
case GRAPHICSRESOURCETYPE_TEXTURE: {
auto texture = static_cast<Texture*>(resource->object);
images.push_back(texture->performDefragmentationMove(defragmentationCommandBuffer, vmaAllocator, pass.pMoves[i].dstTmpAllocation));
break;
}
case GRAPHICSRESOURCETYPE_BUFFER: {
auto buffer = static_cast<Buffer*>(resource->object);
buffers.push_back(buffer->performDefragmentationMove(defragmentationCommandBuffer, vmaAllocator, pass.pMoves[i].dstTmpAllocation));
break;
}
default:
throw love::Exception("unknown graphics resource type");
}
}
}

if (vkEndCommandBuffer(defragmentationCommandBuffer) != VK_SUCCESS)
throw love::Exception("could not end defragmentation command buffer");

VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &defragmentationCommandBuffer;

if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, defragmentationFence) != VK_SUCCESS)
throw love::Exception("could not submit defragmentation queue");

if (vkWaitForFences(device, 1, &defragmentationFence, VK_TRUE, UINT64_MAX) != VK_SUCCESS)
throw love::Exception("could not wait for defragmentation fence");

if (vkResetFences(device, 1, &defragmentationFence) != VK_SUCCESS)
throw love::Exception("could not reset defragmentation fence");

for (const auto& postMove : postMoves)
postMove();

for (VkBuffer buffer : buffers)
vkDestroyBuffer(device, buffer, nullptr);
for (VkImage image : images)
vkDestroyImage(device, image, nullptr);

res = vmaEndDefragmentationPass(vmaAllocator, defragCtx, &pass);
if (res == VK_SUCCESS)
break;
else if (res != VK_INCOMPLETE)
throw love::Exception("could not end defragmentation pass");
}

vmaEndDefragmentation(vmaAllocator, defragCtx, nullptr);

defragmentationCount++;
}

VkSampler Graphics::getCachedSampler(const SamplerState &samplerState)
{
auto samplerkey = samplerState.toKey();
Expand Down Expand Up @@ -3002,6 +3109,11 @@ void Graphics::mapLocalUniformData(void *data, size_t size, VkDescriptorBufferIn
localUniformBuffer->markUsed(alignedSize);
}

void Graphics::requestDefragmentation()
{
defragmentationRequested = true;
}

void Graphics::createColorResources()
{
if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
Expand Down Expand Up @@ -3157,6 +3269,15 @@ void Graphics::createCommandBuffers()

if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS)
throw love::Exception("failed to allocate command buffers");

VkCommandBufferAllocateInfo defragmentationAllocInfo{};
defragmentationAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
defragmentationAllocInfo.commandPool = commandPool;
defragmentationAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
defragmentationAllocInfo.commandBufferCount = 1;

if (vkAllocateCommandBuffers(device, &defragmentationAllocInfo, &defragmentationCommandBuffer) != VK_SUCCESS)
throw love::Exception("failed to allocate command buffers");
}

void Graphics::createSyncObjects()
Expand All @@ -3178,6 +3299,12 @@ void Graphics::createSyncObjects()
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores.at(i)) != VK_SUCCESS ||
vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences.at(i)) != VK_SUCCESS)
throw love::Exception("failed to create synchronization objects for a frame!");

VkFenceCreateInfo defragmentationFenceInfo{};
defragmentationFenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;

if (vkCreateFence(device, &defragmentationFenceInfo, nullptr, &defragmentationFence) != VK_SUCCESS)
throw love::Exception("could not create defragmentation fence");
}

void Graphics::cleanup()
Expand All @@ -3194,8 +3321,10 @@ void Graphics::cleanup()
vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
vkDestroyFence(device, inFlightFences[i], nullptr);
}
vkDestroyFence(device, defragmentationFence, nullptr);

vkFreeCommandBuffers(device, commandPool, MAX_FRAMES_IN_FLIGHT, commandBuffers.data());
vkFreeCommandBuffers(device, commandPool, 1, &defragmentationCommandBuffer);

for (auto const &p : samplers)
vkDestroySampler(device, p.second, nullptr);
Expand Down
Loading

0 comments on commit 4251b9d

Please sign in to comment.