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"
|
2011-12-30 00:00:34 +00:00
|
|
|
#include "HW/Memmap.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "ImageWrite.h"
|
|
|
|
#include "MemoryUtil.h"
|
2011-12-08 04:04:34 +00:00
|
|
|
#include "ProgramShaderCache.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "PixelShaderManager.h"
|
2010-09-28 02:15:02 +00:00
|
|
|
#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-10-19 22:24:27 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
static SHADER s_ColorMatrixProgram;
|
|
|
|
static SHADER s_DepthMatrixProgram;
|
2013-01-12 15:29:16 +00:00
|
|
|
static GLuint s_ColorMatrixUniform;
|
|
|
|
static GLuint s_DepthMatrixUniform;
|
2013-01-14 11:37:31 +00:00
|
|
|
static u32 s_ColorCbufid;
|
|
|
|
static u32 s_DepthCbufid;
|
2013-01-11 14:24:43 +00:00
|
|
|
|
2013-01-19 00:06:34 +00:00
|
|
|
static u32 s_Textures[8];
|
|
|
|
static u32 s_ActiveTexture;
|
|
|
|
|
2012-12-09 19:30:29 +00:00
|
|
|
struct VBOCache {
|
|
|
|
GLuint vbo;
|
2012-12-09 21:49:58 +00:00
|
|
|
GLuint vao;
|
2012-12-09 19:30:29 +00:00
|
|
|
TargetRectangle targetSource;
|
|
|
|
};
|
2012-12-11 16:25:53 +00:00
|
|
|
static std::map<u64,VBOCache> s_VBO;
|
2012-12-09 19:30:29 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
static u32 s_TempFramebuffer = 0;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2012-05-12 11:50:03 +00:00
|
|
|
bool SaveTexture(const char* filename, u32 textarget, u32 tex, int virtual_width, int virtual_height, unsigned int level)
|
2008-12-26 12:24:15 +00:00
|
|
|
{
|
2012-05-12 11:50:03 +00:00
|
|
|
int width = std::max(virtual_width >> level, 1);
|
|
|
|
int height = std::max(virtual_height >> level, 1);
|
2009-05-09 07:55:30 +00:00
|
|
|
std::vector<u32> data(width * height);
|
2013-01-18 23:39:31 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2010-10-19 22:24:27 +00:00
|
|
|
glBindTexture(textarget, tex);
|
2012-05-12 11:50:03 +00:00
|
|
|
glGetTexImage(textarget, level, GL_BGRA, GL_UNSIGNED_BYTE, &data[0]);
|
2013-01-18 23:39:31 +00:00
|
|
|
glBindTexture(textarget, 0);
|
|
|
|
TextureCache::SetStage();
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
const GLenum err = GL_REPORT_ERROR();
|
|
|
|
if (GL_NO_ERROR != err)
|
2009-05-09 07:55:30 +00:00
|
|
|
{
|
|
|
|
PanicAlert("Can't save texture, GL Error: %s", gluErrorString(err));
|
2010-10-19 22:24:27 +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-10-19 22:24:27 +00:00
|
|
|
TextureCache::TCacheEntry::~TCacheEntry()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2011-02-06 15:02:26 +00:00
|
|
|
if (texture)
|
2009-07-19 08:17:41 +00:00
|
|
|
{
|
2013-01-19 00:06:34 +00:00
|
|
|
for(int i=0; i<8; i++)
|
|
|
|
if(s_Textures[i] == texture)
|
|
|
|
s_Textures[i] = 0;
|
2010-09-28 02:15:02 +00:00
|
|
|
glDeleteTextures(1, &texture);
|
2010-10-19 22:24:27 +00:00
|
|
|
texture = 0;
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::TCacheEntry::TCacheEntry()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
glGenTextures(1, &texture);
|
2012-12-12 15:57:00 +00:00
|
|
|
currmode.hex = 0;
|
|
|
|
currmode1.hex = 0;
|
2010-10-19 22:24:27 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
void TextureCache::TCacheEntry::Bind(unsigned int stage)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
// TODO: is this already done somewhere else?
|
|
|
|
TexMode0 &tm0 = bpmem.tex[stage >> 2].texMode0[stage & 3];
|
|
|
|
TexMode1 &tm1 = bpmem.tex[stage >> 2].texMode1[stage & 3];
|
2012-12-12 15:57:00 +00:00
|
|
|
|
|
|
|
if(currmode.hex != tm0.hex || currmode1.hex != tm1.hex)
|
2013-01-19 00:06:34 +00:00
|
|
|
{
|
|
|
|
if(s_ActiveTexture != stage)
|
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
|
|
|
if(s_Textures[stage] != texture)
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
|
2012-12-12 15:57:00 +00:00
|
|
|
SetTextureParameters(tm0, tm1);
|
2013-01-19 00:06:34 +00:00
|
|
|
s_ActiveTexture = stage;
|
|
|
|
s_Textures[stage] = texture;
|
|
|
|
}
|
|
|
|
else if (s_Textures[stage] != texture)
|
|
|
|
{
|
|
|
|
if(s_ActiveTexture != stage)
|
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
s_ActiveTexture = stage;
|
|
|
|
s_Textures[stage] = texture;
|
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2012-05-12 11:50:03 +00:00
|
|
|
bool TextureCache::TCacheEntry::Save(const char filename[], unsigned int level)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
// TODO: make ogl dump PNGs
|
|
|
|
std::string tga_filename(filename);
|
|
|
|
tga_filename.replace(tga_filename.size() - 3, 3, "tga");
|
2009-03-08 19:19:51 +00:00
|
|
|
|
2012-05-12 11:50:03 +00:00
|
|
|
return SaveTexture(tga_filename.c_str(), GL_TEXTURE_2D, texture, virtual_width, virtual_height, level);
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width,
|
|
|
|
unsigned int height, unsigned int expanded_width,
|
|
|
|
unsigned int tex_levels, PC_TexFormat pcfmt)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
int gl_format = 0,
|
|
|
|
gl_iformat = 0,
|
|
|
|
gl_type = 0;
|
2009-02-21 02:42:35 +00:00
|
|
|
|
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;
|
2013-01-14 16:48:38 +00:00
|
|
|
gl_iformat = GL_RGBA;
|
2009-05-09 07:55:30 +00:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
case PC_TEX_FMT_RGBA32:
|
|
|
|
gl_format = GL_RGBA;
|
2013-01-14 16:48:38 +00:00
|
|
|
gl_iformat = GL_RGBA;
|
2009-05-09 07:55:30 +00:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
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;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2009-05-13 02:06:02 +00:00
|
|
|
case PC_TEX_FMT_IA4_AS_IA8:
|
|
|
|
gl_format = GL_LUMINANCE_ALPHA;
|
|
|
|
gl_iformat = GL_LUMINANCE4_ALPHA4;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
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;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
case PC_TEX_FMT_IA8:
|
|
|
|
gl_format = GL_LUMINANCE_ALPHA;
|
|
|
|
gl_iformat = GL_LUMINANCE8_ALPHA8;
|
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
case PC_TEX_FMT_RGB565:
|
|
|
|
gl_format = GL_RGB;
|
|
|
|
gl_iformat = GL_RGB;
|
|
|
|
gl_type = GL_UNSIGNED_SHORT_5_6_5;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TCacheEntry &entry = *new TCacheEntry;
|
|
|
|
entry.gl_format = gl_format;
|
|
|
|
entry.gl_iformat = gl_iformat;
|
|
|
|
entry.gl_type = gl_type;
|
|
|
|
entry.pcfmt = pcfmt;
|
|
|
|
|
2013-01-16 20:15:07 +00:00
|
|
|
entry.m_tex_levels = tex_levels;
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
return &entry;
|
|
|
|
}
|
|
|
|
|
2013-01-18 23:47:48 +00:00
|
|
|
void TextureCache::TCacheEntry::Load(unsigned int stage, unsigned int width, unsigned int height,
|
2010-11-24 19:13:19 +00:00
|
|
|
unsigned int expanded_width, unsigned int level, bool autogen_mips)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2013-01-19 00:06:34 +00:00
|
|
|
if(s_ActiveTexture != stage)
|
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
|
|
|
if(s_Textures[stage] != texture)
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
s_ActiveTexture = stage;
|
|
|
|
s_Textures[stage] = texture;
|
2013-01-16 20:15:07 +00:00
|
|
|
|
|
|
|
if(level == 0 && m_tex_levels != 0)
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, m_tex_levels - 1);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
if (pcfmt != PC_TEX_FMT_DXT1)
|
|
|
|
{
|
|
|
|
if (expanded_width != width)
|
2011-12-26 05:15:54 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2013-01-16 20:15:07 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, level, gl_iformat, width, height, 0, gl_format, gl_type, temp);
|
|
|
|
|
|
|
|
if (autogen_mips)
|
2013-01-14 16:48:38 +00:00
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2009-05-09 07:55:30 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
if (expanded_width != width)
|
2011-12-26 05:15:54 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
PanicAlert("PC_TEX_FMT_DXT1 support disabled");
|
|
|
|
//glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
|
|
|
|
//width, height, 0, expanded_width * expanded_height/2, temp);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-05-19 21:54:54 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture(
|
|
|
|
unsigned int scaled_tex_w, unsigned int scaled_tex_h)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
TCacheEntry *const entry = new TCacheEntry;
|
2013-01-18 23:39:31 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2010-10-19 22:24:27 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, entry->texture);
|
2009-09-04 06:09:21 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
const GLenum
|
|
|
|
gl_format = GL_RGBA,
|
2013-01-14 16:48:38 +00:00
|
|
|
gl_iformat = GL_RGBA,
|
2010-10-19 22:24:27 +00:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
2013-01-16 20:15:07 +00:00
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
|
|
|
entry->m_tex_levels = 1;
|
2009-11-14 23:15:09 +00:00
|
|
|
|
2013-01-16 14:12:39 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, gl_iformat, scaled_tex_w, scaled_tex_h, 0, gl_format, gl_type, NULL);
|
|
|
|
|
2013-01-18 23:39:31 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
SetStage();
|
|
|
|
|
2013-01-16 14:12:39 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2009-11-14 23:15:09 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
return entry;
|
|
|
|
}
|
2009-11-14 23:15:09 +00:00
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat,
|
|
|
|
unsigned int srcFormat, const EFBRectangle& srcRect,
|
|
|
|
bool isIntensity, bool scaleByHalf, unsigned int cbufid,
|
|
|
|
const float *colmat)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2013-01-28 17:16:03 +00:00
|
|
|
g_renderer->ResetAPIState(); // reset any game specific settings
|
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
// Make sure to resolve anything we need to read from.
|
2011-02-26 23:41:02 +00:00
|
|
|
const GLuint read_texture = (srcFormat == PIXELFMT_Z24) ?
|
|
|
|
FramebufferManager::ResolveAndGetDepthTarget(srcRect) :
|
|
|
|
FramebufferManager::ResolveAndGetRenderTarget(srcRect);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
GL_REPORT_ERRORD();
|
|
|
|
|
2012-01-29 19:24:23 +00:00
|
|
|
if (type != TCET_EC_DYNAMIC || g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
|
|
|
if (s_TempFramebuffer == 0)
|
2013-01-03 11:06:47 +00:00
|
|
|
glGenFramebuffers(1, (GLuint*)&s_TempFramebuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(s_TempFramebuffer);
|
2010-07-12 19:30:25 +00:00
|
|
|
// Bind texture to temporary framebuffer
|
2013-01-03 11:06:47 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
|
2010-07-12 19:30:25 +00:00
|
|
|
GL_REPORT_FBO_ERROR();
|
|
|
|
GL_REPORT_ERRORD();
|
2013-01-17 10:16:12 +00:00
|
|
|
|
2013-01-18 23:39:31 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2013-01-03 11:06:47 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE, read_texture);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2011-12-26 16:35:27 +00:00
|
|
|
glViewport(0, 0, virtual_width, virtual_height);
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2013-01-12 15:29:16 +00:00
|
|
|
if(srcFormat == PIXELFMT_Z24) {
|
2013-02-13 12:12:19 +00:00
|
|
|
s_DepthMatrixProgram.Bind();
|
2013-01-14 11:37:31 +00:00
|
|
|
if(s_DepthCbufid != cbufid)
|
|
|
|
glUniform4fv(s_DepthMatrixUniform, 5, colmat);
|
|
|
|
s_DepthCbufid = cbufid;
|
2013-01-12 15:29:16 +00:00
|
|
|
} else {
|
2013-02-13 12:12:19 +00:00
|
|
|
s_ColorMatrixProgram.Bind();
|
2013-01-14 11:37:31 +00:00
|
|
|
if(s_ColorCbufid != cbufid)
|
|
|
|
glUniform4fv(s_ColorMatrixUniform, 7, colmat);
|
|
|
|
s_ColorCbufid = cbufid;
|
2013-01-12 15:29:16 +00:00
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
TargetRectangle targetSource = g_renderer->ConvertEFBRectangle(srcRect);
|
2011-08-25 09:28:29 +00:00
|
|
|
GL_REPORT_ERRORD();
|
2012-10-28 03:50:06 +00:00
|
|
|
|
2012-12-09 19:30:29 +00:00
|
|
|
// should be unique enough, if not, vbo will "only" be uploaded to much
|
2012-12-11 16:25:53 +00:00
|
|
|
u64 targetSourceHash = u64(targetSource.left)<<48 | u64(targetSource.top)<<32 | u64(targetSource.right)<<16 | u64(targetSource.bottom);
|
|
|
|
std::map<u64, VBOCache>::iterator vbo_it = s_VBO.find(targetSourceHash);
|
2012-12-09 19:30:29 +00:00
|
|
|
|
|
|
|
if(vbo_it == s_VBO.end()) {
|
|
|
|
VBOCache item;
|
|
|
|
item.targetSource.bottom = -1;
|
|
|
|
item.targetSource.top = -1;
|
|
|
|
item.targetSource.left = -1;
|
|
|
|
item.targetSource.right = -1;
|
|
|
|
glGenBuffers(1, &item.vbo);
|
2012-12-09 21:49:58 +00:00
|
|
|
glGenVertexArrays(1, &item.vao);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, item.vbo);
|
|
|
|
glBindVertexArray(item.vao);
|
|
|
|
|
2013-01-11 14:41:42 +00:00
|
|
|
glEnableVertexAttribArray(SHADER_POSITION_ATTRIB);
|
|
|
|
glVertexAttribPointer(SHADER_POSITION_ATTRIB, 2, GL_FLOAT, 0, sizeof(GLfloat)*4, (GLfloat*)NULL);
|
|
|
|
glEnableVertexAttribArray(SHADER_TEXTURE0_ATTRIB);
|
|
|
|
glVertexAttribPointer(SHADER_TEXTURE0_ATTRIB, 2, GL_FLOAT, 0, sizeof(GLfloat)*4, (GLfloat*)NULL+2);
|
2012-12-09 21:49:58 +00:00
|
|
|
|
2012-12-11 16:25:53 +00:00
|
|
|
vbo_it = s_VBO.insert(std::pair<u64,VBOCache>(targetSourceHash, item)).first;
|
2012-12-09 19:30:29 +00:00
|
|
|
}
|
|
|
|
if(!(vbo_it->second.targetSource == targetSource)) {
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
-1.f, 1.f,
|
|
|
|
(GLfloat)targetSource.left, (GLfloat)targetSource.bottom,
|
|
|
|
-1.f, -1.f,
|
|
|
|
(GLfloat)targetSource.left, (GLfloat)targetSource.top,
|
|
|
|
1.f, -1.f,
|
|
|
|
(GLfloat)targetSource.right, (GLfloat)targetSource.top,
|
|
|
|
1.f, 1.f,
|
|
|
|
(GLfloat)targetSource.right, (GLfloat)targetSource.bottom
|
|
|
|
};
|
2012-12-09 21:49:58 +00:00
|
|
|
|
2012-12-09 19:30:29 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_it->second.vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, 4*4*sizeof(GLfloat), vertices, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
vbo_it->second.targetSource = targetSource;
|
2012-12-09 21:49:58 +00:00
|
|
|
}
|
2012-12-07 20:09:48 +00:00
|
|
|
|
2012-12-09 21:49:58 +00:00
|
|
|
glBindVertexArray(vbo_it->second.vao);
|
2011-08-25 09:28:29 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2013-01-03 08:45:12 +00:00
|
|
|
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
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
|
2013-01-03 11:06:47 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
if (false == g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2011-12-30 00:00:34 +00:00
|
|
|
int encoded_size = TextureConverter::EncodeToRamFromTexture(
|
2010-10-19 22:24:27 +00:00
|
|
|
addr,
|
2010-07-12 19:30:25 +00:00
|
|
|
read_texture,
|
2011-02-26 23:41:02 +00:00
|
|
|
srcFormat == PIXELFMT_Z24,
|
|
|
|
isIntensity,
|
|
|
|
dstFormat,
|
|
|
|
scaleByHalf,
|
|
|
|
srcRect);
|
2011-12-30 00:00:34 +00:00
|
|
|
|
|
|
|
u8* dst = Memory::GetPointer(addr);
|
2012-06-20 14:43:13 +00:00
|
|
|
u64 hash = GetHash64(dst,encoded_size,g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2011-12-30 00:00:34 +00:00
|
|
|
|
|
|
|
// Mark texture entries in destination address range dynamic unless caching is enabled and the texture entry is up to date
|
|
|
|
if (!g_ActiveConfig.bEFBCopyCacheEnable)
|
|
|
|
TextureCache::MakeRangeDynamic(addr,encoded_size);
|
|
|
|
else if (!TextureCache::Find(addr, hash))
|
|
|
|
TextureCache::MakeRangeDynamic(addr,encoded_size);
|
2012-06-20 14:43:13 +00:00
|
|
|
|
|
|
|
this->hash = hash;
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2008-12-26 10:43:18 +00:00
|
|
|
VertexShaderManager::SetViewportChanged();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
GL_REPORT_ERRORD();
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
if (g_ActiveConfig.bDumpEFBTarget)
|
|
|
|
{
|
2009-05-09 07:55:30 +00:00
|
|
|
static int count = 0;
|
2011-02-28 20:40:15 +00:00
|
|
|
SaveTexture(StringFromFormat("%sefb_frame_%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
2012-05-12 11:50:03 +00:00
|
|
|
count++).c_str(), GL_TEXTURE_2D, texture, virtual_width, virtual_height, 0);
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
2013-01-28 17:16:03 +00:00
|
|
|
|
|
|
|
g_renderer->RestoreAPIState();
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::TCacheEntry::SetTextureParameters(const TexMode0 &newmode, const TexMode1 &newmode1)
|
|
|
|
{
|
2012-12-12 15:57:00 +00:00
|
|
|
currmode = newmode;
|
|
|
|
currmode1 = newmode1;
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
// TODO: not used anywhere
|
|
|
|
TexMode0 mode = newmode;
|
|
|
|
//mode1 = newmode1;
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
2011-04-01 01:46:18 +00:00
|
|
|
(newmode.mag_filter || g_Config.bForceFiltering) ? GL_LINEAR : GL_NEAREST);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2013-01-16 20:15:07 +00:00
|
|
|
if (m_tex_levels != 1)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
|
|
|
// TODO: not used anywhere
|
|
|
|
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]);
|
2013-01-16 20:15:07 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, newmode1.min_lod >> 4);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, newmode1.max_lod >> 4);
|
2009-05-09 07:55:30 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
else
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
2011-04-01 01:46:18 +00:00
|
|
|
(g_ActiveConfig.bForceFiltering || newmode.min_filter >= 4) ? GL_LINEAR : GL_NEAREST);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
if (g_Config.iMaxAnisotropy >= 1)
|
|
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
|
|
|
|
(float)(1 << g_ActiveConfig.iMaxAnisotropy));
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 23:54:38 +00:00
|
|
|
TextureCache::TextureCache()
|
|
|
|
{
|
2013-01-12 15:29:16 +00:00
|
|
|
const char *pColorMatrixProg =
|
2013-01-18 23:39:31 +00:00
|
|
|
"uniform sampler2DRect samp9;\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"uniform vec4 colmat[7];\n"
|
2013-01-11 14:41:42 +00:00
|
|
|
"in vec2 uv0;\n"
|
2013-01-11 14:24:43 +00:00
|
|
|
"out vec4 ocol0;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2013-02-13 17:01:06 +00:00
|
|
|
" vec4 texcol = texture2DRect(samp9, uv0);\n"
|
|
|
|
" texcol = round(texcol * colmat[5]) * colmat[6];\n"
|
|
|
|
" ocol0 = mat4(colmat[0], colmat[1], colmat[2], colmat[3]) * texcol + colmat[4];\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"}\n";
|
2013-01-11 14:24:43 +00:00
|
|
|
|
2013-01-12 15:29:16 +00:00
|
|
|
const char *pDepthMatrixProg =
|
2013-01-18 23:39:31 +00:00
|
|
|
"uniform sampler2DRect samp9;\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"uniform vec4 colmat[5];\n"
|
2013-01-11 14:41:42 +00:00
|
|
|
"in vec2 uv0;\n"
|
2013-01-11 14:24:43 +00:00
|
|
|
"out vec4 ocol0;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2013-02-13 17:01:06 +00:00
|
|
|
" vec4 texcol = texture2DRect(samp9, uv0);\n"
|
|
|
|
" vec4 EncodedDepth = fract((texcol.r * (16777215.0f/16777216.0f)) * vec4(1.0f,256.0f,256.0f*256.0f,1.0f));\n"
|
|
|
|
" texcol = round(EncodedDepth * (16777216.0f/16777215.0f) * vec4(255.0f,255.0f,255.0f,15.0f)) / vec4(255.0f,255.0f,255.0f,15.0f);\n"
|
|
|
|
" ocol0 = mat4(colmat[0], colmat[1], colmat[2], colmat[3]) * texcol + colmat[4];"
|
2013-01-12 15:29:16 +00:00
|
|
|
"}\n";
|
|
|
|
|
2013-01-11 14:41:42 +00:00
|
|
|
|
|
|
|
const char *VProgram =
|
2013-01-11 20:24:59 +00:00
|
|
|
"in vec2 rawpos;\n"
|
2013-01-14 21:59:08 +00:00
|
|
|
"in vec2 tex0;\n"
|
2013-01-11 14:41:42 +00:00
|
|
|
"out vec2 uv0;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2013-01-14 21:59:08 +00:00
|
|
|
" uv0 = tex0;\n"
|
2013-01-11 20:24:59 +00:00
|
|
|
" gl_Position = vec4(rawpos,0,1);\n"
|
2013-01-11 14:41:42 +00:00
|
|
|
"}\n";
|
2013-02-13 12:12:19 +00:00
|
|
|
|
|
|
|
ProgramShaderCache::CompileShader(s_ColorMatrixProgram, VProgram, pColorMatrixProg);
|
|
|
|
ProgramShaderCache::CompileShader(s_DepthMatrixProgram, VProgram, pDepthMatrixProg);
|
2013-01-12 15:29:16 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
s_ColorMatrixUniform = glGetUniformLocation(s_ColorMatrixProgram.glprogid, "colmat");
|
|
|
|
s_DepthMatrixUniform = glGetUniformLocation(s_DepthMatrixProgram.glprogid, "colmat");
|
2013-01-14 11:37:31 +00:00
|
|
|
s_ColorCbufid = -1;
|
|
|
|
s_DepthCbufid = -1;
|
2013-01-19 00:06:34 +00:00
|
|
|
|
|
|
|
s_ActiveTexture = -1;
|
|
|
|
for(int i=0; i<8; i++)
|
|
|
|
s_Textures[i] = -1;
|
2012-12-07 23:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::~TextureCache()
|
|
|
|
{
|
2013-01-11 14:24:43 +00:00
|
|
|
s_ColorMatrixProgram.Destroy();
|
|
|
|
s_DepthMatrixProgram.Destroy();
|
|
|
|
|
2012-12-11 16:25:53 +00:00
|
|
|
for(std::map<u64, VBOCache>::iterator it = s_VBO.begin(); it != s_VBO.end(); it++) {
|
2012-12-09 21:49:58 +00:00
|
|
|
glDeleteBuffers(1, &it->second.vbo);
|
|
|
|
glDeleteVertexArrays(1, &it->second.vao);
|
2012-12-09 19:30:29 +00:00
|
|
|
}
|
2012-12-09 19:56:58 +00:00
|
|
|
s_VBO.clear();
|
2012-12-07 23:54:38 +00:00
|
|
|
|
|
|
|
if (s_TempFramebuffer)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2013-01-03 11:06:47 +00:00
|
|
|
glDeleteFramebuffers(1, (GLuint*)&s_TempFramebuffer);
|
2011-12-26 05:15:54 +00:00
|
|
|
s_TempFramebuffer = 0;
|
2012-12-07 23:54:38 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::DisableStage(unsigned int stage)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
}
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2013-01-18 23:39:31 +00:00
|
|
|
void TextureCache::SetStage ()
|
|
|
|
{
|
2013-01-19 00:06:34 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + s_ActiveTexture);
|
2013-01-18 23:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|