2014-12-07 19:09:38 -05:00
|
|
|
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
/*=============================================================================
|
|
|
|
|
PostProcessUpscale.cpp: Post processing Upscale implementation.
|
|
|
|
|
=============================================================================*/
|
|
|
|
|
|
|
|
|
|
#include "RendererPrivate.h"
|
|
|
|
|
#include "ScenePrivate.h"
|
|
|
|
|
#include "SceneFilterRendering.h"
|
|
|
|
|
#include "PostProcessUpscale.h"
|
|
|
|
|
#include "PostProcessing.h"
|
|
|
|
|
#include "PostProcessHistogram.h"
|
|
|
|
|
#include "PostProcessEyeAdaptation.h"
|
2014-08-21 06:03:00 -04:00
|
|
|
#include "SceneUtils.h"
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
static TAutoConsoleVariable<float> CVarUpscaleSoftness(
|
|
|
|
|
TEXT("r.Upscale.Softness"),
|
2014-06-25 05:47:33 -04:00
|
|
|
0.3f,
|
2014-12-08 09:45:54 -05:00
|
|
|
TEXT("To scale up with higher quality losing some sharpness\n")
|
2014-06-25 05:47:33 -04:00
|
|
|
TEXT(" 0..1 (0.3 is good for ScreenPercentage 90"),
|
|
|
|
|
ECVF_Scalability | ECVF_RenderThreadSafe);
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
/** Encapsulates the upscale vertex shader. */
|
|
|
|
|
class FPostProcessUpscaleVS : public FPostProcessVS
|
|
|
|
|
{
|
|
|
|
|
DECLARE_SHADER_TYPE(FPostProcessUpscaleVS,Global);
|
|
|
|
|
|
|
|
|
|
/** Default constructor. */
|
|
|
|
|
FPostProcessUpscaleVS() {}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
FShaderParameter DistortionParams;
|
|
|
|
|
|
|
|
|
|
/** Initialization constructor. */
|
|
|
|
|
FPostProcessUpscaleVS(const ShaderMetaType::CompiledShaderInitializerType& Initializer)
|
|
|
|
|
: FPostProcessVS(Initializer)
|
|
|
|
|
{
|
|
|
|
|
DistortionParams.Bind(Initializer.ParameterMap,TEXT("DistortionParams"));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-04 08:07:19 -05:00
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
|
|
|
{
|
2014-12-08 09:45:54 -05:00
|
|
|
return true;
|
2014-11-04 08:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
static void ModifyCompilationEnvironment(EShaderPlatform Platform, FShaderCompilerEnvironment& OutEnvironment)
|
|
|
|
|
{
|
|
|
|
|
FPostProcessVS::ModifyCompilationEnvironment(Platform,OutEnvironment);
|
|
|
|
|
OutEnvironment.SetDefine(TEXT("TESS_RECT_X"), FTesselatedScreenRectangleIndexBuffer::Width);
|
|
|
|
|
OutEnvironment.SetDefine(TEXT("TESS_RECT_Y"), FTesselatedScreenRectangleIndexBuffer::Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @param InCylinderDistortion 0=none..1=full in percent, must be in that range
|
|
|
|
|
void SetParameters(const FRenderingCompositePassContext& Context, float InCylinderDistortion)
|
|
|
|
|
{
|
|
|
|
|
const FVertexShaderRHIParamRef ShaderRHI = GetVertexShader();
|
|
|
|
|
|
|
|
|
|
FGlobalShader::SetParameters(Context.RHICmdList, ShaderRHI, Context.View);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
float HalfFOV = FMath::Atan(1.0f / Context.View.ViewMatrices.ProjMatrix.M[0][0]);
|
|
|
|
|
float TanHalfFov = 1.0f / Context.View.ViewMatrices.ProjMatrix.M[0][0];
|
|
|
|
|
float InvHalfFov = 1.0f / HalfFOV;
|
|
|
|
|
|
|
|
|
|
// compute Correction to scale Y the same as X is scaled in the center
|
|
|
|
|
float SmallX = 0.01f;
|
|
|
|
|
float Correction = atan(SmallX * TanHalfFov) * InvHalfFov / SmallX;
|
|
|
|
|
|
|
|
|
|
FVector4 Value(HalfFOV, TanHalfFov, InCylinderDistortion, Correction);
|
|
|
|
|
|
|
|
|
|
SetShaderValue(Context.RHICmdList, ShaderRHI, DistortionParams, Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 07:20:55 -04:00
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
2014-11-03 16:17:18 -05:00
|
|
|
{
|
|
|
|
|
bool bShaderHasOutdatedParameters = FPostProcessVS::Serialize(Ar);
|
|
|
|
|
Ar << DistortionParams;
|
|
|
|
|
return bShaderHasOutdatedParameters;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_SHADER_TYPE(,FPostProcessUpscaleVS,TEXT("PostProcessUpscale"),TEXT("MainVS"),SF_Vertex);
|
|
|
|
|
|
|
|
|
|
/** Encapsulates the post processing upscale pixel shader. */
|
2014-03-14 14:13:41 -04:00
|
|
|
template <uint32 Method>
|
|
|
|
|
class FPostProcessUpscalePS : public FGlobalShader
|
|
|
|
|
{
|
|
|
|
|
DECLARE_SHADER_TYPE(FPostProcessUpscalePS, Global);
|
|
|
|
|
|
|
|
|
|
static bool ShouldCache(EShaderPlatform Platform)
|
|
|
|
|
{
|
2014-12-08 09:45:54 -05:00
|
|
|
// Always allow simple bilinear upscale. (Provides upscaling for ES2 emulation)
|
|
|
|
|
if (Method == 1)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 14:41:54 -04:00
|
|
|
return IsFeatureLevelSupported(Platform, ERHIFeatureLevel::SM4);
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ModifyCompilationEnvironment(EShaderPlatform Platform, FShaderCompilerEnvironment& OutEnvironment)
|
|
|
|
|
{
|
2014-06-05 16:38:54 -04:00
|
|
|
FGlobalShader::ModifyCompilationEnvironment(Platform,OutEnvironment);
|
2014-03-14 14:13:41 -04:00
|
|
|
OutEnvironment.SetDefine(TEXT("METHOD"), Method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Default constructor. */
|
|
|
|
|
FPostProcessUpscalePS() {}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
FPostProcessPassParameters PostprocessParameter;
|
|
|
|
|
FDeferredPixelShaderParameters DeferredParameters;
|
|
|
|
|
FShaderParameter UpscaleSoftness;
|
|
|
|
|
|
|
|
|
|
/** Initialization constructor. */
|
|
|
|
|
FPostProcessUpscalePS(const ShaderMetaType::CompiledShaderInitializerType& Initializer)
|
|
|
|
|
: FGlobalShader(Initializer)
|
|
|
|
|
{
|
|
|
|
|
PostprocessParameter.Bind(Initializer.ParameterMap);
|
|
|
|
|
DeferredParameters.Bind(Initializer.ParameterMap);
|
|
|
|
|
UpscaleSoftness.Bind(Initializer.ParameterMap,TEXT("UpscaleSoftness"));
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 07:13:34 -04:00
|
|
|
void SetPS(const FRenderingCompositePassContext& Context)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
const FPixelShaderRHIParamRef ShaderRHI = GetPixelShader();
|
|
|
|
|
|
2014-06-12 07:13:34 -04:00
|
|
|
FGlobalShader::SetParameters(Context.RHICmdList, ShaderRHI, Context.View);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
FSamplerStateRHIParamRef FilterTable[2];
|
|
|
|
|
FilterTable[0] = TStaticSamplerState<SF_Bilinear,AM_Clamp,AM_Clamp,AM_Clamp>::GetRHI();
|
|
|
|
|
FilterTable[1] = TStaticSamplerState<SF_Point,AM_Clamp,AM_Clamp,AM_Clamp>::GetRHI();
|
|
|
|
|
|
2014-06-12 07:13:34 -04:00
|
|
|
PostprocessParameter.SetPS(ShaderRHI, Context, 0, false, FilterTable);
|
|
|
|
|
DeferredParameters.Set(Context.RHICmdList, ShaderRHI, Context.View);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
|
|
|
|
{
|
2014-11-03 16:17:18 -05:00
|
|
|
float UpscaleSoftnessValue = FMath::Clamp(CVarUpscaleSoftness.GetValueOnRenderThread(), 0.0f, 1.0f);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-06-12 07:13:34 -04:00
|
|
|
SetShaderValue(Context.RHICmdList, ShaderRHI, UpscaleSoftness, UpscaleSoftnessValue);
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FShader interface.
|
2015-04-01 07:20:55 -04:00
|
|
|
virtual bool Serialize(FArchive& Ar) override
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);
|
|
|
|
|
Ar << PostprocessParameter << DeferredParameters << UpscaleSoftness;
|
|
|
|
|
return bShaderHasOutdatedParameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const TCHAR* GetSourceFilename()
|
|
|
|
|
{
|
|
|
|
|
return TEXT("PostProcessUpscale");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const TCHAR* GetFunctionName()
|
|
|
|
|
{
|
|
|
|
|
return TEXT("MainPS");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #define avoids a lot of code duplication
|
|
|
|
|
#define VARIATION1(A) typedef FPostProcessUpscalePS<A> FPostProcessUpscalePS##A; \
|
|
|
|
|
IMPLEMENT_SHADER_TYPE2(FPostProcessUpscalePS##A, SF_Pixel);
|
|
|
|
|
|
|
|
|
|
VARIATION1(0)
|
|
|
|
|
VARIATION1(1)
|
|
|
|
|
VARIATION1(2)
|
|
|
|
|
VARIATION1(3)
|
|
|
|
|
|
|
|
|
|
#undef VARIATION1
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
FRCPassPostProcessUpscale::FRCPassPostProcessUpscale(uint32 InUpscaleQuality, float InCylinderDistortion)
|
|
|
|
|
: UpscaleQuality(InUpscaleQuality)
|
|
|
|
|
, CylinderDistortion(FMath::Clamp(InCylinderDistortion, 0.0f, 1.0f))
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
template <uint32 Method, uint32 bTesselatedQuad>
|
|
|
|
|
FShader* FRCPassPostProcessUpscale::SetShader(const FRenderingCompositePassContext& Context, float InCylinderDistortion)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2014-11-03 16:17:18 -05:00
|
|
|
if(bTesselatedQuad)
|
|
|
|
|
{
|
|
|
|
|
check(InCylinderDistortion > 0.0f);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
TShaderMapRef<FPostProcessUpscaleVS> VertexShader(Context.GetShaderMap());
|
|
|
|
|
TShaderMapRef<FPostProcessUpscalePS<Method> > PixelShader(Context.GetShaderMap());
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
static FGlobalBoundShaderState BoundShaderState;
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
SetGlobalBoundShaderState(Context.RHICmdList, Context.GetFeatureLevel(), BoundShaderState, GFilterVertexDeclaration.VertexDeclarationRHI, *VertexShader, *PixelShader);
|
|
|
|
|
|
|
|
|
|
PixelShader->SetPS(Context);
|
|
|
|
|
VertexShader->SetParameters(Context, InCylinderDistortion);
|
|
|
|
|
return *VertexShader;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
check(InCylinderDistortion == 0.0f);
|
|
|
|
|
|
|
|
|
|
TShaderMapRef<FPostProcessVS> VertexShader(Context.GetShaderMap());
|
|
|
|
|
TShaderMapRef<FPostProcessUpscalePS<Method> > PixelShader(Context.GetShaderMap());
|
|
|
|
|
|
|
|
|
|
static FGlobalBoundShaderState BoundShaderState;
|
|
|
|
|
|
|
|
|
|
SetGlobalBoundShaderState(Context.RHICmdList, Context.GetFeatureLevel(), BoundShaderState, GFilterVertexDeclaration.VertexDeclarationRHI, *VertexShader, *PixelShader);
|
|
|
|
|
|
|
|
|
|
PixelShader->SetPS(Context);
|
|
|
|
|
VertexShader->SetParameters(Context);
|
|
|
|
|
return *VertexShader;
|
|
|
|
|
}
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FRCPassPostProcessUpscale::Process(FRenderingCompositePassContext& Context)
|
|
|
|
|
{
|
2014-10-20 10:43:43 -04:00
|
|
|
SCOPED_DRAW_EVENT(Context.RHICmdList, PostProcessUpscale);
|
2014-03-14 14:13:41 -04:00
|
|
|
const FPooledRenderTargetDesc* InputDesc = GetInputDesc(ePId_Input0);
|
|
|
|
|
|
|
|
|
|
if(!InputDesc)
|
|
|
|
|
{
|
|
|
|
|
// input is not hooked up correctly
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FSceneView& View = Context.View;
|
|
|
|
|
const FSceneViewFamily& ViewFamily = *(View.Family);
|
|
|
|
|
|
|
|
|
|
FIntRect SrcRect = View.ViewRect;
|
|
|
|
|
FIntRect DestRect = View.UnscaledViewRect;
|
|
|
|
|
FIntPoint SrcSize = InputDesc->Extent;
|
|
|
|
|
|
|
|
|
|
const FSceneRenderTargetItem& DestRenderTarget = PassOutputs[0].RequestSurface(Context);
|
|
|
|
|
|
|
|
|
|
// Set the view family's render target/viewport.
|
2014-06-12 07:13:34 -04:00
|
|
|
SetRenderTarget(Context.RHICmdList, DestRenderTarget.TargetableTexture, FTextureRHIRef());
|
2014-03-14 14:13:41 -04:00
|
|
|
Context.SetViewportAndCallRHI(DestRect);
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
bool bTessellatedQuad = CylinderDistortion >= 0.01f;
|
|
|
|
|
|
|
|
|
|
// with distortion (bTessellatedQuad) we need to clear the background
|
|
|
|
|
FIntRect ExcludeRect = bTessellatedQuad ? FIntRect() : View.ViewRect;
|
|
|
|
|
|
|
|
|
|
Context.RHICmdList.Clear(true, FLinearColor::Black, false, 1.0f, false, 0, ExcludeRect);
|
|
|
|
|
|
2014-03-14 14:13:41 -04:00
|
|
|
// set the state
|
2014-06-12 07:13:34 -04:00
|
|
|
Context.RHICmdList.SetBlendState(TStaticBlendState<>::GetRHI());
|
|
|
|
|
Context.RHICmdList.SetRasterizerState(TStaticRasterizerState<>::GetRHI());
|
|
|
|
|
Context.RHICmdList.SetDepthStencilState(TStaticDepthStencilState<false, CF_Always>::GetRHI());
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
FShader* VertexShader = 0;
|
|
|
|
|
|
|
|
|
|
if(bTessellatedQuad)
|
2014-03-14 14:13:41 -04:00
|
|
|
{
|
2014-11-03 16:17:18 -05:00
|
|
|
switch (UpscaleQuality)
|
|
|
|
|
{
|
|
|
|
|
case 0: VertexShader = SetShader<0, 1>(Context, CylinderDistortion); break;
|
|
|
|
|
case 1: VertexShader = SetShader<1, 1>(Context, CylinderDistortion); break;
|
|
|
|
|
case 2: VertexShader = SetShader<2, 1>(Context, CylinderDistortion); break;
|
|
|
|
|
case 3: VertexShader = SetShader<3, 1>(Context, CylinderDistortion); break;
|
|
|
|
|
default:
|
|
|
|
|
checkNoEntry();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (UpscaleQuality)
|
|
|
|
|
{
|
|
|
|
|
case 0: VertexShader = SetShader<0, 0>(Context); break;
|
|
|
|
|
case 1: VertexShader = SetShader<1, 0>(Context); break;
|
|
|
|
|
case 2: VertexShader = SetShader<2, 0>(Context); break;
|
|
|
|
|
case 3: VertexShader = SetShader<3, 0>(Context); break;
|
|
|
|
|
default:
|
|
|
|
|
checkNoEntry();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-03 16:17:18 -05:00
|
|
|
// Draw a quad, a triangle or a tessellated quad
|
2014-03-14 14:13:41 -04:00
|
|
|
DrawRectangle(
|
2014-06-12 07:13:34 -04:00
|
|
|
Context.RHICmdList,
|
2014-03-14 14:13:41 -04:00
|
|
|
0, 0,
|
|
|
|
|
DestRect.Width(), DestRect.Height(),
|
|
|
|
|
SrcRect.Min.X, SrcRect.Min.Y,
|
|
|
|
|
SrcRect.Width(), SrcRect.Height(),
|
|
|
|
|
DestRect.Size(),
|
|
|
|
|
SrcSize,
|
2014-11-03 16:17:18 -05:00
|
|
|
VertexShader,
|
|
|
|
|
bTessellatedQuad ? EDRF_UseTesselatedIndexBuffer: EDRF_UseTriangleOptimization);
|
2014-03-14 14:13:41 -04:00
|
|
|
|
2014-06-12 07:13:34 -04:00
|
|
|
Context.RHICmdList.CopyToResolveTarget(DestRenderTarget.TargetableTexture, DestRenderTarget.ShaderResourceTexture, false, FResolveParams());
|
2014-03-14 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FPooledRenderTargetDesc FRCPassPostProcessUpscale::ComputeOutputDesc(EPassOutputId InPassOutputId) const
|
|
|
|
|
{
|
|
|
|
|
FPooledRenderTargetDesc Ret = PassInputs[0].GetOutput()->RenderTargetDesc;
|
|
|
|
|
|
|
|
|
|
Ret.Reset();
|
|
|
|
|
Ret.DebugName = TEXT("Upscale");
|
|
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
|
}
|