Skip to content

tr_shader: linearize fog colors and fog image #1730

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

illwieckz
Copy link
Member

Linearize fog colors.

Something I overlooked when writing #1687:

This is not enough to make fog looks good in the linear pipeline.

A fog relying on some kind of blend (I guess), we may need to recalibrate/reimplement it for the linear pipeline.

But at least with that patch the colors become right.

@illwieckz illwieckz changed the title tr_shader: linearize fog colors tr_shader: linearize fog colors and fog image Aug 6, 2025
@illwieckz illwieckz force-pushed the illwieckz/fog-color-srgb branch from e0a6449 to 9a75a4d Compare August 6, 2025 01:04
@illwieckz
Copy link
Member Author

In fact I found how to get the fog working in the linear pipeline, by using a linearized image!

So basically I create two fog images, one for the naive pipeline, one for the linear pipeline, and use them accordingly.

For unknown reasons that doesn't work when I ask to upload the second image with an sRGB format, it's like this metadata is ignored. But it works if I convert the image beforehand. I would prefer to flag the image and not convert it beforehand to not lose precision when storing as bytes, so if someone knows why the flag doesn't work, please help! Otherwise it's ready to use.

@illwieckz illwieckz added the T-Feature-Request Proposed new feature label Aug 6, 2025
@illwieckz
Copy link
Member Author

Well, I guess know why the sRGB flag doesn't change anything: only the alpha channel seems to be used and the sRGB conversion doesn't apply on the alpha channel that is always linear (of course!).

@illwieckz illwieckz force-pushed the illwieckz/fog-color-srgb branch from 9a75a4d to 0682adf Compare August 6, 2025 05:55
@illwieckz
Copy link
Member Author

illwieckz commented Aug 6, 2025

So I stored the alpha channel in the red channel and then we can flag the texture as sRGB to get it implicitly converted.

It works.

@illwieckz illwieckz force-pushed the illwieckz/fog-color-srgb branch from d02e54c to 89025ea Compare August 6, 2025 06:44
@illwieckz
Copy link
Member Author

I added code to store the fog naive color as a GL_RED, and the fog linear color GL_SR8_EXT when supported.

@illwieckz illwieckz force-pushed the illwieckz/fog-color-srgb branch from 89025ea to c224e4e Compare August 6, 2025 06:53
@slipher
Copy link
Member

slipher commented Aug 9, 2025

Well, I guess know why the sRGB flag doesn't change anything: only the alpha channel seems to be used and the sRGB conversion doesn't apply on the alpha channel that is always linear (of course!).

The thing is, it actually is used as an alpha. The fog works as if alpha-blending a solid-color image with color fogColor, and an alpha value depending on the fog depth which is sourced from fogImage. So it probably doesn't make sense to sRGB-convert the fog image.

Now if we leave the fog image alone and just convert the color, the result is expected to be brighter than with the naive pipeline, since naive alpha blending produces results that are darker than they should be. Probably the fog shaders just need to be adjusted on a per-map basis, like other translucent shaders.

@illwieckz
Copy link
Member Author

illwieckz commented Aug 9, 2025

Yes it's actually an alpha channel, but we need a different alpha channel for when working in linear space, and it happens that applying this transformation is the cheapest way to produce an approximation that works.

I'm not against us adding a different image generator with a different specially-shaped curve for that alpha channel when blending in linear space, but I have no idea how such curve should be generated. I can add an HACK comment explaining that.

In a similar topic, I've heard that actually multiplying sRGB texture with sRGB lightmap without linearizing them before and delinearizing the result after is interestingly close to doing the things properl. That's why my testing server doesn't look that bad when not using a colorspace-aware engine, and that it still looks better than maps rendered the legacy way.

I wonder if we can also approximate some other blending compatibility by converting the alpha too.

I guess that blending an sRGB texture with a linear alpha channel produces a somewhat sRGB image, so maybe blending a linear texture with an sRGB alpha channel somewhat produces that sRGB image, at least close enough. Or something like that. It's totally hacky but things may produce interesting results this way.

@illwieckz illwieckz force-pushed the illwieckz/fog-color-srgb branch from c224e4e to 685a059 Compare August 11, 2025 14:44
@illwieckz
Copy link
Member Author

I added a comment saying the current fog image implementation in linear pipeline is a hack.

But now I thought about something. The fog image generator for the naive pipeline unaware of colorspaces is doing some complex algorithm, picking values from tables and using custom factors… I actually wonder what would happen if we would just use some linear values instead, when running the linear pipeline. Maybe all that fog factor and fog table thing is yet another workaround for the broken naive pipeline?

@VReaperV
Copy link
Contributor

VReaperV commented Aug 13, 2025

This is what the contents of the fogImage look like as it currently is, with the alpha-channel used for rgb:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants