| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/TextureUtils.h" |
| 5 | |
| 6 | #include <fmt/format.h> |
| 7 | |
| 8 | #include "Common/FileSearch.h" |
| 9 | #include "Common/FileUtil.h" |
| 10 | #include "Common/Logging/Log.h" |
| 11 | |
| 12 | #include "Core/Config/GraphicsSettings.h" |
| 13 | #include "Core/ConfigManager.h" |
| 14 | |
| 15 | #include "VideoCommon/AbstractTexture.h" |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | std::string BuildDumpTextureFilename(std::string basename, u32 level, bool is_arbitrary) |
| 20 | { |
| 21 | if (is_arbitrary) |
| 22 | basename += "_arb"; |
| 23 | |
| 24 | if (level > 0) |
| 25 | basename += fmt::format("_mip{}", level); |
| 26 | |
| 27 | return basename; |
| 28 | } |
| 29 | } // namespace |
| 30 | namespace VideoCommon::TextureUtils |
| 31 | { |
| 32 | void DumpTexture(const ::AbstractTexture& texture, std::string basename, u32 level, |
| 33 | bool is_arbitrary) |
| 34 | { |
| 35 | const std::string dump_dir = |
| 36 | File::GetUserPath(D_DUMPTEXTURES_IDX) + SConfig::GetInstance().GetGameID(); |
| 37 | |
| 38 | if (!File::IsDirectory(dump_dir)) |
| 39 | File::CreateDir(dump_dir); |
| 40 | |
| 41 | const std::string name = BuildDumpTextureFilename(std::move(basename), level, is_arbitrary); |
| 42 | const std::string filename = fmt::format("{}/{}.png", dump_dir, name); |
| 43 | |
| 44 | if (File::Exists(filename)) |
| 45 | return; |
| 46 | |
| 47 | texture.Save(filename, level, Config::Get(Config::GFX_TEXTURE_PNG_COMPRESSION_LEVEL)); |
| 48 | } |
| 49 | |
| 50 | void TextureDumper::DumpTexture(const ::AbstractTexture& texture, std::string basename, u32 level, |
| 51 | bool is_arbitrary) |
| 52 | { |
| 53 | const std::string dump_dir = |
| 54 | File::GetUserPath(D_DUMPTEXTURES_IDX) + SConfig::GetInstance().GetGameID(); |
| 55 | |
| 56 | if (m_dumped_textures.empty()) |
| 57 | { |
| 58 | if (!File::IsDirectory(dump_dir)) |
| 59 | File::CreateDir(dump_dir); |
| 60 | |
| 61 | for (auto& filename : Common::DoFileSearch(dump_dir, ".png", true)) |
| 62 | { |
| 63 | std::string name; |
| 64 | SplitPath(filename, nullptr, &name, nullptr); |
| 65 | m_dumped_textures.insert(name); |
| 66 | } |
| 67 | |
| 68 | NOTICE_LOG_FMT(VIDEO, "Found {} dumped textures that will not be re-dumped.", |
| 69 | m_dumped_textures.size()); |
| 70 | } |
| 71 | |
| 72 | const std::string name = BuildDumpTextureFilename(std::move(basename), level, is_arbitrary); |
| 73 | const bool file_existed = !m_dumped_textures.insert(name).second; |
| 74 | if (file_existed) |
| 75 | return; |
| 76 | |
| 77 | texture.Save(fmt::format("{}/{}.png", dump_dir, name), level, |
| 78 | Config::Get(Config::GFX_TEXTURE_PNG_COMPRESSION_LEVEL)); |
| 79 | } |
| 80 | } // namespace VideoCommon::TextureUtils |