2013-08-17 09:23:51 +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.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "../Globals.h"
|
|
|
|
#include "helper/global.h"
|
|
|
|
#include "helper/fbo.h"
|
|
|
|
#include "GPU/GPUInterface.h"
|
|
|
|
#include "GPU/GPUState.h"
|
2013-09-15 10:46:14 +00:00
|
|
|
#include "GPU/Directx9/TextureScalerDX9.h"
|
2014-09-13 11:53:04 +00:00
|
|
|
#include "GPU/Common/TextureCacheCommon.h"
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2014-09-09 15:12:42 +00:00
|
|
|
struct VirtualFramebuffer;
|
|
|
|
|
2013-09-15 15:53:21 +00:00
|
|
|
namespace DX9 {
|
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
class FramebufferManagerDX9;
|
|
|
|
class ShaderManagerDX9;
|
2013-08-17 09:23:51 +00:00
|
|
|
|
|
|
|
enum TextureFiltering {
|
|
|
|
AUTO = 1,
|
|
|
|
NEAREST = 2,
|
|
|
|
LINEAR = 3,
|
|
|
|
LINEARFMV = 4,
|
|
|
|
};
|
2013-08-19 18:36:43 +00:00
|
|
|
|
|
|
|
enum FramebufferNotification {
|
|
|
|
NOTIFY_FB_CREATED,
|
|
|
|
NOTIFY_FB_UPDATED,
|
|
|
|
NOTIFY_FB_DESTROYED,
|
|
|
|
};
|
|
|
|
|
2014-09-13 11:53:04 +00:00
|
|
|
class TextureCacheDX9 : public TextureCacheCommon {
|
2013-08-17 09:23:51 +00:00
|
|
|
public:
|
2013-09-15 10:46:14 +00:00
|
|
|
TextureCacheDX9();
|
|
|
|
~TextureCacheDX9();
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
void SetTexture(bool force = false);
|
|
|
|
bool SetOffsetTexture(u32 offset);
|
2013-08-17 09:23:51 +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();
|
|
|
|
|
|
|
|
// FramebufferManager keeps TextureCache updated about what regions of memory
|
|
|
|
// are being rendered to. This is barebones so far.
|
2014-09-09 15:12:42 +00:00
|
|
|
void NotifyFramebuffer(u32 address, VirtualFramebuffer *framebuffer, FramebufferNotification msg);
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
void SetFramebufferManager(FramebufferManagerDX9 *fbManager) {
|
|
|
|
framebufferManager_ = fbManager;
|
|
|
|
}
|
|
|
|
void SetShaderManager(ShaderManagerDX9 *sm) {
|
|
|
|
shaderManager_ = sm;
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
size_t NumLoadedTextures() const {
|
|
|
|
return cache.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only used by Qt UI?
|
|
|
|
bool DecodeTexture(u8 *output, GPUgstate state);
|
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
void ForgetLastTexture();
|
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
// Wow this is starting to grow big. Soon need to start looking at resizing it.
|
|
|
|
// Must stay a POD.
|
|
|
|
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,
|
2014-09-09 07:53:01 +00:00
|
|
|
STATUS_RELIABLE = 0x01, // Don't bother rehashing.
|
|
|
|
STATUS_UNRELIABLE = 0x02, // Always recheck hash.
|
2013-08-17 09:23:51 +00:00
|
|
|
STATUS_MASK = 0x03,
|
|
|
|
|
|
|
|
STATUS_ALPHA_UNKNOWN = 0x04,
|
2014-09-09 07:53:01 +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-08-17 09:23:51 +00:00
|
|
|
STATUS_ALPHA_MASK = 0x0c,
|
2014-09-09 07:53:01 +00:00
|
|
|
|
|
|
|
STATUS_CHANGE_FREQUENT = 0x10, // Changes often (less than 15 frames in between.)
|
|
|
|
STATUS_CLUT_RECHECK = 0x20, // Another texture with same addr had a hashfail.
|
|
|
|
STATUS_DEPALETTIZE = 0x40, // Needs to go through a depalettize pass.
|
|
|
|
STATUS_TO_SCALE = 0x80, // Pending texture scaling in a later frame.
|
2013-08-17 09:23:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Status, but int so we can zero initialize.
|
|
|
|
int status;
|
|
|
|
u32 addr;
|
|
|
|
u32 hash;
|
2014-09-09 15:12:42 +00:00
|
|
|
VirtualFramebuffer *framebuffer; // if null, not sourced from an FBO.
|
2013-08-17 09:23:51 +00:00
|
|
|
u32 sizeInRAM;
|
|
|
|
int lastFrame;
|
|
|
|
int numFrames;
|
|
|
|
int numInvalidated;
|
|
|
|
u32 framesUntilNextFullHash;
|
|
|
|
u8 format;
|
|
|
|
u16 dim;
|
|
|
|
u16 bufw;
|
2014-09-09 07:53:01 +00:00
|
|
|
LPDIRECT3DTEXTURE9 texture;
|
2013-08-17 09:23:51 +00:00
|
|
|
int invalidHint;
|
|
|
|
u32 fullhash;
|
|
|
|
u32 cluthash;
|
|
|
|
int maxLevel;
|
2014-09-09 07:53:01 +00:00
|
|
|
float lodBias;
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2014-09-09 07:53:01 +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;
|
|
|
|
}
|
|
|
|
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-08-17 09:23:51 +00:00
|
|
|
bool Matches(u16 dim2, u8 format2, int maxLevel2);
|
2014-08-25 09:15:02 +00:00
|
|
|
void ReleaseTexture() {
|
|
|
|
if (texture) {
|
|
|
|
texture->Release();
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 09:23:51 +00:00
|
|
|
};
|
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
void SetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight);
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::map<u64, TexCacheEntry> TexCache;
|
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
void Decimate(); // Run this once per frame to get rid of old textures.
|
2014-09-09 07:53:01 +00:00
|
|
|
void DeleteTexture(TexCache::iterator it);
|
2014-09-09 03:55:56 +00:00
|
|
|
void *UnswizzleFromMem(const u8 *texptr, u32 bufw, u32 bytesPerPixel, u32 level);
|
|
|
|
void *ReadIndexedTex(int level, const u8 *texptr, int bytesPerIndex, u32 dstFmt, int bufw);
|
2014-09-09 07:53:01 +00:00
|
|
|
void GetSamplingParams(int &minFilt, int &magFilt, bool &sClamp, bool &tClamp, float &lodBias, int maxLevel);
|
2013-08-17 09:23:51 +00:00
|
|
|
void UpdateSamplingParams(TexCacheEntry &entry, bool force);
|
2014-09-09 07:53:01 +00:00
|
|
|
void LoadTextureLevel(TexCacheEntry &entry, int level, bool replaceImages, int scaleFactor, u32 dstFmt);
|
|
|
|
D3DFORMAT GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const;
|
2014-09-09 03:55:56 +00:00
|
|
|
void *DecodeTextureLevel(GETextureFormat format, GEPaletteFormat clutformat, int level, u32 &texByteAlign, u32 &dstFmt, int *bufw = 0);
|
|
|
|
TexCacheEntry::Status CheckAlpha(const u32 *pixelData, u32 dstFmt, int stride, int w, int h);
|
2013-08-17 09:23:51 +00:00
|
|
|
template <typename T>
|
|
|
|
const T *GetCurrentClut();
|
|
|
|
u32 GetCurrentClutHash();
|
|
|
|
void UpdateCurrentClut();
|
2014-09-09 15:12:42 +00:00
|
|
|
bool AttachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer, u32 texaddrOffset = 0);
|
|
|
|
void DetachFramebuffer(TexCacheEntry *entry, u32 address, VirtualFramebuffer *framebuffer);
|
|
|
|
void SetTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer);
|
2013-08-17 09:23:51 +00:00
|
|
|
|
|
|
|
TexCacheEntry *GetEntryAt(u32 texaddr);
|
|
|
|
|
|
|
|
TexCache cache;
|
|
|
|
TexCache secondCache;
|
2014-09-09 15:12:42 +00:00
|
|
|
std::vector<VirtualFramebuffer *> fbCache_;
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
// Separate to keep main texture cache size down.
|
|
|
|
struct AttachedFramebufferInfo {
|
|
|
|
u32 xOffset;
|
|
|
|
u32 yOffset;
|
|
|
|
};
|
|
|
|
std::map<u32, AttachedFramebufferInfo> fbTexInfo_;
|
2014-09-09 15:12:42 +00:00
|
|
|
void AttachFramebufferValid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
|
|
|
|
void AttachFramebufferInvalid(TexCacheEntry *entry, VirtualFramebuffer *framebuffer, const AttachedFramebufferInfo &fbInfo);
|
2014-09-09 07:53:01 +00:00
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
bool clearCacheNextFrame_;
|
|
|
|
bool lowMemoryMode_;
|
2013-09-15 10:46:14 +00:00
|
|
|
TextureScalerDX9 scaler;
|
2013-08-17 09:23:51 +00:00
|
|
|
|
|
|
|
SimpleBuf<u32> tmpTexBuf32;
|
|
|
|
SimpleBuf<u16> tmpTexBuf16;
|
|
|
|
|
|
|
|
SimpleBuf<u32> tmpTexBufRearrange;
|
|
|
|
|
|
|
|
u32 clutLastFormat_;
|
|
|
|
u32 *clutBufRaw_;
|
|
|
|
u32 *clutBufConverted_;
|
|
|
|
u32 *clutBuf_;
|
|
|
|
u32 clutHash_;
|
|
|
|
u32 clutTotalBytes_;
|
2014-09-09 07:53:01 +00:00
|
|
|
u32 clutMaxBytes_;
|
2013-08-17 09:23:51 +00:00
|
|
|
// True if the clut is just alpha values in the same order (RGBA4444-bit only.)
|
|
|
|
bool clutAlphaLinear_;
|
|
|
|
u16 clutAlphaLinearColor_;
|
|
|
|
|
|
|
|
LPDIRECT3DTEXTURE9 lastBoundTexture;
|
|
|
|
float maxAnisotropyLevel;
|
2013-08-19 18:36:43 +00:00
|
|
|
|
|
|
|
int decimationCounter_;
|
2014-09-09 07:53:01 +00:00
|
|
|
int texelsScaledThisFrame_;
|
|
|
|
int timesInvalidatedAllThisFrame_;
|
|
|
|
|
|
|
|
FramebufferManagerDX9 *framebufferManager_;
|
|
|
|
ShaderManagerDX9 *shaderManager_;
|
2013-08-17 09:23:51 +00:00
|
|
|
};
|
|
|
|
|
2014-09-09 07:53:01 +00:00
|
|
|
D3DFORMAT getClutDestFormat(GEPaletteFormat format);
|
|
|
|
|
2013-09-15 15:53:21 +00:00
|
|
|
};
|