2016-04-30 13:44:31 -07:00
|
|
|
// Copyright (c) 2016- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-10-30 08:54:52 -07:00
|
|
|
#include "ppsspp_config.h"
|
2023-12-24 22:00:37 +01:00
|
|
|
|
2022-04-09 09:41:49 -07:00
|
|
|
#include <mutex>
|
2016-04-30 15:41:12 -07:00
|
|
|
#include <string>
|
2016-04-30 16:21:16 -07:00
|
|
|
#include <unordered_map>
|
2023-12-24 22:00:37 +01:00
|
|
|
#include <map>
|
2016-04-30 13:44:31 -07:00
|
|
|
#include <vector>
|
2021-05-06 01:31:38 +02:00
|
|
|
|
2022-10-30 08:54:52 -07:00
|
|
|
#include "Common/CommonFuncs.h"
|
2022-01-30 15:49:02 -08:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-04-30 15:03:39 -07:00
|
|
|
#include "Common/MemoryUtil.h"
|
2021-05-06 01:31:38 +02:00
|
|
|
#include "Common/File/Path.h"
|
2023-03-07 20:53:50 +01:00
|
|
|
#include "Common/File/VFS/VFS.h"
|
2022-07-29 18:36:21 +02:00
|
|
|
#include "Common/GPU/DataFormat.h"
|
2021-05-06 01:31:38 +02:00
|
|
|
|
2021-02-27 17:16:16 -08:00
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
2023-03-09 21:19:20 +01:00
|
|
|
#include "GPU/Common/ReplacedTexture.h"
|
2016-04-30 13:44:31 -07:00
|
|
|
#include "GPU/ge_constants.h"
|
|
|
|
|
2019-07-14 16:01:37 -07:00
|
|
|
class IniFile;
|
2016-04-30 13:44:31 -07:00
|
|
|
class TextureCacheCommon;
|
|
|
|
class TextureReplacer;
|
2021-10-21 13:21:23 -07:00
|
|
|
class ReplacedTextureTask;
|
|
|
|
class LimitedWaitable;
|
2023-03-07 20:53:50 +01:00
|
|
|
class VFSBackend;
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2023-03-09 00:24:30 +01:00
|
|
|
struct SavedTextureCacheData {
|
|
|
|
int levelW[8]{};
|
|
|
|
int levelH[8]{};
|
|
|
|
bool levelSaved[8]{};
|
2023-03-09 10:51:15 +01:00
|
|
|
double lastTimeSaved = 0.0;
|
2023-03-09 00:24:30 +01:00
|
|
|
};
|
|
|
|
|
2016-04-30 19:35:35 -07:00
|
|
|
struct ReplacementCacheKey {
|
|
|
|
u64 cachekey;
|
|
|
|
u32 hash;
|
|
|
|
|
2022-07-29 12:59:43 +02:00
|
|
|
ReplacementCacheKey(u64 c, u32 h) : cachekey(c), hash(h) { }
|
2016-04-30 19:35:35 -07:00
|
|
|
|
|
|
|
bool operator ==(const ReplacementCacheKey &k) const {
|
|
|
|
return k.cachekey == cachekey && k.hash == hash;
|
|
|
|
}
|
2016-05-01 00:33:37 -07:00
|
|
|
|
|
|
|
bool operator <(const ReplacementCacheKey &k) const {
|
|
|
|
if (k.cachekey == cachekey) {
|
|
|
|
return k.hash < hash;
|
|
|
|
}
|
|
|
|
return k.cachekey < cachekey;
|
|
|
|
}
|
2016-04-30 19:35:35 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<ReplacementCacheKey> {
|
|
|
|
size_t operator()(const ReplacementCacheKey &k) const {
|
|
|
|
return std::hash<u64>()(k.cachekey ^ ((u64)k.hash << 32));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-01 08:54:43 -07:00
|
|
|
struct ReplacedTextureDecodeInfo {
|
|
|
|
u64 cachekey;
|
|
|
|
u32 hash;
|
|
|
|
u32 addr;
|
|
|
|
bool isVideo;
|
|
|
|
bool isFinal;
|
2022-07-29 18:36:21 +02:00
|
|
|
Draw::DataFormat fmt;
|
2016-05-01 08:54:43 -07:00
|
|
|
};
|
|
|
|
|
2022-07-27 20:27:56 -07:00
|
|
|
enum class ReplacerDecimateMode {
|
|
|
|
NEW_FRAME,
|
|
|
|
FORCE_PRESSURE,
|
|
|
|
ALL,
|
|
|
|
};
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
class TextureReplacer {
|
|
|
|
public:
|
2023-03-12 13:19:01 +01:00
|
|
|
// The draw context is checked for supported texture formats.
|
2023-03-09 00:05:24 +01:00
|
|
|
TextureReplacer(Draw::DrawContext *draw);
|
2016-04-30 13:44:31 -07:00
|
|
|
~TextureReplacer();
|
|
|
|
|
|
|
|
void NotifyConfigChanged();
|
|
|
|
|
2024-01-23 00:04:30 +01:00
|
|
|
bool Enabled() const { return replaceEnabled_ || saveEnabled_; } // used to check hashing method etc.
|
|
|
|
bool ReplaceEnabled() const { return replaceEnabled_; }
|
|
|
|
bool SaveEnabled() const { return saveEnabled_; }
|
|
|
|
|
2023-03-10 14:18:42 +01:00
|
|
|
bool AllowVideo() const { return allowVideo_; }
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2023-12-07 11:01:51 +01:00
|
|
|
u32 ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV);
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2023-03-08 11:31:32 +01:00
|
|
|
// Returns nullptr if not found.
|
2023-03-16 11:44:38 +01:00
|
|
|
ReplacedTexture *FindReplacement(u64 cachekey, u32 hash, int w, int h);
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2022-04-17 20:25:41 -07:00
|
|
|
// Check if a NotifyTextureDecoded for this texture is desired (used to avoid reads from write-combined memory.)
|
2024-04-05 17:04:31 +03:00
|
|
|
bool WillSave(const ReplacedTextureDecodeInfo &replacedInfo) const;
|
2022-04-17 20:25:41 -07:00
|
|
|
|
2023-03-16 10:21:57 +01:00
|
|
|
// Notify that a new texture was decoded. May already be upscaled, saves the data passed.
|
2023-03-27 15:43:18 +02:00
|
|
|
// If the replacer knows about this one already, texture will be passed in, otherwise nullptr.
|
|
|
|
void NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int origW, int origH, int scaledW, int scaledH);
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2022-07-27 20:27:56 -07:00
|
|
|
void Decimate(ReplacerDecimateMode mode);
|
2021-10-17 09:16:54 -07:00
|
|
|
|
2021-05-11 09:50:28 +02:00
|
|
|
static bool GenerateIni(const std::string &gameID, Path &generatedFilename);
|
2022-07-10 22:34:44 +02:00
|
|
|
static bool IniExists(const std::string &gameID);
|
2018-09-30 17:00:05 -07:00
|
|
|
|
2023-03-10 12:20:55 +01:00
|
|
|
int GetNumTrackedTextures() const { return (int)cache_.size(); }
|
|
|
|
int GetNumCachedReplacedTextures() const { return (int)levelCache_.size(); }
|
|
|
|
|
2023-03-10 17:43:12 +01:00
|
|
|
static std::string HashName(u64 cachekey, u32 hash, int level);
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
protected:
|
2023-03-18 13:03:05 +01:00
|
|
|
bool FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering);
|
|
|
|
|
2016-04-30 16:21:16 -07:00
|
|
|
bool LoadIni();
|
2023-05-01 23:20:54 +02:00
|
|
|
bool LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverride = false);
|
2016-04-30 16:21:16 -07:00
|
|
|
void ParseHashRange(const std::string &key, const std::string &value);
|
2021-02-27 17:16:16 -08:00
|
|
|
void ParseFiltering(const std::string &key, const std::string &value);
|
2021-04-01 13:29:47 +02:00
|
|
|
void ParseReduceHashRange(const std::string& key, const std::string& value);
|
2023-03-16 09:55:49 +01:00
|
|
|
bool LookupHashRange(u32 addr, int w, int h, int *newW, int *newH);
|
2023-03-19 00:00:59 +01:00
|
|
|
float LookupReduceHashRange(int w, int h);
|
2023-03-10 15:39:45 +01:00
|
|
|
std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored);
|
2016-04-30 14:19:23 -07:00
|
|
|
|
2024-04-05 17:04:31 +03:00
|
|
|
static void ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
|
2023-12-24 21:54:44 +01:00
|
|
|
void ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
|
|
|
|
|
2024-01-23 00:04:30 +01:00
|
|
|
bool replaceEnabled_ = false;
|
|
|
|
bool saveEnabled_ = false;
|
2018-03-26 12:33:36 +02:00
|
|
|
bool allowVideo_ = false;
|
|
|
|
bool ignoreAddress_ = false;
|
|
|
|
bool reduceHash_ = false;
|
2023-03-17 12:47:12 +01:00
|
|
|
bool ignoreMipmap_ = false;
|
|
|
|
|
2023-03-07 20:53:50 +01:00
|
|
|
float reduceHashSize = 1.0f; // default value with reduceHash to false
|
|
|
|
float reduceHashGlobalValue = 0.5f; // Global value for textures dump pngs of all sizes, 0.5 by default but can be set in textures.ini
|
|
|
|
|
2022-10-30 08:15:37 -07:00
|
|
|
double lastTextureCacheSizeGB_ = 0.0;
|
2016-04-30 14:05:03 -07:00
|
|
|
std::string gameID_;
|
2021-05-06 01:31:38 +02:00
|
|
|
Path basePath_;
|
2023-03-10 14:58:44 +01:00
|
|
|
Path newTextureDir_;
|
2018-03-26 12:33:36 +02:00
|
|
|
ReplacedTextureHash hash_ = ReplacedTextureHash::QUICK;
|
2023-03-07 20:53:50 +01:00
|
|
|
|
|
|
|
VFSBackend *vfs_ = nullptr;
|
2023-03-08 09:29:19 +01:00
|
|
|
bool vfsIsZip_ = false;
|
2023-03-17 12:47:12 +01:00
|
|
|
|
2023-03-12 13:19:01 +01:00
|
|
|
GPUFormatSupport formatSupport_{};
|
2022-10-30 10:34:25 -07:00
|
|
|
|
2016-04-30 16:21:16 -07:00
|
|
|
typedef std::pair<int, int> WidthHeightPair;
|
|
|
|
std::unordered_map<u64, WidthHeightPair> hashranges_;
|
2021-04-01 13:29:47 +02:00
|
|
|
std::unordered_map<u64, float> reducehashranges_;
|
2023-03-16 09:55:49 +01:00
|
|
|
|
2023-03-09 00:01:38 +01:00
|
|
|
std::unordered_map<ReplacementCacheKey, std::string> aliases_;
|
2021-02-27 17:16:16 -08:00
|
|
|
std::unordered_map<ReplacementCacheKey, TextureFiltering> filtering_;
|
2016-04-30 19:35:35 -07:00
|
|
|
|
2023-03-16 11:44:38 +01:00
|
|
|
std::unordered_map<ReplacementCacheKey, ReplacedTextureRef> cache_;
|
2023-03-09 00:24:30 +01:00
|
|
|
std::unordered_map<ReplacementCacheKey, SavedTextureCacheData> savedCache_;
|
2023-03-09 00:01:38 +01:00
|
|
|
|
2023-03-16 09:55:49 +01:00
|
|
|
// the key is either from aliases_, in which case it's a |-separated sequence of texture filenames of the levels of a texture.
|
|
|
|
// alternatively the key is from the generated texture filename.
|
2023-03-16 11:44:38 +01:00
|
|
|
std::unordered_map<std::string, ReplacedTexture *> levelCache_;
|
2016-04-30 13:44:31 -07:00
|
|
|
};
|