From 881d0b9954ac952715641516a6fd444fa432355b Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Fri, 28 Mar 2014 23:30:26 +0100 Subject: [PATCH] Factorize luminance computation. --- data/shaders/bloom.frag | 4 ++-- data/shaders/utils/getCIEXYZ.frag | 17 +++++++++++++++++ src/graphics/shaders.cpp | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 data/shaders/utils/getCIEXYZ.frag diff --git a/data/shaders/bloom.frag b/data/shaders/bloom.frag index 3be3cb60f..2bccf2dfc 100644 --- a/data/shaders/bloom.frag +++ b/data/shaders/bloom.frag @@ -9,12 +9,12 @@ varying vec2 uv; #define FragColor gl_FragColor #endif +vec3 getCIEYxy(vec3 rgbColor); void main() { - vec3 weights = vec3(0.2126, 0.7152, 0.0722); // ITU-R BT. 709 vec3 col = texture(tex, uv).xyz; - float luma = dot(weights, col); + float luma = getCIEYxy(col).x; col *= smoothstep(1., 2., luma); diff --git a/data/shaders/utils/getCIEXYZ.frag b/data/shaders/utils/getCIEXYZ.frag new file mode 100644 index 000000000..f4f5d2bb5 --- /dev/null +++ b/data/shaders/utils/getCIEXYZ.frag @@ -0,0 +1,17 @@ +// Using numerical value from here +// http://content.gpwiki.org/index.php/D3DBook:High-Dynamic_Range_Rendering + +vec3 getCIEYxy(vec3 rgbColor) +{ + // convert rgb to srgb + vec3 sRGBColor = vec3(pow(rgbColor.x, 1. / 2.2), pow(rgbColor.y, 1. / 2.2), pow(rgbColor.z, 1. / 2.2)); + + mat3 sRGB2XYZ = mat3( + vec3(.4125, .2126, .0193), + vec3(.3576, .7152, .1192), + vec3(.1805, .0722, .9505)); + + vec3 XYZ = sRGB2XYZ * sRGBColor; + float sum = dot(vec3(1.), XYZ); + return vec3(XYZ.y, XYZ.xy / sum); +} \ No newline at end of file diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index eac01581d..60d867ff1 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -1768,6 +1768,7 @@ namespace FullScreenShader { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getCIEXYZ.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/bloom.frag").c_str()); uniform_texture = glGetUniformLocation(Program, "tex"); uniform_low = glGetUniformLocation(Program, "low");