#include "RuntimeSnapshotProvider.h" #include RuntimeSnapshotProvider::RuntimeSnapshotProvider(RuntimeHost& runtimeHost) : mRuntimeHost(runtimeHost) { } bool RuntimeSnapshotProvider::BuildLayerPassFragmentShaderSources(const std::string& layerId, std::vector& passSources, std::string& error) const { return mRuntimeHost.BuildLayerPassFragmentShaderSources(layerId, passSources, error); } unsigned RuntimeSnapshotProvider::GetMaxTemporalHistoryFrames() const { return mRuntimeHost.GetMaxTemporalHistoryFrames(); } RuntimeSnapshotVersions RuntimeSnapshotProvider::GetVersions() const { RuntimeSnapshotVersions versions; versions.renderStateVersion = mRuntimeHost.GetRenderStateVersion(); versions.parameterStateVersion = mRuntimeHost.GetParameterStateVersion(); return versions; } RuntimeRenderFrameContext RuntimeSnapshotProvider::GetFrameContext() const { std::vector 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; } void RuntimeSnapshotProvider::AdvanceFrame() { mRuntimeHost.AdvanceFrame(); } bool RuntimeSnapshotProvider::TryAdvanceFrame() { return mRuntimeHost.TryAdvanceFrame(); } 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 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& 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 RuntimeSnapshotProvider::GetLayerRenderStates(unsigned outputWidth, unsigned outputHeight) const { return GetRenderStateSnapshot(outputWidth, outputHeight).states; } bool RuntimeSnapshotProvider::TryGetLayerRenderStates(unsigned outputWidth, unsigned outputHeight, std::vector& states) const { RuntimeRenderStateSnapshot snapshot; if (!TryGetRenderStateSnapshot(outputWidth, outputHeight, snapshot)) return false; states = std::move(snapshot.states); return true; } bool RuntimeSnapshotProvider::TryRefreshCachedLayerStates(std::vector& 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& states) const { ApplyFrameContext(states, GetFrameContext()); } uint64_t RuntimeSnapshotProvider::GetRenderStateVersion() const { return GetVersions().renderStateVersion; } uint64_t RuntimeSnapshotProvider::GetParameterStateVersion() const { return GetVersions().parameterStateVersion; }