From 383c6c9fb279b220a5ac9fbffae9a180a8019cef Mon Sep 17 00:00:00 2001 From: dbp Date: Wed, 16 Nov 2016 15:44:56 +0300 Subject: [PATCH] 0028114: Visualization, Path tracing - Make path tracing mode interactive in high resolutions --- src/OpenGl/OpenGl_View.hxx | 2 +- src/OpenGl/OpenGl_View_Raytrace.cxx | 10 +++++----- src/Shaders/PathtraceBase.fs | 8 +++++++- src/Shaders/RaytraceRender.fs | 9 +++------ src/Shaders/Shaders_PathtraceBase_fs.pxx | 8 +++++++- src/Shaders/Shaders_RaytraceRender_fs.pxx | 9 +++------ 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/OpenGl/OpenGl_View.hxx b/src/OpenGl/OpenGl_View.hxx index 5d79813d8b..ba07b70f5e 100644 --- a/src/OpenGl/OpenGl_View.hxx +++ b/src/OpenGl/OpenGl_View.hxx @@ -573,7 +573,7 @@ protected: //! @name data types related to ray-tracing OpenGl_RT_uWinSizeY, // sampled frame params - OpenGl_RT_uSampleWeight, + OpenGl_RT_uAccumSamples, OpenGl_RT_uFrameRndSeed, // adaptive FSAA params diff --git a/src/OpenGl/OpenGl_View_Raytrace.cxx b/src/OpenGl/OpenGl_View_Raytrace.cxx index 0b7a463087..062e558664 100644 --- a/src/OpenGl/OpenGl_View_Raytrace.cxx +++ b/src/OpenGl/OpenGl_View_Raytrace.cxx @@ -1721,8 +1721,8 @@ Standard_Boolean OpenGl_View::initRaytraceResources (const Handle(OpenGl_Context myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] = aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeY"); - myUniformLocations[anIndex][OpenGl_RT_uSampleWeight] = - aShaderProgram->GetUniformLocation (theGlContext, "uSampleWeight"); + myUniformLocations[anIndex][OpenGl_RT_uAccumSamples] = + aShaderProgram->GetUniformLocation (theGlContext, "uAccumSamples"); myUniformLocations[anIndex][OpenGl_RT_uFrameRndSeed] = aShaderProgram->GetUniformLocation (theGlContext, "uFrameRndSeed"); @@ -2778,10 +2778,10 @@ Standard_Boolean OpenGl_View::runPathtrace (const Graphic3d_Camera::Projection // We upload tile offset texture each 4 frames in order // to minimize overhead of additional memory bandwidth. - // Adaptive sampling is starting after first 10 frames. + // Adaptive sampling is starting after first 30 frames. if (myAccumFrames % 4 == 0) { - myTileSampler.Upload (theGlContext, myRaytraceTileOffsetsTexture, myAccumFrames > 10); + myTileSampler.Upload (theGlContext, myRaytraceTileOffsetsTexture, myAccumFrames > 30); } } @@ -2828,7 +2828,7 @@ Standard_Boolean OpenGl_View::runPathtrace (const Graphic3d_Camera::Projection // Set frame accumulation weight myRaytraceProgram->SetUniform (theGlContext, - myUniformLocations[0][OpenGl_RT_uSampleWeight], 1.f / (myAccumFrames + 1)); + myUniformLocations[0][OpenGl_RT_uAccumSamples], myAccumFrames); // Set random number generator seed myRaytraceProgram->SetUniform (theGlContext, diff --git a/src/Shaders/PathtraceBase.fs b/src/Shaders/PathtraceBase.fs index 68bdad97ef..98400ccc49 100644 --- a/src/Shaders/PathtraceBase.fs +++ b/src/Shaders/PathtraceBase.fs @@ -12,6 +12,9 @@ #ifdef PATH_TRACING +//! Number of previously rendered frames. +uniform int uAccumSamples; + /////////////////////////////////////////////////////////////////////////////////////// // Specific data types @@ -677,6 +680,9 @@ vec3 IntersectLight (in SRay theRay, in int theDepth, in float theHitDistance, o // Enables expiremental russian roulette sampling #define RUSSIAN_ROULETTE +//! Frame step to increase number of bounces +#define FRAME_STEP 5 + //======================================================================= // function : PathTrace // purpose : Calculates radiance along the given ray @@ -832,7 +838,7 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse) aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f); #endif - if (RandFloat() > aSurvive || all (lessThanEqual (aThroughput, MIN_THROUGHPUT))) + if (RandFloat() > aSurvive || all (lessThan (aThroughput, MIN_THROUGHPUT)) || aDepth >= uAccumSamples / FRAME_STEP + step (1.f / M_PI, aImpPDF)) { aDepth = INVALID_BOUNCES; // terminate path } diff --git a/src/Shaders/RaytraceRender.fs b/src/Shaders/RaytraceRender.fs index 00263f3763..009270ecba 100644 --- a/src/Shaders/RaytraceRender.fs +++ b/src/Shaders/RaytraceRender.fs @@ -9,10 +9,7 @@ uniform int uFrameRndSeed; uniform int uBlockedRngEnabled; #ifndef ADAPTIVE_SAMPLING - //! Weight of current frame related to accumulated samples. - uniform float uSampleWeight; - - //! Input accumulated image. + //! Input image with previously accumulated samples. uniform sampler2D uAccumTexture; #endif @@ -95,13 +92,13 @@ void main (void) #else - if (uSampleWeight >= 1.f) + if (uAccumSamples == 0) { OutColor = aColor; } else { - OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, uSampleWeight); + OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, 1.f / (uAccumSamples + 1)); } #endif // ADAPTIVE_SAMPLING diff --git a/src/Shaders/Shaders_PathtraceBase_fs.pxx b/src/Shaders/Shaders_PathtraceBase_fs.pxx index 1bb6832190..527a49e7f2 100644 --- a/src/Shaders/Shaders_PathtraceBase_fs.pxx +++ b/src/Shaders/Shaders_PathtraceBase_fs.pxx @@ -15,6 +15,9 @@ static const char Shaders_PathtraceBase_fs[] = "\n" "#ifdef PATH_TRACING\n" "\n" + "//! Number of previously rendered frames.\n" + "uniform int uAccumSamples;\n" + "\n" "///////////////////////////////////////////////////////////////////////////////////////\n" "// Specific data types\n" "\n" @@ -680,6 +683,9 @@ static const char Shaders_PathtraceBase_fs[] = "// Enables expiremental russian roulette sampling\n" "#define RUSSIAN_ROULETTE\n" "\n" + "//! Frame step to increase number of bounces\n" + "#define FRAME_STEP 5\n" + "\n" "//=======================================================================\n" "// function : PathTrace\n" "// purpose : Calculates radiance along the given ray\n" @@ -835,7 +841,7 @@ static const char Shaders_PathtraceBase_fs[] = " aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f);\n" "#endif\n" "\n" - " if (RandFloat() > aSurvive || all (lessThanEqual (aThroughput, MIN_THROUGHPUT)))\n" + " if (RandFloat() > aSurvive || all (lessThan (aThroughput, MIN_THROUGHPUT)) || aDepth >= uAccumSamples / FRAME_STEP + step (1.f / M_PI, aImpPDF))\n" " {\n" " aDepth = INVALID_BOUNCES; // terminate path\n" " }\n" diff --git a/src/Shaders/Shaders_RaytraceRender_fs.pxx b/src/Shaders/Shaders_RaytraceRender_fs.pxx index a3a04e0dd3..e32c754690 100644 --- a/src/Shaders/Shaders_RaytraceRender_fs.pxx +++ b/src/Shaders/Shaders_RaytraceRender_fs.pxx @@ -12,10 +12,7 @@ static const char Shaders_RaytraceRender_fs[] = "uniform int uBlockedRngEnabled;\n" "\n" "#ifndef ADAPTIVE_SAMPLING\n" - " //! Weight of current frame related to accumulated samples.\n" - " uniform float uSampleWeight;\n" - "\n" - " //! Input accumulated image.\n" + " //! Input image with previously accumulated samples.\n" " uniform sampler2D uAccumTexture;\n" "#endif\n" "\n" @@ -98,13 +95,13 @@ static const char Shaders_RaytraceRender_fs[] = "\n" "#else\n" "\n" - " if (uSampleWeight >= 1.f)\n" + " if (uAccumSamples == 0)\n" " {\n" " OutColor = aColor;\n" " }\n" " else\n" " {\n" - " OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, uSampleWeight);\n" + " OutColor = mix (texture2D (uAccumTexture, vPixel), aColor, 1.f / (uAccumSamples + 1));\n" " }\n" "\n" "#endif // ADAPTIVE_SAMPLING\n" -- 2.39.5