// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 float SampleDeviceZFromSceneTexturesTempCopy(float2 UV) { return SceneDepthTexture.SampleLevel(SceneDepthTextureSampler, UV, 0).r; } #endif #ifndef GBUFFER_LAYOUT #define GBUFFER_LAYOUT 0 #endif #if GBUFFER_LAYOUT == 0 void EncodeGBufferToMRT(inout FPixelShaderOut Out, FGBufferData GBuffer, float QuantizationBias) { float4 MrtFloat1 = 0.0f; MrtFloat1.x = GBuffer.PrecomputedShadowFactors.x; MrtFloat1.y = GBuffer.PrecomputedShadowFactors.y; MrtFloat1.z = GBuffer.PrecomputedShadowFactors.z; MrtFloat1.w = GBuffer.PrecomputedShadowFactors.w; Out.MRT[1] = MrtFloat1; Out.MRT[2] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[3] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[4] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[5] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[6] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[7] = float4(0.0f, 0.0f, 0.0f, 0.0f); } FGBufferData DecodeGBufferDataDirect(float4 InMRT1, float CustomNativeDepth, float4 AnisotropicData, uint CustomStencil, float SceneDepth, bool bGetNormalizedNormal, bool bChecker) { FGBufferData Ret = (FGBufferData)0; Ret.WorldNormal = float3(0,0,1); Ret.Depth = 0.f; Ret.ShadingModelID = 1; Ret.PrecomputedShadowFactors.x = InMRT1.x; Ret.PrecomputedShadowFactors.y = InMRT1.y; Ret.PrecomputedShadowFactors.z = InMRT1.z; Ret.PrecomputedShadowFactors.w = InMRT1.w; Ret.WorldTangent = AnisotropicData.xyz; Ret.Anisotropy = AnisotropicData.w; GBufferPostDecode(Ret,bChecker,bGetNormalizedNormal); Ret.CustomDepth = ConvertFromDeviceZ(CustomNativeDepth); Ret.CustomStencil = CustomStencil; Ret.Depth = SceneDepth; return Ret; } #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataUV(float2 UV, bool bGetNormalizedNormal = true) { float CustomNativeDepth = Texture2DSampleLevel(SceneTexturesStruct.CustomDepthTexture, SceneTexturesStruct_CustomDepthTextureSampler, UV, 0).r; int2 IntUV = (int2)trunc(UV * View.BufferSizeAndInvSize.xy * View.BufferToSceneTextureScale.xy); uint CustomStencil = SceneTexturesStruct.CustomStencilTexture.Load(int3(IntUV, 0)) STENCIL_COMPONENT_SWIZZLE; float SceneDepth = CalcSceneDepth(UV); float4 AnisotropicData = Texture2DSampleLevel(SceneTexturesStruct.GBufferFTexture, SceneTexturesStruct_GBufferFTextureSampler, UV, 0).xyzw; float4 InMRT1 = Texture2DSampleLevel(SceneTexturesStruct.GBufferETexture, SceneTexturesStruct_GBufferETextureSampler, UV, 0).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromSceneColorUV(UV)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataUint(uint2 PixelPos, bool bGetNormalizedNormal = true) { float CustomNativeDepth = SceneTexturesStruct.CustomDepthTexture.Load(int3(PixelPos, 0)).r; uint CustomStencil = SceneTexturesStruct.CustomStencilTexture.Load(int3(PixelPos, 0)) STENCIL_COMPONENT_SWIZZLE; float SceneDepth = CalcSceneDepth(PixelPos); float4 AnisotropicData = SceneTexturesStruct.GBufferFTexture.Load(int3(PixelPos, 0)).xyzw; float4 InMRT1 = SceneTexturesStruct.GBufferETexture.Load(int3(PixelPos, 0)).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromPixelPos(PixelPos)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataSceneTextures(float2 UV, bool bGetNormalizedNormal = true) { uint CustomStencil = 0; float CustomNativeDepth = 0; float DeviceZ = SampleDeviceZFromSceneTexturesTempCopy(UV); float SceneDepth = ConvertFromDeviceZ(DeviceZ); float4 AnisotropicData = GBufferFTexture.SampleLevel(GBufferFTextureSampler, UV, 0).xyzw; float4 InMRT1 = GBufferETexture.SampleLevel(GBufferETextureSampler, UV, 0).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromSceneColorUV(UV)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataSceneTexturesLoad(uint2 PixelCoord, bool bGetNormalizedNormal = true) { uint CustomStencil = 0; float CustomNativeDepth = 0; float DeviceZ = SceneDepthTexture.Load(int3(PixelCoord, 0)).r; float SceneDepth = ConvertFromDeviceZ(DeviceZ); float4 AnisotropicData = GBufferFTexture.Load(int3(PixelCoord, 0)).xyzw; float4 InMRT1 = GBufferETexture.Load(int3(PixelCoord, 0)).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromPixelPos(PixelCoord)); return Ret; } #endif #endif #if GBUFFER_LAYOUT == 1 void EncodeGBufferToMRT(inout FPixelShaderOut Out, FGBufferData GBuffer, float QuantizationBias) { float4 MrtFloat1 = 0.0f; float4 MrtFloat2 = 0.0f; MrtFloat1.x = GBuffer.Velocity.x; MrtFloat1.y = GBuffer.Velocity.y; MrtFloat1.z = GBuffer.Velocity.z; MrtFloat1.w = GBuffer.Velocity.w; MrtFloat2.x = GBuffer.PrecomputedShadowFactors.x; MrtFloat2.y = GBuffer.PrecomputedShadowFactors.y; MrtFloat2.z = GBuffer.PrecomputedShadowFactors.z; MrtFloat2.w = GBuffer.PrecomputedShadowFactors.w; Out.MRT[1] = MrtFloat1; Out.MRT[2] = MrtFloat2; Out.MRT[3] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[4] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[5] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[6] = float4(0.0f, 0.0f, 0.0f, 0.0f); Out.MRT[7] = float4(0.0f, 0.0f, 0.0f, 0.0f); } FGBufferData DecodeGBufferDataDirect(float4 InMRT1, float4 InMRT2, float CustomNativeDepth, float4 AnisotropicData, uint CustomStencil, float SceneDepth, bool bGetNormalizedNormal, bool bChecker) { FGBufferData Ret = (FGBufferData)0; Ret.WorldNormal = float3(0,0,1); Ret.Depth = 0.f; Ret.ShadingModelID = 1; Ret.Velocity.x = InMRT1.x; Ret.Velocity.y = InMRT1.y; Ret.Velocity.z = InMRT1.z; Ret.Velocity.w = InMRT1.w; Ret.PrecomputedShadowFactors.x = InMRT2.x; Ret.PrecomputedShadowFactors.y = InMRT2.y; Ret.PrecomputedShadowFactors.z = InMRT2.z; Ret.PrecomputedShadowFactors.w = InMRT2.w; Ret.WorldTangent = AnisotropicData.xyz; Ret.Anisotropy = AnisotropicData.w; GBufferPostDecode(Ret,bChecker,bGetNormalizedNormal); Ret.CustomDepth = ConvertFromDeviceZ(CustomNativeDepth); Ret.CustomStencil = CustomStencil; Ret.Depth = SceneDepth; return Ret; } #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataUV(float2 UV, bool bGetNormalizedNormal = true) { float CustomNativeDepth = Texture2DSampleLevel(SceneTexturesStruct.CustomDepthTexture, SceneTexturesStruct_CustomDepthTextureSampler, UV, 0).r; int2 IntUV = (int2)trunc(UV * View.BufferSizeAndInvSize.xy * View.BufferToSceneTextureScale.xy); uint CustomStencil = SceneTexturesStruct.CustomStencilTexture.Load(int3(IntUV, 0)) STENCIL_COMPONENT_SWIZZLE; float SceneDepth = CalcSceneDepth(UV); float4 AnisotropicData = Texture2DSampleLevel(SceneTexturesStruct.GBufferFTexture, SceneTexturesStruct_GBufferFTextureSampler, UV, 0).xyzw; float4 InMRT1 = Texture2DSampleLevel(SceneTexturesStruct.GBufferVelocityTexture, SceneTexturesStruct_GBufferVelocityTextureSampler, UV, 0).xyzw; float4 InMRT2 = Texture2DSampleLevel(SceneTexturesStruct.GBufferETexture, SceneTexturesStruct_GBufferETextureSampler, UV, 0).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, InMRT2, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromSceneColorUV(UV)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataUint(uint2 PixelPos, bool bGetNormalizedNormal = true) { float CustomNativeDepth = SceneTexturesStruct.CustomDepthTexture.Load(int3(PixelPos, 0)).r; uint CustomStencil = SceneTexturesStruct.CustomStencilTexture.Load(int3(PixelPos, 0)) STENCIL_COMPONENT_SWIZZLE; float SceneDepth = CalcSceneDepth(PixelPos); float4 AnisotropicData = SceneTexturesStruct.GBufferFTexture.Load(int3(PixelPos, 0)).xyzw; float4 InMRT1 = SceneTexturesStruct.GBufferVelocityTexture.Load(int3(PixelPos, 0)).xyzw; float4 InMRT2 = SceneTexturesStruct.GBufferETexture.Load(int3(PixelPos, 0)).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, InMRT2, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromPixelPos(PixelPos)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataSceneTextures(float2 UV, bool bGetNormalizedNormal = true) { uint CustomStencil = 0; float CustomNativeDepth = 0; float DeviceZ = SampleDeviceZFromSceneTexturesTempCopy(UV); float SceneDepth = ConvertFromDeviceZ(DeviceZ); float4 AnisotropicData = GBufferFTexture.SampleLevel(GBufferFTextureSampler, UV, 0).xyzw; float4 InMRT1 = GBufferVelocityTexture.SampleLevel(GBufferVelocityTextureSampler, UV, 0).xyzw; float4 InMRT2 = GBufferETexture.SampleLevel(GBufferETextureSampler, UV, 0).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, InMRT2, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromSceneColorUV(UV)); return Ret; } // @param PixelPos relative to left top of the rendertarget (not viewport) FGBufferData DecodeGBufferDataSceneTexturesLoad(uint2 PixelCoord, bool bGetNormalizedNormal = true) { uint CustomStencil = 0; float CustomNativeDepth = 0; float DeviceZ = SceneDepthTexture.Load(int3(PixelCoord, 0)).r; float SceneDepth = ConvertFromDeviceZ(DeviceZ); float4 AnisotropicData = GBufferFTexture.Load(int3(PixelCoord, 0)).xyzw; float4 InMRT1 = GBufferVelocityTexture.Load(int3(PixelCoord, 0)).xyzw; float4 InMRT2 = GBufferETexture.Load(int3(PixelCoord, 0)).xyzw; FGBufferData Ret = DecodeGBufferDataDirect(InMRT1, InMRT2, CustomNativeDepth, AnisotropicData, CustomStencil, SceneDepth, bGetNormalizedNormal, CheckerFromPixelPos(PixelCoord)); return Ret; } #endif #endif