Skip to content

Commit

Permalink
Better submission order
Browse files Browse the repository at this point in the history
Fixed a bunch of compute queue swap issues.
Renamed main framescript pass to Scene.
Submissions are now sent in batches rather than per queue.
Semaphores have debug names for easier tracking and debugging.
  • Loading branch information
Duttenheim committed Aug 14, 2024
1 parent f7f9cd0 commit 9c96720
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 137 deletions.
2 changes: 1 addition & 1 deletion code/addons/graphicsfeature/graphicsfeatureunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ GraphicsFeatureUnit::OnActivate()

Graphics::SetupBufferConstants();
this->gfxServer->AddPreViewCall([](IndexT frameIndex, IndexT bufferIndex) {
static auto lastFrameSubmission = FrameScript_default::Submission_ForwardShadingandPostEffects;
static auto lastFrameSubmission = FrameScript_default::Submission_Scene;
FrameScript_shadows::Run(Math::rectangle<int>(0, 0, 1024, 1024), frameIndex, bufferIndex);
FrameScript_default::Bind_LastFrame(lastFrameSubmission);
FrameScript_default::Bind_Shadows(FrameScript_shadows::Submission_Shadows);
Expand Down
10 changes: 7 additions & 3 deletions code/render/coregraphics/graphicsdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ uint64 NextSubmissionIndex(const CoreGraphics::QueueType queue);
bool PollSubmissionIndex(const CoreGraphics::QueueType queue, uint64 index);

/// Submit a command buffer, but doesn't necessarily execute it immediately
SubmissionWaitEvent SubmitCommandBuffer(const CoreGraphics::CmdBufferId cmds, CoreGraphics::QueueType type);
SubmissionWaitEvent SubmitCommandBuffer(
const CoreGraphics::CmdBufferId cmds
, CoreGraphics::QueueType type
#if NEBULA_GRAPHICS_DEBUG
, const char* name = nullptr
#endif
);
/// Wait for a submission
void WaitForSubmission(SubmissionWaitEvent index, CoreGraphics::QueueType type);
/// Have a queue wait for another queue
void WaitForLastSubmission(CoreGraphics::QueueType type, CoreGraphics::QueueType waitType);

/// Unlock constants
void UnlockConstantUpdates();
Expand Down
5 changes: 4 additions & 1 deletion code/render/coregraphics/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ enum SemaphoreType

struct SemaphoreCreateInfo
{
#if NEBULA_GRAPHICS_DEBUG
const char* name = nullptr;
#endif
SemaphoreType type;
};

Expand All @@ -47,4 +50,4 @@ void SemaphoreReset(const SemaphoreId& semaphore);

} // namespace CoreGraphics

#pragma pop_macro("CreateSemaphore")
#pragma pop_macro("CreateSemaphore")
1 change: 1 addition & 0 deletions code/render/coregraphics/swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct SwapchainCreateInfo
CoreGraphics::DisplayMode displayMode;
bool vsync;
GLFWwindow* window;
CoreGraphics::QueueType preferredQueue = CoreGraphics::ComputeQueueType;
};

/// Create swapchain
Expand Down
66 changes: 37 additions & 29 deletions code/render/coregraphics/vk/vkgraphicsdevice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ struct GraphicsDeviceState : CoreGraphics::GraphicsDeviceState
Util::FixedArray<PendingDeletes> pendingDeletes;
Util::Array<PendingDeletes> deletesPerSubmission;
Util::FixedArray<Util::Array<CoreGraphics::SubmissionWaitEvent>> waitEvents;
CoreGraphics::SubmissionWaitEvent mostRecentEvents[CoreGraphics::QueueType::NumQueueTypes];

struct PendingMarkers
{
Expand Down Expand Up @@ -163,7 +162,6 @@ VkDebugUtilsMessengerEXT VkErrorDebugMessageHandle = nullptr;
PFN_vkCreateDebugUtilsMessengerEXT VkCreateDebugMessenger = nullptr;
PFN_vkDestroyDebugUtilsMessengerEXT VkDestroyDebugMessenger = nullptr;

PFN_vkSetDebugUtilsObjectNameEXT VkDebugObjectName = nullptr;
PFN_vkSetDebugUtilsObjectTagEXT VkDebugObjectTag = nullptr;
PFN_vkQueueBeginDebugUtilsLabelEXT VkQueueBeginLabel = nullptr;
PFN_vkQueueEndDebugUtilsLabelEXT VkQueueEndLabel = nullptr;
Expand Down Expand Up @@ -524,7 +522,6 @@ SparseTextureBind(const VkImage img, const Util::Array<VkSparseMemoryBind>& opaq

// Set wait events in graphics device
state.waitEvents[state.currentBufferedFrameIndex].Append(sparseWait);
state.mostRecentEvents[sparseWait.queue] = sparseWait;

state.sparseImageBinds.Append(GraphicsDeviceState::SparseImageBind{.img = img, .opaqueBinds = opaqueBinds, .pageBinds = pageBinds});
//state.queueHandler.AppendSparseBind(CoreGraphics::SparseQueueType, img, opaqueBinds, pageBinds);
Expand Down Expand Up @@ -1139,7 +1136,11 @@ CreateGraphicsDevice(const GraphicsDeviceCreateInfo& info)
for (i = 0; i < info.numBufferedFrames; i++)
{
state.presentFences[i] = CreateFence({true});
state.renderingFinishedSemaphores[i] = CreateSemaphore({ SemaphoreType::Binary });
state.renderingFinishedSemaphores[i] = CreateSemaphore({
#if NEBULA_GRAPHICS_DEBUG
.name = "Present",
#endif
.type = SemaphoreType::Binary });
}

#pragma pop_macro("CreateSemaphore")
Expand Down Expand Up @@ -1579,25 +1580,29 @@ void
AddSubmissionEvent(const CoreGraphics::SubmissionWaitEvent& event)
{
state.waitEvents[state.currentBufferedFrameIndex].Append(event);
state.mostRecentEvents[event.queue] = event;
}

//------------------------------------------------------------------------------
/**
*/
CoreGraphics::SubmissionWaitEvent handoverWait, graphicsWait, uploadWait;
CoreGraphics::SubmissionWaitEvent
SubmitCommandBuffer(const CoreGraphics::CmdBufferId cmds, CoreGraphics::QueueType type)
SubmitCommandBuffer(
const CoreGraphics::CmdBufferId cmds
, CoreGraphics::QueueType type
#if NEBULA_GRAPHICS_DEBUG
, const char* name
#endif
)
{
// Submit transfer and graphics commands from this frame
CoreGraphics::SubmissionWaitEvent handoverWait, graphicsWait;
transferLock.Enter();
if (state.setupTransferCommandBuffer != CoreGraphics::InvalidCmdBufferId)
{
CmdBufferIdAcquire(state.setupTransferCommandBuffer);
CmdEndMarker(state.setupTransferCommandBuffer);
CmdEndRecord(state.setupTransferCommandBuffer);
CoreGraphics::SubmissionWaitEvent uploadWait;
uploadWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::TransferQueueType, CmdBufferGetVk(state.setupTransferCommandBuffer));
uploadWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::TransferQueueType, CmdBufferGetVk(state.setupTransferCommandBuffer), "Transfer");
uploadWait.queue = CoreGraphics::TransferQueueType;

// Set wait events in graphics device
Expand All @@ -1616,7 +1621,7 @@ SubmitCommandBuffer(const CoreGraphics::CmdBufferId cmds, CoreGraphics::QueueTyp
CmdBufferIdAcquire(state.handoverTransferCommandBuffer);
CmdEndMarker(state.handoverTransferCommandBuffer);
CmdEndRecord(state.handoverTransferCommandBuffer);
handoverWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::TransferQueueType, CmdBufferGetVk(state.handoverTransferCommandBuffer));
handoverWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::TransferQueueType, CmdBufferGetVk(state.handoverTransferCommandBuffer), "Handover");
handoverWait.queue = CoreGraphics::TransferQueueType;

// Set wait events in graphics device
Expand All @@ -1638,12 +1643,12 @@ SubmitCommandBuffer(const CoreGraphics::CmdBufferId cmds, CoreGraphics::QueueTyp
CmdEndMarker(state.setupGraphicsCommandBuffer);
CmdEndRecord(state.setupGraphicsCommandBuffer);

graphicsWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::GraphicsQueueType, CmdBufferGetVk(state.setupGraphicsCommandBuffer));
graphicsWait.timelineIndex = state.queueHandler.AppendSubmissionTimeline(CoreGraphics::GraphicsQueueType, CmdBufferGetVk(state.setupGraphicsCommandBuffer), "Setup");
graphicsWait.queue = CoreGraphics::GraphicsQueueType;

// This command buffer will have handover commands, so wait for that transfer
if (handoverWait != nullptr)
state.queueHandler.AppendWaitTimeline(handoverWait.timelineIndex, CoreGraphics::GraphicsQueueType, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, CoreGraphics::TransferQueueType);
state.queueHandler.AppendWaitTimeline(handoverWait.timelineIndex, CoreGraphics::GraphicsQueueType, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, handoverWait.queue);

// Add wait event
AddSubmissionEvent(graphicsWait);
Expand All @@ -1659,8 +1664,18 @@ SubmitCommandBuffer(const CoreGraphics::CmdBufferId cmds, CoreGraphics::QueueTyp

// Append submission
CoreGraphics::SubmissionWaitEvent ret;
ret.timelineIndex = state.queueHandler.AppendSubmissionTimeline(type, CmdBufferGetVk(cmds));
ret.timelineIndex = state.queueHandler.AppendSubmissionTimeline(
type
, CmdBufferGetVk(cmds)
#if NEBULA_GRAPHICS_DEBUG
, name
#endif
);
ret.queue = type;
//if (uploadWait != nullptr)
// state.queueHandler.AppendWaitTimeline(uploadWait.timelineIndex, type, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, uploadWait.queue);
//if (graphicsWait != nullptr)
// state.queueHandler.AppendWaitTimeline(graphicsWait.timelineIndex, type, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, graphicsWait.queue);

// Add wait event
AddSubmissionEvent(ret);
Expand All @@ -1680,15 +1695,6 @@ WaitForSubmission(SubmissionWaitEvent index, CoreGraphics::QueueType type)
state.queueHandler.AppendWaitTimeline(index.timelineIndex, type, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, index.queue);
}

//------------------------------------------------------------------------------
/**
*/
void
WaitForLastSubmission(CoreGraphics::QueueType type, CoreGraphics::QueueType waitType)
{
state.queueHandler.AppendWaitTimeline(state.mostRecentEvents[waitType].timelineIndex, type, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, waitType);
}

//------------------------------------------------------------------------------
/**
*/
Expand Down Expand Up @@ -1778,20 +1784,22 @@ FinishFrame(IndexT frameIndex)
state.currentFrameIndex = frameIndex;
}

// Flush all pending submissions on the queues
state.queueHandler.FlushSparseBinds(nullptr);
//state.queueHandler.FlushSubmissionsTimeline(CoreGraphics::SparseQueueType, nullptr);
state.queueHandler.FlushSubmissionsTimeline(CoreGraphics::TransferQueueType, nullptr);
state.queueHandler.FlushSubmissionsTimeline(CoreGraphics::ComputeQueueType, nullptr);
uploadWait = SubmissionWaitEvent();
graphicsWait = SubmissionWaitEvent();

// Flush all pending submissions on the queues
//state.queueHandler.FlushSparseBinds(nullptr);

// Signal rendering finished semaphore just before submitting graphics queue
state.queueHandler.AppendPresentSignal(
GraphicsQueueType,
ComputeQueueType,
SemaphoreGetVk(state.renderingFinishedSemaphores[state.currentBufferedFrameIndex])
);

state.queueHandler.FlushSubmissions(nullptr);

// Flush graphics (main)
state.queueHandler.FlushSubmissionsTimeline(CoreGraphics::GraphicsQueueType, nullptr);
//state.queueHandler.FlushSubmissionsTimeline(CoreGraphics::GraphicsQueueType, nullptr);

if (!state.sparseBufferBinds.IsEmpty() || !state.sparseImageBinds.IsEmpty())
{
Expand Down
2 changes: 1 addition & 1 deletion code/render/coregraphics/vk/vkloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#endif
namespace Vulkan
{

PFN_vkSetDebugUtilsObjectNameEXT VkDebugObjectName = nullptr;
//------------------------------------------------------------------------------
/**
*/
Expand Down
5 changes: 5 additions & 0 deletions code/render/coregraphics/vk/vkloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ extern void InitVulkan();
/// initialize Vulkan instance, loads function pointers directly from driver
extern void InitInstance(VkInstance instance);

extern PFN_vkSetDebugUtilsObjectNameEXT VkDebugObjectName;


} // namespace Vulkan

#define _IMP_VK(name) name = (PFN_##name)vkGetInstanceProcAddr(instance, #name);n_assert_fmt(name != nullptr, "Unable to get function proc: %s\n",#name);
#define _DEC_VK(name) extern PFN_##name name;
#define _DEF_VK(name) PFN_##name name;


#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -217,6 +221,7 @@ _DEC_VK(vkGetRayTracingShaderGroupHandlesKHR);
// mesh shaders
_DEC_VK(vkCmdDrawMeshTasksEXT);


#ifdef __cplusplus
}
#endif
2 changes: 0 additions & 2 deletions code/render/coregraphics/vk/vkresourcetable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
namespace Vulkan
{

extern PFN_vkSetDebugUtilsObjectNameEXT VkDebugObjectName;

VkResourceTableAllocator resourceTableAllocator;
VkResourceTableLayoutAllocator resourceTableLayoutAllocator;
VkResourcePipelineAllocator resourcePipelineAllocator;
Expand Down
18 changes: 17 additions & 1 deletion code/render/coregraphics/vk/vksemaphore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,25 @@ CreateSemaphore(const SemaphoreCreateInfo& info)
semaphoreAllocator.Get<Semaphore_Device>(id) = dev;
semaphoreAllocator.Get<Semaphore_Type>(id) = info.type;

VkResult res = vkCreateSemaphore(dev, &cinfo, nullptr, &semaphoreAllocator.Get<Semaphore_VkHandle>(id));
VkSemaphore sem;
VkResult res = vkCreateSemaphore(dev, &cinfo, nullptr, &sem);
n_assert(res == VK_SUCCESS);

semaphoreAllocator.Set<Semaphore_VkHandle>(id, sem);

#if NEBULA_GRAPHICS_DEBUG
VkDebugUtilsObjectNameInfoEXT semInfo =
{
VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
nullptr,
VK_OBJECT_TYPE_SEMAPHORE,
(uint64_t)sem,
info.name
};
VkResult res2 = VkDebugObjectName(dev, &semInfo);
n_assert(res2 == VK_SUCCESS);
#endif

SemaphoreId ret = id;
return ret;
}
Expand Down
Loading

0 comments on commit 9c96720

Please sign in to comment.