2016-04-30 20:44:31 +00: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-04-09 16:41:49 +00:00
|
|
|
#include <mutex>
|
2016-04-30 22:41:12 +00:00
|
|
|
#include <string>
|
2016-04-30 23:21:16 +00:00
|
|
|
#include <unordered_map>
|
2016-04-30 20:44:31 +00:00
|
|
|
#include <vector>
|
2021-05-05 23:31:38 +00:00
|
|
|
|
2022-01-30 23:49:02 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-04-30 22:03:39 +00:00
|
|
|
#include "Common/MemoryUtil.h"
|
2021-05-05 23:31:38 +00:00
|
|
|
#include "Common/File/Path.h"
|
|
|
|
|
2021-02-28 01:16:16 +00:00
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
2016-04-30 20:44:31 +00:00
|
|
|
#include "GPU/ge_constants.h"
|
|
|
|
|
2019-07-14 23:01:37 +00:00
|
|
|
class IniFile;
|
2016-04-30 20:44:31 +00:00
|
|
|
class TextureCacheCommon;
|
|
|
|
class TextureReplacer;
|
2021-10-21 20:21:23 +00:00
|
|
|
class ReplacedTextureTask;
|
|
|
|
class LimitedWaitable;
|
2016-04-30 20:44:31 +00:00
|
|
|
|
|
|
|
enum class ReplacedTextureFormat {
|
|
|
|
F_5650,
|
|
|
|
F_5551,
|
|
|
|
F_4444,
|
|
|
|
F_8888,
|
2016-05-01 02:00:20 +00:00
|
|
|
F_0565_ABGR,
|
|
|
|
F_1555_ABGR,
|
|
|
|
F_4444_ABGR,
|
2016-04-30 22:03:39 +00:00
|
|
|
F_8888_BGRA,
|
2016-04-30 20:44:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// These must match the constants in TextureCacheCommon.
|
|
|
|
enum class ReplacedTextureAlpha {
|
|
|
|
UNKNOWN = 0x04,
|
|
|
|
FULL = 0x00,
|
|
|
|
};
|
|
|
|
|
2016-04-30 23:21:16 +00:00
|
|
|
// For forward comatibility, we specify the hash.
|
|
|
|
enum class ReplacedTextureHash {
|
|
|
|
QUICK,
|
2017-05-06 12:51:24 +00:00
|
|
|
XXH32,
|
|
|
|
XXH64,
|
2016-04-30 23:21:16 +00:00
|
|
|
};
|
|
|
|
|
2016-05-01 03:21:08 +00:00
|
|
|
struct ReplacedTextureLevel {
|
2016-04-30 20:44:31 +00:00
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
ReplacedTextureFormat fmt;
|
2021-05-11 07:50:28 +00:00
|
|
|
Path file;
|
2016-04-30 20:44:31 +00:00
|
|
|
};
|
|
|
|
|
2016-05-01 02:35:35 +00:00
|
|
|
struct ReplacementCacheKey {
|
|
|
|
u64 cachekey;
|
|
|
|
u32 hash;
|
|
|
|
|
|
|
|
ReplacementCacheKey(u64 c, u32 h) : cachekey(c), hash(h) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(const ReplacementCacheKey &k) const {
|
|
|
|
return k.cachekey == cachekey && k.hash == hash;
|
|
|
|
}
|
2016-05-01 07:33:37 +00:00
|
|
|
|
|
|
|
bool operator <(const ReplacementCacheKey &k) const {
|
|
|
|
if (k.cachekey == cachekey) {
|
|
|
|
return k.hash < hash;
|
|
|
|
}
|
|
|
|
return k.cachekey < cachekey;
|
|
|
|
}
|
2016-05-01 02:35:35 +00:00
|
|
|
};
|
|
|
|
|
2016-05-31 05:31:02 +00:00
|
|
|
struct ReplacementAliasKey {
|
|
|
|
u64 cachekey;
|
|
|
|
union {
|
|
|
|
u64 hashAndLevel;
|
|
|
|
struct {
|
|
|
|
u32 level;
|
|
|
|
u32 hash;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-05 17:46:11 +00:00
|
|
|
ReplacementAliasKey(u64 c, u32 h, u32 l) : cachekey(c), level(l), hash(h) {
|
2016-05-31 05:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(const ReplacementAliasKey &k) const {
|
|
|
|
return k.cachekey == cachekey && k.hashAndLevel == hashAndLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator <(const ReplacementAliasKey &k) const {
|
|
|
|
if (k.cachekey == cachekey) {
|
|
|
|
return k.hashAndLevel < hashAndLevel;
|
|
|
|
}
|
|
|
|
return k.cachekey < cachekey;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-01 02:35:35 +00: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-31 05:31:02 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<ReplacementAliasKey> {
|
|
|
|
size_t operator()(const ReplacementAliasKey &k) const {
|
|
|
|
return std::hash<u64>()(k.cachekey ^ k.hashAndLevel);
|
|
|
|
}
|
|
|
|
};
|
2016-05-01 02:35:35 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
struct ReplacedTexture {
|
2021-10-23 15:06:55 +00:00
|
|
|
~ReplacedTexture();
|
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
inline bool Valid() {
|
|
|
|
return !levels_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetSize(int level, int &w, int &h) {
|
|
|
|
if ((size_t)level < levels_.size()) {
|
|
|
|
w = levels_[level].w;
|
|
|
|
h = levels_[level].h;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MaxLevel() {
|
|
|
|
return (int)levels_.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplacedTextureFormat Format(int level) {
|
|
|
|
if ((size_t)level < levels_.size()) {
|
|
|
|
return levels_[level].fmt;
|
|
|
|
}
|
|
|
|
return ReplacedTextureFormat::F_8888;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 AlphaStatus() {
|
|
|
|
return (u8)alphaStatus_;
|
|
|
|
}
|
|
|
|
|
2021-10-17 15:54:45 +00:00
|
|
|
bool IsReady(double budget);
|
|
|
|
|
2021-10-08 13:37:03 +00:00
|
|
|
bool Load(int level, void *out, int rowPitch);
|
2016-04-30 20:44:31 +00:00
|
|
|
|
|
|
|
protected:
|
2021-10-24 03:56:19 +00:00
|
|
|
void Prepare();
|
2021-10-17 16:16:54 +00:00
|
|
|
void PrepareData(int level);
|
|
|
|
void PurgeIfOlder(double t);
|
|
|
|
|
2016-05-01 03:21:08 +00:00
|
|
|
std::vector<ReplacedTextureLevel> levels_;
|
2021-10-17 16:16:54 +00:00
|
|
|
std::vector<std::vector<uint8_t>> levelData_;
|
2016-04-30 20:44:31 +00:00
|
|
|
ReplacedTextureAlpha alphaStatus_;
|
2021-10-17 16:16:54 +00:00
|
|
|
double lastUsed_ = 0.0;
|
2021-10-23 15:06:55 +00:00
|
|
|
LimitedWaitable *threadWaitable_ = nullptr;
|
2022-04-09 16:41:49 +00:00
|
|
|
std::mutex mutex_;
|
2021-10-23 15:06:55 +00:00
|
|
|
bool cancelPrepare_ = false;
|
2016-04-30 20:44:31 +00:00
|
|
|
|
|
|
|
friend TextureReplacer;
|
2021-10-21 20:21:23 +00:00
|
|
|
friend ReplacedTextureTask;
|
2016-04-30 20:44:31 +00:00
|
|
|
};
|
|
|
|
|
2016-05-01 15:54:43 +00:00
|
|
|
struct ReplacedTextureDecodeInfo {
|
|
|
|
u64 cachekey;
|
|
|
|
u32 hash;
|
|
|
|
u32 addr;
|
|
|
|
bool isVideo;
|
|
|
|
bool isFinal;
|
|
|
|
int scaleFactor;
|
|
|
|
ReplacedTextureFormat fmt;
|
|
|
|
};
|
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
class TextureReplacer {
|
|
|
|
public:
|
|
|
|
TextureReplacer();
|
|
|
|
~TextureReplacer();
|
|
|
|
|
|
|
|
void Init();
|
|
|
|
void NotifyConfigChanged();
|
|
|
|
|
|
|
|
inline bool Enabled() {
|
|
|
|
return enabled_;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ComputeHash(u32 addr, int bufw, int w, int h, GETextureFormat fmt, u16 maxSeenV);
|
|
|
|
|
2016-05-01 02:35:35 +00:00
|
|
|
ReplacedTexture &FindReplacement(u64 cachekey, u32 hash, int w, int h);
|
2021-02-28 01:16:16 +00:00
|
|
|
bool FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering);
|
2021-10-17 19:46:02 +00:00
|
|
|
ReplacedTexture &FindNone() {
|
2021-10-17 19:36:20 +00:00
|
|
|
return none_;
|
|
|
|
}
|
2016-04-30 20:44:31 +00:00
|
|
|
|
2016-05-01 15:54:43 +00:00
|
|
|
void NotifyTextureDecoded(const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int w, int h);
|
2016-04-30 20:44:31 +00:00
|
|
|
|
2021-10-17 16:16:54 +00:00
|
|
|
void Decimate(bool forcePressure);
|
|
|
|
|
2021-05-11 07:50:28 +00:00
|
|
|
static bool GenerateIni(const std::string &gameID, Path &generatedFilename);
|
2018-10-01 00:00:05 +00:00
|
|
|
|
2016-04-30 20:44:31 +00:00
|
|
|
protected:
|
2016-04-30 23:21:16 +00:00
|
|
|
bool LoadIni();
|
2019-07-14 23:01:37 +00:00
|
|
|
bool LoadIniValues(IniFile &ini, bool isOverride = false);
|
2016-04-30 23:21:16 +00:00
|
|
|
void ParseHashRange(const std::string &key, const std::string &value);
|
2021-02-28 01:16:16 +00:00
|
|
|
void ParseFiltering(const std::string &key, const std::string &value);
|
2021-04-01 11:29:47 +00:00
|
|
|
void ParseReduceHashRange(const std::string& key, const std::string& value);
|
2016-04-30 21:19:23 +00:00
|
|
|
bool LookupHashRange(u32 addr, int &w, int &h);
|
2021-04-01 11:29:47 +00:00
|
|
|
float LookupReduceHashRange(int& w, int& h);
|
2021-04-03 15:56:28 +00:00
|
|
|
std::string LookupHashFile(u64 cachekey, u32 hash, int level);
|
|
|
|
std::string HashName(u64 cachekey, u32 hash, int level);
|
2016-05-01 01:39:30 +00:00
|
|
|
void PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h);
|
2021-05-16 16:30:33 +00:00
|
|
|
bool PopulateLevel(ReplacedTextureLevel &level);
|
2016-04-30 21:19:23 +00:00
|
|
|
|
2018-03-26 10:33:36 +00:00
|
|
|
bool enabled_ = false;
|
|
|
|
bool allowVideo_ = false;
|
|
|
|
bool ignoreAddress_ = false;
|
|
|
|
bool reduceHash_ = false;
|
2021-04-01 11:29:47 +00:00
|
|
|
float reduceHashSize = 1.0; // default value with reduceHash to false
|
|
|
|
float reduceHashGlobalValue = 0.5; // Global value for textures dump pngs of all sizes, 0.5 by default but can be set in textures.ini
|
2020-04-13 14:29:17 +00:00
|
|
|
bool ignoreMipmap_ = false;
|
2016-04-30 21:05:03 +00:00
|
|
|
std::string gameID_;
|
2021-05-05 23:31:38 +00:00
|
|
|
Path basePath_;
|
2018-03-26 10:33:36 +00:00
|
|
|
ReplacedTextureHash hash_ = ReplacedTextureHash::QUICK;
|
2016-04-30 23:21:16 +00:00
|
|
|
typedef std::pair<int, int> WidthHeightPair;
|
|
|
|
std::unordered_map<u64, WidthHeightPair> hashranges_;
|
2021-04-01 11:29:47 +00:00
|
|
|
std::unordered_map<u64, float> reducehashranges_;
|
2016-05-31 05:31:02 +00:00
|
|
|
std::unordered_map<ReplacementAliasKey, std::string> aliases_;
|
2021-02-28 01:16:16 +00:00
|
|
|
std::unordered_map<ReplacementCacheKey, TextureFiltering> filtering_;
|
2016-05-01 02:35:35 +00:00
|
|
|
|
|
|
|
ReplacedTexture none_;
|
|
|
|
std::unordered_map<ReplacementCacheKey, ReplacedTexture> cache_;
|
2016-05-01 03:21:08 +00:00
|
|
|
std::unordered_map<ReplacementCacheKey, ReplacedTextureLevel> savedCache_;
|
2016-04-30 20:44:31 +00:00
|
|
|
};
|