2013-04-17 21:29:41 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2010-04-18 21:06:18 -06:00
|
|
|
#include <cmath>
|
2014-02-19 04:14:09 -07:00
|
|
|
#include <fstream>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <vector>
|
2008-12-26 05:24:15 -07:00
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Hash.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/OGL/FramebufferManager.h"
|
2014-08-02 00:21:03 -06:00
|
|
|
#include "VideoBackends/OGL/GLInterfaceBase.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/TextureCache.h"
|
|
|
|
#include "VideoBackends/OGL/TextureConverter.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/BPStructs.h"
|
|
|
|
#include "VideoCommon/HiresTextures.h"
|
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
|
|
|
#include "VideoCommon/TextureDecoder.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-05-07 01:43:56 -06:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
namespace OGL
|
|
|
|
{
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2013-02-13 05:12:19 -07:00
|
|
|
static SHADER s_ColorMatrixProgram;
|
|
|
|
static SHADER s_DepthMatrixProgram;
|
2013-01-12 08:29:16 -07:00
|
|
|
static GLuint s_ColorMatrixUniform;
|
|
|
|
static GLuint s_DepthMatrixUniform;
|
2013-11-25 04:19:34 -07:00
|
|
|
static GLuint s_ColorCopyPositionUniform;
|
|
|
|
static GLuint s_DepthCopyPositionUniform;
|
2013-01-14 04:37:31 -07:00
|
|
|
static u32 s_ColorCbufid;
|
|
|
|
static u32 s_DepthCbufid;
|
2013-01-11 07:24:43 -07:00
|
|
|
|
2013-01-18 17:06:34 -07:00
|
|
|
static u32 s_Textures[8];
|
|
|
|
static u32 s_ActiveTexture;
|
|
|
|
|
2014-02-23 15:03:39 -07:00
|
|
|
bool SaveTexture(const std::string& filename, u32 textarget, u32 tex, int virtual_width, int virtual_height, unsigned int level)
|
2008-12-26 05:24:15 -07:00
|
|
|
{
|
2014-01-01 19:25:59 -07:00
|
|
|
if (GLInterface->GetMode() != GLInterfaceMode::MODE_OPENGL)
|
|
|
|
return false;
|
2012-05-12 05:50:03 -06:00
|
|
|
int width = std::max(virtual_width >> level, 1);
|
|
|
|
int height = std::max(virtual_height >> level, 1);
|
2014-10-24 06:25:53 -06:00
|
|
|
std::vector<u8> data(width * height * 4);
|
2013-01-18 16:39:31 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2010-10-19 16:24:27 -06:00
|
|
|
glBindTexture(textarget, tex);
|
2014-10-24 06:25:53 -06:00
|
|
|
glGetTexImage(textarget, level, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
|
2013-01-18 16:39:31 -07:00
|
|
|
glBindTexture(textarget, 0);
|
|
|
|
TextureCache::SetStage();
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2014-10-24 06:25:53 -06:00
|
|
|
return TextureToPng(data.data(), width * 4, filename, width, height, true);
|
2008-12-26 05:24:15 -07:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
TextureCache::TCacheEntry::~TCacheEntry()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2011-02-06 08:02:26 -07:00
|
|
|
if (texture)
|
2009-07-19 02:17:41 -06:00
|
|
|
{
|
2014-03-10 05:30:55 -06:00
|
|
|
for (auto& gtex : s_Textures)
|
|
|
|
if (gtex == texture)
|
2013-10-28 23:09:01 -06:00
|
|
|
gtex = 0;
|
2010-09-27 20:15:02 -06:00
|
|
|
glDeleteTextures(1, &texture);
|
2010-10-19 16:24:27 -06:00
|
|
|
texture = 0;
|
2010-09-27 20:15:02 -06:00
|
|
|
}
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-04-08 06:36:58 -06:00
|
|
|
if (framebuffer)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffers(1, &framebuffer);
|
|
|
|
framebuffer = 0;
|
|
|
|
}
|
2010-09-27 20:15:02 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
TextureCache::TCacheEntry::TCacheEntry()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2010-10-19 16:24:27 -06:00
|
|
|
glGenTextures(1, &texture);
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-04-08 06:36:58 -06:00
|
|
|
framebuffer = 0;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
void TextureCache::TCacheEntry::Bind(unsigned int stage)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2013-02-19 17:22:38 -07:00
|
|
|
if (s_Textures[stage] != texture)
|
2013-01-18 17:06:34 -07:00
|
|
|
{
|
2013-02-19 17:22:38 -07:00
|
|
|
if (s_ActiveTexture != stage)
|
|
|
|
{
|
2013-01-18 17:06:34 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
2013-02-19 17:22:38 -07:00
|
|
|
s_ActiveTexture = stage;
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
|
2013-01-18 17:06:34 -07:00
|
|
|
s_Textures[stage] = texture;
|
|
|
|
}
|
2010-07-12 13:30:25 -06:00
|
|
|
}
|
|
|
|
|
2014-02-23 15:03:39 -07:00
|
|
|
bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int level)
|
2010-07-12 13:30:25 -06:00
|
|
|
{
|
2014-10-09 16:06:29 -06:00
|
|
|
return SaveTexture(filename, GL_TEXTURE_2D_ARRAY, texture, virtual_width, virtual_height, level);
|
2010-09-27 20:15:02 -06:00
|
|
|
}
|
2010-07-12 13:30:25 -06:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width,
|
|
|
|
unsigned int height, unsigned int expanded_width,
|
|
|
|
unsigned int tex_levels, PC_TexFormat pcfmt)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2010-10-19 16:24:27 -06:00
|
|
|
int gl_format = 0,
|
|
|
|
gl_iformat = 0,
|
|
|
|
gl_type = 0;
|
2009-02-20 19:42:35 -07:00
|
|
|
|
2010-09-27 20:15:02 -06:00
|
|
|
if (pcfmt != PC_TEX_FMT_DXT1)
|
2009-05-09 01:55:30 -06:00
|
|
|
{
|
2010-09-27 20:15:02 -06:00
|
|
|
switch (pcfmt)
|
2009-05-09 01:55:30 -06:00
|
|
|
{
|
|
|
|
default:
|
|
|
|
case PC_TEX_FMT_NONE:
|
2013-10-28 23:23:17 -06:00
|
|
|
PanicAlert("Invalid PC texture format %i", pcfmt);
|
2009-05-09 01:55:30 -06:00
|
|
|
case PC_TEX_FMT_BGRA32:
|
|
|
|
gl_format = GL_BGRA;
|
2013-01-14 09:48:38 -07:00
|
|
|
gl_iformat = GL_RGBA;
|
2009-05-09 01:55:30 -06:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2009-05-09 01:55:30 -06:00
|
|
|
case PC_TEX_FMT_RGBA32:
|
|
|
|
gl_format = GL_RGBA;
|
2013-01-14 09:48:38 -07:00
|
|
|
gl_iformat = GL_RGBA;
|
2009-05-09 01:55:30 -06:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2009-05-12 20:06:02 -06: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 16:24:27 -06:00
|
|
|
|
2009-05-12 20:06:02 -06: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 16:24:27 -06:00
|
|
|
|
2009-05-09 01:55:30 -06:00
|
|
|
case PC_TEX_FMT_I8:
|
|
|
|
gl_format = GL_LUMINANCE;
|
2009-06-20 03:29:28 -06:00
|
|
|
gl_iformat = GL_INTENSITY8;
|
2009-05-09 01:55:30 -06:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2009-05-09 01:55:30 -06:00
|
|
|
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-10-19 16:24:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
TCacheEntry &entry = *new TCacheEntry;
|
|
|
|
entry.gl_format = gl_format;
|
|
|
|
entry.gl_iformat = gl_iformat;
|
|
|
|
entry.gl_type = gl_type;
|
|
|
|
entry.pcfmt = pcfmt;
|
|
|
|
|
2014-01-21 02:34:48 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, entry.texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, tex_levels - 1);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-02-20 04:37:01 -07:00
|
|
|
entry.Load(width, height, expanded_width, 0);
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2014-01-21 02:34:48 -07:00
|
|
|
// This isn't needed as Load() also reset the stage in the end
|
|
|
|
//TextureCache::SetStage();
|
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
return &entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
|
2012-08-10 05:13:51 -06:00
|
|
|
unsigned int expanded_width, unsigned int level)
|
2010-10-19 16:24:27 -06:00
|
|
|
{
|
2014-01-21 02:34:48 -07:00
|
|
|
if (pcfmt != PC_TEX_FMT_DXT1)
|
2013-02-19 17:22:38 -07:00
|
|
|
{
|
2014-01-21 02:34:48 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
|
2013-04-15 14:28:55 -06:00
|
|
|
|
|
|
|
if (expanded_width != width)
|
2011-12-25 22:15:54 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width);
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2014-10-09 16:06:29 -06:00
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, level, gl_iformat, width, height, 1, 0, gl_format, gl_type, temp);
|
2009-05-09 01:55:30 -06:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
if (expanded_width != width)
|
2011-12-25 22:15:54 -07:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2009-05-09 01:55:30 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-19 16:24:27 -06: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 01:55:30 -06:00
|
|
|
}
|
2014-01-21 02:34:48 -07:00
|
|
|
TextureCache::SetStage();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture(
|
|
|
|
unsigned int scaled_tex_w, unsigned int scaled_tex_h)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2010-10-19 16:24:27 -06:00
|
|
|
TCacheEntry *const entry = new TCacheEntry;
|
2013-01-18 16:39:31 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, entry->texture);
|
2009-09-04 00:09:21 -06:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
const GLenum
|
|
|
|
gl_format = GL_RGBA,
|
2013-01-14 09:48:38 -07:00
|
|
|
gl_iformat = GL_RGBA,
|
2010-10-19 16:24:27 -06:00
|
|
|
gl_type = GL_UNSIGNED_BYTE;
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2014-10-09 16:06:29 -06:00
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 0);
|
2014-11-06 03:41:39 -07:00
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_iformat, scaled_tex_w, scaled_tex_h, FramebufferManager::GetEFBLayers(), 0, gl_format, gl_type, nullptr);
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-04-08 06:36:58 -06:00
|
|
|
glGenFramebuffers(1, &entry->framebuffer);
|
|
|
|
FramebufferManager::SetFramebuffer(entry->framebuffer);
|
2014-12-04 06:59:16 -07:00
|
|
|
FramebufferManager::FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_ARRAY, entry->texture, 0);
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-01-18 16:39:31 -07:00
|
|
|
SetStage();
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2010-10-19 16:24:27 -06:00
|
|
|
return entry;
|
|
|
|
}
|
2009-11-14 16:15:09 -07:00
|
|
|
|
2011-02-26 16:41:02 -07:00
|
|
|
void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat,
|
2014-03-23 14:44:23 -06:00
|
|
|
PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
|
2011-02-26 16:41:02 -07:00
|
|
|
bool isIntensity, bool scaleByHalf, unsigned int cbufid,
|
|
|
|
const float *colmat)
|
2010-10-19 16:24:27 -06:00
|
|
|
{
|
2013-01-28 10:16:03 -07:00
|
|
|
g_renderer->ResetAPIState(); // reset any game specific settings
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2009-05-09 01:55:30 -06:00
|
|
|
// Make sure to resolve anything we need to read from.
|
2014-03-23 14:44:23 -06:00
|
|
|
const GLuint read_texture = (srcFormat == PEControl::Z24) ?
|
2011-02-26 16:41:02 -07:00
|
|
|
FramebufferManager::ResolveAndGetDepthTarget(srcRect) :
|
|
|
|
FramebufferManager::ResolveAndGetRenderTarget(srcRect);
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2012-01-29 12:24:23 -07:00
|
|
|
if (type != TCET_EC_DYNAMIC || g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 13:30:25 -06:00
|
|
|
{
|
2013-04-08 06:36:58 -06:00
|
|
|
FramebufferManager::SetFramebuffer(framebuffer);
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2014-12-11 00:19:18 -07:00
|
|
|
OpenGL_BindAttributelessVAO();
|
|
|
|
|
2013-01-18 16:39:31 -07:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2014-10-09 16:06:29 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, read_texture);
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2011-12-26 09:35:27 -07:00
|
|
|
glViewport(0, 0, virtual_width, virtual_height);
|
2010-07-12 13:30:25 -06:00
|
|
|
|
2014-08-19 21:18:02 -06:00
|
|
|
GLuint uniform_location;
|
2014-03-23 14:44:23 -06:00
|
|
|
if (srcFormat == PEControl::Z24)
|
2014-03-10 05:30:55 -06:00
|
|
|
{
|
2013-02-13 05:12:19 -07:00
|
|
|
s_DepthMatrixProgram.Bind();
|
2014-03-10 05:30:55 -06:00
|
|
|
if (s_DepthCbufid != cbufid)
|
2013-01-14 04:37:31 -07:00
|
|
|
glUniform4fv(s_DepthMatrixUniform, 5, colmat);
|
|
|
|
s_DepthCbufid = cbufid;
|
2014-08-19 21:18:02 -06:00
|
|
|
uniform_location = s_DepthCopyPositionUniform;
|
2014-03-10 05:30:55 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-13 05:12:19 -07:00
|
|
|
s_ColorMatrixProgram.Bind();
|
2014-03-10 05:30:55 -06:00
|
|
|
if (s_ColorCbufid != cbufid)
|
2013-01-14 04:37:31 -07:00
|
|
|
glUniform4fv(s_ColorMatrixUniform, 7, colmat);
|
|
|
|
s_ColorCbufid = cbufid;
|
2014-08-19 21:18:02 -06:00
|
|
|
uniform_location = s_ColorCopyPositionUniform;
|
2013-01-12 08:29:16 -07:00
|
|
|
}
|
2009-03-07 02:29:25 -07:00
|
|
|
|
2013-11-25 04:19:34 -07:00
|
|
|
TargetRectangle R = g_renderer->ConvertEFBRectangle(srcRect);
|
2014-08-19 21:18:02 -06:00
|
|
|
glUniform4f(uniform_location, static_cast<float>(R.left), static_cast<float>(R.top),
|
|
|
|
static_cast<float>(R.right), static_cast<float>(R.bottom));
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-04-11 08:27:32 -06:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-07-12 13:30:25 -06:00
|
|
|
}
|
2010-10-19 16:24:27 -06:00
|
|
|
|
|
|
|
if (false == g_ActiveConfig.bCopyEFBToTexture)
|
2010-07-12 13:30:25 -06:00
|
|
|
{
|
2011-12-29 17:00:34 -07:00
|
|
|
int encoded_size = TextureConverter::EncodeToRamFromTexture(
|
2010-10-19 16:24:27 -06:00
|
|
|
addr,
|
2010-07-12 13:30:25 -06:00
|
|
|
read_texture,
|
2014-03-23 14:44:23 -06:00
|
|
|
srcFormat == PEControl::Z24,
|
2013-10-28 23:23:17 -06:00
|
|
|
isIntensity,
|
|
|
|
dstFormat,
|
|
|
|
scaleByHalf,
|
2011-02-26 16:41:02 -07:00
|
|
|
srcRect);
|
2011-12-29 17:00:34 -07:00
|
|
|
|
|
|
|
u8* dst = Memory::GetPointer(addr);
|
2013-01-29 15:40:15 -07:00
|
|
|
u64 const new_hash = GetHash64(dst,encoded_size,g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2011-12-29 17:00:34 -07: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);
|
2013-01-29 15:40:15 -07:00
|
|
|
else if (!TextureCache::Find(addr, new_hash))
|
2011-12-29 17:00:34 -07:00
|
|
|
TextureCache::MakeRangeDynamic(addr,encoded_size);
|
2012-06-20 08:43:13 -06:00
|
|
|
|
2013-01-29 15:40:15 -07:00
|
|
|
hash = new_hash;
|
2010-07-12 13:30:25 -06:00
|
|
|
}
|
2010-10-19 16:24:27 -06:00
|
|
|
|
2013-04-15 14:28:55 -06:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2013-04-15 14:28:55 -06:00
|
|
|
if (g_ActiveConfig.bDumpEFBTarget)
|
|
|
|
{
|
2009-05-09 01:55:30 -06:00
|
|
|
static int count = 0;
|
2013-11-15 19:59:59 -07:00
|
|
|
SaveTexture(StringFromFormat("%sefb_frame_%i.png", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(),
|
2014-10-09 16:06:29 -06:00
|
|
|
count++), GL_TEXTURE_2D_ARRAY, texture, virtual_width, virtual_height, 0);
|
2013-04-15 14:28:55 -06:00
|
|
|
}
|
2013-01-28 10:16:03 -07:00
|
|
|
|
|
|
|
g_renderer->RestoreAPIState();
|
2010-10-19 16:24:27 -06:00
|
|
|
}
|
|
|
|
|
2012-12-07 16:54:38 -07:00
|
|
|
TextureCache::TextureCache()
|
2014-11-03 16:53:14 -07:00
|
|
|
{
|
|
|
|
CompileShaders();
|
|
|
|
|
|
|
|
s_ActiveTexture = -1;
|
|
|
|
for (auto& gtex : s_Textures)
|
|
|
|
gtex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TextureCache::~TextureCache()
|
|
|
|
{
|
|
|
|
DeleteShaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::DisableStage(unsigned int stage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::SetStage()
|
|
|
|
{
|
|
|
|
// -1 is the initial value as we don't know which texture should be bound
|
|
|
|
if (s_ActiveTexture != (u32)-1)
|
|
|
|
glActiveTexture(GL_TEXTURE0 + s_ActiveTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCache::CompileShaders()
|
2012-12-07 16:54:38 -07:00
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
const char *pColorMatrixProg =
|
2014-10-09 16:06:29 -06:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-01-12 08:29:16 -07:00
|
|
|
"uniform vec4 colmat[7];\n"
|
2014-10-26 17:52:56 -06:00
|
|
|
"in vec3 f_uv0;\n"
|
2013-11-24 17:06:29 -07:00
|
|
|
"out vec4 ocol0;\n"
|
2013-01-11 07:24:43 -07:00
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2014-10-26 17:52:56 -06:00
|
|
|
" vec4 texcol = texture(samp9, f_uv0);\n"
|
2013-02-13 10:01:06 -07:00
|
|
|
" texcol = round(texcol * colmat[5]) * colmat[6];\n"
|
2013-02-15 05:13:45 -07:00
|
|
|
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + colmat[4];\n"
|
2013-01-12 08:29:16 -07:00
|
|
|
"}\n";
|
2013-01-11 07:24:43 -07:00
|
|
|
|
2013-01-12 08:29:16 -07:00
|
|
|
const char *pDepthMatrixProg =
|
2014-10-09 16:06:29 -06:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-01-12 08:29:16 -07:00
|
|
|
"uniform vec4 colmat[5];\n"
|
2014-10-26 17:52:56 -06:00
|
|
|
"in vec3 f_uv0;\n"
|
2013-11-24 17:06:29 -07:00
|
|
|
"out vec4 ocol0;\n"
|
2013-01-11 07:24:43 -07:00
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2014-11-08 08:19:15 -07:00
|
|
|
" vec4 texcol = texture(samp9, vec3(f_uv0.xy, %s));\n"
|
2014-08-17 01:14:29 -06:00
|
|
|
|
|
|
|
// 255.99998474121 = 16777215/16777216*256
|
|
|
|
" float workspace = texcol.x * 255.99998474121;\n"
|
|
|
|
|
|
|
|
" texcol.x = floor(workspace);\n" // x component
|
|
|
|
|
|
|
|
" workspace = workspace - texcol.x;\n" // subtract x component out
|
|
|
|
" workspace = workspace * 256.0;\n" // shift left 8 bits
|
|
|
|
" texcol.y = floor(workspace);\n" // y component
|
|
|
|
|
|
|
|
" workspace = workspace - texcol.y;\n" // subtract y component out
|
|
|
|
" workspace = workspace * 256.0;\n" // shift left 8 bits
|
|
|
|
" texcol.z = floor(workspace);\n" // z component
|
|
|
|
|
|
|
|
" texcol.w = texcol.x;\n" // duplicate x into w
|
|
|
|
|
|
|
|
" texcol = texcol / 255.0;\n" // normalize components to [0.0..1.0]
|
|
|
|
|
|
|
|
" texcol.w = texcol.w * 15.0;\n"
|
|
|
|
" texcol.w = floor(texcol.w);\n"
|
|
|
|
" texcol.w = texcol.w / 15.0;\n" // w component
|
|
|
|
|
|
|
|
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + colmat[4];\n"
|
2013-01-12 08:29:16 -07:00
|
|
|
"}\n";
|
|
|
|
|
2014-11-08 08:19:15 -07:00
|
|
|
const char *VProgram =
|
|
|
|
"out vec3 %s_uv0;\n"
|
2014-10-09 16:06:29 -06:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-11-25 04:19:34 -07:00
|
|
|
"uniform vec4 copy_position;\n" // left, top, right, bottom
|
2013-01-11 07:41:42 -07:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2013-11-25 04:19:34 -07:00
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
2014-11-08 08:19:15 -07:00
|
|
|
" %s_uv0 = vec3(mix(copy_position.xy, copy_position.zw, rawpos) / vec2(textureSize(samp9, 0).xy), 0.0);\n"
|
2014-10-29 06:47:24 -06:00
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
2013-01-11 07:41:42 -07:00
|
|
|
"}\n";
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2014-10-30 16:29:56 -06:00
|
|
|
const char *GProgram = (g_ActiveConfig.iStereoMode > 0) ?
|
2014-10-26 17:52:56 -06:00
|
|
|
"layout(triangles) in;\n"
|
2014-10-28 11:50:09 -06:00
|
|
|
"layout(triangle_strip, max_vertices = 6) out;\n"
|
2014-11-15 18:15:43 -07:00
|
|
|
"in vec3 v_uv0[3];\n"
|
2014-10-26 17:52:56 -06:00
|
|
|
"out vec3 f_uv0;\n"
|
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" int layers = textureSize(samp9, 0).z;\n"
|
|
|
|
" for (int layer = 0; layer < layers; ++layer) {\n"
|
2014-11-16 01:21:19 -07:00
|
|
|
" for (int i = 0; i < 3; ++i) {\n"
|
2014-11-08 08:19:15 -07:00
|
|
|
" f_uv0 = vec3(v_uv0[i].xy, layer);\n"
|
2014-10-26 17:52:56 -06:00
|
|
|
" gl_Position = gl_in[i].gl_Position;\n"
|
|
|
|
" gl_Layer = layer;\n"
|
|
|
|
" EmitVertex();\n"
|
|
|
|
" }\n"
|
|
|
|
" EndPrimitive();\n"
|
|
|
|
" }\n"
|
2014-10-29 06:47:24 -06:00
|
|
|
"}\n" : nullptr;
|
2014-10-26 17:52:56 -06:00
|
|
|
|
2014-11-08 08:19:15 -07:00
|
|
|
const char* prefix = (GProgram == nullptr) ? "f" : "v";
|
|
|
|
const char* depth_layer = (g_ActiveConfig.bStereoMonoEFBDepth) ? "0" : "f_uv0.z";
|
|
|
|
|
|
|
|
ProgramShaderCache::CompileShader(s_ColorMatrixProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), pColorMatrixProg, GProgram);
|
|
|
|
ProgramShaderCache::CompileShader(s_DepthMatrixProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), StringFromFormat(pDepthMatrixProg, depth_layer).c_str(), GProgram);
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-02-13 05:12:19 -07:00
|
|
|
s_ColorMatrixUniform = glGetUniformLocation(s_ColorMatrixProgram.glprogid, "colmat");
|
|
|
|
s_DepthMatrixUniform = glGetUniformLocation(s_DepthMatrixProgram.glprogid, "colmat");
|
2013-01-14 04:37:31 -07:00
|
|
|
s_ColorCbufid = -1;
|
|
|
|
s_DepthCbufid = -1;
|
2013-04-15 14:28:55 -06:00
|
|
|
|
2013-11-25 04:19:34 -07:00
|
|
|
s_ColorCopyPositionUniform = glGetUniformLocation(s_ColorMatrixProgram.glprogid, "copy_position");
|
|
|
|
s_DepthCopyPositionUniform = glGetUniformLocation(s_DepthMatrixProgram.glprogid, "copy_position");
|
2012-12-07 16:54:38 -07:00
|
|
|
}
|
|
|
|
|
2014-11-03 16:53:14 -07:00
|
|
|
void TextureCache::DeleteShaders()
|
2010-10-19 16:24:27 -06:00
|
|
|
{
|
2013-01-11 07:24:43 -07:00
|
|
|
s_ColorMatrixProgram.Destroy();
|
|
|
|
s_DepthMatrixProgram.Destroy();
|
2010-10-19 16:24:27 -06:00
|
|
|
}
|
|
|
|
|
2009-01-11 15:25:57 -07:00
|
|
|
}
|