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

Color Inaccuracies between Shadertoy.com renderer and shadertoy-rs #29

Closed
seifane opened this issue Oct 21, 2023 · 4 comments
Closed

Color Inaccuracies between Shadertoy.com renderer and shadertoy-rs #29

seifane opened this issue Oct 21, 2023 · 4 comments

Comments

@seifane
Copy link
Contributor

seifane commented Oct 21, 2023

Hello,

I was using shadertoy-rs when I discovered a weird issue that seem to suggest rendering on shadertoy-rs is not 100% the same as on the website.

As an example look at this shader.
https://www.shadertoy.com/view/ttB3Rh
What you will see is three major colors (RGB) with minimal blending between the colors.

When rendering this same shader with shadertoy-rs. I find the result to be quite different.
image
I see that the section between the red and green has a way more pronounced orange tint than the shadertoy rendered version.

At first I thought that there was some kind of weird color interpolation going on somewhere, I tried to look around the code but could not find anything suspicous. (My knowledge of computer graphics is very limited). Do you have an idea of where this issue might be coming from ?

OS: Arch linux, X11
i3 window manager

Let me know if more debug data is needed.

@fmenozzi
Copy link
Owner

I believe you're correct that it's to do with interpolation, and my first thought is that it's due to sRGB, which is designed to take things like computer display technologies and human color perception into account if I remember correctly. Usually this is considered desirable.

If this is indeed the case, I'm not sure why shadertoy.com doesn't use it, as it seems fairly standard at this point (have you tried in different browsers with different graphics implementations?). I'd have to dig into this a bit more to find out for sure but if it's indeed an sRGB issue I'd guess that it should be possible to disable in gfx-rs or something.

@seifane
Copy link
Contributor Author

seifane commented Oct 22, 2023

Following your suggestion I tried opening the shader in ShaderToy on different browsers and OSes. They all behave the same (as initially described). I've tried Linux Chrome and Firefox as well as Windows Chrome and Firefox. On different GPUs (Nvidia 960 and 2070).

I investigated a little bit on sRGB and I think you are correct. I found some issues on gfx related to being unable to turn off sRGB. (gfx-rs/gfx#997)

One seemed to hit the point and provided a possible fix. gfx-rs/gfx#1915
I implemented the fix and it seems to resolve the color blending issues in all my shaders.

unsafe {
    device.with_gl(|gl| {
        gl.Disable(gfx_gl::FRAMEBUFFER_SRGB);
    })
}

However it does force the use of unsafe code. I can open a pull request for this if you wish so.

@fmenozzi
Copy link
Owner

Huh, does indeed seem to be a gfx-related bug, as shadertoy-rs makes use of gfx::format::Rgba8 throughout, rather than Srgba8, so the linear colorspace you were expecting should actually be what's used, but I guess not.

If you wanna send a PR for this, go for it! All I'd ask is that you gate the change behind a CLI flag (maybe something like --force-no-srgb or something), as otherwise it would be a breaking change for existing shaders. Probably the best thing to do would be to eventually migrate away from gfx (as it seems that's in maintenance mode anyway) to a GPU backend that doesn't have this bug, but that would be quite an undertaking and probably better reserved for a breaking 1.0 release.

@seifane
Copy link
Contributor Author

seifane commented Oct 23, 2023

as shadertoy-rs makes use of gfx::format::Rgba8 throughout

Yep I noticied that. I even tried to explicitly set the use of srgb to false for glutin but to no avail.

Probably the best thing to do would be to eventually migrate away from gfx

I was looking into this as well at first to see if other libs would have the same issue but could not find a really good fit. Either really low level like vulkano proved too difficult for me 😅 and wgpu seems like is more geared towards WGSL rather than GLSL.

If you wanna send a PR for this, go for it!

All right I'll prepare something !

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

No branches or pull requests

2 participants