re organisation
All checks were successful
CI / React UI Build (push) Successful in 11s
CI / Native Windows Build And Tests (push) Successful in 2m47s
CI / Windows Release Package (push) Successful in 2m52s

This commit is contained in:
Aiden
2026-05-11 02:11:51 +10:00
parent 5cbdbd6813
commit cbf1b541dc
35 changed files with 92 additions and 78 deletions

View File

@@ -0,0 +1,738 @@
#include "LayerStackStore.h"
#include "RuntimeParameterUtils.h"
#include "RuntimeStateJson.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <set>
#include <sstream>
#include <utility>
namespace
{
std::string TrimCopy(const std::string& text)
{
std::size_t start = 0;
while (start < text.size() && std::isspace(static_cast<unsigned char>(text[start])))
++start;
std::size_t end = text.size();
while (end > start && std::isspace(static_cast<unsigned char>(text[end - 1])))
--end;
return text.substr(start, end - start);
}
std::string SimplifyControlKey(const std::string& text)
{
std::string simplified;
for (unsigned char ch : text)
{
if (std::isalnum(ch))
simplified.push_back(static_cast<char>(std::tolower(ch)));
}
return simplified;
}
bool MatchesControlKey(const std::string& candidate, const std::string& key)
{
return candidate == key || SimplifyControlKey(candidate) == SimplifyControlKey(key);
}
bool TryParseLayerIdNumber(const std::string& layerId, uint64_t& number)
{
const std::string prefix = "layer-";
if (layerId.rfind(prefix, 0) != 0 || layerId.size() == prefix.size())
return false;
uint64_t parsed = 0;
for (std::size_t index = prefix.size(); index < layerId.size(); ++index)
{
const unsigned char ch = static_cast<unsigned char>(layerId[index]);
if (!std::isdigit(ch))
return false;
parsed = parsed * 10 + static_cast<uint64_t>(ch - '0');
}
number = parsed;
return true;
}
}
bool LayerStackStore::LoadPersistentStateValue(const JsonValue& root)
{
if (const JsonValue* layersValue = root.find("layers"))
{
for (const JsonValue& layerValue : layersValue->asArray())
{
if (!layerValue.isObject())
continue;
LayerPersistentState layer;
if (const JsonValue* idValue = layerValue.find("id"))
layer.id = idValue->asString();
if (const JsonValue* shaderIdValue = layerValue.find("shaderId"))
layer.shaderId = shaderIdValue->asString();
if (const JsonValue* bypassValue = layerValue.find("bypass"))
layer.bypass = bypassValue->asBoolean(false);
else if (const JsonValue* enabledValue = layerValue.find("enabled"))
layer.bypass = !enabledValue->asBoolean(true);
if (const JsonValue* parameterValues = layerValue.find("parameterValues"))
{
for (const auto& parameterItem : parameterValues->asObject())
{
ShaderParameterValue value;
const JsonValue& jsonValue = parameterItem.second;
if (jsonValue.isBoolean())
value.booleanValue = jsonValue.asBoolean();
else if (jsonValue.isString())
value.enumValue = jsonValue.asString();
else if (jsonValue.isNumber())
value.numberValues.push_back(jsonValue.asNumber());
else if (jsonValue.isArray())
value.numberValues = JsonArrayToNumbers(jsonValue);
layer.parameterValues[parameterItem.first] = value;
}
}
if (!layer.shaderId.empty())
mLayers.push_back(layer);
}
}
else
{
std::string activeShaderId;
if (const JsonValue* activeShaderValue = root.find("activeShaderId"))
activeShaderId = activeShaderValue->asString();
if (!activeShaderId.empty())
{
LayerPersistentState layer;
layer.id = GenerateLayerId(mLayers, mNextLayerId);
layer.shaderId = activeShaderId;
layer.bypass = false;
if (const JsonValue* valuesByShader = root.find("parameterValuesByShader"))
{
const JsonValue* shaderValues = valuesByShader->find(activeShaderId);
if (shaderValues)
{
for (const auto& parameterItem : shaderValues->asObject())
{
ShaderParameterValue value;
const JsonValue& jsonValue = parameterItem.second;
if (jsonValue.isBoolean())
value.booleanValue = jsonValue.asBoolean();
else if (jsonValue.isString())
value.enumValue = jsonValue.asString();
else if (jsonValue.isNumber())
value.numberValues.push_back(jsonValue.asNumber());
else if (jsonValue.isArray())
value.numberValues = JsonArrayToNumbers(jsonValue);
layer.parameterValues[parameterItem.first] = value;
}
}
}
mLayers.push_back(layer);
}
}
return true;
}
JsonValue LayerStackStore::BuildPersistentStateValue(const ShaderPackageCatalog& shaderCatalog) const
{
JsonValue root = JsonValue::MakeObject();
JsonValue layers = JsonValue::MakeArray();
for (const LayerPersistentState& layer : mLayers)
{
JsonValue layerValue = JsonValue::MakeObject();
layerValue.set("id", JsonValue(layer.id));
layerValue.set("shaderId", JsonValue(layer.shaderId));
layerValue.set("bypass", JsonValue(layer.bypass));
JsonValue parameterValues = JsonValue::MakeObject();
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer.shaderId);
for (const auto& parameterItem : layer.parameterValues)
{
const ShaderParameterDefinition* definition = nullptr;
if (shaderPackage)
{
for (const ShaderParameterDefinition& candidate : shaderPackage->parameters)
{
if (candidate.id == parameterItem.first)
{
definition = &candidate;
break;
}
}
}
if (definition)
parameterValues.set(parameterItem.first, RuntimeStateJson::SerializeParameterValue(*definition, parameterItem.second));
}
layerValue.set("parameterValues", parameterValues);
layers.pushBack(layerValue);
}
root.set("layers", layers);
return root;
}
void LayerStackStore::NormalizeLayerIds()
{
std::set<std::string> usedIds;
uint64_t maxLayerNumber = mNextLayerId;
for (LayerPersistentState& layer : mLayers)
{
uint64_t layerNumber = 0;
const bool hasReusableId = !layer.id.empty() &&
usedIds.find(layer.id) == usedIds.end() &&
TryParseLayerIdNumber(layer.id, layerNumber);
if (hasReusableId)
{
usedIds.insert(layer.id);
maxLayerNumber = (std::max)(maxLayerNumber, layerNumber);
continue;
}
do
{
++maxLayerNumber;
layer.id = "layer-" + std::to_string(maxLayerNumber);
}
while (usedIds.find(layer.id) != usedIds.end());
usedIds.insert(layer.id);
}
mNextLayerId = maxLayerNumber;
}
void LayerStackStore::EnsureDefaultsForAllLayers(const ShaderPackageCatalog& shaderCatalog)
{
for (LayerPersistentState& layer : mLayers)
{
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer.shaderId);
if (shaderPackage)
EnsureLayerDefaults(layer, *shaderPackage);
}
}
void LayerStackStore::EnsureDefaultLayer(const ShaderPackageCatalog& shaderCatalog)
{
if (!mLayers.empty() || shaderCatalog.PackageOrder().empty())
return;
LayerPersistentState layer;
layer.id = GenerateLayerId(mLayers, mNextLayerId);
layer.shaderId = shaderCatalog.PackageOrder().front();
layer.bypass = false;
if (const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer.shaderId))
EnsureLayerDefaults(layer, *shaderPackage);
mLayers.push_back(layer);
}
void LayerStackStore::RemoveLayersWithMissingPackages(const ShaderPackageCatalog& shaderCatalog)
{
for (auto it = mLayers.begin(); it != mLayers.end();)
{
if (!shaderCatalog.HasPackage(it->shaderId))
it = mLayers.erase(it);
else
++it;
}
}
bool LayerStackStore::CreateLayer(const ShaderPackageCatalog& shaderCatalog, const std::string& shaderId, std::string& error)
{
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(shaderId);
if (!shaderPackage)
{
error = "Unknown shader id: " + shaderId;
return false;
}
LayerPersistentState layer;
layer.id = GenerateLayerId(mLayers, mNextLayerId);
layer.shaderId = shaderId;
layer.bypass = false;
EnsureLayerDefaults(layer, *shaderPackage);
mLayers.push_back(layer);
return true;
}
bool LayerStackStore::DeleteLayer(const std::string& layerId, std::string& error)
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
if (it == mLayers.end())
{
error = "Unknown layer id: " + layerId;
return false;
}
mLayers.erase(it);
return true;
}
bool LayerStackStore::MoveLayer(const std::string& layerId, int direction, std::string& error)
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
if (it == mLayers.end())
{
error = "Unknown layer id: " + layerId;
return false;
}
const std::ptrdiff_t index = std::distance(mLayers.begin(), it);
const std::ptrdiff_t newIndex = index + direction;
if (newIndex < 0 || newIndex >= static_cast<std::ptrdiff_t>(mLayers.size()))
return true;
std::swap(mLayers[index], mLayers[newIndex]);
return true;
}
bool LayerStackStore::MoveLayerToIndex(const std::string& layerId, std::size_t targetIndex, std::string& error)
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
if (it == mLayers.end())
{
error = "Unknown layer id: " + layerId;
return false;
}
if (mLayers.empty())
return true;
if (targetIndex >= mLayers.size())
targetIndex = mLayers.size() - 1;
const std::size_t sourceIndex = static_cast<std::size_t>(std::distance(mLayers.begin(), it));
if (sourceIndex == targetIndex)
return true;
LayerPersistentState movedLayer = *it;
mLayers.erase(mLayers.begin() + static_cast<std::ptrdiff_t>(sourceIndex));
mLayers.insert(mLayers.begin() + static_cast<std::ptrdiff_t>(targetIndex), movedLayer);
return true;
}
bool LayerStackStore::SetLayerBypassState(const std::string& layerId, bool bypassed, std::string& error)
{
LayerPersistentState* layer = FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
layer->bypass = bypassed;
return true;
}
bool LayerStackStore::SetLayerShaderSelection(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, const std::string& shaderId, std::string& error)
{
LayerPersistentState* layer = FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(shaderId);
if (!shaderPackage)
{
error = "Unknown shader id: " + shaderId;
return false;
}
layer->shaderId = shaderId;
layer->parameterValues.clear();
EnsureLayerDefaults(*layer, *shaderPackage);
return true;
}
bool LayerStackStore::SetParameterValue(const std::string& layerId, const std::string& parameterId, const ShaderParameterValue& value, std::string& error)
{
LayerPersistentState* layer = FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
layer->parameterValues[parameterId] = value;
return true;
}
bool LayerStackStore::ResetLayerParameterValues(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, std::string& error)
{
LayerPersistentState* layer = FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer->shaderId);
if (!shaderPackage)
{
error = "Unknown shader id: " + layer->shaderId;
return false;
}
layer->parameterValues.clear();
EnsureLayerDefaults(*layer, *shaderPackage);
return true;
}
bool LayerStackStore::HasLayer(const std::string& layerId) const
{
return FindLayerById(layerId) != nullptr;
}
bool LayerStackStore::TryGetParameterById(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, const std::string& parameterId, StoredParameterSnapshot& snapshot, std::string& error) const
{
const LayerPersistentState* layer = FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer->shaderId);
if (!shaderPackage)
{
error = "Unknown shader id: " + layer->shaderId;
return false;
}
auto parameterIt = std::find_if(shaderPackage->parameters.begin(), shaderPackage->parameters.end(),
[&parameterId](const ShaderParameterDefinition& definition) { return definition.id == parameterId; });
if (parameterIt == shaderPackage->parameters.end())
{
error = "Unknown parameter id: " + parameterId;
return false;
}
snapshot = StoredParameterSnapshot();
snapshot.layerId = layer->id;
snapshot.definition = *parameterIt;
auto valueIt = layer->parameterValues.find(parameterIt->id);
if (valueIt != layer->parameterValues.end())
{
snapshot.currentValue = valueIt->second;
snapshot.hasCurrentValue = true;
}
return true;
}
bool LayerStackStore::TryGetParameterByControlKey(const ShaderPackageCatalog& shaderCatalog, const std::string& layerKey, const std::string& parameterKey, StoredParameterSnapshot& snapshot, std::string& error) const
{
const LayerPersistentState* matchedLayer = nullptr;
const ShaderPackage* matchedPackage = nullptr;
for (const LayerPersistentState& layer : mLayers)
{
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(layer.shaderId);
if (!shaderPackage)
continue;
if (MatchesControlKey(layer.id, layerKey) || MatchesControlKey(shaderPackage->id, layerKey) ||
MatchesControlKey(shaderPackage->displayName, layerKey))
{
matchedLayer = &layer;
matchedPackage = shaderPackage;
break;
}
}
if (!matchedLayer || !matchedPackage)
{
error = "Unknown OSC layer key: " + layerKey;
return false;
}
auto parameterIt = std::find_if(matchedPackage->parameters.begin(), matchedPackage->parameters.end(),
[&parameterKey](const ShaderParameterDefinition& definition)
{
return MatchesControlKey(definition.id, parameterKey) || MatchesControlKey(definition.label, parameterKey);
});
if (parameterIt == matchedPackage->parameters.end())
{
error = "Unknown OSC parameter key: " + parameterKey;
return false;
}
snapshot = StoredParameterSnapshot();
snapshot.layerId = matchedLayer->id;
snapshot.definition = *parameterIt;
auto valueIt = matchedLayer->parameterValues.find(parameterIt->id);
if (valueIt != matchedLayer->parameterValues.end())
{
snapshot.currentValue = valueIt->second;
snapshot.hasCurrentValue = true;
}
return true;
}
bool LayerStackStore::ResolveLayerMove(const std::string& layerId, int direction, bool& shouldMove, std::string& error) const
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
if (it == mLayers.end())
{
error = "Unknown layer id: " + layerId;
return false;
}
const std::ptrdiff_t index = std::distance(mLayers.begin(), it);
const std::ptrdiff_t newIndex = index + direction;
shouldMove = newIndex >= 0 && newIndex < static_cast<std::ptrdiff_t>(mLayers.size()) && newIndex != index;
return true;
}
bool LayerStackStore::ResolveLayerMoveToIndex(const std::string& layerId, std::size_t targetIndex, bool& shouldMove, std::string& error) const
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
if (it == mLayers.end())
{
error = "Unknown layer id: " + layerId;
return false;
}
if (mLayers.empty())
{
shouldMove = false;
return true;
}
const std::size_t clampedTargetIndex = (std::min)(targetIndex, mLayers.size() - 1);
const std::size_t sourceIndex = static_cast<std::size_t>(std::distance(mLayers.begin(), it));
shouldMove = sourceIndex != clampedTargetIndex;
return true;
}
JsonValue LayerStackStore::BuildStackPresetValue(const ShaderPackageCatalog& shaderCatalog, const std::string& presetName) const
{
JsonValue root = JsonValue::MakeObject();
root.set("version", JsonValue(1.0));
root.set("name", JsonValue(TrimCopy(presetName)));
root.set("layers", RuntimeStateJson::SerializeLayerStack(*this, shaderCatalog));
return root;
}
bool LayerStackStore::LoadStackPresetValue(const ShaderPackageCatalog& shaderCatalog, const JsonValue& root, std::string& error)
{
const JsonValue* layersValue = root.find("layers");
if (!layersValue || !layersValue->isArray())
{
error = "Preset file is missing a valid 'layers' array.";
return false;
}
std::vector<LayerPersistentState> nextLayers;
uint64_t nextLayerId = mNextLayerId;
if (!DeserializeLayerStack(shaderCatalog, *layersValue, nextLayers, nextLayerId, error))
return false;
if (nextLayers.empty())
{
error = "Preset does not contain any valid layers.";
return false;
}
mLayers = std::move(nextLayers);
mNextLayerId = nextLayerId;
return true;
}
std::string LayerStackStore::MakeSafePresetFileStem(const std::string& presetName)
{
return ::MakeSafePresetFileStem(presetName);
}
const std::vector<LayerStackStore::LayerPersistentState>& LayerStackStore::Layers() const
{
return mLayers;
}
std::vector<LayerStackStore::LayerPersistentState>& LayerStackStore::Layers()
{
return mLayers;
}
std::size_t LayerStackStore::LayerCount() const
{
return mLayers.size();
}
const LayerStackStore::LayerPersistentState* LayerStackStore::FindLayerById(const std::string& layerId) const
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
return it == mLayers.end() ? nullptr : &*it;
}
LayerStackStore::LayerPersistentState* LayerStackStore::FindLayerById(const std::string& layerId)
{
auto it = std::find_if(mLayers.begin(), mLayers.end(),
[&layerId](const LayerPersistentState& layer) { return layer.id == layerId; });
return it == mLayers.end() ? nullptr : &*it;
}
ShaderParameterValue LayerStackStore::DefaultValueForDefinition(const ShaderParameterDefinition& definition)
{
return ::DefaultValueForDefinition(definition);
}
void LayerStackStore::EnsureLayerDefaults(LayerPersistentState& layerState, const ShaderPackage& shaderPackage)
{
for (const ShaderParameterDefinition& definition : shaderPackage.parameters)
{
auto valueIt = layerState.parameterValues.find(definition.id);
if (valueIt == layerState.parameterValues.end())
{
layerState.parameterValues[definition.id] = DefaultValueForDefinition(definition);
continue;
}
JsonValue valueJson;
bool shouldNormalize = true;
switch (definition.type)
{
case ShaderParameterType::Float:
if (valueIt->second.numberValues.empty())
shouldNormalize = false;
else
valueJson = JsonValue(valueIt->second.numberValues.front());
break;
case ShaderParameterType::Vec2:
case ShaderParameterType::Color:
valueJson = JsonValue::MakeArray();
for (double number : valueIt->second.numberValues)
valueJson.pushBack(JsonValue(number));
break;
case ShaderParameterType::Boolean:
valueJson = JsonValue(valueIt->second.booleanValue);
break;
case ShaderParameterType::Enum:
valueJson = JsonValue(valueIt->second.enumValue);
break;
case ShaderParameterType::Text:
{
const std::string textValue = !valueIt->second.textValue.empty()
? valueIt->second.textValue
: valueIt->second.enumValue;
if (textValue.empty())
{
valueIt->second = DefaultValueForDefinition(definition);
shouldNormalize = false;
}
else
{
valueJson = JsonValue(textValue);
}
break;
}
case ShaderParameterType::Trigger:
if (valueIt->second.numberValues.empty())
valueJson = JsonValue(0.0);
else
valueJson = JsonValue((std::max)(0.0, std::floor(valueIt->second.numberValues.front())));
break;
}
if (!shouldNormalize)
continue;
ShaderParameterValue normalizedValue;
std::string normalizeError;
if (NormalizeAndValidateParameterValue(definition, valueJson, normalizedValue, normalizeError))
valueIt->second = normalizedValue;
else
valueIt->second = DefaultValueForDefinition(definition);
}
}
bool LayerStackStore::DeserializeLayerStack(const ShaderPackageCatalog& shaderCatalog, const JsonValue& layersValue, std::vector<LayerPersistentState>& layers, uint64_t& nextLayerId, std::string& error)
{
for (const JsonValue& layerValue : layersValue.asArray())
{
if (!layerValue.isObject())
continue;
const JsonValue* shaderIdValue = layerValue.find("shaderId");
if (!shaderIdValue)
continue;
const std::string shaderId = shaderIdValue->asString();
const ShaderPackage* shaderPackage = shaderCatalog.FindPackage(shaderId);
if (!shaderPackage)
{
error = "Preset references unknown shader id: " + shaderId;
return false;
}
LayerPersistentState layer;
layer.id = GenerateLayerId(layers, nextLayerId);
layer.shaderId = shaderId;
if (const JsonValue* bypassValue = layerValue.find("bypass"))
layer.bypass = bypassValue->asBoolean(false);
if (const JsonValue* parametersValue = layerValue.find("parameters"))
{
for (const JsonValue& parameterValue : parametersValue->asArray())
{
if (!parameterValue.isObject())
continue;
const JsonValue* parameterIdValue = parameterValue.find("id");
const JsonValue* valueValue = parameterValue.find("value");
if (!parameterIdValue || !valueValue)
continue;
const std::string parameterId = parameterIdValue->asString();
auto definitionIt = std::find_if(shaderPackage->parameters.begin(), shaderPackage->parameters.end(),
[&parameterId](const ShaderParameterDefinition& definition) { return definition.id == parameterId; });
if (definitionIt == shaderPackage->parameters.end())
continue;
ShaderParameterValue normalizedValue;
if (!NormalizeAndValidateParameterValue(*definitionIt, *valueValue, normalizedValue, error))
return false;
layer.parameterValues[parameterId] = normalizedValue;
}
}
EnsureLayerDefaults(layer, *shaderPackage);
layers.push_back(layer);
}
return true;
}
std::string LayerStackStore::GenerateLayerId(std::vector<LayerPersistentState>& layers, uint64_t& nextLayerId)
{
while (true)
{
++nextLayerId;
const std::string candidate = "layer-" + std::to_string(nextLayerId);
auto it = std::find_if(layers.begin(), layers.end(),
[&candidate](const LayerPersistentState& layer) { return layer.id == candidate; });
if (it == layers.end())
return candidate;
}
}

View File

@@ -0,0 +1,72 @@
#pragma once
#include "RuntimeJson.h"
#include "ShaderPackageCatalog.h"
#include "ShaderTypes.h"
#include <cstddef>
#include <cstdint>
#include <map>
#include <string>
#include <vector>
class LayerStackStore
{
public:
struct LayerPersistentState
{
std::string id;
std::string shaderId;
bool bypass = false;
std::map<std::string, ShaderParameterValue> parameterValues;
};
struct StoredParameterSnapshot
{
std::string layerId;
ShaderParameterDefinition definition;
ShaderParameterValue currentValue;
bool hasCurrentValue = false;
};
bool LoadPersistentStateValue(const JsonValue& root);
JsonValue BuildPersistentStateValue(const ShaderPackageCatalog& shaderCatalog) const;
void NormalizeLayerIds();
void EnsureDefaultsForAllLayers(const ShaderPackageCatalog& shaderCatalog);
void EnsureDefaultLayer(const ShaderPackageCatalog& shaderCatalog);
void RemoveLayersWithMissingPackages(const ShaderPackageCatalog& shaderCatalog);
bool CreateLayer(const ShaderPackageCatalog& shaderCatalog, const std::string& shaderId, std::string& error);
bool DeleteLayer(const std::string& layerId, std::string& error);
bool MoveLayer(const std::string& layerId, int direction, std::string& error);
bool MoveLayerToIndex(const std::string& layerId, std::size_t targetIndex, std::string& error);
bool SetLayerBypassState(const std::string& layerId, bool bypassed, std::string& error);
bool SetLayerShaderSelection(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, const std::string& shaderId, std::string& error);
bool SetParameterValue(const std::string& layerId, const std::string& parameterId, const ShaderParameterValue& value, std::string& error);
bool ResetLayerParameterValues(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, std::string& error);
bool HasLayer(const std::string& layerId) const;
bool TryGetParameterById(const ShaderPackageCatalog& shaderCatalog, const std::string& layerId, const std::string& parameterId, StoredParameterSnapshot& snapshot, std::string& error) const;
bool TryGetParameterByControlKey(const ShaderPackageCatalog& shaderCatalog, const std::string& layerKey, const std::string& parameterKey, StoredParameterSnapshot& snapshot, std::string& error) const;
bool ResolveLayerMove(const std::string& layerId, int direction, bool& shouldMove, std::string& error) const;
bool ResolveLayerMoveToIndex(const std::string& layerId, std::size_t targetIndex, bool& shouldMove, std::string& error) const;
JsonValue BuildStackPresetValue(const ShaderPackageCatalog& shaderCatalog, const std::string& presetName) const;
bool LoadStackPresetValue(const ShaderPackageCatalog& shaderCatalog, const JsonValue& root, std::string& error);
static std::string MakeSafePresetFileStem(const std::string& presetName);
const std::vector<LayerPersistentState>& Layers() const;
std::vector<LayerPersistentState>& Layers();
std::size_t LayerCount() const;
const LayerPersistentState* FindLayerById(const std::string& layerId) const;
LayerPersistentState* FindLayerById(const std::string& layerId);
private:
static ShaderParameterValue DefaultValueForDefinition(const ShaderParameterDefinition& definition);
static void EnsureLayerDefaults(LayerPersistentState& layerState, const ShaderPackage& shaderPackage);
static bool DeserializeLayerStack(const ShaderPackageCatalog& shaderCatalog, const JsonValue& layersValue, std::vector<LayerPersistentState>& layers, uint64_t& nextLayerId, std::string& error);
static std::string GenerateLayerId(std::vector<LayerPersistentState>& layers, uint64_t& nextLayerId);
std::vector<LayerPersistentState> mLayers;
uint64_t mNextLayerId = 0;
};

View File

@@ -0,0 +1,270 @@
#include "RuntimeConfigStore.h"
#include "RuntimeJson.h"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <vector>
#include <windows.h>
namespace
{
double Clamp01(double value)
{
return (std::max)(0.0, (std::min)(1.0, value));
}
bool LooksLikePackagedRuntimeRoot(const std::filesystem::path& candidate)
{
return std::filesystem::exists(candidate / "config" / "runtime-host.json") &&
std::filesystem::exists(candidate / "runtime" / "templates" / "shader_wrapper.slang.in") &&
std::filesystem::exists(candidate / "shaders");
}
bool LooksLikeRepoRoot(const std::filesystem::path& candidate)
{
return std::filesystem::exists(candidate / "CMakeLists.txt") &&
std::filesystem::exists(candidate / "apps" / "LoopThroughWithOpenGLCompositing");
}
std::filesystem::path FindRepoRootCandidate()
{
std::vector<std::filesystem::path> rootsToTry;
char currentDirectory[MAX_PATH] = {};
if (GetCurrentDirectoryA(MAX_PATH, currentDirectory) > 0)
rootsToTry.push_back(std::filesystem::path(currentDirectory));
char modulePath[MAX_PATH] = {};
DWORD moduleLength = GetModuleFileNameA(NULL, modulePath, MAX_PATH);
if (moduleLength > 0 && moduleLength < MAX_PATH)
rootsToTry.push_back(std::filesystem::path(modulePath).parent_path());
for (const std::filesystem::path& startPath : rootsToTry)
{
std::filesystem::path candidate = startPath;
for (int depth = 0; depth < 10 && !candidate.empty(); ++depth)
{
if (LooksLikePackagedRuntimeRoot(candidate) || LooksLikeRepoRoot(candidate))
return candidate;
candidate = candidate.parent_path();
}
}
return std::filesystem::path();
}
}
bool RuntimeConfigStore::Initialize(std::string& error)
{
if (!ResolvePaths(error))
return false;
if (!LoadConfig(error))
return false;
RefreshConfigDependentPaths();
return true;
}
const RuntimeConfigStore::AppConfig& RuntimeConfigStore::GetConfig() const
{
return mConfig;
}
const std::filesystem::path& RuntimeConfigStore::GetRepoRoot() const
{
return mRepoRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetUiRoot() const
{
return mUiRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetDocsRoot() const
{
return mDocsRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetShaderRoot() const
{
return mShaderRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetRuntimeRoot() const
{
return mRuntimeRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetPresetRoot() const
{
return mPresetRoot;
}
const std::filesystem::path& RuntimeConfigStore::GetRuntimeStatePath() const
{
return mRuntimeStatePath;
}
const std::filesystem::path& RuntimeConfigStore::GetWrapperPath() const
{
return mWrapperPath;
}
const std::filesystem::path& RuntimeConfigStore::GetGeneratedGlslPath() const
{
return mGeneratedGlslPath;
}
const std::filesystem::path& RuntimeConfigStore::GetPatchedGlslPath() const
{
return mPatchedGlslPath;
}
void RuntimeConfigStore::SetBoundControlServerPort(unsigned short port)
{
mConfig.serverPort = port;
}
bool RuntimeConfigStore::ResolvePaths(std::string& error)
{
mRepoRoot = FindRepoRootCandidate();
if (mRepoRoot.empty())
{
error = "Could not locate the repository root from the current runtime path.";
return false;
}
const std::filesystem::path builtUiRoot = mRepoRoot / "ui" / "dist";
mUiRoot = std::filesystem::exists(builtUiRoot) ? builtUiRoot : (mRepoRoot / "ui");
mDocsRoot = mRepoRoot / "docs";
mConfigPath = mRepoRoot / "config" / "runtime-host.json";
mRuntimeRoot = mRepoRoot / "runtime";
mPresetRoot = mRuntimeRoot / "stack_presets";
mRuntimeStatePath = mRuntimeRoot / "runtime_state.json";
RefreshConfigDependentPaths();
std::error_code fsError;
std::filesystem::create_directories(mRuntimeRoot / "shader_cache", fsError);
std::filesystem::create_directories(mPresetRoot, fsError);
return true;
}
bool RuntimeConfigStore::LoadConfig(std::string& error)
{
if (!std::filesystem::exists(mConfigPath))
return true;
std::string configText = ReadTextFile(mConfigPath, error);
if (configText.empty())
return false;
JsonValue configJson;
if (!ParseJson(configText, configJson, error))
return false;
if (const JsonValue* shaderLibraryValue = configJson.find("shaderLibrary"))
mConfig.shaderLibrary = shaderLibraryValue->asString();
if (const JsonValue* serverPortValue = configJson.find("serverPort"))
mConfig.serverPort = static_cast<unsigned short>(serverPortValue->asNumber(mConfig.serverPort));
if (const JsonValue* oscPortValue = configJson.find("oscPort"))
mConfig.oscPort = static_cast<unsigned short>(oscPortValue->asNumber(mConfig.oscPort));
if (const JsonValue* oscBindAddressValue = configJson.find("oscBindAddress"))
mConfig.oscBindAddress = oscBindAddressValue->asString();
if (const JsonValue* oscSmoothingValue = configJson.find("oscSmoothing"))
mConfig.oscSmoothing = Clamp01(oscSmoothingValue->asNumber(mConfig.oscSmoothing));
if (const JsonValue* autoReloadValue = configJson.find("autoReload"))
mConfig.autoReload = autoReloadValue->asBoolean(mConfig.autoReload);
if (const JsonValue* maxTemporalHistoryFramesValue = configJson.find("maxTemporalHistoryFrames"))
{
const double configuredValue = maxTemporalHistoryFramesValue->asNumber(static_cast<double>(mConfig.maxTemporalHistoryFrames));
mConfig.maxTemporalHistoryFrames = configuredValue <= 0.0 ? 0u : static_cast<unsigned>(configuredValue);
}
if (const JsonValue* previewFpsValue = configJson.find("previewFps"))
{
const double configuredValue = previewFpsValue->asNumber(static_cast<double>(mConfig.previewFps));
mConfig.previewFps = configuredValue <= 0.0 ? 0u : static_cast<unsigned>(configuredValue);
}
if (const JsonValue* enableExternalKeyingValue = configJson.find("enableExternalKeying"))
mConfig.enableExternalKeying = enableExternalKeyingValue->asBoolean(mConfig.enableExternalKeying);
if (const JsonValue* videoFormatValue = configJson.find("videoFormat"))
{
if (videoFormatValue->isString() && !videoFormatValue->asString().empty())
{
mConfig.inputVideoFormat = videoFormatValue->asString();
mConfig.outputVideoFormat = videoFormatValue->asString();
}
}
if (const JsonValue* frameRateValue = configJson.find("frameRate"))
{
if (frameRateValue->isString() && !frameRateValue->asString().empty())
{
mConfig.inputFrameRate = frameRateValue->asString();
mConfig.outputFrameRate = frameRateValue->asString();
}
else if (frameRateValue->isNumber())
{
std::ostringstream stream;
stream << frameRateValue->asNumber();
mConfig.inputFrameRate = stream.str();
mConfig.outputFrameRate = stream.str();
}
}
if (const JsonValue* inputVideoFormatValue = configJson.find("inputVideoFormat"))
{
if (inputVideoFormatValue->isString() && !inputVideoFormatValue->asString().empty())
mConfig.inputVideoFormat = inputVideoFormatValue->asString();
}
if (const JsonValue* inputFrameRateValue = configJson.find("inputFrameRate"))
{
if (inputFrameRateValue->isString() && !inputFrameRateValue->asString().empty())
mConfig.inputFrameRate = inputFrameRateValue->asString();
else if (inputFrameRateValue->isNumber())
{
std::ostringstream stream;
stream << inputFrameRateValue->asNumber();
mConfig.inputFrameRate = stream.str();
}
}
if (const JsonValue* outputVideoFormatValue = configJson.find("outputVideoFormat"))
{
if (outputVideoFormatValue->isString() && !outputVideoFormatValue->asString().empty())
mConfig.outputVideoFormat = outputVideoFormatValue->asString();
}
if (const JsonValue* outputFrameRateValue = configJson.find("outputFrameRate"))
{
if (outputFrameRateValue->isString() && !outputFrameRateValue->asString().empty())
mConfig.outputFrameRate = outputFrameRateValue->asString();
else if (outputFrameRateValue->isNumber())
{
std::ostringstream stream;
stream << outputFrameRateValue->asNumber();
mConfig.outputFrameRate = stream.str();
}
}
return true;
}
std::string RuntimeConfigStore::ReadTextFile(const std::filesystem::path& path, std::string& error) const
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
error = "Could not open file: " + path.string();
return std::string();
}
std::ostringstream buffer;
buffer << input.rdbuf();
return buffer.str();
}
void RuntimeConfigStore::RefreshConfigDependentPaths()
{
mShaderRoot = mRepoRoot / mConfig.shaderLibrary;
mWrapperPath = mRuntimeRoot / "shader_cache" / "active_shader_wrapper.slang";
mGeneratedGlslPath = mRuntimeRoot / "shader_cache" / "active_shader.raw.frag";
mPatchedGlslPath = mRuntimeRoot / "shader_cache" / "active_shader.frag";
}

View File

@@ -0,0 +1,59 @@
#pragma once
#include <filesystem>
#include <string>
class RuntimeConfigStore
{
public:
struct AppConfig
{
std::string shaderLibrary = "shaders";
unsigned short serverPort = 8080;
unsigned short oscPort = 9000;
std::string oscBindAddress = "127.0.0.1";
double oscSmoothing = 0.18;
bool autoReload = true;
unsigned maxTemporalHistoryFrames = 4;
unsigned previewFps = 30;
bool enableExternalKeying = false;
std::string inputVideoFormat = "1080p";
std::string inputFrameRate = "59.94";
std::string outputVideoFormat = "1080p";
std::string outputFrameRate = "59.94";
};
bool Initialize(std::string& error);
const AppConfig& GetConfig() const;
const std::filesystem::path& GetRepoRoot() const;
const std::filesystem::path& GetUiRoot() const;
const std::filesystem::path& GetDocsRoot() const;
const std::filesystem::path& GetShaderRoot() const;
const std::filesystem::path& GetRuntimeRoot() const;
const std::filesystem::path& GetPresetRoot() const;
const std::filesystem::path& GetRuntimeStatePath() const;
const std::filesystem::path& GetWrapperPath() const;
const std::filesystem::path& GetGeneratedGlslPath() const;
const std::filesystem::path& GetPatchedGlslPath() const;
void SetBoundControlServerPort(unsigned short port);
private:
bool ResolvePaths(std::string& error);
bool LoadConfig(std::string& error);
std::string ReadTextFile(const std::filesystem::path& path, std::string& error) const;
void RefreshConfigDependentPaths();
AppConfig mConfig;
std::filesystem::path mRepoRoot;
std::filesystem::path mUiRoot;
std::filesystem::path mDocsRoot;
std::filesystem::path mShaderRoot;
std::filesystem::path mRuntimeRoot;
std::filesystem::path mPresetRoot;
std::filesystem::path mRuntimeStatePath;
std::filesystem::path mConfigPath;
std::filesystem::path mWrapperPath;
std::filesystem::path mGeneratedGlslPath;
std::filesystem::path mPatchedGlslPath;
};

View File

@@ -0,0 +1,586 @@
#include "RuntimeStore.h"
#include "RuntimeStatePresenter.h"
#include <cctype>
#include <fstream>
#include <mutex>
#include <random>
#include <sstream>
#include <windows.h>
namespace
{
std::string ToLowerCopy(std::string text)
{
std::transform(text.begin(), text.end(), text.begin(),
[](unsigned char ch) { return static_cast<char>(std::tolower(ch)); });
return text;
}
double GenerateStartupRandom()
{
std::random_device randomDevice;
std::uniform_real_distribution<double> distribution(0.0, 1.0);
return distribution(randomDevice);
}
}
RuntimeStore::RuntimeStore() :
mRenderSnapshotBuilder(*this),
mHealthTelemetry(),
mReloadRequested(false),
mCompileSucceeded(false),
mStartupRandom(GenerateStartupRandom()),
mServerPort(8080),
mAutoReloadEnabled(true),
mStartTime(std::chrono::steady_clock::now()),
mLastScanTime((std::chrono::steady_clock::time_point::min)())
{
}
HealthTelemetry& RuntimeStore::GetHealthTelemetry()
{
return mHealthTelemetry;
}
const HealthTelemetry& RuntimeStore::GetHealthTelemetry() const
{
return mHealthTelemetry;
}
RenderSnapshotBuilder& RuntimeStore::GetRenderSnapshotBuilder()
{
return mRenderSnapshotBuilder;
}
const RenderSnapshotBuilder& RuntimeStore::GetRenderSnapshotBuilder() const
{
return mRenderSnapshotBuilder;
}
bool RuntimeStore::InitializeStore(std::string& error)
{
try
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mConfigStore.Initialize(error))
return false;
if (!LoadPersistentState(error))
return false;
if (!ScanShaderPackages(error))
return false;
mLayerStack.NormalizeLayerIds();
mLayerStack.EnsureDefaultsForAllLayers(mShaderCatalog);
mLayerStack.EnsureDefaultLayer(mShaderCatalog);
mServerPort = mConfigStore.GetConfig().serverPort;
mAutoReloadEnabled = mConfigStore.GetConfig().autoReload;
mReloadRequested = true;
mCompileMessage = "Waiting for shader compile.";
return true;
}
catch (const std::exception& exception)
{
error = std::string("RuntimeStore::InitializeStore exception: ") + exception.what();
return false;
}
catch (...)
{
error = "RuntimeStore::InitializeStore threw a non-standard exception.";
return false;
}
}
std::string RuntimeStore::BuildPersistentStateJson() const
{
return RuntimeStatePresenter::BuildRuntimeStateJson(*this);
}
bool RuntimeStore::PollStoredFileChanges(bool& registryChanged, bool& reloadRequested, std::string& error)
{
try
{
std::lock_guard<std::mutex> lock(mMutex);
registryChanged = false;
reloadRequested = false;
if (!mAutoReloadEnabled)
{
reloadRequested = mReloadRequested;
return true;
}
const auto now = std::chrono::steady_clock::now();
if (mLastScanTime != (std::chrono::steady_clock::time_point::min)() &&
std::chrono::duration_cast<std::chrono::milliseconds>(now - mLastScanTime).count() < 250)
{
reloadRequested = mReloadRequested;
return true;
}
mLastScanTime = now;
std::string scanError;
const ShaderPackageCatalog::Snapshot previousCatalog = mShaderCatalog.CaptureSnapshot();
if (!ScanShaderPackages(scanError))
{
error = scanError;
return false;
}
registryChanged = mShaderCatalog.HasCatalogChangedSince(previousCatalog);
mLayerStack.EnsureDefaultsForAllLayers(mShaderCatalog);
for (RuntimeStore::LayerPersistentState& layer : mLayerStack.Layers())
{
const ShaderPackage* active = mShaderCatalog.FindPackage(layer.shaderId);
if (!active)
continue;
if (mShaderCatalog.HasPackageChangedSince(previousCatalog, layer.shaderId))
mReloadRequested = true;
}
reloadRequested = mReloadRequested;
if (registryChanged || reloadRequested)
MarkRenderStateDirtyLocked();
return true;
}
catch (const std::exception& exception)
{
error = std::string("RuntimeStore::PollStoredFileChanges exception: ") + exception.what();
return false;
}
catch (...)
{
error = "RuntimeStore::PollStoredFileChanges threw a non-standard exception.";
return false;
}
}
bool RuntimeStore::CreateStoredLayer(const std::string& shaderId, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.CreateLayer(mShaderCatalog, shaderId, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::DeleteStoredLayer(const std::string& layerId, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.DeleteLayer(layerId, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::MoveStoredLayer(const std::string& layerId, int direction, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
bool shouldMove = false;
if (!mLayerStack.ResolveLayerMove(layerId, direction, shouldMove, error))
return false;
if (!shouldMove)
return true;
if (!mLayerStack.MoveLayer(layerId, direction, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::MoveStoredLayerToIndex(const std::string& layerId, std::size_t targetIndex, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
bool shouldMove = false;
if (!mLayerStack.ResolveLayerMoveToIndex(layerId, targetIndex, shouldMove, error))
return false;
if (!shouldMove)
return true;
if (!mLayerStack.MoveLayerToIndex(layerId, targetIndex, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::SetStoredLayerBypassState(const std::string& layerId, bool bypassed, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.SetLayerBypassState(layerId, bypassed, error))
return false;
mReloadRequested = true;
MarkParameterStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::SetStoredLayerShaderSelection(const std::string& layerId, const std::string& shaderId, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.SetLayerShaderSelection(mShaderCatalog, layerId, shaderId, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::SetStoredParameterValue(const std::string& layerId, const std::string& parameterId, const ShaderParameterValue& value, bool persistState, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.SetParameterValue(layerId, parameterId, value, error))
return false;
MarkParameterStateDirtyLocked();
return !persistState || SavePersistentState(error);
}
bool RuntimeStore::ResetStoredLayerParameterValues(const std::string& layerId, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
if (!mLayerStack.ResetLayerParameterValues(mShaderCatalog, layerId, error))
return false;
MarkParameterStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::SaveStackPresetSnapshot(const std::string& presetName, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
const std::string safeStem = LayerStackStore::MakeSafePresetFileStem(presetName);
if (safeStem.empty())
{
error = "Preset name must include at least one letter or number.";
return false;
}
JsonValue root = JsonValue::MakeObject();
root = mLayerStack.BuildStackPresetValue(mShaderCatalog, presetName);
return WriteTextFile(mConfigStore.GetPresetRoot() / (safeStem + ".json"), SerializeJson(root, true), error);
}
bool RuntimeStore::LoadStackPresetSnapshot(const std::string& presetName, std::string& error)
{
std::lock_guard<std::mutex> lock(mMutex);
const std::string safeStem = LayerStackStore::MakeSafePresetFileStem(presetName);
if (safeStem.empty())
{
error = "Preset name must include at least one letter or number.";
return false;
}
const std::filesystem::path presetPath = mConfigStore.GetPresetRoot() / (safeStem + ".json");
std::string presetText = ReadTextFile(presetPath, error);
if (presetText.empty())
return false;
JsonValue root;
if (!ParseJson(presetText, root, error))
return false;
if (!mLayerStack.LoadStackPresetValue(mShaderCatalog, root, error))
return false;
mReloadRequested = true;
MarkRenderStateDirtyLocked();
return SavePersistentState(error);
}
bool RuntimeStore::HasStoredLayer(const std::string& layerId) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mLayerStack.HasLayer(layerId);
}
bool RuntimeStore::HasStoredShader(const std::string& shaderId) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mShaderCatalog.HasPackage(shaderId);
}
bool RuntimeStore::TryGetStoredParameterById(const std::string& layerId, const std::string& parameterId, StoredParameterSnapshot& snapshot, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mLayerStack.TryGetParameterById(mShaderCatalog, layerId, parameterId, snapshot, error);
}
bool RuntimeStore::TryGetStoredParameterByControlKey(const std::string& layerKey, const std::string& parameterKey, StoredParameterSnapshot& snapshot, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mLayerStack.TryGetParameterByControlKey(mShaderCatalog, layerKey, parameterKey, snapshot, error);
}
bool RuntimeStore::ResolveStoredLayerMove(const std::string& layerId, int direction, bool& shouldMove, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mLayerStack.ResolveLayerMove(layerId, direction, shouldMove, error);
}
bool RuntimeStore::ResolveStoredLayerMoveToIndex(const std::string& layerId, std::size_t targetIndex, bool& shouldMove, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mLayerStack.ResolveLayerMoveToIndex(layerId, targetIndex, shouldMove, error);
}
bool RuntimeStore::IsValidStackPresetName(const std::string& presetName) const
{
return !LayerStackStore::MakeSafePresetFileStem(presetName).empty();
}
double RuntimeStore::GetRuntimeElapsedSeconds() const
{
return std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::steady_clock::now() - mStartTime).count();
}
const std::filesystem::path& RuntimeStore::GetRuntimeRepositoryRoot() const
{
return mConfigStore.GetRepoRoot();
}
const std::filesystem::path& RuntimeStore::GetRuntimeUiRoot() const
{
return mConfigStore.GetUiRoot();
}
const std::filesystem::path& RuntimeStore::GetRuntimeDocsRoot() const
{
return mConfigStore.GetDocsRoot();
}
const std::filesystem::path& RuntimeStore::GetRuntimeDataRoot() const
{
return mConfigStore.GetRuntimeRoot();
}
unsigned short RuntimeStore::GetConfiguredControlServerPort() const
{
return mServerPort;
}
unsigned short RuntimeStore::GetConfiguredOscPort() const
{
return mConfigStore.GetConfig().oscPort;
}
const std::string& RuntimeStore::GetConfiguredOscBindAddress() const
{
return mConfigStore.GetConfig().oscBindAddress;
}
double RuntimeStore::GetConfiguredOscSmoothing() const
{
return mConfigStore.GetConfig().oscSmoothing;
}
unsigned RuntimeStore::GetConfiguredMaxTemporalHistoryFrames() const
{
return mConfigStore.GetConfig().maxTemporalHistoryFrames;
}
unsigned RuntimeStore::GetConfiguredPreviewFps() const
{
return mConfigStore.GetConfig().previewFps;
}
bool RuntimeStore::IsExternalKeyingConfigured() const
{
return mConfigStore.GetConfig().enableExternalKeying;
}
const std::string& RuntimeStore::GetConfiguredInputVideoFormat() const
{
return mConfigStore.GetConfig().inputVideoFormat;
}
const std::string& RuntimeStore::GetConfiguredInputFrameRate() const
{
return mConfigStore.GetConfig().inputFrameRate;
}
const std::string& RuntimeStore::GetConfiguredOutputVideoFormat() const
{
return mConfigStore.GetConfig().outputVideoFormat;
}
const std::string& RuntimeStore::GetConfiguredOutputFrameRate() const
{
return mConfigStore.GetConfig().outputFrameRate;
}
void RuntimeStore::SetBoundControlServerPort(unsigned short port)
{
std::lock_guard<std::mutex> lock(mMutex);
mServerPort = port;
mConfigStore.SetBoundControlServerPort(port);
}
void RuntimeStore::SetCompileStatus(bool succeeded, const std::string& message)
{
std::lock_guard<std::mutex> lock(mMutex);
mCompileSucceeded = succeeded;
mCompileMessage = message;
}
void RuntimeStore::ClearReloadRequest()
{
std::lock_guard<std::mutex> lock(mMutex);
mReloadRequested = false;
}
bool RuntimeStore::LoadPersistentState(std::string& error)
{
if (!std::filesystem::exists(mConfigStore.GetRuntimeStatePath()))
return true;
std::string stateText = ReadTextFile(mConfigStore.GetRuntimeStatePath(), error);
if (stateText.empty())
return false;
JsonValue root;
if (!ParseJson(stateText, root, error))
return false;
return mLayerStack.LoadPersistentStateValue(root);
}
bool RuntimeStore::SavePersistentState(std::string& error) const
{
return WriteTextFile(mConfigStore.GetRuntimeStatePath(), SerializeJson(mLayerStack.BuildPersistentStateValue(mShaderCatalog), true), error);
}
bool RuntimeStore::ScanShaderPackages(std::string& error)
{
if (!mShaderCatalog.Scan(mConfigStore.GetShaderRoot(), mConfigStore.GetConfig().maxTemporalHistoryFrames, error))
return false;
mLayerStack.RemoveLayersWithMissingPackages(mShaderCatalog);
MarkRenderStateDirtyLocked();
return true;
}
std::string RuntimeStore::ReadTextFile(const std::filesystem::path& path, std::string& error) const
{
std::ifstream input(path, std::ios::binary);
if (!input)
{
error = "Could not open file: " + path.string();
return std::string();
}
std::ostringstream buffer;
buffer << input.rdbuf();
return buffer.str();
}
bool RuntimeStore::WriteTextFile(const std::filesystem::path& path, const std::string& contents, std::string& error) const
{
std::error_code fsError;
std::filesystem::create_directories(path.parent_path(), fsError);
const std::filesystem::path temporaryPath = path.string() + ".tmp";
std::ofstream output(temporaryPath, std::ios::binary | std::ios::trunc);
if (!output)
{
error = "Could not write file: " + temporaryPath.string();
return false;
}
output << contents;
output.close();
if (!output.good())
{
error = "Could not finish writing file: " + temporaryPath.string();
return false;
}
if (!MoveFileExA(temporaryPath.string().c_str(), path.string().c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
{
const DWORD lastError = GetLastError();
std::filesystem::remove(temporaryPath, fsError);
error = "Could not replace file: " + path.string() + " (Win32 error " + std::to_string(lastError) + ")";
return false;
}
return true;
}
std::vector<std::string> RuntimeStore::GetStackPresetNamesLocked() const
{
std::vector<std::string> presetNames;
std::error_code fsError;
if (!std::filesystem::exists(mConfigStore.GetPresetRoot(), fsError))
return presetNames;
for (const auto& entry : std::filesystem::directory_iterator(mConfigStore.GetPresetRoot(), fsError))
{
if (!entry.is_regular_file())
continue;
if (ToLowerCopy(entry.path().extension().string()) != ".json")
continue;
presetNames.push_back(entry.path().stem().string());
}
std::sort(presetNames.begin(), presetNames.end());
return presetNames;
}
bool RuntimeStore::CopyShaderPackageForStoredLayer(const std::string& layerId, ShaderPackage& shaderPackage, std::string& error) const
{
std::lock_guard<std::mutex> lock(mMutex);
const RuntimeStore::LayerPersistentState* layer = mLayerStack.FindLayerById(layerId);
if (!layer)
{
error = "Unknown layer id: " + layerId;
return false;
}
if (!mShaderCatalog.CopyPackage(layer->shaderId, shaderPackage))
{
error = "Unknown shader id: " + layer->shaderId;
return false;
}
return true;
}
void RuntimeStore::GetShaderCompilerInputs(std::filesystem::path& repoRoot, std::filesystem::path& wrapperPath,
std::filesystem::path& generatedGlslPath, std::filesystem::path& patchedGlslPath, unsigned& maxTemporalHistoryFrames) const
{
std::lock_guard<std::mutex> lock(mMutex);
repoRoot = mConfigStore.GetRepoRoot();
wrapperPath = mConfigStore.GetWrapperPath();
generatedGlslPath = mConfigStore.GetGeneratedGlslPath();
patchedGlslPath = mConfigStore.GetPatchedGlslPath();
maxTemporalHistoryFrames = mConfigStore.GetConfig().maxTemporalHistoryFrames;
}
void RuntimeStore::MarkRenderStateDirtyLocked()
{
mRenderSnapshotBuilder.MarkRenderStateDirty();
}
void RuntimeStore::MarkParameterStateDirtyLocked()
{
mRenderSnapshotBuilder.MarkParameterStateDirty();
}

View File

@@ -0,0 +1,103 @@
#pragma once
#include "HealthTelemetry.h"
#include "LayerStackStore.h"
#include "RenderSnapshotBuilder.h"
#include "RuntimeConfigStore.h"
#include "RuntimeJson.h"
#include "ShaderPackageCatalog.h"
#include "ShaderTypes.h"
#include <chrono>
#include <filesystem>
#include <mutex>
#include <string>
#include <vector>
class RuntimeStatePresenter;
class RuntimeStore
{
public:
using StoredParameterSnapshot = LayerStackStore::StoredParameterSnapshot;
using LayerPersistentState = LayerStackStore::LayerPersistentState;
RuntimeStore();
HealthTelemetry& GetHealthTelemetry();
const HealthTelemetry& GetHealthTelemetry() const;
RenderSnapshotBuilder& GetRenderSnapshotBuilder();
const RenderSnapshotBuilder& GetRenderSnapshotBuilder() const;
bool InitializeStore(std::string& error);
std::string BuildPersistentStateJson() const;
bool PollStoredFileChanges(bool& registryChanged, bool& reloadRequested, std::string& error);
bool CreateStoredLayer(const std::string& shaderId, std::string& error);
bool DeleteStoredLayer(const std::string& layerId, std::string& error);
bool MoveStoredLayer(const std::string& layerId, int direction, std::string& error);
bool MoveStoredLayerToIndex(const std::string& layerId, std::size_t targetIndex, std::string& error);
bool SetStoredLayerBypassState(const std::string& layerId, bool bypassed, std::string& error);
bool SetStoredLayerShaderSelection(const std::string& layerId, const std::string& shaderId, std::string& error);
bool SetStoredParameterValue(const std::string& layerId, const std::string& parameterId, const ShaderParameterValue& value, bool persistState, std::string& error);
bool ResetStoredLayerParameterValues(const std::string& layerId, std::string& error);
bool SaveStackPresetSnapshot(const std::string& presetName, std::string& error) const;
bool LoadStackPresetSnapshot(const std::string& presetName, std::string& error);
bool HasStoredLayer(const std::string& layerId) const;
bool HasStoredShader(const std::string& shaderId) const;
bool TryGetStoredParameterById(const std::string& layerId, const std::string& parameterId, StoredParameterSnapshot& snapshot, std::string& error) const;
bool TryGetStoredParameterByControlKey(const std::string& layerKey, const std::string& parameterKey, StoredParameterSnapshot& snapshot, std::string& error) const;
bool ResolveStoredLayerMove(const std::string& layerId, int direction, bool& shouldMove, std::string& error) const;
bool ResolveStoredLayerMoveToIndex(const std::string& layerId, std::size_t targetIndex, bool& shouldMove, std::string& error) const;
bool IsValidStackPresetName(const std::string& presetName) const;
double GetRuntimeElapsedSeconds() const;
const std::filesystem::path& GetRuntimeRepositoryRoot() const;
const std::filesystem::path& GetRuntimeUiRoot() const;
const std::filesystem::path& GetRuntimeDocsRoot() const;
const std::filesystem::path& GetRuntimeDataRoot() const;
unsigned short GetConfiguredControlServerPort() const;
unsigned short GetConfiguredOscPort() const;
const std::string& GetConfiguredOscBindAddress() const;
double GetConfiguredOscSmoothing() const;
unsigned GetConfiguredMaxTemporalHistoryFrames() const;
unsigned GetConfiguredPreviewFps() const;
bool IsExternalKeyingConfigured() const;
const std::string& GetConfiguredInputVideoFormat() const;
const std::string& GetConfiguredInputFrameRate() const;
const std::string& GetConfiguredOutputVideoFormat() const;
const std::string& GetConfiguredOutputFrameRate() const;
void SetBoundControlServerPort(unsigned short port);
void SetCompileStatus(bool succeeded, const std::string& message);
void ClearReloadRequest();
private:
friend class RenderSnapshotBuilder;
friend class RuntimeStatePresenter;
bool LoadPersistentState(std::string& error);
bool SavePersistentState(std::string& error) const;
bool ScanShaderPackages(std::string& error);
std::string ReadTextFile(const std::filesystem::path& path, std::string& error) const;
bool WriteTextFile(const std::filesystem::path& path, const std::string& contents, std::string& error) const;
std::vector<std::string> GetStackPresetNamesLocked() const;
bool CopyShaderPackageForStoredLayer(const std::string& layerId, ShaderPackage& shaderPackage, std::string& error) const;
void GetShaderCompilerInputs(std::filesystem::path& repoRoot, std::filesystem::path& wrapperPath,
std::filesystem::path& generatedGlslPath, std::filesystem::path& patchedGlslPath, unsigned& maxTemporalHistoryFrames) const;
void MarkRenderStateDirtyLocked();
void MarkParameterStateDirtyLocked();
RenderSnapshotBuilder mRenderSnapshotBuilder;
RuntimeConfigStore mConfigStore;
ShaderPackageCatalog mShaderCatalog;
LayerStackStore mLayerStack;
HealthTelemetry mHealthTelemetry;
mutable std::mutex mMutex;
bool mReloadRequested;
bool mCompileSucceeded;
std::string mCompileMessage;
double mStartupRandom;
unsigned short mServerPort;
bool mAutoReloadEnabled;
std::chrono::steady_clock::time_point mStartTime;
std::chrono::steady_clock::time_point mLastScanTime;
};

View File

@@ -0,0 +1,127 @@
#include "ShaderPackageCatalog.h"
#include "ShaderPackageRegistry.h"
bool ShaderPackageCatalog::Scan(const std::filesystem::path& shaderRoot, unsigned maxTemporalHistoryFrames, std::string& error)
{
std::map<std::string, ShaderPackage> packagesById;
std::vector<std::string> packageOrder;
std::vector<ShaderPackageStatus> packageStatuses;
ShaderPackageRegistry registry(maxTemporalHistoryFrames);
if (!registry.Scan(shaderRoot, packagesById, packageOrder, packageStatuses, error))
return false;
mPackagesById.swap(packagesById);
mPackageOrder.swap(packageOrder);
mPackageStatuses.swap(packageStatuses);
return true;
}
ShaderPackageCatalog::Snapshot ShaderPackageCatalog::CaptureSnapshot() const
{
Snapshot snapshot;
snapshot.packagesById = mPackagesById;
snapshot.packageOrder = mPackageOrder;
return snapshot;
}
bool ShaderPackageCatalog::HasCatalogChangedSince(const Snapshot& snapshot) const
{
if (snapshot.packageOrder != mPackageOrder || snapshot.packagesById.size() != mPackagesById.size())
return true;
for (const auto& item : mPackagesById)
{
auto previous = snapshot.packagesById.find(item.first);
if (previous == snapshot.packagesById.end() || !PackagesEquivalent(previous->second, item.second))
return true;
}
return false;
}
bool ShaderPackageCatalog::HasPackageChangedSince(const Snapshot& snapshot, const std::string& shaderId) const
{
auto previous = snapshot.packagesById.find(shaderId);
auto current = mPackagesById.find(shaderId);
if (previous == snapshot.packagesById.end() || current == mPackagesById.end())
return previous != snapshot.packagesById.end() || current != mPackagesById.end();
return !PackagesEquivalent(previous->second, current->second);
}
bool ShaderPackageCatalog::HasPackage(const std::string& shaderId) const
{
return mPackagesById.find(shaderId) != mPackagesById.end();
}
const ShaderPackage* ShaderPackageCatalog::FindPackage(const std::string& shaderId) const
{
auto it = mPackagesById.find(shaderId);
return it == mPackagesById.end() ? nullptr : &it->second;
}
bool ShaderPackageCatalog::CopyPackage(const std::string& shaderId, ShaderPackage& shaderPackage) const
{
const ShaderPackage* package = FindPackage(shaderId);
if (!package)
return false;
shaderPackage = *package;
return true;
}
const std::vector<std::string>& ShaderPackageCatalog::PackageOrder() const
{
return mPackageOrder;
}
const std::vector<ShaderPackageStatus>& ShaderPackageCatalog::PackageStatuses() const
{
return mPackageStatuses;
}
bool ShaderPackageCatalog::PackagesEquivalent(const ShaderPackage& left, const ShaderPackage& right)
{
return left.shaderWriteTime == right.shaderWriteTime &&
left.manifestWriteTime == right.manifestWriteTime &&
TextureAssetsEqual(left.textureAssets, right.textureAssets) &&
FontAssetsEqual(left.fontAssets, right.fontAssets);
}
bool ShaderPackageCatalog::TextureAssetsEqual(const std::vector<ShaderTextureAsset>& left, const std::vector<ShaderTextureAsset>& right)
{
if (left.size() != right.size())
return false;
for (std::size_t index = 0; index < left.size(); ++index)
{
if (left[index].id != right[index].id ||
left[index].path != right[index].path ||
left[index].writeTime != right[index].writeTime)
{
return false;
}
}
return true;
}
bool ShaderPackageCatalog::FontAssetsEqual(const std::vector<ShaderFontAsset>& left, const std::vector<ShaderFontAsset>& right)
{
if (left.size() != right.size())
return false;
for (std::size_t index = 0; index < left.size(); ++index)
{
if (left[index].id != right[index].id ||
left[index].path != right[index].path ||
left[index].writeTime != right[index].writeTime)
{
return false;
}
}
return true;
}

View File

@@ -0,0 +1,37 @@
#pragma once
#include "ShaderTypes.h"
#include <filesystem>
#include <map>
#include <string>
#include <vector>
class ShaderPackageCatalog
{
public:
struct Snapshot
{
std::map<std::string, ShaderPackage> packagesById;
std::vector<std::string> packageOrder;
};
bool Scan(const std::filesystem::path& shaderRoot, unsigned maxTemporalHistoryFrames, std::string& error);
Snapshot CaptureSnapshot() const;
bool HasCatalogChangedSince(const Snapshot& snapshot) const;
bool HasPackageChangedSince(const Snapshot& snapshot, const std::string& shaderId) const;
bool HasPackage(const std::string& shaderId) const;
const ShaderPackage* FindPackage(const std::string& shaderId) const;
bool CopyPackage(const std::string& shaderId, ShaderPackage& shaderPackage) const;
const std::vector<std::string>& PackageOrder() const;
const std::vector<ShaderPackageStatus>& PackageStatuses() const;
private:
static bool PackagesEquivalent(const ShaderPackage& left, const ShaderPackage& right);
static bool TextureAssetsEqual(const std::vector<ShaderTextureAsset>& left, const std::vector<ShaderTextureAsset>& right);
static bool FontAssetsEqual(const std::vector<ShaderFontAsset>& left, const std::vector<ShaderFontAsset>& right);
std::map<std::string, ShaderPackage> mPackagesById;
std::vector<std::string> mPackageOrder;
std::vector<ShaderPackageStatus> mPackageStatuses;
};