dispatch event intergration
All checks were successful
CI / React UI Build (push) Successful in 11s
CI / Native Windows Build And Tests (push) Successful in 2m28s
CI / Windows Release Package (push) Successful in 2m24s

This commit is contained in:
Aiden
2026-05-11 15:42:14 +10:00
parent ccfc0237fd
commit a9b08f7f27
16 changed files with 785 additions and 59 deletions

View File

@@ -50,7 +50,7 @@ OpenGLComposite::OpenGLComposite(HWND hWnd, HDC hDC, HGLRC hRC) :
[this]() { ProcessScreenshotRequest(); },
[this]() { paintGL(false); });
mVideoBackend = std::make_unique<VideoBackend>(*mRenderEngine, mRuntimeStore->GetHealthTelemetry());
mShaderBuildQueue = std::make_unique<ShaderBuildQueue>(*mRuntimeSnapshotProvider);
mShaderBuildQueue = std::make_unique<ShaderBuildQueue>(*mRuntimeSnapshotProvider, *mRuntimeEventDispatcher);
mRuntimeServices = std::make_unique<RuntimeServices>(*mRuntimeEventDispatcher);
mRuntimeUpdateController = std::make_unique<RuntimeUpdateController>(
*mRuntimeStore,

View File

@@ -7,8 +7,26 @@
#include "ShaderBuildQueue.h"
#include "VideoBackend.h"
#include <variant>
#include <vector>
namespace
{
RuntimeCoordinatorRenderResetScope ToRuntimeCoordinatorRenderResetScope(RuntimeEventRenderResetScope scope)
{
switch (scope)
{
case RuntimeEventRenderResetScope::TemporalHistoryOnly:
return RuntimeCoordinatorRenderResetScope::TemporalHistoryOnly;
case RuntimeEventRenderResetScope::TemporalHistoryAndFeedback:
return RuntimeCoordinatorRenderResetScope::TemporalHistoryAndFeedback;
case RuntimeEventRenderResetScope::None:
default:
return RuntimeCoordinatorRenderResetScope::None;
}
}
}
RuntimeUpdateController::RuntimeUpdateController(
RuntimeStore& runtimeStore,
RuntimeCoordinator& runtimeCoordinator,
@@ -28,6 +46,21 @@ RuntimeUpdateController::RuntimeUpdateController(
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::RuntimeStateBroadcastRequested,
[this](const RuntimeEvent& event) { HandleRuntimeStateBroadcastRequested(event); });
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::ShaderBuildRequested,
[this](const RuntimeEvent& event) { HandleShaderBuildRequested(event); });
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::ShaderBuildPrepared,
[this](const RuntimeEvent& event) { HandleShaderBuildPrepared(event); });
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::ShaderBuildFailed,
[this](const RuntimeEvent& event) { HandleShaderBuildFailed(event); });
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::CompileStatusChanged,
[this](const RuntimeEvent& event) { HandleCompileStatusChanged(event); });
mRuntimeEventDispatcher.Subscribe(
RuntimeEventType::RenderResetRequested,
[this](const RuntimeEvent& event) { HandleRenderResetRequested(event); });
}
bool RuntimeUpdateController::ApplyRuntimeCoordinatorResult(const RuntimeCoordinatorResult& result, std::string* error)
@@ -40,7 +73,10 @@ bool RuntimeUpdateController::ApplyRuntimeCoordinatorResult(const RuntimeCoordin
}
if (result.compileStatusChanged)
{
mRuntimeStore.SetCompileStatus(result.compileStatusSucceeded, result.compileStatusMessage);
++mPendingCoordinatorCompileStatusEvents;
}
if (result.clearReloadRequest)
mRuntimeStore.ClearReloadRequest();
@@ -54,9 +90,14 @@ bool RuntimeUpdateController::ApplyRuntimeCoordinatorResult(const RuntimeCoordin
}
mRenderEngine.ApplyRuntimeCoordinatorRenderReset(result.renderResetScope);
if (result.renderResetScope != RuntimeCoordinatorRenderResetScope::None)
++mPendingCoordinatorRenderResetEvents;
if (result.shaderBuildRequested)
{
RequestShaderBuild();
++mPendingCoordinatorShaderBuildEvents;
}
if (result.runtimeStateBroadcastRequired)
BroadcastRuntimeState();
@@ -66,8 +107,6 @@ bool RuntimeUpdateController::ApplyRuntimeCoordinatorResult(const RuntimeCoordin
bool RuntimeUpdateController::ProcessRuntimeWork()
{
mRuntimeEventDispatcher.DispatchPending();
bool shaderBuildRequested = false;
std::vector<RuntimeCoordinatorServiceResult> serviceResults;
mRuntimeServices.ConsumeRuntimeCoordinatorResults(serviceResults);
@@ -82,22 +121,9 @@ bool RuntimeUpdateController::ProcessRuntimeWork()
if (shaderBuildRequested)
return true;
const RenderEngine::PreparedShaderBuildApplyResult buildResult = mRenderEngine.TryApplyReadyShaderBuild(
mShaderBuildQueue,
mVideoBackend.InputFrameWidth(),
mVideoBackend.InputFrameHeight(),
mRuntimeCoordinator.PreserveFeedbackOnNextShaderBuild());
if (!buildResult.hadReadyBuild)
return true;
DispatchRuntimeEvents();
if (!buildResult.applied)
{
ApplyRuntimeCoordinatorResult(mRuntimeCoordinator.HandlePreparedShaderBuildFailure(buildResult.errorMessage));
return false;
}
ApplyRuntimeCoordinatorResult(mRuntimeCoordinator.HandlePreparedShaderBuildSuccess());
return true;
return ConsumeReadyShaderBuild(0, true, true);
}
void RuntimeUpdateController::RequestShaderBuild()
@@ -115,11 +141,188 @@ void RuntimeUpdateController::BroadcastRuntimeState()
return;
}
mRuntimeEventDispatcher.DispatchPending();
DispatchRuntimeEvents();
}
void RuntimeUpdateController::HandleRuntimeStateBroadcastRequested(const RuntimeEvent& event)
{
(void)event;
if (event.source == "ControlServices")
return;
mRuntimeServices.BroadcastState();
}
void RuntimeUpdateController::HandleShaderBuildRequested(const RuntimeEvent& event)
{
const ShaderBuildEvent* payload = std::get_if<ShaderBuildEvent>(&event.payload);
if (!payload || payload->phase != RuntimeEventShaderBuildPhase::Requested)
return;
if (ShouldSuppressCoordinatorFollowUp(event, mPendingCoordinatorShaderBuildEvents))
return;
RequestShaderBuild();
}
void RuntimeUpdateController::HandleShaderBuildPrepared(const RuntimeEvent& event)
{
const ShaderBuildEvent* payload = std::get_if<ShaderBuildEvent>(&event.payload);
if (!payload || payload->phase != RuntimeEventShaderBuildPhase::Prepared)
return;
ConsumeReadyShaderBuild(payload->generation, false, true);
}
void RuntimeUpdateController::HandleShaderBuildFailed(const RuntimeEvent& event)
{
const ShaderBuildEvent* payload = std::get_if<ShaderBuildEvent>(&event.payload);
if (!payload || payload->phase != RuntimeEventShaderBuildPhase::Failed)
return;
ConsumeReadyShaderBuild(payload->generation, false, false);
}
void RuntimeUpdateController::HandleCompileStatusChanged(const RuntimeEvent& event)
{
const CompileStatusChangedEvent* payload = std::get_if<CompileStatusChangedEvent>(&event.payload);
if (!payload)
return;
if (ShouldSuppressCoordinatorFollowUp(event, mPendingCoordinatorCompileStatusEvents))
return;
mRuntimeStore.SetCompileStatus(payload->succeeded, payload->message);
}
void RuntimeUpdateController::HandleRenderResetRequested(const RuntimeEvent& event)
{
const RenderResetEvent* payload = std::get_if<RenderResetEvent>(&event.payload);
if (!payload || payload->applied)
return;
if (ShouldSuppressCoordinatorFollowUp(event, mPendingCoordinatorRenderResetEvents))
return;
mRenderEngine.ApplyRuntimeCoordinatorRenderReset(ToRuntimeCoordinatorRenderResetScope(payload->scope));
}
bool RuntimeUpdateController::ConsumeReadyShaderBuild(uint64_t expectedGeneration, bool publishPreparedEvent, bool publishFailureEvent)
{
PreparedShaderBuild readyBuild;
const bool consumed = expectedGeneration == 0
? mShaderBuildQueue.TryConsumeReadyBuild(readyBuild)
: mShaderBuildQueue.TryConsumeReadyBuild(expectedGeneration, readyBuild);
if (!consumed)
return true;
const unsigned inputWidth = mVideoBackend.InputFrameWidth();
const unsigned inputHeight = mVideoBackend.InputFrameHeight();
if (!readyBuild.succeeded)
{
if (publishFailureEvent)
{
PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase::Failed,
readyBuild.generation,
inputWidth,
inputHeight,
false,
readyBuild.message);
DispatchRuntimeEvents();
}
ApplyRuntimeCoordinatorResult(mRuntimeCoordinator.HandlePreparedShaderBuildFailure(readyBuild.message));
return false;
}
if (publishPreparedEvent)
{
PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase::Prepared,
readyBuild.generation,
inputWidth,
inputHeight,
true,
readyBuild.message);
DispatchRuntimeEvents();
}
char compilerErrorMessage[1024] = {};
if (!mRenderEngine.ApplyPreparedShaderBuild(
readyBuild,
inputWidth,
inputHeight,
mRuntimeCoordinator.PreserveFeedbackOnNextShaderBuild(),
sizeof(compilerErrorMessage),
compilerErrorMessage))
{
const std::string errorMessage = compilerErrorMessage;
if (publishFailureEvent)
{
PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase::Failed,
readyBuild.generation,
inputWidth,
inputHeight,
false,
errorMessage);
DispatchRuntimeEvents();
}
ApplyRuntimeCoordinatorResult(mRuntimeCoordinator.HandlePreparedShaderBuildFailure(errorMessage));
return false;
}
PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase::Applied,
readyBuild.generation,
inputWidth,
inputHeight,
true,
"Shader layers applied successfully.");
ApplyRuntimeCoordinatorResult(mRuntimeCoordinator.HandlePreparedShaderBuildSuccess());
return true;
}
void RuntimeUpdateController::PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase phase,
uint64_t generation,
unsigned inputWidth,
unsigned inputHeight,
bool succeeded,
const std::string& message)
{
ShaderBuildEvent event;
event.phase = phase;
event.generation = generation;
event.inputWidth = inputWidth;
event.inputHeight = inputHeight;
event.preserveFeedbackState = mRuntimeCoordinator.PreserveFeedbackOnNextShaderBuild();
event.succeeded = succeeded;
event.message = message;
mRuntimeEventDispatcher.PublishPayload(event, "RuntimeUpdateController");
}
bool RuntimeUpdateController::ShouldSuppressCoordinatorFollowUp(const RuntimeEvent& event, std::size_t& pendingSuppressions)
{
if (event.source != "RuntimeCoordinator")
return false;
if (pendingSuppressions > 0)
--pendingSuppressions;
return true;
}
RuntimeEventDispatchResult RuntimeUpdateController::DispatchRuntimeEvents(std::size_t maxEvents)
{
RuntimeEventDispatchResult result = mRuntimeEventDispatcher.DispatchPending(maxEvents);
const RuntimeEventQueueMetrics queueMetrics = mRuntimeEventDispatcher.GetQueueMetrics();
HealthTelemetry& telemetry = mRuntimeStore.GetHealthTelemetry();
telemetry.TryRecordRuntimeEventDispatchStats(
result.dispatchedEvents,
result.handlerInvocations,
result.handlerFailures,
result.dispatchDurationMilliseconds);
telemetry.TryRecordRuntimeEventQueueMetrics(
"runtime-events",
queueMetrics.depth,
queueMetrics.capacity,
static_cast<uint64_t>(queueMetrics.droppedCount),
queueMetrics.oldestEventAgeMilliseconds);
return result;
}

View File

@@ -1,11 +1,15 @@
#pragma once
#include "RuntimeCoordinator.h"
#include "RuntimeEventPayloads.h"
#include <cstddef>
#include <cstdint>
#include <string>
class RenderEngine;
struct RuntimeEvent;
struct RuntimeEventDispatchResult;
class RuntimeEventDispatcher;
class RuntimeServices;
class RuntimeStore;
@@ -31,6 +35,21 @@ public:
private:
void HandleRuntimeStateBroadcastRequested(const RuntimeEvent& event);
void HandleShaderBuildRequested(const RuntimeEvent& event);
void HandleShaderBuildPrepared(const RuntimeEvent& event);
void HandleShaderBuildFailed(const RuntimeEvent& event);
void HandleCompileStatusChanged(const RuntimeEvent& event);
void HandleRenderResetRequested(const RuntimeEvent& event);
bool ConsumeReadyShaderBuild(uint64_t expectedGeneration, bool publishPreparedEvent, bool publishFailureEvent);
void PublishShaderBuildLifecycleEvent(
RuntimeEventShaderBuildPhase phase,
uint64_t generation,
unsigned inputWidth,
unsigned inputHeight,
bool succeeded,
const std::string& message);
bool ShouldSuppressCoordinatorFollowUp(const RuntimeEvent& event, std::size_t& pendingSuppressions);
RuntimeEventDispatchResult DispatchRuntimeEvents(std::size_t maxEvents = 0);
RuntimeStore& mRuntimeStore;
RuntimeCoordinator& mRuntimeCoordinator;
@@ -39,4 +58,7 @@ private:
RenderEngine& mRenderEngine;
ShaderBuildQueue& mShaderBuildQueue;
VideoBackend& mVideoBackend;
std::size_t mPendingCoordinatorShaderBuildEvents = 0;
std::size_t mPendingCoordinatorCompileStatusEvents = 0;
std::size_t mPendingCoordinatorRenderResetEvents = 0;
};

View File

@@ -1,5 +1,7 @@
#include "ShaderBuildQueue.h"
#include "RuntimeEventDispatcher.h"
#include <chrono>
#include <utility>
@@ -8,8 +10,9 @@ namespace
constexpr auto kShaderBuildDebounce = std::chrono::milliseconds(400);
}
ShaderBuildQueue::ShaderBuildQueue(RuntimeSnapshotProvider& runtimeSnapshotProvider) :
ShaderBuildQueue::ShaderBuildQueue(RuntimeSnapshotProvider& runtimeSnapshotProvider, RuntimeEventDispatcher& runtimeEventDispatcher) :
mRuntimeSnapshotProvider(runtimeSnapshotProvider),
mRuntimeEventDispatcher(runtimeEventDispatcher),
mWorkerThread([this]() { WorkerLoop(); })
{
}
@@ -44,6 +47,18 @@ bool ShaderBuildQueue::TryConsumeReadyBuild(PreparedShaderBuild& build)
return true;
}
bool ShaderBuildQueue::TryConsumeReadyBuild(uint64_t expectedGeneration, PreparedShaderBuild& build)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mHasReadyBuild || mReadyBuild.generation != expectedGeneration)
return false;
build = std::move(mReadyBuild);
mReadyBuild = PreparedShaderBuild();
mHasReadyBuild = false;
return true;
}
void ShaderBuildQueue::Stop()
{
{
@@ -97,13 +112,20 @@ void ShaderBuildQueue::WorkerLoop()
PreparedShaderBuild build = Build(generation, outputWidth, outputHeight);
std::lock_guard<std::mutex> lock(mMutex);
if (mStopping)
return;
if (generation != mRequestedGeneration)
continue;
mReadyBuild = std::move(build);
mHasReadyBuild = true;
bool shouldPublish = false;
{
std::lock_guard<std::mutex> lock(mMutex);
if (mStopping)
return;
if (generation != mRequestedGeneration)
continue;
mReadyBuild = build;
mHasReadyBuild = true;
shouldPublish = true;
}
if (shouldPublish)
PublishBuildLifecycleEvent(build, outputWidth, outputHeight);
}
}
@@ -130,3 +152,15 @@ PreparedShaderBuild ShaderBuildQueue::Build(uint64_t generation, unsigned output
build.message = "Shader layers prepared successfully.";
return build;
}
void ShaderBuildQueue::PublishBuildLifecycleEvent(const PreparedShaderBuild& build, unsigned outputWidth, unsigned outputHeight) const
{
ShaderBuildEvent event;
event.phase = build.succeeded ? RuntimeEventShaderBuildPhase::Prepared : RuntimeEventShaderBuildPhase::Failed;
event.generation = build.generation;
event.inputWidth = outputWidth;
event.inputHeight = outputHeight;
event.succeeded = build.succeeded;
event.message = build.message;
mRuntimeEventDispatcher.PublishPayload(event, "ShaderBuildQueue");
}

View File

@@ -10,6 +10,8 @@
#include <thread>
#include <vector>
class RuntimeEventDispatcher;
struct PreparedLayerShader
{
RuntimeRenderState state;
@@ -28,7 +30,7 @@ struct PreparedShaderBuild
class ShaderBuildQueue
{
public:
explicit ShaderBuildQueue(RuntimeSnapshotProvider& runtimeSnapshotProvider);
ShaderBuildQueue(RuntimeSnapshotProvider& runtimeSnapshotProvider, RuntimeEventDispatcher& runtimeEventDispatcher);
~ShaderBuildQueue();
ShaderBuildQueue(const ShaderBuildQueue&) = delete;
@@ -36,13 +38,16 @@ public:
void RequestBuild(unsigned outputWidth, unsigned outputHeight);
bool TryConsumeReadyBuild(PreparedShaderBuild& build);
bool TryConsumeReadyBuild(uint64_t expectedGeneration, PreparedShaderBuild& build);
void Stop();
private:
void WorkerLoop();
PreparedShaderBuild Build(uint64_t generation, unsigned outputWidth, unsigned outputHeight);
void PublishBuildLifecycleEvent(const PreparedShaderBuild& build, unsigned outputWidth, unsigned outputHeight) const;
RuntimeSnapshotProvider& mRuntimeSnapshotProvider;
RuntimeEventDispatcher& mRuntimeEventDispatcher;
std::thread mWorkerThread;
std::mutex mMutex;
std::condition_variable mCondition;