2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
2013-04-15 20:28:55 +00:00
|
|
|
// Official Git repository and contact information can be found at
|
2009-07-06 02:10:26 +00:00
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "FileUtil.h"
|
2009-09-13 09:23:30 +00:00
|
|
|
#include "VideoCommon.h"
|
|
|
|
#include "VideoConfig.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
#include "GLUtil.h"
|
|
|
|
#include "PostProcessing.h"
|
2013-02-13 12:12:19 +00:00
|
|
|
#include "ProgramShaderCache.h"
|
2013-03-07 16:00:11 +00:00
|
|
|
#include "FramebufferManager.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
namespace PostProcessing
|
|
|
|
{
|
|
|
|
|
|
|
|
static std::string s_currentShader;
|
2013-02-13 12:12:19 +00:00
|
|
|
static SHADER s_shader;
|
2013-03-07 16:00:11 +00:00
|
|
|
static bool s_enable;
|
|
|
|
|
|
|
|
static u32 s_width;
|
|
|
|
static u32 s_height;
|
|
|
|
static GLuint s_fbo;
|
|
|
|
static GLuint s_texture;
|
|
|
|
static GLuint s_vao;
|
|
|
|
static GLuint s_vbo;
|
|
|
|
|
2013-03-07 16:35:27 +00:00
|
|
|
static GLuint s_uniform_resolution;
|
|
|
|
|
|
|
|
static char s_vertex_shader[] =
|
2013-03-07 16:00:11 +00:00
|
|
|
"in vec2 rawpos;\n"
|
|
|
|
"in vec2 tex0;\n"
|
|
|
|
"out vec2 uv0;\n"
|
|
|
|
"void main(void) {\n"
|
|
|
|
" gl_Position = vec4(rawpos,0,1);\n"
|
|
|
|
" uv0 = tex0;\n"
|
|
|
|
"}\n";
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
s_currentShader = "";
|
2013-03-07 16:00:11 +00:00
|
|
|
s_enable = 0;
|
|
|
|
s_width = 0;
|
|
|
|
s_height = 0;
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &s_fbo);
|
|
|
|
glGenTextures(1, &s_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, s_texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); // disable mipmaps
|
2013-03-08 08:36:28 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2013-03-07 20:30:11 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
2013-03-07 16:00:11 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, s_fbo);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, s_texture, 0);
|
|
|
|
FramebufferManager::SetFramebuffer(0);
|
|
|
|
|
|
|
|
glGenBuffers(1, &s_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, s_vbo);
|
|
|
|
GLfloat vertices[] = {
|
|
|
|
-1.f, -1.f, 0.f, 0.f,
|
|
|
|
-1.f, 1.f, 0.f, 1.f,
|
|
|
|
1.f, -1.f, 1.f, 0.f,
|
|
|
|
1.f, 1.f, 1.f, 1.f
|
|
|
|
};
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &s_vao);
|
|
|
|
glBindVertexArray( s_vao );
|
|
|
|
glEnableVertexAttribArray(SHADER_POSITION_ATTRIB);
|
|
|
|
glVertexAttribPointer(SHADER_POSITION_ATTRIB, 2, GL_FLOAT, 0, sizeof(GLfloat)*4, NULL);
|
|
|
|
glEnableVertexAttribArray(SHADER_TEXTURE0_ATTRIB);
|
|
|
|
glVertexAttribPointer(SHADER_TEXTURE0_ATTRIB, 2, GL_FLOAT, 0, sizeof(GLfloat)*4, (GLfloat*)NULL+2);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
s_shader.Destroy();
|
2013-03-07 16:00:11 +00:00
|
|
|
|
|
|
|
glDeleteFramebuffers(1, &s_vbo);
|
|
|
|
glDeleteTextures(1, &s_texture);
|
|
|
|
|
|
|
|
glDeleteBuffers(1, &s_vbo);
|
|
|
|
glDeleteVertexArrays(1, &s_vao);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReloadShader()
|
|
|
|
{
|
|
|
|
s_currentShader = "";
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
void BindTargetFramebuffer ()
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-03-07 16:00:11 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, s_enable ? s_fbo : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlitToScreen()
|
|
|
|
{
|
|
|
|
if(!s_enable) return;
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
glViewport(0, 0, s_width, s_height);
|
|
|
|
|
|
|
|
glBindVertexArray(s_vao);
|
|
|
|
s_shader.Bind();
|
2013-03-07 16:35:27 +00:00
|
|
|
|
2013-03-07 18:51:57 +00:00
|
|
|
glUniform4f(s_uniform_resolution, (float)s_width, (float)s_height, 1.0f/(float)s_width, 1.0f/(float)s_height);
|
2013-03-07 16:35:27 +00:00
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, s_texture);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
/* glBindFramebuffer(GL_READ_FRAMEBUFFER, s_fbo);
|
|
|
|
|
|
|
|
glBlitFramebuffer(rc.left, rc.bottom, rc.right, rc.top,
|
|
|
|
rc.left, rc.bottom, rc.right, rc.top,
|
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update ( u32 width, u32 height )
|
|
|
|
{
|
|
|
|
ApplyShader();
|
|
|
|
|
|
|
|
if(s_enable && (width != s_width || height != s_height)) {
|
|
|
|
s_width = width;
|
|
|
|
s_height = height;
|
|
|
|
|
|
|
|
// alloc texture for framebuffer
|
|
|
|
glActiveTexture(GL_TEXTURE0+9);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, s_texture);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2013-03-07 16:00:11 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
void ApplyShader()
|
|
|
|
{
|
|
|
|
// shader didn't changed
|
|
|
|
if (s_currentShader == g_ActiveConfig.sPostProcessingShader) return;
|
|
|
|
s_currentShader = g_ActiveConfig.sPostProcessingShader;
|
|
|
|
s_enable = false;
|
|
|
|
s_shader.Destroy();
|
|
|
|
|
|
|
|
// shader disabled
|
|
|
|
if (g_ActiveConfig.sPostProcessingShader == "") return;
|
|
|
|
|
|
|
|
// so need to compile shader
|
|
|
|
|
|
|
|
// loading shader code
|
|
|
|
std::string code;
|
|
|
|
std::string path = File::GetUserPath(D_SHADERS_IDX) + g_ActiveConfig.sPostProcessingShader + ".txt";
|
|
|
|
if(!File::ReadFileToString(true, path.c_str(), code)) {
|
2013-03-31 23:10:21 +00:00
|
|
|
ERROR_LOG(VIDEO, "Post-processing shader not found: %s", path.c_str());
|
2013-03-07 16:00:11 +00:00
|
|
|
return;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2013-03-07 16:00:11 +00:00
|
|
|
|
|
|
|
// and compile it
|
|
|
|
if (!ProgramShaderCache::CompileShader(s_shader, s_vertex_shader, code.c_str())) {
|
|
|
|
ERROR_LOG(VIDEO, "Failed to compile post-processing shader %s", s_currentShader.c_str());
|
|
|
|
return;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2013-03-07 16:00:11 +00:00
|
|
|
|
2013-03-07 16:35:27 +00:00
|
|
|
// read uniform locations
|
|
|
|
s_uniform_resolution = glGetUniformLocation(s_shader.glprogid, "resolution");
|
|
|
|
|
2013-03-07 16:00:11 +00:00
|
|
|
// successful
|
|
|
|
s_enable = true;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2011-01-29 20:16:51 +00:00
|
|
|
|
2011-01-29 22:48:33 +00:00
|
|
|
} // namespace OGL
|