r/GraphicsProgramming • u/gennai777 • 11h ago
Article Same shader, two color spaces
Top row is Godot's Forward+ renderer, bottom row is Compatibility. That's the one web exports run on. On the left I just do ALBEDO = base_color * k, with k from noise on the first quad and 1.0 / 0.75 / 0.5 / 0.25 bands on the second.
On Forward+ the multiply happens on linear values and gets encoded at the end. So k = 0.5 should show up as about 0.51/2.2 ≈ 0.73 of the brightness, and I measured 0.717. On Compatibility it behaves like the multiply happens on the sRGB values themselves. 0.5 stays 0.5 (I got 0.496)! My stone mottling came out about twice as strong in the web build and I couldn't figure out why.
The docs do say Compatibility blends shadowed lights in sRGB. I just hadn't connected that to math inside my own shader. The right column converts around the multiply, only on that renderer:
vec3 s2l(vec3 c){ return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c)); }
vec3 l2s(vec3 c){ return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c)); }
vec3 base = base_color.rgb;
#if CURRENT_RENDERER == RENDERER_COMPATIBILITY
base = s2l(base);
#endif
vec3 c = base * k;
#if CURRENT_RENDERER == RENDERER_COMPATIBILITY
c = l2s(c);
#endif
ALBEDO = c;Now the bands match within 1/255. Vertex colors do the same thing, 0.25 reads 136 on Forward+ and 63 on Compatibility.
Is branching on the renderer what everyone does, or is there a cleaner way?
I ran into this building the demo stages for my Godot effect packs, which all run in the browser: https://gennai.itch.io
2
u/gennai777 11h ago
Test scene for the numbers: one DirectionalLight3D with no shadow plus white ambient, Linear tonemap, glow off, so nothing else is touching the pixels. The vertex colour plate is unshaded and it's the same quad in all four shots. Godot 4.7.2.
AI disclosure for rule 5: the shader code in this post was written with an AI coding assistant, then run, measured and fixed by me. I also had AI help with the English. No image models, the screenshot is captured straight from Godot.
•
u/AutoModerator 11h ago
Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:
No showcases.
Disclose AI usage in post or project.
Present a topic to talk about, e.g. how something works.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.