Skip to content

Draw egui through a non-sRGB view of the window - #107

Merged
sunsided merged 2 commits into
mainfrom
fix/egui-srgb-overlay
Nov 16, 2026
Merged

sunsided merged 2 commits into
mainfrom
fix/egui-srgb-overlay

Conversation

@sunsided

Copy link
Copy Markdown
Owner

Ui created its egui renderer with the sRGB surface format and drew into the sRGB view of the window. egui outputs gamma-space colours and blends in gamma space, so egui-wgpu warned at startup (Detected a linear (sRGBA aware) framebuffer Bgra8UnormSrgb) and fell back to its linear-framebuffer shader, which blends differently from what egui expects.

Ui now draws through a non-sRGB view of the window, the format egui prefers.

Fixes #106

Key changes

  • An sRGB surface is configured with its non-sRGB twin as a view format, where the backend has DownlevelFlags::SURFACE_VIEW_FORMATS (surface_view_formats in graphics.rs).
  • Graphics::overlay_format() returns the format of that view; Frame::overlay_view_and_encoder() hands out the view with the frame's encoder.
  • Ui::new builds the egui Renderer with overlay_format() and Ui::run draws into the overlay view.
  • Changelog entries under Unreleased.

Risks / notes

  • The issue expected a grey of 128 to read back as about 188 before the fix. egui-wgpu already compensated for sRGB targets with its fs_main_linear_framebuffer shader, so the visible difference was probably blending rather than a full double encode. I did not measure a pixel read-back.
  • Without SURFACE_VIEW_FORMATS (GL) the surface keeps no extra view format and overlay_format() falls back to the sRGB format, so behaviour there is unchanged and the warning stays.
  • Surfaces that are already non-sRGB (WebGPU canvases) keep their configuration; egui now draws in the surface's own format there. Not run in a browser.

Testing

  • ex_06_egui on Linux/Vulkan (Intel Iris Xe): starts with no egui_wgpu framebuffer warning and no wgpu validation error.
  • New unit test frames_render_srgb_and_overlays_non_srgb covers the render and overlay format for an sRGB surface with and without the downlevel flag, and for a non-sRGB surface.
  • clippy -D warnings (default, --all-features, wasm32) and cargo doc -D warnings pass.

Review

Start with surface_view_formats and overlay_format at the bottom of crates/gfx/src/graphics.rs; they decide which view formats the surface gets and which one overlays use. Frame only carries the extra view, and the Ui change is two lines.

Ui created its egui renderer with the sRGB surface format and drew into
the sRGB view of the window. egui outputs gamma-space colours and blends
in gamma space, so egui-wgpu warned about a linear framebuffer at startup
and fell back to its linear-framebuffer shader, which blends differently.

An sRGB surface now also carries its non-sRGB twin as a view format, where
the backend supports surface view formats. Graphics::overlay_format() and
Frame::overlay_view_and_encoder() expose that view, and Ui uses both.
Without the downlevel flag (GL) the overlay format stays the sRGB one, so
nothing changes there.

Fixes #106
@github-actions github-actions Bot added documentation Improvements or additions to documentation area: gfx game-toolkit-gfx: wgpu renderer, sprites, SDF, text, tilemap, 3D area: ui game-toolkit-ui: egui debug overlay labels Nov 16, 2026
) -> Vec<wgpu::TextureFormat> {
let srgb = surface_format.add_srgb_suffix();
let linear = surface_format.remove_srgb_suffix();
if srgb != surface_format {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: can_view is not applied to the non-sRGB-surface branch

SURFACE_VIEW_FORMATS only gates the sRGB-surface branch below. A non-sRGB surface (for example a WebGPU canvas that is not sRGB) still returns vec![srgb] here even when can_view is false. The function's own doc says a surface cannot be configured with a view format it does not support, and Graphics::new calls surface.configure immediately with these view formats, so on a backend without SURFACE_VIEW_FORMATS this configuration can fail and abort startup. Either apply the flag to both branches, or document why the non-sRGB branch is always safe. Note the returned sRGB twin is what render_format relies on, so it cannot simply be dropped.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explained in 3c2b40b, no behaviour change. The non-sRGB branch is the code from before this PR (view_formats: vec![format] whenever the surface isn't sRGB), and it can't be gated: render_format needs the sRGB twin, because every painter shader writes linear colour. Dropping it would render every game too dark on those surfaces instead of failing loudly. Surfaces that offer no sRGB format are WebGPU canvases, where the sRGB view format is always allowed. The doc comment on surface_view_formats now says this.

Comment thread crates/gfx/src/frame.rs
/// [`Graphics::overlay_format`] (non-sRGB): what the pass writes is stored as is, not
/// sRGB-encoded. For overlays that output gamma-space colours (egui), which the sRGB view
/// would encode a second time.
pub fn overlay_view_and_encoder(&mut self) -> (&wgpu::TextureView, &mut wgpu::CommandEncoder) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Document the required ordering relative to flush_pending

This view aliases the same texture the composite blit clears with LoadOp::Clear (see crates/gfx/src/canvas.rs:284). A caller that records into this view before Graphics::flush_pending/composite gets the overlay cleared by that blit. Ui::run flushes first, but this is now a public method, so the doc should state that overlays must be recorded after the flush (or after composite has run).

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3c2b40b. The doc now says to record overlays after Graphics::flush_pending, because with a canvas the blit onto the window clears this view.

Comment thread crates/ui/src/lib.rs
gfx.device(),
gfx.surface_format(),
// egui outputs gamma-space colours; an sRGB target would encode them twice.
gfx.overlay_format(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: egui pipelines are pinned to the format chosen at construction

Ui::new bakes gfx.overlay_format() into the egui Renderer, while begin_frame derives the overlay view's format from the surface configuration at frame time. Graphics::resize currently only changes width/height, so this matches today, but any future reconfigure that changes the surface format (HDR/color-space, format fallback) would leave the renderer's pipelines and the attachment format mismatched and trip a wgpu validation error. Rebuilding the renderer on a format change, or asserting both formats agree in Ui::run, keeps that safe.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this. The surface format is picked once in Graphics::new, and resize only changes width and height, so the renderer and the view can't disagree today. If a later change reconfigures the format (HDR, a fallback), that change has to deal with every pipeline built from surface_format() (sprites, shapes, text, meshes, post effects), not only egui. A guard for egui alone wouldn't cover that.

Comment thread CHANGELOG.md Outdated

### Fixed

- `Ui` (egui) rendered into the sRGB view of the window, so its colours were sRGB-encoded twice:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: The entry asserts a cause the PR description says was not verified

This states colours were "sRGB-encoded twice" and panels "came out washed out", while the PR notes egui-wgpu already compensated for sRGB targets with its linear-framebuffer shader, that the visible difference "was probably blending rather than a full double encode", and that no pixel read-back was measured. It also draws through the non-sRGB view only where SURFACE_VIEW_FORMATS exists. Consider describing the confirmed symptom (egui-wgpu framebuffer warning and gamma-space blending mismatch) so the changelog does not overstate the fix.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3c2b40b. The entry now names the confirmed symptoms (the egui-wgpu warning and blending in linear space) and says the fix applies where the backend supports surface view formats.

@kilo-code-bot

kilo-code-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • CHANGELOG.md
  • crates/gfx/src/frame.rs
  • crates/gfx/src/graphics.rs
  • crates/ui/src/lib.rs
Previous Review Summary (commit 67b2143)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 67b2143)

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 3
Issue Details (click to expand)

WARNING

File Line Issue
crates/gfx/src/graphics.rs 1356 surface_view_formats gates only the sRGB-surface branch on SURFACE_VIEW_FORMATS; a non-sRGB surface still requests the sRGB view format and surface.configure can fail where the flag is absent

SUGGESTION

File Line Issue
crates/gfx/src/frame.rs 72 overlay_view_and_encoder does not document that overlays must be recorded after flush_pending, otherwise the composite LoadOp::Clear blit wipes them
crates/ui/src/lib.rs 37 egui Renderer pipelines are pinned to the format chosen at construction; a future surface-format change would mismatch the overlay view
CHANGELOG.md 16 Entry asserts a double-encode/washed-out cause the PR description says was not verified
Files Reviewed (4 files)
  • CHANGELOG.md - 1 issue
  • crates/gfx/src/frame.rs - 1 issue
  • crates/gfx/src/graphics.rs - 1 issue
  • crates/ui/src/lib.rs - 1 issue

Fix these issues in Kilo Cloud


Reviewed by deepseek-v4.1-flash-1017 · Input: 42.1K · Output: 9.9K · Cached: 288.6K

Frame::overlay_view_and_encoder now says overlays must be recorded after
Graphics::flush_pending: with a canvas, the blit onto the window clears
the view.

surface_view_formats explains why a non-sRGB surface always gets its
sRGB twin regardless of SURFACE_VIEW_FORMATS: the painter shaders write
linear colour and rely on it, as they did before this branch.

The changelog entry describes the confirmed symptoms (the egui-wgpu
warning and blending in linear space) instead of a double encode, and
says the fix applies where surface view formats are supported.
@sunsided
sunsided merged commit 994f04d into main Nov 16, 2026
9 checks passed
@sunsided
sunsided deleted the fix/egui-srgb-overlay branch November 16, 2026 18:44
sunsided added a commit that referenced this pull request Nov 16, 2026
Adds `Camera3D::ortho_height`. `Some(h)` switches the 3D camera to an
orthographic projection that shows `h` world units vertically and `h *
aspect` horizontally, centred on the view axis, for isometric and
top-down views. `None`, the default, keeps the perspective projection.

```rust
ctx.gfx.camera3d.ortho_height = Some(6.0); // 6 world units tall
```

Was stacked on #107, which is merged; `main` is merged into this branch.

## Key changes

- `Camera3D::ortho_height: Option<f32>`; `view_proj` picks the
projection from it. `fov_y` is unused while it is set.
- The projection reuses the 2D camera's `ortho` matrix (now
`pub(crate)`), which already has the `[0, 1]` depth range.
- `examples/07_3d`: O toggles between perspective and orthographic.
- README, changelog and the jam agent guide mention it (templates and
`guide.md` re-synced).

## Risks / notes

- `Camera3D` gains a public field, so code that builds one with a full
struct literal needs it. `Camera3D::new` and `gfx.camera3d` are
unaffected.

## Testing

- New unit test `ortho_has_no_foreshortening_and_fills_the_height`: the
corners of the view volume land on NDC `(1, 1, 0)` and `(1, 1, 1)`.
- `07_3d` checked by hand: O switches to the orthographic view.
- `task check`, clippy `--all-features`, wasm32 clippy and `cargo doc -D
warnings` pass.

## Review

The whole mechanism is `Camera for Camera3D` in
`crates/gfx/src/camera3d.rs`. The rest is docs and the example toggle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: gfx game-toolkit-gfx: wgpu renderer, sprites, SDF, text, tilemap, 3D area: ui game-toolkit-ui: egui debug overlay documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

game-toolkit-ui: egui renders into the sRGB surface (egui_wgpu warns, colours washed out)

1 participant