diff --git a/Source/Engine/Graphics/Enums.h b/Source/Engine/Graphics/Enums.h index 86b5e067d..af03678e9 100644 --- a/Source/Engine/Graphics/Enums.h +++ b/Source/Engine/Graphics/Enums.h @@ -609,6 +609,9 @@ API_ENUM() enum class MaterialPostFxLocation : byte /// The 'after' AA filter pass. Rendering is done to the output backbuffer. /// AfterAntiAliasingPass = 5, + + API_ENUM(Attributes="HideInEditor") + MAX, }; /// @@ -637,9 +640,17 @@ API_ENUM() enum class PostProcessEffectLocation BeforeReflectionsPass = 3, /// - /// The 'after' AA filter pass. Rendering is done to the output backbuffer. + /// The 'after' AA filter pass. /// AfterAntiAliasingPass = 4, + + /// + /// The custom frame up-scaling that replaces default implementation. Rendering is done to the output backbuffer (use OutputView and OutputViewport as render destination). + /// + CustomUpscale = 5, + + API_ENUM(Attributes="HideInEditor") + MAX, }; /// diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index fe5126c6d..67aaf4778 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -326,16 +326,8 @@ void RenderList::RunCustomPostFxPass(GPUContext* context, RenderContext& renderC } } -bool RenderList::HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLocation postProcess, MaterialPostFxLocation materialPostFx) const +bool RenderList::HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLocation postProcess) const { - for (int32 i = 0; i < Settings.PostFxMaterials.Materials.Count(); i++) - { - auto material = Settings.PostFxMaterials.Materials[i].Get(); - if (material && material->IsReady() && material->IsPostFx() && material->GetInfo().PostFxLocation == materialPostFx) - { - return true; - } - } if (renderContext.View.Flags & ViewFlags::CustomPostProcess) { for (int32 i = 0; i < renderContext.List->PostFx.Count(); i++) @@ -347,7 +339,19 @@ bool RenderList::HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLoc } } } + return false; +} +bool RenderList::HasAnyPostFx(RenderContext& renderContext, MaterialPostFxLocation materialPostFx) const +{ + for (int32 i = 0; i < Settings.PostFxMaterials.Materials.Count(); i++) + { + auto material = Settings.PostFxMaterials.Materials[i].Get(); + if (material && material->IsReady() && material->IsPostFx() && material->GetInfo().PostFxLocation == materialPostFx) + { + return true; + } + } return false; } diff --git a/Source/Engine/Renderer/RenderList.h b/Source/Engine/Renderer/RenderList.h index 3dfc7eb23..3d372b09e 100644 --- a/Source/Engine/Renderer/RenderList.h +++ b/Source/Engine/Renderer/RenderList.h @@ -488,6 +488,22 @@ public: /// The output texture. void RunCustomPostFxPass(GPUContext* context, RenderContext& renderContext, PostProcessEffectLocation location, GPUTexture*& input, GPUTexture*& output); + /// + /// Determines whether any Custom PostFx specified by given type. Used to pick a faster rendering path by the frame rendering module. + /// + /// The rendering context. + /// The PostFx location to check (for scripts). + /// True if render any postFx of the given type, otherwise false. + bool HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLocation postProcess) const; + + /// + /// Determines whether any Material PostFx specified by given type. Used to pick a faster rendering path by the frame rendering module. + /// + /// The rendering context. + /// The PostFx location to check (for materials). + /// True if render any postFx of the given type, otherwise false. + bool HasAnyPostFx(RenderContext& renderContext, MaterialPostFxLocation materialPostFx) const; + /// /// Determines whether any Custom PostFx or Material PostFx specified by given type. Used to pick a faster rendering path by the frame rendering module. /// @@ -495,7 +511,10 @@ public: /// The PostFx location to check (for scripts). /// The PostFx location to check (for materials). /// True if render any postFx of the given type, otherwise false. - bool HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLocation postProcess, MaterialPostFxLocation materialPostFx) const; + bool HasAnyPostFx(RenderContext& renderContext, PostProcessEffectLocation postProcess, MaterialPostFxLocation materialPostFx) const + { + return HasAnyPostFx(renderContext, postProcess) || HasAnyPostFx(renderContext, materialPostFx); + } public: diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index 7e7a2908c..f9dc824dd 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -112,9 +112,7 @@ void RendererService::Dispose() void RenderAntiAliasingPass(RenderContext& renderContext, GPUTexture* input, GPUTextureView* output) { auto context = GPUDevice::Instance->GetMainContext(); - const auto width = (float)renderContext.Buffers->GetWidth(); - const auto height = (float)renderContext.Buffers->GetHeight(); - context->SetViewportAndScissors(width, height); + context->SetViewportAndScissors(renderContext.View.ScreenSize.X, renderContext.View.ScreenSize.Y); const auto aaMode = renderContext.List->Settings.AntiAliasing.Mode; if (aaMode == AntialiasingMode::FastApproximateAntialiasing) @@ -128,7 +126,6 @@ void RenderAntiAliasingPass(RenderContext& renderContext, GPUTexture* input, GPU else { PROFILE_GPU("Copy frame"); - context->SetRenderTarget(output); context->Draw(input); } @@ -470,6 +467,11 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext) renderContext.List->RunCustomPostFxPass(context, renderContext, PostProcessEffectLocation::Default, frameBuffer, tempBuffer); renderContext.List->RunMaterialPostFxPass(context, renderContext, MaterialPostFxLocation::AfterCustomPostEffects, frameBuffer, tempBuffer); + // Cleanup + context->ResetRenderTarget(); + context->ResetSR(); + context->FlushState(); + // Debug motion vectors if (renderContext.View.Mode == ViewMode::MotionVectors) { @@ -500,9 +502,13 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext) { PROFILE_GPU("Copy frame"); context->SetRenderTarget(task->GetOutputView()); - context->SetViewportAndScissors(task->GetViewport()); + context->SetViewportAndScissors(task->GetOutputViewport()); context->Draw(frameBuffer); } + else if (renderContext.List->HasAnyPostFx(renderContext, PostProcessEffectLocation::CustomUpscale, MaterialPostFxLocation::MAX)) + { + renderContext.List->RunCustomPostFxPass(context, renderContext, PostProcessEffectLocation::CustomUpscale, frameBuffer, frameBuffer); + } else { MultiScaler::Instance()->Upscale(context, task->GetOutputViewport(), frameBuffer, task->GetOutputView()); diff --git a/Source/Shaders/MultiScaler.shader b/Source/Shaders/MultiScaler.shader index 35ce48cbd..269231d29 100644 --- a/Source/Shaders/MultiScaler.shader +++ b/Source/Shaders/MultiScaler.shader @@ -3,10 +3,8 @@ #include "./Flax/Common.hlsl" META_CB_BEGIN(0, Data) - float2 TexelSize; float2 Padding; - META_CB_END // Use linear sampling (less texture fetches required) @@ -70,7 +68,7 @@ META_PS(true, FEATURE_LEVEL_ES2) META_PERMUTATION_1(CONVOLVE_VERTICAL=0) META_PERMUTATION_1(CONVOLVE_VERTICAL=1) float4 PS_Blur9(Quad_VS2PS input) : SV_Target0 -{ +{ const float offsets[3] = { 0.00000000, 1.38461538, @@ -106,7 +104,7 @@ META_PS(true, FEATURE_LEVEL_ES2) META_PERMUTATION_1(CONVOLVE_VERTICAL=0) META_PERMUTATION_1(CONVOLVE_VERTICAL=1) float4 PS_Blur13(Quad_VS2PS input) : SV_Target0 -{ +{ const float offsets[4] = { 0.00000000, 1.41176471, @@ -138,3 +136,65 @@ float4 PS_Blur13(Quad_VS2PS input) : SV_Target0 return color; } + +// Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16. +// See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details +// The following code is licensed under the MIT license: https://gist.github.com/TheRealMJP/bc503b0b87b643d3505d41eab8b332ae +// Source: https://gist.github.com/TheRealMJP/c83b8c0f46b63f3a88a5986f4fa982b1 +float4 SampleTextureCatmullRom(Texture2D tex, SamplerState linearSampler, float2 uv, float2 texelSize) +{ + // We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding + // down the sample location to get the exact center of our "starting" texel. The starting texel will be at + // location [1, 1] in the grid, where [0, 0] is the top left corner. + float2 samplePos = uv / texelSize; + float2 texPos1 = floor(samplePos - 0.5f) + 0.5f; + + // Compute the fractional offset from our starting texel to our original sample location, which we'll + // feed into the Catmull-Rom spline function to get our filter weights. + float2 f = samplePos - texPos1; + + // Compute the Catmull-Rom weights using the fractional offset that we calculated earlier. + // These equations are pre-expanded based on our knowledge of where the texels will be located, + // which lets us avoid having to evaluate a piece-wise function. + float2 w0 = f * (-0.5f + f * (1.0f - 0.5f * f)); + float2 w1 = 1.0f + f * f * (-2.5f + 1.5f * f); + float2 w2 = f * (0.5f + f * (2.0f - 1.5f * f)); + float2 w3 = f * f * (-0.5f + 0.5f * f); + + // Work out weighting factors and sampling offsets that will let us use bilinear filtering to + // simultaneously evaluate the middle 2 samples from the 4x4 grid. + float2 w12 = w1 + w2; + float2 offset12 = w2 / (w1 + w2); + + // Compute the final UV coordinates we'll use for sampling the texture + float2 texPos0 = texPos1 - 1; + float2 texPos3 = texPos1 + 2; + float2 texPos12 = texPos1 + offset12; + + texPos0 *= texelSize; + texPos3 *= texelSize; + texPos12 *= texelSize; + + float4 result = 0.0f; + result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos0.y), 0.0f) * w0.x * w0.y; + result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos0.y), 0.0f) * w12.x * w0.y; + result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos0.y), 0.0f) * w3.x * w0.y; + + result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos12.y), 0.0f) * w0.x * w12.y; + result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos12.y), 0.0f) * w12.x * w12.y; + result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos12.y), 0.0f) * w3.x * w12.y; + + result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos3.y), 0.0f) * w0.x * w3.y; + result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos3.y), 0.0f) * w12.x * w3.y; + result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos3.y), 0.0f) * w3.x * w3.y; + + return result; +} + +// Pixel Shader for upscaling image +META_PS(true, FEATURE_LEVEL_ES2) +float4 PS_Upscale(Quad_VS2PS input) : SV_Target0 +{ + // Catmull-Rom filtering with 9-taps + return SampleTextureCatmullRom(Input, SamplerLinearClamp, input.TexCoord, TexelSize); +}