From b56e3212183be90e35ca1cc3a7d0ffcde670654c Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Sun, 17 Sep 2017 23:03:39 -0400 Subject: [PATCH] Avoid calculating whitepoint for each sample for HDR (#722) * Avoid calculating whitepoint for each sample for HDR --- .../Common/MatDefs/Post/ToneMap.frag | 54 +++++++++++-------- .../Common/MatDefs/Post/ToneMap.j3md | 4 +- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.frag b/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.frag index a5a439ada..673be8c02 100644 --- a/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.frag +++ b/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.frag @@ -1,12 +1,7 @@ +#extension GL_ARB_texture_multisample : enable #import "Common/ShaderLib/GLSLCompat.glsllib" -#import "Common/ShaderLib/MultiSample.glsllib" -uniform COLORTEXTURE m_Texture; -uniform vec3 m_WhitePoint; - -varying vec2 texCoord; - -vec3 FilmicCurve(in vec3 x){ +vec3 FilmicCurve(in vec3 x) { const float A = 0.22; const float B = 0.30; const float C = 0.10; @@ -19,27 +14,40 @@ vec3 FilmicCurve(in vec3 x){ // whitePoint should be 11.2 -vec3 ToneMap_Filmic(vec3 color, vec3 whitePoint){ +vec3 ToneMap_Filmic(vec3 color, vec3 whitePoint) { return FilmicCurve(color) / FilmicCurve(whitePoint); } -vec4 tonemap(int i) { - - vec4 texVal = fetchTextureSample(m_Texture, texCoord, i); - vec3 toneMapped = ToneMap_Filmic(texVal.rgb, m_WhitePoint); - - return vec4(toneMapped, texVal.a); +uniform vec3 m_WhitePoint; +varying vec2 texCoord; + +#ifdef NUM_SAMPLES + +uniform sampler2DMS m_Texture; + +vec4 ToneMap_TextureFilmic() { + ivec2 iTexC = ivec2(texCoord * vec2(textureSize(m_Texture))); + vec4 color = vec4(0.0); + for (int i = 0; i < NUM_SAMPLES; i++) { + vec4 hdrColor = texelFetch(m_Texture, iTexC, i); + vec3 ldrColor = FilmicCurve(hdrColor.rgb); + color += vec4(ldrColor, hdrColor.a); + } + color.rgb /= FilmicCurve(m_WhitePoint); + return color / float(NUM_SAMPLES); } +#else + +uniform sampler2D m_Texture; + +vec4 ToneMap_TextureFilmic() { + vec4 texVal = texture2D(m_Texture, texCoord); + return vec4(ToneMap_Filmic(texVal.rgb, m_WhitePoint), texVal.a); +} + +#endif void main() { - #ifdef RESOLVE_MS - vec4 color = vec4(0.0); - for (int i = 0; i < m_NumSamples; i++){ - color += tonemap(i); - } - gl_FragColor = color / m_NumSamples; - #else - gl_FragColor = tonemap(0); - #endif + gl_FragColor = ToneMap_TextureFilmic(); } diff --git a/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.j3md b/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.j3md index a77cc36a9..7c1eb6673 100644 --- a/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.j3md +++ b/jme3-effects/src/main/resources/Common/MatDefs/Post/ToneMap.j3md @@ -15,9 +15,7 @@ MaterialDef Default GUI { } Defines { - RESOLVE_MS : NumSamples + NUM_SAMPLES : NumSamples } - } - } \ No newline at end of file