CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Common/TextureReplacer.h
Views: 1401
// Copyright (c) 2016- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include "ppsspp_config.h"2021#include <mutex>22#include <string>23#include <unordered_map>24#include <map>25#include <vector>2627#include "Common/CommonFuncs.h"28#include "Common/CommonTypes.h"29#include "Common/MemoryUtil.h"30#include "Common/File/Path.h"31#include "Common/File/VFS/VFS.h"32#include "Common/GPU/DataFormat.h"3334#include "GPU/Common/TextureDecoder.h"35#include "GPU/Common/ReplacedTexture.h"36#include "GPU/ge_constants.h"3738class IniFile;39class TextureCacheCommon;40class TextureReplacer;41class ReplacedTextureTask;42class LimitedWaitable;43class VFSBackend;4445struct SavedTextureCacheData {46int levelW[8]{};47int levelH[8]{};48bool levelSaved[8]{};49double lastTimeSaved = 0.0;50};5152struct ReplacementCacheKey {53u64 cachekey;54u32 hash;5556ReplacementCacheKey(u64 c, u32 h) : cachekey(c), hash(h) { }5758bool operator ==(const ReplacementCacheKey &k) const {59return k.cachekey == cachekey && k.hash == hash;60}6162bool operator <(const ReplacementCacheKey &k) const {63if (k.cachekey == cachekey) {64return k.hash < hash;65}66return k.cachekey < cachekey;67}68};6970namespace std {71template <>72struct hash<ReplacementCacheKey> {73size_t operator()(const ReplacementCacheKey &k) const {74return std::hash<u64>()(k.cachekey ^ ((u64)k.hash << 32));75}76};77}7879struct ReplacedTextureDecodeInfo {80u64 cachekey;81u32 hash;82u32 addr;83bool isVideo;84bool isFinal;85Draw::DataFormat fmt;86};8788enum class ReplacerDecimateMode {89NEW_FRAME,90FORCE_PRESSURE,91ALL,92};9394class TextureReplacer {95public:96// The draw context is checked for supported texture formats.97TextureReplacer(Draw::DrawContext *draw);98~TextureReplacer();99100void NotifyConfigChanged();101102bool Enabled() const { return replaceEnabled_ || saveEnabled_; } // used to check hashing method etc.103bool ReplaceEnabled() const { return replaceEnabled_; }104bool SaveEnabled() const { return saveEnabled_; }105106bool AllowVideo() const { return allowVideo_; }107108u32 ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV);109110// Returns nullptr if not found.111ReplacedTexture *FindReplacement(u64 cachekey, u32 hash, int w, int h);112113// Check if a NotifyTextureDecoded for this texture is desired (used to avoid reads from write-combined memory.)114bool WillSave(const ReplacedTextureDecodeInfo &replacedInfo) const;115116// Notify that a new texture was decoded. May already be upscaled, saves the data passed.117// If the replacer knows about this one already, texture will be passed in, otherwise nullptr.118void NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int origW, int origH, int scaledW, int scaledH);119120void Decimate(ReplacerDecimateMode mode);121122static bool GenerateIni(const std::string &gameID, Path &generatedFilename);123static bool IniExists(const std::string &gameID);124125int GetNumTrackedTextures() const { return (int)cache_.size(); }126int GetNumCachedReplacedTextures() const { return (int)levelCache_.size(); }127128static std::string HashName(u64 cachekey, u32 hash, int level);129130protected:131bool FindFiltering(u64 cachekey, u32 hash, TextureFiltering *forceFiltering);132133bool LoadIni();134bool LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverride = false);135void ParseHashRange(const std::string &key, const std::string &value);136void ParseFiltering(const std::string &key, const std::string &value);137void ParseReduceHashRange(const std::string& key, const std::string& value);138bool LookupHashRange(u32 addr, int w, int h, int *newW, int *newH);139float LookupReduceHashRange(int w, int h);140std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored);141142static void ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);143void ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);144145bool replaceEnabled_ = false;146bool saveEnabled_ = false;147bool allowVideo_ = false;148bool ignoreAddress_ = false;149bool reduceHash_ = false;150bool ignoreMipmap_ = false;151152float reduceHashSize = 1.0f; // default value with reduceHash to false153float reduceHashGlobalValue = 0.5f; // Global value for textures dump pngs of all sizes, 0.5 by default but can be set in textures.ini154155double lastTextureCacheSizeGB_ = 0.0;156std::string gameID_;157Path basePath_;158Path newTextureDir_;159ReplacedTextureHash hash_ = ReplacedTextureHash::QUICK;160161VFSBackend *vfs_ = nullptr;162bool vfsIsZip_ = false;163164GPUFormatSupport formatSupport_{};165166typedef std::pair<int, int> WidthHeightPair;167std::unordered_map<u64, WidthHeightPair> hashranges_;168std::unordered_map<u64, float> reducehashranges_;169170std::unordered_map<ReplacementCacheKey, std::string> aliases_;171std::unordered_map<ReplacementCacheKey, TextureFiltering> filtering_;172173std::unordered_map<ReplacementCacheKey, ReplacedTextureRef> cache_;174std::unordered_map<ReplacementCacheKey, SavedTextureCacheData> savedCache_;175176// the key is either from aliases_, in which case it's a |-separated sequence of texture filenames of the levels of a texture.177// alternatively the key is from the generated texture filename.178std::unordered_map<std::string, ReplacedTexture *> levelCache_;179};180181182