Repository navigation
Draw egui through a non-sRGB view of the window - #107
Conversation
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
| ) -> Vec<wgpu::TextureFormat> { | ||
| let srgb = surface_format.add_srgb_suffix(); | ||
| let linear = surface_format.remove_srgb_suffix(); | ||
| if srgb != surface_format { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| /// [`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) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
| gfx.device(), | ||
| gfx.surface_format(), | ||
| // egui outputs gamma-space colours; an sRGB target would encode them twice. | ||
| gfx.overlay_format(), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
|
||
| ### Fixed | ||
|
|
||
| - `Ui` (egui) rendered into the sRGB view of the window, so its colours were sRGB-encoded twice: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (4 files)
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
Issue Details (click to expand)WARNING
SUGGESTION
Files Reviewed (4 files)
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.
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.
Uicreated 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.Uinow draws through a non-sRGB view of the window, the format egui prefers.Fixes #106
Key changes
DownlevelFlags::SURFACE_VIEW_FORMATS(surface_view_formatsingraphics.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::newbuilds the eguiRendererwithoverlay_format()andUi::rundraws into the overlay view.Risks / notes
fs_main_linear_framebuffershader, so the visible difference was probably blending rather than a full double encode. I did not measure a pixel read-back.SURFACE_VIEW_FORMATS(GL) the surface keeps no extra view format andoverlay_format()falls back to the sRGB format, so behaviour there is unchanged and the warning stays.Testing
ex_06_eguion Linux/Vulkan (Intel Iris Xe): starts with no egui_wgpu framebuffer warning and no wgpu validation error.frames_render_srgb_and_overlays_non_srgbcovers the render and overlay format for an sRGB surface with and without the downlevel flag, and for a non-sRGB surface.-D warnings(default,--all-features, wasm32) andcargo doc -D warningspass.Review
Start with
surface_view_formatsandoverlay_formatat the bottom ofcrates/gfx/src/graphics.rs; they decide which view formats the surface gets and which one overlays use.Frameonly carries the extra view, and theUichange is two lines.