2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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
|
2012-11-26 03:25:14 +00:00
|
|
|
|
2014-10-19 19:11:09 +00:00
|
|
|
#include <map>
|
|
|
|
|
2013-01-31 23:18:23 +00:00
|
|
|
#include "gfx_es2/fbo.h"
|
2014-05-27 20:22:56 +00:00
|
|
|
#include "gfx_es2/gpu_features.h"
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2013-05-06 05:52:01 +00:00
|
|
|
#include "GPU/GPUInterface.h"
|
2013-02-17 00:06:06 +00:00
|
|
|
#include "GPU/GPUState.h"
|
2015-01-22 18:53:32 +00:00
|
|
|
#include "GPU/GLES/TextureScaler.h"
|
2014-09-13 11:15:18 +00:00
|
|
|
#include "GPU/Common/TextureCacheCommon.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-02-21 20:37:19 +00:00
|
|
|
struct VirtualFramebuffer;
|
2014-01-20 01:28:11 +00:00
|
|
|
class FramebufferManager;
|
2014-03-29 20:58:38 +00:00
|
|
|
class DepalShaderCache;
|
2014-06-01 01:55:00 +00:00
|
|
|
class ShaderManager;
|
2013-02-21 20:37:19 +00:00
|
|
|
|
2013-07-29 09:12:27 +00:00
|
|
|
enum TextureFiltering {
|
2013-07-29 08:46:40 +00:00
|
|
|
AUTO = 1,
|
2013-07-23 11:28:01 +00:00
|
|
|
NEAREST = 2,
|
2013-10-08 13:00:48 +00:00
|
|
|
LINEAR = 3,
|
2013-07-23 11:28:01 +00:00
|
|
|
LINEARFMV = 4,
|
2013-07-19 13:48:50 +00:00
|
|
|
};
|
2013-07-29 08:46:40 +00:00
|
|
|
|
2013-08-01 06:19:15 +00:00
|
|
|
enum FramebufferNotification {
|
|
|
|
NOTIFY_FB_CREATED,
|
|
|
|
NOTIFY_FB_UPDATED,
|
|
|
|
NOTIFY_FB_DESTROYED,
|
|
|
|
};
|
|
|
|
|
2014-05-27 20:22:56 +00:00
|
|
|
inline bool UseBGRA8888() {
|
|
|
|
// TODO: Other platforms? May depend on vendor which is faster?
|
|
|
|
#ifdef _WIN32
|
|
|
|
return gl_extensions.EXT_bgra;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:15:18 +00:00
|
|
|
class TextureCache : public TextureCacheCommon {
|
2013-01-30 19:40:26 +00:00
|
|
|
public:
|
|
|
|
TextureCache();
|
|
|
|
~TextureCache();
|
|
|
|
|
2013-09-28 04:53:42 +00:00
|
|
|
void SetTexture(bool force = false);
|
2014-09-14 07:34:52 +00:00
|
|
|
virtual bool SetOffsetTexture(u32 offset) override;
|
2013-01-30 19:40:26 +00:00
|
|
|
|
|
|
|
void Clear(bool delete_them);
|
|
|
|
void StartFrame();
|
2013-05-06 05:52:01 +00:00
|
|
|
void Invalidate(u32 addr, int size, GPUInvalidationType type);
|
|
|
|
void InvalidateAll(GPUInvalidationType type);
|
2013-04-30 15:20:28 +00:00
|
|
|
void ClearNextFrame();
|
2013-05-12 17:57:41 +00:00
|
|
|
void LoadClut();
|
2013-01-30 19:40:26 +00:00
|
|
|
|
2013-01-31 23:18:23 +00:00
|
|
|
// FramebufferManager keeps TextureCache updated about what regions of memory
|
|
|
|
// are being rendered to. This is barebones so far.
|
2013-08-01 06:19:15 +00:00
|
|
|
void NotifyFramebuffer(u32 address, VirtualFramebuffer *framebuffer, FramebufferNotification msg);
|
2013-01-31 23:18:23 +00:00
|
|
|
|
2014-01-20 01:28:11 +00:00
|
|
|
void SetFramebufferManager(FramebufferManager *fbManager) {
|
|
|
|
framebufferManager_ = fbManager;
|
|
|
|
}
|
2014-03-29 20:58:38 +00:00
|
|
|
void SetDepalShaderCache(DepalShaderCache *dpCache) {
|
|
|
|
depalShaderCache_ = dpCache;
|
|
|
|
}
|
2014-06-01 01:55:00 +00:00
|
|
|
void SetShaderManager(ShaderManager *sm) {
|
|
|
|
shaderManager_ = sm;
|
|
|
|
}
|
2014-01-20 01:28:11 +00:00
|
|
|
|
2013-01-30 19:40:26 +00:00
|
|
|
size_t NumLoadedTextures() const {
|
|
|
|
return cache.size();
|
|
|
|
}
|
|
|
|
|
2014-05-27 09:00:49 +00:00
|
|
|
void ForgetLastTexture() {
|
|
|
|
lastBoundTexture = -1;
|
|
|
|
gstate_c.textureChanged |= TEXCHANGE_PARAMSONLY;
|
|
|
|
}
|
|
|
|
|
2014-06-07 22:25:35 +00:00
|
|
|
u32 AllocTextureName();
|
|
|
|
|
2013-05-02 08:56:30 +00:00
|
|
|
// Only used by Qt UI?
|
2015-01-17 19:57:07 +00:00
|
|
|
bool DecodeTexture(u8 *output, const GPUgstate &state);
|
2013-02-21 20:37:19 +00:00
|
|
|
|
2013-04-27 17:35:42 +00:00
|
|
|
// Wow this is starting to grow big. Soon need to start looking at resizing it.
|
2013-06-11 18:21:19 +00:00
|
|
|
// Must stay a POD.
|
2013-01-30 19:40:26 +00:00
|
|
|
struct TexCacheEntry {
|
2013-02-11 07:29:44 +00:00
|
|
|
// After marking STATUS_UNRELIABLE, if it stays the same this many frames we'll trust it again.
|
|
|
|
const static int FRAMES_REGAIN_TRUST = 1000;
|
|
|
|
|
2013-02-10 22:11:53 +00:00
|
|
|
enum Status {
|
2013-05-08 14:52:54 +00:00
|
|
|
STATUS_HASHING = 0x00,
|
2014-01-20 04:54:48 +00:00
|
|
|
STATUS_RELIABLE = 0x01, // Don't bother rehashing.
|
|
|
|
STATUS_UNRELIABLE = 0x02, // Always recheck hash.
|
2013-05-08 14:52:54 +00:00
|
|
|
STATUS_MASK = 0x03,
|
|
|
|
|
|
|
|
STATUS_ALPHA_UNKNOWN = 0x04,
|
2014-01-20 04:54:48 +00:00
|
|
|
STATUS_ALPHA_FULL = 0x00, // Has no alpha channel, or always full alpha.
|
|
|
|
STATUS_ALPHA_SIMPLE = 0x08, // Like above, but also has 0 alpha (e.g. 5551.)
|
2013-05-08 14:52:54 +00:00
|
|
|
STATUS_ALPHA_MASK = 0x0c,
|
2014-01-20 04:54:48 +00:00
|
|
|
|
|
|
|
STATUS_CHANGE_FREQUENT = 0x10, // Changes often (less than 15 frames in between.)
|
2014-04-13 23:19:18 +00:00
|
|
|
STATUS_CLUT_RECHECK = 0x20, // Another texture with same addr had a hashfail.
|
2014-06-15 20:34:58 +00:00
|
|
|
STATUS_DEPALETTIZE = 0x40, // Needs to go through a depalettize pass.
|
|
|
|
STATUS_TO_SCALE = 0x80, // Pending texture scaling in a later frame.
|
2013-02-10 22:11:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Status, but int so we can zero initialize.
|
|
|
|
int status;
|
2013-01-30 19:40:26 +00:00
|
|
|
u32 addr;
|
|
|
|
u32 hash;
|
2013-02-21 20:37:19 +00:00
|
|
|
VirtualFramebuffer *framebuffer; // if null, not sourced from an FBO.
|
2013-01-30 19:40:26 +00:00
|
|
|
u32 sizeInRAM;
|
2013-02-10 21:19:01 +00:00
|
|
|
int lastFrame;
|
|
|
|
int numFrames;
|
2013-05-01 18:31:57 +00:00
|
|
|
int numInvalidated;
|
2013-02-10 21:19:01 +00:00
|
|
|
u32 framesUntilNextFullHash;
|
2013-01-31 23:18:23 +00:00
|
|
|
u8 format;
|
2013-04-27 17:35:42 +00:00
|
|
|
u16 dim;
|
2013-07-28 03:20:39 +00:00
|
|
|
u16 bufw;
|
2013-01-30 19:40:26 +00:00
|
|
|
u32 texture; //GLuint
|
|
|
|
int invalidHint;
|
|
|
|
u32 fullhash;
|
2013-05-20 07:55:53 +00:00
|
|
|
u32 cluthash;
|
2013-02-07 23:04:34 +00:00
|
|
|
int maxLevel;
|
|
|
|
float lodBias;
|
2013-01-30 19:40:26 +00:00
|
|
|
|
|
|
|
// Cache the current filter settings so we can avoid setting it again.
|
2013-04-27 17:35:42 +00:00
|
|
|
// (OpenGL madness where filter settings are attached to each texture).
|
2013-01-30 19:40:26 +00:00
|
|
|
u8 magFilt;
|
|
|
|
u8 minFilt;
|
|
|
|
bool sClamp;
|
|
|
|
bool tClamp;
|
2013-05-05 03:33:31 +00:00
|
|
|
|
2014-04-13 19:13:02 +00:00
|
|
|
Status GetHashStatus() {
|
|
|
|
return Status(status & STATUS_MASK);
|
|
|
|
}
|
|
|
|
void SetHashStatus(Status newStatus) {
|
|
|
|
status = (status & ~STATUS_MASK) | newStatus;
|
|
|
|
}
|
|
|
|
Status GetAlphaStatus() {
|
|
|
|
return Status(status & STATUS_ALPHA_MASK);
|
|
|
|
}
|
|
|
|
void SetAlphaStatus(Status newStatus) {
|
|
|
|
status = (status & ~STATUS_ALPHA_MASK) | newStatus;
|
|
|
|
}
|
2014-06-03 07:59:34 +00:00
|
|
|
void SetAlphaStatus(Status newStatus, int level) {
|
|
|
|
// For non-level zero, only set more restrictive.
|
|
|
|
if (newStatus == STATUS_ALPHA_UNKNOWN || level == 0) {
|
|
|
|
SetAlphaStatus(newStatus);
|
|
|
|
} else if (newStatus == STATUS_ALPHA_SIMPLE && GetAlphaStatus() == STATUS_ALPHA_FULL) {
|
|
|
|
SetAlphaStatus(STATUS_ALPHA_SIMPLE);
|
|
|
|
}
|
|
|
|
}
|
2013-05-26 20:44:47 +00:00
|
|
|
bool Matches(u16 dim2, u8 format2, int maxLevel2);
|
2013-01-30 19:40:26 +00:00
|
|
|
};
|
|
|
|
|
2014-06-15 20:34:58 +00:00
|
|
|
void SetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight);
|
|
|
|
|
2014-04-01 12:14:56 +00:00
|
|
|
private:
|
2014-10-19 19:11:09 +00:00
|
|
|
// Can't be unordered_map, we use lower_bound ... although for some reason that compiles on MSVC.
|
2014-06-08 20:52:05 +00:00
|
|
|
typedef std::map<u64, TexCacheEntry> TexCache;
|
|
|
|
|
2013-01-30 19:40:26 +00:00
|
|
|
void Decimate(); // Run this once per frame to get rid of old textures.
|
2014-06-08 20:52:05 +00:00
|
|
|
void DeleteTexture(TexCache::iterator it);
|
2014-01-20 00:13:55 +00:00
|
|
|
void *UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 bytesPerPixel, u32 level);
|
|
|
|
void *ReadIndexedTex(int level, const u8 *texptr, int bytesPerIndex, GLuint dstFmt, int bufw);
|
2014-06-15 20:34:58 +00:00
|
|
|
void GetSamplingParams(int &minFilt, int &magFilt, bool &sClamp, bool &tClamp, float &lodBias, int maxLevel);
|
2013-01-30 19:40:26 +00:00
|
|
|
void UpdateSamplingParams(TexCacheEntry &entry, bool force);
|
2014-04-01 22:13:50 +00:00
|
|
|
void LoadTextureLevel(TexCacheEntry &entry, int level, bool replaceImages, int scaleFactor, GLenum dstFmt);
|
2013-10-08 13:00:48 +00:00
|
|
|
GLenum GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const;
|
2013-10-31 01:39:46 +00:00
|
|
|
void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, GLenum dstFmt, int *bufw = 0);
|
2014-09-09 03:55:56 +00:00
|
|
|
TexCacheEntry::Status CheckAlpha(const u32 *pixelData, GLenum dstFmt, int stride, int w, int h);
|
2013-05-11 22:45:17 +00:00
|
|
|
template <typename T>
|
|
|
|
const T *GetCurrentClut();
|
|
|
|
u32 GetCurrentClutHash();
|
2013-05-12 17:57:41 +00:00
|
|
|
void UpdateCurrentClut();
|
2014-06-14 16:50:54 +00:00
|
|
|
bool AttachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer, u32 texaddrOffset = 0);
|
2013-08-01 06:19:15 +00:00
|
|
|
void DetachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer);
|
2014-06-05 06:50:02 +00:00
|
|
|
void SetTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer);
|
2013-01-30 19:40:26 +00:00
|
|
|
|
2013-01-31 23:18:23 +00:00
|
|
|
TexCacheEntry *GetEntryAt(u32 texaddr);
|
|
|
|
|
2013-01-30 19:40:26 +00:00
|
|
|
TexCache cache;
|
2013-05-05 03:33:31 +00:00
|
|
|
TexCache secondCache;
|
2013-09-01 18:44:49 +00:00
|
|
|
std::vector<VirtualFramebuffer *> fbCache_;
|
2014-06-07 22:25:35 +00:00
|
|
|
std::vector<u32> nameCache_;
|
2014-12-22 00:54:26 +00:00
|
|
|
u32 cacheSizeEstimate_;
|
|
|
|
u32 secondCacheSizeEstimate_;
|
2013-01-30 19:40:26 +00:00
|
|
|
|
2014-06-08 20:52:05 +00:00
|
|
|
// Separate to keep main texture cache size down.
|
|
|
|
struct AttachedFramebufferInfo {
|
|
|
|
u32 xOffset;
|
|
|
|
u32 yOffset;
|
|
|
|
};
|
|
|
|
std::map<u32, AttachedFramebufferInfo> fbTexInfo_;
|
2014-06-10 05:45:14 +00:00
|
|
|
void AttachFramebufferValid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
|
|
|
|
void AttachFramebufferInvalid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
|
2014-06-08 20:52:05 +00:00
|
|
|
|
2013-04-30 15:20:28 +00:00
|
|
|
bool clearCacheNextFrame_;
|
2013-05-05 07:00:22 +00:00
|
|
|
bool lowMemoryMode_;
|
2013-04-30 19:44:34 +00:00
|
|
|
TextureScaler scaler;
|
2013-04-09 08:52:19 +00:00
|
|
|
|
|
|
|
SimpleBuf<u32> tmpTexBuf32;
|
|
|
|
SimpleBuf<u16> tmpTexBuf16;
|
2013-01-30 19:40:26 +00:00
|
|
|
|
2013-04-09 08:52:19 +00:00
|
|
|
SimpleBuf<u32> tmpTexBufRearrange;
|
2013-01-30 19:40:26 +00:00
|
|
|
|
2013-06-16 06:04:43 +00:00
|
|
|
u32 clutLastFormat_;
|
2013-05-26 19:45:53 +00:00
|
|
|
u32 *clutBufRaw_;
|
|
|
|
u32 *clutBufConverted_;
|
2013-05-11 22:45:17 +00:00
|
|
|
u32 *clutBuf_;
|
|
|
|
u32 clutHash_;
|
2013-05-26 19:41:36 +00:00
|
|
|
u32 clutTotalBytes_;
|
2014-08-20 05:28:26 +00:00
|
|
|
u32 clutMaxBytes_;
|
2013-05-12 16:15:31 +00:00
|
|
|
// True if the clut is just alpha values in the same order (RGBA4444-bit only.)
|
|
|
|
bool clutAlphaLinear_;
|
|
|
|
u16 clutAlphaLinearColor_;
|
2013-02-12 18:05:30 +00:00
|
|
|
|
2013-02-18 16:34:51 +00:00
|
|
|
u32 lastBoundTexture;
|
2013-02-14 23:30:02 +00:00
|
|
|
float maxAnisotropyLevel;
|
2013-07-31 22:13:58 +00:00
|
|
|
|
|
|
|
int decimationCounter_;
|
2014-06-13 20:07:26 +00:00
|
|
|
int texelsScaledThisFrame_;
|
2014-06-14 22:40:08 +00:00
|
|
|
int timesInvalidatedAllThisFrame_;
|
2014-06-13 20:07:26 +00:00
|
|
|
|
2014-01-20 01:28:11 +00:00
|
|
|
FramebufferManager *framebufferManager_;
|
2014-03-29 20:58:38 +00:00
|
|
|
DepalShaderCache *depalShaderCache_;
|
2014-06-01 01:55:00 +00:00
|
|
|
ShaderManager *shaderManager_;
|
2013-01-30 19:40:26 +00:00
|
|
|
};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-03-29 20:58:38 +00:00
|
|
|
GLenum getClutDestFormat(GEPaletteFormat format);
|