From 186873420f9f10c65814db8d3a3388cbd21f8444 Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 20 Aug 2015 10:10:35 -0500 Subject: GPU/Rasterizer: Corrected the stencil implementation. Verified the behavior with hardware tests. --- src/video_core/rasterizer.cpp | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index b83798b0f..000b4542b 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -215,14 +215,25 @@ static void SetStencil(int x, int y, u8 value) { } } -// TODO: Should the stencil mask be applied to the "dest" or "ref" operands? Most likely not! -static u8 PerformStencilAction(Regs::StencilAction action, u8 dest, u8 ref) { +// TODO: Should the stencil mask be applied to the "old_stencil" or "ref" operands? Most likely not! +static u8 PerformStencilAction(Regs::StencilAction action, u8 old_stencil, u8 ref) { switch (action) { case Regs::StencilAction::Keep: - return dest; + return old_stencil; - case Regs::StencilAction::Xor: - return dest ^ ref; + case Regs::StencilAction::Replace: + return ref; + + case Regs::StencilAction::Increment: + // Saturated increment + return std::min(old_stencil, 254) + 1; + + case Regs::StencilAction::Decrement: + // Saturated decrement + return std::max(old_stencil, 1) - 1; + + case Regs::StencilAction::Invert: + return ~old_stencil; default: LOG_CRITICAL(HW_GPU, "Unknown stencil action %x", (int)action); @@ -782,8 +793,8 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, u8 old_stencil = 0; if (stencil_action_enable) { old_stencil = GetStencil(x >> 4, y >> 4); - u8 dest = old_stencil & stencil_test.mask; - u8 ref = stencil_test.reference_value & stencil_test.mask; + u8 dest = old_stencil & stencil_test.input_mask; + u8 ref = stencil_test.reference_value & stencil_test.input_mask; bool pass = false; switch (stencil_test.func) { @@ -821,8 +832,8 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, } if (!pass) { - u8 new_stencil = PerformStencilAction(stencil_test.action_stencil_fail, old_stencil, stencil_test.replacement_value); - SetStencil(x >> 4, y >> 4, new_stencil); + u8 new_stencil = PerformStencilAction(stencil_test.action_stencil_fail, old_stencil, stencil_test.reference_value); + SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); continue; } } @@ -873,8 +884,8 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, if (!pass) { if (stencil_action_enable) { - u8 new_stencil = PerformStencilAction(stencil_test.action_depth_fail, old_stencil, stencil_test.replacement_value); - SetStencil(x >> 4, y >> 4, new_stencil); + u8 new_stencil = PerformStencilAction(stencil_test.action_depth_fail, old_stencil, stencil_test.reference_value); + SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); } continue; } @@ -884,8 +895,8 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, if (stencil_action_enable) { // TODO: What happens if stencil testing is enabled, but depth testing is not? Will stencil get updated anyway? - u8 new_stencil = PerformStencilAction(stencil_test.action_depth_pass, old_stencil, stencil_test.replacement_value); - SetStencil(x >> 4, y >> 4, new_stencil); + u8 new_stencil = PerformStencilAction(stencil_test.action_depth_pass, old_stencil, stencil_test.reference_value); + SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); } } -- cgit v1.2.3 From e74825e3d09c924f03ff63408bccefbccab19bae Mon Sep 17 00:00:00 2001 From: Subv Date: Thu, 20 Aug 2015 10:31:39 -0500 Subject: Rasterizer: Abstract duplicated stencil code into a lambda. --- src/video_core/rasterizer.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 000b4542b..c768a5eea 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -791,6 +791,12 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, } u8 old_stencil = 0; + + auto UpdateStencil = [stencil_test, x, y, &old_stencil](Pica::Regs::StencilAction action) { + u8 new_stencil = PerformStencilAction(action, old_stencil, stencil_test.reference_value); + SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); + }; + if (stencil_action_enable) { old_stencil = GetStencil(x >> 4, y >> 4); u8 dest = old_stencil & stencil_test.input_mask; @@ -832,8 +838,7 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, } if (!pass) { - u8 new_stencil = PerformStencilAction(stencil_test.action_stencil_fail, old_stencil, stencil_test.reference_value); - SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); + UpdateStencil(stencil_test.action_stencil_fail); continue; } } @@ -884,8 +889,7 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, if (!pass) { if (stencil_action_enable) { - u8 new_stencil = PerformStencilAction(stencil_test.action_depth_fail, old_stencil, stencil_test.reference_value); - SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); + UpdateStencil(stencil_test.action_depth_fail); } continue; } @@ -895,8 +899,7 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, if (stencil_action_enable) { // TODO: What happens if stencil testing is enabled, but depth testing is not? Will stencil get updated anyway? - u8 new_stencil = PerformStencilAction(stencil_test.action_depth_pass, old_stencil, stencil_test.reference_value); - SetStencil(x >> 4, y >> 4, (new_stencil & stencil_test.write_mask) | (old_stencil & ~stencil_test.write_mask)); + UpdateStencil(stencil_test.action_depth_pass); } } -- cgit v1.2.3 From 8e6336d96bf7ee0e31ca51064c85f1b65913fe7a Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 21 Aug 2015 09:48:43 -0500 Subject: SWRenderer: The stencil depth_pass action is executed even if depth testing is disabled. The HW renderer already did this. --- src/video_core/rasterizer.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index c768a5eea..696839da6 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -888,21 +888,19 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, } if (!pass) { - if (stencil_action_enable) { + if (stencil_action_enable) UpdateStencil(stencil_test.action_depth_fail); - } continue; } if (output_merger.depth_write_enable) SetDepth(x >> 4, y >> 4, z); - - if (stencil_action_enable) { - // TODO: What happens if stencil testing is enabled, but depth testing is not? Will stencil get updated anyway? - UpdateStencil(stencil_test.action_depth_pass); - } } + // The stencil depth_pass action is executed even if depth testing is disabled + if (stencil_action_enable) + UpdateStencil(stencil_test.action_depth_pass); + auto dest = GetPixel(x >> 4, y >> 4); Math::Vec4 blend_output = combiner_output; -- cgit v1.2.3 From b3e530d005324d915974a4d36a2588656364ab17 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 21 Aug 2015 10:09:15 -0500 Subject: SWRasterizer: Removed a todo. Verified with hwtests. --- src/video_core/rasterizer.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 696839da6..c6557bf1a 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -215,7 +215,6 @@ static void SetStencil(int x, int y, u8 value) { } } -// TODO: Should the stencil mask be applied to the "old_stencil" or "ref" operands? Most likely not! static u8 PerformStencilAction(Regs::StencilAction action, u8 old_stencil, u8 ref) { switch (action) { case Regs::StencilAction::Keep: -- cgit v1.2.3 From fef1462371b51fb5540817b76d30630868b2a961 Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 21 Aug 2015 10:35:25 -0500 Subject: SWRasterizer: Implemented stencil action 1 (GL_ZERO). Verified with hwtests. --- src/video_core/rasterizer.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index c6557bf1a..25911e0a2 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -220,6 +220,9 @@ static u8 PerformStencilAction(Regs::StencilAction action, u8 old_stencil, u8 re case Regs::StencilAction::Keep: return old_stencil; + case Regs::StencilAction::Zero: + return 0; + case Regs::StencilAction::Replace: return ref; -- cgit v1.2.3 From 7c1f84a92b9727765a755c312e90b09b0cf6ed1d Mon Sep 17 00:00:00 2001 From: Subv Date: Fri, 21 Aug 2015 11:01:42 -0500 Subject: SWRasterizer: Implemented stencil ops 6 and 7. IncrementWrap and DecrementWrap, verified with hwtests. --- src/video_core/rasterizer.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/video_core/rasterizer.cpp') diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 25911e0a2..b6c0e1bff 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -237,6 +237,12 @@ static u8 PerformStencilAction(Regs::StencilAction action, u8 old_stencil, u8 re case Regs::StencilAction::Invert: return ~old_stencil; + case Regs::StencilAction::IncrementWrap: + return old_stencil + 1; + + case Regs::StencilAction::DecrementWrap: + return old_stencil - 1; + default: LOG_CRITICAL(HW_GPU, "Unknown stencil action %x", (int)action); UNIMPLEMENTED(); -- cgit v1.2.3