Parcourir la source

Clean up and polish for SSRR

BearishSun il y a 8 ans
Parent
commit
bdcdee2641

+ 4 - 0
Data/Raw/Engine/DataList.json

@@ -361,6 +361,10 @@
         {
             "Path": "DebugDraw.bsl",
             "UUID": "6d3eb44e-b7e9-4c5a-8f8c-e5695607ca13"
+        },
+        {
+            "Path": "ClearStencilBits.bsl",
+            "UUID": "e123138f-af24-422f-9301-f71113fcd282"
         }
     ],
     "Skin": [

+ 25 - 4
Data/Raw/Engine/Includes/TemporalResolve.bslinc

@@ -16,6 +16,12 @@ mixin TemporalResolve
 		#ifndef MSAA
 			#define MSAA 0
 		#endif
+		
+		// Only relevant when MSAA is enabled. When disabled color textures are assumed to be non-MSAA. When
+		// enabled all textures are assumed to be MSAA.
+		#ifndef MSAA_COLOR
+			#define MSAA_COLOR MSAA
+		#endif
 	
 		// 0 - System will use the velocity of the current pixel
 		// 1 - System will search 4 neighbor pixels in + pattern, and choose the velocity of the pixel nearest 
@@ -107,15 +113,30 @@ mixin TemporalResolve
 		////////////////////////// HELPER MACROS /////////////////////////
 		#if MSAA
 			#define _TEX2D(n) Texture2DMS n
+			#if MSAA_COLOR
+				#define _TEXCOLOR(n) Texture2DMS n
+			#else
+				#define _TEXCOLOR(n) Texture2D n, SamplerState n##SampState, float2 n##TexelSize
+			#endif
+			
 			#define _PTEX2D(n) n
 			#define _SAMPLE(n, uv) n.Load((int2)uv, sampleIdx)
 			#define _SAMPLEOFF(n, uv, offset) n.Load((int2)(uv) + offset)
+			
+			#if MSAA_COLOR
+				#define _SAMPLECOL(n, uv, offset) _SAMPLEOFF(n, uv, offset)
+			#else
+				#define _SAMPLECOL(n, uv, offset) n.Sample(n##SampState, uv, offset)
+			#endif
+			
 			#define _PIXSIZE(n) int2(1, 1)
 		#else
 			#define _TEX2D(n) Texture2D n, SamplerState n##SampState, float2 n##TexelSize
+			#define _TEXCOLOR(n) _TEX2D(n)
 			#define _PTEX2D(n) n, n##SampState, n##TexelSize
 			#define _SAMPLE(n, uv) n.Sample(n##SampState, uv)
 			#define _SAMPLEOFF(n, uv, offset) n.Sample(n##SampState, uv, offset)
+			#define _SAMPLECOL(n, uv, offset) _SAMPLEOFF(n, uv, offset)
 			#define _PIXSIZE(n) n##TexelSize
 		#endif
 		
@@ -222,9 +243,9 @@ mixin TemporalResolve
 		
 		// Automatically convert from/to YCoCg space, if enabled
 		#if TEMPORAL_YCOCG
-			#define _SAMPLE_COLOR(n, uv, offset) _TONEMAP_COLOR(RGBToYCoCg(_SAMPLEOFF(n, uv, offset).rgb))
+			#define _SAMPLE_COLOR(n, uv, offset) _TONEMAP_COLOR(RGBToYCoCg(_SAMPLECOL(n, uv, offset).rgb))
 		#else // TEMPORAL_YCOCG
-			#define _SAMPLE_COLOR(n, uv, offset) _TONEMAP_COLOR(_SAMPLEOFF(n, uv, offset).rgb)
+			#define _SAMPLE_COLOR(n, uv, offset) _TONEMAP_COLOR(_SAMPLECOL(n, uv, offset).rgb)
 		#endif // TEMPORAL_YCOCG
 						
 		///////////////////////////// MAIN /////////////////////////////////
@@ -237,8 +258,8 @@ mixin TemporalResolve
 		
 		float3 temporalResolve(
 			_TEX2D(sceneDepth), 
-			_TEX2D(sceneColor), 
-			_TEX2D(prevColor), 
+			_TEXCOLOR(sceneColor), 
+			_TEXCOLOR(prevColor), 
 			#if TEMPORAL_LOCAL_VELOCITY
 			_TEX2D(velocityBuffer),
 			#endif // TEMPORAL_LOCAL_VELOCITY

+ 43 - 0
Data/Raw/Engine/Shaders/ClearStencilBits.bsl

@@ -0,0 +1,43 @@
+technique ClearStencilBits
+{
+	depth
+	{
+		read = false;
+		write = false;
+	};
+	
+	stencil
+	{
+		enabled = true;
+		front = { zero, zero, zero, always };
+		back = { zero, zero, zero, always };
+		writemask = 0x7F;
+	};	
+
+	code
+	{
+		struct VStoFS
+		{
+			float4 position : SV_Position;
+		};
+
+		struct VertexInput
+		{
+			float2 screenPos : POSITION;
+			float2 uv0 : TEXCOORD0;
+		};
+		
+		VStoFS vsmain(VertexInput input)
+		{
+			VStoFS output;
+			output.position = float4(input.screenPos, 0, 1);
+
+			return output;
+		}			
+
+		uint4 fsmain(VStoFS input) : SV_Target0
+		{
+			return uint4(gClearValue, gClearValue, gClearValue, gClearValue);
+		}
+	};
+};

+ 14 - 22
Data/Raw/Engine/Shaders/PPSSRResolve.bsl

@@ -6,9 +6,10 @@
 #define TEMPORAL_SEARCH_NEAREST 0
 #define TEMPORAL_BLEND_FACTOR 8
 #define TEMPORAL_SMOOTH_NEIGHBORHOOD 0
+#define MSAA_COLOR 0
 #include "$ENGINE$\TemporalResolve.bslinc"
 
-technique PPSSRStencil
+technique PPSSRResolve
 {
 	mixin PPBase;
 	mixin PerCameraData;
@@ -27,39 +28,30 @@ technique PPSSRStencil
 		
 		#if MSAA
 			Texture2DMS gSceneDepth;
-			Texture2DMS gSceneColor;
-			Texture2DMS gPrevColor;
 		#else
 			Texture2D gSceneDepth;
-			Texture2D gSceneColor;
-			Texture2D gPrevColor;
+		#endif	
 
-			SamplerState gPointSampler;
-			SamplerState gLinearSampler;
-		#endif		
-		
-		#if EYE_ADAPTATION
-			Texture2D gEyeAdaptationTex;
-		#endif
+		Texture2D gSceneColor;
+		Texture2D gPrevColor;
+
+		SamplerState gPointSampler;
+		SamplerState gLinearSampler;		
 		
 		float3 fsmain(VStoFS input) : SV_Target0
 		{
-			float exposureScale;
-			#if EYE_ADAPTATION
-				exposureScale = gEyeAdaptationTex.Load(int3(0, 0, 0)).r;
-			#else
-				exposureScale = gManualExposure;
-			#endif
-		
 			#if MSAA
-				return temporalResolve(gSceneDepth, gSceneColor, gPrevColor, 
-					exposureScale, input.uv0, input.screenPos, 0);
+				return temporalResolve(
+					gSceneDepth, 
+					gSceneColor, gLinearSampler, gSceneColorTexelSize, 
+					gPrevColor, gLinearSampler, gSceneColorTexelSize,
+					gManualExposure, input.position.xy, input.screenPos, 0);
 			#else
 				return temporalResolve(
 					gSceneDepth, gPointSampler, gSceneDepthTexelSize,
 					gSceneColor, gLinearSampler, gSceneColorTexelSize, 
 					gPrevColor, gLinearSampler, gSceneColorTexelSize,
-					exposureScale, input.uv0, input.screenPos, 0);
+					gManualExposure, input.uv0, input.screenPos, 0);
 			#endif
 		}	
 	};

+ 7 - 0
Data/Raw/Engine/Shaders/PPSSRStencil.bsl

@@ -13,6 +13,7 @@ technique PPSSRStencil
 		enabled = true;
 		reference = 1;
 		front = { replace, replace, replace, always };
+		writemask = 0x7F;
 	};		
 	
 	code
@@ -38,6 +39,12 @@ technique PPSSRStencil
 			// Surfaces that are too rough fall back to refl. probes
 			float fadeValue = 1.0f - saturate(surfData.roughness * gRoughnessScaleBias.x + gRoughnessScaleBias.y);
 			
+			// Reflection contribution is too low for dieletrics to waste performance on high quality reflections
+			// 0 if metalness <= 0.4
+			// [0, 1] if metalness > 0.4 && < 0.6
+			// 1 if metalness >= 0.6
+			fadeValue *= saturate(surfData.metalness * 5 - 2);
+			
 			if(fadeValue > 0.0f)
 				discard;
 			

+ 14 - 12
Data/Raw/Engine/Shaders/PPSSRTrace.bsl

@@ -21,6 +21,7 @@ technique PPSSRTrace
 		enabled = true;
 		reference = 0;
 		front = { keep, keep, keep, eq };
+		readmask = 0x7F;
 	};	
 	
 	code
@@ -41,6 +42,18 @@ technique PPSSRTrace
 			#define MSAA_RESOLVE_0TH 0
 		#endif
 		
+		#if QUALITY == 0
+			#define NUM_RAYS 1
+		#elif QUALITY == 1
+			#define NUM_RAYS 4
+		#elif QUALITY == 2
+			#define NUM_RAYS 8
+		#elif QUALITY == 3
+			#define NUM_RAYS 12
+		#else
+			#define NUM_RAYS 16
+		#endif
+		
 		Texture2D gSceneColor;
 		SamplerState gSceneColorSamp;
 		
@@ -81,19 +94,9 @@ technique PPSSRTrace
 			
 			float roughness = surfData.roughness;
 			
-			
-			roughness = 0.3f;//DEBUG ONLY
-			
-			
 			float roughness2 = roughness * roughness;
 			float roughness4 = roughness2 * roughness2;
 			
-			// TODO - DEBUG ONLY - Only handle reflections on up facing surfaces
-			if(dot(N, float3(0,1,0)) < 0.8)
-				return gSceneColor.Sample(gSceneColorSamp, input.uv0);	
-			else
-				N = float3(0,1,0);
-
 			// Jitter ray offset in 4x4 tile, in order to avoid stairstep artifacts
 			uint pixelIdx = mortonCode4x4((uint)pixelPos.x, (uint)pixelPos.y);
 			
@@ -107,8 +110,7 @@ technique PPSSRTrace
 			// Make sure each pixel chooses different ray directions (noise looks better than repeating patterns)
 			//// Magic integer is arbitrary, in order to convert from [0, 1] float
 			uint2 pixRandom = random(pixelPos.xy + gTemporalJitter * uint2(61, 85)) * uint2(0x36174842, 0x15249835);
-			int NUM_RAYS = 8; // DEBUG ONLY
-			
+
 			float4 sum = 0;
 			[loop]
 			for(int i = 0; i < NUM_RAYS; ++i)

+ 2 - 0
Data/Raw/Engine/Shaders/ShadowProject.bsl

@@ -20,6 +20,8 @@ technique ShadowProject
 		// Note: Need to test performance clearing the stencil this way vs. clearing it separately,
 		//   as this disables HiStencil optimization.
 		front = { zero, zero, zero, neq };
+		readmask = 0x7F;
+		writemask = 0x7F;
 	};
 	
 	#ifdef FADE_PLANE

+ 2 - 0
Data/Raw/Engine/Shaders/ShadowProjectStencil.bsl

@@ -28,6 +28,7 @@ technique ShadowProjectStencil
 		enabled = true;
 		front = { keep, incwrap, keep, always };
 		back = { keep, decwrap, keep, always };
+		writemask = 0x7F;
 	};
 	#else
 	stencil
@@ -35,6 +36,7 @@ technique ShadowProjectStencil
 		enabled = true;
 		front = { keep, keep, incwrap, always };
 		back = { keep, keep, decwrap, always };
+		writemask = 0x7F;
 	};	
 	#endif
 };

+ 2 - 2
Documentation/Manuals/Native/User/bsl.md

@@ -134,8 +134,8 @@ Name                 | Valid values				   | Reference
 ---------------------|---------------------------- |-----------------------
 reference    	  	 | integer			           | Reference value to use for stencil compare operations.
 enabled    	  	     | true, false				   | @ref bs::DEPTH_STENCIL_STATE_DESC::stencilEnable "DEPTH_STENCIL_STATE_DESC::stencilEnable"
-readmask    	  	 | R, G, B, A or any combination (e.g. RG, RBA, RGBA). "empty" for zero mask.				   | @ref bs::DEPTH_STENCIL_STATE_DESC::stencilReadMask "DEPTH_STENCIL_STATE_DESC::stencilReadMask"
-writemask    	  	 | R, G, B, A or any combination (e.g. RG, RBA, RGBA). "empty" for zero mask.				   | @ref bs::DEPTH_STENCIL_STATE_DESC::stencilWriteMask "DEPTH_STENCIL_STATE_DESC::stencilWriteMask"
+readmask    	  	 | integer in [0, 255] range   | @ref bs::DEPTH_STENCIL_STATE_DESC::stencilReadMask "DEPTH_STENCIL_STATE_DESC::stencilReadMask"
+writemask    	  	 | integer in [0, 255] range   | @ref bs::DEPTH_STENCIL_STATE_DESC::stencilWriteMask "DEPTH_STENCIL_STATE_DESC::stencilWriteMask"
 front				 | StencilOp block			   | Stencil operations and compare function for front facing geometry
 back				 | StencilOp block			   | Stencil operations and compare function for back facing geometry
  

+ 32 - 10
Source/RenderBeast/Include/BsPostProcessing.h

@@ -690,21 +690,31 @@ namespace bs { namespace ct
 		/** 
 		 * Returns the material variation matching the provided parameters. 
 		 * 
+		 * @param[in]	quality				Determines how many rays to trace. In range [0, 4].
 		 * @param[in]	msaa				True if the shader will operate on a multisampled surface.
 		 * @param[in]	singleSampleMSAA	Only relevant of @p msaa is true. When enabled only the first sample will be
 		 *									evaluated. Otherwise all samples will be evaluated.
 		 * @return							Requested variation of the material.
 		 */
-		static SSRTraceMat* getVariation(bool msaa, bool singleSampleMSAA = false);
+		static SSRTraceMat* getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA = false);
 	private:
 		SPtr<GpuParamBlockBuffer> mParamBuffer;
 		GBufferParams mGBufferParams;
 		GpuParamTexture mSceneColorTexture;
 		GpuParamTexture mHiZTexture;
 
-		static ShaderVariation VAR_NoMSAA;
-		static ShaderVariation VAR_FullMSAA;
-		static ShaderVariation VAR_SingleMSAA;
+#define VARIATION(QUALITY) \
+		static ShaderVariation VAR_NoMSAA_Quality##QUALITY;			\
+		static ShaderVariation VAR_FullMSAA_Quality##QUALITY;		\
+		static ShaderVariation VAR_SingleMSAA_Quality##QUALITY;		\
+
+		VARIATION(0)
+		VARIATION(1)
+		VARIATION(2)
+		VARIATION(3)
+		VARIATION(4)
+
+#undef VARIATION
 	};
 
 	BS_PARAM_BLOCK_BEGIN(TemporalResolveParamDef)
@@ -745,11 +755,11 @@ namespace bs { namespace ct
 		/** 
 		 * Returns the material variation matching the provided parameters. 
 		 * 
-		 * @param	eyeAdaptation	When true the shader will expect a texture containing an exposure value calculated by
-		 *							the eye adaptation shader. Otherwise the manually provided exposure value is used
-		 *							instead.
+		 * @param[in]	msaa				True if the shader will operate on a multisampled surface. Note that previous
+		 *									and current frame color textures must be non-MSAA, regardless of this parameter.
+		 * @return							Requested variation of the material.
 		 */
-		static SSRResolveMat* getVariation(bool eyeAdaptation);
+		static SSRResolveMat* getVariation(bool msaa);
 
 	private:
 		SPtr<GpuParamBlockBuffer> mSSRParamBuffer;
@@ -760,8 +770,20 @@ namespace bs { namespace ct
 		GpuParamTexture mSceneDepthTexture;
 		GpuParamTexture mEyeAdaptationTexture;
 
-		static ShaderVariation VAR_EyeAdaptation;
-		static ShaderVariation VAR_NoEyeAdaptation;
+		static ShaderVariation VAR_MSAA;
+		static ShaderVariation VAR_NoMSAA;
+	};
+
+	/** Shader that clears only the unreserved portion of the stencil buffer. */
+	class ClearStencilBitsMat : public RendererMaterial<ClearStencilBitsMat>
+	{
+		RMAT_DEF("ClearStencilBits.bsl");
+
+	public:
+		ClearStencilBitsMat();
+
+		/** Executes the material on the currently bound render target. */
+		void execute();
 	};
 
 	/** @} */

+ 81 - 41
Source/RenderBeast/Source/BsPostProcessing.cpp

@@ -1217,7 +1217,6 @@ namespace bs { namespace ct
 		}
 
 #undef PICK_MATERIAL
-		
 	}
 
 	SSAODownsampleParamDef gSSAODownsampleParamDef;
@@ -1407,18 +1406,28 @@ namespace bs { namespace ct
 
 	SSRTraceParamDef gSSRTraceParamDef;
 
-	ShaderVariation SSRTraceMat::VAR_NoMSAA = ShaderVariation({
-		ShaderVariation::Param("MSAA_COUNT", 1)
-	});
+#define VARIATION(QUALITY)																	\
+		ShaderVariation SSRTraceMat::VAR_NoMSAA_Quality##QUALITY = ShaderVariation({		\
+			ShaderVariation::Param("MSAA_COUNT", 1),										\
+			ShaderVariation::Param("QUALITY", QUALITY)										\
+		});																					\
+		ShaderVariation SSRTraceMat::VAR_FullMSAA_Quality##QUALITY = ShaderVariation({		\
+			ShaderVariation::Param("MSAA_COUNT", 2),										\
+			ShaderVariation::Param("QUALITY", QUALITY)										\
+		});																					\
+		ShaderVariation SSRTraceMat::VAR_SingleMSAA_Quality##QUALITY = ShaderVariation({	\
+			ShaderVariation::Param("MSAA_COUNT", 2),										\
+			ShaderVariation::Param("MSAA_RESOLVE_0TH", true),								\
+			ShaderVariation::Param("QUALITY", QUALITY)										\
+		});																					\
 
-	ShaderVariation SSRTraceMat::VAR_FullMSAA = ShaderVariation({
-		ShaderVariation::Param("MSAA_COUNT", 2)
-	});
+		VARIATION(0)
+		VARIATION(1)
+		VARIATION(2)
+		VARIATION(3)
+		VARIATION(4)
 
-	ShaderVariation SSRTraceMat::VAR_SingleMSAA = ShaderVariation({
-		ShaderVariation::Param("MSAA_COUNT", 2),
-		ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
-	});
+#undef VARIATION
 
 	SSRTraceMat::SSRTraceMat()
 		:mGBufferParams(mMaterial, mParamsSet)
@@ -1435,9 +1444,18 @@ namespace bs { namespace ct
 
 	void SSRTraceMat::_initVariations(ShaderVariations& variations)
 	{
-		variations.add(VAR_NoMSAA);
-		variations.add(VAR_FullMSAA);
-		variations.add(VAR_SingleMSAA);
+#define VARIATION(QUALITY) \
+		variations.add(VAR_NoMSAA_Quality##QUALITY);		\
+		variations.add(VAR_FullMSAA_Quality##QUALITY);		\
+		variations.add(VAR_SingleMSAA_Quality##QUALITY);	\
+
+		VARIATION(0)
+		VARIATION(1)
+		VARIATION(2)
+		VARIATION(3)
+		VARIATION(4)
+
+#undef VARIATION
 	}
 
 	void SSRTraceMat::execute(const RendererView& view, GBufferTextures gbuffer, const SPtr<Texture>& sceneColor, 
@@ -1495,7 +1513,7 @@ namespace bs { namespace ct
 		SPtr<GpuParamBlockBuffer> perView = view.getPerViewBuffer();
 		mParamsSet->setParamBlockBuffer("PerCamera", perView);
 
-		rapi.setRenderTarget(destination);
+		rapi.setRenderTarget(destination, FBT_DEPTH);
 
 		gRendererUtility().setPass(mMaterial);
 		gRendererUtility().setPassParams(mParamsSet);
@@ -1513,29 +1531,43 @@ namespace bs { namespace ct
 		return scaleBias;
 	}
 
-	SSRTraceMat* SSRTraceMat::getVariation(bool msaa, bool singleSampleMSAA)
+	SSRTraceMat* SSRTraceMat::getVariation(UINT32 quality, bool msaa, bool singleSampleMSAA)
 	{
-		if (msaa)
+#define PICK_MATERIAL(QUALITY)											\
+		if(msaa)														\
+			if(singleSampleMSAA)										\
+				return get(VAR_SingleMSAA_Quality##QUALITY);			\
+			else														\
+				return get(VAR_FullMSAA_Quality##QUALITY);				\
+		else															\
+			return get(VAR_NoMSAA_Quality##QUALITY);					\
+
+		switch(quality)
 		{
-			if (singleSampleMSAA)
-				return get(VAR_SingleMSAA);
-			else
-				return get(VAR_FullMSAA);
+		case 0:
+			PICK_MATERIAL(0)
+		case 1:
+			PICK_MATERIAL(1)
+		case 2:
+			PICK_MATERIAL(2)
+		case 3:
+			PICK_MATERIAL(3)
+		default:
+		case 4:
+			PICK_MATERIAL(4)
 		}
-		else
-			return get(VAR_NoMSAA);
+
+#undef PICK_MATERIAL
 	}
 
 	TemporalResolveParamDef gTemporalResolveParamDef;
 	SSRResolveParamDef gSSRResolveParamDef;
 
-	ShaderVariation SSRResolveMat::VAR_EyeAdaptation = ShaderVariation({
-		ShaderVariation::Param("EYE_ADAPTATION", true),
-		ShaderVariation::Param("MSAA", false)
+	ShaderVariation SSRResolveMat::VAR_MSAA = ShaderVariation({
+		ShaderVariation::Param("MSAA", true)
 	});
 
-	ShaderVariation SSRResolveMat::VAR_NoEyeAdaptation = ShaderVariation({
-		ShaderVariation::Param("EYE_ADAPTATION", false),
+	ShaderVariation SSRResolveMat::VAR_NoMSAA = ShaderVariation({
 		ShaderVariation::Param("MSAA", false)
 	});
 
@@ -1577,15 +1609,13 @@ namespace bs { namespace ct
 
 	void SSRResolveMat::_initVariations(ShaderVariations& variations)
 	{
-		variations.add(VAR_EyeAdaptation);
-		variations.add(VAR_NoEyeAdaptation);
+		variations.add(VAR_NoMSAA);
+		variations.add(VAR_MSAA);
 	}
 
 	void SSRResolveMat::execute(const RendererView& view, const SPtr<Texture>& prevFrame, 
 		const SPtr<Texture>& curFrame, const SPtr<Texture>& sceneDepth, const SPtr<RenderTarget>& destination)
 	{
-		// TODO - MSAA not supported (remember UV must be in pixels) 
-		
 		// Note: This shader should not be called when temporal AA is turned on
 		// Note: This shader doesn't have velocity texture enabled and will only account for camera movement (can be easily
 		//		 enabled when velocity texture is added)
@@ -1595,11 +1625,6 @@ namespace bs { namespace ct
 		mSceneColorTexture.set(curFrame);
 		mSceneDepthTexture.set(sceneDepth);
 
-		if(mVariation.getBool("EYE_ADAPTATION"))
-		{
-			// TODO - Set eye adaptation texture
-		}
-
 		auto& colorProps = curFrame->getProperties(); // Assuming prev and current frame are the same size
 		auto& depthProps = sceneDepth->getProperties();
 
@@ -1699,11 +1724,26 @@ namespace bs { namespace ct
 		gRendererUtility().drawScreenQuad();
 	}
 
-	SSRResolveMat* SSRResolveMat::getVariation(bool eyeAdaptation)
+	SSRResolveMat* SSRResolveMat::getVariation(bool msaa)
 	{
-		if (eyeAdaptation)
-			return get(VAR_EyeAdaptation);
+		if (msaa)
+			return get(VAR_MSAA);
 		else
-			return get(VAR_NoEyeAdaptation);
+			return get(VAR_NoMSAA);
+	}
+
+	ClearStencilBitsMat::ClearStencilBitsMat()
+	{ }
+
+	void ClearStencilBitsMat::_initVariations(ShaderVariations& variations)
+	{
+		// Do nothing
+	}
+
+	void ClearStencilBitsMat::execute()
+	{
+		gRendererUtility().setPass(mMaterial);
+		gRendererUtility().setPassParams(mParamsSet);
+		gRendererUtility().drawScreenQuad();
 	}
 }}

+ 27 - 19
Source/RenderBeast/Source/BsRenderCompositor.cpp

@@ -293,16 +293,8 @@ namespace bs { namespace ct
 		Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
 		rapi.setViewport(area);
 
-		// Clear depth & stencil according to user defined values, don't clear color as all values will get written to
-		UINT32 clearFlags = viewProps.clearFlags & ~FBT_COLOR;
-		if (clearFlags != 0)
-		{
-			rapi.clearViewport(clearFlags, viewProps.clearColor, viewProps.clearDepthValue, 
-				viewProps.clearStencilValue, 0x01);
-		}
-
-		// Clear all non primary targets (Note: I could perhaps clear all but albedo, since it stores a per-pixel write mask)
-		rapi.clearViewport(FBT_COLOR, Color::ZERO, 1.0f, 0, 0xFF & ~0x01);
+		// Clear all targets
+		rapi.clearViewport(FBT_COLOR | FBT_DEPTH | FBT_STENCIL, Color::ZERO, 1.0f, 0);
 
 		// Render all visible opaque elements
 		const Vector<RenderQueueElement>& opaqueElements = inputs.view.getOpaqueQueue()->getSortedElements();
@@ -1397,10 +1389,11 @@ namespace bs { namespace ct
 			UINT32 width = viewProps.viewRect.width;
 			UINT32 height = viewProps.viewRect.height;
 
-			output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D32, width, height, TU_RENDERTARGET, 1, false)); 
+			output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D32_S8X24, width, height, TU_RENDERTARGET, 1, false)); 
 
 			RenderAPI& rapi = RenderAPI::instance();
 			rapi.setRenderTarget(output->renderTexture);
+			rapi.clearRenderTarget(FBT_STENCIL);
 			gRendererUtility().blit(sceneDepthNode->depthTex->texture, Rect2I::EMPTY, false, true);
 
 			mPassThrough = false;
@@ -1717,10 +1710,13 @@ namespace bs { namespace ct
 		const ScreenSpaceReflectionsSettings& settings = inputs.view.getRenderSettings().screenSpaceReflections;
 		if (settings.enabled)
 		{
+			RenderAPI& rapi = RenderAPI::instance();
+
 			RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
 			RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[1]);
 			RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[2]);
 			RCNodeHiZ* hiZNode = static_cast<RCNodeHiZ*>(inputs.inputNodes[3]);
+			RCNodeResolvedSceneDepth* resolvedSceneDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[4]);
 
 			GpuResourcePool& resPool = GpuResourcePool::instance();
 			const RendererViewProperties& viewProps = inputs.view.getProperties();
@@ -1740,7 +1736,7 @@ namespace bs { namespace ct
 				resolvedSceneColor = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, 
 					TU_RENDERTARGET));
 
-				RenderAPI::instance().setRenderTarget(resolvedSceneColor->renderTexture);
+				rapi.setRenderTarget(resolvedSceneColor->renderTexture);
 				gRendererUtility().blit(sceneColor);
 
 				sceneColor = resolvedSceneColor->texture;
@@ -1754,16 +1750,24 @@ namespace bs { namespace ct
 
 			SSRStencilMat* stencilMat = SSRStencilMat::getVariation(viewProps.numSamples > 1);
 
-			// TODO - Run SSRStencil
-			// TODO - Is stencil clear at this point? Also use stencil mask.
-			// RenderAPI::instance().setRenderTarget(sceneDepthNode->depthTex->renderTexture);
-			// stencilMat->execute(inputs.view, gbuffer, settings);
+			// Note: Making the assumption that the stencil buffer is clear at this point
+			rapi.setRenderTarget(resolvedSceneDepthNode->output->renderTexture);
+			stencilMat->execute(inputs.view, gbuffer, settings);
 
 			SPtr<PooledRenderTexture> traceOutput = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, 
 				height, TU_RENDERTARGET));
 
-			SSRTraceMat* traceMat = SSRTraceMat::getVariation(viewProps.numSamples > 1);
-			traceMat->execute(inputs.view, gbuffer, sceneColor, hiZ, settings, traceOutput->renderTexture);
+			RENDER_TEXTURE_DESC traceRtDesc;
+			traceRtDesc.colorSurfaces[0].texture = traceOutput->texture;
+			traceRtDesc.depthStencilSurface.texture = resolvedSceneDepthNode->output->texture;
+
+			SPtr<RenderTexture> traceRt = RenderTexture::create(traceRtDesc);
+
+			rapi.setRenderTarget(traceRt);
+			rapi.clearRenderTarget(FBT_COLOR);
+
+			SSRTraceMat* traceMat = SSRTraceMat::getVariation(settings.quality, viewProps.numSamples > 1);
+			traceMat->execute(inputs.view, gbuffer, sceneColor, hiZ, settings, traceRt);
 
 			if (resolvedSceneColor)
 			{
@@ -1775,7 +1779,10 @@ namespace bs { namespace ct
 			{
 				output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, TU_RENDERTARGET));
 
-				SSRResolveMat* resolveMat = SSRResolveMat::getVariation(false);
+				rapi.setRenderTarget(output->renderTexture);
+				rapi.clearRenderTarget(FBT_COLOR);
+
+				SSRResolveMat* resolveMat = SSRResolveMat::getVariation(viewProps.numSamples > 1);
 				resolveMat->execute(inputs.view, mPrevFrame->texture, traceOutput->texture, sceneDepthNode->depthTex->texture,
 					output->renderTexture);
 
@@ -1821,6 +1828,7 @@ namespace bs { namespace ct
 			deps.push_back(RCNodeLightAccumulation::getNodeId());
 			deps.push_back(RCNodeGBuffer::getNodeId());
 			deps.push_back(RCNodeHiZ::getNodeId());
+			deps.push_back(RCNodeResolvedSceneDepth::getNodeId());
 
 			if (view.getProperties().numSamples > 1)
 				deps.push_back(RCNodeUnflattenLightAccum::getNodeId());