From f510cc31697334a06ff71cc575d58e8c4f1363e7 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Sun, 21 Jul 2024 17:44:00 -0500 Subject: [PATCH] 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 --- .../Assets/TextureSamplerValue.cpp | 71 +++++++++++++++++++ .../VideoCommon/Assets/TextureSamplerValue.h | 34 +++++++++ 2 files changed, 105 insertions(+) create mode 100644 Source/Core/VideoCommon/Assets/TextureSamplerValue.cpp create mode 100644 Source/Core/VideoCommon/Assets/TextureSamplerValue.h diff --git a/Source/Core/VideoCommon/Assets/TextureSamplerValue.cpp b/Source/Core/VideoCommon/Assets/TextureSamplerValue.cpp new file mode 100644 index 0000000000..61c3468d54 --- /dev/null +++ b/Source/Core/VideoCommon/Assets/TextureSamplerValue.cpp @@ -0,0 +1,71 @@ +// Copyright 2024 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "VideoCommon/Assets/TextureSamplerValue.h" + +#include + +#include "Common/EnumUtils.h" +#include "Common/JsonUtil.h" +#include "Common/StringUtil.h" + +namespace VideoCommon +{ +namespace +{ +std::optional +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(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( + Common::ToUnderlying(data.camera_originating_draw_call)); + obj->emplace("camera_draw_call", camera_draw_call); +} +} // namespace VideoCommon diff --git a/Source/Core/VideoCommon/Assets/TextureSamplerValue.h b/Source/Core/VideoCommon/Assets/TextureSamplerValue.h new file mode 100644 index 0000000000..f6c2328c95 --- /dev/null +++ b/Source/Core/VideoCommon/Assets/TextureSamplerValue.h @@ -0,0 +1,34 @@ +// Copyright 2024 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include + +#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