Files
UnrealEngineUWP/Engine/Shaders/Private/DistortApplyScreenPS.usf
Charles deRousiers 2257ad3325 Fix/Add split screen & VR support for Strata (i.e., Tiling/Lighting/SSS/Distortion).
This CL split the resources shared between views (non-buffer/tiled data) and resources unique to view (e.g., tile data). This is to ease overall setup, but we might re-regroup them later.

#rb none
#jira none
#preflight 62488f4d927e60e3418751db
#fyi sebastien.hillaire

[CL 19601993 by Charles deRousiers in ue5-main branch]
2022-04-02 14:31:01 -04:00

158 lines
5.3 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
/*=============================================================================
DistortApplyScreenPixelShader.usf: Pixel shader for rendering screen distortion pass
=============================================================================*/
#include "Common.ush"
#include "PostProcessCommon.ush"
#if USE_ROUGH_REFRACTION
#include "/Engine/Private/Strata/StrataStatisticalOperators.ush"
#endif
static const half InvDistortionScaleBias = 1 / 4.0f;
/**
* contains accumulated distortion values as
* R=positive horizontal offset
* G=positive vertical offset
* B=negative horizontal offset
* A=negative vertical offset
*/
Texture2D SceneColorTexture;
SamplerState SceneColorTextureSampler;
#if USE_MSAA
Texture2DMS<float4> DistortionMSAATexture;
Texture2DMS<float4> SceneColorMSAATexture;
Texture2DMS<float4> RoughnessScatterMSAATexture;
#else
Texture2D DistortionTexture;
SamplerState DistortionTextureSampler;
Texture2D<float4> RoughnessScatterTexture;
SamplerState RoughnessScatterSampler;
#endif
float RefractionRoughnessToMipLevelFactor;
/** distorts screen texture using accumulated distortion offsets */
void Main(
in noperspective float4 TexCoord: TEXCOORD0,
out float4 OutColor : SV_Target0
#if USE_MSAA
, in uint SampleIndex : SV_SampleIndex
#endif
)
{
// sample accumulated distortion and apply inverse scale
#if USE_MSAA
half4 AccumDist = DistortionMSAATexture.Load(int2(TexCoord.xy * View.BufferSizeAndInvSize.xy), SampleIndex);
#else
half4 AccumDist = Texture2DSample(DistortionTexture,DistortionTextureSampler,TexCoord.xy);
#endif
// apply inverse scale
// offset = [R-B,G-A]
half2 DistBufferUVOffset = (AccumDist.rg - AccumDist.ba);
//Scale by the screen size and a fudge factor to come close to the offset values we would have had under normal circumstances before my changes. Also flip Y and invert the precision bias scale.
DistBufferUVOffset *= InvDistortionScaleBias;
#if USE_ROUGH_REFRACTION
#if USE_MSAA
half RefractionLobeVariance = RoughnessScatterMSAATexture.Load(int2(TexCoord.xy * View.BufferSizeAndInvSize.xy), SampleIndex).x;
#else
half RefractionLobeVariance = Texture2DSample(RoughnessScatterTexture, RoughnessScatterSampler, TexCoord.xy).x;
#endif
half RefractionRoughness = saturate(StrataLobeVarianceToRoughness(RefractionLobeVariance));
half RefractionMipLevel = RefractionRoughness * RefractionRoughnessToMipLevelFactor;
const float2 NoDistortionCoord = (TexCoord.xy * View.BufferSizeAndInvSize.xy - View.ViewRectMin);
const float2 NoDistortionUVs = NoDistortionCoord * View.ViewSizeAndInvSize.zw;
float2 NewBufferUV = NoDistortionUVs + DistBufferUVOffset;
FLATTEN if (any(NewBufferUV < 0.0f) || any(NewBufferUV > 1.0f))
{
NewBufferUV = NoDistortionUVs;
}
#if USE_MSAA
if (RefractionMipLevel < 0.001f)
{
// Sample the full res MSAA source texture
OutColor = SceneColorMSAATexture.Load(int2(NoDistortionCoord), SampleIndex);
}
else
{
OutColor = Texture2DSampleLevel(SceneColorTexture, SceneColorTextureSampler, NewBufferUV, RefractionMipLevel);
}
#else
OutColor = Texture2DSampleLevel(SceneColorTexture, SceneColorTextureSampler, NewBufferUV, RefractionMipLevel);
#endif
#else // USE_ROUGH_REFRACTION
float2 NewBufferUV = TexCoord.xy + DistBufferUVOffset;
// If we're about to sample outside the valid BufferUVMinMax, set to 0 distortion.
FLATTEN if ( NewBufferUV.x < View.BufferBilinearUVMinMax.x || NewBufferUV.x > View.BufferBilinearUVMinMax.z ||
NewBufferUV.y < View.BufferBilinearUVMinMax.y || NewBufferUV.y > View.BufferBilinearUVMinMax.w )
{
NewBufferUV = TexCoord.xy;
}
// sample screen using offset coords
#if USE_MSAA
OutColor = SceneColorMSAATexture.Load(int2(NewBufferUV * View.BufferSizeAndInvSize.xy), SampleIndex);
#else
OutColor = Texture2DSample(SceneColorTexture, SceneColorTextureSampler, NewBufferUV);
#endif
#endif // USE_ROUGH_REFRACTION
}
void Merge(
in noperspective float4 TexCoord: TEXCOORD0,
out float4 OutColor : SV_Target0
#if USE_MSAA
, in uint SampleIndex : SV_SampleIndex
#endif
)
{
#if USE_MSAA
OutColor = SceneColorMSAATexture.Load(int2(View.BufferSizeAndInvSize.xy * TexCoord.xy), SampleIndex);
#else
OutColor = Texture2DSample(SceneColorTexture, SceneColorTextureSampler, TexCoord.xy);
#endif
}
Texture2D DistortionAccumulateTexture;
SamplerState DistortionAccumulateSampler;
void Merge_Mobile(
in noperspective float4 TexCoord: TEXCOORD0,
out HALF4_TYPE OutColor : SV_Target0
)
{
half4 AccumDist = DistortionAccumulateTexture.Sample(DistortionAccumulateSampler, TexCoord.xy);
// apply inverse scale
// offset = [R-B,G-A]
half2 DistBufferUVOffset = (AccumDist.rg - AccumDist.ba);
//Scale by the screen size and a fudge factor to come close to the offset values we would have had under normal circumstances before my changes. Also flip Y and invert the precision bias scale.
DistBufferUVOffset *= InvDistortionScaleBias;
float2 NewBufferUV = TexCoord.xy + DistBufferUVOffset;
// If we're about to sample outside the valid BufferUVMinMax, set to 0 distortion.
FLATTEN if ( NewBufferUV.x < View.BufferBilinearUVMinMax.x || NewBufferUV.x > View.BufferBilinearUVMinMax.z ||
NewBufferUV.y < View.BufferBilinearUVMinMax.y || NewBufferUV.y > View.BufferBilinearUVMinMax.w )
{
NewBufferUV = TexCoord.xy;
}
OutColor = SceneColorTexture.Sample(SceneColorTextureSampler, NewBufferUV.xy);
}