From 425e6468a677893c54d49bfa5151bb6514e7fc18 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Mon, 23 Oct 2023 20:50:01 +0100 Subject: [PATCH] [Renderer] The grid now writes into the FSR 2 reactive mask --- data/shaders/grid.hlsl | 35 ++++++++++++++++++--------- data/shaders/line.hlsl | 14 +++++------ runtime/Rendering/Renderer_Passes.cpp | 20 +++++++++------ 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/data/shaders/grid.hlsl b/data/shaders/grid.hlsl index 9f4717651..fe8e8ed12 100644 --- a/data/shaders/grid.hlsl +++ b/data/shaders/grid.hlsl @@ -25,16 +25,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // properties static const float4 grid_color = float4(0.5f, 0.5f, 0.5f, 1.0f); -static const float fade_start = 0.3f; -static const float fade_end = 0.6f; +static const float fade_start = 0.1f; +static const float fade_end = 0.5f; static const float anti_moire_start = 0.0f; static const float anti_moire_strength = 100.0f; // increase line thickness the further away we get, this counteracts the moire pattern -float compute_anti_moire_factor(float2 uv, float moire_start, float moire_end) +float compute_anti_moire_factor(float2 uv, float moire_start) { float2 centered_uv = uv - 0.5f; float distance_from_center = length(centered_uv); + float moire_end = 1.0f; return lerp(1.0f, anti_moire_strength, saturate((distance_from_center - moire_start) / (moire_end - moire_start))); } @@ -46,14 +47,23 @@ float compute_fade_factor(float2 uv, float fade_start, float fade_end) return saturate((fade_end - distance_from_center) / (fade_end - fade_start)); } -float4 mainPS(Pixel_PosUv input) : SV_TARGET +struct PixelOutputType { - const float2 properties = pass_get_f3_value().xy; - const float line_interval = properties.x; - float line_thickness = properties.y; + float4 color : SV_Target0; + float fsr2_reactive_mask : SV_Target1; +}; +PixelOutputType mainPS(Pixel_PosUv input) +{ + const float2 properties = pass_get_f3_value().xy; + const float line_interval = properties.x; + float line_thickness = properties.y; + + float fade_factor = compute_fade_factor(input.uv, fade_start, fade_end); + float anti_moire_factor = compute_anti_moire_factor(input.uv, anti_moire_start); + // anti-moire - line_thickness *= compute_anti_moire_factor(input.uv, anti_moire_start, fade_end); + line_thickness *= anti_moire_factor; // calculate the modulated distance for both x and y coordinates float mod_x = fmod(input.uv.x, line_interval); @@ -66,7 +76,10 @@ float4 mainPS(Pixel_PosUv input) : SV_TARGET // combine line_x and line_y to decide if either is true, then color it as a line float is_line = max(1.0f - line_x, 1.0f - line_y); - // either grid_color or 0 based on is_line - float fade_factor = compute_fade_factor(input.uv, fade_start, fade_end); - return fade_factor * (is_line * grid_color + (1.0f - is_line) * 0.0f); + // write both on the color render target and the reactive mask + PixelOutputType output; + output.color = fade_factor * (is_line * grid_color + (1.0f - is_line) * 0.0f); + output.fsr2_reactive_mask = output.color.a; + + return output; } diff --git a/data/shaders/line.hlsl b/data/shaders/line.hlsl index 83a9da866..ce5dea912 100644 --- a/data/shaders/line.hlsl +++ b/data/shaders/line.hlsl @@ -23,12 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "common.hlsl" //==================== -struct PixelOutputType -{ - float4 color : SV_Target0; - float fsr2_reactive_mask : SV_Target1; -}; - Pixel_PosColor mainVS(Vertex_PosColor input) { Pixel_PosColor output; @@ -41,12 +35,18 @@ Pixel_PosColor mainVS(Vertex_PosColor input) return output; } +struct PixelOutputType +{ + float4 color : SV_Target0; + float fsr2_reactive_mask : SV_Target1; +}; + PixelOutputType mainPS(Pixel_PosColor input) { PixelOutputType output; output.color = input.color; - output.fsr2_reactive_mask = input.color.a; + output.fsr2_reactive_mask = output.color.a; return output; } diff --git a/runtime/Rendering/Renderer_Passes.cpp b/runtime/Rendering/Renderer_Passes.cpp index 9ccbc61f3..6cdba2e75 100644 --- a/runtime/Rendering/Renderer_Passes.cpp +++ b/runtime/Rendering/Renderer_Passes.cpp @@ -1305,9 +1305,15 @@ namespace Spartan } // Debug rendering (world grid, vectors, debugging etc) - Pass_Grid(cmd_list, get_render_out); - Pass_Lines(cmd_list, get_render_out); - Pass_Outline(cmd_list, get_render_out); + { + // clear the reactive mask since all the debug primitives have no motion vectors and need to be added to the mask + RHI_Texture* tex_reactive_mask = GetRenderTarget(Renderer_RenderTexture::fsr2_mask_reactive).get(); + cmd_list->ClearRenderTarget(tex_reactive_mask, 0, 0, false, Color::standard_black); + + Pass_Grid(cmd_list, get_render_out); + Pass_Lines(cmd_list, get_render_out); + Pass_Outline(cmd_list, get_render_out); + } } // Determine antialiasing modes @@ -2043,6 +2049,8 @@ namespace Spartan pso.rasterizer_state = GetRasterizerState(Renderer_RasterizerState::Solid_cull_none).get(); pso.blend_state = GetBlendState(Renderer_BlendState::Alpha).get(); pso.depth_stencil_state = GetDepthStencilState(Renderer_DepthStencilState::Depth_read).get(); + pso.render_target_color_textures[1] = GetRenderTarget(Renderer_RenderTexture::fsr2_mask_reactive).get(); + pso.clear_color[1] = rhi_color_load; pso.render_target_color_textures[0] = tex_out; pso.render_target_depth_texture = GetRenderTarget(Renderer_RenderTexture::gbuffer_depth).get(); pso.primitive_topology = RHI_PrimitiveTopology_Mode::TriangleList; @@ -2091,10 +2099,6 @@ namespace Spartan cmd_list->BeginTimeblock("lines"); - // clear the reactive mask - RHI_Texture* tex_reactive_mask = GetRenderTarget(Renderer_RenderTexture::fsr2_mask_reactive).get(); - cmd_list->ClearRenderTarget(tex_reactive_mask, 0, 0, false, Color::standard_black); - // define the pipeline state static RHI_PipelineState pso; pso.shader_vertex = shader_v; @@ -2102,7 +2106,7 @@ namespace Spartan pso.rasterizer_state = GetRasterizerState(Renderer_RasterizerState::Wireframe_cull_none).get(); pso.render_target_color_textures[0] = tex_out; pso.clear_color[0] = rhi_color_load; - pso.render_target_color_textures[1] = tex_reactive_mask; + pso.render_target_color_textures[1] = GetRenderTarget(Renderer_RenderTexture::fsr2_mask_reactive).get(); pso.clear_color[1] = rhi_color_load; pso.render_target_depth_texture = GetRenderTarget(Renderer_RenderTexture::gbuffer_depth).get(); pso.primitive_topology = RHI_PrimitiveTopology_Mode::LineList;