| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/TextureAssetUtils.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include "Common/FileUtil.h" |
| 9 | #include "Common/Logging/Log.h" |
| 10 | #include "Common/StringUtil.h" |
| 11 | |
| 12 | namespace VideoCommon |
| 13 | { |
| 14 | namespace |
| 15 | { |
| 16 | // Loads additional mip levels into the texture structure until _mip<N> texture is not found |
| 17 | bool LoadMips(const std::filesystem::path& asset_path, CustomTextureData::ArraySlice* data) |
| 18 | { |
| 19 | if (!data) [[unlikely]] |
| 20 | return false; |
| 21 | |
| 22 | std::string path; |
| 23 | std::string filename; |
| 24 | std::string extension; |
| 25 | SplitPath(PathToString(asset_path), &path, &filename, &extension); |
| 26 | |
| 27 | std::string extension_lower = extension; |
| 28 | Common::ToLower(&extension_lower); |
| 29 | |
| 30 | // Load additional mip levels |
| 31 | for (u32 mip_level = static_cast<u32>(data->m_levels.size());; mip_level++) |
| 32 | { |
| 33 | const auto mip_level_filename = filename + fmt::format("_mip{}", mip_level); |
| 34 | |
| 35 | const auto full_path = path + mip_level_filename + extension; |
| 36 | if (!File::Exists(full_path)) |
| 37 | return true; |
| 38 | |
| 39 | VideoCommon::CustomTextureData::ArraySlice::Level level; |
| 40 | if (extension_lower == ".dds") |
| 41 | { |
| 42 | if (!LoadDDSTexture(&level, full_path, mip_level)) |
| 43 | { |
| 44 | ERROR_LOG_FMT(VIDEO, "Custom mipmap '{}' failed to load", mip_level_filename); |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | else if (extension_lower == ".png") |
| 49 | { |
| 50 | if (!LoadPNGTexture(&level, full_path)) |
| 51 | { |
| 52 | ERROR_LOG_FMT(VIDEO, "Custom mipmap '{}' failed to load", mip_level_filename); |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | ERROR_LOG_FMT(VIDEO, "Custom mipmap '{}' has unsupported extension", mip_level_filename); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | data->m_levels.push_back(std::move(level)); |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | } // namespace |
| 68 | bool LoadTextureDataFromFile(const CustomAssetLibrary::AssetID& asset_id, |
| 69 | const std::filesystem::path& asset_path, AbstractTextureType type, |
| 70 | CustomTextureData* data) |
| 71 | { |
| 72 | auto ext = PathToString(asset_path.extension()); |
| 73 | Common::ToLower(&ext); |
| 74 | if (ext == ".dds") |
| 75 | { |
| 76 | if (!LoadDDSTexture(data, PathToString(asset_path))) |
| 77 | { |
| 78 | ERROR_LOG_FMT(VIDEO, "Asset '{}' error - could not load dds texture!", asset_id); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (data->m_slices.empty()) [[unlikely]] |
| 83 | data->m_slices.emplace_back(); |
| 84 | |
| 85 | if (!LoadMips(asset_path, data->m_slices.data())) |
| 86 | return false; |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (ext == ".png") |
| 92 | { |
| 93 | // PNG could support more complicated texture types in the future |
| 94 | // but for now just error |
| 95 | if (type != AbstractTextureType::Texture_2D) |
| 96 | { |
| 97 | ERROR_LOG_FMT(VIDEO, "Asset '{}' error - PNG is not supported for texture type '{}'!", |
| 98 | asset_id, type); |
| 99 | return {}; |
| 100 | } |
| 101 | |
| 102 | // If we have no slices, create one |
| 103 | if (data->m_slices.empty()) |
| 104 | data->m_slices.emplace_back(); |
| 105 | |
| 106 | auto& slice = data->m_slices[0]; |
| 107 | // If we have no levels, create one to pass into LoadPNGTexture |
| 108 | if (slice.m_levels.empty()) |
| 109 | slice.m_levels.emplace_back(); |
| 110 | |
| 111 | if (!LoadPNGTexture(slice.m_levels.data(), PathToString(asset_path))) |
| 112 | { |
| 113 | ERROR_LOG_FMT(VIDEO, "Asset '{}' error - could not load png texture!", asset_id); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | if (!LoadMips(asset_path, &slice)) |
| 118 | return false; |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | ERROR_LOG_FMT(VIDEO, "Asset '{}' error - extension '{}' unknown!", asset_id, ext); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool ValidateTextureData(const CustomAssetLibrary::AssetID& asset_id, const CustomTextureData& data, |
| 128 | u32 native_width, u32 native_height) |
| 129 | { |
| 130 | if (data.m_slices.empty()) |
| 131 | { |
| 132 | ERROR_LOG_FMT(VIDEO, |
| 133 | "Texture data can't be validated for asset '{}' because no data was available.", |
| 134 | asset_id); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if (data.m_slices.size() > 1) |
| 139 | { |
| 140 | ERROR_LOG_FMT( |
| 141 | VIDEO, |
| 142 | "Texture data can't be validated for asset '{}' because it has more slices than expected.", |
| 143 | asset_id); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | const auto& slice = data.m_slices[0]; |
| 148 | if (slice.m_levels.empty()) |
| 149 | { |
| 150 | ERROR_LOG_FMT( |
| 151 | VIDEO, |
| 152 | "Texture data can't be validated for asset '{}' because first slice has no data available.", |
| 153 | asset_id); |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | // Verify that the aspect ratio of the texture hasn't changed, as this could have |
| 158 | // side-effects. |
| 159 | const CustomTextureData::ArraySlice::Level& first_mip = slice.m_levels[0]; |
| 160 | if (first_mip.width * native_height != first_mip.height * native_width) |
| 161 | { |
| 162 | // Note: this feels like this should return an error but |
| 163 | // for legacy reasons this is only a notice that something *could* |
| 164 | // go wrong |
| 165 | WARN_LOG_FMT(VIDEO, |
| 166 | "Invalid texture data size {}x{} for asset '{}'. The aspect differs " |
| 167 | "from the native size {}x{}.", |
| 168 | first_mip.width, first_mip.height, asset_id, native_width, native_height); |
| 169 | } |
| 170 | |
| 171 | // Same deal if the custom texture isn't a multiple of the native size. |
| 172 | if (native_width != 0 && native_height != 0 && |
| 173 | (first_mip.width % native_width || first_mip.height % native_height)) |
| 174 | { |
| 175 | // Note: this feels like this should return an error but |
| 176 | // for legacy reasons this is only a notice that something *could* |
| 177 | // go wrong |
| 178 | WARN_LOG_FMT(VIDEO, |
| 179 | "Invalid texture data size {}x{} for asset '{}'. Please use an integer " |
| 180 | "upscaling factor based on the native size {}x{}.", |
| 181 | first_mip.width, first_mip.height, asset_id, native_width, native_height); |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | bool PurgeInvalidMipsFromTextureData(const CustomAssetLibrary::AssetID& asset_id, |
| 188 | CustomTextureData* data) |
| 189 | { |
| 190 | for (std::size_t slice_index = 0; slice_index < data->m_slices.size(); slice_index++) |
| 191 | { |
| 192 | auto& slice = data->m_slices[slice_index]; |
| 193 | const auto& first_mip = slice.m_levels[0]; |
| 194 | |
| 195 | // Verify that each mip level is the correct size (divide by 2 each time). |
| 196 | u32 current_mip_width = first_mip.width; |
| 197 | u32 current_mip_height = first_mip.height; |
| 198 | for (u32 mip_level = 1; mip_level < static_cast<u32>(slice.m_levels.size()); mip_level++) |
| 199 | { |
| 200 | if (current_mip_width != 1 || current_mip_height != 1) |
| 201 | { |
| 202 | current_mip_width = std::max(current_mip_width / 2, 1u); |
| 203 | current_mip_height = std::max(current_mip_height / 2, 1u); |
| 204 | |
| 205 | const VideoCommon::CustomTextureData::ArraySlice::Level& level = slice.m_levels[mip_level]; |
| 206 | if (current_mip_width == level.width && current_mip_height == level.height) |
| 207 | continue; |
| 208 | |
| 209 | ERROR_LOG_FMT(VIDEO, |
| 210 | "Invalid custom game texture size {}x{} for texture asset {}. Slice {} with " |
| 211 | "mipmap level {} " |
| 212 | "must be {}x{}.", |
| 213 | level.width, level.height, asset_id, slice_index, mip_level, |
| 214 | current_mip_width, current_mip_height); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | // It is invalid to have more than a single 1x1 mipmap. |
| 219 | ERROR_LOG_FMT(VIDEO, |
| 220 | "Custom game texture {} has too many 1x1 mipmaps for slice {}. Skipping " |
| 221 | "extra levels.", |
| 222 | asset_id, slice_index); |
| 223 | } |
| 224 | |
| 225 | // Drop this mip level and any others after it. |
| 226 | while (slice.m_levels.size() > mip_level) |
| 227 | slice.m_levels.pop_back(); |
| 228 | } |
| 229 | |
| 230 | // All levels have to have the same format. |
| 231 | if (std::ranges::any_of(slice.m_levels, |
| 232 | [&first_mip](const auto& l) { return l.format != first_mip.format; })) |
| 233 | { |
| 234 | ERROR_LOG_FMT( |
| 235 | VIDEO, "Custom game texture {} has inconsistent formats across mip levels for slice {}.", |
| 236 | asset_id, slice_index); |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | } // namespace VideoCommon |