Commit 908faf7b authored by Christopher Tenter's avatar Christopher Tenter

fxaa plugin now uses the official nvidia shader

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free-Staging@20357 383ad7c9-94d9-4d36-a494-682f7c89f535
parent d94fdc60
......@@ -54,13 +54,14 @@
PostProcessorFXAAPlugin::PostProcessorFXAAPlugin() :
fxaa_(0)
fxaa_(0), luma_(0)
{
}
PostProcessorFXAAPlugin::~PostProcessorFXAAPlugin()
{
delete fxaa_;
delete luma_;
}
......@@ -69,9 +70,12 @@ QString PostProcessorFXAAPlugin::postProcessorName() {
}
QString PostProcessorFXAAPlugin::checkOpenGL() {
if ( ! ACG::openGLVersion(3, 0) )
if (!ACG::openGLVersion(3, 0))
return QString("Insufficient OpenGL Version! OpenGL 3.0 or higher required");
if (!ACG::checkExtensionSupported("ARB_texture_gather"))
return QString("Missing extension: ARB_texture_gather");
return QString("");
}
......@@ -79,10 +83,16 @@ QString PostProcessorFXAAPlugin::checkOpenGL() {
void PostProcessorFXAAPlugin::postProcess(ACG::GLState* _glstate, const std::vector<const PostProcessorInput*>& _input, const PostProcessorOutput& _output) {
// ======================================================================================================
// Load shader if needed
// Load shaders if needed
// ======================================================================================================
if (!fxaa_)
fxaa_ = GLSL::loadProgram("FXAA/screenquad.glsl", "FXAA/fxaa.glsl");
fxaa_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/fxaa.glsl");
if (!luma_)
luma_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "FXAA/luma.glsl");
if (!fxaa_ || !luma_)
return;
// ======================================================================================================
// Bind input texture
......@@ -92,17 +102,31 @@ void PostProcessorFXAAPlugin::postProcess(ACG::GLState* _glstate, const std::vec
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _input[0]->colorTex_);
// ======================================================================================================
// Bind output FBO
// Put luma in alpha channel
// ======================================================================================================
glBindFramebuffer(GL_FRAMEBUFFER, _output.fbo_);
glDrawBuffer(_output.drawBuffer_);
if (!lumaRT_.width())
lumaRT_.attachTexture2D(GL_COLOR_ATTACHMENT0, _input[0]->width, _input[0]->height, GL_RGBA, GL_RGBA, GL_CLAMP, GL_LINEAR, GL_LINEAR);
else
lumaRT_.resize(_input[0]->width, _input[0]->height);
lumaRT_.bind();
luma_->use();
luma_->setUniform("textureSampler", 0);
ACG::ScreenQuad::draw(luma_);
glBindTexture(GL_TEXTURE_2D, lumaRT_.getAttachment(GL_COLOR_ATTACHMENT0));
// ======================================================================================================
// Clear rendering buffer
// Bind output FBO
// ======================================================================================================
_glstate->clearBuffers();
glBindFramebuffer(GL_FRAMEBUFFER, _output.fbo_);
glDrawBuffer(_output.drawBuffer_);
// ======================================================================================================
// Setup render states
......@@ -121,8 +145,14 @@ void PostProcessorFXAAPlugin::postProcess(ACG::GLState* _glstate, const std::vec
fxaa_->use();
fxaa_->setUniform("textureSampler", 0);
ACG::Vec2f texcoordOffset(1.0f / float(_input[0]->width), 1.0f / float(_input[0]->height));
fxaa_->setUniform("texcoordOffset", texcoordOffset);
ACG::Vec2f texelSize(1.0f / float(_input[0]->width), 1.0f / float(_input[0]->height));
fxaa_->setUniform("fxaaQualityRcpFrame", texelSize);
ACG::Vec4f fxaaConsoleRcp(-texelSize[0], -texelSize[1], texelSize[0], texelSize[1]);
fxaa_->setUniform("fxaaConsoleRcpFrameOpt", fxaaConsoleRcp * 0.5f);
fxaa_->setUniform("fxaaConsoleRcpFrameOpt2", fxaaConsoleRcp * 2.0f);
// ======================================================================================================
// Execute
......
......@@ -48,6 +48,9 @@
#include <ACG/GL/globjects.hh>
#include <ACG/ShaderUtils/GLSLShader.hh>
#include <ACG/GL/FBO.hh>
#include <map>
class PostProcessorFXAAPlugin : public QObject, BaseInterface, PostProcessorInterface
{
......@@ -84,6 +87,12 @@ class PostProcessorFXAAPlugin : public QObject, BaseInterface, PostProcessorInte
/// fxaa shader
GLSL::Program* fxaa_;
/// luma shader
GLSL::Program* luma_;
/// fbo with rt that contains luminance in alpha
ACG::FBO lumaRT_;
private:
};
// FXAA shader, GLSL code adapted from:
// http://horde3d.org/wiki/index.php5?title=Shading_Technique_-_FXAA
// Whitepaper describing the technique:
#version 130
#extension GL_ARB_texture_gather : enable
// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
#version 150
in vec2 vTexCoord;
out vec4 outColor;
uniform sampler2D textureSampler;
// The inverse of the texture dimensions along X and Y
uniform vec2 texcoordOffset;
// fxaa constants
uniform vec2 fxaaQualityRcpFrame;
uniform vec4 fxaaConsoleRcpFrameOpt;
uniform vec4 fxaaConsoleRcpFrameOpt2;
uniform vec4 fxaaConsole360RcpFrameOpt2;
uniform float fxaaQualitySubpix = 0.75;
uniform float fxaaQualityEdgeThreshold = 0.116;
uniform float fxaaQualityEdgeThresholdMin = 0.0833;
uniform float fxaaConsoleEdgeSharpness = 8.0;
uniform float fxaaConsoleEdgeThreshold = 0.125;
uniform float fxaaConsoleEdgeThresholdMin = 0.05;
uniform vec4 fxaaConsole360ConstDir = vec4(0,0,0,0);
in vec2 vTexCoord;
out vec4 oColor;
void main() {
// The parameters are hardcoded for now, but could be
// made into uniforms to control fromt he program.
float FXAA_SPAN_MAX = 8.0;
float FXAA_REDUCE_MUL = 1.0/8.0;
float FXAA_REDUCE_MIN = (1.0/128.0);
// configure fxaa
#define FXAA_PC 1
#define FXAA_GLSL_130 1
#define FXAA_QUALITY__PRESET 39
vec3 rgbNW = texture2D(textureSampler, vTexCoord.xy + (vec2(-1.0, -1.0) * texcoordOffset)).xyz;
vec3 rgbNE = texture2D(textureSampler, vTexCoord.xy + (vec2(+1.0, -1.0) * texcoordOffset)).xyz;
vec3 rgbSW = texture2D(textureSampler, vTexCoord.xy + (vec2(-1.0, +1.0) * texcoordOffset)).xyz;
vec3 rgbSE = texture2D(textureSampler, vTexCoord.xy + (vec2(+1.0, +1.0) * texcoordOffset)).xyz;
vec3 rgbM = texture2D(textureSampler, vTexCoord.xy).xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot( rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * texcoordOffset;
vec3 rgbA = (1.0/2.0) * (
texture2D(textureSampler, vTexCoord.xy + dir * (1.0/3.0 - 0.5)).xyz +
texture2D(textureSampler, vTexCoord.xy + dir * (2.0/3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
texture2D(textureSampler, vTexCoord.xy + dir * (0.0/3.0 - 0.5)).xyz +
texture2D(textureSampler, vTexCoord.xy + dir * (3.0/3.0 - 0.5)).xyz);
float lumaB = dot(rgbB, luma);
#include "Fxaa3_11.h"
if((lumaB < lumaMin) || (lumaB > lumaMax)){
oColor.xyz=rgbA;
} else {
oColor.xyz=rgbB;
}
oColor.a = 1.0;
void main()
{
vec4 fxaaConsolePosPos = vec4(vTexCoord - 0.5 * fxaaQualityRcpFrame, vTexCoord + 0.5 * fxaaQualityRcpFrame);
// oColor.xyz = rgbM;
outColor = FxaaPixelShader(
vTexCoord,
fxaaConsolePosPos,
textureSampler,
textureSampler,
textureSampler,
fxaaQualityRcpFrame,
fxaaConsoleRcpFrameOpt,
fxaaConsoleRcpFrameOpt2,
fxaaConsole360RcpFrameOpt2,
fxaaQualitySubpix,
fxaaQualityEdgeThreshold,
fxaaQualityEdgeThresholdMin,
fxaaConsoleEdgeSharpness,
fxaaConsoleEdgeThreshold,
fxaaConsoleEdgeThresholdMin,
fxaaConsole360ConstDir);
// outColor = texture(textureSampler, vTexCoord).wwww;
// outColor = vec4(1,1,1,1);
}
#version 130
in vec2 vTexCoord;
out vec4 outColor;
uniform sampler2D textureSampler;
void main()
{
vec3 color = texture(textureSampler, vTexCoord).xyz;
float lum = dot(color, vec3(0.299, 0.587, 0.114));
outColor = vec4(color, lum);
}
\ No newline at end of file
#version 150
in vec4 inPosition;
out vec2 vTexCoord;
void main()
{
gl_Position = inPosition;
vTexCoord = inPosition.xy * 0.5 + vec2(0.5, 0.5);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment