Prepare shader reload.

- Expose wait until gpus is idle to api
- Alt+R to reload
This commit is contained in:
2025-03-15 19:55:41 -04:00
parent f01c8c3ccb
commit c9cd01bb31
8 changed files with 44 additions and 9 deletions

View File

@@ -20,8 +20,8 @@ namespace Juliet
uint16 Raw;
};
extern bool IsKeyDown(ScanCode scanCode);
extern JULIET_API bool IsKeyDown(ScanCode scanCode);
extern KeyMod GetKeyModState();
extern KeyCode GetKeyCodeFromScanCode(ScanCode scanCode, KeyMod keyModState);
extern JULIET_API KeyMod GetKeyModState();
extern JULIET_API KeyCode GetKeyCodeFromScanCode(ScanCode scanCode, KeyMod keyModState);
} // namespace Juliet

View File

@@ -118,6 +118,9 @@ namespace Juliet
extern JULIET_API void DrawPrimitives(NonNullPtr<RenderPass> renderPass, uint32 numVertices, uint32 numInstances,
uint32 firstVertex, uint32 firstInstance);
// Fences
extern JULIET_API bool WaitUntilGPUIsIdle(NonNullPtr<GraphicsDevice> device);
// Shaders
extern JULIET_API Shader* CreateShader(NonNullPtr<GraphicsDevice> device, String filename, ShaderCreateInfo& shaderCreateInfo);
extern JULIET_API void DestroyShader(NonNullPtr<GraphicsDevice> device, NonNullPtr<Shader> shader);

View File

@@ -406,7 +406,7 @@ namespace Juliet::D3D12
auto* windowData = d3d12Driver->WindowData;
Assert(windowData && "Trying to destroy a swapchain but no Window Data exists");
Wait(driver);
WaitUntilGPUIsIdle(driver);
for (uint32 idx = 0; idx < GPUDriver::kMaxFramesInFlight; idx += 1)
{
@@ -748,7 +748,7 @@ namespace Juliet::D3D12
device->SetStencilReference = SetStencilReference;
device->BindGraphicsPipeline = BindGraphicsPipeline;
device->DrawPrimitives = DrawPrimitives;
device->Wait = Wait;
device->WaitUntilGPUIsIdle = WaitUntilGPUIsIdle;
device->QueryFence = QueryFence;
device->ReleaseFence = ReleaseFence;
device->CreateShader = CreateShader;

View File

@@ -23,7 +23,7 @@ namespace Juliet::D3D12
}
} // namespace
bool Wait(NonNullPtr<GPUDriver> driver)
bool WaitUntilGPUIsIdle(NonNullPtr<GPUDriver> driver)
{
auto d3d12driver = static_cast<D3D12Driver*>(driver.Get());
D3D12Fence* fence = Internal::AcquireFence(d3d12driver);

View File

@@ -25,7 +25,7 @@ namespace Juliet::D3D12
int32 ReferenceCount; // TODO : Atomic
};
extern bool Wait(NonNullPtr<GPUDriver> driver);
extern bool WaitUntilGPUIsIdle(NonNullPtr<GPUDriver> driver);
extern bool Wait(NonNullPtr<GPUDriver> driver, bool waitForAll, Fence* const* fences, uint32 numFences);
extern bool QueryFence(NonNullPtr<GPUDriver> driver, NonNullPtr<Fence> fence);
extern void ReleaseFence(NonNullPtr<GPUDriver> driver, NonNullPtr<Fence> fence);

View File

@@ -258,6 +258,12 @@ namespace Juliet
commandListHeader->Device->DrawPrimitives(commandList, numVertices, numInstances, firstVertex, firstInstance);
}
// Fences
bool WaitUntilGPUIsIdle(NonNullPtr<GraphicsDevice> device)
{
return device->WaitUntilGPUIsIdle(device->Driver);
}
// Shaders
Shader* CreateShader(NonNullPtr<GraphicsDevice> device, String filename, ShaderCreateInfo& shaderCreateInfo)
{
@@ -304,5 +310,4 @@ namespace Juliet
{
device->DestroyGraphicsPipeline(device->Driver, graphicsPipeline);
}
} // namespace Juliet

View File

@@ -69,7 +69,7 @@ namespace Juliet
uint32 firstVertex, uint32 firstInstance);
// Fences
bool (*Wait)(NonNullPtr<GPUDriver> driver);
bool (*WaitUntilGPUIsIdle)(NonNullPtr<GPUDriver> driver);
bool (*QueryFence)(NonNullPtr<GPUDriver> driver, NonNullPtr<Fence> fence);
void (*ReleaseFence)(NonNullPtr<GPUDriver> driver, NonNullPtr<Fence> fence);

View File

@@ -1,6 +1,7 @@
#include <main.h>
#include <Core/Application/ApplicationManager.h>
#include <Core/Common/EnumUtils.h>
#include <Core/HAL/Display/Display.h>
#include <Core/HAL/Event/SystemEvent.h>
#include <Core/HAL/Filesystem/Filesystem.h>
@@ -158,6 +159,9 @@ void JulietApplication::Shutdown()
void JulietApplication::Update()
{
bool reloadShaders = false;
static bool reloadShadersDebounce = false;
SystemEvent evt;
while (GetEvent(evt))
{
@@ -168,6 +172,23 @@ void JulietApplication::Update()
Running = false;
}
}
if (evt.Type == EventType::Key_Down)
{
}
// Shader hot reload using keyboard.
// TODO: Add Input debounce in the library. (Just pressed vs pressed)
if (!reloadShadersDebounce && ((GetKeyModState() & KeyMod::Alt) != KeyMod::None) && IsKeyDown(ScanCode::R))
{
reloadShaders = true;
reloadShadersDebounce = true;
}
if (reloadShadersDebounce && !IsKeyDown(ScanCode::R))
{
reloadShadersDebounce = false;
}
}
Game.Update(0.0f);
@@ -177,6 +198,12 @@ void JulietApplication::Update()
ReloadCode(GameCode);
}
if (reloadShaders)
{
// We need to wait for the gpu to be idle to recreate our graphics pipelines
WaitUntilGPUIsIdle(GraphicsDevice);
}
// Draw here for now
// 1) Acquire a Command Buffer
CommandList* cmdList = AcquireCommandList(GraphicsDevice, QueueType::Graphics);