Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ namespace NovelRT::Ecs::Graphics

cmdList->CmdTransitionSwapchainImageTo(image, NovelRT::Graphics::ImageLayout::DstTransfer,
NovelRT::Graphics::ImageLayout::Present);
if (!passes.empty())
{
cmdList->CmdInitialSwapchainImageBarrierLegacyVersion(image);
}

for (auto& root : roots)
{
Expand Down
25 changes: 15 additions & 10 deletions Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@

#include <NovelRT/Utilities/Memory.hpp>

#include <cmath>
#include <mutex>
#include <tbb/mutex.h>

#include <optional>
#include <span>
#include <unordered_map>
Expand Down Expand Up @@ -138,6 +140,8 @@ namespace NovelRT::Ecs::Graphics

std::unordered_map<EntityId, BufferRegion> _cameraConstantBuffers;

std::unique_ptr<tbb::mutex> _textureUploadRegistrationMutex;

void HandleNewSpriteTextures(Catalogue catalogue)
{
auto lastCompletedUpload = _uploadSemaphore->GetValue();
Expand Down Expand Up @@ -169,6 +173,11 @@ namespace NovelRT::Ecs::Graphics
Timing::Timestamp /* now */, Timing::Timestamp::duration /* delta */,
Catalogue completionCatalogue, ResourceManagement::TextureMetadata textureMetadata)
{
// TODO: This is going to get handled far better once Karl's batching code is in. Need this
// for mitigating possible bugs with a race condition involving the timeline semaphore.
// Honestly, I can't believe I didn't realise this sooner. - Matt J.
std::lock_guard lock(*_textureUploadRegistrationMutex);

auto texture2D = _memoryAllocator->CreateTexture2DRepeatGpuWriteOnly(textureMetadata.width,
textureMetadata.height);

Expand All @@ -188,12 +197,12 @@ namespace NovelRT::Ecs::Graphics

textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion);

auto gfxContext = _graphicsDevice->CreateGraphicsContext();
auto gfxContext = _graphicsDevice->CreateGraphicsContext(true);
auto uploadCmdList = gfxContext->CreateCmdList();
uploadCmdList->Begin();
uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D);
uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion);
uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D);
uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D, true);
uploadCmdList->End();

std::vector<std::shared_ptr<NovelRT::Graphics::GraphicsCmdList<TGraphicsBackend>>> lists{
Expand Down Expand Up @@ -230,14 +239,9 @@ namespace NovelRT::Ecs::Graphics
{
auto lastSubmittedFrame = _orchestratorSubmissionSemaphore->GetValue();

if (lastSubmittedFrame > 0)
{
lastSubmittedFrame -= 1;
}

_pendingContexts.erase(std::remove_if(_pendingContexts.begin(), _pendingContexts.end(),
[lastSubmittedFrame](const auto& pending)
{ return pending.frameIndex <= lastSubmittedFrame; }),
{ return pending.frameIndex < lastSubmittedFrame; }),
_pendingContexts.end());

auto [renderPassView, cmdListView, spriteView, transformView, trackedSemaphoresView, cameraView,
Expand Down Expand Up @@ -442,7 +446,8 @@ namespace NovelRT::Ecs::Graphics
_renderPass(std::move(renderPass)),
_surfaceContext(std::move(surfaceContext)),
_uploadSemaphore(_graphicsDevice->CreateSemaphore(0)),
_orchestratorSubmissionSemaphore(_graphicsDevice->CreateSemaphore(0))
_orchestratorSubmissionSemaphore(_graphicsDevice->CreateSemaphore(0)),
_textureUploadRegistrationMutex(std::make_unique<tbb::mutex>())
{
NovelRT::Graphics::GraphicsBufferCreateInfo bufferCreateInfo{
.cpuAccessKind = NovelRT::Graphics::GraphicsResourceAccess::Write,
Expand Down
3 changes: 2 additions & 1 deletion Graphics/Core/include/NovelRT/Graphics/GraphicsCmdList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ namespace NovelRT::Graphics
void CmdSetViewport(ViewportInfo viewportInfo);

void CmdBeginTexturePipelineBarrierLegacyVersion(const std::shared_ptr<GraphicsTexture<TBackend>>& texture);
void CmdEndTexturePipelineBarrierLegacyVersion(const std::shared_ptr<GraphicsTexture<TBackend>>& texture);
void CmdEndTexturePipelineBarrierLegacyVersion(const std::shared_ptr<GraphicsTexture<TBackend>>& texture,
bool useTransferQueue = false);

void CmdInitialSwapchainImageBarrierLegacyVersion(
const std::shared_ptr<GraphicsSwapchainImage<TBackend>>& swapchainImage);
Expand Down
2 changes: 1 addition & 1 deletion Graphics/Core/include/NovelRT/Graphics/GraphicsDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace NovelRT::Graphics
ShaderProgramKind kind,
NovelRT::Utilities::Span<uint8_t> byteData);

[[nodiscard]] std::shared_ptr<GraphicsContext<TBackend>> CreateGraphicsContext();
[[nodiscard]] std::shared_ptr<GraphicsContext<TBackend>> CreateGraphicsContext(bool useTransferQueue = false);

[[nodiscard]] std::shared_ptr<GraphicsSwapchainImage<TBackend>> BeginFrame();
void EndFrame();
Expand Down
23 changes: 15 additions & 8 deletions Graphics/Vulkan/VulkanGraphicsCmdList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ namespace NovelRT::Graphics
vulkanImageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
vulkanImageMemoryBarrier.subresourceRange = subresourceRange;
vulkanImageMemoryBarrier.image = texture->GetVulkanImage();
vulkanImageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
vulkanImageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;

if (_commandBuffer == VK_NULL_HANDLE)
{
Expand All @@ -518,7 +520,8 @@ namespace NovelRT::Graphics
}

void VulkanGraphicsCmdList::CmdEndTexturePipelineBarrierLegacyVersion(
const std::shared_ptr<VulkanGraphicsTexture>& texture)
const std::shared_ptr<VulkanGraphicsTexture>& texture,
bool useTransferQueue)
{
VkImageSubresourceRange subresourceRange{};
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
Expand All @@ -528,26 +531,30 @@ namespace NovelRT::Graphics
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.dstAccessMask = useTransferQueue ? 0 : 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();
vulkanImageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
vulkanImageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;

const VkPipelineStageFlags dstStage =
useTransferQueue ? VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT : VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;

if (_commandBuffer == VK_NULL_HANDLE)
{
_replayData.emplace_back(
[vulkanImageMemoryBarrier](VkCommandBuffer cmdBuffer)
[vulkanImageMemoryBarrier, dstStage](VkCommandBuffer cmdBuffer)
{
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1,
&vulkanImageMemoryBarrier);
vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, dstStage, 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);
vkCmdPipelineBarrier(_commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, dstStage, 0, 0, nullptr, 0, nullptr, 1,
&vulkanImageMemoryBarrier);
}
}

Expand Down
13 changes: 9 additions & 4 deletions Graphics/Vulkan/VulkanGraphicsContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ namespace NovelRT::Graphics

auto device = GetDevice();

const auto& indicesData = device->GetIndicesData();

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 =
_useTransferQueue ? indicesData.transferFamily.value() : indicesData.graphicsFamily.value();

const VkResult result =
vkCreateCommandPool(device->GetVulkanDevice(), &commandPoolCreateInfo, nullptr, &vulkanCommandPool);
Expand Down Expand Up @@ -105,9 +108,11 @@ namespace NovelRT::Graphics
return std::static_pointer_cast<const VulkanGraphicsContext>(GraphicsDeviceObject::shared_from_this());
}

VulkanGraphicsContext::GraphicsContext(
std::shared_ptr<GraphicsDevice<Vulkan::VulkanGraphicsBackend>> device) noexcept
: _device(std::move(device)), _vulkanCommandPool([this]() { return CreateVulkanCommandPool(); })
VulkanGraphicsContext::GraphicsContext(std::shared_ptr<GraphicsDevice<Vulkan::VulkanGraphicsBackend>> device,
bool useTransferQueue) noexcept
: _device(std::move(device)),
_vulkanCommandPool([this]() { return CreateVulkanCommandPool(); }),
_useTransferQueue(useTransferQueue)
{
unused(_state.Transition(Threading::VolatileState::Initialised));
}
Expand Down
7 changes: 4 additions & 3 deletions Graphics/Vulkan/VulkanGraphicsDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ namespace NovelRT::Graphics
_indicesData = Vulkan::Utilities::FindQueueFamilies(_adapter->GetPhysicalDevice(), _surface);

std::vector<VkDeviceQueueCreateInfo> queueCreateInfos{};
std::set<uint32_t> uniqueQueueFamilies{_indicesData.graphicsFamily.value(), _indicesData.presentFamily.value()};
std::set<uint32_t> uniqueQueueFamilies{_indicesData.graphicsFamily.value(), _indicesData.presentFamily.value(),
_indicesData.transferFamily.value()};

float queuePriority = 1.0f;
for (uint32_t queueFamily : uniqueQueueFamilies)
Expand Down Expand Up @@ -347,9 +348,9 @@ namespace NovelRT::Graphics
inputs, resources, pushConstantRanges);
}

std::shared_ptr<VulkanGraphicsContext> VulkanGraphicsDevice::CreateGraphicsContext()
std::shared_ptr<VulkanGraphicsContext> VulkanGraphicsDevice::CreateGraphicsContext(bool useTransferQueue)
{
return std::make_shared<VulkanGraphicsContext>(shared_from_this());
return std::make_shared<VulkanGraphicsContext>(shared_from_this(), useTransferQueue);
}

std::shared_ptr<GraphicsSwapchainImage<Vulkan::VulkanGraphicsBackend>> VulkanGraphicsDevice::BeginFrame()
Expand Down
10 changes: 10 additions & 0 deletions Graphics/Vulkan/VulkanGraphicsMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ namespace NovelRT::Graphics
allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
// allocationCreateInfo.priority = 1; //TODO: do I need this?

const auto& indices = _device->GetIndicesData();
std::array<uint32_t, 2> queueFamilies = {indices.graphicsFamily.value(), indices.transferFamily.value()};

if (queueFamilies[0] != queueFamilies[1])
{
imageCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT;
imageCreateInfo.queueFamilyIndexCount = static_cast<uint32_t>(queueFamilies.size());
imageCreateInfo.pQueueFamilyIndices = queueFamilies.data();
}

VkResult result = vmaCreateImage(GetVmaAllocator(), &imageCreateInfo, &allocationCreateInfo, &vulkanImage,
&outAllocation, &outAllocationInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ namespace NovelRT::Graphics
void CmdBeginTexturePipelineBarrierLegacyVersion(
const std::shared_ptr<GraphicsTexture<Vulkan::VulkanGraphicsBackend>>& texture);
void CmdEndTexturePipelineBarrierLegacyVersion(
const std::shared_ptr<GraphicsTexture<Vulkan::VulkanGraphicsBackend>>& texture);
const std::shared_ptr<GraphicsTexture<Vulkan::VulkanGraphicsBackend>>& texture,
bool useTransferQueue = false);

void CmdInitialSwapchainImageBarrierLegacyVersion(
const std::shared_ptr<GraphicsSwapchainImage<Vulkan::VulkanGraphicsBackend>>& swapchainImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace NovelRT::Graphics

mutable NovelRT::Utilities::Lazy<VkCommandPool> _vulkanCommandPool;

bool _useTransferQueue;

Threading::VolatileState _state;

VkCommandPool CreateVulkanCommandPool() const;
Expand All @@ -41,7 +43,8 @@ namespace NovelRT::Graphics
// NOLINTNEXTLINE(readability-identifier-naming) - stdlib compatibility
std::shared_ptr<const GraphicsContext<Vulkan::VulkanGraphicsBackend>> shared_from_this() const;

GraphicsContext(std::shared_ptr<GraphicsDevice<Vulkan::VulkanGraphicsBackend>> device) noexcept;
GraphicsContext(std::shared_ptr<GraphicsDevice<Vulkan::VulkanGraphicsBackend>> device,
bool useTransferQueue) noexcept;
~GraphicsContext() final;

[[nodiscard]] std::shared_ptr<GraphicsDevice<Vulkan::VulkanGraphicsBackend>> GetDevice() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ namespace NovelRT::Graphics
ShaderProgramKind kind,
NovelRT::Utilities::Span<uint8_t> byteData);

[[nodiscard]] std::shared_ptr<GraphicsContext<Vulkan::VulkanGraphicsBackend>> CreateGraphicsContext();
[[nodiscard]] std::shared_ptr<GraphicsContext<Vulkan::VulkanGraphicsBackend>> CreateGraphicsContext(
bool useTransferQueue = false);

[[nodiscard]] std::shared_ptr<GraphicsSwapchainImage<Vulkan::VulkanGraphicsBackend>> BeginFrame();
void EndFrame();
Expand Down
Loading