Skip to main content

WgpuRenderer

Struct WgpuRenderer 

Source
pub struct WgpuRenderer { /* private fields */ }
Expand description

WGPU-based renderer for GPU-accelerated 2D rendering.

This renderer supports:

  • GPU-accelerated shape rendering (rectangles, rounded rectangles)
  • Gradients (solid, linear, radial)
  • GPU text rendering via retained raster image batches
  • Cross-platform support (Desktop, Web, Android)

Implementations§

Source§

impl WgpuRenderer

Source

pub fn new(fonts: &[&[u8]]) -> Self

Create a new WGPU renderer.

  • fonts – font bytes to load, ordered by priority (first = highest priority). Pass &[] to load no fonts; text will not render until fonts are provided.

Call init_gpu before rendering.

Source

pub fn with_font_set(fonts: SoftwareTextFontSet) -> Self

Create a renderer over an already-parsed font set.

Measurement and rasterization both take clones of this one set, so an app-supplied family resolves identically on both sides.

Source

pub fn with_text_system(text_system: WgpuTextSystem) -> Self

Source

pub fn init_gpu( &mut self, device: Arc<Device>, queue: Arc<Queue>, surface_format: TextureFormat, adapter_backend: Backend, adapter_downlevel: DownlevelFlags, )

Initialize GPU resources with a WGPU device and queue.

Replacing a live renderer (Android surface recreation, device loss) bumps the renderer epoch, so a packet built against the previous renderer is cancelled instead of drawn.

Source

pub fn warm_shaders(&mut self, warm_ups: impl IntoIterator<Item = ShaderWarmUp>)

Registers runtime shaders to compile on the background compiler at every init_gpu, before their first draw, so an app’s own shaders reach the compiler the way the framework’s do. Call it before the first init_gpu; each warm-up names the target its pipeline draws to.

Source

pub fn init_gpu_threaded( &mut self, device: Arc<Device>, queue: Arc<Queue>, surface_format: TextureFormat, adapter_backend: Backend, adapter_downlevel: DownlevelFlags, waker: Arc<dyn Fn() + Send + Sync>, clock: Option<Arc<dyn Fn() -> i64 + Send + Sync>>, ) -> Result<(), WgpuRendererError>

init_gpu for the threaded present runtime (Android): the same epoch bump and planner replacement hygiene, but instead of constructing a GpuRenderer here, everything it needs — all owned, all Send — crosses to a spawned present thread that constructs its own (its Rc caches are thread-confined). Frames then flow through publish_frame / drain_present_returns under the depth-one credit protocol instead of render.

  • waker — wakes the producer’s event loop after every returns send (the Android frame waker).
  • clock — producer’s monotonic nanosecond clock, so present-side PresentTimings share the producer telemetry’s clock domain; None leaves timings at zero.
Source

pub fn note_surface_reconfigured(&mut self)

Record that the surface was reconfigured (resize, format change, swapchain recreation): bumps the surface epoch stamped into every subsequent packet, so a packet built against the previous configuration is cancelled by the present stage instead of drawn.

Source

pub fn set_root_scale(&mut self, scale: f32)

Set root scale factor for text rendering (e.g., density scaling on Android)

Source

pub fn set_transparent_background(&mut self, transparent: bool)

Clears each frame to nothing instead of the framework’s background, for a window whose surface composites with the desktop behind it. A renderer starts opaque; false puts the background back. Takes effect from the next frame, on either present backend.

Source

pub fn root_scale(&self) -> f32

Source

pub fn render( &mut self, texture: &Texture, view: &TextureView, width: u32, height: u32, ) -> Result<(), WgpuRendererError>

Render the scene to a texture view.

Producer first, present second: the frontend collects the frame into a frame_packet::FramePacket (root and dev overlay alike), the GPU renderer consumes it, and the present stage’s returns fold back into the frontend afterwards.

Source

pub fn render_surface_texture( &mut self, texture: &Texture, view: &TextureView, width: u32, height: u32, ) -> Result<(), WgpuRendererError>

Renders the frame into a presentable image. When the image carries the composition format and the capture usages, the scene renders into it directly and no output conversion pass runs.

Source

pub fn present(&self, frame: SurfaceTexture)

Presents a surface image this renderer drew with render_surface_texture, on the queue that recorded it.

Before init_gpu there is no queue, and the image is released without being shown.

Source

pub fn capture_frame( &mut self, width: u32, height: u32, ) -> Result<CapturedFrame, WgpuRendererError>

Render the current scene into an RGBA pixel buffer for robot tests.

Uses the renderer’s configured root scale.

Source

pub fn capture_frame_with_scale( &mut self, width: u32, height: u32, root_scale: f32, ) -> Result<CapturedFrame, WgpuRendererError>

Render the current scene into an RGBA pixel buffer with an explicit scale.

Source

pub fn has_frame_credit(&self) -> bool

Threaded mode: whether the depth-one slot has room for a packet. The Android loop checks this BEFORE shell.update() so backpressure lands before the expensive update/lowering work. Always true on the sync path, which has no slot to fill.

Source

pub fn publish_frame(&mut self, width: u32, height: u32) -> PublishOutcome

Threaded mode: lower the current scene into a packet and hand it to the present runtime. Credit is checked FIRST — a NoCredit return means no packet was built at all (frame_sequence does not advance). Returns NoCredit (with an error log) when the renderer is not in threaded mode.

Source

pub fn drain_present_returns(&mut self) -> usize

Threaded mode: fold every pending RenderReturns back into producer state and free the publish credit. Returns how many were drained. No-op outside threaded mode.

Source

pub fn drain_present_returns_with( &mut self, on_return: &mut dyn FnMut(u64, PresentOutcome, PresentTimings), ) -> usize

drain_present_returns, reporting each drained frame’s id, outcome and present-thread timings — the Android loop feeds its frame telemetry from this.

Source

pub fn present_replace_surface( &mut self, surface: Surface<'static>, config: SurfaceConfiguration, ) -> bool

Threaded mode: install a (re)created surface on the present thread and wait for the acknowledgement. The caller must have bumped the surface epoch first (note_surface_reconfigured Self::note_surface_reconfigured) when the message invalidates in-flight packets; the message carries the current epoch.

Source

pub fn present_reconfigure(&mut self, config: SurfaceConfiguration) -> bool

Threaded mode: reconfigure the present thread’s surface (resize) and wait for the acknowledgement. Same epoch contract as present_replace_surface.

Source

pub fn present_drop_surface(&mut self) -> bool

Threaded mode: drop the present thread’s surface (the window died; the renderer survives for the next one) and wait for the acknowledgement. Bump the epoch first so in-flight packets cancel.

Source

pub fn shutdown_present_runtime(&mut self)

Threaded mode: drain outstanding returns, stop the present thread and join it. The renderer returns to the uninitialized state.

Source

pub fn last_published_frame_id(&self) -> u64

The producer’s monotone packet sequence: the frame_id stamped on the most recently lowered packet. After a Published outcome this is the published frame’s id (the Android loop keys its telemetry on it); it also proves a NoCredit publish never lowered a frame.

Source

pub fn last_frame_stats(&self) -> Option<RenderStatsSnapshot>

Source

pub fn gpu_pass_timings(&self) -> GpuPassTimingReport

GPU milliseconds by pass label, aggregated since the last [GPU-PASS] print. Empty unless CRANPOSE_GPU_PASS_TIMING armed pass timing on a device with wgpu::Features::TIMESTAMP_QUERY.

Source

pub fn debug_cpu_allocation_stats(&self) -> DebugCpuAllocationStats

Source

pub fn try_device(&self) -> Option<&Device>

Return the WGPU device when GPU resources are initialized. Sync backend only (desktop/web reconfigure paths); the threaded runtime owns its device on the present thread.

Trait Implementations§

Source§

impl Default for WgpuRenderer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Renderer for WgpuRenderer

Source§

type Scene = Scene

Source§

type Error = WgpuRendererError

Source§

fn attach_app_context_services(&mut self, app_context: &AppContext)

Installs renderer-provided app services into the target AppContext. Read more
Source§

fn scene(&self) -> &Self::Scene

Source§

fn scene_mut(&mut self) -> &mut Self::Scene

Source§

fn rebuild_scene( &mut self, layout_tree: &LayoutTree, _viewport: Size, ) -> Result<(), Self::Error>

Source§

fn rebuild_scene_from_applier( &mut self, applier: &mut MemoryApplier, root: NodeId, _viewport: Size, ) -> Result<(), Self::Error>

Rebuilds the scene by traversing the LayoutNode tree directly via Applier. Read more
Source§

fn update_scene_from_applier( &mut self, applier: &mut MemoryApplier, root: NodeId, viewport: Size, dirty_nodes: &[NodeId], ) -> Result<(), Self::Error>

Source§

fn update_visual_scene_from_applier( &mut self, applier: &mut MemoryApplier, root: NodeId, viewport: Size, dirty_nodes: &[NodeId], ) -> Result<(), Self::Error>

Source§

fn draw_dev_overlay(&mut self, text: &str, viewport: Size)

Draw a development overlay (e.g., FPS counter) on top of the scene. Read more
Source§

fn set_inspector_overlay(&mut self, graph: Option<RenderGraph>)

Replaces the developer inspector graph outside the application scene. Read more
Source§

fn needs_frame_warmup(&self) -> bool

Returns whether renderer-side cache materialization needs a visible follow-up frame.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>