Add proper tonemap, although disabled.

It currently generates way too much HDR.
This commit is contained in:
vlj 2014-04-21 01:52:44 +02:00
parent 1d40851600
commit 8947242de4
7 changed files with 128 additions and 78 deletions

View File

@ -205,43 +205,18 @@ void main()
#else
//#define AUTO_EXPOSURE
uniform sampler2D tex;
uniform sampler2D dtex;
uniform vec3 inlevel;
uniform vec2 outlevel;
uniform mat4 invprojm;
uniform sampler2D logluminancetex;
#if __VERSION__ >= 130
in vec2 uv;
out vec4 FragColor;
#else
varying vec2 uv;
#define FragColor gl_FragColor
#endif
vec3 getCIEYxy(vec3 rgbColor);
vec3 getRGBFromCIEXxy(vec3 YxyColor);
float exposure = .1;
float whitePoint = 2.5;
float delta = 0.0001;
void main()
{
vec4 col = texture(tex, uv);
#ifdef AUTO_EXPOSURE
float avgLuminance = textureLod(logluminancetex, uv, 10.).x;
avgLuminance = exp(avgLuminance) - delta;
vec3 Yxy = getCIEYxy(col.xyz);
float a = max(0, 1.5 - 1.5 / (avgLuminance * .1 + 1)) + .1;
float Lp = Yxy.r * a / avgLuminance;
Yxy.r = (Lp * (1. * Lp / (whitePoint * whitePoint))) / (1. + Lp);
col.xyz = getRGBFromCIEXxy(Yxy);
#endif
float curdepth = texture(dtex, uv).x;
vec4 FragPos = invprojm * (2.0 * vec4(uv, curdepth, 1.0f) - 1.0f);

View File

@ -3,7 +3,7 @@ uniform sampler2D tex;
in vec2 uv;
out vec4 FragColor;
float delta = 0.0001;
float delta = .1;
void main()
{

26
data/shaders/tonemap.frag Normal file
View File

@ -0,0 +1,26 @@
#define AUTO_EXPOSURE
uniform sampler2D tex;
uniform sampler2D logluminancetex;
in vec2 uv;
out vec4 FragColor;
vec3 getCIEYxy(vec3 rgbColor);
vec3 getRGBFromCIEXxy(vec3 YxyColor);
float exposure = .4;
float whitePoint = 1.5;
float delta = .1;
void main()
{
vec4 col = texture(tex, uv);
float avgLuminance = textureLod(logluminancetex, uv, 10.).x;
avgLuminance = exp(avgLuminance) - delta;
vec3 Yxy = getCIEYxy(col.xyz);
float Lp = Yxy.r * exposure / avgLuminance;
Yxy.r = (Lp * (1. * Lp / (whitePoint * whitePoint))) / (1. + Lp);
FragColor.xyz = getRGBFromCIEXxy(Yxy);
}

View File

@ -234,7 +234,6 @@ void renderColorLevel(GLuint in)
glUniform1i(FullScreenShader::ColorLevelShader::uniform_tex, 0);
glUniform1i(FullScreenShader::ColorLevelShader::uniform_dtex, 1);
setTexture(2, irr_driver->getRenderTargetTexture(RTT_LOG_LUMINANCE), GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST);
glUniform1i(FullScreenShader::ColorLevelShader::uniform_logluminancetex, 2);
glUniformMatrix4fv(FullScreenShader::ColorLevelShader::uniform_invprojm, 1, GL_FALSE, irr_driver->getInvProjMatrix().pointer());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -524,6 +523,18 @@ static void renderGodRay(GLuint tex, const core::vector2df &sunpos)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
static void toneMap(GLuint fbo, GLuint rtt)
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glUseProgram(FullScreenShader::ToneMapShader::Program);
glBindVertexArray(FullScreenShader::ToneMapShader::vao);
setTexture(0, rtt, GL_NEAREST, GL_NEAREST);
setTexture(1, irr_driver->getRenderTargetTexture(RTT_LOG_LUMINANCE), GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST);
FullScreenShader::ToneMapShader::setUniforms(0, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
static void averageTexture(GLuint tex)
{
glActiveTexture(GL_TEXTURE0);
@ -533,6 +544,7 @@ static void averageTexture(GLuint tex)
static void computeLogLuminance(GLuint tex)
{
glViewport(0, 0, 1024, 1024);
glBindFramebuffer(GL_FRAMEBUFFER, irr_driver->getFBO(FBO_LOG_LUMINANCE));
glUseProgram(FullScreenShader::LogLuminanceShader::Program);
glBindVertexArray(FullScreenShader::LogLuminanceShader::vao);
@ -541,6 +553,7 @@ static void computeLogLuminance(GLuint tex)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
averageTexture(irr_driver->getRenderTargetTexture(RTT_LOG_LUMINANCE));
glViewport(0, 0, UserConfigParams::m_width, UserConfigParams::m_height);
}
void PostProcessing::applyMLAA()
@ -624,12 +637,17 @@ void PostProcessing::render()
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
/* computeLogLuminance(in_rtt);
toneMap(out_fbo, in_rtt);
std::swap(in_rtt, out_rtt);
std::swap(in_fbo, out_fbo);*/
PROFILER_PUSH_CPU_MARKER("- Bloom", 0xFF, 0x00, 0x00);
if (UserConfigParams::m_bloom)
{
glClear(GL_STENCIL_BUFFER_BIT);
glBindFramebuffer(GL_READ_FRAMEBUFFER, irr_driver->getFBO(FBO_COLORS));
glBindFramebuffer(GL_READ_FRAMEBUFFER, in_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, irr_driver->getFBO(FBO_BLOOM_1024));
glBlitFramebuffer(0, 0, UserConfigParams::m_width, UserConfigParams::m_height, 0, 0, 1024, 1024, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
@ -666,7 +684,7 @@ void PostProcessing::render()
glViewport(0, 0, UserConfigParams::m_width, UserConfigParams::m_height);
// Additively blend on top of tmp1
glBindFramebuffer(GL_FRAMEBUFFER, irr_driver->getFBO(FBO_COLORS));
glBindFramebuffer(GL_FRAMEBUFFER, in_fbo);
glEnable(GL_BLEND);
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
@ -760,8 +778,6 @@ void PostProcessing::render()
PROFILER_POP_CPU_MARKER();
}
computeLogLuminance(in_rtt);
glBindFramebuffer(GL_FRAMEBUFFER, out_fbo);
renderColorLevel(in_rtt);
std::swap(in_fbo, out_fbo);

View File

@ -86,7 +86,6 @@ RTT::RTT()
RenderTargetTextures[RTT_NORMAL_AND_DEPTH] = generateRTT(res, GL_RGBA16F, GL_RGBA, GL_FLOAT);
RenderTargetTextures[RTT_COLOR] = generateRTT(res, GL_RGBA16F, GL_BGRA, GL_FLOAT);
RenderTargetTextures[RTT_MLAA_COLORS] = generateRTT(res, GL_SRGB, GL_BGR, GL_UNSIGNED_BYTE);
RenderTargetTextures[RTT_LOG_LUMINANCE] = generateRTT(res, GL_R16F, GL_RED, GL_FLOAT);
RenderTargetTextures[RTT_SSAO] = generateRTT(half, GL_R16F, GL_RED, GL_FLOAT);
RenderTargetTextures[RTT_DISPLACE] = generateRTT(res, GL_RGBA16F, GL_BGRA, GL_FLOAT);
@ -105,6 +104,7 @@ RTT::RTT()
RenderTargetTextures[RTT_TMP_256] = generateRTT(shadowsize2, GL_RGBA16F, GL_BGR, GL_FLOAT);
RenderTargetTextures[RTT_BLOOM_128] = generateRTT(shadowsize3, GL_RGBA16F, GL_BGR, GL_FLOAT);
RenderTargetTextures[RTT_TMP_128] = generateRTT(shadowsize3, GL_RGBA16F, GL_BGR, GL_FLOAT);
RenderTargetTextures[RTT_LOG_LUMINANCE] = generateRTT(shadowsize0, GL_R16F, GL_RED, GL_FLOAT);
FrameBuffers[FBO_SSAO] = generateFBO(RenderTargetTextures[RTT_SSAO]);
// Clear this FBO to 1s so that if no SSAO is computed we can still use it.

View File

@ -317,6 +317,7 @@ void Shaders::loadShaders()
FullScreenShader::GodFadeShader::init();
FullScreenShader::GodRayShader::init();
FullScreenShader::LogLuminanceShader::init();
FullScreenShader::ToneMapShader::init();
FullScreenShader::MLAAColorEdgeDetectionSHader::init();
FullScreenShader::MLAABlendWeightSHader::init();
FullScreenShader::MLAAGatherSHader::init();
@ -2061,9 +2062,31 @@ namespace FullScreenShader
glUniform1i(FullScreenShader::BloomShader::uniform_texture, TU_tex);
}
GLuint ToneMapShader::Program;
GLuint ToneMapShader::uniform_tex;
GLuint ToneMapShader::uniform_logluminancetex;
GLuint ToneMapShader::vao;
void ToneMapShader::init()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getRGBfromCIEXxy.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getCIEXYZ.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/tonemap.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_logluminancetex = glGetUniformLocation(Program, "logluminancetex");
vao = createVAO(Program);
}
void ToneMapShader::setUniforms(unsigned TU_tex, unsigned TU_loglum)
{
glUniform1i(uniform_tex, TU_tex);
glUniform1i(uniform_logluminancetex, TU_loglum);
}
GLuint ColorLevelShader::Program;
GLuint ColorLevelShader::uniform_tex;
GLuint ColorLevelShader::uniform_logluminancetex;
GLuint ColorLevelShader::uniform_inlevel;
GLuint ColorLevelShader::uniform_outlevel;
GLuint ColorLevelShader::vao;
@ -2077,7 +2100,6 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getCIEXYZ.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/color_levels.frag").c_str());
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_logluminancetex = glGetUniformLocation(Program, "logluminancetex");
uniform_dtex = glGetUniformLocation(Program, "dtex");
uniform_inlevel = glGetUniformLocation(Program, "inlevel");
uniform_outlevel = glGetUniformLocation(Program, "outlevel");

View File

@ -520,11 +520,22 @@ public:
static void setUniforms(unsigned TU_tex);
};
class ToneMapShader
{
public:
static GLuint Program;
static GLuint uniform_tex, uniform_logluminancetex;
static GLuint vao;
static void init();
static void setUniforms(unsigned TU_tex, unsigned TU_logluminance);
};
class ColorLevelShader
{
public:
static GLuint Program;
static GLuint uniform_tex, uniform_invprojm, uniform_dtex, uniform_inlevel, uniform_outlevel, uniform_logluminancetex;
static GLuint uniform_tex, uniform_invprojm, uniform_dtex, uniform_inlevel, uniform_outlevel;
static GLuint vao;
static void init();