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
|
|
|
|
2009-02-21 13:53:26 +00:00
|
|
|
// Fast image conversion using OpenGL shaders.
|
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoBackends/OGL/TextureConverter.h"
|
|
|
|
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-03 05:08:54 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/OGL/FramebufferManager.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"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/TextureCache.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/TextureConversionShader.h"
|
2016-07-21 23:04:57 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
namespace TextureConverter
|
|
|
|
{
|
2017-12-03 23:47:14 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-04 13:55:36 +00:00
|
|
|
struct EncodingProgram
|
|
|
|
{
|
|
|
|
SHADER program;
|
|
|
|
GLint copy_position_uniform;
|
2017-07-03 02:24:20 +00:00
|
|
|
GLint y_scale_uniform;
|
2018-04-29 08:52:30 +00:00
|
|
|
GLint gamma_rcp_uniform;
|
|
|
|
GLint clamp_tb_uniform;
|
|
|
|
GLint filter_coefficients_uniform;
|
2017-04-04 13:55:36 +00:00
|
|
|
};
|
2017-12-03 23:47:14 +00:00
|
|
|
|
|
|
|
std::map<EFBCopyParams, EncodingProgram> s_encoding_programs;
|
|
|
|
std::unique_ptr<AbstractTexture> s_encoding_render_texture;
|
|
|
|
std::unique_ptr<AbstractStagingTexture> s_encoding_readback_texture;
|
|
|
|
|
|
|
|
const int renderBufferWidth = EFB_WIDTH * 4;
|
|
|
|
const int renderBufferHeight = 1024;
|
2018-05-22 02:14:48 +00:00
|
|
|
} // namespace
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
static EncodingProgram& GetOrCreateEncodingShader(const EFBCopyParams& params)
|
2009-01-11 22:25:57 +00:00
|
|
|
{
|
2017-07-30 19:45:55 +00:00
|
|
|
auto iter = s_encoding_programs.find(params);
|
2017-04-04 13:55:36 +00:00
|
|
|
if (iter != s_encoding_programs.end())
|
|
|
|
return iter->second;
|
2009-01-11 22:25:57 +00:00
|
|
|
|
2017-11-25 10:13:22 +00:00
|
|
|
const char* shader =
|
|
|
|
TextureConversionShaderTiled::GenerateEncodingShader(params, APIType::OpenGL);
|
2009-01-11 22:25:57 +00:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2017-04-04 13:55:36 +00:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS && shader)
|
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
std::string filename =
|
|
|
|
StringFromFormat("%senc_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
SaveData(filename, shader);
|
|
|
|
}
|
2009-01-11 22:25:57 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
const char* VProgram = "void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
2013-11-25 14:01:18 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
EncodingProgram program;
|
|
|
|
if (!ProgramShaderCache::CompileShader(program.program, VProgram, shader))
|
|
|
|
PanicAlert("Failed to compile texture encoding shader.");
|
2014-01-05 08:52:26 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
program.copy_position_uniform = glGetUniformLocation(program.program.glprogid, "position");
|
2017-07-03 02:24:20 +00:00
|
|
|
program.y_scale_uniform = glGetUniformLocation(program.program.glprogid, "y_scale");
|
2018-04-29 08:52:30 +00:00
|
|
|
program.gamma_rcp_uniform = glGetUniformLocation(program.program.glprogid, "gamma_rcp");
|
|
|
|
program.clamp_tb_uniform = glGetUniformLocation(program.program.glprogid, "clamp_tb");
|
|
|
|
program.filter_coefficients_uniform =
|
|
|
|
glGetUniformLocation(program.program.glprogid, "filter_coefficients");
|
2017-07-30 19:45:55 +00:00
|
|
|
return s_encoding_programs.emplace(params, program).first->second;
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2018-01-21 05:03:06 +00:00
|
|
|
TextureConfig config(renderBufferWidth, renderBufferHeight, 1, 1, 1, AbstractTextureFormat::BGRA8,
|
2017-10-30 13:06:46 +00:00
|
|
|
true);
|
|
|
|
s_encoding_render_texture = g_renderer->CreateTexture(config);
|
|
|
|
s_encoding_readback_texture =
|
|
|
|
g_renderer->CreateStagingTexture(StagingTextureType::Readback, config);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
2017-10-30 13:06:46 +00:00
|
|
|
s_encoding_readback_texture.reset();
|
|
|
|
s_encoding_render_texture.reset();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
for (auto& program : s_encoding_programs)
|
|
|
|
program.second.program.Destroy();
|
|
|
|
s_encoding_programs.clear();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:09:25 +00:00
|
|
|
// dst_line_size, writeStride in bytes
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static void EncodeToRamUsingShader(GLuint srcTexture, u8* destAddr, u32 dst_line_size,
|
2017-07-03 02:24:20 +00:00
|
|
|
u32 dstHeight, u32 writeStride, bool linearFilter, float y_scale)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2017-10-30 13:06:46 +00:00
|
|
|
FramebufferManager::SetFramebuffer(
|
|
|
|
static_cast<OGLTexture*>(s_encoding_render_texture.get())->GetFramebuffer());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// set source texture
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, srcTexture);
|
|
|
|
|
2016-09-06 23:17:32 +00:00
|
|
|
// We also linear filtering for both box filtering and downsampling higher resolutions to 1x
|
2017-07-03 14:32:02 +00:00
|
|
|
// TODO: This only produces perfect downsampling for 2x IR, other resolutions will need more
|
|
|
|
// complex down filtering to average all pixels and produce the correct result.
|
2016-09-06 23:17:32 +00:00
|
|
|
// Also, box filtering won't be correct for anything other than 1x IR
|
2017-07-03 02:24:20 +00:00
|
|
|
if (linearFilter || g_renderer->GetEFBScale() != 1 || y_scale > 1.0f)
|
2016-06-24 08:43:46 +00:00
|
|
|
g_sampler_cache->BindLinearSampler(9);
|
|
|
|
else
|
|
|
|
g_sampler_cache->BindNearestSampler(9);
|
|
|
|
|
|
|
|
glViewport(0, 0, (GLsizei)(dst_line_size / 4), (GLsizei)dstHeight);
|
|
|
|
|
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);
|
|
|
|
|
2017-10-30 13:06:46 +00:00
|
|
|
MathUtil::Rectangle<int> copy_rect(0, 0, dst_line_size / 4, dstHeight);
|
|
|
|
s_encoding_readback_texture->CopyFromTexture(s_encoding_render_texture.get(), copy_rect, 0, 0,
|
|
|
|
copy_rect);
|
|
|
|
s_encoding_readback_texture->ReadTexels(copy_rect, destAddr, writeStride);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
void EncodeToRamFromTexture(u8* dest_ptr, 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,
|
2018-04-29 08:52:30 +00:00
|
|
|
const EFBRectangle& src_rect, bool scale_by_half, float y_scale,
|
|
|
|
float gamma, float clamp_top, float clamp_bottom,
|
|
|
|
const TextureCacheBase::CopyFilterCoefficientArray& filter_coefficients)
|
2010-07-12 19:30:25 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
g_renderer->ResetAPIState();
|
2015-10-29 07:45:17 +00:00
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
EncodingProgram& texconv_shader = GetOrCreateEncodingShader(params);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
texconv_shader.program.Bind();
|
|
|
|
glUniform4i(texconv_shader.copy_position_uniform, src_rect.left, src_rect.top, native_width,
|
|
|
|
scale_by_half ? 2 : 1);
|
2018-04-29 08:52:30 +00:00
|
|
|
glUniform1f(texconv_shader.y_scale_uniform, y_scale);
|
|
|
|
glUniform1f(texconv_shader.gamma_rcp_uniform, 1.0f / gamma);
|
|
|
|
glUniform2f(texconv_shader.clamp_tb_uniform, clamp_top, clamp_bottom);
|
2018-05-22 02:14:48 +00:00
|
|
|
glUniform3f(texconv_shader.filter_coefficients_uniform, filter_coefficients[0],
|
2018-04-29 08:52:30 +00:00
|
|
|
filter_coefficients[1], filter_coefficients[2]);
|
2010-12-20 17:06:39 +00:00
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
const GLuint read_texture = params.depth ?
|
2017-04-04 13:55:36 +00:00
|
|
|
FramebufferManager::ResolveAndGetDepthTarget(src_rect) :
|
|
|
|
FramebufferManager::ResolveAndGetRenderTarget(src_rect);
|
2015-10-29 07:45:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
EncodeToRamUsingShader(read_texture, dest_ptr, bytes_per_row, num_blocks_y, memory_stride,
|
2018-04-29 08:52:30 +00:00
|
|
|
scale_by_half && !params.depth, y_scale);
|
2015-10-29 07:45:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2009-01-11 22:25:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:14:48 +00:00
|
|
|
} // namespace TextureConverter
|
2011-01-29 20:16:51 +00:00
|
|
|
|
2011-01-29 22:48:33 +00:00
|
|
|
} // namespace OGL
|