ppsspp/GPU/GLES/TextureCache.h

171 lines
5.1 KiB
C
Raw Normal View History

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
// 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-01 15:19:01 +00:00
#include "../Globals.h"
#include "gfx_es2/fbo.h"
#include "GPU/GPUInterface.h"
2013-02-17 00:06:06 +00:00
#include "GPU/GPUState.h"
2013-04-30 19:44:34 +00:00
#include "TextureScaler.h"
2012-11-01 15:19:01 +00:00
struct VirtualFramebuffer;
class FramebufferManager;
enum TextureFiltering {
AUTO = 1,
2013-07-23 11:28:01 +00:00
NEAREST = 2,
LINEAR = 3,
2013-07-23 11:28:01 +00:00
LINEARFMV = 4,
};
enum FramebufferNotification {
NOTIFY_FB_CREATED,
NOTIFY_FB_UPDATED,
NOTIFY_FB_DESTROYED,
};
class TextureCache {
2013-01-30 19:40:26 +00:00
public:
TextureCache();
~TextureCache();
void SetTexture(bool force = false);
2013-01-30 19:40:26 +00:00
void Clear(bool delete_them);
void StartFrame();
void Invalidate(u32 addr, int size, GPUInvalidationType type);
void InvalidateAll(GPUInvalidationType type);
void ClearNextFrame();
void LoadClut();
2013-01-30 19:40:26 +00:00
// FramebufferManager keeps TextureCache updated about what regions of memory
// are being rendered to. This is barebones so far.
void NotifyFramebuffer(u32 address, VirtualFramebuffer *framebuffer, FramebufferNotification msg);
void SetFramebufferManager(FramebufferManager *fbManager) {
framebufferManager_ = fbManager;
}
2013-01-30 19:40:26 +00:00
size_t NumLoadedTextures() const {
return cache.size();
}
// Only used by Qt UI?
2013-02-17 00:06:06 +00:00
bool DecodeTexture(u8 *output, GPUgstate state);
2013-01-30 19:40:26 +00:00
private:
// Wow this is starting to grow big. Soon need to start looking at resizing it.
// Must stay a POD.
2013-01-30 19:40:26 +00:00
struct TexCacheEntry {
// After marking STATUS_UNRELIABLE, if it stays the same this many frames we'll trust it again.
const static int FRAMES_REGAIN_TRUST = 1000;
enum Status {
STATUS_HASHING = 0x00,
STATUS_RELIABLE = 0x01, // Don't bother rehashing.
STATUS_UNRELIABLE = 0x02, // Always recheck hash.
STATUS_MASK = 0x03,
STATUS_ALPHA_UNKNOWN = 0x04,
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.)
STATUS_ALPHA_MASK = 0x0c,
STATUS_CHANGE_FREQUENT = 0x10, // Changes often (less than 15 frames in between.)
};
// Status, but int so we can zero initialize.
int status;
2013-01-30 19:40:26 +00:00
u32 addr;
u32 hash;
VirtualFramebuffer *framebuffer; // if null, not sourced from an FBO.
2013-01-30 19:40:26 +00:00
u32 sizeInRAM;
int lastFrame;
int numFrames;
int numInvalidated;
u32 framesUntilNextFullHash;
u8 format;
u16 dim;
u16 bufw;
2013-01-30 19:40:26 +00:00
u32 texture; //GLuint
int invalidHint;
u32 fullhash;
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.
// (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;
bool Matches(u16 dim2, u8 format2, int maxLevel2);
2013-01-30 19:40:26 +00:00
};
void Decimate(); // Run this once per frame to get rid of old textures.
void *UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 bytesPerPixel, u32 level);
void *ReadIndexedTex(int level, const u8 *texptr, int bytesPerIndex, GLuint dstFmt, int bufw);
2013-01-30 19:40:26 +00:00
void UpdateSamplingParams(TexCacheEntry &entry, bool force);
void LoadTextureLevel(TexCacheEntry &entry, int level, bool replaceImages, int scaleFactor, GLenum dstFmt);
GLenum GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const;
void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, GLenum dstFmt, int *bufw = 0);
void CheckAlpha(TexCacheEntry &entry, u32 *pixelData, GLenum dstFmt, int stride, int w, int h);
template <typename T>
const T *GetCurrentClut();
u32 GetCurrentClutHash();
void UpdateCurrentClut();
void AttachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer, bool exactMatch);
void DetachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer);
2013-09-01 18:40:35 +00:00
void SetTextureFramebuffer(TexCacheEntry *entry);
2013-01-30 19:40:26 +00:00
TexCacheEntry *GetEntryAt(u32 texaddr);
2013-01-30 19:40:26 +00:00
typedef std::map<u64, TexCacheEntry> TexCache;
TexCache cache;
TexCache secondCache;
std::vector<VirtualFramebuffer *> fbCache_;
2013-01-30 19:40:26 +00:00
bool clearCacheNextFrame_;
bool lowMemoryMode_;
2013-04-30 19:44:34 +00:00
TextureScaler scaler;
SimpleBuf<u32> tmpTexBuf32;
SimpleBuf<u16> tmpTexBuf16;
2013-01-30 19:40:26 +00:00
SimpleBuf<u32> tmpTexBufRearrange;
2013-01-30 19:40:26 +00:00
u32 clutLastFormat_;
u32 *clutBufRaw_;
u32 *clutBufConverted_;
u32 *clutBuf_;
u32 clutHash_;
u32 clutTotalBytes_;
// True if the clut is just alpha values in the same order (RGBA4444-bit only.)
bool clutAlphaLinear_;
u16 clutAlphaLinearColor_;
u32 lastBoundTexture;
float maxAnisotropyLevel;
int decimationCounter_;
FramebufferManager *framebufferManager_;
2013-01-30 19:40:26 +00:00
};
2012-11-01 15:19:01 +00:00