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
|
|
|
|
2015-11-30 07:16:26 +00:00
|
|
|
#include <algorithm>
|
2010-04-19 03:06:18 +00:00
|
|
|
#include <cmath>
|
2017-02-01 04:29:29 +00:00
|
|
|
#include <cstring>
|
2014-02-19 11:14:09 +00:00
|
|
|
#include <fstream>
|
2015-12-21 15:15:17 +00:00
|
|
|
#include <memory>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <vector>
|
2008-12-26 12:24:15 +00:00
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
#include "Common/Assert.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLInterfaceBase.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/OGL/FramebufferManager.h"
|
2016-11-27 08:15:02 +00:00
|
|
|
#include "VideoBackends/OGL/GPUTimer.h"
|
2017-04-23 04:44:34 +00:00
|
|
|
#include "VideoBackends/OGL/OGLTexture.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"
|
2017-04-23 04:44:34 +00:00
|
|
|
#include "VideoBackends/OGL/TextureCache.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/TextureConverter.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/ImageWrite.h"
|
2016-11-27 08:15:00 +00:00
|
|
|
#include "VideoCommon/TextureConversionShader.h"
|
2017-08-04 15:56:24 +00:00
|
|
|
#include "VideoCommon/TextureConverterShaderGen.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/TextureDecoder.h"
|
2017-08-04 15:56:24 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-05-07 07:43:56 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2017-08-04 15:56:24 +00:00
|
|
|
constexpr const char* vertex_program =
|
|
|
|
"out vec3 %c_uv0;\n"
|
|
|
|
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
|
|
|
|
"uniform vec4 copy_position;\n" // left, top, right, bottom
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
|
|
|
" %c_uv0 = vec3(mix(copy_position.xy, copy_position.zw, rawpos) / vec2(textureSize(samp9, "
|
|
|
|
"0).xy), 0.0);\n"
|
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
constexpr const char* geometry_program = "layout(triangles) in;\n"
|
|
|
|
"layout(triangle_strip, max_vertices = 6) out;\n"
|
|
|
|
"in vec3 v_uv0[3];\n"
|
|
|
|
"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"
|
|
|
|
" for (int i = 0; i < 3; ++i) {\n"
|
|
|
|
" f_uv0 = vec3(v_uv0[i].xy, layer);\n"
|
|
|
|
" gl_Position = gl_in[i].gl_Position;\n"
|
|
|
|
" gl_Layer = layer;\n"
|
|
|
|
" EmitVertex();\n"
|
|
|
|
" }\n"
|
|
|
|
" EndPrimitive();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
|
2016-11-27 08:15:02 +00:00
|
|
|
//#define TIME_TEXTURE_DECODING 1
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
void TextureCache::CopyEFB(u8* dst, const EFBCopyParams& params, u32 native_width,
|
2017-04-04 13:55:36 +00:00
|
|
|
u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride,
|
2017-07-30 19:45:55 +00:00
|
|
|
const EFBRectangle& src_rect, bool scale_by_half)
|
2015-10-29 16:48:35 +00:00
|
|
|
{
|
2017-07-30 19:45:55 +00:00
|
|
|
TextureConverter::EncodeToRamFromTexture(dst, params, native_width, bytes_per_row, num_blocks_y,
|
|
|
|
memory_stride, src_rect, scale_by_half);
|
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
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
CompileShaders();
|
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
2016-11-27 08:15:00 +00:00
|
|
|
s32 buffer_size_mb = (g_ActiveConfig.backend_info.bSupportsGPUTextureDecoding ? 32 : 1);
|
|
|
|
s32 buffer_size = buffer_size_mb * 1024 * 1024;
|
2016-06-24 08:43:46 +00:00
|
|
|
s32 max_buffer_size = 0;
|
|
|
|
|
2016-11-27 08:15:00 +00:00
|
|
|
// The minimum MAX_TEXTURE_BUFFER_SIZE that the spec mandates is 65KB, we are asking for a 1MB
|
|
|
|
// buffer here. This buffer is also used as storage for undecoded textures when compute shader
|
|
|
|
// texture decoding is enabled, in which case the requested size is 32MB.
|
2016-06-24 08:43:46 +00:00
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &max_buffer_size);
|
2016-11-27 08:15:00 +00:00
|
|
|
|
|
|
|
// Clamp the buffer size to the maximum size that the driver supports.
|
2016-06-24 08:43:46 +00:00
|
|
|
buffer_size = std::min(buffer_size, max_buffer_size);
|
|
|
|
|
2017-09-17 05:54:03 +00:00
|
|
|
m_palette_stream_buffer = StreamBuffer::Create(GL_TEXTURE_BUFFER, buffer_size);
|
|
|
|
glGenTextures(1, &m_palette_resolv_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_palette_resolv_texture);
|
|
|
|
glTexBuffer(GL_TEXTURE_BUFFER, GL_R16UI, m_palette_stream_buffer->m_buffer);
|
2016-11-27 08:15:00 +00:00
|
|
|
|
2017-10-22 05:20:54 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGPUTextureDecoding)
|
|
|
|
CreateTextureDecodingResources();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureCache::~TextureCache()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DeleteShaders();
|
2017-10-22 05:20:54 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGPUTextureDecoding)
|
|
|
|
DestroyTextureDecodingResources();
|
2015-02-20 23:12:27 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
2017-09-17 05:54:03 +00:00
|
|
|
glDeleteTextures(1, &m_palette_resolv_texture);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
TextureCache* TextureCache::GetInstance()
|
2014-11-03 23:53:14 +00:00
|
|
|
{
|
2017-04-23 04:44:34 +00:00
|
|
|
return static_cast<TextureCache*>(g_texture_cache.get());
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
const SHADER& TextureCache::GetColorCopyProgram() const
|
2014-11-03 23:53:14 +00:00
|
|
|
{
|
2017-04-23 04:44:34 +00:00
|
|
|
return m_colorCopyProgram;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint TextureCache::GetColorCopyPositionUniform() const
|
|
|
|
{
|
|
|
|
return m_colorCopyPositionUniform;
|
2014-11-03 23:53:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 05:54:03 +00:00
|
|
|
bool TextureCache::CompilePaletteShader(TLUTFormat tlutfmt, const std::string& vcode,
|
|
|
|
const std::string& pcode, const std::string& gcode)
|
2017-07-30 19:45:55 +00:00
|
|
|
{
|
|
|
|
_assert_(IsValidTLUTFormat(tlutfmt));
|
2017-09-17 05:54:03 +00:00
|
|
|
PaletteShader& shader = m_palette_shaders[static_cast<int>(tlutfmt)];
|
2017-07-30 19:45:55 +00:00
|
|
|
|
|
|
|
if (!ProgramShaderCache::CompileShader(shader.shader, vcode, pcode, gcode))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
shader.buffer_offset_uniform =
|
|
|
|
glGetUniformLocation(shader.shader.glprogid, "texture_buffer_offset");
|
|
|
|
shader.multiplier_uniform = glGetUniformLocation(shader.shader.glprogid, "multiplier");
|
|
|
|
shader.copy_position_uniform = glGetUniformLocation(shader.shader.glprogid, "copy_position");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:56:01 +00:00
|
|
|
bool TextureCache::CompileShaders()
|
2012-12-07 23:54:38 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
constexpr const char* color_copy_program = "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";
|
|
|
|
|
2017-08-04 15:56:24 +00:00
|
|
|
std::string geo_program = "";
|
|
|
|
char prefix = 'f';
|
|
|
|
if (g_ActiveConfig.stereo_mode != StereoMode::Off)
|
|
|
|
{
|
|
|
|
geo_program = geometry_program;
|
|
|
|
prefix = 'v';
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
if (!ProgramShaderCache::CompileShader(m_colorCopyProgram,
|
2016-08-13 12:56:01 +00:00
|
|
|
StringFromFormat(vertex_program, prefix, prefix),
|
2017-08-04 15:56:24 +00:00
|
|
|
color_copy_program, geo_program))
|
2016-08-13 12:56:01 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
m_colorCopyPositionUniform = glGetUniformLocation(m_colorCopyProgram.glprogid, "copy_position");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
std::string palette_shader =
|
|
|
|
R"GLSL(
|
2015-02-20 23:12:27 +00:00
|
|
|
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";
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
{
|
2017-07-30 19:45:55 +00:00
|
|
|
if (!CompilePaletteShader(TLUTFormat::IA8, StringFromFormat(vertex_program, prefix, prefix),
|
|
|
|
"#define DECODE DecodePixel_IA8" + palette_shader, geo_program))
|
2016-08-13 12:56:01 +00:00
|
|
|
return false;
|
2017-07-30 19:45:55 +00:00
|
|
|
|
|
|
|
if (!CompilePaletteShader(TLUTFormat::RGB565, StringFromFormat(vertex_program, prefix, prefix),
|
|
|
|
"#define DECODE DecodePixel_RGB565" + palette_shader, geo_program))
|
2016-08-13 12:56:01 +00:00
|
|
|
return false;
|
2017-07-30 19:45:55 +00:00
|
|
|
|
|
|
|
if (!CompilePaletteShader(TLUTFormat::RGB5A3, StringFromFormat(vertex_program, prefix, prefix),
|
|
|
|
"#define DECODE DecodePixel_RGB5A3" + palette_shader, geo_program))
|
2016-08-13 12:56:01 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-08-13 12:56:01 +00:00
|
|
|
|
|
|
|
return true;
|
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
|
|
|
{
|
2017-08-04 15:56:24 +00:00
|
|
|
for (auto& it : m_efb_copy_programs)
|
|
|
|
it.second.shader.Destroy();
|
|
|
|
m_efb_copy_programs.clear();
|
2015-02-20 23:12:27 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
2017-09-17 05:54:03 +00:00
|
|
|
for (auto& shader : m_palette_shaders)
|
2017-07-30 19:45:55 +00:00
|
|
|
shader.shader.Destroy();
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
void TextureCache::ConvertTexture(TCacheEntry* destination, TCacheEntry* source,
|
|
|
|
const void* palette, TLUTFormat tlutfmt)
|
2015-01-26 23:33:23 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsPaletteConversion)
|
|
|
|
return;
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
_assert_(IsValidTLUTFormat(tlutfmt));
|
2017-09-17 05:54:03 +00:00
|
|
|
const PaletteShader& palette_shader = m_palette_shaders[static_cast<int>(tlutfmt)];
|
2017-07-30 19:45:55 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
OGLTexture* source_texture = static_cast<OGLTexture*>(source->texture.get());
|
|
|
|
OGLTexture* destination_texture = static_cast<OGLTexture*>(destination->texture.get());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2017-04-23 04:44:34 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, source_texture->GetRawTexIdentifier());
|
2016-06-24 08:43:46 +00:00
|
|
|
g_sampler_cache->BindNearestSampler(9);
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
FramebufferManager::SetFramebuffer(destination_texture->GetFramebuffer());
|
|
|
|
glViewport(0, 0, destination->GetWidth(), destination->GetHeight());
|
2017-07-30 19:45:55 +00:00
|
|
|
palette_shader.shader.Bind();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// C14 textures are currently unsupported
|
2017-07-30 19:45:55 +00:00
|
|
|
int size = source->format == TextureFormat::I4 ? 32 : 512;
|
2017-09-17 05:54:03 +00:00
|
|
|
auto buffer = m_palette_stream_buffer->Map(size);
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(buffer.first, palette, size);
|
2017-09-17 05:54:03 +00:00
|
|
|
m_palette_stream_buffer->Unmap(size);
|
2017-07-30 19:45:55 +00:00
|
|
|
glUniform1i(palette_shader.buffer_offset_uniform, buffer.second / 2);
|
|
|
|
glUniform1f(palette_shader.multiplier_uniform,
|
|
|
|
source->format == TextureFormat::I4 ? 15.0f : 255.0f);
|
|
|
|
glUniform4f(palette_shader.copy_position_uniform, 0.0f, 0.0f,
|
2017-04-23 04:44:34 +00:00
|
|
|
static_cast<float>(source->GetWidth()), static_cast<float>(source->GetHeight()));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE10);
|
2017-09-17 05:54:03 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_palette_resolv_texture);
|
2016-06-24 08:43:46 +00:00
|
|
|
g_sampler_cache->BindNearestSampler(10);
|
|
|
|
|
2018-01-20 14:59:10 +00:00
|
|
|
ProgramShaderCache::BindVertexFormat(nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
FramebufferManager::SetFramebuffer(0);
|
|
|
|
g_renderer->RestoreAPIState();
|
2015-01-26 23:33:23 +00:00
|
|
|
}
|
2016-11-27 08:15:00 +00:00
|
|
|
|
|
|
|
static const std::string decoding_vertex_shader = R"(
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);
|
|
|
|
gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
2017-09-17 05:54:03 +00:00
|
|
|
void TextureCache::CreateTextureDecodingResources()
|
2016-11-27 08:15:00 +00:00
|
|
|
{
|
2017-11-25 10:13:22 +00:00
|
|
|
static const GLenum gl_view_types[TextureConversionShaderTiled::BUFFER_FORMAT_COUNT] = {
|
2017-09-03 02:30:34 +00:00
|
|
|
GL_R8UI, // BUFFER_FORMAT_R8_UINT
|
|
|
|
GL_R16UI, // BUFFER_FORMAT_R16_UINT
|
|
|
|
GL_RG32UI, // BUFFER_FORMAT_R32G32_UINT
|
|
|
|
GL_RGBA8UI, // BUFFER_FORMAT_RGBA8_UINT
|
2016-11-27 08:15:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-25 10:13:22 +00:00
|
|
|
glGenTextures(TextureConversionShaderTiled::BUFFER_FORMAT_COUNT,
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_buffer_views.data());
|
2017-11-25 10:13:22 +00:00
|
|
|
for (size_t i = 0; i < TextureConversionShaderTiled::BUFFER_FORMAT_COUNT; i++)
|
2016-11-27 08:15:00 +00:00
|
|
|
{
|
2017-09-17 05:54:03 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_texture_decoding_buffer_views[i]);
|
|
|
|
glTexBuffer(GL_TEXTURE_BUFFER, gl_view_types[i], m_palette_stream_buffer->m_buffer);
|
2016-11-27 08:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 05:54:03 +00:00
|
|
|
void TextureCache::DestroyTextureDecodingResources()
|
2016-11-27 08:15:00 +00:00
|
|
|
{
|
2017-11-25 10:13:22 +00:00
|
|
|
glDeleteTextures(TextureConversionShaderTiled::BUFFER_FORMAT_COUNT,
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_buffer_views.data());
|
|
|
|
m_texture_decoding_buffer_views.fill(0);
|
|
|
|
m_texture_decoding_program_info.clear();
|
2016-11-27 08:15:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
bool TextureCache::SupportsGPUTextureDecode(TextureFormat format, TLUTFormat palette_format)
|
2016-11-27 08:15:00 +00:00
|
|
|
{
|
|
|
|
auto key = std::make_pair(static_cast<u32>(format), static_cast<u32>(palette_format));
|
2017-09-17 05:54:03 +00:00
|
|
|
auto iter = m_texture_decoding_program_info.find(key);
|
|
|
|
if (iter != m_texture_decoding_program_info.end())
|
2016-11-27 08:15:00 +00:00
|
|
|
return iter->second.valid;
|
|
|
|
|
|
|
|
TextureDecodingProgramInfo info;
|
2017-11-25 10:13:22 +00:00
|
|
|
info.base_info = TextureConversionShaderTiled::GetDecodingShaderInfo(format);
|
2016-11-27 08:15:00 +00:00
|
|
|
if (!info.base_info)
|
|
|
|
{
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_program_info.emplace(key, info);
|
2016-11-27 08:15:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string shader_source =
|
2017-11-25 10:13:22 +00:00
|
|
|
TextureConversionShaderTiled::GenerateDecodingShader(format, palette_format, APIType::OpenGL);
|
2016-11-27 08:15:00 +00:00
|
|
|
if (shader_source.empty())
|
|
|
|
{
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_program_info.emplace(key, info);
|
2016-11-27 08:15:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ProgramShaderCache::CompileComputeShader(info.program, shader_source))
|
|
|
|
{
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_program_info.emplace(key, info);
|
2016-11-27 08:15:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.uniform_dst_size = glGetUniformLocation(info.program.glprogid, "u_dst_size");
|
|
|
|
info.uniform_src_size = glGetUniformLocation(info.program.glprogid, "u_src_size");
|
|
|
|
info.uniform_src_offset = glGetUniformLocation(info.program.glprogid, "u_src_offset");
|
|
|
|
info.uniform_src_row_stride = glGetUniformLocation(info.program.glprogid, "u_src_row_stride");
|
|
|
|
info.uniform_palette_offset = glGetUniformLocation(info.program.glprogid, "u_palette_offset");
|
|
|
|
info.valid = true;
|
2017-09-17 05:54:03 +00:00
|
|
|
m_texture_decoding_program_info.emplace(key, info);
|
2016-11-27 08:15:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
void TextureCache::DecodeTextureOnGPU(TCacheEntry* entry, u32 dst_level, const u8* data,
|
2016-11-27 08:15:00 +00:00
|
|
|
size_t data_size, TextureFormat format, u32 width, u32 height,
|
|
|
|
u32 aligned_width, u32 aligned_height, u32 row_stride,
|
2017-07-30 19:45:55 +00:00
|
|
|
const u8* palette, TLUTFormat palette_format)
|
2016-11-27 08:15:00 +00:00
|
|
|
{
|
|
|
|
auto key = std::make_pair(static_cast<u32>(format), static_cast<u32>(palette_format));
|
2017-09-17 05:54:03 +00:00
|
|
|
auto iter = m_texture_decoding_program_info.find(key);
|
|
|
|
if (iter == m_texture_decoding_program_info.end())
|
2016-11-27 08:15:00 +00:00
|
|
|
return;
|
|
|
|
|
2016-11-27 08:15:02 +00:00
|
|
|
#ifdef TIME_TEXTURE_DECODING
|
|
|
|
GPUTimer timer;
|
|
|
|
#endif
|
|
|
|
|
2016-11-27 08:15:00 +00:00
|
|
|
// Copy to GPU-visible buffer, aligned to the data type.
|
|
|
|
auto info = iter->second;
|
|
|
|
u32 bytes_per_buffer_elem =
|
2017-11-25 10:13:22 +00:00
|
|
|
TextureConversionShaderTiled::GetBytesPerBufferElement(info.base_info->buffer_format);
|
2016-11-27 08:15:00 +00:00
|
|
|
|
|
|
|
// Only copy palette if it is required.
|
|
|
|
bool has_palette = info.base_info->palette_size > 0;
|
|
|
|
u32 total_upload_size = static_cast<u32>(data_size);
|
|
|
|
u32 palette_offset = total_upload_size;
|
|
|
|
if (has_palette)
|
|
|
|
{
|
|
|
|
// Align to u16.
|
|
|
|
if ((total_upload_size % sizeof(u16)) != 0)
|
|
|
|
{
|
|
|
|
total_upload_size++;
|
|
|
|
palette_offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_upload_size += info.base_info->palette_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate space in stream buffer, and copy texture + palette across.
|
2017-09-17 05:54:03 +00:00
|
|
|
auto buffer = m_palette_stream_buffer->Map(total_upload_size, bytes_per_buffer_elem);
|
2016-11-27 08:15:00 +00:00
|
|
|
memcpy(buffer.first, data, data_size);
|
|
|
|
if (has_palette)
|
|
|
|
memcpy(buffer.first + palette_offset, palette, info.base_info->palette_size);
|
2017-09-17 05:54:03 +00:00
|
|
|
m_palette_stream_buffer->Unmap(total_upload_size);
|
2016-11-27 08:15:00 +00:00
|
|
|
|
|
|
|
info.program.Bind();
|
|
|
|
|
|
|
|
// Calculate stride in buffer elements
|
|
|
|
u32 row_stride_in_elements = row_stride / bytes_per_buffer_elem;
|
|
|
|
u32 offset_in_elements = buffer.second / bytes_per_buffer_elem;
|
|
|
|
u32 palette_offset_in_elements = (buffer.second + palette_offset) / sizeof(u16);
|
|
|
|
if (info.uniform_dst_size >= 0)
|
|
|
|
glUniform2ui(info.uniform_dst_size, width, height);
|
|
|
|
if (info.uniform_src_size >= 0)
|
|
|
|
glUniform2ui(info.uniform_src_size, aligned_width, aligned_height);
|
|
|
|
if (info.uniform_src_offset >= 0)
|
|
|
|
glUniform1ui(info.uniform_src_offset, offset_in_elements);
|
|
|
|
if (info.uniform_src_row_stride >= 0)
|
|
|
|
glUniform1ui(info.uniform_src_row_stride, row_stride_in_elements);
|
|
|
|
if (info.uniform_palette_offset >= 0)
|
|
|
|
glUniform1ui(info.uniform_palette_offset, palette_offset_in_elements);
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2017-09-17 05:54:03 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_texture_decoding_buffer_views[info.base_info->buffer_format]);
|
2016-11-27 08:15:00 +00:00
|
|
|
|
|
|
|
if (has_palette)
|
|
|
|
{
|
|
|
|
// Use an R16UI view for the palette.
|
|
|
|
glActiveTexture(GL_TEXTURE10);
|
2017-09-17 05:54:03 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, m_palette_resolv_texture);
|
2016-11-27 08:15:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 13:00:42 +00:00
|
|
|
auto dispatch_groups =
|
2017-11-25 10:13:22 +00:00
|
|
|
TextureConversionShaderTiled::GetDispatchCount(info.base_info, aligned_width, aligned_height);
|
2017-04-23 04:44:34 +00:00
|
|
|
glBindImageTexture(0, static_cast<OGLTexture*>(entry->texture.get())->GetRawTexIdentifier(),
|
|
|
|
dst_level, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA8);
|
2016-11-27 08:15:00 +00:00
|
|
|
glDispatchCompute(dispatch_groups.first, dispatch_groups.second, 1);
|
|
|
|
glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
|
|
|
|
|
2016-11-27 08:15:02 +00:00
|
|
|
#ifdef TIME_TEXTURE_DECODING
|
|
|
|
WARN_LOG(VIDEO, "Decode texture format %u size %ux%u took %.4fms", static_cast<u32>(format),
|
|
|
|
width, height, timer.GetTimeMilliseconds());
|
|
|
|
#endif
|
2016-11-27 08:15:00 +00:00
|
|
|
}
|
2017-04-23 04:44:34 +00:00
|
|
|
|
|
|
|
void TextureCache::CopyEFBToCacheEntry(TCacheEntry* entry, bool is_depth_copy,
|
|
|
|
const EFBRectangle& src_rect, bool scale_by_half,
|
2017-08-04 15:56:24 +00:00
|
|
|
EFBCopyFormat dst_format, bool is_intensity)
|
2017-04-23 04:44:34 +00:00
|
|
|
{
|
|
|
|
auto* destination_texture = static_cast<OGLTexture*>(entry->texture.get());
|
|
|
|
g_renderer->ResetAPIState(); // reset any game specific settings
|
|
|
|
|
|
|
|
// Make sure to resolve anything we need to read from.
|
|
|
|
const GLuint read_texture = is_depth_copy ?
|
|
|
|
FramebufferManager::ResolveAndGetDepthTarget(src_rect) :
|
|
|
|
FramebufferManager::ResolveAndGetRenderTarget(src_rect);
|
|
|
|
|
|
|
|
FramebufferManager::SetFramebuffer(destination_texture->GetFramebuffer());
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, read_texture);
|
|
|
|
if (scale_by_half)
|
|
|
|
g_sampler_cache->BindLinearSampler(9);
|
|
|
|
else
|
|
|
|
g_sampler_cache->BindNearestSampler(9);
|
|
|
|
|
|
|
|
glViewport(0, 0, destination_texture->GetConfig().width, destination_texture->GetConfig().height);
|
|
|
|
|
2017-11-25 10:07:14 +00:00
|
|
|
auto uid = TextureConversionShaderGen::GetShaderUid(dst_format, is_depth_copy, is_intensity,
|
|
|
|
scale_by_half);
|
2017-08-04 15:56:24 +00:00
|
|
|
|
|
|
|
auto it = m_efb_copy_programs.emplace(uid, EFBCopyShader());
|
|
|
|
EFBCopyShader& shader = it.first->second;
|
|
|
|
bool created = it.second;
|
|
|
|
|
|
|
|
if (created)
|
2017-04-23 04:44:34 +00:00
|
|
|
{
|
2017-11-25 10:07:14 +00:00
|
|
|
ShaderCode code = TextureConversionShaderGen::GenerateShader(APIType::OpenGL, uid.GetUidData());
|
2017-08-04 15:56:24 +00:00
|
|
|
|
|
|
|
std::string geo_program = "";
|
|
|
|
char prefix = 'f';
|
|
|
|
if (g_ActiveConfig.stereo_mode != StereoMode::Off)
|
|
|
|
{
|
|
|
|
geo_program = geometry_program;
|
|
|
|
prefix = 'v';
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramShaderCache::CompileShader(shader.shader,
|
|
|
|
StringFromFormat(vertex_program, prefix, prefix),
|
|
|
|
code.GetBuffer(), geo_program);
|
|
|
|
|
|
|
|
shader.position_uniform = glGetUniformLocation(shader.shader.glprogid, "copy_position");
|
2017-04-23 04:44:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 15:56:24 +00:00
|
|
|
shader.shader.Bind();
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
TargetRectangle R = g_renderer->ConvertEFBRectangle(src_rect);
|
2017-08-04 15:56:24 +00:00
|
|
|
glUniform4f(shader.position_uniform, static_cast<float>(R.left), static_cast<float>(R.top),
|
2017-04-23 04:44:34 +00:00
|
|
|
static_cast<float>(R.right), static_cast<float>(R.bottom));
|
|
|
|
|
2018-01-20 14:59:10 +00:00
|
|
|
ProgramShaderCache::BindVertexFormat(nullptr);
|
2017-04-23 04:44:34 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
FramebufferManager::SetFramebuffer(0);
|
|
|
|
g_renderer->RestoreAPIState();
|
|
|
|
}
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|