egui icon indicating copy to clipboard operation
egui copied to clipboard

Dithering in Fragment Shader

Open jwagner opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. Egui currently doesn't apply any dithering to it's output. That's hardly an issue with the flat default look and feel. However, when drawing gradients or images (which have been resampled) banding becomes a problem.

Describe the solution you'd like I think ideally egui would perform dithering in the fragment shader. Given the trivial nature of the shader performing a few more operations on the ALU for some simple dithering should not have a relevant impact on rendering performance. The one exception is potentially when software rasterization.

Describe alternatives you've considered I'm currently just drawing a noisy texture on top of gradients to mask the problem but it requires a lot more noise to be added to the gradient to make it look smooth (ironically) than doing it in a fragment shader.

I think there is also an argument that this is out of scope for egui. Other than shadows most widgets seem to use solid colors. For the shadows the fall off is quick enough that dithering isn't necessary. Even when rendering images this is likely going to be an edge case and only an issue when rendering images with smooth gradients that either aren't dithered or have been resampled to the extent that the dithering in the image is ineffective.

Additional context I'd be happy to create a PR if dithering is considered to be in scope for egui.

jwagner avatar May 13 '24 16:05 jwagner

I think this could be a nice improvement, and would reduce banding for e.g. the color picker.

We could use the same approach as in https://github.com/rerun-io/rerun/blob/55cf671d8c578648c9354a552d21f185a8eee688/crates/re_renderer/shader/generic_skybox.wgsl#L29-L65

We should make sure to mention in a comment that this is an optional improvement, and that someone writing their own egui renderer don't need to copy if they don't want to.

emilk avatar May 14 '24 09:05 emilk