VideoCommon: add structure for the texture value for materials, this value contains details on how to get the sampler (either pulled from the asset or using a game texture), as well as details about the camera if the texture is a render target

This commit is contained in:
iwubcode 2024-07-21 17:44:00 -05:00
commit f510cc3169
2 changed files with 105 additions and 0 deletions

View file

@ -0,0 +1,71 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoCommon/Assets/TextureSamplerValue.h"
#include <optional>
#include "Common/EnumUtils.h"
#include "Common/JsonUtil.h"
#include "Common/StringUtil.h"
namespace VideoCommon
{
namespace
{
std::optional<TextureSamplerValue::SamplerOrigin>
ReadSamplerOriginFromJSON(const picojson::object& json)
{
auto sampler_origin = ReadStringFromJson(json, "sampler_origin").value_or("");
Common::ToLower(&sampler_origin);
if (sampler_origin == "asset")
{
return TextureSamplerValue::SamplerOrigin::Asset;
}
else if (sampler_origin == "texture_hash")
{
return TextureSamplerValue::SamplerOrigin::TextureHash;
}
return std::nullopt;
}
} // namespace
std::string TextureSamplerValue::ToString(SamplerOrigin sampler_origin)
{
if (sampler_origin == SamplerOrigin::Asset)
return "asset";
return "texture_hash";
}
bool TextureSamplerValue::FromJson(const picojson::object& json, TextureSamplerValue* data)
{
data->asset = ReadStringFromJson(json, "asset").value_or("");
data->texture_hash = ReadStringFromJson(json, "texture_hash").value_or("");
data->sampler_origin =
ReadSamplerOriginFromJSON(json).value_or(TextureSamplerValue::SamplerOrigin::Asset);
// Render targets
data->is_render_target = ReadBoolFromJson(json, "is_render_target").value_or(false);
data->camera_originating_draw_call =
GraphicsModSystem::DrawCallID{ReadNumericFromJson<u64>(json, "camera_draw_call").value_or(0)};
return true;
}
void TextureSamplerValue::ToJson(picojson::object* obj, const TextureSamplerValue& data)
{
if (!obj) [[unlikely]]
return;
obj->emplace("asset", data.asset);
obj->emplace("texture_hash", data.texture_hash);
obj->emplace("sampler_origin", ToString(data.sampler_origin));
obj->emplace("is_render_target", data.is_render_target);
const auto camera_draw_call = static_cast<double>(
Common::ToUnderlying<GraphicsModSystem::DrawCallID>(data.camera_originating_draw_call));
obj->emplace("camera_draw_call", camera_draw_call);
}
} // namespace VideoCommon

View file

@ -0,0 +1,34 @@
// Copyright 2024 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <string>
#include <picojson.h>
#include "VideoCommon/Assets/CustomAssetLibrary.h"
#include "VideoCommon/GraphicsModSystem/Types.h"
namespace VideoCommon
{
struct TextureSamplerValue
{
CustomAssetLibrary::AssetID asset;
enum class SamplerOrigin
{
Asset,
TextureHash
};
static std::string ToString(SamplerOrigin sampler_origin);
SamplerOrigin sampler_origin = SamplerOrigin::Asset;
std::string texture_hash;
bool is_render_target = false;
GraphicsModSystem::DrawCallID camera_originating_draw_call;
static bool FromJson(const picojson::object& json, TextureSamplerValue* data);
static void ToJson(picojson::object* obj, const TextureSamplerValue& data);
};
} // namespace VideoCommon