Merge pull request #12515 from iwubcode/shader_asset_helper_functions

VideoCommon: add some helper functions for shader asset
This commit is contained in:
Mai 2024-01-23 13:21:00 -05:00 committed by GitHub
commit e12933f8dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 178 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,150 @@ 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};
}
std::span<const std::string_view> ShaderProperty::GetValueTypeNames()
{
static constexpr std::array<std::string_view, 14> values = {
"sampler2d", "sampler2darray", "samplercube", "int", "int2", "int3", "int4",
"float", "float2", "float3", "float4", "rgb", "rgba", "bool"};
return values;
}
ShaderProperty::Value ShaderProperty::GetDefaultValueFromTypeName(std::string_view name)
{
if (name == "sampler2d")
{
return Sampler2D{};
}
else if (name == "sampler2darray")
{
return Sampler2DArray{};
}
else if (name == "samplercube")
{
return SamplerCube{};
}
else if (name == "int")
{
return 0;
}
else if (name == "int2")
{
return std::array<s32, 2>{};
}
else if (name == "int3")
{
return std::array<s32, 3>{};
}
else if (name == "int4")
{
return std::array<s32, 4>{};
}
else if (name == "float")
{
return 0.0f;
}
else if (name == "float2")
{
return std::array<float, 2>{};
}
else if (name == "float3")
{
return std::array<float, 3>{};
}
else if (name == "float4")
{
return std::array<float, 4>{};
}
else if (name == "rgb")
{
return RGB{};
}
else if (name == "rgba")
{
return RGBA{};
}
else if (name == "bool")
{
return false;
}
return Value{};
}
CustomAssetLibrary::LoadInfo PixelShaderAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id)
{
auto potential_data = std::make_shared<PixelShaderData>();

View File

@ -5,7 +5,9 @@
#include <array>
#include <map>
#include <span>
#include <string>
#include <string_view>
#include <variant>
#include <picojson.h>
@ -44,6 +46,8 @@ struct ShaderProperty
using Value = std::variant<s32, std::array<s32, 2>, std::array<s32, 3>, std::array<s32, 4>, float,
std::array<float, 2>, std::array<float, 3>, std::array<float, 4>, bool,
RGB, RGBA, Sampler2D, Sampler2DArray, SamplerCube>;
static std::span<const std::string_view> GetValueTypeNames();
static Value GetDefaultValueFromTypeName(std::string_view name);
Value m_default;
std::string m_description;
@ -52,6 +56,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