diff --git a/include/openspace/rendering/framebufferrenderer.h b/include/openspace/rendering/framebufferrenderer.h index f5b90f3b36..efa772a800 100644 --- a/include/openspace/rendering/framebufferrenderer.h +++ b/include/openspace/rendering/framebufferrenderer.h @@ -227,9 +227,9 @@ private: std::unique_ptr _resolveProgram; UniformCache(mainColorTexture, blackoutFactor, nAaSamples) _uniformCache; - UniformCache(deferredResultsTexture, blackoutFactor, hdrExposure, gamma, + UniformCache(hdrFeedingTexture, blackoutFactor, hdrExposure, gamma, toneMapOperator, aveLum, maxWhite, Hue, Saturation, Value, - Lightness, colorSpace) _hdrUniformCache; + Lightness, colorSpace, nAaSamples) _hdrUniformCache; UniformCache(renderedImage, bloomImage, bloomOrigFactor, bloomNewFactor) _bloomUniformCache; diff --git a/modules/atmosphere/shaders/atmosphere_deferred_fs.glsl b/modules/atmosphere/shaders/atmosphere_deferred_fs.glsl index d2a5999144..53da2d939d 100644 --- a/modules/atmosphere/shaders/atmosphere_deferred_fs.glsl +++ b/modules/atmosphere/shaders/atmosphere_deferred_fs.glsl @@ -61,7 +61,6 @@ #include "floatoperations.glsl" -#include "hdr.glsl" #include "atmosphere_common.glsl" out vec4 renderTarget; @@ -71,11 +70,6 @@ uniform int nAaSamples; uniform double msaaSamplePatter[48]; uniform int cullAtmosphere; -// The following uniforms are -// set into the current Renderer -uniform bool firstPaint; -uniform float atmExposure; - uniform sampler2D irradianceTexture; uniform sampler3D inscatterTexture; uniform sampler2DMS mainPositionTexture; @@ -90,8 +84,6 @@ uniform dmat4 dSgctProjectionToModelTransformMatrix; uniform dvec4 dCamPosObj; uniform dvec3 sunDirectionObj; -uniform float blackoutFactor; - /******************************************************************************* ***** ALL CALCULATIONS FOR ECLIPSE ARE IN METERS AND IN WORLD SPACE SYSTEM **** *******************************************************************************/ @@ -315,7 +307,7 @@ vec3 inscatterRadiance(inout vec3 x, inout float t, inout float irradianceFactor // through the atmosphere. If this ray hits something inside the atmosphere, // we will subtract the attenuated scattering light from that path in the // current path. - vec4 inscatterRadiance = max(texture4D(inscatterTexture, r, mu, muSun, nu), 0.0); + vec4 inscatterRadiance = max(texture4D(inscatterTexture, r, mu, muSun, nu), 0.0); // After removing the initial path from camera pos to top of atmosphere (for an // observer in the space) we test if the light ray is hitting the atmosphere @@ -363,7 +355,7 @@ vec3 inscatterRadiance(inout vec3 x, inout float t, inout float irradianceFactor if (abs(mu - muHorizon) < INTERPOLATION_EPS) { // We want an interpolation value close to 1/2, so the // contribution of each radiance value is almost the same - // or it has a havey weight if from above or below horizon + // or it has a heavy weight if from above or below horizon float interpolationValue = ((mu - muHorizon) + INTERPOLATION_EPS) / (2.0f * INTERPOLATION_EPS); //float t2 = t * t; @@ -429,7 +421,11 @@ vec3 inscatterRadiance(inout vec3 x, inout float t, inout float irradianceFactor return finalScatteringRadiance; } else { //return ((r-Rg) * invRtMinusRg)*spaceColor.rgb + finalScatteringRadiance; - return attenuation * spaceColor.rgb + finalScatteringRadiance; + //return attenuation * spaceColor.rgb + finalScatteringRadiance; + // JCC: Review next step. I don't like it. + return attenuation * spaceColor.rgb + + 1.0 * attenuation * finalScatteringRadiance + + (vec3(1.0) - attenuation) * finalScatteringRadiance; } } @@ -536,7 +532,8 @@ vec3 sunColor(const vec3 x, const float t, const vec3 v, const vec3 s, const flo vec3(0.0f) : transmittanceLUT(r, mu)) : vec3(1.0f); // JCC: Change this function to a impostor texture with gaussian decay color weighted // by tge sunRadiance, transmittance and irradianceColor (11/03/2017) - float sunFinalColor = step(cos(M_PI / 650.0f), dot(v, s)) * sunRadiance * (1.0f- irradianceFactor); + float sunFinalColor = smoothstep(cos(M_PI / 500.0f), cos(M_PI / 900.0f), dot(v, s)) * + sunRadiance * (1.0f - irradianceFactor); return transmittance * sunFinalColor; } @@ -546,7 +543,6 @@ void main() { if (cullAtmosphere == 0) { vec4 atmosphereFinalColor = vec4(0.0f); - vec4 backgroundFinalColor = vec4(0.0f); int nSamples = 1; // First we determine if the pixel is complex (different fragments on it) @@ -570,7 +566,8 @@ void main() { for (int i = 0; i < nSamples; i++) { // Color from G-Buffer //vec4 color = texelFetch(mainColorTexture, fragCoords, i); - vec4 color = vec4(-log2(vec3(1.0) - colorArray[i].rgb), colorArray[i].a); + //vec4 color = vec4(-log2(vec3(1.0) - colorArray[i].rgb), colorArray[i].a); + vec4 color = colorArray[i]; // Ray in object space dRay ray; dvec4 planetPositionObjectCoords = dvec4(0.0); @@ -636,8 +633,7 @@ void main() { if (dot(position.xyz, vec3(1.0)) > 0.0 && (pixelDepth < offset)) { // ATM Occluded - Something in fron of ATM. - //atmosphereFinalColor += vec4(HDR(color.xyz * backgroundConstant, atmExposure), color.a); - atmosphereFinalColor += vec4(color.xyz, color.a); + atmosphereFinalColor += color; } else { // Following paper nomenclature double t = offset; @@ -686,44 +682,27 @@ void main() { } // Final Color of ATM plus terrain: - //vec4 finalRadiance = vec4(HDR(inscatterColor + groundColorV + sunColorV, atmExposure), 1.0); vec4 finalRadiance = vec4(inscatterColor + groundColorV + sunColorV, 1.0); atmosphereFinalColor += finalRadiance; } } else { // no intersection - //atmosphereFinalColor += vec4(HDR(color.xyz * backgroundConstant, atmExposure), color.a); - //atmosphereFinalColor += vec4(color.xyz * backgroundConstant, color.a); - //backgroundFinalColor += color; - discard; + // Buffer color + atmosphereFinalColor += color; } } renderTarget = atmosphereFinalColor / float(nSamples); - - // renderTarget = vec4( - // (atmosphereFinalColor.xyz + (backgroundFinalColor.xyz * backgroundConstant)) / float(nSamples), - // (atmosphereFinalColor.a + backgroundFinalColor.a) / float(nSamples) - // ); } else { // culling - if (firstPaint) { - vec4 bColor = vec4(0.0f); - for (int f = 0; f < nAaSamples; f++) { - bColor += texelFetch(mainColorTexture, fragCoords, f); - } - bColor /= float(nAaSamples); - //renderTarget = vec4(HDR(bColor.xyz * backgroundConstant, atmExposure), bColor.a); - //renderTarget = vec4(bColor.xyz * backgroundConstant, bColor.a); - renderTarget = vec4(-log2(vec3(1.0) - bColor.rgb), bColor.a); - //renderTarget = bColor; - - } - else { - //renderTarget = vec4(vec3(1.0, 0.0, 0.0), 1.0); - discard; + vec4 bColor = vec4(0.0f); + for (int f = 0; f < nAaSamples; f++) { + bColor += texelFetch(mainColorTexture, fragCoords, f); } + bColor /= float(nAaSamples); + //renderTarget = vec4(-log2(vec3(1.0) - bColor.rgb), bColor.a); + renderTarget = bColor; } } diff --git a/shaders/framebuffer/hdrAndFiltering.frag b/shaders/framebuffer/hdrAndFiltering.frag index afa8de682f..3996ea5e44 100644 --- a/shaders/framebuffer/hdrAndFiltering.frag +++ b/shaders/framebuffer/hdrAndFiltering.frag @@ -43,53 +43,64 @@ uniform float Value; uniform float Lightness; uniform int toneMapOperator; uniform uint colorSpace; +uniform int nAaSamples; -uniform sampler2D deferredResultsTexture; +//uniform sampler2D hdrFeedingTexture; +uniform sampler2DMS hdrFeedingTexture; in vec2 texCoord; +// JCC: Change the next function to work with a MSAA texture vec4 adaptiveToneMap() { - int i; - float lum[25]; - vec2 tex_scale = vec2(1.0) / textureSize(deferredResultsTexture, 0); + // int i; + // float lum[25]; + // // Non MSAAA variant: + // //vec2 tex_scale = vec2(1.0) / textureSize(hdrFeedingTexture, 0); + // vec2 tex_scale = vec2(1.0) / textureSize(hdrFeedingTexture); - for (i = 0; i < 25; i++) - { - vec2 tc = (gl_FragCoord.xy + 3.5 * vec2(i % 5 - 2, i / 5 - 2)); - vec3 col = texture(deferredResultsTexture, tc * tex_scale).rgb; - lum[i] = dot(col, vec3(0.3, 0.59, 0.11)); - } + // for (i = 0; i < 25; i++) + // { + // vec2 tc = (gl_FragCoord.xy + 3.5 * vec2(i % 5 - 2, i / 5 - 2)); + // vec3 col = texture(hdrFeedingTexture, tc * tex_scale).rgb; + // lum[i] = dot(col, vec3(0.3, 0.59, 0.11)); + // } - // Calculate weighted color of region - vec3 vColor = texelFetch(deferredResultsTexture, ivec2(gl_FragCoord.xy), 0).rgb; + // // Calculate weighted color of region + // vec3 vColor = texelFetch(hdrFeedingTexture, ivec2(gl_FragCoord.xy), 0).rgb; - float kernelLuminance = ( - (1.0 * (lum[0] + lum[4] + lum[20] + lum[24])) + - (4.0 * (lum[1] + lum[3] + lum[5] + lum[9] + - lum[15] + lum[19] + lum[21] + lum[23])) + - (7.0 * (lum[2] + lum[10] + lum[14] + lum[22])) + - (16.0 * (lum[6] + lum[8] + lum[16] + lum[18])) + - (26.0 * (lum[7] + lum[11] + lum[13] + lum[17])) + - (41.0 * lum[12]) - ) / 273.0; + // float kernelLuminance = ( + // (1.0 * (lum[0] + lum[4] + lum[20] + lum[24])) + + // (4.0 * (lum[1] + lum[3] + lum[5] + lum[9] + + // lum[15] + lum[19] + lum[21] + lum[23])) + + // (7.0 * (lum[2] + lum[10] + lum[14] + lum[22])) + + // (16.0 * (lum[6] + lum[8] + lum[16] + lum[18])) + + // (26.0 * (lum[7] + lum[11] + lum[13] + lum[17])) + + // (41.0 * lum[12]) + // ) / 273.0; - // Compute the corresponding exposure - float exposure = sqrt(8.0 / (kernelLuminance + 0.25)); + // // Compute the corresponding exposure + // float exposure = sqrt(8.0 / (kernelLuminance + 0.25)); - // Apply the exposure to this texel - vec4 fColor; - fColor.rgb = 1.0 - exp2(-vColor * exposure); - fColor.a = 1.0f; + // // Apply the exposure to this texel + // vec4 fColor; + // fColor.rgb = 1.0 - exp2(-vColor * exposure); + // fColor.a = 1.0f; - return fColor; + // return fColor; + return vec4(1.0, 0.0, 0.0, 1.0); } void main() { vec4 color = vec4(0.0); - color = texture(deferredResultsTexture, texCoord); - //color = texelFetch(deferredResultsTexture, ivec2(gl_FragCoord.xy), 0); - color.a *= blackoutFactor; + + //color = texture(hdrFeedingTexture, texCoord); + for (int i = 0; i < nAaSamples; i++) { + color += texelFetch(hdrFeedingTexture, ivec2(gl_FragCoord), i); + } + + color /= nAaSamples; + color.rgb *= blackoutFactor; vec3 tColor = vec3(0.0); if (toneMapOperator == EXPONENTIAL) { diff --git a/shaders/framebuffer/resolveframebuffer.frag b/shaders/framebuffer/resolveframebuffer.frag index e3ff098396..39f2946dec 100644 --- a/shaders/framebuffer/resolveframebuffer.frag +++ b/shaders/framebuffer/resolveframebuffer.frag @@ -31,13 +31,13 @@ uniform int nAaSamples; uniform sampler2DMS mainColorTexture; void main() { - vec4 color = vec4(0.0); - for (int i = 0; i < nAaSamples; i++) { - color += texelFetch(mainColorTexture, ivec2(gl_FragCoord), i); - } + vec4 color = vec4(0.0); + for (int i = 0; i < nAaSamples; i++) { + color += texelFetch(mainColorTexture, ivec2(gl_FragCoord), i); + } - color /= nAaSamples; - color.rgb *= blackoutFactor; + color /= nAaSamples; + color.rgb *= blackoutFactor; - finalColor = vec4(color.rgb, 1.0); + finalColor = vec4(color.rgb, 1.0); } diff --git a/src/rendering/framebufferrenderer.cpp b/src/rendering/framebufferrenderer.cpp index 7998fedcd5..f97703fb2e 100644 --- a/src/rendering/framebufferrenderer.cpp +++ b/src/rendering/framebufferrenderer.cpp @@ -55,10 +55,10 @@ namespace { "mainColorTexture", "blackoutFactor", "nAaSamples" }; - constexpr const std::array HDRUniformNames = { - "deferredResultsTexture", "blackoutFactor", "hdrExposure", "gamma", + constexpr const std::array HDRUniformNames = { + "hdrFeedingTexture", "blackoutFactor", "hdrExposure", "gamma", "toneMapOperator", "aveLum", "maxWhite", "Hue", "Saturation", "Value", - "Lightness", "colorSpace" + "Lightness", "colorSpace", "nAaSamples" }; constexpr const std::array BloomUniformNames = { @@ -528,8 +528,7 @@ void FramebufferRenderer::resolveMSAA(float blackoutFactor) { ghoul::opengl::TextureUnit mainColorTextureUnit; mainColorTextureUnit.activate(); - //glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _gBuffers._mainColorTexture); - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _pingPongBuffers.colorTexture[_pingPongIndex]); + glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _gBuffers._mainColorTexture); _resolveProgram->setUniform(_uniformCache.mainColorTexture, mainColorTextureUnit); _resolveProgram->setUniform(_uniformCache.blackoutFactor, blackoutFactor); _resolveProgram->setUniform(_uniformCache.nAaSamples, _nAaSamples); @@ -551,6 +550,7 @@ void FramebufferRenderer::applyTMO(float blackoutFactor) { "FramebufferRenderer::render::TMO_GLOBAL" ); } + // JCC: Mudar aqui para que a entrada do TMO seja MSAA averageLuminaceInFB = computeBufferAveLuminanceGPU(); if (std::isnan(averageLuminaceInFB)) { averageLuminaceInFB = 1000.0; @@ -565,6 +565,7 @@ void FramebufferRenderer::applyTMO(float blackoutFactor) { "FramebufferRenderer::render::TMO_Mipmapping" ); } + // JCC: Mudar aqui para que a entrada do TMO seja MSAA if (_bloomEnabled) { computeMipMappingFromHDRBuffer(_bloomBuffers._bloomTexture[2]); } @@ -584,14 +585,18 @@ void FramebufferRenderer::applyTMO(float blackoutFactor) { ghoul::opengl::TextureUnit hdrFeedingTextureUnit; hdrFeedingTextureUnit.activate(); if (_bloomEnabled) { + // JCC: The next texture must be a MSAA texture glBindTexture(GL_TEXTURE_2D, _bloomBuffers._bloomTexture[2]); } else { - glBindTexture(GL_TEXTURE_2D, _hdrBuffers._hdrFilteringTexture); + glBindTexture( + GL_TEXTURE_2D_MULTISAMPLE, + _pingPongBuffers.colorTexture[_pingPongIndex] + ); } _hdrFilteringProgram->setUniform( - _hdrUniformCache.deferredResultsTexture, + _hdrUniformCache.hdrFeedingTexture, hdrFeedingTextureUnit ); @@ -607,6 +612,7 @@ void FramebufferRenderer::applyTMO(float blackoutFactor) { _hdrFilteringProgram->setUniform(_hdrUniformCache.Value, _value); _hdrFilteringProgram->setUniform(_hdrUniformCache.Lightness, _lightness); _hdrFilteringProgram->setUniform(_hdrUniformCache.colorSpace, _colorSpace); + _hdrFilteringProgram->setUniform(_hdrUniformCache.nAaSamples, _nAaSamples); glBindVertexArray(_screenQuad); @@ -762,13 +768,13 @@ void FramebufferRenderer::applyBloomFilter() { // the _bloomBuffers._bloomTexture[2] texture (JCC: That can be // done by blending operation (ONE)) { - ghoul::opengl::TextureUnit deferredResultsTextureUnit; - deferredResultsTextureUnit.activate(); + ghoul::opengl::TextureUnit hdrFeedingTextureUnit; + hdrFeedingTextureUnit.activate(); // Original buffer will be summed to the bloom result glBindTexture(GL_TEXTURE_2D, _hdrBuffers._hdrFilteringTexture); _bloomResolveProgram->setUniform( _bloomUniformCache.renderedImage, - deferredResultsTextureUnit + hdrFeedingTextureUnit ); ghoul::opengl::TextureUnit bloomTextureUnit; @@ -1761,6 +1767,10 @@ void FramebufferRenderer::updateMSAASamplingPattern() { } void FramebufferRenderer::render(Scene* scene, Camera* camera, float blackoutFactor) { + // Reset ping pong texture rendering + _pingPongIndex = 0; + + // Measurements cache variable const bool doPerformanceMeasurements = global::performanceManager.isEnabled(); std::unique_ptr perf; @@ -1819,34 +1829,38 @@ void FramebufferRenderer::render(Scene* scene, Camera* camera, float blackoutFac //glDrawBuffers(1, ColorAttachment0Array); //glClear(GL_COLOR_BUFFER_BIT); - glBindFramebuffer(GL_FRAMEBUFFER, _pingPongBuffers.framebuffer); - glDrawBuffers(1, &ColorAttachment01Array[_pingPongIndex]); - glEnablei(GL_BLEND, 0); - glDisablei(GL_BLEND, 1); - glDisablei(GL_BLEND, 2); - glDisablei(GL_BLEND, 3); - - // Run Deferred Tasks (resolve step is executed together with these tasks) - { - std::unique_ptr perfInternal; - if (doPerformanceMeasurements) { - perfInternal = std::make_unique( - "FramebufferRenderer::render::deferredTasks" - ); - } - performDeferredTasks(tasks.deferredcasterTasks, blackoutFactor); - } - + // If no Deferred Task are prensent, the resolve step // is executed in a separated step if (tasks.deferredcasterTasks.empty()) { resolveMSAA(blackoutFactor); } + else { + // We use ping pong rendering in order to be able to + // render multiple deferred tasks at same time (e.g. + // more than 1 ATM being seen at once) + glBindFramebuffer(GL_FRAMEBUFFER, _pingPongBuffers.framebuffer); + glDrawBuffers(1, &ColorAttachment01Array[_pingPongIndex]); + // JCC: next commands should be in the cache.... + glEnablei(GL_BLEND, 0); + glDisablei(GL_BLEND, 1); + glDisablei(GL_BLEND, 2); + glDisablei(GL_BLEND, 3); + + std::unique_ptr perfInternal; + if (doPerformanceMeasurements) { + perfInternal = std::make_unique( + "FramebufferRenderer::render::deferredTasks" + ); + } + performDeferredTasks(tasks.deferredcasterTasks, blackoutFactor); + } // Disabling depth test for filtering and hdr glDisable(GL_DEPTH_TEST); + // JCC: Change bloom to work in a MSAA environment if (_bloomEnabled) { std::unique_ptr perfInternal; if (doPerformanceMeasurements) { @@ -1861,37 +1875,12 @@ void FramebufferRenderer::render(Scene* scene, Camera* camera, float blackoutFac } // When applying the TMO, the result is saved to the default FBO to be displayed - // by the Operating System + // by the Operating System. Also, the resolve procedure is executed in this step. glBindFramebuffer(GL_FRAMEBUFFER, _osDefaultGLState.defaultFBO); glViewport(0, 0, _resolution.x, _resolution.y); - - // JCC: AQUI -> PRECISO FAZER UM RESOLVE COM HDR JUNTO PARA ECONOMIZAR TEMPO - // - // Apply the selected TMO on the results applyTMO(blackoutFactor); - // JCC: ADJUST HERE SO THE HDR IS TOGETHER WITH THE RESOLVE: - // As a last step, copy the rendering to the final default framebuffer. - /*_resolveProgram->activate(); - - ghoul::opengl::TextureUnit mainColorTextureUnit; - mainColorTextureUnit.activate(); - - glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _pingPongBuffers[_pingPongIndex].colorTexture); - _resolveProgram->setUniform(_uniformCache.mainColorTexture, mainColorTextureUnit); - _resolveProgram->setUniform(_uniformCache.blackoutFactor, blackoutFactor); - _resolveProgram->setUniform(_uniformCache.nAaSamples, _nAaSamples); - _resolveProgram->setUniform(_uniformCache.exposure, _hdrExposure); - _resolveProgram->setUniform(_uniformCache.gamma, _gamma); - _resolveProgram->setUniform(_uniformCache.nAaSamples, _nAaSamples); - - glBindVertexArray(_screenQuad); - glDrawArrays(GL_TRIANGLES, 0, 6); - glBindVertexArray(0); - - _resolveProgram->deactivate();*/ - //================================ // Adjusting color and brightness @@ -1993,9 +1982,7 @@ void FramebufferRenderer::performDeferredTasks( const std::vector& tasks, float blackoutFactor ) -{ - bool firstPaint = true; - +{ for (const DeferredcasterTask& deferredcasterTask : tasks) { Deferredcaster* deferredcaster = deferredcasterTask.deferredcaster; @@ -2050,8 +2037,6 @@ void FramebufferRenderer::performDeferredTasks( // 48 = 16 samples * 3 coords deferredcastProgram->setUniform("msaaSamplePatter", &_mSAAPattern[0], 48); - deferredcastProgram->setUniform("firstPaint", firstPaint); - deferredcastProgram->setUniform("atmExposure", _hdrExposure); deferredcaster->preRaycast( deferredcasterTask.renderData, _deferredcastData[deferredcaster], @@ -2075,11 +2060,6 @@ void FramebufferRenderer::performDeferredTasks( ); deferredcastProgram->deactivate(); - - // JCC: No neeeded anymore - /*if (firstPaint) { - firstPaint = false; - }*/ } else { LWARNING(