Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #12024 from iwubcode/material_asset
VideoCommon: add material asset
- Loading branch information
Showing
9 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // Copyright 2023 Dolphin Emulator Project | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "VideoCommon/Assets/MaterialAsset.h" | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "Common/Logging/Log.h" | ||
| #include "Common/StringUtil.h" | ||
| #include "VideoCommon/Assets/CustomAssetLibrary.h" | ||
|
|
||
| namespace VideoCommon | ||
| { | ||
| namespace | ||
| { | ||
| bool ParsePropertyValue(const CustomAssetLibrary::AssetID& asset_id, MaterialProperty::Type type, | ||
| const picojson::value& json_value, | ||
| std::optional<MaterialProperty::Value>* value) | ||
| { | ||
| switch (type) | ||
| { | ||
| case MaterialProperty::Type::Type_TextureAsset: | ||
| { | ||
| if (json_value.is<std::string>()) | ||
| { | ||
| *value = json_value.to_str(); | ||
| return true; | ||
| } | ||
| } | ||
| break; | ||
| }; | ||
|
|
||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value is not valid for type '{}'", | ||
| asset_id, type); | ||
| return false; | ||
| } | ||
|
|
||
| bool ParseMaterialProperties(const CustomAssetLibrary::AssetID& asset_id, | ||
| const picojson::array& values_data, | ||
| std::vector<MaterialProperty>* material_property) | ||
| { | ||
| for (const auto& value_data : values_data) | ||
| { | ||
| VideoCommon::MaterialProperty property; | ||
| if (!value_data.is<picojson::object>()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value is not the right json type", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| const auto& value_data_obj = value_data.get<picojson::object>(); | ||
|
|
||
| const auto type_iter = value_data_obj.find("type"); | ||
| if (type_iter == value_data_obj.end()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value entry 'type' not found", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| if (!type_iter->second.is<std::string>()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, | ||
| "Asset '{}' failed to parse the json, value entry 'type' is not " | ||
| "the right json type", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| std::string type = type_iter->second.to_str(); | ||
| Common::ToLower(&type); | ||
| if (type == "texture_asset") | ||
| { | ||
| property.m_type = MaterialProperty::Type::Type_TextureAsset; | ||
| } | ||
| else | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, | ||
| "Asset '{}' failed to parse the json, value entry 'type' is " | ||
| "an invalid option", | ||
| asset_id); | ||
| return false; | ||
| } | ||
|
|
||
| const auto code_name_iter = value_data_obj.find("code_name"); | ||
| if (code_name_iter == value_data_obj.end()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, | ||
| "Asset '{}' failed to parse the json, value entry " | ||
| "'code_name' not found", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| if (!code_name_iter->second.is<std::string>()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, | ||
| "Asset '{}' failed to parse the json, value entry 'code_name' is not " | ||
| "the right json type", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| property.m_code_name = code_name_iter->second.to_str(); | ||
|
|
||
| const auto value_iter = value_data_obj.find("value"); | ||
| if (value_iter != value_data_obj.end()) | ||
| { | ||
| if (!ParsePropertyValue(asset_id, property.m_type, value_iter->second, &property.m_value)) | ||
| return false; | ||
| } | ||
|
|
||
| material_property->push_back(std::move(property)); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } // namespace | ||
| bool MaterialData::FromJson(const CustomAssetLibrary::AssetID& asset_id, | ||
| const picojson::object& json, MaterialData* data) | ||
| { | ||
| const auto values_iter = json.find("values"); | ||
| if (values_iter == json.end()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'values' not found", asset_id); | ||
| return false; | ||
| } | ||
| if (!values_iter->second.is<picojson::array>()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'values' is not the right json type", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| const auto& values_array = values_iter->second.get<picojson::array>(); | ||
|
|
||
| if (!ParseMaterialProperties(asset_id, values_array, &data->properties)) | ||
| return false; | ||
|
|
||
| const auto shader_asset_iter = json.find("shader_asset"); | ||
| if (shader_asset_iter == json.end()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'shader_asset' not found", asset_id); | ||
| return false; | ||
| } | ||
| if (!shader_asset_iter->second.is<std::string>()) | ||
| { | ||
| ERROR_LOG_FMT(VIDEO, | ||
| "Asset '{}' failed to parse json, 'shader_asset' is not the right json type", | ||
| asset_id); | ||
| return false; | ||
| } | ||
| data->shader_asset = shader_asset_iter->second.to_str(); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| CustomAssetLibrary::LoadInfo MaterialAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) | ||
| { | ||
| auto potential_data = std::make_shared<MaterialData>(); | ||
| const auto loaded_info = m_owning_library->LoadMaterial(asset_id, potential_data.get()); | ||
| if (loaded_info.m_bytes_loaded == 0) | ||
| return {}; | ||
| { | ||
| std::lock_guard lk(m_data_lock); | ||
| m_loaded = true; | ||
| m_data = std::move(potential_data); | ||
| } | ||
| return loaded_info; | ||
| } | ||
| } // namespace VideoCommon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2023 Dolphin Emulator Project | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <optional> | ||
| #include <string> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| #include <picojson.h> | ||
|
|
||
| #include "Common/CommonTypes.h" | ||
| #include "Common/EnumFormatter.h" | ||
| #include "VideoCommon/Assets/CustomAsset.h" | ||
|
|
||
| namespace VideoCommon | ||
| { | ||
| struct MaterialProperty | ||
| { | ||
| using Value = std::variant<std::string>; | ||
| enum class Type | ||
| { | ||
| Type_Undefined, | ||
| Type_TextureAsset, | ||
| Type_Max = Type_TextureAsset | ||
| }; | ||
| std::string m_code_name; | ||
| Type m_type = Type::Type_Undefined; | ||
| std::optional<Value> m_value; | ||
| }; | ||
|
|
||
| struct MaterialData | ||
| { | ||
| static bool FromJson(const CustomAssetLibrary::AssetID& asset_id, const picojson::object& json, | ||
| MaterialData* data); | ||
| std::string shader_asset; | ||
| std::vector<MaterialProperty> properties; | ||
| }; | ||
|
|
||
| // Much like Unity and Unreal materials, a Dolphin material does very little on its own | ||
| // Its sole purpose is to provide data (through properties) that are used in conjunction | ||
| // with a shader asset that is provided by name. It is up to user of this asset to | ||
| // use the two together to create the relevant runtime data | ||
| class MaterialAsset final : public CustomLoadableAsset<MaterialData> | ||
| { | ||
| public: | ||
| using CustomLoadableAsset::CustomLoadableAsset; | ||
|
|
||
| private: | ||
| CustomAssetLibrary::LoadInfo LoadImpl(const CustomAssetLibrary::AssetID& asset_id) override; | ||
| }; | ||
|
|
||
| } // namespace VideoCommon | ||
|
|
||
| template <> | ||
| struct fmt::formatter<VideoCommon::MaterialProperty::Type> | ||
| : EnumFormatter<VideoCommon::MaterialProperty::Type::Type_Max> | ||
| { | ||
| constexpr formatter() : EnumFormatter({"Undefined", "Texture"}) {} | ||
| }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters