2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2014-07-29 16:57:02 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/OGL/FramebufferManager.h"
|
|
|
|
#include "VideoBackends/OGL/GLUtil.h"
|
|
|
|
#include "VideoBackends/OGL/PostProcessing.h"
|
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
|
2014-10-30 01:06:45 +00:00
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2014-10-30 01:06:45 +00:00
|
|
|
static char s_vertex_workaround_shader[] =
|
|
|
|
"in vec4 rawpos;\n"
|
|
|
|
"out vec2 uv0;\n"
|
|
|
|
"uniform vec4 src_rect;\n"
|
|
|
|
"void main(void) {\n"
|
|
|
|
" gl_Position = vec4(rawpos.xy, 0.0, 1.0);\n"
|
|
|
|
" uv0 = rawpos.zw * src_rect.zw + src_rect.xy;\n"
|
|
|
|
"}\n";
|
|
|
|
|
2013-03-07 16:35:27 +00:00
|
|
|
static char s_vertex_shader[] =
|
2013-03-07 16:00:11 +00:00
|
|
|
"out vec2 uv0;\n"
|
2014-10-18 07:32:24 +00:00
|
|
|
"uniform vec4 src_rect;\n"
|
2013-03-07 16:00:11 +00:00
|
|
|
"void main(void) {\n"
|
2013-11-25 11:36:17 +00:00
|
|
|
" vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
|
|
|
|
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
|
2014-10-18 07:32:24 +00:00
|
|
|
" uv0 = rawpos * src_rect.zw + src_rect.xy;\n"
|
2013-10-29 05:23:17 +00:00
|
|
|
"}\n";
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
OpenGLPostProcessing::OpenGLPostProcessing()
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-07-29 16:57:02 +00:00
|
|
|
CreateHeader();
|
2014-10-30 01:06:45 +00:00
|
|
|
|
|
|
|
m_attribute_workaround = DriverDetails::HasBug(DriverDetails::BUG_BROKENATTRIBUTELESS);
|
|
|
|
if (m_attribute_workaround)
|
|
|
|
{
|
|
|
|
glGenBuffers(1, &m_attribute_vbo);
|
|
|
|
glGenVertexArrays(1, &m_attribute_vao);
|
|
|
|
}
|
|
|
|
|
2014-10-18 07:32:24 +00:00
|
|
|
m_initialized = false;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
OpenGLPostProcessing::~OpenGLPostProcessing()
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-07-29 16:47:56 +00:00
|
|
|
m_shader.Destroy();
|
2014-10-30 01:06:45 +00:00
|
|
|
|
|
|
|
if (m_attribute_workaround)
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &m_attribute_vbo);
|
|
|
|
glDeleteVertexArrays(1, &m_attribute_vao);
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-18 07:32:24 +00:00
|
|
|
void OpenGLPostProcessing::BlitFromTexture(TargetRectangle src, TargetRectangle dst,
|
|
|
|
int src_texture, int src_width, int src_height)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2014-10-18 07:32:24 +00:00
|
|
|
ApplyShader();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
2014-10-18 07:32:24 +00:00
|
|
|
|
|
|
|
glViewport(dst.left, dst.bottom, dst.GetWidth(), dst.GetHeight());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-30 01:06:45 +00:00
|
|
|
if (m_attribute_workaround)
|
|
|
|
glBindVertexArray(m_attribute_vao);
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
m_shader.Bind();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-18 07:32:24 +00:00
|
|
|
glUniform4f(m_uniform_resolution, (float)src_width, (float)src_height, 1.0f / (float)src_width, 1.0f / (float)src_height);
|
|
|
|
glUniform4f(m_uniform_src_rect, src.left / (float) src_width, src.bottom / (float) src_height,
|
|
|
|
src.GetWidth() / (float) src_width, src.GetHeight() / (float) src_height);
|
2014-07-29 16:47:56 +00:00
|
|
|
glUniform1ui(m_uniform_time, (GLuint)m_timer.GetTimeElapsed());
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
if (m_config.IsDirty())
|
|
|
|
{
|
|
|
|
for (auto& it : m_config.GetOptions())
|
|
|
|
{
|
|
|
|
if (it.second.m_dirty)
|
|
|
|
{
|
|
|
|
switch (it.second.m_type)
|
|
|
|
{
|
|
|
|
case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_BOOL:
|
|
|
|
glUniform1i(m_uniform_bindings[it.first], it.second.m_bool_value);
|
|
|
|
break;
|
|
|
|
case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_INTEGER:
|
|
|
|
switch (it.second.m_integer_values.size())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
glUniform1i(m_uniform_bindings[it.first], it.second.m_integer_values[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
glUniform2i(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_integer_values[0],
|
|
|
|
it.second.m_integer_values[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
glUniform3i(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_integer_values[0],
|
|
|
|
it.second.m_integer_values[1],
|
|
|
|
it.second.m_integer_values[2]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
glUniform4i(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_integer_values[0],
|
|
|
|
it.second.m_integer_values[1],
|
|
|
|
it.second.m_integer_values[2],
|
|
|
|
it.second.m_integer_values[3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_FLOAT:
|
|
|
|
switch (it.second.m_float_values.size())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
glUniform1f(m_uniform_bindings[it.first], it.second.m_float_values[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
glUniform2f(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_float_values[0],
|
|
|
|
it.second.m_float_values[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
glUniform3f(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_float_values[0],
|
|
|
|
it.second.m_float_values[1],
|
|
|
|
it.second.m_float_values[2]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
glUniform4f(m_uniform_bindings[it.first],
|
|
|
|
it.second.m_float_values[0],
|
|
|
|
it.second.m_float_values[1],
|
|
|
|
it.second.m_float_values[2],
|
|
|
|
it.second.m_float_values[3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it.second.m_dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_config.SetDirty(false);
|
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
2014-10-18 07:32:24 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, src_texture);
|
2014-10-27 20:15:32 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2013-03-07 16:00:11 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
}
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
void OpenGLPostProcessing::ApplyShader()
|
2013-03-07 16:00:11 +00:00
|
|
|
{
|
|
|
|
// shader didn't changed
|
2014-10-18 07:32:24 +00:00
|
|
|
if (m_initialized && m_config.GetShader() == g_ActiveConfig.sPostProcessingShader)
|
2014-08-15 18:09:53 +00:00
|
|
|
return;
|
2014-07-29 16:57:02 +00:00
|
|
|
|
|
|
|
m_shader.Destroy();
|
|
|
|
m_uniform_bindings.clear();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-18 07:32:24 +00:00
|
|
|
// load shader from disk
|
|
|
|
std::string default_shader = "void main() { SetOutput(Sample()); }";
|
|
|
|
std::string code = "";
|
|
|
|
if (g_ActiveConfig.sPostProcessingShader != "")
|
|
|
|
code = m_config.LoadShader();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
if (code == "")
|
2014-10-18 07:32:24 +00:00
|
|
|
code = default_shader;
|
2014-07-29 16:57:02 +00:00
|
|
|
|
|
|
|
code = LoadShaderOptions(code);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-10-30 01:06:45 +00:00
|
|
|
const char* vertex_shader = s_vertex_shader;
|
|
|
|
|
|
|
|
if (m_attribute_workaround)
|
|
|
|
vertex_shader = s_vertex_workaround_shader;
|
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
// and compile it
|
2014-10-30 01:06:45 +00:00
|
|
|
if (!ProgramShaderCache::CompileShader(m_shader, vertex_shader, code.c_str()))
|
2014-08-15 18:09:53 +00:00
|
|
|
{
|
2014-07-29 16:57:02 +00:00
|
|
|
ERROR_LOG(VIDEO, "Failed to compile post-processing shader %s", m_config.GetShader().c_str());
|
2014-10-18 07:32:24 +00:00
|
|
|
|
|
|
|
code = LoadShaderOptions(default_shader);
|
2014-10-30 01:06:45 +00:00
|
|
|
ProgramShaderCache::CompileShader(m_shader, vertex_shader, code.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-03-07 16:35:27 +00:00
|
|
|
// read uniform locations
|
2014-07-29 16:47:56 +00:00
|
|
|
m_uniform_resolution = glGetUniformLocation(m_shader.glprogid, "resolution");
|
|
|
|
m_uniform_time = glGetUniformLocation(m_shader.glprogid, "time");
|
2014-10-18 07:32:24 +00:00
|
|
|
m_uniform_src_rect = glGetUniformLocation(m_shader.glprogid, "src_rect");
|
2014-07-29 16:47:56 +00:00
|
|
|
|
2014-10-30 01:06:45 +00:00
|
|
|
if (m_attribute_workaround)
|
|
|
|
{
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
-1.f, -1.f, 0.f, 0.f,
|
|
|
|
1.f, -1.f, 1.f, 0.f,
|
|
|
|
-1.f, 1.f, 0.f, 1.f,
|
|
|
|
1.f, 1.f, 1.f, 1.f,
|
|
|
|
};
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_attribute_vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glBindVertexArray(m_attribute_vao);
|
|
|
|
glEnableVertexAttribArray(SHADER_POSITION_ATTRIB);
|
|
|
|
glVertexAttribPointer(SHADER_POSITION_ATTRIB, 4, GL_FLOAT, 0, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
for (const auto& it : m_config.GetOptions())
|
2014-07-29 16:47:56 +00:00
|
|
|
{
|
|
|
|
std::string glsl_name = "option_" + it.first;
|
|
|
|
m_uniform_bindings[it.first] = glGetUniformLocation(m_shader.glprogid, glsl_name.c_str());
|
|
|
|
}
|
2014-10-18 07:32:24 +00:00
|
|
|
m_initialized = true;
|
2014-07-29 16:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLPostProcessing::CreateHeader()
|
|
|
|
{
|
|
|
|
m_glsl_header =
|
|
|
|
// Required variables
|
|
|
|
// Shouldn't be accessed directly by the PP shader
|
|
|
|
// Texture sampler
|
|
|
|
"SAMPLER_BINDING(8) uniform sampler2D samp8;\n"
|
|
|
|
"SAMPLER_BINDING(9) uniform sampler2D samp9;\n"
|
|
|
|
|
|
|
|
// Output variable
|
|
|
|
"out float4 ocol0;\n"
|
|
|
|
// Input coordinates
|
|
|
|
"in float2 uv0;\n"
|
|
|
|
// Resolution
|
|
|
|
"uniform float4 resolution;\n"
|
|
|
|
// Time
|
|
|
|
"uniform uint time;\n"
|
|
|
|
|
|
|
|
// Interfacing functions
|
|
|
|
"float4 Sample()\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn texture(samp9, uv0);\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"float4 SampleLocation(float2 location)\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn texture(samp9, location);\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"#define SampleOffset(offset) textureOffset(samp9, uv0, offset)\n"
|
|
|
|
|
|
|
|
"float4 SampleFontLocation(float2 location)\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn texture(samp8, location);\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"float2 GetResolution()\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn resolution.xy;\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"float2 GetInvResolution()\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn resolution.zw;\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"float2 GetCoordinates()\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn uv0;\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"uint GetTime()\n"
|
|
|
|
"{\n"
|
|
|
|
"\treturn time;\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"void SetOutput(float4 color)\n"
|
|
|
|
"{\n"
|
|
|
|
"\tocol0 = color;\n"
|
|
|
|
"}\n"
|
|
|
|
|
2014-08-18 02:22:00 +00:00
|
|
|
"#define GetOption(x) (option_##x)\n"
|
|
|
|
"#define OptionEnabled(x) (option_##x != 0)\n";
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:57:02 +00:00
|
|
|
std::string OpenGLPostProcessing::LoadShaderOptions(const std::string& code)
|
|
|
|
{
|
|
|
|
std::string glsl_options = "";
|
|
|
|
m_uniform_bindings.clear();
|
|
|
|
|
|
|
|
for (const auto& it : m_config.GetOptions())
|
|
|
|
{
|
|
|
|
if (it.second.m_type == PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_BOOL)
|
|
|
|
{
|
|
|
|
glsl_options += StringFromFormat("uniform int option_%s;\n", it.first.c_str());
|
|
|
|
}
|
|
|
|
else if (it.second.m_type == PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_INTEGER)
|
|
|
|
{
|
2014-08-20 03:18:02 +00:00
|
|
|
u32 count = static_cast<u32>(it.second.m_integer_values.size());
|
2014-07-29 16:57:02 +00:00
|
|
|
if (count == 1)
|
|
|
|
glsl_options += StringFromFormat("uniform int option_%s;\n", it.first.c_str());
|
|
|
|
else
|
|
|
|
glsl_options += StringFromFormat("uniform int%d option_%s;\n", count, it.first.c_str());
|
|
|
|
}
|
|
|
|
else if (it.second.m_type == PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_FLOAT)
|
|
|
|
{
|
2014-08-20 03:18:02 +00:00
|
|
|
u32 count = static_cast<u32>(it.second.m_float_values.size());
|
2014-07-29 16:57:02 +00:00
|
|
|
if (count == 1)
|
|
|
|
glsl_options += StringFromFormat("uniform float option_%s;\n", it.first.c_str());
|
|
|
|
else
|
|
|
|
glsl_options += StringFromFormat("uniform float%d option_%s;\n", count, it.first.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_uniform_bindings[it.first] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_glsl_header + glsl_options + code;
|
|
|
|
}
|
2011-01-29 20:16:51 +00:00
|
|
|
|
2011-01-29 22:48:33 +00:00
|
|
|
} // namespace OGL
|