Files
video-shader-toys/apps/LoopThroughWithOpenGLCompositing/runtime/RuntimeSnapshotProvider.cpp
Aiden 739231d5a1
All checks were successful
CI / React UI Build (push) Successful in 10s
CI / Native Windows Build And Tests (push) Successful in 2m34s
CI / Windows Release Package (push) Successful in 2m42s
Phase 1 clean-up and separation of concerns
2026-05-10 23:21:13 +10:00

154 lines
5.1 KiB
C++

#include "RuntimeSnapshotProvider.h"
#include <utility>
RuntimeSnapshotProvider::RuntimeSnapshotProvider(RuntimeHost& runtimeHost) :
mRuntimeHost(runtimeHost)
{
}
bool RuntimeSnapshotProvider::BuildLayerPassFragmentShaderSources(const std::string& layerId, std::vector<ShaderPassBuildSource>& passSources, std::string& error) const
{
return mRuntimeHost.BuildLayerPassFragmentShaderSources(layerId, passSources, error);
}
RuntimeSnapshotVersions RuntimeSnapshotProvider::GetVersions() const
{
RuntimeSnapshotVersions versions;
versions.renderStateVersion = mRuntimeHost.GetRenderStateVersion();
versions.parameterStateVersion = mRuntimeHost.GetParameterStateVersion();
return versions;
}
RuntimeRenderFrameContext RuntimeSnapshotProvider::GetFrameContext() const
{
std::vector<RuntimeRenderState> stateScratch(1);
mRuntimeHost.RefreshDynamicRenderStateFields(stateScratch);
RuntimeRenderFrameContext frameContext;
const RuntimeRenderState& state = stateScratch.front();
frameContext.timeSeconds = state.timeSeconds;
frameContext.utcTimeSeconds = state.utcTimeSeconds;
frameContext.utcOffsetSeconds = state.utcOffsetSeconds;
frameContext.startupRandom = state.startupRandom;
frameContext.frameCount = state.frameCount;
return frameContext;
}
RuntimeRenderStateSnapshot RuntimeSnapshotProvider::GetRenderStateSnapshot(unsigned outputWidth, unsigned outputHeight) const
{
for (;;)
{
const RuntimeSnapshotVersions versionsBefore = GetVersions();
RuntimeRenderStateSnapshot snapshot;
snapshot.outputWidth = outputWidth;
snapshot.outputHeight = outputHeight;
snapshot.states = mRuntimeHost.GetLayerRenderStates(outputWidth, outputHeight);
const RuntimeSnapshotVersions versionsAfter = GetVersions();
if (versionsBefore.renderStateVersion == versionsAfter.renderStateVersion &&
versionsBefore.parameterStateVersion == versionsAfter.parameterStateVersion)
{
snapshot.versions = versionsAfter;
return snapshot;
}
}
}
bool RuntimeSnapshotProvider::TryGetRenderStateSnapshot(unsigned outputWidth, unsigned outputHeight, RuntimeRenderStateSnapshot& snapshot) const
{
const RuntimeSnapshotVersions versionsBefore = GetVersions();
std::vector<RuntimeRenderState> states;
if (!mRuntimeHost.TryGetLayerRenderStates(outputWidth, outputHeight, states))
return false;
const RuntimeSnapshotVersions versionsAfter = GetVersions();
if (versionsBefore.renderStateVersion != versionsAfter.renderStateVersion ||
versionsBefore.parameterStateVersion != versionsAfter.parameterStateVersion)
{
return false;
}
snapshot.outputWidth = outputWidth;
snapshot.outputHeight = outputHeight;
snapshot.versions = versionsAfter;
snapshot.states = std::move(states);
return true;
}
bool RuntimeSnapshotProvider::TryRefreshSnapshotParameters(RuntimeRenderStateSnapshot& snapshot) const
{
const uint64_t expectedRenderStateVersion = snapshot.versions.renderStateVersion;
if (!mRuntimeHost.TryRefreshCachedLayerStates(snapshot.states))
return false;
const RuntimeSnapshotVersions versions = GetVersions();
if (versions.renderStateVersion != expectedRenderStateVersion)
return false;
snapshot.versions = versions;
return true;
}
void RuntimeSnapshotProvider::ApplyFrameContext(std::vector<RuntimeRenderState>& states, const RuntimeRenderFrameContext& frameContext) const
{
for (RuntimeRenderState& state : states)
{
state.timeSeconds = frameContext.timeSeconds;
state.utcTimeSeconds = frameContext.utcTimeSeconds;
state.utcOffsetSeconds = frameContext.utcOffsetSeconds;
state.startupRandom = frameContext.startupRandom;
state.frameCount = frameContext.frameCount;
}
}
void RuntimeSnapshotProvider::ApplyFrameContext(RuntimeRenderStateSnapshot& snapshot, const RuntimeRenderFrameContext& frameContext) const
{
ApplyFrameContext(snapshot.states, frameContext);
}
std::vector<RuntimeRenderState> RuntimeSnapshotProvider::GetLayerRenderStates(unsigned outputWidth, unsigned outputHeight) const
{
return GetRenderStateSnapshot(outputWidth, outputHeight).states;
}
bool RuntimeSnapshotProvider::TryGetLayerRenderStates(unsigned outputWidth, unsigned outputHeight, std::vector<RuntimeRenderState>& states) const
{
RuntimeRenderStateSnapshot snapshot;
if (!TryGetRenderStateSnapshot(outputWidth, outputHeight, snapshot))
return false;
states = std::move(snapshot.states);
return true;
}
bool RuntimeSnapshotProvider::TryRefreshCachedLayerStates(std::vector<RuntimeRenderState>& states) const
{
RuntimeRenderStateSnapshot snapshot;
snapshot.versions.renderStateVersion = mRuntimeHost.GetRenderStateVersion();
snapshot.versions.parameterStateVersion = mRuntimeHost.GetParameterStateVersion();
snapshot.states = states;
if (!TryRefreshSnapshotParameters(snapshot))
return false;
states = std::move(snapshot.states);
return true;
}
void RuntimeSnapshotProvider::RefreshDynamicRenderStateFields(std::vector<RuntimeRenderState>& states) const
{
ApplyFrameContext(states, GetFrameContext());
}
uint64_t RuntimeSnapshotProvider::GetRenderStateVersion() const
{
return GetVersions().renderStateVersion;
}
uint64_t RuntimeSnapshotProvider::GetParameterStateVersion() const
{
return GetVersions().parameterStateVersion;
}