From 6d04d9554eba18b64e9da584fca8fd4a389da475 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 23 Nov 2026 16:40:42 +0000 Subject: [PATCH 1/4] Remove legacy barrier functions that are hard to reason about. --- .../Ecs/Graphics/RenderOrchestratorSystem.hpp | 8 +- .../Ecs/Graphics/SpriteRendererSystem.hpp | 8 +- .../NovelRT/Graphics/GraphicsCmdList.hpp | 13 +- .../GraphicsRenderPassDescription.hpp | 3 +- Graphics/Vulkan/VulkanGraphicsCmdList.cpp | 116 +++++++----------- .../Graphics/Vulkan/Utilities/ImageLayout.hpp | 6 +- .../Graphics/Vulkan/VulkanGraphicsCmdList.hpp | 11 +- Samples/Experimental/ImGui/main.cpp | 15 ++- Samples/Experimental/VulkanRender/main.cpp | 4 +- .../NovelRT/UI/ImGui/ImGuiUIProvider.hpp | 4 +- 10 files changed, 86 insertions(+), 102 deletions(-) diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp index 0a8db2f5d..937228a61 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp @@ -165,13 +165,13 @@ namespace NovelRT::Ecs::Graphics colourDataStruct.depth = 0; colourDataStruct.stencil = 0; - cmdList->CmdTransitionSwapchainImageTo(image, NovelRT::Graphics::ImageLayout::Undefined, - NovelRT::Graphics::ImageLayout::DstTransfer); + cmdList->CmdTransitionTextureTo(image, NovelRT::Graphics::ImageLayout::Undefined, + NovelRT::Graphics::ImageLayout::DstTransfer); cmdList->CmdClearColourSwapchainImage(image, colourDataStruct); - cmdList->CmdTransitionSwapchainImageTo(image, NovelRT::Graphics::ImageLayout::DstTransfer, - NovelRT::Graphics::ImageLayout::Present); + cmdList->CmdTransitionTextureTo(image, NovelRT::Graphics::ImageLayout::DstTransfer, + NovelRT::Graphics::ImageLayout::Present); for (auto& root : roots) { EnumerateChildren(root, renderPasses, ordered); diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp index 5d9090c40..fd5f3ec07 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp @@ -39,7 +39,6 @@ #include -#include #include #include #include @@ -230,9 +229,12 @@ namespace NovelRT::Ecs::Graphics textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion); - uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); + uploadCmdList->CmdTransitionTextureTo(texture2D, NovelRT::Graphics::ImageLayout::Undefined, + NovelRT::Graphics::ImageLayout::DstTransfer); uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion); - uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); + uploadCmdList->CmdTransitionTextureTo( + texture2D, NovelRT::Graphics::ImageLayout::DstTransfer, + NovelRT::Graphics::ImageLayout::ShaderReadOnlyOptimal); _pendingStagingBuffers.push_back({_submittedUploads, textureStagingBuffer, textureStagingBufferRegion, gfxContext, uploadCmdList}); diff --git a/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp b/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp index 178c12f47..9c62ef8dd 100644 --- a/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp +++ b/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp @@ -117,9 +117,6 @@ namespace NovelRT::Graphics void CmdSetViewport(ViewportInfo viewportInfo); - void CmdBeginTexturePipelineBarrierLegacyVersion(const std::shared_ptr>& texture); - void CmdEndTexturePipelineBarrierLegacyVersion(const std::shared_ptr>& texture); - void CmdBindPipeline(const std::shared_ptr>& pipeline); void CmdPushConstants(const std::shared_ptr>& pipelineSignature, @@ -135,9 +132,13 @@ namespace NovelRT::Graphics void CmdExecuteCommands(const std::shared_ptr>& cmdList); - void CmdTransitionSwapchainImageTo(const std::shared_ptr>& swapchainImage, - ImageLayout oldLayout, - ImageLayout newLayout); + void CmdTransitionTextureTo(const std::shared_ptr>& swapchainImage, + ImageLayout oldLayout, + ImageLayout newLayout); + + void CmdTransitionTextureTo(const std::shared_ptr>& texture, + ImageLayout oldLayout, + ImageLayout newLayout); void CmdClearColourSwapchainImage(const std::shared_ptr>& swapchainImage, ClearValue clearData); diff --git a/Graphics/Core/include/NovelRT/Graphics/GraphicsRenderPassDescription.hpp b/Graphics/Core/include/NovelRT/Graphics/GraphicsRenderPassDescription.hpp index 330ff97e7..002661439 100644 --- a/Graphics/Core/include/NovelRT/Graphics/GraphicsRenderPassDescription.hpp +++ b/Graphics/Core/include/NovelRT/Graphics/GraphicsRenderPassDescription.hpp @@ -24,10 +24,11 @@ namespace NovelRT::Graphics enum class ImageLayout : uint32_t { Undefined, - Optimal, + General, Present, SrcTransfer, DstTransfer, + ShaderReadOnlyOptimal, }; struct GraphicsAttachmentDescription diff --git a/Graphics/Vulkan/VulkanGraphicsCmdList.cpp b/Graphics/Vulkan/VulkanGraphicsCmdList.cpp index ca5aa9462..b7b22c019 100644 --- a/Graphics/Vulkan/VulkanGraphicsCmdList.cpp +++ b/Graphics/Vulkan/VulkanGraphicsCmdList.cpp @@ -50,10 +50,6 @@ namespace NovelRT::Graphics switch (layout) { - case VK_IMAGE_LAYOUT_UNDEFINED: - accessMask = 0; - stageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - break; case VK_IMAGE_LAYOUT_GENERAL: accessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; stageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; @@ -70,6 +66,7 @@ namespace NovelRT::Graphics accessMask = VK_ACCESS_TRANSFER_WRITE_BIT; stageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; break; + case VK_IMAGE_LAYOUT_UNDEFINED: default: accessMask = 0; stageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; @@ -485,72 +482,6 @@ namespace NovelRT::Graphics } } - void VulkanGraphicsCmdList::CmdBeginTexturePipelineBarrierLegacyVersion( - const std::shared_ptr& texture) - { - VkImageSubresourceRange subresourceRange{}; - subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - subresourceRange.levelCount = 1; - subresourceRange.layerCount = 1; - - VkImageMemoryBarrier vulkanImageMemoryBarrier{}; - vulkanImageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - vulkanImageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - vulkanImageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; - vulkanImageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - vulkanImageMemoryBarrier.subresourceRange = subresourceRange; - vulkanImageMemoryBarrier.image = texture->GetVulkanImage(); - - if (_commandBuffer == VK_NULL_HANDLE) - { - _replayData.emplace_back( - [vulkanImageMemoryBarrier](VkCommandBuffer cmdBuffer) - { - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, - nullptr, 0, nullptr, 1, &vulkanImageMemoryBarrier); - }); - } - else - { - vkCmdPipelineBarrier(_commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, - nullptr, 0, nullptr, 1, &vulkanImageMemoryBarrier); - } - } - - void VulkanGraphicsCmdList::CmdEndTexturePipelineBarrierLegacyVersion( - const std::shared_ptr& texture) - { - VkImageSubresourceRange subresourceRange{}; - subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - subresourceRange.levelCount = 1; - subresourceRange.layerCount = 1; - - VkImageMemoryBarrier vulkanImageMemoryBarrier{}; - vulkanImageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - vulkanImageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - vulkanImageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; - vulkanImageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - vulkanImageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - vulkanImageMemoryBarrier.subresourceRange = subresourceRange; - vulkanImageMemoryBarrier.image = texture->GetVulkanImage(); - - if (_commandBuffer == VK_NULL_HANDLE) - { - _replayData.emplace_back( - [vulkanImageMemoryBarrier](VkCommandBuffer cmdBuffer) - { - vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, - &vulkanImageMemoryBarrier); - }); - } - else - { - vkCmdPipelineBarrier(_commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, - 0, 0, nullptr, 0, nullptr, 1, &vulkanImageMemoryBarrier); - } - } - void VulkanGraphicsCmdList::CmdBindPipeline(const std::shared_ptr& pipeline) { if (_commandBuffer == VK_NULL_HANDLE) @@ -644,7 +575,7 @@ namespace NovelRT::Graphics if (childBuffer == VK_NULL_HANDLE) { - // NOTE: Not sure we want to rely on same-time-friend behaviour (I don't know what the official name is) for + // NOTE: Not sure we want to rely on same-type-friend behaviour (I don't know what the official name is) for // this. It compiles though. Leaving it for the code review. - Matt J. for (const auto& commandFrame : cmdList->_replayData) { @@ -657,7 +588,7 @@ namespace NovelRT::Graphics vkCmdExecuteCommands(_commandBuffer, 1, &childBuffer); } - void VulkanGraphicsCmdList::CmdTransitionSwapchainImageTo( + void VulkanGraphicsCmdList::CmdTransitionTextureTo( const std::shared_ptr>& swapchainImage, ImageLayout oldLayout, ImageLayout newLayout) @@ -698,6 +629,47 @@ namespace NovelRT::Graphics } } + void VulkanGraphicsCmdList::CmdTransitionTextureTo( + const std::shared_ptr>& texture, + ImageLayout oldLayout, + ImageLayout newLayout) + { + VkImageLayout vkOldLayout = Vulkan::GetVulkanImageLayout(oldLayout); + VkImageLayout vkNewLayout = Vulkan::GetVulkanImageLayout(newLayout); + + VkImageSubresourceRange subresourceRange{}; + subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + subresourceRange.levelCount = 1; + subresourceRange.layerCount = 1; + + const auto [srcAccessMask, srcStageMask] = GetAccessAndStageMasksForLayout(vkOldLayout, true); + const auto [dstAccessMask, dstStageMask] = GetAccessAndStageMasksForLayout(vkNewLayout, false); + + VkImageMemoryBarrier barrier{}; + barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; + barrier.srcAccessMask = srcAccessMask; + barrier.dstAccessMask = dstAccessMask; + barrier.oldLayout = vkOldLayout; + barrier.newLayout = vkNewLayout; + barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.image = texture->GetVulkanImage(); + barrier.subresourceRange = subresourceRange; + + if (_commandBuffer == VK_NULL_HANDLE) + { + _replayData.emplace_back( + [srcStageMask, dstStageMask, barrier](VkCommandBuffer cmdBuffer) + { + vkCmdPipelineBarrier(cmdBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr, 1, &barrier); + }); + } + else + { + vkCmdPipelineBarrier(_commandBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr, 1, &barrier); + } + } + void VulkanGraphicsCmdList::CmdClearColourSwapchainImage( const std::shared_ptr>& swapchainImage, ClearValue clearData) diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/ImageLayout.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/ImageLayout.hpp index e599fa99e..9e1ad4c53 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/ImageLayout.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/ImageLayout.hpp @@ -5,6 +5,7 @@ #include #include +#include namespace NovelRT::Graphics::Vulkan { @@ -17,7 +18,7 @@ namespace NovelRT::Graphics::Vulkan case ImageLayout::Undefined: returnLayout = VK_IMAGE_LAYOUT_UNDEFINED; break; - case ImageLayout::Optimal: + case ImageLayout::General: returnLayout = VK_IMAGE_LAYOUT_GENERAL; break; case ImageLayout::Present: @@ -29,6 +30,9 @@ namespace NovelRT::Graphics::Vulkan case ImageLayout::DstTransfer: returnLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; break; + case ImageLayout::ShaderReadOnlyOptimal: + returnLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + break; } return returnLayout; diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp index b04528643..218290e83 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp @@ -95,11 +95,6 @@ namespace NovelRT::Graphics void CmdSetViewport(ViewportInfo viewportInfo); - void CmdBeginTexturePipelineBarrierLegacyVersion( - const std::shared_ptr>& texture); - void CmdEndTexturePipelineBarrierLegacyVersion( - const std::shared_ptr>& texture); - void CmdBindPipeline(const std::shared_ptr>& pipeline); void CmdPushConstants( @@ -116,11 +111,15 @@ namespace NovelRT::Graphics void CmdExecuteCommands(const std::shared_ptr>& cmdList); - void CmdTransitionSwapchainImageTo( + void CmdTransitionTextureTo( const std::shared_ptr>& swapchainImage, ImageLayout oldLayout, ImageLayout newLayout); + void CmdTransitionTextureTo(const std::shared_ptr>& texture, + ImageLayout oldLayout, + ImageLayout newLayout); + void CmdClearColourSwapchainImage( const std::shared_ptr>& swapchainImage, ClearValue clearData); diff --git a/Samples/Experimental/ImGui/main.cpp b/Samples/Experimental/ImGui/main.cpp index 89c34aa43..39e9ca6fe 100644 --- a/Samples/Experimental/ImGui/main.cpp +++ b/Samples/Experimental/ImGui/main.cpp @@ -184,9 +184,14 @@ RenderingData SetupTriangleSample(std::shared_ptrtemplate Map(vertexBufferRegion); - pVertexBuffer[0] = TexturedVertex{NovelRT::Maths::GeoVector3F(0, 1, 0), NovelRT::Maths::GeoVector2F(0.5f, 0.0f)}; - pVertexBuffer[1] = TexturedVertex{NovelRT::Maths::GeoVector3F(1, -1, 0), NovelRT::Maths::GeoVector2F(1.0f, 1.0f)}; - pVertexBuffer[2] = TexturedVertex{NovelRT::Maths::GeoVector3F(-1, -1, 0), NovelRT::Maths::GeoVector2F(0.0f, 1.0f)}; + pVertexBuffer[0] = + TexturedVertex{.Position = NovelRT::Maths::GeoVector3F(0, 1, 0), .UV = NovelRT::Maths::GeoVector2F(0.5f, 0.0f)}; + + pVertexBuffer[1] = TexturedVertex{.Position = NovelRT::Maths::GeoVector3F(1, -1, 0), + .UV = NovelRT::Maths::GeoVector2F(1.0f, 1.0f)}; + + pVertexBuffer[2] = TexturedVertex{.Position = NovelRT::Maths::GeoVector3F(-1, -1, 0), + .UV = NovelRT::Maths::GeoVector2F(0.0f, 1.0f)}; vertexStagingBuffer->UnmapAndWrite(vertexBufferRegion); @@ -219,9 +224,9 @@ RenderingData SetupTriangleSample(std::shared_ptrBegin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); - cmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); cmdList->CmdCopy(texture2D, textureStagingBufferRegion); - cmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); cmdList->End(); gfxContext->EndFrame(); diff --git a/Samples/Experimental/VulkanRender/main.cpp b/Samples/Experimental/VulkanRender/main.cpp index 0420fe549..d97c8f6d1 100644 --- a/Samples/Experimental/VulkanRender/main.cpp +++ b/Samples/Experimental/VulkanRender/main.cpp @@ -200,9 +200,9 @@ RenderingData SetupTriangleSample(std::shared_ptrBegin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); - cmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); cmdList->CmdCopy(texture2D, textureStagingBufferRegion); - cmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); cmdList->End(); gfxContext->EndFrame(); diff --git a/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp b/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp index 8c9d4d04b..7cecf11dd 100644 --- a/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp +++ b/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp @@ -639,9 +639,9 @@ namespace NovelRT::UI::ImGui // Send the data to GPU cmdList->Begin(); - cmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); cmdList->CmdCopy(texture2D, stagingBufferRegion); - cmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); cmdList->End(); // End Frame From e29fb435ea5f7db4329e70e945421915c978fb92 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Tue, 24 Nov 2026 22:02:00 +0000 Subject: [PATCH 2/4] More memory barrier antics. --- .../Ecs/Graphics/SpriteRendererSystem.hpp | 6 +-- .../include/NovelRT/Ecs/UI/EcsUIBuilder.hpp | 2 +- Ecs/UI/include/NovelRT/Ecs/UI/UISystem.hpp | 5 ++- .../NovelRT/Graphics/GraphicsDevice.hpp | 1 + Graphics/Vulkan/VulkanGraphicsContext.cpp | 10 +++-- Graphics/Vulkan/VulkanGraphicsDevice.cpp | 12 +++++- .../Vulkan/VulkanGraphicsMemoryAllocator.cpp | 37 ++++++++++++++++--- .../Graphics/Vulkan/Utilities/Support.hpp | 21 ++++------- .../Graphics/Vulkan/VulkanGraphicsContext.hpp | 5 ++- .../Graphics/Vulkan/VulkanGraphicsDevice.hpp | 1 + LuaOnlyTargets/VisualNovel/main.cpp | 8 ++++ Samples/Experimental/ImGui/main.cpp | 12 +++--- Samples/Experimental/VulkanRender/main.cpp | 7 ++-- 13 files changed, 88 insertions(+), 39 deletions(-) diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp index fd5f3ec07..837cff33a 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp @@ -212,9 +212,9 @@ namespace NovelRT::Ecs::Graphics .size = totalSize}; auto textureStagingBuffer = _memoryAllocator->CreateBuffer(bufferCreateInfo); - auto gfxContext = _graphicsDevice->CreateGraphicsContext(); - + auto gfxContext = _graphicsDevice->CreateTransferContext(); auto uploadCmdList = gfxContext->CreateCmdList(); + uploadCmdList->Begin(); for (auto [texture, texture2D] : newTextures) @@ -556,7 +556,7 @@ namespace NovelRT::Ecs::Graphics _pipeline = _graphicsDevice->CreatePipeline(signature, vertShaderProg, pixelShaderProg, _renderPass.RenderPass); - auto gfxContext = _graphicsDevice->CreateGraphicsContext(); + auto gfxContext = _graphicsDevice->CreateTransferContext(); auto cmdList = gfxContext->CreateCmdList(); cmdList->Begin(); cmdList->CmdCopy(_vertexBufferRegion, stagingRegion); diff --git a/Ecs/UI/include/NovelRT/Ecs/UI/EcsUIBuilder.hpp b/Ecs/UI/include/NovelRT/Ecs/UI/EcsUIBuilder.hpp index bb236aab0..1aeef0634 100644 --- a/Ecs/UI/include/NovelRT/Ecs/UI/EcsUIBuilder.hpp +++ b/Ecs/UI/include/NovelRT/Ecs/UI/EcsUIBuilder.hpp @@ -133,7 +133,7 @@ namespace NovelRT::Ecs::UI { // By default, we will upload the font data when creating the default UI system. // This is the user's responsibility if using a non-default UI system. - auto gfxContext = _gfxDevice->CreateGraphicsContext(); + auto gfxContext = _gfxDevice->CreateTransferContext(); auto cmdList = gfxContext->CreateCmdList(); _uiProvider->UploadFontData(cmdList); diff --git a/Ecs/UI/include/NovelRT/Ecs/UI/UISystem.hpp b/Ecs/UI/include/NovelRT/Ecs/UI/UISystem.hpp index f124117d5..e2379eb3f 100644 --- a/Ecs/UI/include/NovelRT/Ecs/UI/UISystem.hpp +++ b/Ecs/UI/include/NovelRT/Ecs/UI/UISystem.hpp @@ -217,8 +217,8 @@ namespace NovelRT::Ecs::UI Graphics::Components::TrackedSemaphore>(); // create upload command list - auto uiContext = _gfxDevice->CreateGraphicsContext(); - auto uploadCmdList = uiContext->CreateCmdList(); + auto uploadContext = _gfxDevice->CreateTransferContext(); + auto uploadCmdList = uploadContext->CreateCmdList(); // upload data to gpu if (_uiProvider->UploadToGPU(uploadCmdList)) @@ -270,6 +270,7 @@ namespace NovelRT::Ecs::UI trackedSemaphores.AddComponent(_drawSemaphoreEntity.value(), signalSemaComponent); // create the draw cmdList + auto uiContext = _gfxDevice->CreateGraphicsContext(); auto drawCmdList = uiContext->CreateCmdList(std::optional>( {_uiProvider->GetDedicatedRenderPass(), 0})); diff --git a/Graphics/Core/include/NovelRT/Graphics/GraphicsDevice.hpp b/Graphics/Core/include/NovelRT/Graphics/GraphicsDevice.hpp index 1bf532254..ec6113ff5 100644 --- a/Graphics/Core/include/NovelRT/Graphics/GraphicsDevice.hpp +++ b/Graphics/Core/include/NovelRT/Graphics/GraphicsDevice.hpp @@ -80,6 +80,7 @@ namespace NovelRT::Graphics NovelRT::Utilities::Span byteData); [[nodiscard]] std::shared_ptr> CreateGraphicsContext(); + [[nodiscard]] std::shared_ptr> CreateTransferContext(); [[nodiscard]] std::shared_ptr> BeginFrame(); void EndFrame(); diff --git a/Graphics/Vulkan/VulkanGraphicsContext.cpp b/Graphics/Vulkan/VulkanGraphicsContext.cpp index d8804be34..6d96d3b2e 100644 --- a/Graphics/Vulkan/VulkanGraphicsContext.cpp +++ b/Graphics/Vulkan/VulkanGraphicsContext.cpp @@ -39,7 +39,7 @@ namespace NovelRT::Graphics VkCommandPoolCreateInfo commandPoolCreateInfo{}; commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - commandPoolCreateInfo.queueFamilyIndex = device->GetIndicesData().graphicsFamily.value(); + commandPoolCreateInfo.queueFamilyIndex = _queueFamilyIndex; const VkResult result = vkCreateCommandPool(device->GetVulkanDevice(), &commandPoolCreateInfo, nullptr, &vulkanCommandPool); @@ -105,9 +105,11 @@ namespace NovelRT::Graphics return std::static_pointer_cast(GraphicsDeviceObject::shared_from_this()); } - VulkanGraphicsContext::GraphicsContext( - std::shared_ptr> device) noexcept - : _device(std::move(device)), _vulkanCommandPool([this]() { return CreateVulkanCommandPool(); }) + VulkanGraphicsContext::GraphicsContext(std::shared_ptr> device, + uint32_t queueFamilyIndex) noexcept + : _device(std::move(device)), + _queueFamilyIndex(queueFamilyIndex), + _vulkanCommandPool([this]() { return CreateVulkanCommandPool(); }) { unused(_state.Transition(Threading::VolatileState::Initialised)); } diff --git a/Graphics/Vulkan/VulkanGraphicsDevice.cpp b/Graphics/Vulkan/VulkanGraphicsDevice.cpp index df446d655..ad7af5d31 100644 --- a/Graphics/Vulkan/VulkanGraphicsDevice.cpp +++ b/Graphics/Vulkan/VulkanGraphicsDevice.cpp @@ -170,7 +170,8 @@ namespace NovelRT::Graphics _indicesData = Vulkan::Utilities::FindQueueFamilies(_adapter->GetPhysicalDevice(), _surface); std::vector queueCreateInfos{}; - std::set uniqueQueueFamilies{_indicesData.graphicsFamily.value(), _indicesData.presentFamily.value()}; + std::set uniqueQueueFamilies{_indicesData.graphicsFamily.value(), _indicesData.presentFamily.value(), + _indicesData.transferFamily.value()}; float queuePriority = 1.0f; for (uint32_t queueFamily : uniqueQueueFamilies) @@ -349,7 +350,14 @@ namespace NovelRT::Graphics std::shared_ptr VulkanGraphicsDevice::CreateGraphicsContext() { - return std::make_shared(shared_from_this()); + unused(GetVulkanDevice()); + return std::make_shared(shared_from_this(), _indicesData.graphicsFamily.value()); + } + + std::shared_ptr VulkanGraphicsDevice::CreateTransferContext() + { + unused(GetVulkanDevice()); + return std::make_shared(shared_from_this(), _indicesData.transferFamily.value()); } std::shared_ptr> VulkanGraphicsDevice::BeginFrame() diff --git a/Graphics/Vulkan/VulkanGraphicsMemoryAllocator.cpp b/Graphics/Vulkan/VulkanGraphicsMemoryAllocator.cpp index 6e17af24e..f7d7e04d7 100644 --- a/Graphics/Vulkan/VulkanGraphicsMemoryAllocator.cpp +++ b/Graphics/Vulkan/VulkanGraphicsMemoryAllocator.cpp @@ -14,6 +14,8 @@ #include #include +#include + namespace NovelRT::Graphics { using VulkanGraphicsBuffer = GraphicsBuffer; @@ -55,7 +57,7 @@ namespace NovelRT::Graphics if (result != VK_SUCCESS) { - Exceptions::InitialisationFailureException("Failed to create the VulkanMemoryAllocator.", result); + throw Exceptions::InitialisationFailureException("Failed to create the VulkanMemoryAllocator.", result); } } @@ -78,6 +80,16 @@ namespace NovelRT::Graphics bufferCreateInfo.usage = Vulkan::Utilities::GetVulkanBufferUsageKind(createInfo.bufferKind, createInfo.gpuAccessKind); + const auto& indices = _device->GetIndicesData(); + const std::array queueFamilies{indices.graphicsFamily.value(), indices.transferFamily.value()}; + + if (queueFamilies[0] != queueFamilies[1]) + { + bufferCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT; + bufferCreateInfo.queueFamilyIndexCount = 2; + bufferCreateInfo.pQueueFamilyIndices = queueFamilies.data(); + } + VmaAllocationCreateInfo allocationCreateInfo{}; allocationCreateInfo.flags = Vulkan::Utilities::GetVmaAllocationKind(createInfo.cpuAccessKind); allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; @@ -139,6 +151,16 @@ namespace NovelRT::Graphics imageCreateInfo.usage = Vulkan::Utilities::GetVulkanImageUsageKind(createInfo.textureKind, createInfo.gpuAccessKind); + const auto& indices = _device->GetIndicesData(); + const std::array queueFamilies{indices.graphicsFamily.value(), indices.transferFamily.value()}; + + if (queueFamilies[0] != queueFamilies[1]) + { + imageCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT; + imageCreateInfo.queueFamilyIndexCount = 2; + imageCreateInfo.pQueueFamilyIndices = queueFamilies.data(); + } + VkImage vulkanImage = VK_NULL_HANDLE; VmaAllocation outAllocation = VK_NULL_HANDLE; VmaAllocationInfo outAllocationInfo{}; // TODO: I haven't figured out what I am doing with this yet, but I'm @@ -167,10 +189,15 @@ namespace NovelRT::Graphics [[nodiscard]] std::shared_ptr VulkanGraphicsMemoryAllocator:: CreateTexture2DRepeatGpuWriteOnly(uint32_t width, uint32_t height) { - return CreateTexture( - GraphicsTextureCreateInfo{GraphicsTextureAddressMode::Repeat, GraphicsTextureKind::TwoDimensional, - GraphicsResourceAccess::None, GraphicsResourceAccess::Write, width, height, 1, - GraphicsMemoryRegionAllocationFlags::None, TexelFormat::R8G8B8A8_UNORM}); + return CreateTexture(GraphicsTextureCreateInfo{.addressMode = GraphicsTextureAddressMode::Repeat, + .textureKind = GraphicsTextureKind::TwoDimensional, + .cpuAccessKind = GraphicsResourceAccess::None, + .gpuAccessKind = GraphicsResourceAccess::Write, + .width = width, + .height = height, + .depth = 1, + .allocationFlags = GraphicsMemoryRegionAllocationFlags::None, + .texelFormat = TexelFormat::R8G8B8A8_UNORM}); } VmaAllocator VulkanGraphicsMemoryAllocator::GetVmaAllocator() const noexcept diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/Support.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/Support.hpp index 82d31fec6..f4e488105 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/Support.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/Support.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -32,14 +31,10 @@ namespace NovelRT::Graphics::Vulkan::Utilities returnObject.graphicsFamily = familyIndex; } - if (queueFamily.queueFlags & VK_QUEUE_TRANSFER_BIT) + if (!returnObject.transferFamily.has_value() && (queueFamily.queueFlags & VK_QUEUE_TRANSFER_BIT) && + !(queueFamily.queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT))) { - if (!returnObject.transferFamily.has_value() || - (Maths::Utilities::PopCount(queueFamily.queueFlags) <= - Maths::Utilities::PopCount(returnObject.transferFamily.value()))) - { - returnObject.transferFamily = familyIndex; - } + returnObject.transferFamily = familyIndex; } vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, familyIndex, surface, &presentSupport); @@ -48,14 +43,14 @@ namespace NovelRT::Graphics::Vulkan::Utilities returnObject.presentFamily = familyIndex; } - if (returnObject.IsComplete()) - { - break; - } - ++familyIndex; } + if (!returnObject.transferFamily.has_value()) + { + returnObject.transferFamily = returnObject.graphicsFamily; + } + return returnObject; } diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsContext.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsContext.hpp index 0f39f5c0c..8a4ee5a62 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsContext.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsContext.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -27,6 +28,7 @@ namespace NovelRT::Graphics { private: std::shared_ptr> _device; + uint32_t _queueFamilyIndex; mutable NovelRT::Utilities::Lazy _vulkanCommandPool; @@ -41,7 +43,8 @@ namespace NovelRT::Graphics // NOLINTNEXTLINE(readability-identifier-naming) - stdlib compatibility std::shared_ptr> shared_from_this() const; - GraphicsContext(std::shared_ptr> device) noexcept; + GraphicsContext(std::shared_ptr> device, + uint32_t queueFamilyIndex) noexcept; ~GraphicsContext() final; [[nodiscard]] std::shared_ptr> GetDevice() const; diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsDevice.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsDevice.hpp index 0ade55fec..727712ffa 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsDevice.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsDevice.hpp @@ -102,6 +102,7 @@ namespace NovelRT::Graphics NovelRT::Utilities::Span byteData); [[nodiscard]] std::shared_ptr> CreateGraphicsContext(); + [[nodiscard]] std::shared_ptr> CreateTransferContext(); [[nodiscard]] std::shared_ptr> BeginFrame(); void EndFrame(); diff --git a/LuaOnlyTargets/VisualNovel/main.cpp b/LuaOnlyTargets/VisualNovel/main.cpp index 3deb1e00c..c516506c2 100644 --- a/LuaOnlyTargets/VisualNovel/main.cpp +++ b/LuaOnlyTargets/VisualNovel/main.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include @@ -304,6 +305,13 @@ int main() VulkanGraphicsAdapterSelector selector{}; auto gfxAdapter = selector.GetDefaultRecommendedAdapter(gfxProvider, gfxSurfaceContext); + + if (gfxAdapter == nullptr) + { + throw NovelRT::Exceptions::NotSupportedException( + "The current computer does not meet the minimum requirements to run the NovelRT engine."); + } + auto gfxDevice = gfxAdapter->CreateDevice(gfxSurfaceContext); auto memoryAllocator = std::make_shared>(gfxDevice, gfxProvider); diff --git a/Samples/Experimental/ImGui/main.cpp b/Samples/Experimental/ImGui/main.cpp index 39e9ca6fe..b12f2dc08 100644 --- a/Samples/Experimental/ImGui/main.cpp +++ b/Samples/Experimental/ImGui/main.cpp @@ -218,8 +218,9 @@ RenderingData SetupTriangleSample(std::shared_ptrBeginFrame(); - auto cmdList = gfxContext->CreateCmdList(); + auto transferContext = gfxDevice->CreateTransferContext(); + transferContext->BeginFrame(); + auto cmdList = transferContext->CreateCmdList(); cmdList->Begin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); @@ -229,7 +230,7 @@ RenderingData SetupTriangleSample(std::shared_ptrCmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); cmdList->End(); - gfxContext->EndFrame(); + transferContext->EndFrame(); std::vector>> lists{cmdList}; gfxDevice->QueueSubmit(lists); gfxDevice->WaitForIdle(); @@ -277,7 +278,8 @@ void Render(RenderingData& renderingData, context->BeginFrame(); // Setup + begin command list - auto currentCmdList = context->CreateCmdList(); + auto transferContext = gfxDevice->CreateTransferContext(); + auto currentCmdList = transferContext->CreateCmdList(); // currentCmdList->Begin(); // Track new frame rendering @@ -480,7 +482,7 @@ int main() }; // Upload Fonts - auto gfxContext = gfxDevice->CreateGraphicsContext(); + auto gfxContext = gfxDevice->CreateTransferContext(); auto cmdList = gfxContext->CreateCmdList(); uiProvider->UploadFontData(cmdList); diff --git a/Samples/Experimental/VulkanRender/main.cpp b/Samples/Experimental/VulkanRender/main.cpp index d97c8f6d1..2636c7bc2 100644 --- a/Samples/Experimental/VulkanRender/main.cpp +++ b/Samples/Experimental/VulkanRender/main.cpp @@ -194,8 +194,9 @@ RenderingData SetupTriangleSample(std::shared_ptrBeginFrame(); - auto cmdList = gfxContext->CreateCmdList(); + auto transferContext = gfxDevice->CreateTransferContext(); + transferContext->BeginFrame(); + auto cmdList = transferContext->CreateCmdList(); cmdList->Begin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); @@ -205,7 +206,7 @@ RenderingData SetupTriangleSample(std::shared_ptrCmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); cmdList->End(); - gfxContext->EndFrame(); + transferContext->EndFrame(); std::vector>> lists{cmdList}; gfxDevice->QueueSubmit(lists); gfxDevice->WaitForIdle(); From 195dc8808026c0d1bc451f466ec85b2677768210 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Tue, 24 Nov 2026 23:52:48 +0000 Subject: [PATCH 3/4] Fix some overzealous synchronisation. Fix memory barriers AGAIN. I think this covers everything. --- .../Ecs/Graphics/RenderOrchestratorSystem.hpp | 13 +++- .../Ecs/Graphics/SpriteRendererSystem.hpp | 6 ++ .../NovelRT/Graphics/GraphicsCmdList.hpp | 12 ++- .../Graphics/GraphicsPipelineVisibility.hpp | 12 ++- Graphics/Vulkan/VulkanGraphicsCmdList.cpp | 77 +++++++------------ .../Vulkan/Utilities/MemoryAccessMode.hpp | 60 ++++++++------- .../Vulkan/Utilities/PipelineVisibility.hpp | 46 ++++++++--- .../Graphics/Vulkan/VulkanGraphicsCmdList.hpp | 12 ++- Samples/Experimental/ImGui/main.cpp | 8 +- Samples/Experimental/VulkanRender/main.cpp | 8 +- .../NovelRT/UI/ImGui/ImGuiUIProvider.hpp | 18 ++--- 11 files changed, 155 insertions(+), 117 deletions(-) diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp index 937228a61..325ec3147 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/RenderOrchestratorSystem.hpp @@ -165,13 +165,18 @@ namespace NovelRT::Ecs::Graphics colourDataStruct.depth = 0; colourDataStruct.stencil = 0; - cmdList->CmdTransitionTextureTo(image, NovelRT::Graphics::ImageLayout::Undefined, - NovelRT::Graphics::ImageLayout::DstTransfer); + using Access = NovelRT::Graphics::GraphicsMemoryAccessMode; + using Stage = NovelRT::Graphics::GraphicsPipelineVisibility; + using Layout = NovelRT::Graphics::ImageLayout; + + cmdList->CmdTransitionTextureTo(image, Layout::Undefined, Layout::DstTransfer, Access::None, + Access::TransferWrite, Stage::Transfer, Stage::Transfer); cmdList->CmdClearColourSwapchainImage(image, colourDataStruct); - cmdList->CmdTransitionTextureTo(image, NovelRT::Graphics::ImageLayout::DstTransfer, - NovelRT::Graphics::ImageLayout::Present); + cmdList->CmdTransitionTextureTo(image, Layout::DstTransfer, Layout::Present, Access::TransferWrite, + Access::ColorAttachmentRead | Access::ColorAttachmentWrite, Stage::Transfer, + Stage::ColorAttachmentOutput); for (auto& root : roots) { EnumerateChildren(root, renderPasses, ordered); diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp index 837cff33a..b1ab79ba4 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp @@ -212,6 +212,12 @@ namespace NovelRT::Ecs::Graphics .size = totalSize}; auto textureStagingBuffer = _memoryAllocator->CreateBuffer(bufferCreateInfo); + + // this was getting unreadable. - Matt J. + using Access = NovelRT::Graphics::GraphicsMemoryAccessMode; + using Stage = NovelRT::Graphics::GraphicsPipelineVisibility; + using Layout = NovelRT::Graphics::ImageLayout; + auto gfxContext = _graphicsDevice->CreateTransferContext(); auto uploadCmdList = gfxContext->CreateCmdList(); diff --git a/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp b/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp index 9c62ef8dd..65b1fac4f 100644 --- a/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp +++ b/Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp @@ -134,11 +134,19 @@ namespace NovelRT::Graphics void CmdTransitionTextureTo(const std::shared_ptr>& swapchainImage, ImageLayout oldLayout, - ImageLayout newLayout); + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage); void CmdTransitionTextureTo(const std::shared_ptr>& texture, ImageLayout oldLayout, - ImageLayout newLayout); + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage); void CmdClearColourSwapchainImage(const std::shared_ptr>& swapchainImage, ClearValue clearData); diff --git a/Graphics/Core/include/NovelRT/Graphics/GraphicsPipelineVisibility.hpp b/Graphics/Core/include/NovelRT/Graphics/GraphicsPipelineVisibility.hpp index 8a1ec046a..1c0d10d5b 100644 --- a/Graphics/Core/include/NovelRT/Graphics/GraphicsPipelineVisibility.hpp +++ b/Graphics/Core/include/NovelRT/Graphics/GraphicsPipelineVisibility.hpp @@ -11,6 +11,12 @@ namespace NovelRT::Graphics { enum class GraphicsPipelineVisibility : uint32_t { + // IMPLEMENTATION DETAIL: + // All of these values match the Vulkan implementation, + // or at least, they should. The vulkan module has + // static asserts due to some trickery we're doing to + // take advantage of this. - Matt J. + None = 0, TopOfPipe = 1 << 0, DrawIndirect = 1 << 1, @@ -27,10 +33,8 @@ namespace NovelRT::Graphics Transfer = 1 << 12, BottomOfPipe = 1 << 13, Host = 1 << 14, - AllGraphics = DrawIndirect | VertexInput | VertexShader | TesselationControlShader | - TesselationEvaluationShader | GeometryShader | FragmentShader | EarlyFragmentTests | - LateFragmentTests | ColorAttachmentOutput, - AllCommands = AllGraphics | TopOfPipe | Transfer | BottomOfPipe | Host, + AllGraphics = 1u << 15, + AllCommands = 1u << 16, All = AllCommands }; } diff --git a/Graphics/Vulkan/VulkanGraphicsCmdList.cpp b/Graphics/Vulkan/VulkanGraphicsCmdList.cpp index b7b22c019..63f7c761a 100644 --- a/Graphics/Vulkan/VulkanGraphicsCmdList.cpp +++ b/Graphics/Vulkan/VulkanGraphicsCmdList.cpp @@ -42,40 +42,6 @@ namespace NovelRT::Graphics using VulkanGraphicsResourceMemoryRegion = GraphicsResourceMemoryRegion; using VulkanGraphicsTexture = GraphicsTexture; - [[nodiscard]] std::pair GetAccessAndStageMasksForLayout(VkImageLayout layout, - bool isSource) - { - VkAccessFlags accessMask = 0; - VkPipelineStageFlags stageMask = 0; - - switch (layout) - { - case VK_IMAGE_LAYOUT_GENERAL: - accessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; - stageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; - break; - case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: - accessMask = isSource ? VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT : 0; - stageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; - break; - case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: - accessMask = VK_ACCESS_TRANSFER_READ_BIT; - stageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; - break; - case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: - accessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - stageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; - break; - case VK_IMAGE_LAYOUT_UNDEFINED: - default: - accessMask = 0; - stageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - break; - } - - return {accessMask, stageMask}; - } - VulkanGraphicsCmdList::GraphicsCmdList( std::shared_ptr> device, VkCommandBuffer commandBuffer, @@ -531,6 +497,9 @@ namespace NovelRT::Graphics GraphicsPipelineVisibility sourceStageFlag, GraphicsPipelineVisibility destinationStageFlag) { + const auto srcStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(sourceStageFlag); + const auto dstStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(destinationStageFlag); + VkBufferMemoryBarrier barrierInfo{.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, .pNext = nullptr, .srcAccessMask = Vulkan::Utilities::GetVulkanAccessFlags(sourceAccessFlag), @@ -545,18 +514,16 @@ namespace NovelRT::Graphics if (_commandBuffer == VK_NULL_HANDLE) { _replayData.emplace_back( - [sourceStageFlag, destinationStageFlag, barrierInfo](VkCommandBuffer cmdBuffer) + [srcStageMask, dstStageMask, barrierInfo](VkCommandBuffer cmdBuffer) { - vkCmdPipelineBarrier(cmdBuffer, Vulkan::Utilities::GetVulkanPipelineStageFlags(sourceStageFlag), - Vulkan::Utilities::GetVulkanPipelineStageFlags(destinationStageFlag), 0, 0, - nullptr, 1, &barrierInfo, 0, nullptr); + vkCmdPipelineBarrier(cmdBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 1, &barrierInfo, 0, + nullptr); }); } else { - vkCmdPipelineBarrier(_commandBuffer, Vulkan::Utilities::GetVulkanPipelineStageFlags(sourceStageFlag), - Vulkan::Utilities::GetVulkanPipelineStageFlags(destinationStageFlag), 0, 0, nullptr, 1, - &barrierInfo, 0, nullptr); + vkCmdPipelineBarrier(_commandBuffer, srcStageMask, dstStageMask, 0, 0, nullptr, 1, &barrierInfo, 0, + nullptr); } } @@ -591,7 +558,11 @@ namespace NovelRT::Graphics void VulkanGraphicsCmdList::CmdTransitionTextureTo( const std::shared_ptr>& swapchainImage, ImageLayout oldLayout, - ImageLayout newLayout) + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage) { VkImageLayout vkOldLayout = Vulkan::GetVulkanImageLayout(oldLayout); VkImageLayout vkNewLayout = Vulkan::GetVulkanImageLayout(newLayout); @@ -601,13 +572,13 @@ namespace NovelRT::Graphics subresourceRange.levelCount = 1; subresourceRange.layerCount = 1; - const auto [srcAccessMask, srcStageMask] = GetAccessAndStageMasksForLayout(vkOldLayout, true); - const auto [dstAccessMask, dstStageMask] = GetAccessAndStageMasksForLayout(vkNewLayout, false); + const auto srcStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(sourceStage); + const auto dstStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(destinationStage); VkImageMemoryBarrier barrier{}; barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - barrier.srcAccessMask = srcAccessMask; - barrier.dstAccessMask = dstAccessMask; + barrier.srcAccessMask = Vulkan::Utilities::GetVulkanAccessFlags(sourceAccess); + barrier.dstAccessMask = Vulkan::Utilities::GetVulkanAccessFlags(destinationAccess); barrier.oldLayout = vkOldLayout; barrier.newLayout = vkNewLayout; barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; @@ -632,7 +603,11 @@ namespace NovelRT::Graphics void VulkanGraphicsCmdList::CmdTransitionTextureTo( const std::shared_ptr>& texture, ImageLayout oldLayout, - ImageLayout newLayout) + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage) { VkImageLayout vkOldLayout = Vulkan::GetVulkanImageLayout(oldLayout); VkImageLayout vkNewLayout = Vulkan::GetVulkanImageLayout(newLayout); @@ -642,13 +617,13 @@ namespace NovelRT::Graphics subresourceRange.levelCount = 1; subresourceRange.layerCount = 1; - const auto [srcAccessMask, srcStageMask] = GetAccessAndStageMasksForLayout(vkOldLayout, true); - const auto [dstAccessMask, dstStageMask] = GetAccessAndStageMasksForLayout(vkNewLayout, false); + const auto srcStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(sourceStage); + const auto dstStageMask = Vulkan::Utilities::GetVulkanPipelineStageFlags(destinationStage); VkImageMemoryBarrier barrier{}; barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - barrier.srcAccessMask = srcAccessMask; - barrier.dstAccessMask = dstAccessMask; + barrier.srcAccessMask = Vulkan::Utilities::GetVulkanAccessFlags(sourceAccess); + barrier.dstAccessMask = Vulkan::Utilities::GetVulkanAccessFlags(destinationAccess); barrier.oldLayout = vkOldLayout; barrier.newLayout = vkNewLayout; barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/MemoryAccessMode.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/MemoryAccessMode.hpp index 955830e8c..077835c75 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/MemoryAccessMode.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/MemoryAccessMode.hpp @@ -4,40 +4,44 @@ // for more information. #include -#include +#include +#include +#include #include namespace NovelRT::Graphics::Vulkan::Utilities { [[nodiscard]] inline VkAccessFlags GetVulkanAccessFlags(Graphics::GraphicsMemoryAccessMode accessFlag) noexcept { - VkAccessFlags accessFlags = VK_ACCESS_NONE; - - if ((accessFlag & Graphics::GraphicsMemoryAccessMode::IndexRead) == - Graphics::GraphicsMemoryAccessMode::IndexRead) - { - accessFlags |= VK_ACCESS_INDEX_READ_BIT; - } - - if ((accessFlag & Graphics::GraphicsMemoryAccessMode::VertexAttributeRead) == - Graphics::GraphicsMemoryAccessMode::VertexAttributeRead) - { - accessFlags |= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; - } - - if ((accessFlag & Graphics::GraphicsMemoryAccessMode::TransferRead) == - Graphics::GraphicsMemoryAccessMode::TransferRead) - { - accessFlags |= VK_ACCESS_TRANSFER_READ_BIT; - } - - if ((accessFlag & Graphics::GraphicsMemoryAccessMode::TransferWrite) == - Graphics::GraphicsMemoryAccessMode::TransferWrite) - { - accessFlags |= VK_ACCESS_TRANSFER_WRITE_BIT; - } - - return accessFlags; + using Access = Graphics::GraphicsMemoryAccessMode; + + static constexpr auto accessFlags = std::to_array>({ + {Access::None, 0}, + {Access::IndirectCommandRead, VK_ACCESS_INDIRECT_COMMAND_READ_BIT}, + {Access::IndexRead, VK_ACCESS_INDEX_READ_BIT}, + {Access::VertexAttributeRead, VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT}, + {Access::UniformRead, VK_ACCESS_UNIFORM_READ_BIT}, + {Access::InputAttachmentRead, VK_ACCESS_INPUT_ATTACHMENT_READ_BIT}, + {Access::ShaderRead, VK_ACCESS_SHADER_READ_BIT}, + {Access::ShaderWrite, VK_ACCESS_SHADER_WRITE_BIT}, + {Access::ColorAttachmentRead, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT}, + {Access::ColorAttachmentWrite, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT}, + {Access::DepthStencilAttachmentRead, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT}, + {Access::DepthStencilAttachmentWrite, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT}, + {Access::TransferRead, VK_ACCESS_TRANSFER_READ_BIT}, + {Access::TransferWrite, VK_ACCESS_TRANSFER_WRITE_BIT}, + {Access::HostRead, VK_ACCESS_HOST_READ_BIT}, + {Access::HostWrite, VK_ACCESS_HOST_WRITE_BIT}, + {Access::MemoryRead, VK_ACCESS_MEMORY_READ_BIT}, + {Access::MemoryWrite, VK_ACCESS_MEMORY_WRITE_BIT}, + }); + + static_assert(std::ranges::all_of(accessFlags, + [](const auto& flag) + { return static_cast(flag.first) == flag.second; }), + "Graphics memory access modes must match Vulkan access flags."); + + return static_cast(accessFlag); } } diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/PipelineVisibility.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/PipelineVisibility.hpp index 74aafa05d..0a0bdfa19 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/PipelineVisibility.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/Utilities/PipelineVisibility.hpp @@ -3,30 +3,52 @@ // Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root // for more information. +#include #include -#include +#include +#include +#include #include namespace NovelRT::Graphics::Vulkan::Utilities { [[nodiscard]] inline VkPipelineStageFlags GetVulkanPipelineStageFlags( - Graphics::GraphicsPipelineVisibility stageFlag) noexcept + Graphics::GraphicsPipelineVisibility stageFlag) { - VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_NONE; + using Stage = Graphics::GraphicsPipelineVisibility; - if ((stageFlag & Graphics::GraphicsPipelineVisibility::VertexInput) == - Graphics::GraphicsPipelineVisibility::VertexInput) - { - stageFlags |= VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; - } + static constexpr auto stageFlags = std::to_array>({ + {Stage::None, 0}, + {Stage::TopOfPipe, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT}, + {Stage::DrawIndirect, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT}, + {Stage::VertexInput, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT}, + {Stage::VertexShader, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT}, + {Stage::TesselationControlShader, VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT}, + {Stage::TesselationEvaluationShader, VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT}, + {Stage::GeometryShader, VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT}, + {Stage::FragmentShader, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT}, + {Stage::EarlyFragmentTests, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT}, + {Stage::LateFragmentTests, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT}, + {Stage::ColorAttachmentOutput, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}, + {Stage::ComputeShader, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT}, + {Stage::Transfer, VK_PIPELINE_STAGE_TRANSFER_BIT}, + {Stage::BottomOfPipe, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT}, + {Stage::Host, VK_PIPELINE_STAGE_HOST_BIT}, + {Stage::AllGraphics, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT}, + {Stage::AllCommands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT}, + }); + + static_assert(std::ranges::all_of(stageFlags, + [](const auto& flag) + { return static_cast(flag.first) == flag.second; }), + "Graphics pipeline stages must match Vulkan pipeline stage flags."); - if ((stageFlag & Graphics::GraphicsPipelineVisibility::Transfer) == - Graphics::GraphicsPipelineVisibility::Transfer) + if (stageFlag == Stage::None) { - stageFlags |= VK_PIPELINE_STAGE_TRANSFER_BIT; + throw Exceptions::NotSupportedException("Vulkan pipeline barrier stage flags cannot be None."); } - return stageFlags; + return static_cast(stageFlag); } } diff --git a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp index 218290e83..7a4872ca8 100644 --- a/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp +++ b/Graphics/Vulkan/include/NovelRT/Graphics/Vulkan/VulkanGraphicsCmdList.hpp @@ -114,11 +114,19 @@ namespace NovelRT::Graphics void CmdTransitionTextureTo( const std::shared_ptr>& swapchainImage, ImageLayout oldLayout, - ImageLayout newLayout); + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage); void CmdTransitionTextureTo(const std::shared_ptr>& texture, ImageLayout oldLayout, - ImageLayout newLayout); + ImageLayout newLayout, + GraphicsMemoryAccessMode sourceAccess, + GraphicsMemoryAccessMode destinationAccess, + GraphicsPipelineVisibility sourceStage, + GraphicsPipelineVisibility destinationStage); void CmdClearColourSwapchainImage( const std::shared_ptr>& swapchainImage, diff --git a/Samples/Experimental/ImGui/main.cpp b/Samples/Experimental/ImGui/main.cpp index b12f2dc08..af5b0d786 100644 --- a/Samples/Experimental/ImGui/main.cpp +++ b/Samples/Experimental/ImGui/main.cpp @@ -225,9 +225,13 @@ RenderingData SetupTriangleSample(std::shared_ptrBegin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer, + GraphicsMemoryAccessMode::None, GraphicsMemoryAccessMode::TransferWrite, + GraphicsPipelineVisibility::TopOfPipe, GraphicsPipelineVisibility::Transfer); cmdList->CmdCopy(texture2D, textureStagingBufferRegion); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal, + GraphicsMemoryAccessMode::TransferWrite, GraphicsMemoryAccessMode::None, + GraphicsPipelineVisibility::Transfer, GraphicsPipelineVisibility::BottomOfPipe); cmdList->End(); transferContext->EndFrame(); diff --git a/Samples/Experimental/VulkanRender/main.cpp b/Samples/Experimental/VulkanRender/main.cpp index 2636c7bc2..f926cbcdb 100644 --- a/Samples/Experimental/VulkanRender/main.cpp +++ b/Samples/Experimental/VulkanRender/main.cpp @@ -201,9 +201,13 @@ RenderingData SetupTriangleSample(std::shared_ptrBegin(); cmdList->CmdCopy(vertexBufferRegion, stagingBufferRegion); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer, + GraphicsMemoryAccessMode::None, GraphicsMemoryAccessMode::TransferWrite, + GraphicsPipelineVisibility::TopOfPipe, GraphicsPipelineVisibility::Transfer); cmdList->CmdCopy(texture2D, textureStagingBufferRegion); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal, + GraphicsMemoryAccessMode::TransferWrite, GraphicsMemoryAccessMode::None, + GraphicsPipelineVisibility::Transfer, GraphicsPipelineVisibility::BottomOfPipe); cmdList->End(); transferContext->EndFrame(); diff --git a/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp b/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp index 7cecf11dd..d589493a2 100644 --- a/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp +++ b/UI/include/NovelRT/UI/ImGui/ImGuiUIProvider.hpp @@ -427,14 +427,6 @@ namespace NovelRT::UI::ImGui currentCmdList->CmdCopy(vertexBufferRegion, vertexStageBufferRegion); currentCmdList->CmdCopy(indexBufferRegion, indexStageBufferRegion); - // Sync the commands so that we can prevent data issues - currentCmdList->CmdPipelineBufferBarrier( - vertexBuffer, GraphicsMemoryAccessMode::TransferWrite, GraphicsMemoryAccessMode::VertexAttributeRead, - GraphicsPipelineVisibility::Transfer, GraphicsPipelineVisibility::VertexInput); - currentCmdList->CmdPipelineBufferBarrier( - indexBuffer, GraphicsMemoryAccessMode::TransferWrite, GraphicsMemoryAccessMode::IndexRead, - GraphicsPipelineVisibility::Transfer, GraphicsPipelineVisibility::VertexInput); - _currentIndexBufferRegion = indexBufferRegion; _currentVertexBuffer = vertexBuffer; _currentVertexBufferRegion = vertexBufferRegion; @@ -639,9 +631,15 @@ namespace NovelRT::UI::ImGui // Send the data to GPU cmdList->Begin(); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::Undefined, ImageLayout::DstTransfer, + GraphicsMemoryAccessMode::None, GraphicsMemoryAccessMode::TransferWrite, + GraphicsPipelineVisibility::TopOfPipe, + GraphicsPipelineVisibility::Transfer); cmdList->CmdCopy(texture2D, stagingBufferRegion); - cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal); + cmdList->CmdTransitionTextureTo(texture2D, ImageLayout::DstTransfer, ImageLayout::ShaderReadOnlyOptimal, + GraphicsMemoryAccessMode::TransferWrite, GraphicsMemoryAccessMode::None, + GraphicsPipelineVisibility::Transfer, + GraphicsPipelineVisibility::BottomOfPipe); cmdList->End(); // End Frame From d693bd7449ca3c002471c7687d77074265e5f584 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Wed, 25 Nov 2026 00:24:57 +0000 Subject: [PATCH 4/4] Fix rebase oopsies. --- .../Ecs/Graphics/SpriteRendererSystem.hpp | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp index b1ab79ba4..fee41fa80 100644 --- a/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp +++ b/Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp @@ -149,10 +149,6 @@ namespace NovelRT::Ecs::Graphics void HandleNewSpriteTextures(Catalogue catalogue) { auto lastCompletedUpload = _uploadSemaphore->GetValue(); - if (lastCompletedUpload > 0) - { - lastCompletedUpload -= 1; - } _pendingStagingBuffers.erase(std::remove_if(_pendingStagingBuffers.begin(), _pendingStagingBuffers.end(), [lastCompletedUpload](const auto& pending) @@ -221,6 +217,9 @@ namespace NovelRT::Ecs::Graphics auto gfxContext = _graphicsDevice->CreateTransferContext(); auto uploadCmdList = gfxContext->CreateCmdList(); + std::vector stagingBufferRegions{}; + stagingBufferRegions.reserve(newTextures.size()); + uploadCmdList->Begin(); for (auto [texture, texture2D] : newTextures) @@ -235,15 +234,15 @@ namespace NovelRT::Ecs::Graphics textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion); - uploadCmdList->CmdTransitionTextureTo(texture2D, NovelRT::Graphics::ImageLayout::Undefined, - NovelRT::Graphics::ImageLayout::DstTransfer); + uploadCmdList->CmdTransitionTextureTo(texture2D, Layout::Undefined, Layout::DstTransfer, + Access::None, Access::TransferWrite, Stage::TopOfPipe, + Stage::Transfer); uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion); - uploadCmdList->CmdTransitionTextureTo( - texture2D, NovelRT::Graphics::ImageLayout::DstTransfer, - NovelRT::Graphics::ImageLayout::ShaderReadOnlyOptimal); + uploadCmdList->CmdTransitionTextureTo(texture2D, Layout::DstTransfer, + Layout::ShaderReadOnlyOptimal, Access::TransferWrite, + Access::None, Stage::Transfer, Stage::BottomOfPipe); - _pendingStagingBuffers.push_back({_submittedUploads, textureStagingBuffer, - textureStagingBufferRegion, gfxContext, uploadCmdList}); + stagingBufferRegions.push_back(textureStagingBufferRegion); _textureCache.emplace( texture.databaseHandle, TextureCacheItem{texture2DRegion, @@ -252,20 +251,27 @@ namespace NovelRT::Ecs::Graphics _pendingLoads.erase(texture.databaseHandle); } - std::vector>, - uint64_t>> - signalSemaphores{std::make_pair(_uploadSemaphore, ++_submittedUploads)}; - uploadCmdList->End(); std::vector>> list{ uploadCmdList}; + _pendingStagingBuffers.reserve(_pendingStagingBuffers.size() + stagingBufferRegions.size()); + const auto uploadIndex = ++_submittedUploads; + std::vector>, + uint64_t>> + signalSemaphores{std::make_pair(_uploadSemaphore, uploadIndex)}; _graphicsDevice->QueueSubmit(list, signalSemaphores); + for (const auto& region : stagingBufferRegions) + { + _pendingStagingBuffers.push_back( + {uploadIndex, textureStagingBuffer, region, gfxContext, uploadCmdList}); + } + Components::TrackedSemaphore newUploadTracker{ .semaphore = new std::shared_ptr>( _uploadSemaphore), - .signalValue = _submittedUploads}; + .signalValue = uploadIndex}; auto id = completionCatalogue.CreateEntity();