VideoCommon: add function to serialize ShaderAsset to json

This commit is contained in:
iwubcode 2024-01-23 01:05:16 -06:00
parent d1b4c5482c
commit b5a6225e1a
5 changed files with 104 additions and 0 deletions

View File

@ -86,6 +86,7 @@ add_library(common
IOFile.h
JitRegister.cpp
JitRegister.h
JsonUtil.h
Lazy.h
LinearDiskCache.h
Logging/ConsoleListener.h

View File

@ -0,0 +1,26 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <span>
#include <picojson.h>
// Ideally this would use a concept like, 'template <std::ranges::range Range>' to constrain it,
// but unfortunately we'd need to require clang 15 for that, since the ranges library isn't
// fully implemented until then, but this should suffice.
template <typename Range>
picojson::array ToJsonArray(const Range& data)
{
picojson::array result;
result.reserve(std::size(data));
for (const auto& value : data)
{
result.emplace_back(static_cast<double>(value));
}
return result;
}

View File

@ -125,6 +125,7 @@
<ClInclude Include="Common\Intrinsics.h" />
<ClInclude Include="Common\IOFile.h" />
<ClInclude Include="Common\JitRegister.h" />
<ClInclude Include="Common\JsonUtil.h" />
<ClInclude Include="Common\Lazy.h" />
<ClInclude Include="Common\LdrWatcher.h" />
<ClInclude Include="Common\LinearDiskCache.h" />

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <utility>
#include "Common/JsonUtil.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "Common/VariantUtil.h"
@ -291,6 +292,80 @@ bool PixelShaderData::FromJson(const VideoCommon::CustomAssetLibrary::AssetID& a
return true;
}
void PixelShaderData::ToJson(picojson::object& obj, const PixelShaderData& data)
{
picojson::array json_properties;
for (const auto& [name, property] : data.m_properties)
{
picojson::object json_property;
json_property["code_name"] = picojson::value{name};
json_property["description"] = picojson::value{property.m_description};
std::visit(
overloaded{[&](const ShaderProperty::Sampler2D& default_value) {
json_property["type"] = picojson::value{"sampler2d"};
json_property["default"] = picojson::value{default_value.value};
},
[&](const ShaderProperty::Sampler2DArray& default_value) {
json_property["type"] = picojson::value{"sampler2darray"};
json_property["default"] = picojson::value{default_value.value};
},
[&](const ShaderProperty::SamplerCube& default_value) {
json_property["type"] = picojson::value{"samplercube"};
json_property["default"] = picojson::value{default_value.value};
},
[&](s32 default_value) {
json_property["type"] = picojson::value{"int"};
json_property["default"] = picojson::value{static_cast<double>(default_value)};
},
[&](const std::array<s32, 2>& default_value) {
json_property["type"] = picojson::value{"int2"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](const std::array<s32, 3>& default_value) {
json_property["type"] = picojson::value{"int3"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](const std::array<s32, 4>& default_value) {
json_property["type"] = picojson::value{"int4"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](float default_value) {
json_property["type"] = picojson::value{"float"};
json_property["default"] = picojson::value{static_cast<double>(default_value)};
},
[&](const std::array<float, 2>& default_value) {
json_property["type"] = picojson::value{"float2"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](const std::array<float, 3>& default_value) {
json_property["type"] = picojson::value{"float3"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](const std::array<float, 4>& default_value) {
json_property["type"] = picojson::value{"float4"};
json_property["default"] = picojson::value{ToJsonArray(default_value)};
},
[&](const ShaderProperty::RGB& default_value) {
json_property["type"] = picojson::value{"rgb"};
json_property["default"] = picojson::value{ToJsonArray(default_value.value)};
},
[&](const ShaderProperty::RGBA& default_value) {
json_property["type"] = picojson::value{"rgba"};
json_property["default"] = picojson::value{ToJsonArray(default_value.value)};
},
[&](bool default_value) {
json_property["type"] = picojson::value{"bool"};
json_property["default"] = picojson::value{default_value};
}},
property.m_default);
json_properties.push_back(picojson::value{json_property});
}
obj["properties"] = picojson::value{json_properties};
}
CustomAssetLibrary::LoadInfo PixelShaderAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id)
{
auto potential_data = std::make_shared<PixelShaderData>();

View File

@ -52,6 +52,7 @@ struct PixelShaderData
{
static bool FromJson(const CustomAssetLibrary::AssetID& asset_id, const picojson::object& json,
PixelShaderData* data);
static void ToJson(picojson::object& obj, const PixelShaderData& data);
// These shader properties describe the input that the
// shader expects to expose. The key is text