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/
|
|
|
|
|
2008-12-26 12:24:15 +00:00
|
|
|
#include <vector>
|
2010-04-19 03:06:18 +00:00
|
|
|
#include <cmath>
|
2008-12-26 12:24:15 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
|
2009-05-07 07:43:56 +00:00
|
|
|
#include <fstream>
|
2008-12-08 05:25:12 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define _interlockedbittestandset workaround_ms_header_bug_platform_sdk6_set
|
|
|
|
#define _interlockedbittestandreset workaround_ms_header_bug_platform_sdk6_reset
|
|
|
|
#define _interlockedbittestandset64 workaround_ms_header_bug_platform_sdk6_set64
|
|
|
|
#define _interlockedbittestandreset64 workaround_ms_header_bug_platform_sdk6_reset64
|
|
|
|
#include <intrin.h>
|
|
|
|
#undef _interlockedbittestandset
|
|
|
|
#undef _interlockedbittestandreset
|
|
|
|
#undef _interlockedbittestandset64
|
|
|
|
#undef _interlockedbittestandreset64
|
|
|
|
#endif
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
#include "BPStructs.h"
|
|
|
|
#include "CommonPaths.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "FramebufferManager.h"
|
|
|
|
#include "Globals.h"
|
2009-05-07 07:43:56 +00:00
|
|
|
#include "Hash.h"
|
2010-09-28 02:15:02 +00:00
|
|
|
#include "HiresTextures.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "ImageWrite.h"
|
|
|
|
#include "MemoryUtil.h"
|
2008-12-26 11:23:59 +00:00
|
|
|
#include "PixelShaderCache.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "PixelShaderManager.h"
|
2010-09-28 02:15:02 +00:00
|
|
|
#include "Profiler.h"
|
|
|
|
#include "Render.h"
|
|
|
|
#include "Statistics.h"
|
|
|
|
#include "StringUtil.h"
|
|
|
|
#include "TextureCache.h"
|
2010-07-12 19:30:25 +00:00
|
|
|
#include "TextureConverter.h"
|
2010-09-28 02:15:02 +00:00
|
|
|
#include "TextureDecoder.h"
|
|
|
|
#include "VertexShaderManager.h"
|
|
|
|
#include "VideoConfig.h"
|
2009-05-07 07:43:56 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
u8 *TextureCache::temp = NULL;
|
|
|
|
TextureCache::TexCache TextureCache::textures;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
extern int frameCount;
|
|
|
|
static u32 s_TempFramebuffer = 0;
|
|
|
|
|
2010-10-04 01:54:51 +00:00
|
|
|
#define TEMP_SIZE (2048*2048*4)
|
2008-12-08 05:25:12 +00:00
|
|
|
#define TEXTURE_KILL_THRESHOLD 200
|
|
|
|
|
2009-03-22 11:21:44 +00:00
|
|
|
static const GLint c_MinLinearFilter[8] = {
|
2009-05-09 07:55:30 +00:00
|
|
|
GL_NEAREST,
|
|
|
|
GL_NEAREST_MIPMAP_NEAREST,
|
|
|
|
GL_NEAREST_MIPMAP_LINEAR,
|
|
|
|
GL_NEAREST,
|
|
|
|
GL_LINEAR,
|
|
|
|
GL_LINEAR_MIPMAP_NEAREST,
|
|
|
|
GL_LINEAR_MIPMAP_LINEAR,
|
|
|
|
GL_LINEAR,
|
2008-12-08 05:25:12 +00:00
|
|
|
};
|
|
|
|
|
2009-03-22 11:21:44 +00:00
|
|
|
static const GLint c_WrapSettings[4] = {
|
2009-05-09 07:55:30 +00:00
|
|
|
GL_CLAMP_TO_EDGE,
|
|
|
|
GL_REPEAT,
|
|
|
|
GL_MIRRORED_REPEAT,
|
|
|
|
GL_REPEAT,
|
2008-12-26 12:24:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool SaveTexture(const char* filename, u32 textarget, u32 tex, int width, int height)
|
|
|
|
{
|
2009-05-09 07:55:30 +00:00
|
|
|
std::vector<u32> data(width * height);
|
2008-12-26 12:24:15 +00:00
|
|
|
glBindTexture(textarget, tex);
|
2009-03-07 09:44:19 +00:00
|
|
|
glGetTexImage(textarget, 0, GL_BGRA, GL_UNSIGNED_BYTE, &data[0]);
|
2009-03-22 11:21:44 +00:00
|
|
|
GLenum err = GL_REPORT_ERROR();
|
2008-12-26 12:24:15 +00:00
|
|
|
if (err != GL_NO_ERROR)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
PanicAlert("Can't save texture, GL Error: %s", gluErrorString(err));
|
2008-12-26 12:24:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2008-12-26 12:24:15 +00:00
|
|
|
return SaveTGA(filename, width, height, &data[0]);
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::TCacheEntry::SetTextureParameters(TexMode0 &newmode,TexMode1 &newmode1)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
mode = newmode;
|
2010-04-14 13:57:16 +00:00
|
|
|
mode1 = newmode1;
|
2009-07-19 08:17:41 +00:00
|
|
|
|
2010-08-29 04:56:53 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
|
(newmode.mag_filter || g_Config.bForceFiltering) ? GL_LINEAR : GL_NEAREST);
|
2009-07-19 08:17:41 +00:00
|
|
|
|
2010-08-29 04:56:53 +00:00
|
|
|
if (bHaveMipMaps)
|
2009-07-19 08:17:41 +00:00
|
|
|
{
|
2010-08-29 04:56:53 +00:00
|
|
|
if (g_ActiveConfig.bForceFiltering && newmode.min_filter < 4)
|
|
|
|
mode.min_filter += 4; // take equivalent forced linear
|
|
|
|
int filt = newmode.min_filter;
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, c_MinLinearFilter[filt & 7]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, newmode1.min_lod >> 4);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, newmode1.max_lod >> 4);
|
|
|
|
glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, (newmode.lod_bias/32.0f));
|
2009-07-19 08:17:41 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2010-08-29 04:56:53 +00:00
|
|
|
else
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
|
|
(g_ActiveConfig.bForceFiltering || newmode.min_filter >= 4) ? GL_LINEAR : GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, c_WrapSettings[newmode.wrap_s]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, c_WrapSettings[newmode.wrap_t]);
|
2009-05-09 07:55:30 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
if (g_Config.iMaxAnisotropy >= 1)
|
2009-09-13 08:21:35 +00:00
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)(1 << g_ActiveConfig.iMaxAnisotropy));
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::TCacheEntry::Destroy(bool shutdown)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
if (texture)
|
|
|
|
glDeleteTextures(1, &texture);
|
|
|
|
texture = 0;
|
|
|
|
if (!isRenderTarget && !shutdown && !g_ActiveConfig.bSafeTextureCache)
|
|
|
|
{
|
|
|
|
u32 *ptr = (u32*)g_VideoInitialize.pGetMemoryPointer(addr);
|
|
|
|
if (ptr && *ptr == hash)
|
|
|
|
*ptr = oldpixel;
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::Init()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
temp = (u8*)AllocateMemoryPages(TEMP_SIZE);
|
2009-09-13 08:21:35 +00:00
|
|
|
TexDecoder_SetTexFmtOverlayOptions(g_ActiveConfig.bTexFmtOverlayEnable, g_ActiveConfig.bTexFmtOverlayCenter);
|
2009-09-13 08:54:46 +00:00
|
|
|
HiresTextures::Init(globals->unique_id);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::Invalidate(bool shutdown)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter)
|
|
|
|
iter->second.Destroy(shutdown);
|
|
|
|
textures.clear();
|
2009-05-09 07:55:30 +00:00
|
|
|
HiresTextures::Shutdown();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::Shutdown()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
Invalidate(true);
|
2009-02-22 20:21:56 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (s_TempFramebuffer)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffersEXT(1, (GLuint *)&s_TempFramebuffer);
|
|
|
|
s_TempFramebuffer = 0;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
FreeMemoryPages(temp, TEMP_SIZE);
|
|
|
|
temp = NULL;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::Cleanup()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-07-12 20:11:19 +00:00
|
|
|
TexCache::iterator iter = textures.begin();
|
|
|
|
while (iter != textures.end())
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-07-12 20:11:19 +00:00
|
|
|
if (frameCount > TEXTURE_KILL_THRESHOLD + iter->second.frameCount)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-07-12 20:11:19 +00:00
|
|
|
iter->second.Destroy(false);
|
|
|
|
textures.erase(iter++);
|
|
|
|
}
|
|
|
|
else
|
2010-09-28 02:15:02 +00:00
|
|
|
{
|
2010-07-12 20:11:19 +00:00
|
|
|
++iter;
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
2010-07-12 20:11:19 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::InvalidateRange(u32 start_address, u32 size)
|
2009-09-19 13:14:55 +00:00
|
|
|
{
|
2009-05-09 07:55:30 +00:00
|
|
|
TexCache::iterator iter = textures.begin();
|
|
|
|
while (iter != textures.end())
|
|
|
|
{
|
2010-07-12 19:30:25 +00:00
|
|
|
int rangePosition = iter->second.IntersectsMemoryRange(start_address, size);
|
|
|
|
if (rangePosition == 0)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
iter->second.Destroy(false);
|
2010-05-28 23:14:16 +00:00
|
|
|
textures.erase(iter++);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-09-28 02:15:02 +00:00
|
|
|
else
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
++iter;
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::MakeRangeDynamic(u32 start_address, u32 size)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
|
|
|
TexCache::iterator iter = textures.begin();
|
|
|
|
while (iter != textures.end())
|
|
|
|
{
|
|
|
|
int rangePosition = iter->second.IntersectsMemoryRange(start_address, size);
|
|
|
|
if ( rangePosition == 0)
|
|
|
|
{
|
|
|
|
iter->second.hash = 0;
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
++iter;
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2009-03-08 19:19:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
int TextureCache::TCacheEntry::IntersectsMemoryRange(u32 range_address, u32 range_size)
|
|
|
|
{
|
|
|
|
if (addr + size_in_bytes < range_address)
|
|
|
|
return -1;
|
|
|
|
if (addr >= range_address + range_size)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::TCacheEntry* TextureCache::Load(int texstage, u32 address, int width, int height, u32 tex_format, int tlutaddr, int tlutfmt)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-05-09 07:55:30 +00:00
|
|
|
// notes (about "UNsafe texture cache"):
|
|
|
|
// Have to be removed soon.
|
|
|
|
// But we keep it until the "safe" way became rock solid
|
|
|
|
// pros: it has an unique ID held by the texture data itself (@address) once cached.
|
|
|
|
// cons: it writes this unique ID in the gc RAM <- very dangerous (break MP1) and ugly
|
|
|
|
|
|
|
|
// notes (about "safe texture cache"):
|
|
|
|
// Metroids text issue (character table):
|
|
|
|
// Same addr, same GX_TF_C4 texture data but different TLUT (hence different outputs).
|
|
|
|
// That's why we have to hash the TLUT too for TLUT tex_format dependent textures (ie. GX_TF_C4, GX_TF_C8, GX_TF_C14X2).
|
|
|
|
// And since the address and tex data don't change, the key index in the cacheEntry map can't be the address but
|
|
|
|
// have to be a real unique ID.
|
|
|
|
// DONE but not satifiying yet -> may break copyEFBToTexture sometimes.
|
|
|
|
|
|
|
|
// Pokemon Colosseum text issue (plain text):
|
|
|
|
// Use a GX_TF_I4 512x512 text-flush-texture at a const address.
|
|
|
|
// The problem here was just the sparse hash on the texture. This texture is partly overwrited (what is needed only)
|
|
|
|
// so lot's of remaning old text. Thin white chars on black bg too.
|
|
|
|
|
|
|
|
// TODO: - clean this up when ready to kill old "unsafe texture cache"
|
|
|
|
// - fix the key index situation with CopyRenderTargetToTexture.
|
|
|
|
// Could happen only for GX_TF_C4, GX_TF_C8 and GX_TF_C14X2 fmt.
|
|
|
|
// Wonder if we can't use tex width&height to know if EFB might be copied to it...
|
|
|
|
// raw idea: TOCHECK if addresses are aligned we have few bits left...
|
2009-01-16 16:28:33 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (address == 0)
|
|
|
|
return NULL;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-04-22 02:51:07 +00:00
|
|
|
TexMode0 &tm0 = bpmem.tex[texstage >> 2].texMode0[texstage & 3];
|
|
|
|
TexMode1 &tm1 = bpmem.tex[texstage >> 2].texMode1[texstage & 3];
|
2010-06-06 14:44:35 +00:00
|
|
|
int maxlevel = (tm1.max_lod >> 4);
|
|
|
|
bool UseNativeMips = (tm0.min_filter & 3) && (tm0.min_filter != 8) && g_ActiveConfig.bUseNativeMips;
|
2010-04-14 13:57:16 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
u8 *ptr = g_VideoInitialize.pGetMemoryPointer(address);
|
|
|
|
int bsw = TexDecoder_GetBlockWidthInTexels(tex_format) - 1; // TexelSizeInNibbles(format)*width*height/16;
|
|
|
|
int bsh = TexDecoder_GetBlockHeightInTexels(tex_format) - 1; // TexelSizeInNibbles(format)*width*height/16;
|
2010-04-14 13:57:16 +00:00
|
|
|
int bsdepth = TexDecoder_GetTexelSizeInNibbles(tex_format);
|
2010-09-28 02:15:02 +00:00
|
|
|
int expandedWidth = (width + bsw) & (~bsw);
|
2009-05-09 07:55:30 +00:00
|
|
|
int expandedHeight = (height + bsh) & (~bsh);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-04-19 03:06:18 +00:00
|
|
|
u64 hash_value = 0;
|
2010-09-28 02:15:02 +00:00
|
|
|
u32 texID = address;
|
2010-04-19 03:06:18 +00:00
|
|
|
u64 texHash = 0;
|
2010-02-03 03:52:50 +00:00
|
|
|
u32 FullFormat = tex_format;
|
2010-07-18 00:18:31 +00:00
|
|
|
bool TextureisDynamic = false;
|
2010-02-03 03:52:50 +00:00
|
|
|
if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
|
2010-02-20 04:18:19 +00:00
|
|
|
FullFormat = (tex_format | (tlutfmt << 16));
|
2010-09-28 02:15:02 +00:00
|
|
|
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache || g_ActiveConfig.bHiresTextures || g_ActiveConfig.bDumpTextures)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
texHash = GetHash64(ptr,TexDecoder_GetTextureSizeInBytes(expandedWidth, expandedHeight, tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2009-05-09 07:55:30 +00:00
|
|
|
if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
|
|
|
|
{
|
|
|
|
// 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.
|
2010-08-28 15:09:42 +00:00
|
|
|
u64 tlutHash = GetHash64(&texMem[tlutaddr], TexDecoder_GetPaletteSize(tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
First a bugfix:
fixed a misbehavior in the clear code that causes depth clear problems in reference hardware (Intel as example).
add 6 parameters to optimize Safe Texture Cache:
SafeTextureCacheColorSamples, SafeTextureCacheIndexedSamples, SafeTextureCacheTlutSamples:
this 3 parameters gives the number of samples taken to calculate the final hash value, less samples = more speed, more samples = more accuracy
if 0 is specified the hash is calculated using all the data in the texture.
SafeTextureCacheColorMaxSize, SafeTextureCacheIndexedMaxSize, SafeTextureCacheTlutMaxSize:
this parameters limits the amount of data used for the hash calculation, it could appear as redundant but in some games is better to make a full hash of the first bytes instead of some samples of all the texture.
color, indexed, tlut : define the texture type, full color data, indexed, and the tlut memory.
the parameters are available in the config , no GUI at this time, if the test are OK will add it to the GUI.
if someone needs it will give more examples on how to configure the values for specific games.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5116 8ced0084-cf51-0410-be5f-012b33b47a6e
2010-02-23 21:52:12 +00:00
|
|
|
texHash ^= tlutHash;
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache)
|
First a bugfix:
fixed a misbehavior in the clear code that causes depth clear problems in reference hardware (Intel as example).
add 6 parameters to optimize Safe Texture Cache:
SafeTextureCacheColorSamples, SafeTextureCacheIndexedSamples, SafeTextureCacheTlutSamples:
this 3 parameters gives the number of samples taken to calculate the final hash value, less samples = more speed, more samples = more accuracy
if 0 is specified the hash is calculated using all the data in the texture.
SafeTextureCacheColorMaxSize, SafeTextureCacheIndexedMaxSize, SafeTextureCacheTlutMaxSize:
this parameters limits the amount of data used for the hash calculation, it could appear as redundant but in some games is better to make a full hash of the first bytes instead of some samples of all the texture.
color, indexed, tlut : define the texture type, full color data, indexed, and the tlut memory.
the parameters are available in the config , no GUI at this time, if the test are OK will add it to the GUI.
if someone needs it will give more examples on how to configure the values for specific games.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5116 8ced0084-cf51-0410-be5f-012b33b47a6e
2010-02-23 21:52:12 +00:00
|
|
|
{
|
2010-02-26 22:14:29 +00:00
|
|
|
texID = texID ^ ((u32)(tlutHash & 0xFFFFFFFF)) ^ ((u32)((tlutHash >> 32) & 0xFFFFFFFF));
|
First a bugfix:
fixed a misbehavior in the clear code that causes depth clear problems in reference hardware (Intel as example).
add 6 parameters to optimize Safe Texture Cache:
SafeTextureCacheColorSamples, SafeTextureCacheIndexedSamples, SafeTextureCacheTlutSamples:
this 3 parameters gives the number of samples taken to calculate the final hash value, less samples = more speed, more samples = more accuracy
if 0 is specified the hash is calculated using all the data in the texture.
SafeTextureCacheColorMaxSize, SafeTextureCacheIndexedMaxSize, SafeTextureCacheTlutMaxSize:
this parameters limits the amount of data used for the hash calculation, it could appear as redundant but in some games is better to make a full hash of the first bytes instead of some samples of all the texture.
color, indexed, tlut : define the texture type, full color data, indexed, and the tlut memory.
the parameters are available in the config , no GUI at this time, if the test are OK will add it to the GUI.
if someone needs it will give more examples on how to configure the values for specific games.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5116 8ced0084-cf51-0410-be5f-012b33b47a6e
2010-02-23 21:52:12 +00:00
|
|
|
}
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-02-03 03:52:50 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache)
|
|
|
|
hash_value = texHash;
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool skip_texture_create = false;
|
|
|
|
TexCache::iterator iter = textures.find(texID);
|
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
if (iter != textures.end())
|
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
TCacheEntry &entry = iter->second;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-13 08:21:35 +00:00
|
|
|
if (!g_ActiveConfig.bSafeTextureCache)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-07-18 00:18:31 +00:00
|
|
|
if(entry.isRenderTarget || entry.isDynamic)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-07-18 00:18:31 +00:00
|
|
|
if(!g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-08-28 15:09:42 +00:00
|
|
|
hash_value = GetHash64(ptr,TexDecoder_GetTextureSizeInBytes(expandedWidth, expandedHeight, tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2010-07-12 19:30:25 +00:00
|
|
|
if ((tex_format == GX_TF_C4) || (tex_format == GX_TF_C8) || (tex_format == GX_TF_C14X2))
|
|
|
|
{
|
2010-08-28 15:09:42 +00:00
|
|
|
hash_value ^= GetHash64(&texMem[tlutaddr], TexDecoder_GetPaletteSize(tex_format),g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hash_value = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hash_value = ((u32 *)ptr)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-18 00:18:31 +00:00
|
|
|
if(entry.isRenderTarget || entry.isDynamic)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-07-18 00:18:31 +00:00
|
|
|
if(g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
|
|
|
hash_value = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-28 02:15:02 +00:00
|
|
|
if (((entry.isRenderTarget || entry.isDynamic) && hash_value == entry.hash && address == entry.addr)
|
2010-07-12 19:30:25 +00:00
|
|
|
|| ((address == entry.addr) && (hash_value == entry.hash) && ((int) FullFormat == entry.fmt) && entry.MipLevels >= maxlevel))
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
entry.frameCount = frameCount;
|
2010-08-29 04:56:53 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, entry.texture);
|
2010-02-06 16:05:48 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-07-12 19:30:25 +00:00
|
|
|
entry.SetTextureParameters(tm0,tm1);
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.isDynamic = false;
|
2009-05-09 07:55:30 +00:00
|
|
|
return &entry;
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Let's reload the new texture data into the same texture,
|
2009-05-09 07:55:30 +00:00
|
|
|
// instead of destroying it and having to create a new one.
|
|
|
|
// Might speed up movie playback very, very slightly.
|
2010-07-18 00:18:31 +00:00
|
|
|
TextureisDynamic = (entry.isRenderTarget || entry.isDynamic) && !g_ActiveConfig.bCopyEFBToTexture;
|
2010-09-28 02:15:02 +00:00
|
|
|
if (((!(entry.isRenderTarget || entry.isDynamic) && width == entry.w && height == entry.h && (int)FullFormat == entry.fmt) ||
|
|
|
|
((entry.isRenderTarget || entry.isDynamic) && entry.w == width && entry.h == height && entry.Scaledw == width && entry.Scaledh == height)))
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-08-29 04:56:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, entry.texture);
|
2010-02-06 16:05:48 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-07-12 19:30:25 +00:00
|
|
|
entry.SetTextureParameters(tm0,tm1);
|
2009-05-09 07:55:30 +00:00
|
|
|
skip_texture_create = true;
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry.Destroy(false);
|
|
|
|
textures.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Make an entry in the table
|
2009-05-09 07:55:30 +00:00
|
|
|
TCacheEntry& entry = textures[texID];
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.isDynamic = TextureisDynamic;
|
2010-07-21 02:34:59 +00:00
|
|
|
entry.isRenderTarget = false;
|
2010-10-04 01:54:51 +00:00
|
|
|
entry.Realw = width;
|
|
|
|
entry.Realh = height;
|
2010-09-28 02:15:02 +00:00
|
|
|
PC_TexFormat pcfmt = PC_TEX_FMT_NONE;
|
2009-05-09 07:55:30 +00:00
|
|
|
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bHiresTextures)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
// Load Custom textures
|
2009-05-09 07:55:30 +00:00
|
|
|
char texPathTemp[MAX_PATH];
|
2009-05-15 12:28:32 +00:00
|
|
|
|
2010-10-04 11:09:32 +00:00
|
|
|
int newWidth = width;
|
|
|
|
int newHeight = height;
|
|
|
|
|
2010-02-20 04:18:19 +00:00
|
|
|
sprintf(texPathTemp, "%s_%08x_%i", globals->unique_id, (unsigned int) texHash, tex_format);
|
2010-10-04 11:09:32 +00:00
|
|
|
pcfmt = HiresTextures::GetHiresTex(texPathTemp, &newWidth, &newHeight, tex_format, temp);
|
2009-05-15 07:07:24 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (pcfmt != PC_TEX_FMT_NONE)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-10-04 11:09:32 +00:00
|
|
|
expandedWidth = width = newWidth;
|
|
|
|
expandedHeight = height = newHeight;
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (pcfmt == PC_TEX_FMT_NONE)
|
|
|
|
pcfmt = TexDecoder_Decode(temp, ptr, expandedWidth, expandedHeight, tex_format, tlutaddr, tlutfmt);
|
2009-05-09 07:55:30 +00:00
|
|
|
|
2009-09-19 13:14:55 +00:00
|
|
|
entry.oldpixel = ((u32 *)ptr)[0];
|
2009-02-21 02:42:35 +00:00
|
|
|
|
2010-07-18 00:18:31 +00:00
|
|
|
if (g_ActiveConfig.bSafeTextureCache || entry.isDynamic)
|
2009-05-09 07:55:30 +00:00
|
|
|
entry.hash = hash_value;
|
2010-07-12 19:30:25 +00:00
|
|
|
else
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
entry.hash = (u32)(((double)rand() / RAND_MAX) * 0xFFFFFFFF);
|
2010-07-12 19:30:25 +00:00
|
|
|
((u32 *)ptr)[0] = entry.hash;
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2009-07-19 08:17:41 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
entry.addr = address;
|
|
|
|
entry.size_in_bytes = TexDecoder_GetTextureSizeInBytes(expandedWidth, expandedHeight, tex_format);
|
2008-12-14 14:28:41 +00:00
|
|
|
|
2010-08-29 04:56:53 +00:00
|
|
|
GLenum target = GL_TEXTURE_2D;
|
2009-05-09 07:55:30 +00:00
|
|
|
if (!skip_texture_create) {
|
|
|
|
glGenTextures(1, (GLuint *)&entry.texture);
|
2009-07-19 08:17:41 +00:00
|
|
|
glBindTexture(target, entry.texture);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-14 13:57:16 +00:00
|
|
|
bool isPow2 = !((width & (width - 1)) || (height & (height - 1)));
|
|
|
|
int TexLevels = (width > height)?width:height;
|
2010-09-28 02:15:02 +00:00
|
|
|
TexLevels = (isPow2 && UseNativeMips && (maxlevel > 0)) ? (int)(log((double)TexLevels)/log((double)2)) + 1 : (isPow2? 0 : 1);
|
|
|
|
if(TexLevels > (maxlevel + 1) && maxlevel > 0)
|
2010-06-06 14:44:35 +00:00
|
|
|
TexLevels = (maxlevel + 1);
|
2010-06-05 00:01:18 +00:00
|
|
|
entry.MipLevels = maxlevel;
|
2010-05-04 14:43:30 +00:00
|
|
|
bool GenerateMipmaps = TexLevels > 1 || TexLevels == 0;
|
|
|
|
entry.bHaveMipMaps = GenerateMipmaps;
|
2010-04-19 03:06:18 +00:00
|
|
|
int gl_format = 0;
|
|
|
|
int gl_iformat = 0;
|
2010-05-04 14:43:30 +00:00
|
|
|
int gl_type = 0;
|
2010-05-19 21:54:54 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-09-28 02:15:02 +00:00
|
|
|
if (pcfmt != PC_TEX_FMT_DXT1)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
switch (pcfmt)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
default:
|
|
|
|
case PC_TEX_FMT_NONE:
|
2010-09-28 02:15:02 +00:00
|
|
|
PanicAlert("Invalid PC texture format %i", pcfmt);
|
2009-05-09 07:55:30 +00:00
|
|
|
case PC_TEX_FMT_BGRA32:
|
|
|
|
gl_format = GL_BGRA;
|
|
|
|
gl_iformat = 4;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case PC_TEX_FMT_RGBA32:
|
|
|
|
gl_format = GL_RGBA;
|
|
|
|
gl_iformat = 4;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2009-05-13 02:06:02 +00:00
|
|
|
case PC_TEX_FMT_I4_AS_I8:
|
|
|
|
gl_format = GL_LUMINANCE;
|
|
|
|
gl_iformat = GL_INTENSITY4;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case PC_TEX_FMT_IA4_AS_IA8:
|
|
|
|
gl_format = GL_LUMINANCE_ALPHA;
|
|
|
|
gl_iformat = GL_LUMINANCE4_ALPHA4;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2009-05-09 07:55:30 +00:00
|
|
|
case PC_TEX_FMT_I8:
|
|
|
|
gl_format = GL_LUMINANCE;
|
2009-06-20 09:29:28 +00:00
|
|
|
gl_iformat = GL_INTENSITY8;
|
2009-05-09 07:55:30 +00:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case PC_TEX_FMT_IA8:
|
|
|
|
gl_format = GL_LUMINANCE_ALPHA;
|
|
|
|
gl_iformat = GL_LUMINANCE8_ALPHA8;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case PC_TEX_FMT_RGB565:
|
|
|
|
gl_format = GL_RGB;
|
|
|
|
gl_iformat = GL_RGB;
|
|
|
|
gl_type = GL_UNSIGNED_SHORT_5_6_5;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-22 02:51:07 +00:00
|
|
|
if (expandedWidth != width)
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, expandedWidth);
|
2010-05-04 14:43:30 +00:00
|
|
|
//generate mipmaps even if we use native mips to suport textures with less levels
|
2009-12-03 20:09:15 +00:00
|
|
|
if(skip_texture_create)
|
|
|
|
{
|
|
|
|
glTexSubImage2D(target, 0,0,0,width, height, gl_format, gl_type, temp);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
else
|
2009-12-03 20:09:15 +00:00
|
|
|
{
|
2010-04-22 02:51:07 +00:00
|
|
|
if (GenerateMipmaps)
|
|
|
|
{
|
2010-05-04 14:43:30 +00:00
|
|
|
if(UseNativeMips)
|
|
|
|
{
|
2010-05-25 19:25:31 +00:00
|
|
|
glTexImage2D(target, 0, gl_iformat, width, height, 0, gl_format, gl_type, temp);
|
2010-05-04 14:43:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
|
|
|
glTexImage2D(target, 0, gl_iformat, width, height, 0, gl_format, gl_type, temp);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
|
|
|
|
}
|
2010-04-22 02:51:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
glTexImage2D(target, 0, gl_iformat, width, height, 0, gl_format, gl_type, temp);
|
2010-04-22 02:51:07 +00:00
|
|
|
}
|
2009-12-03 20:09:15 +00:00
|
|
|
}
|
2009-05-09 07:55:30 +00:00
|
|
|
|
|
|
|
if (expandedWidth != width) // reset
|
2010-09-28 02:15:02 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-30 15:24:34 +00:00
|
|
|
if (skip_texture_create)
|
2009-12-03 20:09:15 +00:00
|
|
|
{
|
2010-02-03 03:52:50 +00:00
|
|
|
glCompressedTexSubImage2D(target, 0,0,0,width, height,
|
2009-12-03 20:09:15 +00:00
|
|
|
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,expandedWidth*expandedHeight/2, temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glCompressedTexImage2D(target, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
|
2010-02-03 03:52:50 +00:00
|
|
|
width, height, 0, expandedWidth*expandedHeight/2, temp);
|
2009-12-03 20:09:15 +00:00
|
|
|
}
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-05-19 21:54:54 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-09-28 02:15:02 +00:00
|
|
|
if(TexLevels > 1 && pcfmt != PC_TEX_FMT_NONE)
|
2010-04-14 13:57:16 +00:00
|
|
|
{
|
|
|
|
int level = 1;
|
2010-04-22 02:51:07 +00:00
|
|
|
int mipWidth = width >> 1;
|
|
|
|
int mipHeight = height >> 1;
|
2010-04-14 13:57:16 +00:00
|
|
|
ptr += entry.size_in_bytes;
|
2010-04-22 02:51:07 +00:00
|
|
|
while((mipHeight || mipWidth) && (level < TexLevels))
|
2010-04-14 13:57:16 +00:00
|
|
|
{
|
|
|
|
u32 currentWidth = (mipWidth > 0)? mipWidth : 1;
|
|
|
|
u32 currentHeight = (mipHeight > 0)? mipHeight : 1;
|
|
|
|
expandedWidth = (currentWidth + bsw) & (~bsw);
|
|
|
|
expandedHeight = (currentHeight + bsh) & (~bsh);
|
2010-09-28 02:15:02 +00:00
|
|
|
TexDecoder_Decode(temp, ptr, expandedWidth, expandedHeight, tex_format, tlutaddr, tlutfmt);
|
|
|
|
if (pcfmt != PC_TEX_FMT_DXT1)
|
2010-04-14 13:57:16 +00:00
|
|
|
{
|
2010-04-19 03:06:18 +00:00
|
|
|
if (expandedWidth != (int)currentWidth)
|
2010-04-14 13:57:16 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, expandedWidth);
|
2010-06-05 00:01:18 +00:00
|
|
|
glTexImage2D(target, level, gl_iformat, currentWidth, currentHeight, 0, gl_format, gl_type, temp);
|
2010-04-22 02:51:07 +00:00
|
|
|
if (expandedWidth != (int)currentWidth)
|
2010-04-14 13:57:16 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-05 00:01:18 +00:00
|
|
|
glCompressedTexImage2D(target, level, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, currentWidth, currentHeight, 0, expandedWidth*expandedHeight/2, temp);
|
2010-04-14 13:57:16 +00:00
|
|
|
}
|
2010-04-22 02:51:07 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-04-14 13:57:16 +00:00
|
|
|
u32 size = (max(mipWidth, bsw) * max(mipHeight, bsh) * bsdepth) >> 1;
|
|
|
|
ptr += size;
|
|
|
|
mipWidth >>= 1;
|
|
|
|
mipHeight >>= 1;
|
|
|
|
level++;
|
|
|
|
}
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
|
|
|
entry.frameCount = frameCount;
|
|
|
|
entry.w = width;
|
|
|
|
entry.h = height;
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.Scaledw = width;
|
|
|
|
entry.Scaledh = height;
|
2010-09-28 02:15:02 +00:00
|
|
|
entry.fmt = FullFormat;
|
2010-04-14 13:57:16 +00:00
|
|
|
entry.SetTextureParameters(tm0,tm1);
|
2010-09-28 02:15:02 +00:00
|
|
|
if (g_ActiveConfig.bDumpTextures)
|
|
|
|
{
|
|
|
|
// dump texture to file
|
|
|
|
char szTemp[MAX_PATH];
|
2009-05-15 12:28:32 +00:00
|
|
|
char szDir[MAX_PATH];
|
2009-09-13 08:54:46 +00:00
|
|
|
const char* uniqueId = globals->unique_id;
|
2010-08-08 00:13:05 +00:00
|
|
|
static bool bCheckedDumpDir = false;
|
2009-05-15 12:28:32 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
sprintf(szDir, "%s%s", File::GetUserPath(D_DUMPTEXTURES_IDX), uniqueId);
|
2009-05-15 12:28:32 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (!bCheckedDumpDir)
|
2009-05-15 12:28:32 +00:00
|
|
|
{
|
2009-05-16 01:21:57 +00:00
|
|
|
if (!File::Exists(szDir) || !File::IsDirectory(szDir))
|
|
|
|
File::CreateDir(szDir);
|
2009-05-15 12:28:32 +00:00
|
|
|
|
2009-05-16 01:21:57 +00:00
|
|
|
bCheckedDumpDir = true;
|
2009-05-15 12:28:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 00:13:05 +00:00
|
|
|
sprintf(szTemp, "%s/%s_%08x_%i.tga", szDir, uniqueId, (unsigned int) texHash, tex_format);
|
2009-05-09 07:55:30 +00:00
|
|
|
if (!File::Exists(szTemp))
|
2010-08-08 00:13:05 +00:00
|
|
|
SaveTexture(szTemp, target, entry.texture, entry.w, entry.h);
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
INCSTAT(stats.numTexturesCreated);
|
|
|
|
SETSTAT(stats.numTexturesAlive, textures.size());
|
|
|
|
return &entry;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +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-04 06:09:21 +00:00
|
|
|
DVSTARTPROFILE();
|
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
|
|
|
// for intensity values, use Y of YUV format!
|
|
|
|
// for all purposes, treat 4bit equivalents as 8bit (probably just used for compression)
|
|
|
|
// RGBA8 - RGBA8
|
|
|
|
// RGB565 - RGB565
|
|
|
|
// RGB5A3 - RGB5A3
|
|
|
|
// I4,R4,Z4 - I4
|
|
|
|
// IA4,RA4 - IA4
|
|
|
|
// Z8M,G8,I8,A8,Z8,R8,B8,Z8L - I8
|
|
|
|
// Z16,GB8,RG8,Z16L,IA8,RA8 - IA8
|
|
|
|
float colmat[16];
|
|
|
|
float fConstAdd[4] = {0};
|
|
|
|
memset(colmat, 0, sizeof(colmat));
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
if (bFromZBuffer)
|
|
|
|
{
|
|
|
|
switch(copyfmt)
|
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
case 0: // Z4
|
2008-12-08 05:25:12 +00:00
|
|
|
case 1: // Z8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[2] = colmat[6] = colmat[10] = colmat[14] = 1;
|
2009-11-14 23:15:09 +00:00
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
case 3: // Z16 //?
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[1] = colmat[5] = colmat[9] = colmat[14] = 1;
|
2009-11-14 23:15:09 +00:00
|
|
|
break;
|
2009-05-15 02:39:55 +00:00
|
|
|
case 11: // Z16 (reverse order)
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[2] = colmat[6] = colmat[10] = colmat[13] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 6: // Z24X8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[2] = colmat[5] = colmat[8] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 9: // Z8M
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[1] = colmat[5] = colmat[9] = colmat[13] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 10: // Z8L
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 12: // Z16L
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[13] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-03-21 20:07:56 +00:00
|
|
|
ERROR_LOG(VIDEO, "Unknown copy zbuf format: 0x%x", copyfmt);
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-05-09 07:55:30 +00:00
|
|
|
else if (bIsIntensityFmt)
|
|
|
|
{
|
2009-11-14 23:15:09 +00:00
|
|
|
// TODO - verify these coefficients
|
2008-12-08 05:25:12 +00:00
|
|
|
fConstAdd[0] = fConstAdd[1] = fConstAdd[2] = 16.0f/255.0f;
|
2009-11-14 23:15:09 +00:00
|
|
|
colmat[0] = 0.257f; colmat[1] = 0.504f; colmat[2] = 0.098f;
|
|
|
|
colmat[4] = 0.257f; colmat[5] = 0.504f; colmat[6] = 0.098f;
|
|
|
|
colmat[8] = 0.257f; colmat[9] = 0.504f; colmat[10] = 0.098f;
|
|
|
|
if (copyfmt < 2)
|
|
|
|
{
|
|
|
|
fConstAdd[3] = 16.0f / 255.0f;
|
|
|
|
colmat[12] = 0.257f; colmat[13] = 0.504f; colmat[14] = 0.098f;
|
|
|
|
}
|
|
|
|
else// alpha
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-05-09 07:55:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (copyfmt)
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
case 0: // R4
|
2009-11-14 23:15:09 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
|
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
case 8: // R8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[12] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 2: // RA4
|
2009-11-14 23:15:09 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[15] = 1;
|
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
case 3: // RA8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 7: // A8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[3] = colmat[7] = colmat[11] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 9: // G8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[1] = colmat[5] = colmat[9] = colmat[13] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 10: // B8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[2] = colmat[6] = colmat[10] = colmat[14] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 11: // RG8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[4] = colmat[8] = colmat[13] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 12: // GB8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[1] = colmat[5] = colmat[9] = colmat[14] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
case 4: // RGB565
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[5] = colmat[10] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
fConstAdd[3] = 1; // set alpha to 1
|
|
|
|
break;
|
|
|
|
case 5: // RGB5A3
|
2009-11-14 23:15:09 +00:00
|
|
|
colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
|
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
case 6: // RGBA8
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-03-21 20:07:56 +00:00
|
|
|
ERROR_LOG(VIDEO, "Unknown copy color format: 0x%x", copyfmt);
|
2009-12-03 20:09:15 +00:00
|
|
|
colmat[0] = colmat[5] = colmat[10] = colmat[15] = 1;
|
2008-12-08 05:25:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 23:15:09 +00:00
|
|
|
bool bIsInit = textures.find(address) != textures.end();
|
|
|
|
|
|
|
|
PRIM_LOG("copytarg: addr=0x%x, fromz=%d, intfmt=%d, copyfmt=%d", address, (int)bFromZBuffer, (int)bIsIntensityFmt,copyfmt);
|
|
|
|
|
|
|
|
TCacheEntry& entry = textures[address];
|
|
|
|
entry.hash = 0;
|
|
|
|
entry.frameCount = frameCount;
|
|
|
|
|
|
|
|
int w = (abs(source_rect.GetWidth()) >> bScaleByHalf);
|
|
|
|
int h = (abs(source_rect.GetHeight()) >> bScaleByHalf);
|
|
|
|
|
2010-07-18 00:18:31 +00:00
|
|
|
float xScale = Renderer::GetTargetScaleX();
|
|
|
|
float yScale = Renderer::GetTargetScaleY();
|
|
|
|
|
|
|
|
int Scaledtex_w = (g_ActiveConfig.bCopyEFBScaled)?((int)(xScale * w)) : w;
|
|
|
|
int Scaledtex_h = (g_ActiveConfig.bCopyEFBScaled)?((int)(yScale * h)) : h;
|
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
GLenum gl_format = GL_RGBA;
|
|
|
|
GLenum gl_iformat = 4;
|
|
|
|
GLenum gl_type = GL_UNSIGNED_BYTE;
|
2009-12-07 18:48:31 +00:00
|
|
|
|
|
|
|
|
2009-11-14 23:15:09 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
|
|
|
if (!bIsInit)
|
|
|
|
{
|
|
|
|
glGenTextures(1, (GLuint *)&entry.texture);
|
2010-08-29 04:56:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, entry.texture);
|
2010-02-06 16:05:48 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-08-29 04:56:53 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, gl_iformat, Scaledtex_w, Scaledtex_h, 0, gl_format, gl_type, NULL);
|
2009-11-14 23:15:09 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-07-12 19:30:25 +00:00
|
|
|
entry.isRenderTarget = true;
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.isDynamic = false;
|
2009-11-14 23:15:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_assert_(entry.texture);
|
2010-02-06 16:05:48 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-07-21 02:34:59 +00:00
|
|
|
if(entry.isDynamic)
|
2009-11-14 23:15:09 +00:00
|
|
|
{
|
2010-07-21 02:34:59 +00:00
|
|
|
Scaledtex_h = h;
|
|
|
|
Scaledtex_w = w;
|
|
|
|
}
|
|
|
|
if (((entry.isRenderTarget || entry.isDynamic) && entry.Scaledw == Scaledtex_w && entry.Scaledh == Scaledtex_h))
|
|
|
|
{
|
2010-08-29 04:56:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, entry.texture);
|
2009-11-14 23:15:09 +00:00
|
|
|
// for some reason mario sunshine errors here...
|
|
|
|
// Beyond Good and Evil does too, occasionally.
|
2010-02-06 16:05:48 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2009-11-14 23:15:09 +00:00
|
|
|
} else {
|
2010-07-21 02:34:59 +00:00
|
|
|
// Delete existing texture.
|
2009-11-14 23:15:09 +00:00
|
|
|
glDeleteTextures(1,(GLuint *)&entry.texture);
|
|
|
|
glGenTextures(1, (GLuint *)&entry.texture);
|
2010-08-29 04:56:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, entry.texture);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, gl_iformat, Scaledtex_w, Scaledtex_h, 0, gl_format, gl_type, NULL);
|
2009-11-14 23:15:09 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.isRenderTarget = !entry.isDynamic;
|
2009-11-14 23:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-21 02:34:59 +00:00
|
|
|
if (!bIsInit)
|
2009-11-14 23:15:09 +00:00
|
|
|
{
|
2010-08-29 04:56:53 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2009-11-14 23:15:09 +00:00
|
|
|
|
2010-08-29 04:56:53 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2010-08-03 10:48:49 +00:00
|
|
|
if (GL_REPORT_ERROR() != GL_NO_ERROR) {
|
2010-08-29 04:56:53 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
2009-11-14 23:15:09 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-21 02:34:59 +00:00
|
|
|
entry.addr = address;
|
2010-10-04 01:54:51 +00:00
|
|
|
entry.w = entry.Realw = w;
|
|
|
|
entry.h = entry.Realh = h;
|
2010-07-18 00:18:31 +00:00
|
|
|
entry.Scaledw = Scaledtex_w;
|
|
|
|
entry.Scaledh = Scaledtex_h;
|
2009-11-14 23:15:09 +00:00
|
|
|
entry.fmt = copyfmt;
|
2010-07-21 02:34:59 +00:00
|
|
|
entry.hash = 0;
|
2009-11-14 23:15:09 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
// Make sure to resolve anything we need to read from.
|
2009-09-03 20:37:35 +00:00
|
|
|
GLuint read_texture = bFromZBuffer ? g_framebufferManager.ResolveAndGetDepthTarget(source_rect) : g_framebufferManager.ResolveAndGetRenderTarget(source_rect);
|
2009-03-08 19:19:51 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
2009-03-05 23:11:13 +00:00
|
|
|
// We have to run a pixel shader, for color conversion.
|
2009-09-03 19:24:16 +00:00
|
|
|
Renderer::ResetAPIState(); // reset any game specific settings
|
2010-07-18 00:18:31 +00:00
|
|
|
if(!entry.isDynamic || g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
|
|
|
if (s_TempFramebuffer == 0)
|
|
|
|
glGenFramebuffersEXT(1, (GLuint *)&s_TempFramebuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
g_framebufferManager.SetFramebuffer(s_TempFramebuffer);
|
|
|
|
// Bind texture to temporary framebuffer
|
2010-08-29 04:56:53 +00:00
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, entry.texture, 0);
|
2010-07-12 19:30:25 +00:00
|
|
|
GL_REPORT_FBO_ERROR();
|
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
|
|
|
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glEnable(GL_TEXTURE_RECTANGLE_ARB);
|
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, read_texture);
|
|
|
|
|
2010-07-18 00:18:31 +00:00
|
|
|
glViewport(0, 0, Scaledtex_w, Scaledtex_h);
|
2010-07-12 19:30:25 +00:00
|
|
|
|
|
|
|
PixelShaderCache::SetCurrentShader(bFromZBuffer ? PixelShaderCache::GetDepthMatrixProgram() : PixelShaderCache::GetColorMatrixProgram());
|
|
|
|
PixelShaderManager::SetColorMatrix(colmat, fConstAdd); // set transformation
|
|
|
|
GL_REPORT_ERRORD();
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
TargetRectangle targetSource = Renderer::ConvertEFBRectangle(source_rect);
|
2009-07-15 00:51:24 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2f((GLfloat)targetSource.left, (GLfloat)targetSource.bottom); glVertex2f(-1, 1);
|
|
|
|
glTexCoord2f((GLfloat)targetSource.left, (GLfloat)targetSource.top ); glVertex2f(-1, -1);
|
|
|
|
glTexCoord2f((GLfloat)targetSource.right, (GLfloat)targetSource.top ); glVertex2f( 1, -1);
|
|
|
|
glTexCoord2f((GLfloat)targetSource.right, (GLfloat)targetSource.bottom); glVertex2f( 1, 1);
|
|
|
|
glEnd();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
// Unbind texture from temporary framebuffer
|
2010-08-29 04:56:53 +00:00
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
2010-09-28 02:15:02 +00:00
|
|
|
|
2010-07-12 19:30:25 +00:00
|
|
|
if(!g_ActiveConfig.bCopyEFBToTexture)
|
|
|
|
{
|
|
|
|
textures[address].hash = TextureConverter::EncodeToRamFromTexture(
|
|
|
|
address,
|
|
|
|
read_texture,
|
2010-07-21 02:34:59 +00:00
|
|
|
xScale,
|
2010-09-28 02:15:02 +00:00
|
|
|
yScale,
|
2010-07-12 19:30:25 +00:00
|
|
|
bFromZBuffer,
|
|
|
|
bIsIntensityFmt,
|
|
|
|
copyfmt,
|
|
|
|
bScaleByHalf,
|
|
|
|
source_rect);
|
|
|
|
|
|
|
|
}
|
2009-06-28 23:35:08 +00:00
|
|
|
// Return to the EFB.
|
2009-09-03 20:37:35 +00:00
|
|
|
g_framebufferManager.SetFramebuffer(0);
|
2009-09-03 19:24:16 +00:00
|
|
|
Renderer::RestoreAPIState();
|
2008-12-26 10:43:18 +00:00
|
|
|
VertexShaderManager::SetViewportChanged();
|
2010-09-28 02:15:02 +00:00
|
|
|
TextureCache::DisableStage(0);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
GL_REPORT_ERRORD();
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2009-09-13 08:21:35 +00:00
|
|
|
if (g_ActiveConfig.bDumpEFBTarget)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
static int count = 0;
|
2010-08-29 04:56:53 +00:00
|
|
|
SaveTexture(StringFromFormat("%sefb_frame_%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), count++).c_str(), GL_TEXTURE_2D, entry.texture, entry.w, entry.h);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::DisableStage(int stage)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-05-09 07:55:30 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
void TextureCache::ClearRenderTargets()
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
2010-05-28 23:14:16 +00:00
|
|
|
for (TexCache::iterator iter = textures.begin(); iter != textures.end(); ++iter)
|
2009-01-11 22:25:57 +00:00
|
|
|
iter->second.isRenderTarget = false;
|
|
|
|
}
|