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

Software: Remove alpha=1 blending special-case #11389

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

Pokechu22
Copy link
Contributor

This was added in #10394 for both the hardware and software backends to work around an issue with Mario Kart Wii, Fortune Street, and Baten Kaitos. However, it seems like the software renderer handles blending well enough that we don't need this (and in any case, it's easy to change blending in the software renderer).

Some experimentation with #11387 (not pushed) showed that the software renderer's logic would also produce correct results on the hardware backends with this hack removed, but would require fbfetch (currently); if a better solution is found the hack can also be removed from the hardware backends.

This was added in dolphin-emu#10394 for both the hardware and software backends to work around an issue with Mario Kart Wii, Fortune Street, and Baten Kaitos. However, it seems like the software renderer handles blending well enough that we don't need this (and in any case, it's easy to change blending in the software renderer).

Some experimentation with dolphin-emu#11387 (not pushed) showed that the software renderer's logic would also produce correct results on the hardware backends with this hack removed, but would require fbfetch (currently); if a better solution is found the hack can also be removed from the hardware backends.
@dolphin-emu-bot
Copy link
Contributor

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

  • ab11-homebrew on sw-lin-mesa: diff
  • aeon-charge-attack on sw-lin-mesa: diff
  • 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
  • custom-brawl-char on sw-lin-mesa: diff
  • dbz-depth 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
  • ed-updated on sw-lin-mesa: diff
  • et-vid on sw-lin-mesa: diff
  • find-mii on sw-lin-mesa: diff
  • fishing-resort-map on sw-lin-mesa: diff
  • fortune-street 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
  • inverted-depth-range on sw-lin-mesa: diff
  • jb-shadow on sw-lin-mesa: diff
  • jd2-fmv on sw-lin-mesa: diff
  • jj-awae-mirrored on sw-lin-mesa: diff
  • kirby-logicop on sw-lin-mesa: diff
  • last-story-shadows on sw-lin-mesa: diff
  • lego-star-wars-crane-shadow on sw-lin-mesa: diff
  • lm-mario-portrait on sw-lin-mesa: diff
  • luigi-shadows on sw-lin-mesa: diff
  • major-minor on sw-lin-mesa: diff
  • mario-baseball-shadows on sw-lin-mesa: diff
  • mario-golf-oob on sw-lin-mesa: diff
  • mario-sluggers-bar on sw-lin-mesa: diff
  • mario-tennis-menu on sw-lin-mesa: diff
  • MaS-LOG-wiimote 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
  • milotic-texture on sw-lin-mesa: diff
  • mini-ninjas on sw-lin-mesa: diff
  • mkdd-babypark on sw-lin-mesa: diff
  • mkdd-efb on sw-lin-mesa: diff
  • mkw-bridge on sw-lin-mesa: diff
  • mkw-flags on sw-lin-mesa: diff
  • mkwii-bluebox on sw-lin-mesa: diff
  • monkeyball-fuse on sw-lin-mesa: diff
  • mp2-scanner on sw-lin-mesa: diff
  • mp3-bloom on sw-lin-mesa: diff
  • mp4-vertexcache on sw-lin-mesa: diff
  • mp7-text on sw-lin-mesa: diff
  • mp8-widescreen on sw-lin-mesa: diff
  • mtennis-zfreeze on sw-lin-mesa: diff
  • my-word-coach on sw-lin-mesa: diff
  • nddemo-lighting 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
  • nintendo-channel on sw-lin-mesa: diff
  • nsmbw-coins on sw-lin-mesa: diff
  • nsmbw-intro on sw-lin-mesa: diff
  • pbr-sfx on sw-lin-mesa: diff
  • pm-hc-jp on sw-lin-mesa: diff
  • rs2-bumpmapping on sw-lin-mesa: diff
  • rs2-glass 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
  • sfa-shadows on sw-lin-mesa: diff
  • sf-assault-flashing on sw-lin-mesa: diff
  • simpsons-game on sw-lin-mesa: diff
  • smb-mirror on sw-lin-mesa: diff
  • smg2-fog on sw-lin-mesa: diff
  • smg-marioeyes on sw-lin-mesa: diff
  • smg-mmg on sw-lin-mesa: diff
  • smg-roar 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
  • sonic-riders-blur on sw-lin-mesa: diff
  • sonic-riders-zg-4p on sw-lin-mesa: diff
  • sonicriderszg-gb on sw-lin-mesa: diff
  • ssbb-mod-lloyd on sw-lin-mesa: diff
  • ssbm-pointsize on sw-lin-mesa: diff
  • ss-map 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
  • tsp3-pinkgrass on sw-lin-mesa: diff
  • vegas-party-depth on sw-lin-mesa: diff
  • xblade-bloom on sw-lin-mesa: diff
  • xenoblade-menu on sw-lin-mesa: diff
  • ztp-grass on sw-lin-mesa: diff
  • zww-armos on sw-lin-mesa: diff
  • zww-water on sw-lin-mesa: diff
  • zww-waves on sw-lin-mesa: diff

automated-fifoci-reporter

@Pokechu22
Copy link
Contributor Author

Nearly all of those fifoci diffs are just a few individual pixels changing slightly (probably alpha=1 showing up naturally near the edge of transparent objects). The only exception seems to be the clouds in xenoblade-menu / xblade-bloom (but I can confirm (at least for xblade-bloom) that the new result is closer to correct than it was before; the colors seem to match almost perfectly now before the bloom step), and bk-tev where a few things also got slightly brighter. bk-tev, mkw-flags, and fortune-street-white-box did not regress.


For reference, here's the software renderer's blend logic:

static void BlendColor(u8* srcClr, u8* dstClr)
{
u32 srcFactor = GetSourceFactor(srcClr, dstClr, bpmem.blendmode.srcfactor);
u32 dstFactor = GetDestinationFactor(srcClr, dstClr, bpmem.blendmode.dstfactor);
for (int i = 0; i < 4; i++)
{
// add MSB of factors to make their range 0 -> 256
u32 sf = (srcFactor & 0xff);
sf += sf >> 7;
u32 df = (dstFactor & 0xff);
df += df >> 7;
u32 color = (srcClr[i] * sf + dstClr[i] * df) >> 8;
dstClr[i] = (color > 255) ? 255 : color;
dstFactor >>= 8;
srcFactor >>= 8;
}
}

The key logic here is sf += sf >> 7. This effectively means if the source factor is greater than or equal to 128, add 1 to it. So, the actual value used in the blending equations go 0⇒0, 1⇒1, 2⇒2, 3⇒3, ..., 125⇒125, 126⇒126, 127⇒127, 128⇒129, 129⇒130, ... 253⇒254, 254⇒255, 255⇒256. This allows the maximum value of 255 to be treated as 1.0 (instead of 255/256 = 0.99609375).

Then, after multiplying and also adding in the dest factor with the same logic, the code divides by 256, truncating towards zero (written as >> 8).

If the source factor (e.g. alpha value) is 1 and the source color is 255, then the output is 255 / 256 which becomes zero. If the source factor is 255 and the source color is 1, then the source factor is treated as 256 and the output becomes 256 / 256 or 1. When both the source and dest factors and colors are factored in, the fractional part plays a more meaningful role, since if both multiplications have factional parts that can bump it up to the next number.

Similar logic exists for the tev's c factor, and that part has been hardware tested. I haven't done extensive hardware testing with blending, but I did previously confirm the behavior with a source factor of 1.

@iwubcode
Copy link
Contributor

iwubcode commented Dec 29, 2022

Thanks for the write up @Pokechu22 ! Your explanation makes sense.

You mention in your original PR that it fixed https://bugs.dolphin-emu.org/issues/12713#note-6 . Does that still work properly?

@@ -544,19 +544,6 @@ void Tev::Draw()
if (!TevAlphaTest(output[ALP_C]))
return;

// Hardware testing indicates that an alpha of 1 can pass an alpha test,
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this blurb was written by you. You said it was hardware tested but then you have the TODO. Does that mean there was some issue with the hardware test you performed or was there another reason you doubted the correctness of this behavior?

I see in the original PR that you mention that the second commit (the one that contains this change) you were less certain on...

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I see what you are saying, it was only tested in one scenario, so while there was a single case that was tested, not every case was tested and therefore it needed more investigation.

Copy link
Contributor

@iwubcode iwubcode Dec 29, 2022

Choose a reason for hiding this comment

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

Ok, sorry for the comment spam. I was a bit confused initially why this was added (especially if the original logic in Software Renderer was working) but I think I fully understand the history now. In addition to the one scenario bit above, you were also trying to resolve an issue with the hardware renderers. So in order to be consistent, you also added the workaround to the software renderer.

If the plan is to fix the hardware renderers and avoid this hack altogether, then I have no concerns. Even if not, software being the ideal solution is fine too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's correct. I was trying to remove a previous issue that affected the hardware renderers, and removed an older hack (#4514) that existed in both the software renderer and the hardware renderers. I then added an alternative hack on alpha=1 that was needed to avoid regressions in fortune street caused by removing the original hack, and I added that to both the software renderer and hardware renderers.

I did investigate what happens both when the first hack is removed and the second added in #10394 (comment), and I even noted that there was "No diff" for fortune-street-white-box on the software renderer, but I didn't realize at the time that that meant that the software renderer didn't regress from removing the hack and thus it didn't need the new alpha=1 hack at all. This PR removes that hack.

Incidentally, the old hack wasn't added to the software renderer in #4514, but instead was accidentally added in 1dead05 for both color and alpha, and then fixed for color (but not alpha) in 5e1c6ab. So, instead of being deliberately introduced there, the exact same behavior existed by accident, and thus was there for me to remove in my PR.

I'd like to remove the hack in the hardware renderers too, but I'm not sure how feasible that will be.

@Pokechu22
Copy link
Contributor Author

Yes, micro machines still works properly with this PR.

@JMC47 JMC47 merged commit a20e41d into dolphin-emu:master Dec 29, 2022
Medard22 added a commit to Medard22/Dolphin-MMJR2-VBI that referenced this pull request Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants