Skip to main content

WgpuBackend

Struct WgpuBackend 

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

GPU-based rendering backend.

Creates a wgpu device and surface from a platform-provided Metal layer (macOS) or window handle. Implements RenderBackend by accumulating geometry per frame, then flushing it in present().

Implementations§

Source§

impl WgpuBackend

Source

pub fn from_surface( instance: &Instance, surface: Surface<'static>, logical_w: u32, logical_h: u32, scale: f32, ) -> Option<Self>

Create a GPU backend from a pre-created wgpu surface.

logical_w and logical_h are in logical points. scale is the display scale factor (2.0 on Retina). The surface is configured at logical × scale physical pixels.

§Panics

Panics if the embedded font fails to parse (a bug in the bundled font asset, never user input).

Source

pub unsafe fn from_window( window: &Window<'_>, logical_w: u32, logical_h: u32, scale: f32, ) -> Option<Self>

Create a GPU backend from a baseview window handle. baseview is the macOS / Windows / Linux windowing layer - iOS does not compile this constructor (the iOS editor builds its surface directly from a CAMetalLayer attached to a UIView).

§Safety

The window must remain valid for the lifetime of the backend.

Source

pub fn new( device: Arc<Device>, queue: Arc<Queue>, target_format: TextureFormat, max_logical_w: u32, max_logical_h: u32, scale: f32, ) -> Option<Self>

Build a standalone WgpuBackend that records into encoders supplied per-frame by the caller.

Unlike Self::from_surface / from_metal_layer / Self::from_window, this constructor does not own a wgpu::Surface or manage frame acquisition. The caller is expected to have its own render loop, allocate command encoders, and present - this backend only supplies the 2D widget pipeline, glyph atlas, and lyon-tessellated primitive recording.

Usage:

let mut backend = WgpuBackend::new(
    device.clone(), queue.clone(),
    target_format, max_w, max_h,
).expect("backend init");

// per-frame, after the caller has drawn its own content into `view`:
backend.begin_frame(w, h);
truce_gui::widgets::draw(&mut backend, &layout, &theme, &snap, &mut state);
backend.finish(&mut encoder, &view);
// caller submits encoder + presents.

max_logical_w / max_logical_h are in logical points; scale is the display scale factor (2.0 on Retina, 1.0 otherwise). The MSAA texture is seeded at logical × scale physical pixels; if a subsequent begin_frame(logical_w, logical_h) exceeds the seed, the MSAA texture is reallocated transparently.

Matches the coordinate contract of Self::from_surface / Self::from_window: draw calls and event coordinates are logical points; the backend multiplies by scale internally when rasterizing.

§Panics

Panics if the embedded font fails to parse (bundled-asset bug, never user input).

Source

pub fn begin_frame(&mut self, logical_w: u32, logical_h: u32)

Prepare for recording a frame of logical_w × logical_h logical points. The MSAA target and ortho matrix are sized at logical × self.scale() physical pixels; widget draw calls use logical coordinates.

Resets accumulated geometry and the clear flag. Rebuilds the MSAA texture if the physical size differs from the previous frame.

Only meaningful when the backend was built via Self::new; the surface-owning constructors drive their own frame lifecycle.

Source

pub fn scale(&self) -> f32

Display scale factor: logical × scale = physical. Callers sizing sibling GPU resources (e.g. an intermediate texture that the backend will resolve into) should use this to stay consistent with the backend’s raster dimensions.

Source

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

Update the display scale factor. The next Self::resize (or Self::begin_frame in headless mode) recomputes physical dimensions and reconfigures the surface / MSAA target. Callers driving a windowed surface should follow with a resize so the surface_config picks up the new size on the same frame; the short-circuit in resize doesn’t trigger because the scale change makes the new physical dims differ from the old.

Source

pub fn finish(&mut self, encoder: &mut CommandEncoder, view: &TextureView)

Flush accumulated geometry into a single render pass on view, recorded into encoder. The caller retains ownership of both - this method neither submits the encoder nor calls present().

If clear() was called since the last begin_frame, the pass uses LoadOp::Clear(clear_color); otherwise LoadOp::Load so any prior content in view is preserved (the common case when widgets overlay a custom render).

Source

pub fn resize(&mut self, logical_w: u32, logical_h: u32) -> bool

Resize the wgpu surface, MSAA texture, and viewport projection.

logical_w and logical_h are in logical points (same coordinate space as BuiltinEditor::size()). Returns true if the surface was actually reconfigured.

Source§

impl WgpuBackend

Source

pub fn headless(width: u32, height: u32, scale: f32) -> Option<Self>

Create a headless GPU backend (no window or surface). Used for snapshot testing.

§Panics

Panics if the embedded font fails to parse (bundled-asset bug, never user input).

Source

pub fn read_pixels(&mut self) -> Vec<u8>

Render to an offscreen texture and read back RGBA pixels. Only works for headless backends (no surface).

§Panics

Panics if wgpu::Buffer::map_async reports failure when reading back the GPU readback buffer - that indicates an adapter / driver fault rather than a recoverable runtime condition, so the snapshot path bubbles it up rather than papering over it.

Trait Implementations§

Source§

impl RenderBackend for WgpuBackend

All RenderBackend methods accept coordinates in logical points. The backend multiplies by self.scale to get physical pixel positions. Font glyphs are rasterized at physical resolution for sharp text.

Source§

fn clear(&mut self, color: Color)

Clear the entire surface with a solid color.
Source§

fn fill_rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color)

Fill a rectangle.
Source§

fn fill_circle(&mut self, cx: f32, cy: f32, radius: f32, color: Color)

Fill a circle.
Source§

fn stroke_circle( &mut self, cx: f32, cy: f32, radius: f32, color: Color, width: f32, )

Stroke a circle outline.
Source§

fn stroke_arc( &mut self, cx: f32, cy: f32, radius: f32, start_angle: f32, end_angle: f32, color: Color, width: f32, )

Stroke an arc (portion of a circle).
Source§

fn draw_line( &mut self, x1: f32, y1: f32, x2: f32, y2: f32, color: Color, width: f32, )

Draw a line between two points.
Source§

fn draw_text(&mut self, text: &str, x: f32, y: f32, size: f32, color: Color)

Draw text using the embedded TrueType font (fontdue).
Source§

fn text_width(&self, text: &str, size: f32) -> f32

Measure the width of a text string in logical points, at the given logical-point font size.
Source§

fn register_image(&mut self, rgba: &[u8], width: u32, height: u32) -> ImageId

Register an RGBA8 image (premultiplied alpha, row-major, tightly packed). Read more
Source§

fn unregister_image(&mut self, id: ImageId)

Remove a previously-registered image. No-op if the id is invalid or already unregistered.
Source§

fn draw_image(&mut self, id: ImageId, x: f32, y: f32, w: f32, h: f32)

Draw a previously-registered image at (x, y) sized w × h. Read more
Source§

fn present(&mut self)

Flush rendering to the display surface. Read more

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,