2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// 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 SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include <d3dx9.h>
|
|
|
|
|
2009-09-01 11:14:58 +00:00
|
|
|
#include "Globals.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "Statistics.h"
|
2009-03-07 18:05:29 +00:00
|
|
|
#include "MemoryUtil.h"
|
2009-09-01 11:14:58 +00:00
|
|
|
#include "Hash.h"
|
|
|
|
|
|
|
|
#include "CommonPaths.h"
|
|
|
|
#include "FileUtil.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
#include "D3DBase.h"
|
|
|
|
#include "D3DTexture.h"
|
2009-09-13 17:46:33 +00:00
|
|
|
#include "FramebufferManager.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
#include "Render.h"
|
|
|
|
|
|
|
|
#include "TextureDecoder.h"
|
|
|
|
#include "TextureCache.h"
|
|
|
|
|
2009-09-18 03:12:32 +00:00
|
|
|
#include "debugger/debugger.h"
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
u8 *TextureCache::temp = NULL;
|
|
|
|
TextureCache::TexCache TextureCache::textures;
|
|
|
|
|
|
|
|
extern int frameCount;
|
|
|
|
|
|
|
|
#define TEMP_SIZE (1024*1024*4)
|
2009-03-07 18:05:29 +00:00
|
|
|
#define TEXTURE_KILL_THRESHOLD 200
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-03-06 16:47:04 +00:00
|
|
|
void TextureCache::TCacheEntry::Destroy(bool shutdown)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
if (texture)
|
|
|
|
texture->Release();
|
|
|
|
texture = 0;
|
2009-09-19 13:14:55 +00:00
|
|
|
if (!isRenderTarget && !shutdown && !g_ActiveConfig.bSafeTextureCache)
|
2009-09-13 08:21:35 +00:00
|
|
|
{
|
2009-09-15 21:05:31 +00:00
|
|
|
u32 *ptr = (u32*)g_VideoInitialize.pGetMemoryPointer(addr);
|
2009-03-07 18:05:29 +00:00
|
|
|
if (ptr && *ptr == hash)
|
2008-12-08 05:25:12 +00:00
|
|
|
*ptr = oldpixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::Init()
|
|
|
|
{
|
2009-03-07 18:05:29 +00:00
|
|
|
temp = (u8*)AllocateMemoryPages(TEMP_SIZE);
|
2009-09-13 08:21:35 +00:00
|
|
|
TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, g_ActiveConfig.bTexFmtOverlayCenter);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-03-06 16:47:04 +00:00
|
|
|
void TextureCache::Invalidate(bool shutdown)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-03-07 18:05:29 +00:00
|
|
|
for (TexCache::iterator iter = textures.begin(); iter != textures.end(); iter++)
|
2009-03-06 16:47:04 +00:00
|
|
|
iter->second.Destroy(shutdown);
|
2008-12-08 05:25:12 +00:00
|
|
|
textures.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::Shutdown()
|
|
|
|
{
|
2009-03-06 16:47:04 +00:00
|
|
|
Invalidate(true);
|
2009-03-07 18:05:29 +00:00
|
|
|
FreeMemoryPages(temp, TEMP_SIZE);
|
|
|
|
temp = NULL;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::Cleanup()
|
|
|
|
{
|
2009-09-13 08:21:35 +00:00
|
|
|
TexCache::iterator iter = textures.begin();
|
|
|
|
while (iter != textures.end())
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-09-02 15:38:04 +00:00
|
|
|
if (frameCount > TEXTURE_KILL_THRESHOLD + iter->second.frameCount)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
if (!iter->second.isRenderTarget)
|
|
|
|
{
|
2009-03-06 16:47:04 +00:00
|
|
|
iter->second.Destroy(false);
|
2008-12-08 05:25:12 +00:00
|
|
|
iter = textures.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-07 18:05:29 +00:00
|
|
|
// Used to be just iter++
|
|
|
|
iter->second.Destroy(false);
|
|
|
|
iter = textures.erase(iter);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
TextureCache::TCacheEntry *TextureCache::Load(int stage, u32 address, int width, int height, int tex_format, int tlutaddr, int tlutfmt)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-09-02 18:55:36 +00:00
|
|
|
if (address == 0)
|
|
|
|
return NULL;
|
2009-09-13 17:46:33 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
u8 *ptr = g_VideoInitialize.pGetMemoryPointer(address);
|
2009-09-19 13:14:55 +00:00
|
|
|
int bsw = TexDecoder_GetBlockWidthInTexels(tex_format) - 1; //TexelSizeInNibbles(format)*width*height/16;
|
|
|
|
int bsh = TexDecoder_GetBlockHeightInTexels(tex_format) - 1; //TexelSizeInNibbles(format)*width*height/16;
|
|
|
|
int expandedWidth = (width + bsw) & (~bsw);
|
|
|
|
int expandedHeight = (height + bsh) & (~bsh);
|
2009-09-02 06:33:41 +00:00
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
u32 hash_value;
|
|
|
|
u32 texID = address;
|
|
|
|
u32 texHash;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache || g_ActiveConfig.bDumpTextures)
|
2009-09-04 05:26:19 +00:00
|
|
|
{
|
2009-09-19 13:14:55 +00:00
|
|
|
texHash = TexDecoder_GetSafeTextureHash(ptr, expandedWidth, expandedHeight, tex_format, 0);
|
|
|
|
if (g_ActiveConfig.bSafeTextureCache)
|
|
|
|
hash_value = texHash;
|
|
|
|
if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
|
2009-09-04 05:26:19 +00:00
|
|
|
{
|
2009-09-19 13:14:55 +00:00
|
|
|
// WARNING! texID != address now => may break CopyRenderTargetToTexture (cf. TODO up)
|
|
|
|
// tlut size can be up to 32768B (GX_TF_C14X2) but Safer == Slower.
|
|
|
|
// This trick (to change the texID depending on the TLUT addr) is a trick to get around
|
|
|
|
// an issue with metroid prime's fonts, where it has multiple sets of fonts on top of
|
|
|
|
// each other stored in a single texture, and uses the palette to make different characters
|
|
|
|
// visible or invisible. Thus, unless we want to recreate the textures for every drawn character,
|
|
|
|
// we must make sure that texture with different tluts get different IDs.
|
|
|
|
u32 tlutHash = TexDecoder_GetTlutHash(&texMem[tlutaddr], (tex_format == GX_TF_C4) ? 32 : 128);
|
|
|
|
texHash ^= tlutHash;
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache)
|
2009-09-11 05:05:00 +00:00
|
|
|
texID ^= tlutHash;
|
2009-09-04 05:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-02 06:33:41 +00:00
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
bool skip_texture_create = false;
|
2009-09-11 05:05:00 +00:00
|
|
|
TexCache::iterator iter = textures.find(texID);
|
2009-09-19 13:14:55 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
if (iter != textures.end())
|
|
|
|
{
|
|
|
|
TCacheEntry &entry = iter->second;
|
2009-09-19 13:14:55 +00:00
|
|
|
|
|
|
|
if (!g_ActiveConfig.bSafeTextureCache)
|
|
|
|
hash_value = ((u32 *)ptr)[0];
|
|
|
|
|
2009-03-07 18:05:29 +00:00
|
|
|
if (entry.isRenderTarget || ((address == entry.addr) && (hash_value == entry.hash)))
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-03-07 18:05:29 +00:00
|
|
|
entry.frameCount = frameCount;
|
2009-09-13 08:21:35 +00:00
|
|
|
D3D::SetTexture(stage, entry.texture);
|
2009-03-07 18:05:29 +00:00
|
|
|
return &entry;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-19 13:14:55 +00:00
|
|
|
// Let's reload the new texture data into the same texture,
|
|
|
|
// instead of destroying it and having to create a new one.
|
|
|
|
// Might speed up movie playback very, very slightly.
|
|
|
|
|
|
|
|
if (width == entry.w && height==entry.h && tex_format == entry.fmt)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-09-19 13:14:55 +00:00
|
|
|
skip_texture_create = true;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
else
|
2009-09-19 13:14:55 +00:00
|
|
|
{
|
2009-03-07 18:05:29 +00:00
|
|
|
entry.Destroy(false);
|
2008-12-08 05:25:12 +00:00
|
|
|
textures.erase(iter);
|
2009-09-19 13:14:55 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
PC_TexFormat pcfmt = TexDecoder_Decode(temp, ptr, expandedWidth, height, tex_format, tlutaddr, tlutfmt);
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
D3DFORMAT d3d_fmt;
|
|
|
|
switch (pcfmt) {
|
|
|
|
case PC_TEX_FMT_BGRA32:
|
|
|
|
d3d_fmt = D3DFMT_A8R8G8B8;
|
|
|
|
break;
|
2009-02-12 22:32:33 +00:00
|
|
|
case PC_TEX_FMT_RGB565:
|
|
|
|
d3d_fmt = D3DFMT_R5G6B5;
|
|
|
|
break;
|
2009-05-13 02:06:02 +00:00
|
|
|
case PC_TEX_FMT_IA4_AS_IA8:
|
2009-09-02 18:55:36 +00:00
|
|
|
d3d_fmt = D3DFMT_A8L8;
|
2009-02-14 09:04:40 +00:00
|
|
|
break;
|
2009-02-12 13:54:08 +00:00
|
|
|
case PC_TEX_FMT_I8:
|
2009-08-31 06:35:43 +00:00
|
|
|
case PC_TEX_FMT_I4_AS_I8:
|
2009-02-16 23:01:37 +00:00
|
|
|
d3d_fmt = D3DFMT_A8P8; // A hack which means the format is a packed
|
|
|
|
// 8-bit intensity texture. It is unpacked
|
|
|
|
// to A8L8 in D3DTexture.cpp
|
2009-02-13 14:14:45 +00:00
|
|
|
break;
|
|
|
|
case PC_TEX_FMT_IA8:
|
2009-02-12 13:54:08 +00:00
|
|
|
d3d_fmt = D3DFMT_A8L8;
|
|
|
|
break;
|
2009-02-16 23:01:37 +00:00
|
|
|
case PC_TEX_FMT_DXT1:
|
|
|
|
d3d_fmt = D3DFMT_DXT1;
|
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Make an entry in the table
|
2009-09-11 05:05:00 +00:00
|
|
|
TCacheEntry& entry = textures[texID];
|
2009-03-07 18:05:29 +00:00
|
|
|
|
2009-09-15 21:05:31 +00:00
|
|
|
entry.oldpixel = ((u32 *)ptr)[0];
|
2009-09-19 13:14:55 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache)
|
|
|
|
entry.hash = hash_value;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry.hash = (u32)(((double)rand() / RAND_MAX) * 0xFFFFFFFF);
|
|
|
|
((u32 *)ptr)[0] = entry.hash;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
entry.addr = address;
|
2009-09-19 13:14:55 +00:00
|
|
|
entry.size_in_bytes = TexDecoder_GetTextureSizeInBytes(width, height, tex_format);
|
2009-03-07 18:05:29 +00:00
|
|
|
entry.isRenderTarget = false;
|
|
|
|
entry.isNonPow2 = ((width & (width - 1)) || (height & (height - 1)));
|
2009-09-19 13:14:55 +00:00
|
|
|
if (!skip_texture_create) {
|
|
|
|
entry.texture = D3D::CreateTexture2D((BYTE*)temp, width, height, expandedWidth, d3d_fmt);
|
|
|
|
} else {
|
|
|
|
D3D::ReplaceTexture2D(entry.texture, (BYTE*)temp, width, height, expandedWidth, d3d_fmt);
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
entry.frameCount = frameCount;
|
2009-03-07 18:05:29 +00:00
|
|
|
entry.w = width;
|
|
|
|
entry.h = height;
|
2009-09-19 13:14:55 +00:00
|
|
|
entry.fmt = tex_format;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bDumpTextures)
|
2009-09-19 13:14:55 +00:00
|
|
|
{
|
|
|
|
// dump texture to file
|
2008-12-08 05:25:12 +00:00
|
|
|
char szTemp[MAX_PATH];
|
2009-09-01 11:14:58 +00:00
|
|
|
char szDir[MAX_PATH];
|
2009-09-13 08:54:46 +00:00
|
|
|
const char* uniqueId = globals->unique_id;
|
2009-09-01 11:14:58 +00:00
|
|
|
bool bCheckedDumpDir = false;
|
2009-09-15 21:05:31 +00:00
|
|
|
sprintf(szDir, "%s/%s", FULL_DUMP_TEXTURES_DIR, uniqueId);
|
|
|
|
if (!bCheckedDumpDir)
|
2009-09-01 11:14:58 +00:00
|
|
|
{
|
|
|
|
if (!File::Exists(szDir) || !File::IsDirectory(szDir))
|
|
|
|
File::CreateDir(szDir);
|
|
|
|
|
|
|
|
bCheckedDumpDir = true;
|
|
|
|
}
|
2009-09-19 13:14:55 +00:00
|
|
|
sprintf(szTemp, "%s/%s_%08x_%i.png", szDir, uniqueId, texHash, tex_format);
|
2009-09-01 11:14:58 +00:00
|
|
|
//sprintf(szTemp, "%s\\txt_%04i_%i.png", g_Config.texDumpPath.c_str(), counter++, format); <-- Old method
|
|
|
|
if (!File::Exists(szTemp))
|
2009-09-02 06:33:41 +00:00
|
|
|
D3DXSaveTextureToFileA(szTemp,D3DXIFF_BMP,entry.texture,0);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INCSTAT(stats.numTexturesCreated);
|
|
|
|
SETSTAT(stats.numTexturesAlive, (int)textures.size());
|
|
|
|
|
|
|
|
//Set the texture!
|
2009-09-13 08:21:35 +00:00
|
|
|
D3D::SetTexture(stage, entry.texture);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-18 03:12:32 +00:00
|
|
|
DEBUGGER_PAUSE_LOG_AT(NEXT_NEW_TEXTURE,true,{printf("A new texture (%d x %d) is loaded", width, height);});
|
2009-03-07 18:05:29 +00:00
|
|
|
return &entry;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 21:56:08 +00:00
|
|
|
// EXTREMELY incomplete.
|
2009-09-04 06:09:21 +00:00
|
|
|
void TextureCache::CopyRenderTargetToTexture(u32 address, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle &source_rect)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-09-03 21:56:08 +00:00
|
|
|
int efb_w = source_rect.GetWidth();
|
|
|
|
int efb_h = source_rect.GetHeight();
|
|
|
|
|
2009-09-04 06:09:21 +00:00
|
|
|
int tex_w = (abs(source_rect.GetWidth()) >> bScaleByHalf);
|
|
|
|
int tex_h = (abs(source_rect.GetHeight()) >> bScaleByHalf);
|
2009-09-03 21:56:08 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
TexCache::iterator iter;
|
|
|
|
LPDIRECT3DTEXTURE9 tex;
|
|
|
|
iter = textures.find(address);
|
|
|
|
if (iter != textures.end())
|
|
|
|
{
|
|
|
|
if (!iter->second.isRenderTarget)
|
|
|
|
{
|
2009-09-03 21:56:08 +00:00
|
|
|
// Remove it and recreate it as a render target
|
|
|
|
iter->second.texture->Release();
|
|
|
|
iter->second.texture = 0;
|
|
|
|
textures.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex = iter->second.texture;
|
|
|
|
iter->second.frameCount = frameCount;
|
|
|
|
goto have_texture;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-03 21:56:08 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
TCacheEntry entry;
|
2009-09-02 18:55:36 +00:00
|
|
|
entry.isRenderTarget = true;
|
2008-12-08 05:25:12 +00:00
|
|
|
entry.hash = 0;
|
|
|
|
entry.frameCount = frameCount;
|
2009-09-03 21:56:08 +00:00
|
|
|
entry.w = tex_w;
|
|
|
|
entry.h = tex_h;
|
|
|
|
|
|
|
|
D3D::dev->CreateTexture(tex_w, tex_h, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &entry.texture, 0);
|
2008-12-08 05:25:12 +00:00
|
|
|
textures[address] = entry;
|
|
|
|
tex = entry.texture;
|
|
|
|
}
|
2009-09-02 18:55:36 +00:00
|
|
|
|
2009-09-03 21:56:08 +00:00
|
|
|
have_texture:
|
|
|
|
TargetRectangle targetSource = Renderer::ConvertEFBRectangle(source_rect);
|
|
|
|
RECT source_rc;
|
|
|
|
source_rc.left = targetSource.left;
|
|
|
|
source_rc.top = targetSource.top;
|
|
|
|
source_rc.right = targetSource.right;
|
|
|
|
source_rc.bottom = targetSource.bottom;
|
|
|
|
RECT dest_rc;
|
|
|
|
dest_rc.left = 0;
|
|
|
|
dest_rc.top = 0;
|
|
|
|
dest_rc.right = tex_w;
|
|
|
|
dest_rc.bottom = tex_h;
|
|
|
|
|
|
|
|
LPDIRECT3DSURFACE9 srcSurface, destSurface;
|
|
|
|
tex->GetSurfaceLevel(0, &destSurface);
|
2009-09-13 17:46:33 +00:00
|
|
|
srcSurface = FBManager::GetEFBColorRTSurface();
|
2009-09-03 21:56:08 +00:00
|
|
|
D3D::dev->StretchRect(srcSurface, &source_rc, destSurface, &dest_rc, D3DTEXF_LINEAR);
|
2008-12-08 05:25:12 +00:00
|
|
|
destSurface->Release();
|
2009-09-20 03:29:43 +00:00
|
|
|
|
|
|
|
DEBUGGER_LOG_AT((NEXT_XFB_CMD|NEXT_EFB_CMD|NEXT_FRAME|NEXT_NEW_TEXTURE|NEXT_FLUSH),
|
|
|
|
{printf("StretchRect, EFB (%d,%d) -> Texture at 0x%08X (%d,%d)\n",efb_w,efb_h,address,tex_w,tex_h);});
|
|
|
|
DEBUGGER_PAUSE_AT((NEXT_EFB_CMD|NEXT_NEW_TEXTURE),false);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|