| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/TextureAsset.h" |
| 5 | |
| 6 | #include <optional> |
| 7 | |
| 8 | #include "Common/JsonUtil.h" |
| 9 | #include "Common/Logging/Log.h" |
| 10 | #include "Common/StringUtil.h" |
| 11 | #include "VideoCommon/BPMemory.h" |
| 12 | |
| 13 | namespace VideoCommon |
| 14 | { |
| 15 | namespace |
| 16 | { |
| 17 | std::optional<WrapMode> ReadWrapModeFromJSON(const picojson::object& json, const std::string& uv) |
| 18 | { |
| 19 | auto uv_mode = ReadStringFromJson(json, uv).value_or(""); |
| 20 | Common::ToLower(&uv_mode); |
| 21 | |
| 22 | if (uv_mode == "clamp") |
| 23 | { |
| 24 | return WrapMode::Clamp; |
| 25 | } |
| 26 | else if (uv_mode == "repeat") |
| 27 | { |
| 28 | return WrapMode::Repeat; |
| 29 | } |
| 30 | else if (uv_mode == "mirror") |
| 31 | { |
| 32 | return WrapMode::Mirror; |
| 33 | } |
| 34 | |
| 35 | return std::nullopt; |
| 36 | } |
| 37 | |
| 38 | std::optional<FilterMode> ReadFilterModeFromJSON(const picojson::object& json, |
| 39 | const std::string& filter) |
| 40 | { |
| 41 | auto filter_mode = ReadStringFromJson(json, filter).value_or(""); |
| 42 | Common::ToLower(&filter_mode); |
| 43 | |
| 44 | if (filter_mode == "linear") |
| 45 | { |
| 46 | return FilterMode::Linear; |
| 47 | } |
| 48 | else if (filter_mode == "near") |
| 49 | { |
| 50 | return FilterMode::Near; |
| 51 | } |
| 52 | |
| 53 | return std::nullopt; |
| 54 | } |
| 55 | |
| 56 | bool ParseSampler(const VideoCommon::CustomAssetLibrary::AssetID& asset_id, |
| 57 | const picojson::object& json, SamplerState* sampler) |
| 58 | { |
| 59 | if (!sampler) [[unlikely]] |
| 60 | return false; |
| 61 | |
| 62 | *sampler = RenderState::GetLinearSamplerState(); |
| 63 | |
| 64 | const auto sampler_state_wrap_iter = json.find("wrap_mode"); |
| 65 | if (sampler_state_wrap_iter != json.end()) |
| 66 | { |
| 67 | if (!sampler_state_wrap_iter->second.is<picojson::object>()) |
| 68 | { |
| 69 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'wrap_mode' is not the right type", |
| 70 | asset_id); |
| 71 | return false; |
| 72 | } |
| 73 | const auto sampler_state_wrap_obj = sampler_state_wrap_iter->second.get<picojson::object>(); |
| 74 | |
| 75 | if (const auto mode = ReadWrapModeFromJSON(sampler_state_wrap_obj, "u")) |
| 76 | { |
| 77 | sampler->tm0.wrap_u = *mode; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | ERROR_LOG_FMT(VIDEO, |
| 82 | "Asset '{}' failed to parse json, 'wrap_mode[u]' has an invalid " |
| 83 | "value", |
| 84 | asset_id); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (const auto mode = ReadWrapModeFromJSON(sampler_state_wrap_obj, "v")) |
| 89 | { |
| 90 | sampler->tm0.wrap_v = *mode; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | ERROR_LOG_FMT(VIDEO, |
| 95 | "Asset '{}' failed to parse json, 'wrap_mode[v]' has an invalid " |
| 96 | "value", |
| 97 | asset_id); |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | const auto sampler_state_filter_iter = json.find("filter_mode"); |
| 103 | if (sampler_state_filter_iter != json.end()) |
| 104 | { |
| 105 | if (!sampler_state_filter_iter->second.is<picojson::object>()) |
| 106 | { |
| 107 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'filter_mode' is not the right type", |
| 108 | asset_id); |
| 109 | return false; |
| 110 | } |
| 111 | const auto sampler_state_filter_obj = sampler_state_filter_iter->second.get<picojson::object>(); |
| 112 | |
| 113 | if (const auto mode = ReadFilterModeFromJSON(sampler_state_filter_obj, "min")) |
| 114 | { |
| 115 | sampler->tm0.min_filter = *mode; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | ERROR_LOG_FMT(VIDEO, |
| 120 | "Asset '{}' failed to parse json, 'filter_mode[min]' has an invalid " |
| 121 | "value", |
| 122 | asset_id); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (const auto mode = ReadFilterModeFromJSON(sampler_state_filter_obj, "mag")) |
| 127 | { |
| 128 | sampler->tm0.mag_filter = *mode; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | ERROR_LOG_FMT(VIDEO, |
| 133 | "Asset '{}' failed to parse json, 'filter_mode[mag]' has an invalid " |
| 134 | "value", |
| 135 | asset_id); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if (const auto mode = ReadFilterModeFromJSON(sampler_state_filter_obj, "mipmap")) |
| 140 | { |
| 141 | sampler->tm0.mipmap_filter = *mode; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | ERROR_LOG_FMT(VIDEO, |
| 146 | "Asset '{}' failed to parse json, 'filter_mode[mipmap]' has an invalid " |
| 147 | "value", |
| 148 | asset_id); |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | } // namespace |
| 156 | bool TextureAndSamplerData::FromJson(const CustomAssetLibrary::AssetID& asset_id, |
| 157 | const picojson::object& json, TextureAndSamplerData* data) |
| 158 | { |
| 159 | const auto type_iter = json.find("type"); |
| 160 | if (type_iter == json.end()) |
| 161 | { |
| 162 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, property entry 'type' not found", |
| 163 | asset_id); |
| 164 | return false; |
| 165 | } |
| 166 | if (!type_iter->second.is<std::string>()) |
| 167 | { |
| 168 | ERROR_LOG_FMT(VIDEO, |
| 169 | "Asset '{}' failed to parse json, property entry 'type' is not " |
| 170 | "the right json type", |
| 171 | asset_id); |
| 172 | return false; |
| 173 | } |
| 174 | std::string type = type_iter->second.to_str(); |
| 175 | Common::ToLower(&type); |
| 176 | |
| 177 | if (type == "texture2d") |
| 178 | { |
| 179 | data->type = AbstractTextureType::Texture_2D; |
| 180 | |
| 181 | if (!ParseSampler(asset_id, json, &data->sampler)) |
| 182 | { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | else if (type == "texturecube") |
| 187 | { |
| 188 | data->type = AbstractTextureType::Texture_CubeMap; |
| 189 | } |
| 190 | else if (type == "texture2darray") |
| 191 | { |
| 192 | data->type = AbstractTextureType::Texture_2DArray; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | ERROR_LOG_FMT(VIDEO, |
| 197 | "Asset '{}' failed to parse json, texture type '{}' " |
| 198 | "an invalid option", |
| 199 | asset_id, type); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | void TextureAndSamplerData::ToJson(picojson::object* obj, const TextureAndSamplerData& data) |
| 207 | { |
| 208 | if (!obj) [[unlikely]] |
| 209 | return; |
| 210 | |
| 211 | auto& json_obj = *obj; |
| 212 | switch (data.type) |
| 213 | { |
| 214 | case AbstractTextureType::Texture_2D: |
| 215 | json_obj.emplace("type", "texture2d"); |
| 216 | break; |
| 217 | case AbstractTextureType::Texture_CubeMap: |
| 218 | json_obj.emplace("type", "texturecube"); |
| 219 | break; |
| 220 | case AbstractTextureType::Texture_2DArray: |
| 221 | json_obj.emplace("type", "texture2darray"); |
| 222 | break; |
| 223 | }; |
| 224 | |
| 225 | auto wrap_mode_to_string = [](WrapMode mode) { |
| 226 | switch (mode) |
| 227 | { |
| 228 | case WrapMode::Clamp: |
| 229 | return "clamp"; |
| 230 | case WrapMode::Mirror: |
| 231 | return "mirror"; |
| 232 | case WrapMode::Repeat: |
| 233 | return "repeat"; |
| 234 | }; |
| 235 | |
| 236 | return ""; |
| 237 | }; |
| 238 | auto filter_mode_to_string = [](FilterMode mode) { |
| 239 | switch (mode) |
| 240 | { |
| 241 | case FilterMode::Linear: |
| 242 | return "linear"; |
| 243 | case FilterMode::Near: |
| 244 | return "near"; |
| 245 | }; |
| 246 | |
| 247 | return ""; |
| 248 | }; |
| 249 | |
| 250 | picojson::object wrap_mode; |
| 251 | wrap_mode.emplace("u", wrap_mode_to_string(data.sampler.tm0.wrap_u)); |
| 252 | wrap_mode.emplace("v", wrap_mode_to_string(data.sampler.tm0.wrap_v)); |
| 253 | json_obj.emplace("wrap_mode", wrap_mode); |
| 254 | |
| 255 | picojson::object filter_mode; |
| 256 | filter_mode.emplace("min", filter_mode_to_string(data.sampler.tm0.min_filter)); |
| 257 | filter_mode.emplace("mag", filter_mode_to_string(data.sampler.tm0.mag_filter)); |
| 258 | filter_mode.emplace("mipmap", filter_mode_to_string(data.sampler.tm0.mipmap_filter)); |
| 259 | json_obj.emplace("filter_mode", filter_mode); |
| 260 | } |
| 261 | |
| 262 | CustomAssetLibrary::LoadInfo TextureAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) |
| 263 | { |
| 264 | auto potential_data = std::make_shared<CustomTextureData>(); |
| 265 | const auto loaded_info = m_owning_library->LoadTexture(asset_id, potential_data.get()); |
| 266 | if (loaded_info.bytes_loaded == 0) |
| 267 | return {}; |
| 268 | { |
| 269 | std::lock_guard lk(m_data_lock); |
| 270 | m_loaded = true; |
| 271 | m_data = std::move(potential_data); |
| 272 | } |
| 273 | return loaded_info; |
| 274 | } |
| 275 | |
| 276 | CustomAssetLibrary::LoadInfo |
| 277 | TextureAndSamplerAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) |
| 278 | { |
| 279 | auto potential_data = std::make_shared<TextureAndSamplerData>(); |
| 280 | const auto loaded_info = m_owning_library->LoadTexture(asset_id, potential_data.get()); |
| 281 | if (loaded_info.bytes_loaded == 0) |
| 282 | return {}; |
| 283 | { |
| 284 | std::lock_guard lk(m_data_lock); |
| 285 | m_loaded = true; |
| 286 | m_data = std::move(potential_data); |
| 287 | } |
| 288 | return loaded_info; |
| 289 | } |
| 290 | } // namespace VideoCommon |