ui/gpu_out.rs#
Source: lib/ui/gpu_out.rs
gpu_out -- UI windows on the screen through a swapchain (Windows; Linux in programs against glibc;
Android; the browser: docs/GPU.md "Ausgabe der UI", "Ausgabe unter Linux", "Ausgabe unter Android",
"Ausgabe im Browser")
The UI rasterizes on the CPU as ever, the framebuffer stays the truth. This module only shows
it: the dirty rectangles the runtime computed go into a texture of the window, a triangle draws
the texture to the swapchain image, the image is presented. All windows share one device.
gp_window_mode() window_main: is the output wanted (jfgui_GPU, Remote Desktop)
gp_present(cur, pw, n) uiw_present: true when the GPU showed the frame, else the caller
presents through the CPU path and calls gp_cpu_done(cur, pw)
gp_tick() / gp_deadline() the window loop: finished jobs, frames that found no free image;
the time of the next look (None: nothing to look at, no wake-up)
gp_window_gone(i) before window i's plat window is destroyed
gp_window_show(i, show) before window i is hidden or shown again
gp_shutdown() window_main's end
sc_gpu(app, args) the script command `gpu` (below)
Ui::gpu_output(win) what a window shows through (status and counters for tests)
Stages of a window: Cpu (plat::win_present: GDI, as without a GPU) -> Job (its swapchain is
being built on a worker thread, the CPU path goes on) -> Switch (the whole framebuffer uploaded
and presented on the hidden child, then the child shown) -> Gpu. Back to Cpu: the window's size
changed (also the modal size loop: every frame of a resize is a CPU frame, drawn before
SetWindowPos returns, so nothing lags behind the frame; the swapchain is rebuilt when the size
has settled), or an error. Going back shows the framebuffer through the CPU path first, then
hides the child: no gap, no wrong frame. The first frames of a program are CPU frames: the
instance, device and swapchain (hundreds of ms the first time) are made on a worker thread that
starts after the first frame, so the first frame never waits for them.
A hidden window goes back to Cpu too: a swapchain's image does not come back with its window (white
until the next present, measured on Windows, and a frame that did not change presents nothing). What
the CPU path holds does come back, the swapchain follows as after a resize; nothing is built or
switched for a window nobody sees (unseen: the UI hid it, or the system with its minimised owner).
The swapchain lives on a child window (plat win_gpuout.rs): once a swapchain has presented on a
window GDI stays invisible there, also after the swapchain is gone.
Linux: the same code. A Swapchain there is a set of dma-bufs lib/gpu makes itself (vk_dmabuf.rs,
no VkSurface: that would want libwayland or libxcb), plat attaches them with its own protocol code
(linux_gpuout.rs); shared memory and dma-bufs take turns on one wl_surface or X window, so there is
no child and win_gpu_child only says what the display accepts. The differences this code sees: a
frame reaches the window after the GPU has finished it (flush, busy: the loop looks every ms while
one is on its way), the next one starts when the display has answered the last (next_look: no
polling, the answer ends the wait; the frame stays in the texture and its newest state is drawn
then), the compositor gets the frame's rectangles (damage), a worker may not ask the window system
(Surface::track_size). What cannot be read back is said by the tests: no client reads a Wayland
window, Xwayland's GetImage of a flipped window lags one frame.
Android: the same code again. The host's View with its Bitmap is the CPU path and stays below; the
swapchain lives on a SurfaceView above it (plat android_gpu.rs), the analogue of Windows' child
window: the surface exists while the View is visible, shows nothing until its first present and
then covers the Bitmap. Going back: the frame to the Bitmap, the SurfaceView away, and with it the
surface and swapchain (win_gpu_child_show). The system can also take the surface (the activity
stops): gp_surface_gone, before the host's callback returns. win_gpu_child says None for a while
(the surface is on its way, a tool window or popup shows on the canvas below): win_gpu_child_later,
the host wakes the loop.
The browser: the same code once more, through WebGPU (lib/gpu web_*.rs, web/gpu.js). The UI runs in a
worker in all three bundles and WebGPU's objects stay on that thread: there are no job threads, the jobs
run on this thread when the browser has answered (S::web_start: requestAdapter and requestDevice
answer later, the app goes on through the CPU path meanwhile). The page's #screen is the CPU path and
stays below; a canvas above it, an OffscreenCanvas here, is the analogue of the SurfaceView: it shows
nothing until its first frame, then covers #screen; each period on the GPU has a new one (plat
web_gpu.rs). Before a device is used, a small picture through all three pipelines is read back and
compared: a browser whose WebGPU draws wrongly never shows a frame (S::web_probe). After the switch the
page looks at what reached the canvas (watchGpuCanvas in web/loader.js): nothing there, and the CPU path
takes over for good (plat::win_gpu_child_blank). Reading back comes
later here: jfgui_GPU_VERIFY compares when the browser has answered (verify_poll). The shaders go as
WGSL (gpu_out.wgsl), no SPIR-V in the bundle. Runs with ?agent=1 stay on the CPU path like scripts,
unless ?gpu=1 asks; no script command here.
Presents never wait: Swapchain::try_acquire. A frame without a free image stays in the texture
and is shown by gp_tick or with the next frame (gp_deadline: 2 ms); so is a frame less than 4 ms
after the last present (nothing shows faster, the GPU need not draw it). Nothing runs while the
app is idle: no thread, no timer, no frame. Minimised windows upload nothing; they get the whole
frame when they come back.
Popups (plat::popup: small, short-lived windows with a window region) and tooltips stay on the
CPU path: a swapchain takes tens of ms, longer than many of them live.
Layers (G3b). The framebuffer stays the truth: all layers flattened, byte for byte what the CPU
path holds. What the GPU gets apart: a window's base texture holds the base layer only; each
layer above it with commands has a texture of its own that holds, in the 32 x 32 px cells the
layer touches, the pixels up to and including that layer, flattened on the CPU over what really
lies below (shadows, translucent widgets, subpixel text, RTL: nothing to get wrong, the same
kernels on the same pixels). The render pass draws the base, then these cells opaque in layer
order; the GPU blends nothing (blending a premultiplied layer differs from drawing directly by
1 .. 2 levels in about an eighth of its translucent pixels, measured, and subpixel text needs an
opaque target; docs/GPU.md). Beside each texture the pixels below the layer stay in a Store,
cell by cell, so a layer that moves or closes puts them back in the framebuffer: nothing below
it is drawn again or uploaded; closing costs no upload at all. dirty.rs tells per cell which
layer changed first (W::through): gp_split_draw restores below that layer, then draws and uploads
only from there on. Something changing below an open layer draws what lies above it in those
cells, as ever. Textures and stores exist while their layer has content and LINGER_US longer
(a menu closed and another opened need no new memory).
Images (G3b). A big opaque Cmd::Image of the base layer that has cost as much in redraws as its
upload would (rent or buy: a static image never takes GPU memory) becomes a texture, cached by
surface identity and version, under a budget with LRU eviction; it goes up in parts over the
frames that draw it (IMAGE_STEP_BYTES, the CPU draws it meanwhile). In the cells it fills, as the
last command there, its pixels are drawn by the GPU with Dc::blit_scaled's integer mapping: the
CPU neither draws nor uploads them (Split::owed: the framebuffer lacks them; gp_settle draws them
before anybody reads it). Everything else, also the cells at the image's edge or with something
on top of it, goes the usual way.
Canvases (G3c, canvas.rs). While a canvas's view moves and its content stays, its content comes
from tiles: TILE x TILE px textures of the content at one scale (a Level), drawn once by
CanvasContent::draw_view on this thread (at most FILL_US per frame) and kept under a budget with
LRU eviction. The render pass draws them with the transform: by copy (fs) at the level's scale
and whole pixels away, else bilinear (fs_tile). In the cells the canvas ends up on top of (also
those it only partly covers, with the scissor at its edge) the CPU neither draws nor uploads it:
its command's hash stands only for where it lies (dirty.rs), Split::owed and Split::stale as for
images, gp_settle runs its Draw before anybody reads the framebuffer. Cells with something over
it and cells without their tiles go the CPU's way: its Draw, the overlay, the upload. SETTLE_US
after the last move gp_tick asks for a frame, the canvas is no longer apart, the CPU draws what
the view shows: at rest the picture is the CPU path's. A frame that changes the content (new
CanvasDesc::ver, ui.canvas_gpu_damage) ends the gesture at once and costs no tile; a tile is only
drawn when a view moves over it. Nothing of it exists before the first move, and nothing SETTLE
+ CANVAS_UNUSED_US after the last one. Right-to-left windows draw canvases on the CPU.
Views (G5e1, gpu_view.rs). A view of GPU content (ui.gpu_view: a 3D scene between widgets) draws its frames
on this device into a texture of its size (gp_views_begin, at render time before the cells are hashed: a new
ver, size or device draws one, nothing else does). Where the window is on its swapchain and the view ends up
on top it is a quad like an image's (QuadSrc::View, drawn by the shader that copies: `fs` at its place), its
command's hash stands for where it lies as a canvas apart's (dirty.canv), and a new frame counts as a canvas's
move: its cells owed and stale, the cells with something over it and under a layer drawn again by the CPU,
whose Draw reads the frame back (the only way the CPU gets it: a copy to a read-back buffer, waited for).
VIEW_REST_US after the last frame gp_tick settles the window once: at rest the framebuffer holds the picture.
Anywhere else the view's command hashes its frame's number, so each new frame is drawn by the CPU, which reads
it back. A view's frame is bound to the display (G5e3, docs/GPU.md "3D-Ansicht"): while its window is on its
swapchain with a frame not shown yet (Win::pending), a new ver waits; gp_views_wake asks for a render once the
window's frame has been shown, at the latest VIEW_HOLD_US after the first frame waited (a display that never
answers does not stop the view). Without this a view drawn as fast as the app can starved the compositor on the
same GPU (Intel under Linux: no frame presented at all). (G7b) Cells of the base with something over a view (a game's
HUD, a menu) are veils (below), not read back. AF_GPU_3D makes the device at level Voll on the fastest
adapter; without the flag views work on the Basis device of the frugal adapter. Without an output in this run (not
wanted, never possible) views make a device of their own (gp_view_device). A lost device detaches every content
(gp_views_gone) and attaches it to the next one.
Veils (G7b, docs/GAMECORE.md "Spielfenster"). Before, a cell of the base layer with anything over a view apart (a
HUD's text, a menu's panel) was drawn by the CPU over the view's frame, and its Draw read every new frame back: a copy
of the whole target and a wait for the GPU in each frame, 1.3 ms at 1200 x 700 on the RTX 2070 SUPER, and the CPU drew
and uploaded those cells again although nothing in them changed but the view. Now such a cell (the view the only one
in it, no layer above it) is a veil: the CPU draws what lies over the view twice, once over a black view and once over
a white one (Cpu::veil: the view's Draw paints the colour, reads nothing), and keeps K, the cell over black, in the
base texture and T = white − black, per channel the share of the view that shows through, in the veil's texture.
The output's pass draws the cell as K + T · view (fs_veil), which is the CPU's "over" exactly: every blend of gfx is
dst = src + dst · (255 − a) / 255, with K = src and T = 255 − a, and the shader rounds once (the CPU rounds the
product to the nearest level too). Text in veils is greyscale (subpixel text blends in linear light, which is not of
that form; as in layers and images, text/lcd.rs). A new frame of the view costs a veil nothing: K and T change only
when what lies over the view changes, then the cell is drawn twice and goes up. The framebuffer lacks the true pixels
of a veil (like owed cells): gp_settle draws them over the frame read back (once, at rest), puts them into the base
texture and ends the veil, the next frame of the view makes it again. jfgui_GPU_VEIL=0 (Ui::gpu_test_veil): the
way before, every frame read back.
A lost device or any other failure: every window back to the CPU path (the framebuffer holds the
frame), a new device after 2 s, 4 s, ... 5 min (jfgui_GPU_RETRY_MS: the first wait). No Vulkan, no
usable adapter or surface, a display that takes or imports no dma-bufs: the CPU path for good, silently
but for one line in the log (level info) that says why. plat::gpu_out_state tells the platform how the first
start went (Android keeps it across runs: a driver that killed the program does not get a third chance).
Environment: jfgui_GPU=0|1 (off, on; default: DEFAULT_ON, DEFAULT_ON_LINUX, DEFAULT_ON_BROWSER; Android:
GalleryActivity always sets it, GPU_DEFAULT there; the browser: ?gpu=0|1), jfgui_GPU_VERIFY=1 (every presented
image read back and compared with the framebuffer), jfgui_GPU_TRACE=1 (stderr: the stages and
their times), jfgui_GPU_LAYERS=0 (one texture per window: no layers, no images),
jfgui_GPU_IMAGES=0 (no images), jfgui_GPU_IMAGE_MB=n (the images' budget), jfgui_GPU_CANVAS=0 (no
canvas uses tiles), jfgui_GPU_CANVAS_CPU=a,b (the canvases named so draw on the CPU),
jfgui_GPU_CANVAS_PAN=a,b (they only pan from tiles), jfgui_GPU_CANVAS_ZOOM=a,b (they also zoom from
tiles, whatever the widget chose), jfgui_GPU_CANVAS_MB=n (the tiles' budget),
jfgui_GPU_CANVAS_SETTLE_MS=n (the time until the CPU's picture follows a move), jfgui_GPU_OUTPUT=0 (the
windows through the CPU path, the GPU stays for views, which read every frame back), jfgui_GPU_VIEWS=0 (no view
is drawn by the output's render pass: every frame read back into the framebuffer), jfgui_GPU_VEIL=0 (no veils:
cells with something over a view read its frame back);
lib/gpu's jfgui_GPU_PRESENT, jfgui_GPU_ADAPTER, jfgui_GPU_VALIDATE, ...
Script command `gpu` (headless runs stay on the CPU path without it; on other targets (a static
Linux program, the browser), without a GPU, or when the screen's work area cannot hold the
window at `gpu on` (the system keeps it smaller, on the CPU path), it prints a SKIP once and
does nothing):
gpu on [fast] a real window per UI window (shown without activation) with
the output, every presented image compared with the framebuffer (fast:
not compared, for measurements), a render after every input edge; waits
for the swapchain; a canvas stays apart until `gpu settle` or `same`
gpu off windows closed
gpu wait [MS] until the scoped windows are on the swapchain (after `size`, `gpu lose`)
gpu expect gpu|cpu the main window's stage
gpu settle every canvas goes back to the CPU's picture now
gpu expect same every canvas settled, every window presents once more: the image read
back = the framebuffer byte for byte (x byte: 255), and no frame differed
since the last check (a canvas's scaled tiles count as GpuOutput::canvas_diff_*)
gpu expect screen the composed screen at the window = the framebuffer (another window on
top of ours is a note, not a failure; Windows: also what takes no clicks,
an overlay or a shadow, when the window as DWM composes it = the
framebuffer); X11: what the server holds for the window; Wayland: a
note, no client can read it
gpu expect window the window's DC = the framebuffer (the CPU path's pixels); Linux as `screen`
gpu expect upload N at most N bytes uploaded since the last check
gpu expect base N at most N bytes of them to the base texture since the last check
gpu expect raster N at most N pixels of the base layer drawn since the last check
gpu expect layers N N layers above the base have a texture now
gpu expect images N N image textures exist
gpu expect apart N N canvases of the window come from tiles now
gpu expect tiles N N canvas tiles exist
gpu expect fills N at most N tiles were drawn since the last check
gpu expect nodiff no pixel of a canvas's tiles differed from the CPU's since the last
`same` or `nodiff`
gpu budget BYTES the images' budget
gpu tilebudget BYTES the tiles' budget
gpu shot FILE the image the window presents now (read back: also what a gesture's
tiles show; `shot` writes the framebuffer)
gpu lose the device behaves as lost from now on (Device::test_lose)
gpu hold MS real time passes, the windows stay as they are (a look from outside)
gpu print the counters
/// the embedded shaders of the output and their source (uigputest: the embedding check)
GPU_OUT_WGSL: &str = include_str!("gpu_out.wgsl")
GPU_OUT_VS: &[u32] = spv::VS
GPU_OUT_FS: &[u32] = spv::FS
GPU_OUT_FS_IMAGE: &[u32] = spv::FS_IMAGE
GPU_OUT_FS_TILE: &[u32] = spv::FS_TILE
GPU_OUT_FS_VEIL: &[u32] = spv::FS_VEIL
impl Ui
fn gpu_output(&self, win: u32) -> GpuOutputwhat windowwin(a UI window index, 0 = main) shows throughfn gpu_test_lose(&mut self)(test aid) the device behaves as lost from now on: the next frame goes back to the CPU path, a new device followsfn gpu_test_hold(&mut self, win: u32, on: bool)(test aid, G5e3) windowwin's frames are not shown while on (as if the display did not answer): they stay in its texture, its views' new frames wait for them; off: the next look shows the newestfn gpu_test_veil(&mut self, on: bool)(test aid, G7b) cells of the base with something over a view are veils (the default; jfgui_GPU_VEIL=0 turns them off), or off: those cells read the view's frame back, the way before. For measurementsfn gpu_test_image_budget(&mut self, bytes: u64)(test aid) the bytes image textures may takefn gpu_test_canvas_budget(&mut self, bytes: u64)(test aid) the bytes canvas tiles may takefn gpu_test_canvas_unused(&mut self, ms: u64)(test aid) the tiles go this long (ms) after the last movefn gpu_canvas_settle(&mut self)every canvas whose view comes from tiles goes back to the CPU's picture with the next framefn gpu_settle(&mut self)the pixels of the images the GPU draws into the framebuffers (Ui::fb reads them then)