diff --git a/crates/gpu-wgpu/src/lib.rs b/crates/gpu-wgpu/src/lib.rs index 4d0e290354..9a6364f4ee 100644 --- a/crates/gpu-wgpu/src/lib.rs +++ b/crates/gpu-wgpu/src/lib.rs @@ -86,7 +86,7 @@ mod native { /// An instance over whichever native backends were compiled in (`native-vulkan`, /// `native-dx12`, `native-metal`, `native-gles`), honoring `WGPU_BACKEND` when set, so the - /// same build can be pointed at a software Vulkan adapter (lavapipe/llvmpipe) in CI. + /// same build can be pointed at a software Vulkan adapter (lavapipe/llvmpipe) in a local container run or a CI lane. /// `new_without_display_handle` because this context never has a window (no surface is ever /// requested); FilmCraft's own off-screen export device uses the same constructor for the /// same reason (`filmcraft-gpu`'s `export_renderer.rs`, see `docs/gpu-backends.md`). diff --git a/crates/gpu-wgpu/src/tests.rs b/crates/gpu-wgpu/src/tests.rs index a4d31c4e39..6f0b74f88e 100644 --- a/crates/gpu-wgpu/src/tests.rs +++ b/crates/gpu-wgpu/src/tests.rs @@ -3,7 +3,9 @@ //! RFC-0022 acceptance requirement 2: real backends beside the null one. `craft-gpu-null` stays //! mandatory for every consumer (it cannot validate shader compilation, resource rules, or //! numerical results), so these tests run representative jobs on a real wgpu device when one is -//! available, and skip loudly rather than failing the suite when the host genuinely has none +//! available. Runs against a software Vulkan adapter in a local container are local testing, not +//! hosted CI coverage: hosted lanes and the pilot consumers' browser and hardware validation +//! remain their own, separate requirements. The suite skips loudly when the host has no adapter //! (`cargo test` without `--features adapter-probe,native-vulkan` already exercises the masked //! path: no adapter is ever requested, and every context here falls back to the staging path, //! same shape as `craft-gpu-null`). diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index 22ca694e49..8d8ccb0a24 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -25,10 +25,10 @@ pub struct ReadOutcome { /// ## Device loss /// /// A context can lose its device at any time: a driver reset, OS-level eviction under memory -/// pressure, or an explicit `device.destroy()`. Device loss is a fault, not a panic. It surfaces -/// through [`GpuFault::DeviceLost`] from the next call that would otherwise touch the device, and -/// also through [`poll_reads`](Self::poll_reads), so a host's event loop can observe it even -/// between buffer calls. Once a context has reported [`GpuFault::DeviceLost`], every +/// pressure, or an explicit `device.destroy()`. Device loss surfaces as a fault and never panics. It arrives +/// through [`GpuFault::DeviceLost`] from the next call that would otherwise touch the device. +/// It also arrives through [`poll_reads`](Self::poll_reads), letting a host's event loop observe +/// it even between buffer calls. Once a context has reported [`GpuFault::DeviceLost`], every /// [`DeviceCaps`] it reported before that point is stale. The consumer's recovery, per RFC-0022, /// is one of: /// @@ -50,13 +50,16 @@ pub struct ReadOutcome { /// later poll, analogous to `craft-worker-host`'s `HostMessage::GpuJobs` being answered later by /// `WorkerMessage::GpuBatchDone`. /// -/// A backend that can honestly block (the null context, which never has anything to wait for; -/// native wgpu driving `Device::poll` itself) uses `read_buffer`'s default body. A backend that -/// cannot honestly block without freezing its single thread (WebGPU on `wasm32`, where a map -/// completes only via the browser's own event loop) overrides `read_buffer` to return -/// [`GpuFault::Operation`] instead of deadlocking; callers there must drive `request_read` and -/// `poll_reads` from the worker's own message loop. See `docs/gpu-backends.md` for which backend -/// does which. +/// Both bundled backends override `read_buffer`'s default body, since the default has nowhere +/// to put completions belonging to other outstanding requests: the null context takes only its +/// own completion, and the wgpu context re-queues the others while blocking efficiently on a +/// native device. On `wasm32` the wgpu context's only backing today is the in-memory staging +/// store, whose reads resolve synchronously and are served; only a read that did not resolve +/// synchronously returns [`GpuFault::Operation`] there, the refusal a future real device backing +/// will need, because a map on that target completes only via the browser's own event loop and +/// blocking for it would freeze the one thread that loop runs on. Callers on such a backing +/// drive `request_read` and `poll_reads` from the worker's own message loop. See +/// `docs/gpu-backends.md` for which backend does which. pub trait GpuContext { fn caps(&self) -> DeviceCaps; diff --git a/docs/gpu-backends.md b/docs/gpu-backends.md index 35527d5b0b..6e2246e918 100644 --- a/docs/gpu-backends.md +++ b/docs/gpu-backends.md @@ -8,18 +8,18 @@ Feature flags on `craft-gpu-wgpu`: -- `adapter-probe` — native only; uses pollster to `request_adapter`/`request_device` and build real [`DeviceCaps`](../crates/gpu/src/caps.rs) from the created device (falls back to the staging path when no GPU, e.g. CI with no backend feature enabled). -- `native-vulkan`, `native-dx12`, `native-metal`, `native-gles` — enable the matching wgpu backend. `adapter-probe` alone compiles the probing code but finds no adapter unless at least one of these is also on. +- `adapter-probe`: native only; uses pollster to `request_adapter`/`request_device` and build real [`DeviceCaps`](../crates/gpu/src/caps.rs) from the created device (falls back to the staging path when no GPU, e.g. CI with no backend feature enabled). +- `native-vulkan`, `native-dx12`, `native-metal`, `native-gles`: enable the matching wgpu backend. `adapter-probe` alone compiles the probing code but finds no adapter unless at least one of these is also on. ## Real device, device-truth caps (RFC-0022 requirement 1) -With `adapter-probe` and a `native-*` backend, `WgpuContext::new` calls `Adapter::request_device`, not just `request_adapter`. `DeviceCaps` is built from the created `Device`'s own `limits()` / `features()`, never from the adapter's advertised maximum: an adapter can advertise more than a device grants. `required_features` is the adapter's features intersected with what this context asks for (currently nothing — no provider dispatches a real WGSL job yet, see [gpu-providers.md](./gpu-providers.md)); `required_limits` is the adapter's own limits. Both choices mean `request_device` cannot panic on an unsupported feature or limit here. +With `adapter-probe` and a `native-*` backend, `WgpuContext::new` calls `Adapter::request_device` after `request_adapter`, creating a real device. `DeviceCaps` is built from the created `Device`'s own `limits()` / `features()`, never from the adapter's advertised maximum: an adapter can advertise more than a device grants. `required_features` is the adapter's features intersected with what this context asks for (currently nothing, since no provider dispatches a real WGSL job yet, see [gpu-providers.md](./gpu-providers.md)); `required_limits` is the adapter's own limits. Both choices mean `request_device` cannot panic on an unsupported feature or limit here. -Buffers are real `wgpu::Buffer`s (`STORAGE | COPY_SRC | COPY_DST`), not a `Vec>` staged on the host. +Buffers are real `wgpu::Buffer`s (`STORAGE | COPY_SRC | COPY_DST`). The host-staged `Vec>` form survives only on the no-device path. -**The no-adapter path is still defined, not an error.** Without a usable adapter — `adapter-probe` off, no backend feature on, no GPU present, or on `wasm32` (see below) — `WgpuContext` degrades to the same in-memory staging behavior as `craft-gpu-null`. `WgpuContext::has_device()` tells a caller which path it got; `probe_caps()` (no context) stays the quick, adapter-only, advisory estimate it always was. +**The no-adapter path is still a defined path.** Without a usable adapter (`adapter-probe` off, no backend feature on, no GPU present, or `wasm32`, see below), `WgpuContext` degrades to the same in-memory staging behavior as `craft-gpu-null`. `WgpuContext::has_device()` tells a caller which path it got; `probe_caps()` (no context) stays the quick, adapter-only, advisory estimate it always was. -**`wasm32` gap, acknowledged, not hidden.** RFC-0022 itself describes browser capability probing moving "from static defaults to real adapter queries once async initialization lands in the worker" — that landing hasn't happened yet. `request_adapter`/`request_device` are JS promises; driving them synchronously the way `pollster::block_on` does natively isn't available on `wasm32`, and wiring the async worker-init path is a larger, separate piece of work. Until then, `WgpuContext` on `wasm32` reports the same conservative, adapter-free defaults it always has. This is the RFC's own acknowledged follow-up, not new scope invented for this PR. +**`wasm32` gap, acknowledged, not hidden.** RFC-0022 itself describes browser capability probing moving "from static defaults to real adapter queries once async initialization lands in the worker". That landing hasn't happened yet. `request_adapter`/`request_device` are JS promises. Driving them synchronously the way `pollster::block_on` does natively is unavailable on `wasm32`. Wiring the async worker-init path is a larger, separate piece of work. Until then, `WgpuContext` on `wasm32` reports the same conservative, adapter-free defaults it always has. This is the RFC's own acknowledged follow-up, not new scope invented for this PR. ## Device loss (RFC-0022 requirement 1) @@ -36,14 +36,14 @@ No backend here supports continuing to dispatch jobs against a context that alre `GpuContext::read_buffer` used to be synchronous. WebGPU can only map a buffer asynchronously (`map_async`), so the trait's real primitives are now `request_read` (queue a readback, get a `ReadRequest`) and `poll_reads` (drain completions). This mirrors `craft-worker-host`'s own async shape: a `HostMessage::GpuJobs { batch_id, .. }` is answered later by `WorkerMessage::GpuBatchDone { batch_id, outcomes }`, not inline; `request_read`/`poll_reads` is the same split one level down, for one buffer instead of one job batch. -`read_buffer` stays as a blocking convenience with a default body built on those two primitives, for callers that need bytes now (the CPU-stand-in providers in `craft-gpu-providers`): +`read_buffer` stays as a blocking convenience with a default body built on those two primitives, for callers that need bytes now (the CPU-stand-in providers in `craft-gpu-providers`). Both bundled backends override that default body. The default has nowhere to put completions belonging to other outstanding requests. The overrides keep those queued for the next `poll_reads`: -- `craft-gpu-null`: resolves every request immediately — nothing to wait for. -- `craft-gpu-wgpu`, native, with a real device: blocks by calling `Device::poll(PollType::wait_indefinitely())` between polls, so it waits on the GPU instead of busy-spinning the CPU. +- `craft-gpu-null`: overrides to take only its own completion; every request resolves immediately, with nothing to wait for. +- `craft-gpu-wgpu`, native, with a real device: overrides to block by calling `Device::poll(PollType::wait_indefinitely())` between polls. It waits on the GPU without busy-spinning the CPU, re-queuing other requests' completions. - `craft-gpu-wgpu`, native, staging path (no device): resolves immediately, same as the null context. -- `craft-gpu-wgpu`, `wasm32`: **returns `GpuFault::Operation`, not bytes.** `wasm32` is single-threaded; blocking the one thread the browser needs to run `map_async`'s own completion callback on would hang the tab, not return. A caller there must drive `request_read` + `poll_reads` from the worker's own message loop instead of calling `read_buffer`. +- `craft-gpu-wgpu`, `wasm32`: **serves the staging read.** The only backing on this target today is the in-memory staging store, since real device creation needs the async worker-init path that has not landed. Staging reads resolve synchronously. `read_buffer` returns their bytes. Only a read that did not resolve synchronously returns `GpuFault::Operation`. That refusal will matter once a real device backing exists there: `wasm32` is single-threaded and blocking the one thread the browser needs to run `map_async`'s own completion callback on would hang the tab. A caller on a future device backing must drive `request_read` + `poll_reads` from the worker's own message loop. -**Known follow-up on `craft-libs#662`.** That branch's `RuntimeContext` wrapper (`crates/runtime/src/runtime.rs`) implements `GpuContext` by matching on its `Null`/`Wgpu` variants, including an explicit `read_buffer` arm. When #662 rebases past this PR, that block needs two added match arms (`request_read`, `poll_reads`) delegating to each inner context the same way the existing methods do, and its own `read_buffer` arm can simply be deleted in favor of the trait's default (or kept, delegating the same way). No logic in `craft-worker-host` or the runtime facade changes: `HostMessage::GpuJobs` still dispatches a whole batch synchronously today, same as before this PR. +**Consumed on `craft-libs#662`.** That branch's `RuntimeContext` wrapper now forwards `request_read`, `poll_reads`, `free_buffer` and `device_status` to its inner context. It forwards `read_buffer` too, letting the inner backend's completion-preserving override apply. No logic in `craft-worker-host` or the runtime facade changed shape beyond that: `HostMessage::GpuJobs` still dispatches a whole batch synchronously today, answered per job. ## Named consumers (RFC-0022's "what this RFC decides") @@ -56,6 +56,6 @@ No backend here supports continuing to dispatch jobs against a context that alre The question that decides whether a render-path app can adopt `WgpuContext` as-is: who owns the `Device`/`Queue` when egui (via `eframe`/`egui-wgpu`) is also drawing? -**FilmCraft's answer today is "it depends on which device."** Its on-screen monitor compositor adopts egui's own device: both the native desktop app and the web app call `app.set_wgpu(cc.wgpu_render_state.clone())` (or the equivalent on native), so the compositor draws through the exact `Device`/`Queue` `eframe`'s `RenderState` already created — one device, shared, no second adapter opened for the window. Its **export** path is deliberately the opposite: a separate, off-screen `wgpu::Device` with no surface, created once and kept for the process (`export_renderer.rs`), specifically because export runs independent of the UI's lifecycle (worker threads, no window) and must keep working even if the UI's device were ever lost or recreated. +**FilmCraft's answer today is "it depends on which device."** Its on-screen monitor compositor adopts egui's own device: both the native desktop app and the web app call `app.set_wgpu(cc.wgpu_render_state.clone())` (or the equivalent on native), so the compositor draws through the exact `Device`/`Queue` `eframe`'s `RenderState` already created. One device, shared, no second adapter opened for the window. Its **export** path is deliberately the opposite: a separate, off-screen `wgpu::Device` with no surface, created once and kept for the process (`export_renderer.rs`), specifically because export runs independent of the UI's lifecycle (worker threads, no window) and must keep working even if the UI's device were ever lost or recreated. -`WgpuContext::new` in this PR always opens its own device (or degrades to staging) and has no constructor that accepts an externally-supplied `(Device, Queue)`. That is **documented here as the named follow-up, not implemented in this PR**: accepting an existing device/queue pair (with `DeviceCaps` built from that pair, the same device-truth way `create_device` builds it today) is a small, additive constructor — it does not change `GpuContext`'s trait shape — but it needs a decision this PR's scope doesn't force: whether the shared constructor should require the caller to prove the features/limits it needs were already granted (since a second caller can't call `request_device` again on a device that already exists), or whether it should accept whatever was granted and fail a job if it needs more. FilmCraft's own split (share for render, separate for export) is a reasonable default for this layer too: a render-path app wanting GPU compute on the same frame it draws with egui should share; a worker-isolated batch job (the first named consumer, `craft-libs#662`'s worker-host) has no window and no reason to share, and keeps its own device exactly as implemented here. +`WgpuContext::new` in this PR always opens its own device (or degrades to staging) and has no constructor that accepts an externally-supplied `(Device, Queue)`. That is **documented here as the named follow-up, not implemented in this PR**: accepting an existing device/queue pair (with `DeviceCaps` built from that pair, the same device-truth way `create_device` builds it today) is a small, additive constructor that does not change `GpuContext`'s trait shape, but it needs a decision this PR's scope doesn't force: whether the shared constructor should require the caller to prove the features/limits it needs were already granted (since a second caller can't call `request_device` again on a device that already exists), or whether it should accept whatever was granted and fail a job if it needs more. FilmCraft's own split (share for render, separate for export) is a reasonable default for this layer too: a render-path app wanting GPU compute on the same frame it draws with egui should share; a worker-isolated batch job (the first named consumer, `craft-libs#662`'s worker-host) has no window and no reason to share, and keeps its own device exactly as implemented here. diff --git a/docs/gpu-providers.md b/docs/gpu-providers.md index 0705b9124f..a1282b8a1e 100644 --- a/docs/gpu-providers.md +++ b/docs/gpu-providers.md @@ -2,17 +2,17 @@ Provider ids: -- `craft.*` — shared ops in **craft-gpu-providers** (no app crate deps) -- `craft.noop`, `craft.test.echo`, `craft.mask`, `craft.composite.premul`, `craft.readback` — registered by [`register_all()`](../crates/gpu-providers/src/lib.rs) for worker/runtime init - - `craft.mask` — CPU stand-in for FilmCraft mask composite until WGSL lands on wgpu - - `craft.composite.premul` — CPU premultiplied "over" (Normal path from `composite.wgsl`) until WGSL lands on wgpu - - `craft.readback` — CPU decode of staging buffers (`f16` RGBA → `f32`, or `fmt:u8` passthrough); uses [`chunk_buffer`](../crates/gpu/src/chunk.rs) against device caps and [`f16_to_f32*`](../crates/gpu/src/readback.rs) from **craft-gpu** -- `{app}.*` — registered by application wasm at init +- `craft.*`: shared ops in **craft-gpu-providers** (no app crate deps) +- `craft.noop`, `craft.test.echo`, `craft.mask`, `craft.composite.premul`, `craft.readback`: registered by [`register_all()`](../crates/gpu-providers/src/lib.rs) for worker/runtime init + - `craft.mask`: CPU stand-in for FilmCraft mask composite until WGSL lands on wgpu + - `craft.composite.premul`: CPU premultiplied "over" (Normal path from `composite.wgsl`) until WGSL lands on wgpu + - `craft.readback`: CPU decode of staging buffers (`f16` RGBA → `f32`, or `fmt:u8` passthrough); uses [`chunk_buffer`](../crates/gpu/src/chunk.rs) against device caps and [`f16_to_f32*`](../crates/gpu/src/readback.rs) from **craft-gpu** +- `{app}.*`: registered by application wasm at init -Generic worker code must only call `ProviderRegistry::dispatch` — never import app gpu crates. +Generic worker code must only call `ProviderRegistry::dispatch`: never import app gpu crates. Dependency lint: **craft-gpu-providers** must not depend on `effectcraft-*`, `filmcraft-*`, or `*-engine`. ## Readback is async underneath; these providers don't see it -`GpuContext::read_buffer`, the method every provider above calls, is a blocking convenience over the trait's real primitives, `request_read` + `poll_reads` (see [gpu-backends.md](./gpu-backends.md)). Nothing in this crate changed for that: `craft-gpu-null` still resolves `read_buffer` immediately, and `craft-gpu-wgpu`'s native path now does too, by blocking on the device between polls instead of staging in a plain `Vec>`. `crates/gpu-wgpu/src/tests.rs` runs these same providers (`craft.test.echo`, `craft.composite.premul`, `craft.readback`) against a real `WgpuContext`, not just the null one, when a real adapter is available (RFC-0022 requirement 2). A provider dispatching a real WGSL job will be the one that actually needs the non-blocking `request_read`/`poll_reads` shape directly, since on `wasm32` `read_buffer` itself returns a fault rather than blocking. +`GpuContext::read_buffer`, the method every provider above calls, is a blocking convenience over the trait's real primitives, `request_read` + `poll_reads` (see [gpu-backends.md](./gpu-backends.md)). Nothing in this crate changed for that. `craft-gpu-null` still resolves `read_buffer` immediately. `craft-gpu-wgpu`'s native path now does too, blocking on the device between polls. `crates/gpu-wgpu/src/tests.rs` runs these same providers (`craft.test.echo`, `craft.composite.premul`, `craft.readback`) against a real `WgpuContext` as well as the null one, when a real adapter is available (RFC-0022 requirement 2). A provider dispatching a real WGSL job will be the one that actually needs the non-blocking `request_read`/`poll_reads` shape directly. On `wasm32` today `read_buffer` serves the staging read, the only backing on that target. A read that did not resolve synchronously faults. A future real device backing there will refuse blocking outright, per `gpu-backends.md`.