#include "RuntimeSnapshotProvider.h" #include "ShaderCompiler.h" #include #include RuntimeSnapshotProvider::RuntimeSnapshotProvider(RuntimeStore& runtimeStore) : mRuntimeStore(runtimeStore) { } bool RuntimeSnapshotProvider::BuildLayerPassFragmentShaderSources(const std::string& layerId, std::vector& passSources, std::string& error) const { try { ShaderPackage shaderPackage; if (!mRuntimeStore.CopyShaderPackageForStoredLayer(layerId, shaderPackage, error)) return false; std::filesystem::path repoRoot; std::filesystem::path wrapperPath; std::filesystem::path generatedGlslPath; std::filesystem::path patchedGlslPath; unsigned maxTemporalHistoryFrames = 0; mRuntimeStore.GetShaderCompilerInputs(repoRoot, wrapperPath, generatedGlslPath, patchedGlslPath, maxTemporalHistoryFrames); ShaderCompiler compiler( repoRoot, wrapperPath, generatedGlslPath, patchedGlslPath, maxTemporalHistoryFrames); passSources.clear(); passSources.reserve(shaderPackage.passes.size()); for (const ShaderPassDefinition& pass : shaderPackage.passes) { ShaderPassBuildSource passSource; passSource.passId = pass.id; passSource.inputNames = pass.inputNames; passSource.outputName = pass.outputName; if (!compiler.BuildPassFragmentShaderSource(shaderPackage, pass, passSource.fragmentShaderSource, error)) return false; passSources.push_back(std::move(passSource)); } return true; } catch (const std::exception& exception) { error = std::string("RuntimeSnapshotProvider::BuildLayerPassFragmentShaderSources exception: ") + exception.what(); return false; } catch (...) { error = "RuntimeSnapshotProvider::BuildLayerPassFragmentShaderSources threw a non-standard exception."; return false; } } unsigned RuntimeSnapshotProvider::GetMaxTemporalHistoryFrames() const { return mRuntimeStore.GetConfiguredMaxTemporalHistoryFrames(); } RuntimeSnapshotVersions RuntimeSnapshotProvider::GetVersions() const { RuntimeSnapshotVersions versions; versions.renderStateVersion = mRuntimeStore.GetRenderStateVersion(); versions.parameterStateVersion = mRuntimeStore.GetParameterStateVersion(); return versions; } void RuntimeSnapshotProvider::AdvanceFrame() { mRuntimeStore.AdvanceFrameCounter(); } RuntimeRenderStateSnapshot RuntimeSnapshotProvider::GetRenderStateSnapshot(unsigned outputWidth, unsigned outputHeight) const { for (;;) { const RuntimeSnapshotVersions versionsBefore = GetVersions(); RuntimeRenderStateSnapshot snapshot; snapshot.outputWidth = outputWidth; snapshot.outputHeight = outputHeight; mRuntimeStore.BuildLayerRenderStates(outputWidth, outputHeight, snapshot.states); 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 (!mRuntimeStore.TryBuildLayerRenderStates(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 (!mRuntimeStore.TryRefreshLayerParameters(snapshot.states)) return false; const RuntimeSnapshotVersions versions = GetVersions(); if (versions.renderStateVersion != expectedRenderStateVersion) return false; snapshot.versions = versions; return true; } void RuntimeSnapshotProvider::RefreshDynamicRenderStateFields(std::vector& states) const { mRuntimeStore.RefreshDynamicRenderStateFields(states); }