diff options
| author | iwubcode <iwubcode@users.noreply.github.com> | 2023-08-24 00:35:31 -0500 |
|---|---|---|
| committer | iwubcode <iwubcode@users.noreply.github.com> | 2023-08-24 00:35:31 -0500 |
| commit | 7c52a524407aa8055938ec68efe135bf4162db34 (patch) | |
| tree | 922defd1dcad9c716e68502c3df3b5a6bd11aff7 /Source/Core/VideoCommon/Assets/TextureAsset.cpp | |
| parent | 260bad74eac728c3c64a1a2e8b00efc7d9bede92 (diff) | |
VideoCommon: add TextureData structure that contains the raw texture data, a sampler, and the type of texture information
Diffstat (limited to 'Source/Core/VideoCommon/Assets/TextureAsset.cpp')
| -rw-r--r-- | Source/Core/VideoCommon/Assets/TextureAsset.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/Source/Core/VideoCommon/Assets/TextureAsset.cpp b/Source/Core/VideoCommon/Assets/TextureAsset.cpp index 3bf477a58a..759703a8a5 100644 --- a/Source/Core/VideoCommon/Assets/TextureAsset.cpp +++ b/Source/Core/VideoCommon/Assets/TextureAsset.cpp @@ -4,9 +4,99 @@ #include "VideoCommon/Assets/TextureAsset.h" #include "Common/Logging/Log.h" +#include "VideoCommon/BPMemory.h" namespace VideoCommon { +namespace +{ +bool ParseSampler(const VideoCommon::CustomAssetLibrary::AssetID& asset_id, + const picojson::object& json, SamplerState* sampler) +{ + if (!sampler) [[unlikely]] + return false; + + *sampler = RenderState::GetLinearSamplerState(); + + const auto sampler_state_mode_iter = json.find("texture_mode"); + if (sampler_state_mode_iter == json.end()) + { + ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'texture_mode' not found", asset_id); + return false; + } + if (!sampler_state_mode_iter->second.is<std::string>()) + { + ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'texture_mode' is not the right type", + asset_id); + return false; + } + std::string sampler_state_mode = sampler_state_mode_iter->second.to_str(); + std::transform(sampler_state_mode.begin(), sampler_state_mode.end(), sampler_state_mode.begin(), + [](unsigned char c) { return std::tolower(c); }); + + if (sampler_state_mode == "clamp") + { + sampler->tm0.wrap_u = WrapMode::Clamp; + sampler->tm0.wrap_v = WrapMode::Clamp; + } + else if (sampler_state_mode == "repeat") + { + sampler->tm0.wrap_u = WrapMode::Repeat; + sampler->tm0.wrap_v = WrapMode::Repeat; + } + else if (sampler_state_mode == "mirrored_repeat") + { + sampler->tm0.wrap_u = WrapMode::Mirror; + sampler->tm0.wrap_v = WrapMode::Mirror; + } + else + { + ERROR_LOG_FMT(VIDEO, + "Asset '{}' failed to parse json, 'texture_mode' has an invalid " + "value '{}'", + asset_id, sampler_state_mode); + return false; + } + + const auto sampler_state_filter_iter = json.find("texture_filter"); + if (sampler_state_filter_iter == json.end()) + { + ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'texture_filter' not found", asset_id); + return false; + } + if (!sampler_state_filter_iter->second.is<std::string>()) + { + ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'texture_filter' is not the right type", + asset_id); + return false; + } + std::string sampler_state_filter = sampler_state_filter_iter->second.to_str(); + std::transform(sampler_state_filter.begin(), sampler_state_filter.end(), + sampler_state_filter.begin(), [](unsigned char c) { return std::tolower(c); }); + if (sampler_state_filter == "linear") + { + sampler->tm0.min_filter = FilterMode::Linear; + sampler->tm0.mag_filter = FilterMode::Linear; + sampler->tm0.mipmap_filter = FilterMode::Linear; + } + else if (sampler_state_filter == "point") + { + sampler->tm0.min_filter = FilterMode::Linear; + sampler->tm0.mag_filter = FilterMode::Linear; + sampler->tm0.mipmap_filter = FilterMode::Linear; + } + else + { + ERROR_LOG_FMT(VIDEO, + "Asset '{}' failed to parse json, 'texture_filter' has an invalid " + "value '{}'", + asset_id, sampler_state_filter); + return false; + } + + return true; +} +} // namespace CustomAssetLibrary::LoadInfo RawTextureAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) { auto potential_data = std::make_shared<CustomTextureData>(); @@ -21,6 +111,49 @@ CustomAssetLibrary::LoadInfo RawTextureAsset::LoadImpl(const CustomAssetLibrary: return loaded_info; } +bool TextureData::FromJson(const CustomAssetLibrary::AssetID& asset_id, + const picojson::object& json, TextureData* data) +{ + const auto type_iter = json.find("type"); + if (type_iter == json.end()) + { + ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, property entry 'type' not found", + asset_id); + return false; + } + if (!type_iter->second.is<std::string>()) + { + ERROR_LOG_FMT(VIDEO, + "Asset '{}' failed to parse json, property entry 'type' is not " + "the right json type", + asset_id); + return false; + } + std::string type = type_iter->second.to_str(); + std::transform(type.begin(), type.end(), type.begin(), + [](unsigned char c) { return std::tolower(c); }); + + if (type == "texture2d") + { + data->m_type = TextureData::Type::Type_Texture2D; + } + else + { + ERROR_LOG_FMT(VIDEO, + "Asset '{}' failed to parse json, texture type '{}' " + "an invalid option", + asset_id, type); + return false; + } + + if (!ParseSampler(asset_id, json, &data->m_sampler)) + { + return false; + } + + return true; +} + CustomAssetLibrary::LoadInfo GameTextureAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) { auto potential_data = std::make_shared<CustomTextureData>(); |
