Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copy filter clamping #10222

Merged
merged 2 commits into from Nov 18, 2021
Merged

Conversation

phire
Copy link
Member

@phire phire commented Nov 16, 2021

This fixes horizontal lines in the bloom effect of Spyro: A Hero's Tail which is a regression caused by PR #10204.

Screenshot of regression

final

Fixed

final-fixed

Spyro uses an 640x80 pixel sub-buffer within the EFB to calculate
it's bloom effects, which it places below the main 640x448 buffer.

EFB layout

Note: Colors are wrong because the main color buffer uses RGBA6, while the bloom is calculated in RGB8

full-efb

This allows it to do bloom without backing up part of the EFB to
main memory, as most games do.

But, since some of the sub-buffers used in the bloom effect are taller
than 80 pixels, they need to be sliced up into smaller sub, sub buffers
which get combined later when copied to main memory.

At one point, a 320x224 buffer is broken up into 320x80, 320x64 and
320x80 slices. These are copied out with the copy filter set to a
vertical blur.

Because there was an off-by-one errror in the clamping coordinates,
the bottom line of the color buffer would be blurred into
the top of each slice.

Final combined EFB copy

merged-copy

Fixed version

merged-copy-fixed

Before #10204 the copy filter wasn't enabled for efb copies, and most
other games don't do this type of slicing.

FIFO CI shows that a few other games are effected, it's always just a minor difference to the top line where there was previously a slight hint of garbage.

This fixes horizontal lines in the bloom effect of Spyro: A Hero's Tail,
which is a regression caused by PR dolphin-emu#10204

Screenshot of regression:
https://user-images.githubusercontent.com/138484/142030503-90fcd8d5-63d3-4820-874a-72e9be0c4768.png

Fixed:
https://user-images.githubusercontent.com/138484/142031598-b85ff55c-1302-4e4d-bcb2-57848974056b.png

Spyro uses an 640x80 pixel sub-buffer within the EFB to calculate
it's bloom effects, which it places below the main 640x448 buffer.

EFB layout:
https://user-images.githubusercontent.com/138484/142030573-e933b6ae-c37e-4be6-86d4-0bc779b92535.png
Note: Colors are wrong because the main color buffer uses RGBA6,
      while the bloom is calculated in RGB8

This allows it to do bloom without backing up part of the EFB to
main memory, as most games do.

But, since some of the sub-buffers used in the bloom effect are taller
than 80 pixels, they need to be sliced up into smaller sub, sub buffers
which get combined later when copied to main memory.

At one point, a 320x224 buffer is broken up into 320x80, 320x64 and
320x80 slices. These are copied out with the copy filter set to a
vertical blur.

Because there was an off-by-one errror in the clamping coordinates,
the bottom line of the color buffer would be blurred into
the top of each slice.

Final combined EFB copy:
https://user-images.githubusercontent.com/138484/142031360-2c076839-7c96-4b3b-a093-d899d0a2c7ae.png

Fixed version:
https://user-images.githubusercontent.com/138484/142031370-72e41a35-3b3e-4662-a483-79203e357ecc.png

Before dolphin-emu#10204 the copy filter wasn't enabled for efb copies, and most
other games don't do this type of slicing.

FIFO CI shows that a few other games are effected, it's always just a minor difference to the top line where there was previously a slight hint of garbage.
@phire phire marked this pull request as ready for review November 16, 2021 17:19
uniforms.clamp_top = clamp_top ? framebuffer_rect.top * rcp_efb_height : 0.0f;
uniforms.clamp_bottom = clamp_bottom ? framebuffer_rect.bottom * rcp_efb_height : 1.0f;
int bottom_coord = framebuffer_rect.bottom - 1;
uniforms.clamp_bottom = clamp_bottom ? bottom_coord * rcp_efb_height : 1.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the corresponding code in the software renderer (which only exists in EncodeXFB; the equivalent for EFBs doesn't handle clamping or the copy filter currently):

for (int y = source_rect.top; y < source_rect.bottom; y++)
{
// Clamping behavior
// NOTE: when the clamp bits aren't set, the hardware will happily read beyond the EFB,
// which returns random garbage from the empty bus (confirmed by hardware tests).
//
// In our implementation, the garbage just so happens to be the top or bottom row.
// Statistically, that could happen.
const u16 y_prev = static_cast<u16>(std::max(clamp_top ? source_rect.top : 0, y - 1));
const u16 y_next =
static_cast<u16>(std::min<int>(clamp_bottom ? source_rect.bottom : EFB_HEIGHT, y + 1));
// Get a scanline of YUV pixels in 4:4:4 format
for (int i = 1, x = left; x < right; i++, x++)
{
// Get RGB colors
std::array<u32, 3> colors = {{GetColor(x, y_prev), GetColor(x, y), GetColor(x, y_next)}};
// Vertical Filter (Multisampling resolve, deflicker, brightness)
u32 filtered = VerticalFilter(colors, filter_coefficients);
// Gamma correction happens here.
filtered = GammaCorrection(filtered, gamma_rcp);
scanline[i] = ConvertColorToYUV(filtered);
}

Is it also affected? Given that y_prev can be 0 and y_next can be EFB_HEIGHT or source_rect.bottom I suspect it is... but I'm not 100% certain. Since it's not implemented for EFBs at all currently this isn't too pressing.

I also need to question the 1.0f in this code, though; shouldn't it be (EFB_HEIGHT - 1) * rcp_efb_height? Or does that not matter because the texture is clamped elsewhere as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the same bug. The for loop iterates from top to bottom - 1, yet it clamps at bottom.

As for the 1.0f... I'm not sure, but I suspect our whole coordinate system might be off-by-one here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed videosoftware.

I'll have to look more deeply into the UV coordinates later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fifoci dffs for software are most visible in mii-channel and nhl-slap; I took a look at mii-channel and it has clamping enabled, so the garbage data going away is correct (it's not an out-of-bounds read that the comment warns about).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I previously guessed, the texture is clamped elsewhere: either GetLinearSamplerState or GetPointSamplerState is used:

g_renderer->SetSamplerState(0, linear_filter ? RenderState::GetLinearSamplerState() :
RenderState::GetPointSamplerState());

and those clamp u and v:

state.wrap_u = SamplerState::AddressMode::Clamp;
state.wrap_v = SamplerState::AddressMode::Clamp;

state.wrap_u = SamplerState::AddressMode::Clamp;
state.wrap_v = SamplerState::AddressMode::Clamp;

So, it won't go out of bounds, though whether this results in things being off slightly I'm less sure. With linear filtering I vaguely think it might be correct to use 1.0f but I'm iffy on that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the texture is clamped.

If the coordinate space is off-by-one then it should still produce the correct result for point-sampled copies, but will produce a slightly wrong result for linear sampling.

@dolphin-emu-bot
Copy link
Contributor

FifoCI detected that this change impacts graphical rendering. Here are the behavior differences detected by the system:

  • bk-tev on sw-lin-mesa: diff
  • burnout2-vehicletextures on sw-lin-mesa: diff
  • chibi-robo-fastdepth on sw-lin-mesa: diff
  • chibi-robo-zfighting on sw-lin-mesa: diff
  • dbz-depth on sw-lin-mesa: diff
  • djfny-menu on sw-lin-mesa: diff
  • djhero2-blend on sw-lin-mesa: diff
  • DKCR-Char on sw-lin-mesa: diff
  • DKCR-fast-depth on sw-lin-mesa: diff
  • ea-pink on sw-lin-mesa: diff
  • ea-vp6 on sw-lin-mesa: diff
  • fishing-resort-map on sw-lin-mesa: diff
  • fortune-street-fog on sw-lin-mesa: diff
  • fortune-street-white-box on sw-lin-mesa: diff
  • fsa-layers on sw-lin-mesa: diff
  • f-zero-rain on sw-lin-mesa: diff
  • goldeneye-depth on sw-lin-mesa: diff
  • inverted-depth-range on sw-lin-mesa: diff
  • jd2-fmv on sw-lin-mesa: diff
  • jj-awae-mirrored on sw-lin-mesa: diff
  • last-story-shadows on sw-lin-mesa: diff
  • lm-mario-portrait on sw-lin-mesa: diff
  • luigi-shadows on sw-lin-mesa: diff
  • mario-baseball-shadows on sw-lin-mesa: diff
  • mario-sluggers-bar on sw-lin-mesa: diff
  • mario-tennis-menu on sw-lin-mesa: diff
  • megaman-heat on sw-lin-mesa: diff
  • melee-depth on sw-lin-mesa: diff
  • melee-lighting on sw-lin-mesa: diff
  • metroid-visor on sw-lin-mesa: diff
  • mii-channel on sw-lin-mesa: diff
  • milotic-texture on sw-lin-mesa: diff
  • mini-ninjas on sw-lin-mesa: diff
  • mkw-bridge on sw-lin-mesa: diff
  • mkwii-bluebox on sw-lin-mesa: diff
  • monkeyball-fuse on sw-lin-mesa: diff
  • mp3-bloom on sw-lin-mesa: diff
  • mp7-text on sw-lin-mesa: diff
  • mtennis-zfreeze on sw-lin-mesa: diff
  • my-word-coach on sw-lin-mesa: diff
  • nddemo-bumpmapping on sw-lin-mesa: diff
  • nfsu-purplerect on sw-lin-mesa: diff
  • nfsu-reflections on sw-lin-mesa: diff
  • nhl-slap on sw-lin-mesa: diff
  • nsmbw-intro on sw-lin-mesa: diff
  • pm-hc-jp on sw-lin-mesa: diff
  • puzzle-collection on sw-lin-mesa: diff
  • rs2-bumpmapping on sw-lin-mesa: diff
  • rs2-skybox on sw-lin-mesa: diff
  • rs2-zfreeze on sw-lin-mesa: diff
  • rs3-bumpmapping on sw-lin-mesa: diff
  • rs3-skybox2 on sw-lin-mesa: diff
  • sadx-ui on sw-lin-mesa: diff
  • sf-assault-flashing on sw-lin-mesa: diff
  • shadow-eyes on sw-lin-mesa: diff
  • smb-mirror on sw-lin-mesa: diff
  • sms-bubbles on sw-lin-mesa: diff
  • sms-gc on sw-lin-mesa: diff
  • sms-water on sw-lin-mesa: diff
  • soa-black on sw-lin-mesa: diff
  • soniccolors-mm on sw-lin-mesa: diff
  • sonic-riders-blur on sw-lin-mesa: diff
  • sonic-riders-zg-4p on sw-lin-mesa: diff
  • spyro-bloom on sw-lin-mesa: diff
  • spyro-depth on sw-lin-mesa: diff
  • ssbm-pointsize on sw-lin-mesa: diff
  • super-sluggers-white-out on sw-lin-mesa: diff
  • sw3-dt on sw-lin-mesa: diff
  • thps3-earlyz on sw-lin-mesa: diff
  • thps4-shadow on sw-lin-mesa: diff
  • tla-menu on sw-lin-mesa: diff
  • tos-invis-char on sw-lin-mesa: diff
  • tp-skin on sw-lin-mesa: diff
  • ztp-grass on sw-lin-mesa: diff
  • zww-water on sw-lin-mesa: diff
  • zww-waves on sw-lin-mesa: diff

automated-fifoci-reporter

Copy link
Contributor

@Pokechu22 Pokechu22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still a chance that there's an off-by-one when linear filtering is enabled, but this is definitely more accurate in most cases.

@JMC47 JMC47 merged commit dbaebdc into dolphin-emu:master Nov 18, 2021
10 checks passed
@phire phire deleted the fix-copy-filter-clamping branch February 2, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants