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 alpha compositing for translucent water surfaces #384

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

rdw-software
Copy link
Member

After some investigation, it's become clear that the reference client seems to incorrectly mix sRGB and linear-space colors. While this is a bit of a guess, it would explain why the contrast correction was needed in the first place; it makes up for the underexposure introduced by the lack of gamma-decompression steps after loading sRGB-encoded textures. Indeed, screen blending is used for this exact purpose in digital photo editing (apparently). At least that's the best explanation I could come up with so far.

In order to reproduce the resulting outputs, neither sRGB nor linearized colors can be used. The "in-between" method used so far, however, trips up the alpha blend stage while the underlying texture view is in sRGB format: WebGPU automatically converts the fragment colors - first to sRGB and then back to linear as part of the blend stage. Unfortunately, the color space used is actually neither of the two by this point, and so the results can't possibly match expectations unless a non-sRGB output format is used.

Originally, sRGB was selected because it's the preferred surface format. Using a different one grants full control of the color space used and eliminates the undesirable conversion, but may incur a performance hit. However, conversions between RGBA and sRGB should be hardware-accelerated on any reasonably modern device. Switching to RGBA also simplifies the shader logic as the added linearization step is no longer needed. So overall it's probably a net gain? Either way, for now that's a secondary concern.

After some investigation, it's become clear that the reference client seems to incorrectly mix sRGB and linear-space colors. While this is a bit of a guess, it would explain why the contrast correction was needed in the first place; it makes up for the underexposure introduced by the lack of gamma-decompression steps after loading sRGB-encoded textures. Indeed, screen blending is used for this exact purpose in digital photo editing (apparently). At least that's the best explanation I could come up with so far.

In order to reproduce the resulting outputs, neither sRGB nor linearized colors can be used. The "in-between"  method used so far, however, trips up the alpha blend stage while the underlying texture view is in sRGB format: WebGPU automatically converts the fragment colors - first to sRGB and then back to linear as part of the blend stage. Unfortunately, the color space used is actually neither of the two by this point, and so the results can't possibly match expectations unless a non-sRGB output format is used.

Originally, sRGB was selected because it's the preferred surface format. Using a different one grants full control of the color space used and eliminates the undesirable conversion, but may incur a performance hit. However, conversions between RGBA and sRGB should be hardware-accelerated on any reasonably modern device. Switching to RGBA also simplifies the shader logic as the added linearization step is no longer needed. So overall it's probably a net gain? Either way, for now that's a secondary concern.
@rdw-software rdw-software force-pushed the fix-water-transparency branch from 1125ce9 to 31652c0 Compare February 28, 2024 03:41
@rdw-software rdw-software merged commit 8660f7a into main Feb 28, 2024
6 checks passed
@rdw-software rdw-software deleted the fix-water-transparency branch February 28, 2024 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant