2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2010-04-19 03:06:18 +00:00
|
|
|
#include <cmath>
|
2014-02-19 11:14:09 +00:00
|
|
|
#include <fstream>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <vector>
|
2008-12-26 12:24:15 +00:00
|
|
|
|
2014-02-17 10:18:15 +00: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 06:21:03 +00:00
|
|
|
#include "VideoBackends/OGL/GLInterfaceBase.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
2015-05-28 22:53:07 +00:00
|
|
|
#include "VideoBackends/OGL/SamplerCache.h"
|
2015-02-20 23:12:27 +00:00
|
|
|
#include "VideoBackends/OGL/StreamBuffer.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#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 07:43:56 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-06-30 01:19:19 +00:00
|
|
|
static SHADER s_ColorCopyProgram;
|
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-11-25 11:19:34 +00:00
|
|
|
static GLuint s_ColorCopyPositionUniform;
|
2015-06-30 01:19:19 +00:00
|
|
|
static GLuint s_ColorMatrixPositionUniform;
|
2013-11-25 11:19:34 +00:00
|
|
|
static GLuint s_DepthCopyPositionUniform;
|
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;
|
|
|
|
|
2015-02-20 23:12:27 +00:00
|
|
|
static SHADER s_palette_pixel_shader[3];
|
|
|
|
static StreamBuffer* s_palette_stream_buffer = nullptr;
|
|
|
|
static GLuint s_palette_resolv_texture;
|
|
|
|
static GLuint s_palette_buffer_offset_uniform[3];
|
|
|
|
static GLuint s_palette_multiplier_uniform[3];
|
|
|
|
static GLuint s_palette_copy_position_uniform[3];
|
|
|
|
|
2014-02-23 22:03:39 +00:00
|
|
|
bool SaveTexture(const std::string& filename, u32 textarget, u32 tex, int virtual_width, int virtual_height, unsigned int level)
|
2008-12-26 12:24:15 +00:00
|
|
|
{
|
2014-01-02 02:25:59 +00:00
|
|
|
if (GLInterface->GetMode() != GLInterfaceMode::MODE_OPENGL)
|
|
|
|
return false;
|
2012-05-12 11:50:03 +00:00
|
|
|
int width = std::max(virtual_width >> level, 1);
|
|
|
|
int height = std::max(virtual_height >> level, 1);
|
2014-10-24 12:25:53 +00:00
|
|
|
std::vector<u8> data(width * height * 4);
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2010-10-19 22:24:27 +00:00
|
|
|
glBindTexture(textarget, tex);
|
2014-10-24 12:25:53 +00:00
|
|
|
glGetTexImage(textarget, level, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
|
2013-01-18 23:39:31 +00:00
|
|
|
TextureCache::SetStage();
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2014-10-24 12:25:53 +00:00
|
|
|
return TextureToPng(data.data(), width * 4, filename, width, height, true);
|
2008-12-26 12:24:15 +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
|
|
|
{
|
2011-02-06 15:02:26 +00:00
|
|
|
if (texture)
|
2009-07-19 08:17:41 +00:00
|
|
|
{
|
2014-03-10 11:30:55 +00:00
|
|
|
for (auto& gtex : s_Textures)
|
|
|
|
if (gtex == texture)
|
2013-10-29 05:09:01 +00:00
|
|
|
gtex = 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
|
|
|
}
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2013-04-08 12:36:58 +00:00
|
|
|
if (framebuffer)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffers(1, &framebuffer);
|
|
|
|
framebuffer = 0;
|
|
|
|
}
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-01-11 14:03:41 +00:00
|
|
|
TextureCache::TCacheEntry::TCacheEntry(const TCacheEntryConfig& _config)
|
|
|
|
: TCacheEntryBase(_config)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
glGenTextures(1, &texture);
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2013-04-08 12:36:58 +00:00
|
|
|
framebuffer = 0;
|
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
|
|
|
{
|
2013-02-20 00:22:38 +00:00
|
|
|
if (s_Textures[stage] != texture)
|
2013-01-19 00:06:34 +00:00
|
|
|
{
|
2013-02-20 00:22:38 +00:00
|
|
|
if (s_ActiveTexture != stage)
|
|
|
|
{
|
2013-01-19 00:06:34 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + stage);
|
2013-02-20 00:22:38 +00:00
|
|
|
s_ActiveTexture = stage;
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-09 22:06:29 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
|
2013-01-19 00:06:34 +00:00
|
|
|
s_Textures[stage] = texture;
|
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:03:39 +00:00
|
|
|
bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int level)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2015-01-11 14:03:41 +00:00
|
|
|
return SaveTexture(filename, GL_TEXTURE_2D_ARRAY, texture, config.width, config.height, level);
|
2010-09-28 02:15:02 +00:00
|
|
|
}
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2015-01-17 09:01:41 +00:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2015-01-17 09:01:41 +00:00
|
|
|
TCacheEntry* entry = new TCacheEntry(config);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2015-01-17 09:01:41 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, entry->texture);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-01-17 09:01:41 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, config.levels - 1);
|
2014-01-21 09:34:48 +00:00
|
|
|
|
2015-01-17 09:01:41 +00:00
|
|
|
if (config.rendertarget)
|
|
|
|
{
|
|
|
|
for (u32 level = 0; level <= config.levels; level++)
|
|
|
|
{
|
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, config.width, config.height, config.layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
}
|
|
|
|
glGenFramebuffers(1, &entry->framebuffer);
|
|
|
|
FramebufferManager::SetFramebuffer(entry->framebuffer);
|
|
|
|
FramebufferManager::FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_ARRAY, entry->texture, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureCache::SetStage();
|
|
|
|
return entry;
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 01:19:19 +00:00
|
|
|
void TextureCache::TCacheEntry::CopyRectangleFromTexture(
|
|
|
|
const TCacheEntryBase* source,
|
|
|
|
const MathUtil::Rectangle<int> &srcrect,
|
|
|
|
const MathUtil::Rectangle<int> &dstrect)
|
2015-06-13 13:51:58 +00:00
|
|
|
{
|
2015-06-30 01:19:19 +00:00
|
|
|
TCacheEntry* srcentry = (TCacheEntry*)source;
|
|
|
|
if (srcrect.GetWidth() == dstrect.GetWidth()
|
|
|
|
&& srcrect.GetHeight() == dstrect.GetHeight()
|
|
|
|
&& g_ActiveConfig.backend_info.bSupportsCopySubImage)
|
|
|
|
{
|
|
|
|
glCopyImageSubData(
|
|
|
|
srcentry->texture,
|
|
|
|
GL_TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
srcrect.left,
|
|
|
|
srcrect.top,
|
|
|
|
0,
|
|
|
|
texture,
|
|
|
|
GL_TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
dstrect.left,
|
|
|
|
dstrect.top,
|
|
|
|
0,
|
|
|
|
dstrect.GetWidth(),
|
|
|
|
dstrect.GetHeight(),
|
|
|
|
1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (!config.rendertarget)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
FramebufferManager::SetFramebuffer(framebuffer);
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, srcentry->texture);
|
|
|
|
g_sampler_cache->BindLinearSampler(9);
|
|
|
|
glViewport(dstrect.left, dstrect.top, dstrect.GetWidth(), dstrect.GetHeight());
|
|
|
|
s_ColorCopyProgram.Bind();
|
|
|
|
glUniform4f(s_ColorCopyPositionUniform,
|
|
|
|
float(srcrect.left),
|
|
|
|
float(srcrect.top),
|
|
|
|
float(srcrect.GetWidth()),
|
|
|
|
float(srcrect.GetHeight()));
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
FramebufferManager::SetFramebuffer(0);
|
|
|
|
g_renderer->RestoreAPIState();
|
2015-06-13 13:51:58 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
|
2012-08-10 11:13:51 +00:00
|
|
|
unsigned int expanded_width, unsigned int level)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2015-01-11 21:47:40 +00:00
|
|
|
if (level >= config.levels)
|
|
|
|
PanicAlert("Texture only has %d levels, can't update level %d", config.levels, level);
|
|
|
|
if (width != std::max(1u, config.width >> level) || height != std::max(1u, config.height >> level))
|
|
|
|
PanicAlert("size of level %d must be %dx%d, but %dx%d requested",
|
|
|
|
level, std::max(1u, config.width >> level), std::max(1u, config.height >> level), width, height);
|
|
|
|
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2015-01-17 08:46:31 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2015-01-17 08:46:31 +00:00
|
|
|
if (expanded_width != width)
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, expanded_width);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-01-17 08:46:31 +00:00
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, width, height, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp);
|
|
|
|
|
|
|
|
if (expanded_width != width)
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2009-05-09 07:55:30 +00:00
|
|
|
|
2014-01-21 09:34:48 +00:00
|
|
|
TextureCache::SetStage();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat,
|
2014-03-23 20:44:23 +00:00
|
|
|
PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
|
2011-02-26 23:41:02 +00:00
|
|
|
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
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2009-05-09 07:55:30 +00:00
|
|
|
// Make sure to resolve anything we need to read from.
|
2014-03-23 20:44:23 +00:00
|
|
|
const GLuint read_texture = (srcFormat == PEControl::Z24) ?
|
2011-02-26 23:41:02 +00:00
|
|
|
FramebufferManager::ResolveAndGetDepthTarget(srcRect) :
|
|
|
|
FramebufferManager::ResolveAndGetRenderTarget(srcRect);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
FramebufferManager::SetFramebuffer(framebuffer);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
OpenGL_BindAttributelessVAO();
|
2014-12-11 07:19:18 +00:00
|
|
|
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2015-01-22 04:37:12 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, read_texture);
|
2015-05-28 22:53:07 +00:00
|
|
|
if (scaleByHalf)
|
|
|
|
g_sampler_cache->BindLinearSampler(9);
|
|
|
|
else
|
|
|
|
g_sampler_cache->BindNearestSampler(9);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
glViewport(0, 0, config.width, config.height);
|
2010-07-12 19:30:25 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
GLuint uniform_location;
|
|
|
|
if (srcFormat == PEControl::Z24)
|
|
|
|
{
|
|
|
|
s_DepthMatrixProgram.Bind();
|
|
|
|
if (s_DepthCbufid != cbufid)
|
|
|
|
glUniform4fv(s_DepthMatrixUniform, 5, colmat);
|
|
|
|
s_DepthCbufid = cbufid;
|
|
|
|
uniform_location = s_DepthCopyPositionUniform;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_ColorMatrixProgram.Bind();
|
|
|
|
if (s_ColorCbufid != cbufid)
|
|
|
|
glUniform4fv(s_ColorMatrixUniform, 7, colmat);
|
|
|
|
s_ColorCbufid = cbufid;
|
2015-06-30 01:19:19 +00:00
|
|
|
uniform_location = s_ColorMatrixPositionUniform;
|
2015-01-22 04:37:12 +00:00
|
|
|
}
|
2009-03-07 09:29:25 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
TargetRectangle R = g_renderer->ConvertEFBRectangle(srcRect);
|
|
|
|
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 20:28:55 +00:00
|
|
|
|
2015-01-22 04:37:12 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-02-21 11:08:47 +00:00
|
|
|
if (!g_ActiveConfig.bSkipEFBCopyToRam)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2011-12-30 00:00:34 +00:00
|
|
|
int encoded_size = TextureConverter::EncodeToRamFromTexture(
|
2015-01-26 23:33:23 +00:00
|
|
|
dstAddr,
|
2010-07-12 19:30:25 +00:00
|
|
|
read_texture,
|
2014-03-23 20:44:23 +00:00
|
|
|
srcFormat == PEControl::Z24,
|
2013-10-29 05:23:17 +00:00
|
|
|
isIntensity,
|
|
|
|
dstFormat,
|
|
|
|
scaleByHalf,
|
2015-07-07 13:09:25 +00:00
|
|
|
srcRect,
|
|
|
|
copyMipMapStrideChannels * 32);
|
2011-12-30 00:00:34 +00:00
|
|
|
|
2015-01-26 23:33:23 +00:00
|
|
|
u8* dst = Memory::GetPointer(dstAddr);
|
2013-01-29 22:40:15 +00:00
|
|
|
u64 const new_hash = GetHash64(dst,encoded_size,g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2011-12-30 00:00:34 +00:00
|
|
|
|
2015-01-10 12:47:52 +00:00
|
|
|
size_in_bytes = (u32)encoded_size;
|
|
|
|
|
2015-01-26 23:33:23 +00:00
|
|
|
TextureCache::MakeRangeDynamic(dstAddr, encoded_size);
|
2012-06-20 14:43:13 +00:00
|
|
|
|
2013-01-29 22:40:15 +00:00
|
|
|
hash = new_hash;
|
2010-07-12 19:30:25 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2013-04-15 20:28:55 +00:00
|
|
|
FramebufferManager::SetFramebuffer(0);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2013-01-28 17:16:03 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 23:54:38 +00:00
|
|
|
TextureCache::TextureCache()
|
2014-11-03 23:53:14 +00:00
|
|
|
{
|
|
|
|
CompileShaders();
|
|
|
|
|
|
|
|
s_ActiveTexture = -1;
|
|
|
|
for (auto& gtex : s_Textures)
|
|
|
|
gtex = -1;
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
|
|
|
s_palette_stream_buffer = StreamBuffer::Create(GL_TEXTURE_BUFFER, 1024*1024);
|
|
|
|
glGenTextures(1, &s_palette_resolv_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, s_palette_resolv_texture);
|
|
|
|
glTexBuffer(GL_TEXTURE_BUFFER, GL_R16UI, s_palette_stream_buffer->m_buffer);
|
|
|
|
}
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TextureCache::~TextureCache()
|
|
|
|
{
|
|
|
|
DeleteShaders();
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
|
|
|
delete s_palette_stream_buffer;
|
|
|
|
s_palette_stream_buffer = nullptr;
|
|
|
|
glDeleteTextures(1, &s_palette_resolv_texture);
|
|
|
|
}
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 23:54:38 +00:00
|
|
|
{
|
2015-06-30 01:19:19 +00:00
|
|
|
const char *pColorCopyProg =
|
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
|
|
|
"in vec3 f_uv0;\n"
|
|
|
|
"out vec4 ocol0;\n"
|
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
|
|
|
" vec4 texcol = texture(samp9, f_uv0);\n"
|
|
|
|
" ocol0 = texcol;\n"
|
|
|
|
"}\n";
|
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
const char *pColorMatrixProg =
|
2014-10-09 22:06:29 +00:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"uniform vec4 colmat[7];\n"
|
2014-10-26 23:52:56 +00:00
|
|
|
"in vec3 f_uv0;\n"
|
2013-11-25 00:06:29 +00:00
|
|
|
"out vec4 ocol0;\n"
|
2013-01-11 14:24:43 +00:00
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2014-10-26 23:52:56 +00:00
|
|
|
" vec4 texcol = texture(samp9, f_uv0);\n"
|
2013-02-13 17:01:06 +00:00
|
|
|
" texcol = round(texcol * colmat[5]) * colmat[6];\n"
|
2013-02-15 12:13:45 +00:00
|
|
|
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + 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 =
|
2014-10-09 22:06:29 +00:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"uniform vec4 colmat[5];\n"
|
2014-10-26 23:52:56 +00:00
|
|
|
"in vec3 f_uv0;\n"
|
2013-11-25 00:06:29 +00:00
|
|
|
"out vec4 ocol0;\n"
|
2013-01-11 14:24:43 +00:00
|
|
|
"\n"
|
|
|
|
"void main(){\n"
|
2014-11-08 15:19:15 +00:00
|
|
|
" vec4 texcol = texture(samp9, vec3(f_uv0.xy, %s));\n"
|
2015-05-06 20:42:06 +00:00
|
|
|
" int depth = clamp(int(texcol.x * 16777216.0), 0, 0xFFFFFF);\n"
|
2014-08-17 07:14:29 +00:00
|
|
|
|
2015-05-04 21:28:30 +00:00
|
|
|
// Convert to Z24 format
|
|
|
|
" ivec4 workspace;\n"
|
|
|
|
" workspace.r = (depth >> 16) & 255;\n"
|
|
|
|
" workspace.g = (depth >> 8) & 255;\n"
|
|
|
|
" workspace.b = depth & 255;\n"
|
2014-08-17 07:14:29 +00:00
|
|
|
|
2015-05-04 21:28:30 +00:00
|
|
|
// Convert to Z4 format
|
|
|
|
" workspace.a = (depth >> 16) & 0xF0;\n"
|
2014-08-17 07:14:29 +00:00
|
|
|
|
2015-05-04 21:28:30 +00:00
|
|
|
// Normalize components to [0.0..1.0]
|
|
|
|
" texcol = vec4(workspace) / 255.0;\n"
|
2014-08-17 07:14:29 +00:00
|
|
|
|
|
|
|
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + colmat[4];\n"
|
2013-01-12 15:29:16 +00:00
|
|
|
"}\n";
|
|
|
|
|
2014-11-08 15:19:15 +00:00
|
|
|
const char *VProgram =
|
|
|
|
"out vec3 %s_uv0;\n"
|
2014-10-09 22:06:29 +00:00
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
2013-11-25 11:19:34 +00:00
|
|
|
"uniform vec4 copy_position;\n" // left, top, right, bottom
|
2013-01-11 14:41:42 +00:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2013-11-25 11:19:34 +00:00
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
2014-11-08 15:19:15 +00:00
|
|
|
" %s_uv0 = vec3(mix(copy_position.xy, copy_position.zw, rawpos) / vec2(textureSize(samp9, 0).xy), 0.0);\n"
|
2014-10-29 12:47:24 +00:00
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
2013-01-11 14:41:42 +00:00
|
|
|
"}\n";
|
2013-04-15 20:28:55 +00:00
|
|
|
|
2014-12-29 10:18:14 +00:00
|
|
|
const char *GProgram = g_ActiveConfig.iStereoMode > 0 ?
|
2014-10-26 23:52:56 +00:00
|
|
|
"layout(triangles) in;\n"
|
2014-10-28 17:50:09 +00:00
|
|
|
"layout(triangle_strip, max_vertices = 6) out;\n"
|
2014-11-16 01:15:43 +00:00
|
|
|
"in vec3 v_uv0[3];\n"
|
2014-10-26 23:52:56 +00: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 08:21:19 +00:00
|
|
|
" for (int i = 0; i < 3; ++i) {\n"
|
2014-11-08 15:19:15 +00:00
|
|
|
" f_uv0 = vec3(v_uv0[i].xy, layer);\n"
|
2014-10-26 23:52:56 +00:00
|
|
|
" gl_Position = gl_in[i].gl_Position;\n"
|
|
|
|
" gl_Layer = layer;\n"
|
|
|
|
" EmitVertex();\n"
|
|
|
|
" }\n"
|
|
|
|
" EndPrimitive();\n"
|
|
|
|
" }\n"
|
2014-10-29 12:47:24 +00:00
|
|
|
"}\n" : nullptr;
|
2014-10-26 23:52:56 +00:00
|
|
|
|
2014-11-08 15:19:15 +00:00
|
|
|
const char* prefix = (GProgram == nullptr) ? "f" : "v";
|
2014-12-24 22:06:44 +00:00
|
|
|
const char* depth_layer = (g_ActiveConfig.bStereoEFBMonoDepth) ? "0.0" : "f_uv0.z";
|
2014-11-08 15:19:15 +00:00
|
|
|
|
2015-06-30 01:19:19 +00:00
|
|
|
ProgramShaderCache::CompileShader(s_ColorCopyProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), pColorCopyProg, GProgram);
|
2014-11-08 15:19:15 +00:00
|
|
|
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 20:28:55 +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-04-15 20:28:55 +00:00
|
|
|
|
2015-06-30 01:19:19 +00:00
|
|
|
s_ColorCopyPositionUniform = glGetUniformLocation(s_ColorCopyProgram.glprogid, "copy_position");
|
|
|
|
s_ColorMatrixPositionUniform = glGetUniformLocation(s_ColorMatrixProgram.glprogid, "copy_position");
|
2013-11-25 11:19:34 +00:00
|
|
|
s_DepthCopyPositionUniform = glGetUniformLocation(s_DepthMatrixProgram.glprogid, "copy_position");
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
std::string palette_shader =
|
|
|
|
R"GLSL(
|
|
|
|
uniform int texture_buffer_offset;
|
|
|
|
uniform float multiplier;
|
|
|
|
SAMPLER_BINDING(9) uniform sampler2DArray samp9;
|
|
|
|
SAMPLER_BINDING(10) uniform usamplerBuffer samp10;
|
|
|
|
|
|
|
|
in vec3 f_uv0;
|
|
|
|
out vec4 ocol0;
|
|
|
|
|
|
|
|
int Convert3To8(int v)
|
|
|
|
{
|
|
|
|
// Swizzle bits: 00000123 -> 12312312
|
|
|
|
return (v << 5) | (v << 2) | (v >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Convert4To8(int v)
|
|
|
|
{
|
|
|
|
// Swizzle bits: 00001234 -> 12341234
|
|
|
|
return (v << 4) | v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Convert5To8(int v)
|
|
|
|
{
|
|
|
|
// Swizzle bits: 00012345 -> 12345123
|
|
|
|
return (v << 3) | (v >> 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Convert6To8(int v)
|
|
|
|
{
|
|
|
|
// Swizzle bits: 00123456 -> 12345612
|
|
|
|
return (v << 2) | (v >> 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 DecodePixel_RGB5A3(int val)
|
|
|
|
{
|
|
|
|
int r,g,b,a;
|
|
|
|
if ((val&0x8000) > 0)
|
|
|
|
{
|
|
|
|
r=Convert5To8((val>>10) & 0x1f);
|
|
|
|
g=Convert5To8((val>>5 ) & 0x1f);
|
|
|
|
b=Convert5To8((val ) & 0x1f);
|
|
|
|
a=0xFF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a=Convert3To8((val>>12) & 0x7);
|
|
|
|
r=Convert4To8((val>>8 ) & 0xf);
|
|
|
|
g=Convert4To8((val>>4 ) & 0xf);
|
|
|
|
b=Convert4To8((val ) & 0xf);
|
|
|
|
}
|
|
|
|
return float4(r, g, b, a) / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 DecodePixel_RGB565(int val)
|
|
|
|
{
|
|
|
|
int r, g, b, a;
|
|
|
|
r = Convert5To8((val >> 11) & 0x1f);
|
|
|
|
g = Convert6To8((val >> 5) & 0x3f);
|
|
|
|
b = Convert5To8((val) & 0x1f);
|
|
|
|
a = 0xFF;
|
|
|
|
return float4(r, g, b, a) / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 DecodePixel_IA8(int val)
|
|
|
|
{
|
|
|
|
int i = val & 0xFF;
|
|
|
|
int a = val >> 8;
|
|
|
|
return float4(i, i, i, a) / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
int src = int(round(texture(samp9, f_uv0).r * multiplier));
|
|
|
|
src = int(texelFetch(samp10, src + texture_buffer_offset).r);
|
|
|
|
src = ((src << 8) & 0xFF00) | (src >> 8);
|
|
|
|
ocol0 = DECODE(src);
|
|
|
|
}
|
|
|
|
)GLSL";
|
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
|
|
|
ProgramShaderCache::CompileShader(
|
|
|
|
s_palette_pixel_shader[GX_TL_IA8],
|
|
|
|
StringFromFormat(VProgram, prefix, prefix).c_str(),
|
|
|
|
("#define DECODE DecodePixel_IA8" + palette_shader).c_str(),
|
|
|
|
GProgram);
|
|
|
|
s_palette_buffer_offset_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "texture_buffer_offset");
|
|
|
|
s_palette_multiplier_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "multiplier");
|
|
|
|
s_palette_copy_position_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "copy_position");
|
|
|
|
|
|
|
|
ProgramShaderCache::CompileShader(
|
|
|
|
s_palette_pixel_shader[GX_TL_RGB565],
|
|
|
|
StringFromFormat(VProgram, prefix, prefix).c_str(),
|
|
|
|
("#define DECODE DecodePixel_RGB565" + palette_shader).c_str(),
|
|
|
|
GProgram);
|
|
|
|
s_palette_buffer_offset_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "texture_buffer_offset");
|
|
|
|
s_palette_multiplier_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "multiplier");
|
|
|
|
s_palette_copy_position_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "copy_position");
|
|
|
|
|
|
|
|
ProgramShaderCache::CompileShader(
|
|
|
|
s_palette_pixel_shader[GX_TL_RGB5A3],
|
|
|
|
StringFromFormat(VProgram, prefix, prefix).c_str(),
|
|
|
|
("#define DECODE DecodePixel_RGB5A3" + palette_shader).c_str(),
|
|
|
|
GProgram);
|
|
|
|
s_palette_buffer_offset_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "texture_buffer_offset");
|
|
|
|
s_palette_multiplier_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "multiplier");
|
|
|
|
s_palette_copy_position_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "copy_position");
|
|
|
|
}
|
2012-12-07 23:54:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 23:53:14 +00:00
|
|
|
void TextureCache::DeleteShaders()
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2013-01-11 14:24:43 +00:00
|
|
|
s_ColorMatrixProgram.Destroy();
|
|
|
|
s_DepthMatrixProgram.Destroy();
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
for (auto& shader : s_palette_pixel_shader)
|
|
|
|
shader.Destroy();
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 23:12:27 +00:00
|
|
|
void TextureCache::ConvertTexture(TCacheEntryBase* _entry, TCacheEntryBase* _unconverted, void* palette, TlutFormat format)
|
2015-01-26 23:33:23 +00:00
|
|
|
{
|
2015-02-20 23:12:27 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
|
|
|
|
TCacheEntry* entry = (TCacheEntry*) _entry;
|
|
|
|
TCacheEntry* unconverted = (TCacheEntry*) _unconverted;
|
|
|
|
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2015-02-20 23:12:27 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, unconverted->texture);
|
2015-05-28 22:53:07 +00:00
|
|
|
g_sampler_cache->BindNearestSampler(9);
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
FramebufferManager::SetFramebuffer(entry->framebuffer);
|
|
|
|
glViewport(0, 0, entry->config.width, entry->config.height);
|
|
|
|
s_palette_pixel_shader[format].Bind();
|
|
|
|
|
|
|
|
int size = unconverted->format == 0 ? 32 : 512;
|
|
|
|
auto buffer = s_palette_stream_buffer->Map(size);
|
|
|
|
memcpy(buffer.first, palette, size);
|
|
|
|
s_palette_stream_buffer->Unmap(size);
|
|
|
|
glUniform1i(s_palette_buffer_offset_uniform[format], buffer.second / 2);
|
|
|
|
glUniform1f(s_palette_multiplier_uniform[format], unconverted->format == 0 ? 15.0f : 255.0f);
|
|
|
|
glUniform4f(s_palette_copy_position_uniform[format], 0.0f, 0.0f, (float)unconverted->config.width, (float)unconverted->config.height);
|
|
|
|
|
2015-05-25 13:14:20 +00:00
|
|
|
glActiveTexture(GL_TEXTURE10);
|
2015-02-20 23:12:27 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, s_palette_resolv_texture);
|
2015-05-28 22:53:07 +00:00
|
|
|
g_sampler_cache->BindNearestSampler(10);
|
2015-02-20 23:12:27 +00:00
|
|
|
|
|
|
|
OpenGL_BindAttributelessVAO();
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
FramebufferManager::SetFramebuffer(0);
|
|
|
|
g_renderer->RestoreAPIState();
|
2015-01-26 23:33:23 +00:00
|
|
|
}
|
|
|
|
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|