From 1bf08f696be1f58e4e3c59bea9e8eda14a20a1fa Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 26 Jan 2024 23:19:18 +0100 Subject: [PATCH] Trying to fix weird sync issue --- src/Engine/Component/Mesh.h | 2 -- src/Engine/Graphics/RenderPass/BasePass.cpp | 4 ++-- .../Graphics/RenderPass/DepthPrepass.cpp | 3 +++ .../Graphics/RenderPass/SkyboxRenderPass.cpp | 20 +++++++++++++++---- .../Graphics/RenderPass/SkyboxRenderPass.h | 4 ++-- src/Engine/Graphics/Vulkan/Command.cpp | 2 +- src/Engine/Graphics/Vulkan/Debug.cpp | 16 +++++++++------ src/Engine/Graphics/Vulkan/Debug.h | 10 +++++++--- src/Engine/Graphics/Vulkan/Graphics.cpp | 19 +++++++++--------- src/Engine/Graphics/Vulkan/Graphics.h | 2 +- src/Engine/Graphics/Vulkan/PipelineCache.h | 1 + src/Engine/Graphics/Vulkan/Window.cpp | 2 +- 12 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/Engine/Component/Mesh.h b/src/Engine/Component/Mesh.h index 6dd63fa..5fad93f 100644 --- a/src/Engine/Component/Mesh.h +++ b/src/Engine/Component/Mesh.h @@ -8,8 +8,6 @@ namespace Component struct Mesh { PMeshAsset asset; - Mesh() {} - Mesh(PMeshAsset asset) : asset(asset) {} }; } // namespace Component } // namespace Seele diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 1215154..514ae9e 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -72,8 +72,8 @@ void BasePass::render() Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); depthAttachment->getTexture()->pipelineBarrier( - Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, - Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT); + Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, + Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT); depthAttachment->getTexture()->transferOwnership(Gfx::QueueType::GRAPHICS); depthAttachment->getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 9b3e6db..cd10cbd 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -9,6 +9,8 @@ #include "Math/Vector.h" #include "RenderGraph.h" #include "Graphics/Command.h" +#include +#include "Graphics/Vulkan/Graphics.h" using namespace Seele; @@ -130,6 +132,7 @@ void DepthPrepass::render() } graphics->executeCommands(commands); graphics->endRenderPass(); + //vkDeviceWaitIdle(((Vulkan::Graphics*)graphics.getHandle())->getDevice()); } void DepthPrepass::endFrame() diff --git a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp index 0147c1d..08d4008 100644 --- a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp @@ -34,6 +34,10 @@ void SkyboxRenderPass::beginFrame(const Component::Camera& cam) skyboxDataSet = skyboxDataLayout->allocateDescriptorSet(); skyboxDataSet->updateBuffer(0, skyboxBuffer); skyboxDataSet->writeChanges(); + skyboxBuffer->pipelineBarrier( + Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, + Gfx::SE_ACCESS_MEMORY_READ_BIT, Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT + ); textureSet = textureLayout->allocateDescriptorSet(); textureSet->updateTexture(0, skybox.day); textureSet->updateTexture(1, skybox.night); @@ -43,6 +47,14 @@ void SkyboxRenderPass::beginFrame(const Component::Camera& cam) void SkyboxRenderPass::render() { + colorAttachment->getTexture()->pipelineBarrier( + Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + Gfx::SE_ACCESS_COLOR_ATTACHMENT_READ_BIT, Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT + ); + depthAttachment->getTexture()->pipelineBarrier( + Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, + Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT + ); graphics->beginRenderPass(renderPass); Gfx::PRenderCommand renderCommand = graphics->createRenderCommand("SkyboxRender"); renderCommand->setViewport(viewport); @@ -74,12 +86,12 @@ void SkyboxRenderPass::publishOutputs() void SkyboxRenderPass::createRenderPass() { - baseColorAttachment = resources->requestRenderTarget("BASEPASS_COLOR"); - baseColorAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD; - Gfx::PRenderTargetAttachment depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH"); + colorAttachment = resources->requestRenderTarget("BASEPASS_COLOR"); + colorAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD; + depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH"); depthAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD; Gfx::ORenderTargetLayout layout = new Gfx::RenderTargetLayout{ - .colorAttachments = { baseColorAttachment }, + .colorAttachments = { colorAttachment }, .depthAttachment = depthAttachment }; renderPass = graphics->createRenderPass(std::move(layout), viewport); diff --git a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.h b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.h index 85fed84..a91019c 100644 --- a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.h +++ b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.h @@ -18,8 +18,8 @@ public: virtual void publishOutputs() override; virtual void createRenderPass() override; private: - Gfx::PRenderTargetAttachment baseColorAttachment; - Gfx::OVertexBuffer cubeBuffer; + Gfx::PRenderTargetAttachment colorAttachment; + Gfx::PRenderTargetAttachment depthAttachment; Gfx::ODescriptorLayout skyboxDataLayout; Gfx::PDescriptorSet skyboxDataSet; Gfx::ODescriptorLayout textureLayout; diff --git a/src/Engine/Graphics/Vulkan/Command.cpp b/src/Engine/Graphics/Vulkan/Command.cpp index 0dc0bc3..29a173c 100644 --- a/src/Engine/Graphics/Vulkan/Command.cpp +++ b/src/Engine/Graphics/Vulkan/Command.cpp @@ -459,7 +459,7 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue) .queueFamilyIndex = queue->getFamilyIndex(), }; VK_CHECK(vkCreateCommandPool(graphics->getDevice(), &info, nullptr, &commandPool)); - + // TODO: dont reset individual commands, reset pool instead allocatedBuffers.add(new Command(graphics, commandPool, this)); command = allocatedBuffers.back(); diff --git a/src/Engine/Graphics/Vulkan/Debug.cpp b/src/Engine/Graphics/Vulkan/Debug.cpp index 4d81d8c..ace4b8b 100644 --- a/src/Engine/Graphics/Vulkan/Debug.cpp +++ b/src/Engine/Graphics/Vulkan/Debug.cpp @@ -3,15 +3,19 @@ using namespace Seele::Vulkan; -VkBool32 Seele::Vulkan::debugCallback(VkDebugReportFlagsEXT, VkDebugReportObjectTypeEXT, uint64_t, size_t, int32_t, const char *layerPrefix, const char *msg, void *) +VkBool32 Seele::Vulkan::debugCallback( + VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageTypes, + const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, + void* pUserData) { - std::cerr << layerPrefix << ": " << msg << std::endl; + std::cerr << pCallbackData->pMessage << std::endl; return VK_FALSE; } -VkResult Seele::Vulkan::CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) +VkResult Seele::Vulkan::CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT *pCallback) { - auto func = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT"); + auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); if (func != nullptr) { return func(instance, pCreateInfo, pAllocator, pCallback); @@ -22,9 +26,9 @@ VkResult Seele::Vulkan::CreateDebugReportCallbackEXT(VkInstance instance, const } } -void Seele::Vulkan::DestroyDebugReportCallbackEXT(VkInstance instance, const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT pCallback) +void Seele::Vulkan::DestroyDebugUtilsMessengerEXT(VkInstance instance, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT pCallback) { - auto func = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT"); + auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); if (func != nullptr) { func(instance, pCallback, pAllocator); diff --git a/src/Engine/Graphics/Vulkan/Debug.h b/src/Engine/Graphics/Vulkan/Debug.h index 1f1cee9..a4165cd 100644 --- a/src/Engine/Graphics/Vulkan/Debug.h +++ b/src/Engine/Graphics/Vulkan/Debug.h @@ -6,9 +6,13 @@ namespace Seele { namespace Vulkan { -VkBool32 debugCallback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objType, uint64_t obj, size_t location, int32_t code, const char *layerPrefix, const char *msg, void *userData); +VkBool32 debugCallback( + VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageTypes, + const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, + void* pUserData); -VkResult CreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback); -void DestroyDebugReportCallbackEXT(VkInstance instance, const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT pCallback); +VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT *pCallback); +void DestroyDebugUtilsMessengerEXT(VkInstance instance, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT pCallback); } // namespace Vulkan } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index 5b32831..3887530 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -42,7 +42,7 @@ Graphics::~Graphics() shaderCompiler = nullptr; pools.clear(); vkDestroyDevice(handle, nullptr); - DestroyDebugReportCallbackEXT(instance, nullptr, callback); + DestroyDebugUtilsMessengerEXT(instance, nullptr, callback); vkDestroyInstance(instance, nullptr); } @@ -367,7 +367,7 @@ Array Graphics::getRequiredExtensions() extensions.add(glfwExtensions[i]); } #if ENABLE_VALIDATION - extensions.add(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); + extensions.add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); #endif // ENABLE_VALIDATION return extensions; } @@ -381,7 +381,7 @@ void Graphics::initInstance(GraphicsInitializer initInfo) .applicationVersion = VK_MAKE_VERSION(0, 0, 1), .pEngineName = initInfo.engineName, .engineVersion = VK_MAKE_VERSION(0, 0, 1), - .apiVersion = VK_API_VERSION_1_3, + .apiVersion = VK_API_VERSION_1_2, }; @@ -411,15 +411,16 @@ void Graphics::initInstance(GraphicsInitializer initInfo) } void Graphics::setupDebugCallback() { - VkDebugReportCallbackCreateInfoEXT createInfo = { - .sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT, + VkDebugUtilsMessengerCreateInfoEXT createInfo = { + .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, .pNext = nullptr, - .flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, - .pfnCallback = &debugCallback, + .flags = 0, + .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT, + .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT, + .pfnUserCallback = &debugCallback, .pUserData = nullptr, }; - - VK_CHECK(CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback)); + VK_CHECK(CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &callback)); } void Graphics::pickPhysicalDevice() diff --git a/src/Engine/Graphics/Vulkan/Graphics.h b/src/Engine/Graphics/Vulkan/Graphics.h index 43feea7..4498d68 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.h +++ b/src/Engine/Graphics/Vulkan/Graphics.h @@ -98,7 +98,7 @@ protected: VkPhysicalDeviceFeatures2 features; VkPhysicalDeviceVulkan11Features features11; VkPhysicalDeviceVulkan12Features features12; - VkDebugReportCallbackEXT callback; + VkDebugUtilsMessengerEXT callback; Map allocatedFramebuffers; VmaAllocator allocator; OPipelineCache pipelineCache; diff --git a/src/Engine/Graphics/Vulkan/PipelineCache.h b/src/Engine/Graphics/Vulkan/PipelineCache.h index 58b6e2d..e6bbfc0 100644 --- a/src/Engine/Graphics/Vulkan/PipelineCache.h +++ b/src/Engine/Graphics/Vulkan/PipelineCache.h @@ -16,6 +16,7 @@ public: private: Map graphicsPipelines; Map computePipelines; + std::mutex cacheLock; VkPipelineCache cache; PGraphics graphics; std::string cacheFile; diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index a987a10..0520aab 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -111,8 +111,8 @@ void Window::beginFrame() imageAvailableFences[currentSemaphoreIndex]->wait(100000); imageAvailableFences[currentSemaphoreIndex]->reset(); vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits::max(), imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(), imageAvailableFences[currentSemaphoreIndex]->getHandle(), ¤tImageIndex); - swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, imageAvailableSemaphores[currentSemaphoreIndex]); + swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); } void Window::endFrame()