Skip to content

Commit

Permalink
[Renderer] The grid now writes into the FSR 2 reactive mask
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Oct 23, 2023
1 parent 01ca33d commit 425e646
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
35 changes: 24 additions & 11 deletions data/shaders/grid.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand All @@ -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);
Expand All @@ -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;
}
14 changes: 7 additions & 7 deletions data/shaders/line.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
20 changes: 12 additions & 8 deletions runtime/Rendering/Renderer_Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2091,18 +2099,14 @@ 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;
pso.shader_pixel = shader_p;
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;
Expand Down

0 comments on commit 425e646

Please sign in to comment.