Handling window minimizing

This commit is contained in:
Dynamitos
2023-12-28 21:42:18 +01:00
parent e18254ec29
commit 4b58ab84be
21 changed files with 109 additions and 76 deletions
+26 -32
View File
@@ -134,6 +134,10 @@ void Allocation::markFree(PSubAllocation allocation)
}
activeAllocations.remove(allocation, false);
bytesUsed -= allocation->allocatedSize;
//if (activeAllocations.size() == 0)
//{
// pool->free(this);
//}
}
void Allocation::flushMemory()
@@ -203,7 +207,7 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
OSubAllocation suballoc = alloc->getSuballocation(requirements.size, requirements.alignment);
if (suballoc != nullptr)
{
return std::move(suballoc);
return suballoc;
}
}
}
@@ -216,18 +220,15 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment);
}
void Allocator::free()
void Allocator::free(PAllocation allocation)
{
for (uint32 heapIndex = 0; heapIndex < heaps.size(); ++heapIndex)
{
for (uint32 alloc = 0; alloc < heaps[heapIndex].allocations.size(); ++alloc)
{
if (heaps[heapIndex].allocations[alloc]->bytesUsed == 0)
if (heaps[heapIndex].allocations[alloc] == allocation)
{
heaps[heapIndex].inUse -= heaps[heapIndex].allocations[alloc]->bytesAllocated;
std::cout << "Heap " << heapIndex << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize * 100 << "%" << std::endl;
heaps[heapIndex].allocations.removeAt(alloc, false);
alloc--;
}
}
}
@@ -259,7 +260,7 @@ uint32 Allocator::findMemoryType(uint32 typeFilter, VkMemoryPropertyFlags proper
}
StagingBuffer::StagingBuffer(PGraphics graphics, OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, Gfx::QueueType owner)
: QueueOwnedResource(graphics->getFamilyMapping(), owner)
: owner(owner)
, graphics(graphics)
, allocation(std::move(allocation))
, buffer(buffer)
@@ -270,9 +271,9 @@ StagingBuffer::StagingBuffer(PGraphics graphics, OSubAllocation allocation, VkBu
StagingBuffer::~StagingBuffer()
{
graphics->getDestructionManager()->queueBuffer(
graphics->getQueueCommands(currentOwner)->getCommands(), buffer);
graphics->getQueueCommands(owner)->getCommands(), buffer);
graphics->getDestructionManager()->queueAllocation(
graphics->getQueueCommands(currentOwner)->getCommands(), std::move(allocation));
graphics->getQueueCommands(owner)->getCommands(), std::move(allocation));
}
void* StagingBuffer::map()
@@ -300,28 +301,6 @@ VkDeviceSize StagingBuffer::getOffset() const
return allocation->getOffset();
}
void StagingBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner)
{
assert(false);
}
void StagingBuffer::executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage,
Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage)
{
PCommand commandBuffer = graphics->getQueueCommands(currentOwner)->getCommands();
VkBufferMemoryBarrier barrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.pNext = nullptr,
.srcAccessMask = srcAccess,
.dstAccessMask = dstAccess,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.buffer = buffer,
.offset = 0,
.size = size,
};
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, 1, &barrier, 0, nullptr);
}
StagingManager::StagingManager(PGraphics graphics, PAllocator pool)
: graphics(graphics), pool(pool)
{
@@ -333,8 +312,18 @@ StagingManager::~StagingManager()
OStagingBuffer StagingManager::create(uint64 size, Gfx::QueueType owner)
{
//vkDeviceWaitIdle(graphics->getDevice());
//std::cout << "Creating new stagingbuffer" << std::endl;
uint32 queueIndex = graphics->getFamilyMapping().getQueueTypeFamilyIndex(Gfx::QueueType::DEDICATED_TRANSFER);
for (uint32 i = 0; i < freeBuffers.size(); ++i)
{
if (size <= freeBuffers[i]->getSize() && owner == freeBuffers[i]->getOwner())
{
OStagingBuffer result = std::move(freeBuffers[i]);
freeBuffers.removeAt(i);
return result;
}
}
uint32 queueIndex = graphics->getFamilyMapping().getQueueTypeFamilyIndex(owner);
VkBufferCreateInfo stagingBufferCreateInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
@@ -375,3 +364,8 @@ OStagingBuffer StagingManager::create(uint64 size, Gfx::QueueType owner)
return stagingBuffer;
}
void StagingManager::release(OStagingBuffer buffer)
{
freeBuffers.add(std::move(buffer));
}
+11 -6
View File
@@ -49,6 +49,8 @@ class Allocation
public:
Allocation(PGraphics graphics, PAllocator pool, VkDeviceSize size, uint8 memoryTypeIndex,
VkMemoryPropertyFlags properties, VkMemoryDedicatedAllocateInfo *dedicatedInfo = nullptr);
Allocation(const Allocation& other) = delete;
Allocation& operator=(const Allocation& other) = delete;
~Allocation();
OSubAllocation getSuballocation(VkDeviceSize size, VkDeviceSize alignment);
void markFree(PSubAllocation alloc);
@@ -122,7 +124,7 @@ public:
return allocate(requirements, props, &allocInfo);
}
void free();
void free(PAllocation allocation);
void print();
private:
static constexpr VkDeviceSize DEFAULT_ALLOCATION = 16 * 1024 * 1024; // 16MB
@@ -143,7 +145,7 @@ private:
VkPhysicalDeviceMemoryProperties memProperties;
};
DEFINE_REF(Allocator)
class StagingBuffer : public Gfx::QueueOwnedResource
class StagingBuffer
{
public:
StagingBuffer(PGraphics graphics, OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, Gfx::QueueType owner);
@@ -161,10 +163,12 @@ public:
{
return size;
}
constexpr Gfx::QueueType getOwner() const
{
return owner;
}
private:
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner) override;
virtual void executePipelineBarrier(Gfx::SeAccessFlags srcAccess, Gfx::SePipelineStageFlags srcStage,
Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) override;
Gfx::QueueType owner;
OSubAllocation allocation;
PGraphics graphics;
VkBuffer buffer;
@@ -178,10 +182,11 @@ public:
StagingManager(PGraphics graphics, PAllocator pool);
~StagingManager();
OStagingBuffer create(uint64 size, Gfx::QueueType owner);
void release(OStagingBuffer buffer);
private:
PGraphics graphics;
PAllocator pool;
Array<OStagingBuffer> freeBuffers;
};
DEFINE_REF(StagingManager)
} // namespace Vulkan
+9
View File
@@ -255,6 +255,7 @@ void Buffer::unmap()
vkCmdCopyBuffer(cmdHandle, stagingBuffer->getHandle(), buffers[currentBuffer].buffer, 1, &region);
}
//requestOwnershipTransfer(pending.prevQueue);
graphics->getStagingManager()->release(std::move(pending.stagingBuffer));
pendingBuffers.erase(this);
}
}
@@ -278,6 +279,10 @@ UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo &
UniformBuffer::~UniformBuffer()
{
if (dedicatedStagingBuffer != nullptr)
{
graphics->getStagingManager()->release(std::move(dedicatedStagingBuffer));
}
}
bool UniformBuffer::updateContents(const DataSource &sourceData)
@@ -365,6 +370,10 @@ ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &sou
ShaderBuffer::~ShaderBuffer()
{
if (dedicatedStagingBuffer != nullptr)
{
graphics->getStagingManager()->release(std::move(dedicatedStagingBuffer));
}
}
bool ShaderBuffer::updateContents(const DataSource &sourceData)
@@ -346,13 +346,6 @@ DescriptorPool::DescriptorPool(PGraphics graphics, DescriptorLayout &layout)
DescriptorPool::~DescriptorPool()
{
for (uint32 setIndex = 0; setIndex < cachedHandles.size(); ++setIndex)
{
if (cachedHandles[setIndex] != nullptr && cachedHandles[setIndex]->setHandle != VK_NULL_HANDLE)
{
graphics->getDestructionManager()->queueDescriptorSet(graphics->getGraphicsCommands()->getCommands(), Pair<VkDescriptorSet, VkDescriptorPool>(cachedHandles[setIndex]->setHandle, poolHandle));
}
}
graphics->getDestructionManager()->queueDescriptorPool(graphics->getGraphicsCommands()->getCommands(), poolHandle);
if(nextAlloc)
{
+9 -13
View File
@@ -46,16 +46,21 @@ bool Fence::isSignaled()
{
return true;
}
VkResult res = vkGetFenceStatus(graphics->getDevice(), fence);
if (res == VK_SUCCESS)
VkResult r = vkGetFenceStatus(graphics->getDevice(), fence);
if (r == VK_SUCCESS)
{
signaled = true;
return true;
}
if (res == VK_NOT_READY)
if (r == VK_NOT_READY)
{
return false;
}
else
{
VK_CHECK(r);
return false;
}
}
void Fence::reset()
@@ -124,11 +129,6 @@ void DestructionManager::queueDescriptorPool(PCommand cmd, VkDescriptorPool pool
pools[cmd].add(pool);
}
void DestructionManager::queueDescriptorSet(PCommand cmd, Pair<VkDescriptorSet, VkDescriptorPool> set)
{
sets[cmd].add(set);
}
void DestructionManager::queueAllocation(PCommand cmd, OSubAllocation alloc)
{
allocs[cmd].add(std::move(alloc));
@@ -152,10 +152,6 @@ void DestructionManager::notifyCmdComplete(PCommand cmd)
{
vkDestroySemaphore(graphics->getDevice(), sem, nullptr);
}
for (auto [set, pool] : sets[cmd])
{
vkFreeDescriptorSets(graphics->getDevice(), pool, 1, &set);
}
for (auto pool : pools[cmd])
{
vkDestroyDescriptorPool(graphics->getDevice(), pool, nullptr);
@@ -168,7 +164,7 @@ void DestructionManager::notifyCmdComplete(PCommand cmd)
images[cmd].clear();
views[cmd].clear();
sems[cmd].clear();
pools[cmd].clear();
renderPasses[cmd].clear();
allocs[cmd].clear();
//graphics->getAllocator()->free();
}
-2
View File
@@ -63,7 +63,6 @@ public:
void queueSemaphore(PCommand cmd, VkSemaphore sem);
void queueRenderPass(PCommand cmd, VkRenderPass renderPass);
void queueDescriptorPool(PCommand cmd, VkDescriptorPool pool);
void queueDescriptorSet(PCommand cmd, Pair<VkDescriptorSet, VkDescriptorPool> set);
void queueAllocation(PCommand cmd, OSubAllocation alloc);
void notifyCmdComplete(PCommand cmdbuffer);
private:
@@ -74,7 +73,6 @@ private:
Map<PCommand, List<VkSemaphore>> sems;
Map<PCommand, List<VkRenderPass>> renderPasses;
Map<PCommand, List<VkDescriptorPool>> pools;
Map<PCommand, List<Pair<VkDescriptorSet, VkDescriptorPool>>> sets;
Map<PCommand, List<OSubAllocation>> allocs;
};
DEFINE_REF(DestructionManager)
+1
View File
@@ -157,6 +157,7 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
// When loading a texture from a file, we will almost always use it as a texture map for fragment shaders
changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
graphics->getStagingManager()->release(std::move(staging));
}
else if(usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
+11 -1
View File
@@ -97,9 +97,13 @@ Window::~Window()
glfwDestroyWindow(static_cast<GLFWwindow *>(windowHandle));
}
void Window::beginFrame()
void Window::pollInput()
{
glfwPollEvents();
}
void Window::beginFrame()
{
vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits<uint64>::max(), imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(), VK_NULL_HANDLE, &currentImageIndex);
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, imageAvailableSemaphores[currentSemaphoreIndex]);
@@ -210,6 +214,12 @@ void Window::close()
void Window::resize(int width, int height)
{
if (width == 0 || height == 0)
{
paused = true;
return;
}
paused = false;
querySurface();
chooseSwapSurfaceFormat();
framebufferFormat = cast(format.format);
+1
View File
@@ -13,6 +13,7 @@ class Window : public Gfx::Window
public:
Window(PGraphics graphics, const WindowCreateInfo &createInfo);
virtual ~Window();
virtual void pollInput() override;
virtual void beginFrame() override;
virtual void endFrame() override;
virtual Gfx::PTexture2D getBackBuffer() override;