CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/TextureReplacer.h
Views: 1401
1
// Copyright (c) 2016- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include "ppsspp_config.h"
21
22
#include <mutex>
23
#include <string>
24
#include <unordered_map>
25
#include <map>
26
#include <vector>
27
28
#include "Common/CommonFuncs.h"
29
#include "Common/CommonTypes.h"
30
#include "Common/MemoryUtil.h"
31
#include "Common/File/Path.h"
32
#include "Common/File/VFS/VFS.h"
33
#include "Common/GPU/DataFormat.h"
34
35
#include "GPU/Common/TextureDecoder.h"
36
#include "GPU/Common/ReplacedTexture.h"
37
#include "GPU/ge_constants.h"
38
39
class IniFile;
40
class TextureCacheCommon;
41
class TextureReplacer;
42
class ReplacedTextureTask;
43
class LimitedWaitable;
44
class VFSBackend;
45
46
struct SavedTextureCacheData {
47
int levelW[8]{};
48
int levelH[8]{};
49
bool levelSaved[8]{};
50
double lastTimeSaved = 0.0;
51
};
52
53
struct ReplacementCacheKey {
54
u64 cachekey;
55
u32 hash;
56
57
ReplacementCacheKey(u64 c, u32 h) : cachekey(c), hash(h) { }
58
59
bool operator ==(const ReplacementCacheKey &k) const {
60
return k.cachekey == cachekey && k.hash == hash;
61
}
62
63
bool operator <(const ReplacementCacheKey &k) const {
64
if (k.cachekey == cachekey) {
65
return k.hash < hash;
66
}
67
return k.cachekey < cachekey;
68
}
69
};
70
71
namespace std {
72
template <>
73
struct hash<ReplacementCacheKey> {
74
size_t operator()(const ReplacementCacheKey &k) const {
75
return std::hash<u64>()(k.cachekey ^ ((u64)k.hash << 32));
76
}
77
};
78
}
79
80
struct ReplacedTextureDecodeInfo {
81
u64 cachekey;
82
u32 hash;
83
u32 addr;
84
bool isVideo;
85
bool isFinal;
86
Draw::DataFormat fmt;
87
};
88
89
enum class ReplacerDecimateMode {
90
NEW_FRAME,
91
FORCE_PRESSURE,
92
ALL,
93
};
94
95
class TextureReplacer {
96
public:
97
// The draw context is checked for supported texture formats.
98
TextureReplacer(Draw::DrawContext *draw);
99
~TextureReplacer();
100
101
void NotifyConfigChanged();
102
103
bool Enabled() const { return replaceEnabled_ || saveEnabled_; } // used to check hashing method etc.
104
bool ReplaceEnabled() const { return replaceEnabled_; }
105
bool SaveEnabled() const { return saveEnabled_; }
106
107
bool AllowVideo() const { return allowVideo_; }
108
109
u32 ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV);
110
111
// Returns nullptr if not found.
112
ReplacedTexture *FindReplacement(u64 cachekey, u32 hash, int w, int h);
113
114
// Check if a NotifyTextureDecoded for this texture is desired (used to avoid reads from write-combined memory.)
115
bool WillSave(const ReplacedTextureDecodeInfo &replacedInfo) const;
116
117
// Notify that a new texture was decoded. May already be upscaled, saves the data passed.
118
// If the replacer knows about this one already, texture will be passed in, otherwise nullptr.
119
void NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int origW, int origH, int scaledW, int scaledH);
120
121
void Decimate(ReplacerDecimateMode mode);
122
123
static bool GenerateIni(const std::string &gameID, Path &generatedFilename);
124
static bool IniExists(const std::string &gameID);
125
126
int GetNumTrackedTextures() const { return (int)cache_.size(); }
127
int GetNumCachedReplacedTextures() const { return (int)levelCache_.size(); }
128
129
static std::string HashName(u64 cachekey, u32 hash, int level);
130
131
protected:
132
bool FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering);
133
134
bool LoadIni();
135
bool LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverride = false);
136
void ParseHashRange(const std::string &key, const std::string &value);
137
void ParseFiltering(const std::string &key, const std::string &value);
138
void ParseReduceHashRange(const std::string& key, const std::string& value);
139
bool LookupHashRange(u32 addr, int w, int h, int *newW, int *newH);
140
float LookupReduceHashRange(int w, int h);
141
std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored);
142
143
static void ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
144
void ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
145
146
bool replaceEnabled_ = false;
147
bool saveEnabled_ = false;
148
bool allowVideo_ = false;
149
bool ignoreAddress_ = false;
150
bool reduceHash_ = false;
151
bool ignoreMipmap_ = false;
152
153
float reduceHashSize = 1.0f; // default value with reduceHash to false
154
float reduceHashGlobalValue = 0.5f; // Global value for textures dump pngs of all sizes, 0.5 by default but can be set in textures.ini
155
156
double lastTextureCacheSizeGB_ = 0.0;
157
std::string gameID_;
158
Path basePath_;
159
Path newTextureDir_;
160
ReplacedTextureHash hash_ = ReplacedTextureHash::QUICK;
161
162
VFSBackend *vfs_ = nullptr;
163
bool vfsIsZip_ = false;
164
165
GPUFormatSupport formatSupport_{};
166
167
typedef std::pair<int, int> WidthHeightPair;
168
std::unordered_map<u64, WidthHeightPair> hashranges_;
169
std::unordered_map<u64, float> reducehashranges_;
170
171
std::unordered_map<ReplacementCacheKey, std::string> aliases_;
172
std::unordered_map<ReplacementCacheKey, TextureFiltering> filtering_;
173
174
std::unordered_map<ReplacementCacheKey, ReplacedTextureRef> cache_;
175
std::unordered_map<ReplacementCacheKey, SavedTextureCacheData> savedCache_;
176
177
// the key is either from aliases_, in which case it's a |-separated sequence of texture filenames of the levels of a texture.
178
// alternatively the key is from the generated texture filename.
179
std::unordered_map<std::string, ReplacedTexture *> levelCache_;
180
};
181
182