Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vulkan: Improve post-shutdown cleanup and minor improvements #1401

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3cff05e
Vulkan: Fix race condition validation errors
goeiecool9999 Nov 4, 2024
e09f85f
Vulkan: Fix swapchain renderpass validation errors
goeiecool9999 Nov 4, 2024
b41d600
Vulkan: Change command buffers from simultaneous use to single use
goeiecool9999 Nov 4, 2024
af5114d
Renderers: Cleanup RendererOutputShaders
goeiecool9999 Nov 4, 2024
2793f88
Vulkan: Destroy VkShaderModule objects
goeiecool9999 Nov 4, 2024
4581281
Vulkan: Partially(?) implement CopySurface cleanup
goeiecool9999 Nov 4, 2024
d6a5693
Vulkan: Manage ownership of memory manager with unique_ptr
goeiecool9999 Nov 4, 2024
7dd28f7
Vulkan: Cleanup backbufferblit pipelines
goeiecool9999 Nov 4, 2024
df53971
Vulkan: Cleanup pipeline cache
goeiecool9999 Nov 4, 2024
721d0ce
Vulkan: Fix sampler object leak
goeiecool9999 Nov 4, 2024
f2096a6
Vulkan: Free RingAllocator buffers upon destruction
goeiecool9999 Nov 4, 2024
95c9701
Vulkan: Properly free TextureChunkedHeaps
goeiecool9999 Nov 4, 2024
c93858b
Vulkan: Free swapchain DescriptorSetLayout
goeiecool9999 Nov 4, 2024
e524e08
Vulkan: Free command buffer semaphores
goeiecool9999 Nov 4, 2024
16c5726
Vulkan: Fix ImGui font leak
goeiecool9999 Nov 4, 2024
f924e13
Vulkan: cleanup copySurface pipeline framebuffers/descriptors
goeiecool9999 Nov 4, 2024
d4c4c32
Vulkan: Remove obsolete comments/vector and process destruction queue…
goeiecool9999 Nov 4, 2024
44a3c00
Vulkan: Cleanup backbuffer descriptor sets and pool
goeiecool9999 Nov 4, 2024
f013609
Vulkan: Proper fix for sampler leak in draw_getOrCreateDescriptorSet
goeiecool9999 Nov 4, 2024
7ce6ab7
Vulkan: Delete superfluous DestroyFontsTexture call
goeiecool9999 Nov 4, 2024
45afeaa
Vulkan: mark descriptor samplers on draw
goeiecool9999 Nov 5, 2024
ec91b6c
Vulkan: Loop processing destruction queue until empty
goeiecool9999 Nov 5, 2024
0866c47
Vulkan: destroy occlusion query pool
goeiecool9999 Nov 5, 2024
de2cc13
flag samplers at more logical place, add reference from dsInfo
goeiecool9999 Nov 5, 2024
ac2b5d0
deduplicate prepareDescriptorSets code
goeiecool9999 Nov 5, 2024
0b559fb
Fix imgui texture leak if shadercache loading is interrupted
goeiecool9999 Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Cafe/HW/Latte/Core/LatteShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ void LatteShaderCache_Load()
g_renderer->DeleteTexture(g_shaderCacheLoaderState.textureTVId);
if (g_shaderCacheLoaderState.textureDRCId)
g_renderer->DeleteTexture(g_shaderCacheLoaderState.textureDRCId);

if(Latte_GetStopSignal())
LatteThread_Exit();
}

void LatteShaderCache_ShowProgress(const std::function <bool(void)>& loadUpdateFunc, bool isPipelines)
Expand Down Expand Up @@ -505,8 +508,6 @@ void LatteShaderCache_LoadVulkanPipelineCache(uint64 cacheTitleId)
g_shaderCacheLoaderState.loadedPipelines = 0;
LatteShaderCache_ShowProgress(LatteShaderCache_updatePipelineLoadingProgress, true);
pipelineCache.EndLoading();
if(Latte_GetStopSignal())
LatteThread_Exit();
}

bool LatteShaderCache_updatePipelineLoadingProgress()
Expand Down
1 change: 1 addition & 0 deletions src/Cafe/HW/Latte/Core/LatteThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ void LatteThread_Exit()
LatteSHRC_UnloadAll();
// close disk cache
LatteShaderCache_Close();
RendererOutputShader::ShutdownStatic();
// destroy renderer but make sure that g_renderer remains valid until the destructor has finished
if (g_renderer)
{
Expand Down
16 changes: 14 additions & 2 deletions src/Cafe/HW/Latte/Renderer/RendererOuputShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void main(){

RendererOutputShader::RendererOutputShader(const std::string& vertex_source, const std::string& fragment_source)
{
m_vertex_shader = g_renderer->shader_create(RendererShader::ShaderType::kVertex, 0, 0, vertex_source, false, false);
m_fragment_shader = g_renderer->shader_create(RendererShader::ShaderType::kFragment, 0, 0, fragment_source, false, false);
m_vertex_shader.reset(g_renderer->shader_create(RendererShader::ShaderType::kVertex, 0, 0, vertex_source, false, false));
m_fragment_shader.reset(g_renderer->shader_create(RendererShader::ShaderType::kFragment, 0, 0, fragment_source, false, false));

m_vertex_shader->PreponeCompilation(true);
m_fragment_shader->PreponeCompilation(true);
Expand Down Expand Up @@ -374,3 +374,15 @@ void RendererOutputShader::InitializeStatic()
s_hermit_shader_ud = new RendererOutputShader(vertex_source_ud, s_hermite_shader_source);*/
}
}

void RendererOutputShader::ShutdownStatic()
{
delete s_copy_shader;
delete s_copy_shader_ud;

delete s_bicubic_shader;
delete s_bicubic_shader_ud;

delete s_hermit_shader;
delete s_hermit_shader_ud;
}
9 changes: 5 additions & 4 deletions src/Cafe/HW/Latte/Renderer/RendererOuputShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ class RendererOutputShader

RendererShader* GetVertexShader() const
{
return m_vertex_shader;
return m_vertex_shader.get();
}

RendererShader* GetFragmentShader() const
{
return m_fragment_shader;
return m_fragment_shader.get();
}

static void InitializeStatic();
static void ShutdownStatic();

static RendererOutputShader* s_copy_shader;
static RendererOutputShader* s_copy_shader_ud;
Expand All @@ -44,8 +45,8 @@ class RendererOutputShader
static std::string GetOpenGlVertexSource(bool render_upside_down);

protected:
RendererShader* m_vertex_shader;
RendererShader* m_fragment_shader;
std::unique_ptr<RendererShader> m_vertex_shader;
std::unique_ptr<RendererShader> m_fragment_shader;

struct
{
Expand Down
3 changes: 3 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ RendererShaderVk::~RendererShaderVk()
{
while (!list_pipelineInfo.empty())
delete list_pipelineInfo[0];

VkDevice vkDev = VulkanRenderer::GetInstance()->GetLogicalDevice();
vkDestroyShaderModule(vkDev, m_shader_module, nullptr);
}

void RendererShaderVk::Init()
Expand Down
2 changes: 1 addition & 1 deletion src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void SwapchainInfoVk::Create()
VkAttachmentDescription colorAttachment = {};
colorAttachment.format = m_surfaceFormat.format;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Expand Down
2 changes: 1 addition & 1 deletion src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct SwapchainInfoVk
VkSurfaceFormatKHR m_surfaceFormat{};
VkSwapchainKHR m_swapchain{};
Vector2i m_desiredExtent{};
VkExtent2D m_actualExtent{};
uint32 swapchainImageIndex = (uint32)-1;
uint64 m_presentId = 1;
uint64 m_queueDepth = 0; // number of frames with pending presentation requests
Expand All @@ -92,5 +93,4 @@ struct SwapchainInfoVk
VkSemaphore m_currentSemaphore = VK_NULL_HANDLE;

std::array<uint32, 2> m_swapchainQueueFamilyIndices;
VkExtent2D m_actualExtent{};
};
7 changes: 7 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VKRBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,11 @@ class VKRObjectDescriptorSet : public VKRDestructibleObject
~VKRObjectDescriptorSet() override;

VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
};

class VKRObjectSampler : public VKRDestructibleObject
{
public:
~VKRObjectSampler() override;
VkSampler sampler { VK_NULL_HANDLE };
};
18 changes: 17 additions & 1 deletion src/Cafe/HW/Latte/Renderer/Vulkan/VKRMemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

/* VKRSynchronizedMemoryBuffer */

VKRSynchronizedRingAllocator::~VKRSynchronizedRingAllocator()
{
for(auto& buf : m_buffers)
{
m_vkrMemMgr->DeleteBuffer(buf.vk_buffer, buf.vk_mem);
}
}

void VKRSynchronizedRingAllocator::addUploadBufferSyncPoint(AllocatorBuffer_t& buffer, uint32 offset)
{
auto cmdBufferId = m_vkr->GetCurrentCommandBufferId();
Expand Down Expand Up @@ -169,6 +177,14 @@ void VKRSynchronizedRingAllocator::GetStats(uint32& numBuffers, size_t& totalBuf

/* VkTextureChunkedHeap */

VkTextureChunkedHeap::~VkTextureChunkedHeap()
{
for (auto& i : m_list_chunkInfo)
{
vkFreeMemory(m_device, i.mem, nullptr);
}
}

uint32 VkTextureChunkedHeap::allocateNewChunk(uint32 chunkIndex, uint32 minimumAllocationSize)
{
cemu_assert_debug(m_list_chunkInfo.size() == chunkIndex);
Expand Down Expand Up @@ -473,7 +489,7 @@ VkImageMemAllocation* VKRMemoryManager::imageMemoryAllocate(VkImage image)
map_textureHeap.emplace(typeFilter, texHeap);
}
else
texHeap = it->second;
texHeap = it->second.get();

// alloc mem from heap
uint32 allocationSize = (uint32)memRequirements.size;
Expand Down
4 changes: 3 additions & 1 deletion src/Cafe/HW/Latte/Renderer/Vulkan/VKRMemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class VkTextureChunkedHeap : private ChunkedHeap
{
public:
VkTextureChunkedHeap(class VKRMemoryManager* memoryManager, uint32 typeFilter, VkDevice device) : m_vkrMemoryManager(memoryManager), m_typeFilter(typeFilter), m_device(device) { };
~VkTextureChunkedHeap();

struct ChunkInfo
{
Expand Down Expand Up @@ -80,6 +81,7 @@ class VKRSynchronizedRingAllocator

VKRSynchronizedRingAllocator(class VulkanRenderer* vkRenderer, class VKRMemoryManager* vkMemoryManager, BUFFER_TYPE bufferType, uint32 minimumBufferAllocSize) : m_vkr(vkRenderer), m_vkrMemMgr(vkMemoryManager), m_bufferType(bufferType), m_minimumBufferAllocSize(minimumBufferAllocSize) {};
VKRSynchronizedRingAllocator(const VKRSynchronizedRingAllocator&) = delete; // disallow copy
~VKRSynchronizedRingAllocator();

struct BufferSyncPoint_t
{
Expand Down Expand Up @@ -148,7 +150,7 @@ class VKRMemoryManager
}

// texture memory management
std::unordered_map<uint32, VkTextureChunkedHeap*> map_textureHeap; // one heap per memory type
std::unordered_map<uint32, std::unique_ptr<VkTextureChunkedHeap>> map_textureHeap; // one heap per memory type
std::vector<uint8> m_textureUploadBuffer;

// texture upload buffer
Expand Down
3 changes: 3 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ VKFUNC_DEVICE(vkCmdDraw);
VKFUNC_DEVICE(vkCmdCopyBufferToImage);
VKFUNC_DEVICE(vkCmdCopyImageToBuffer);
VKFUNC_DEVICE(vkCmdClearColorImage);
VKFUNC_DEVICE(vkCmdClearAttachments);
VKFUNC_DEVICE(vkCmdBindIndexBuffer);
VKFUNC_DEVICE(vkCmdBindVertexBuffers);
VKFUNC_DEVICE(vkCmdDrawIndexed);
Expand Down Expand Up @@ -198,6 +199,7 @@ VKFUNC_DEVICE(vkCmdEndTransformFeedbackEXT);

// query
VKFUNC_DEVICE(vkCreateQueryPool);
VKFUNC_DEVICE(vkDestroyQueryPool);
VKFUNC_DEVICE(vkCmdResetQueryPool);
VKFUNC_DEVICE(vkCmdBeginQuery);
VKFUNC_DEVICE(vkCmdEndQuery);
Expand Down Expand Up @@ -236,6 +238,7 @@ VKFUNC_DEVICE(vkAllocateDescriptorSets);
VKFUNC_DEVICE(vkFreeDescriptorSets);
VKFUNC_DEVICE(vkUpdateDescriptorSets);
VKFUNC_DEVICE(vkCreateDescriptorPool);
VKFUNC_DEVICE(vkDestroyDescriptorPool);
VKFUNC_DEVICE(vkDestroyDescriptorSetLayout);

#undef VKFUNC_INIT
Expand Down
Loading
Loading