Browse Source

Bugfix: Fix for rendering when SSR or AO are disabled

BearishSun 8 years ago
parent
commit
33b290215b

+ 4 - 0
Data/Raw/Engine/Includes/ImageBasedLighting.bslinc

@@ -29,7 +29,10 @@ mixin ImageBasedLighting
 		SamplerState gReflProbeSamp;
 		
 		Texture2D gAmbientOcclusionTex;
+		SamplerState gAmbientOcclusionSamp;
+		
 		Texture2D gSSRTex;
+		SamplerState gSSRSamp;
 		
 		Texture2D gPreintegratedEnvBRDF;
 		SamplerState gPreintegratedEnvBRDFSamp;
@@ -143,6 +146,7 @@ mixin ImageBasedLighting
 			float mipLevel = mapRoughnessToMipLevel(roughness, gReflCubemapNumMips);
 			
 			float3 output = 0;
+			[loop]
 			for(uint i = 0; i < numProbes; i++)
 			{
 				if(alpha < 0.001f)

+ 7 - 4
Data/Raw/Engine/Includes/LightingCommon.bslinc

@@ -364,6 +364,7 @@ mixin LightingCommon
 			if(surfaceData.worldNormal.w > 0.0f)
 			{
 				// Handle directional lights
+				[loop]
 				for(uint i = 0; i < lightOffsets.x; ++i)
 				{
 					LightData lightData = gLights[i];
@@ -371,18 +372,20 @@ mixin LightingCommon
 				}
 				
 				// Handle radial lights
-				for (uint i = lightOffsets.y; i < lightOffsets.z; ++i)
+				[loop]
+				for (uint j = lightOffsets.y; j < lightOffsets.z; ++j)
 				{
-					uint lightIdx = gLightIndices[i];
+					uint lightIdx = gLightIndices[j];
 					LightData lightData = gLights[lightIdx];
 					
 					outLuminance += getLuminanceRadial(lightData, worldPos, V, R, roughness2, surfaceData);
 				}
 
 				// Handle spot lights
-				for(uint i = lightOffsets.z; i < lightOffsets.w; ++i)
+				[loop]
+				for(uint k = lightOffsets.z; k < lightOffsets.w; ++k)
 				{
-					uint lightIdx = gLightIndices[i];
+					uint lightIdx = gLightIndices[k];
 					LightData lightData = gLights[lightIdx];
 					
 					outLuminance += getLuminanceSpot(lightData, worldPos, V, R, roughness2, surfaceData);

+ 7 - 10
Data/Raw/Engine/Shaders/TiledDeferredImageBasedLighting.bsl

@@ -77,10 +77,7 @@ technique TiledDeferredImageBasedLighting
 			
 			// Determine minimum and maximum depth values for a tile			
 			InterlockedMin(sTileMinZ, sampleMinZ);
-			
-			// Skip samples on the far plane (e.g. skybox) as they ruin the precision
-			if(sampleMaxZ < 1.0f)
-				InterlockedMax(sTileMaxZ, sampleMaxZ);
+			InterlockedMax(sTileMaxZ, sampleMaxZ);
 			
 			GroupMemoryBarrierWithGroupSync();
 			
@@ -147,7 +144,7 @@ technique TiledDeferredImageBasedLighting
 			return dot(closestOnBox, closestOnBox) < sRadius * sRadius;
 		}
 		
-		float4 getLighting(uint2 pixelPos, uint sampleIdx, float2 clipSpacePos, SurfaceData surfaceData, uint probeOffset, uint numProbes)
+		float4 getLighting(uint2 pixelPos, float2 uv, uint sampleIdx, float2 clipSpacePos, SurfaceData surfaceData, uint probeOffset, uint numProbes)
 		{
 			// x, y are now in clip space, z, w are in view space
 			// We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
@@ -169,8 +166,8 @@ technique TiledDeferredImageBasedLighting
 			existingColor = gInColor.Load(int3(pixelPos.xy, 0));
 			#endif				
 			
-			float ao = gAmbientOcclusionTex.Load(int3(pixelPos.xy, 0));
-			float4 ssr = gSSRTex.Load(int3(pixelPos.xy, 0));
+			float ao = gAmbientOcclusionTex.SampleLevel(gAmbientOcclusionSamp, uv, 0.0f).r;
+			float4 ssr = gSSRTex.SampleLevel(gSSRSamp, uv, 0.0f);
 			float3 imageBasedSpecular = getImageBasedSpecular(worldPosition, V, specR, surfaceData, ao, ssr, probeOffset, numProbes);
 
 			float4 totalLighting = existingColor;
@@ -261,7 +258,7 @@ technique TiledDeferredImageBasedLighting
 				#if MSAA_COUNT > 1
 				float coverage = gMSAACoverage.Load(int3(pixelPos, 0)).r;
 				
-				float4 lighting = getLighting(pixelPos, 0, clipSpacePos.xy, surfaceData[0], 0, sNumProbes);
+				float4 lighting = getLighting(pixelPos, screenUv, 0, clipSpacePos.xy, surfaceData[0], 0, sNumProbes);
 				writeBufferSample(pixelPos, 0, lighting);
 
 				bool doPerSampleShading = coverage > 0.5f;
@@ -270,7 +267,7 @@ technique TiledDeferredImageBasedLighting
 					[unroll]
 					for(uint i = 1; i < MSAA_COUNT; ++i)
 					{
-						lighting = getLighting(pixelPos, i, clipSpacePos.xy, surfaceData[i], 0, sNumProbes);
+						lighting = getLighting(pixelPos, screenUv, i, clipSpacePos.xy, surfaceData[i], 0, sNumProbes);
 						writeBufferSample(pixelPos, i, lighting);
 					}
 				}
@@ -285,7 +282,7 @@ technique TiledDeferredImageBasedLighting
 				}
 				
 				#else
-				float4 lighting = getLighting(pixelPos, 0, clipSpacePos.xy, surfaceData[0], 0, sNumProbes);
+				float4 lighting = getLighting(pixelPos, screenUv, 0, clipSpacePos.xy, surfaceData[0], 0, sNumProbes);
 				gOutput[pixelPos] = lighting;
 				#endif
 			}

+ 2 - 2
Data/Raw/Engine/Shaders/Transparent.bsl

@@ -82,8 +82,8 @@ mixin Surface
 			float3 specR = getSpecularDominantDir(N, R, surfaceData.roughness);
 			
 			float4 directLighting = getDirectLighting(input.worldPosition, V, specR, surfaceData, lightOffsets);
-			float ao = gAmbientOcclusionTex.Sample(gAlbedoSamp, input.uv0);
-			float4 ssr = gSSRTex.Load(int3(pixelPos.xy, 0));
+			float ao = gAmbientOcclusionTex.Sample(gAmbientOcclusionSamp, input.uv0);
+			float4 ssr = gSSRTex.Sample(gSSRSamp, input.uv0);
 			float3 imageBasedSpecular = getImageBasedSpecular(input.worldPosition, V, specR, surfaceData, ao, ssr,
 				reflProbeOffsetAndSize.x, reflProbeOffsetAndSize.y);
 

+ 4 - 4
Source/BansheeCore/Managers/BsTextureManager.cpp

@@ -109,10 +109,10 @@ namespace bs
 		SPtr<Texture> blackTexture = createTexture(desc);
 
 		SPtr<PixelData> blackPixelData = PixelData::create(2, 2, 1, PF_RGBA8);
-		blackPixelData->setColorAt(Color::Black, 0, 0);
-		blackPixelData->setColorAt(Color::Black, 0, 1);
-		blackPixelData->setColorAt(Color::Black, 1, 0);
-		blackPixelData->setColorAt(Color::Black, 1, 1);
+		blackPixelData->setColorAt(Color::ZERO, 0, 0);
+		blackPixelData->setColorAt(Color::ZERO, 0, 1);
+		blackPixelData->setColorAt(Color::ZERO, 1, 0);
+		blackPixelData->setColorAt(Color::ZERO, 1, 1);
 
 		blackTexture->writeData(*blackPixelData);
 		Texture::BLACK = blackTexture;

+ 2 - 2
Source/BansheeCore/Renderer/BsReflectionProbe.cpp

@@ -13,12 +13,12 @@ namespace bs
 {
 	ReflectionProbeBase::ReflectionProbeBase()
 		: mPosition(BsZero), mRotation(BsIdentity), mScale(1.0f, 1.0f, 1.0f), mType(ReflectionProbeType::Box), mRadius(1.0f)
-		, mExtents(1.0f, 1.0f, 1.0f), mTransitionDistance(0.5f), mIsActive(true), mBounds(Vector3::ZERO, 1.0f)
+		, mExtents(1.0f, 1.0f, 1.0f), mTransitionDistance(0.1f), mIsActive(true), mBounds(Vector3::ZERO, 1.0f)
 	{ }
 
 	ReflectionProbeBase::ReflectionProbeBase(ReflectionProbeType type, float radius, const Vector3& extents)
 		: mPosition(BsZero), mRotation(BsIdentity), mScale(1.0f, 1.0f, 1.0f), mType(type), mRadius(radius)
-		, mExtents(extents), mTransitionDistance(0.5f), mIsActive(true), mBounds(Vector3::ZERO, 1.0f)
+		, mExtents(extents), mTransitionDistance(0.1f), mIsActive(true), mBounds(Vector3::ZERO, 1.0f)
 	{ }
 
 	void ReflectionProbeBase::updateBounds()

+ 20 - 15
Source/BansheeD3D11RenderAPI/BsD3D11GpuProgram.cpp

@@ -119,7 +119,7 @@ namespace bs { namespace ct
 			compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
 
 		ID3DBlob* microCode = nullptr;
-		ID3DBlob* errors = nullptr;
+		ID3DBlob* messages = nullptr;
 
 		const String& source = mProperties.getSource();
 		const String& entryPoint = mProperties.getEntryPoint();
@@ -141,35 +141,40 @@ namespace bs { namespace ct
 			compileFlags,		// [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
 			0,					// [in] Effect compile flags
 			&microCode,			// [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information. 
-			&errors				// [out] A pointer to an ID3DBlob Interface which contains a listing of errors and warnings that occurred during compilation. These errors and warnings are identical to the the debug output from a debugger.
+			&messages			// [out] A pointer to an ID3DBlob Interface which contains a listing of errors and warnings that occurred during compilation. These errors and warnings are identical to the the debug output from a debugger.
 			);
 
-		if (FAILED(hr))
+		if (messages != nullptr)
 		{
-			const char* errorMessage = static_cast<const char*>(errors->GetBufferPointer());
-			UINT32 errorLineIdx = parseErrorMessage(errorMessage);
+			const char* message = static_cast<const char*>(messages->GetBufferPointer());
+			UINT32 lineIdx = parseErrorMessage(message);
 
 			Vector<String> sourceLines = StringUtil::split(source, "\n");
 			String sourceLine;
-			if (errorLineIdx < sourceLines.size())
-				sourceLine = sourceLines[errorLineIdx];
+			if (lineIdx < sourceLines.size())
+				sourceLine = sourceLines[lineIdx];
 
-			mIsCompiled = false;
-			mCompileError = "Cannot compile D3D11 high-level shader. Errors:\n" +
-				String(errorMessage) + "\n" +
+			mCompileError =
+				String(message) + "\n" +
 				"\n" +
-				"Line " + toString(errorLineIdx) + ": " + sourceLine;
+				"Line " + toString(lineIdx) + ": " + sourceLine;
+
+			SAFE_RELEASE(messages);
+		}
+		else
+			mCompileError = "";
+
+		if (FAILED(hr))
+		{
+			mIsCompiled = false;
+			mCompileError = "Cannot compile D3D11 high-level shader. Errors:\n" + mCompileError;
 
 			SAFE_RELEASE(microCode);
-			SAFE_RELEASE(errors);
 			return nullptr;
 		}
 		else
 		{
 			mIsCompiled = true;
-			mCompileError = "";
-
-			SAFE_RELEASE(errors);
 			return microCode;
 		}
 	}

+ 20 - 2
Source/RenderBeast/BsImageBasedLighting.cpp

@@ -121,12 +121,18 @@ namespace bs { namespace ct
 		}
 
 		// AO
-		if(params->hasTexture(programType, "gAmbientOcclusionTex"))
+		if (params->hasTexture(programType, "gAmbientOcclusionTex"))
+		{
 			params->getTextureParam(programType, "gAmbientOcclusionTex", ambientOcclusionTexParam);
+			params->getSamplerStateParam(programType, "gAmbientOcclusionSamp", ambientOcclusionSampParam);
+		}
 
 		// SSR
-		if(params->hasTexture(programType, "gSSRTex"))
+		if (params->hasTexture(programType, "gSSRTex"))
+		{
 			params->getTextureParam(programType, "gSSRTex", ssrTexParam);
+			params->getSamplerStateParam(programType, "gSSRSamp", ssrSampParam);
+		}
 
 		if(gridIndices)
 		{
@@ -224,6 +230,18 @@ namespace bs { namespace ct
 
 		mImageBasedParams.populate(mParamsSet, GPT_COMPUTE_PROGRAM, false, false);
 
+		SAMPLER_STATE_DESC desc;
+		desc.minFilter = FO_POINT;
+		desc.magFilter = FO_POINT;
+		desc.mipFilter = FO_POINT;
+		desc.addressMode.u = TAM_CLAMP;
+		desc.addressMode.v = TAM_CLAMP;
+		desc.addressMode.w = TAM_CLAMP;
+
+		SPtr<SamplerState> samplerState = SamplerState::create(desc);
+		mImageBasedParams.ssrSampParam.set(samplerState);
+		mImageBasedParams.ambientOcclusionSampParam.set(samplerState);
+
 		mParamsSet->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
 	}
 

+ 3 - 0
Source/RenderBeast/BsImageBasedLighting.h

@@ -109,6 +109,9 @@ namespace bs { namespace ct
 		GpuParamTexture preintegratedEnvBRDFParam;
 		GpuParamBuffer reflectionProbesParam;
 
+		GpuParamSampState ambientOcclusionSampParam;
+		GpuParamSampState ssrSampParam;
+
 		GpuParamBuffer reflectionProbeIndicesParam;
 		UINT32 reflProbeParamsBindingIdx;
 	};

+ 16 - 0
Source/RenderBeast/BsRenderCompositor.cpp

@@ -911,6 +911,19 @@ namespace bs { namespace ct
 		return { RCNodeSceneColor::getNodeId() };
 	}
 
+	RCNodeClusteredForward::RCNodeClusteredForward()
+	{
+		SAMPLER_STATE_DESC desc;
+		desc.minFilter = FO_POINT;
+		desc.magFilter = FO_POINT;
+		desc.mipFilter = FO_POINT;
+		desc.addressMode.u = TAM_CLAMP;
+		desc.addressMode.v = TAM_CLAMP;
+		desc.addressMode.w = TAM_CLAMP;
+
+		mSSRAOSamplerState = SamplerState::create(desc);
+	}
+
 	void RCNodeClusteredForward::render(const RenderCompositorNodeInputs& inputs)
 	{
 		const SceneInfo& sceneInfo = inputs.scene;
@@ -976,6 +989,9 @@ namespace bs { namespace ct
 
 				iblParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
 				iblParams.preintegratedEnvBRDFParam.set(RendererTextures::preintegratedEnvGF);
+
+				iblParams.ssrSampParam.set(mSSRAOSamplerState);
+				iblParams.ambientOcclusionSampParam.set(mSSRAOSamplerState);
 			}
 		}
 

+ 4 - 0
Source/RenderBeast/BsRenderCompositor.h

@@ -395,6 +395,8 @@ namespace ct
 	class RCNodeClusteredForward : public RenderCompositorNode
 	{
 	public:
+		RCNodeClusteredForward();
+
 		static StringID getNodeId() { return "ClusteredForward"; }
 		static SmallVector<StringID, 4> getDependencies(const RendererView& view);
 	protected:
@@ -403,6 +405,8 @@ namespace ct
 
 		/** @copydoc RenderCompositorNode::clear */
 		void clear() override;
+
+		SPtr<SamplerState> mSSRAOSamplerState;
 	};
 
 	/**