Skip to main content

WgpuRenderer

Struct WgpuRenderer 

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

wgpu backend that renders Scene commands into a presentation surface.

The renderer owns the GPU device, queue, surface, pipeline, and transient vertex buffer. Window creation stays outside the library so each application can choose its own windowing framework.

Implementations§

Source§

impl WgpuRenderer

Source

pub async fn recover_device_and_surface( &mut self, ) -> Result<(), RendererInitError>

Requests a replacement logical device for the existing surface and recreates all renderer-owned transient pipelines and buffers.

External prepared, dynamic, particle, scalar, target, and trail resources deliberately retain the previous renderer identity. Recreate them with the matching restore_* methods after this call succeeds. Targets and trails restore empty and must be redrawn.

Replaced logical devices enter a bounded quarantine until this renderer is dropped. Some native swapchain drivers crash if a healthy previous device is destroyed immediately after the surface migrates to its replacement. Device recovery should therefore remain exceptional rather than a normal quality switch. Query WgpuRenderer::remaining_device_recoveries before attempting it.

Source§

impl WgpuRenderer

Source

pub fn create_mesh3d( &self, source: Mesh3d, ) -> Result<RetainedMesh3d, Mesh3dResourceError>

Uploads validated immutable topology into retained GPU buffers.

Counts, byte arithmetic, draw-count representation, and active-device buffer limits are checked before conversion staging allocation. Host staging reservation failure is returned explicitly; GPU allocation itself follows wgpu’s device-error model.

Source

pub fn restore_mesh3d( &self, source: &RetainedMesh3d, ) -> Result<RetainedMesh3d, Mesh3dResourceError>

Restores retained topology onto this renderer after device replacement.

Source

pub fn restore_scene3d( &self, scene: &mut Scene3d, ) -> Result<Scene3dRestoreReport, Mesh3dResourceError>

Atomically restores every stale retained mesh referenced by a 3D scene.

Distinct shared mesh resources are uploaded once. Object IDs, insertion order, transforms, styles, visibility, scene provenance, and the next ID remain unchanged. If any capacity or host-staging allocation fails, the original scene is not modified. Targets remain separate resources and must be restored with WgpuRenderer::restore_render_target3d.

Source

pub fn create_render_target3d( &self, width: u32, height: u32, logical_viewport: LogicalViewport, ) -> Result<RenderTarget3d, Mesh3dResourceError>

Creates color/depth attachments for an explicit logical viewport.

width and height are physical target texels. Their aspect must match logical_viewport; the resulting texels-per-logical-pixel ratio controls wireframe width independently of the window DPI scale.

Source

pub fn restore_render_target3d( &self, source: &RenderTarget3d, ) -> Result<RenderTarget3d, Mesh3dResourceError>

Restores empty color/depth attachments with the source dimensions.

Source

pub fn render_scene3d_to_target( &mut self, target: &RenderTarget3d, scene: &Scene3d, camera: Camera3d, ) -> Result<Mesh3dRenderReport, Mesh3dRenderError>

Draws opaque retained objects into a reusable color/depth target.

Object insertion order does not determine visibility. Every surface writes and tests hardware depth. Model transforms are uploaded through a reusable instance buffer; retained topology is never retessellated.

Source§

impl WgpuRenderer

Source

pub fn render_layered_visualization( &mut self, target: &RenderTarget2d, scalar: &ScalarFieldTexture, color_map: &ColorMap, particles: &mut ParticleField2d, camera: &Camera2d, options: LayeredVisualizationOptions, ) -> Result<RenderReport, LayeredVisualizationError>

Draws a heatmap, overlays a budgeted particle field, and presents the target with one command encoder and one queue submission.

This fused path avoids synchronously submitting each layer and leaves substantially more CPU time for an expensive host simulation. The target should have the presentation surface’s aspect ratio; lowering both dimensions proportionally bounds raster cost without changing the logical camera contract.

Source§

impl WgpuRenderer

Source

pub async fn new( surface_target: impl Into<SurfaceTarget<'static>>, width: u32, height: u32, ) -> Result<Self, RendererInitError>

Creates a renderer for a window or canvas surface target.

width and height are physical surface pixels. Zero sizes are clamped to one pixel because wgpu surfaces cannot be configured at zero size. This convenience constructor assumes a display scale factor of 1.0; HiDPI hosts should use WgpuRenderer::new_with_options.

Source

pub async fn new_with_options( surface_target: impl Into<SurfaceTarget<'static>>, width: u32, height: u32, options: WgpuRendererOptions, ) -> Result<Self, RendererInitError>

Creates a renderer with explicit options.

Use this when measuring renderer throughput or when the host application needs control over presentation behavior or display scale.

Source

pub fn size(&self) -> (u32, u32)

Returns the configured surface size in physical pixels.

Source

pub fn logical_size(&self) -> (f32, f32)

Returns the render viewport size in logical screen pixels.

Scene clipping, stroke widths, shadows, and camera zoom use this logical coordinate space so their visual size remains stable across display DPI.

Source

pub fn logical_viewport(&self) -> Result<LogicalViewport, LogicalViewportError>

Returns the current camera viewport in logical screen pixels.

Source

pub fn physical_to_logical_screen( &self, position: PhysicalScreenPosition, ) -> Result<LogicalScreenPosition, RendererCoordinateError>

Converts a physical surface position into logical screen pixels.

Source

pub fn logical_to_physical_screen( &self, position: LogicalScreenPosition, ) -> Result<PhysicalScreenPosition, RendererCoordinateError>

Converts a logical screen position into physical surface pixels.

Source

pub fn scale_factor(&self) -> f64

Returns physical surface pixels per logical screen pixel.

Source

pub fn surface_present_mode(&self) -> RendererSurfacePresentMode

Returns the concrete presentation mode selected for the active surface.

RendererPresentMode::NoVsync prefers Immediate, then Mailbox, then FIFO. This method reveals which supported fallback was configured. The desktop compositor may still pace host redraw callbacks independently.

Source

pub fn quarantined_device_count(&self) -> usize

Returns previous logical devices retained for safe native-driver teardown.

Source

pub fn max_quarantined_device_count(&self) -> usize

Returns the configured maximum previous-device quarantine size.

Source

pub fn remaining_device_recoveries(&self) -> usize

Returns successful recoveries available before the bounded quarantine is full.

Source

pub fn wait_for_gpu_idle(&self) -> Result<(), PollError>

Blocks until all previously submitted GPU work has completed.

This is intended for diagnostic throughput measurements, readback, and controlled resource teardown. Calling it in an interactive frame loop defeats normal CPU/GPU pipelining.

Source

pub fn set_scale_factor( &mut self, scale_factor: f64, ) -> Result<(), RendererConfigurationError>

Replaces display scale without changing physical surface dimensions.

Invalid values return an error and leave the current scale unchanged.

Source

pub fn resize(&mut self, width: u32, height: u32)

Reconfigures the surface after a host window resize.

Zero width or height is ignored because minimized windows often report zero size and wgpu cannot configure a zero-sized surface.

Source

pub fn resize_with_scale_factor( &mut self, width: u32, height: u32, scale_factor: f64, ) -> Result<(), RendererConfigurationError>

Reconfigures physical surface size and logical-to-physical display scale.

Invalid scale returns an error without changing either value. Zero physical dimensions update the scale but leave the minimized surface configuration unchanged.

Source

pub fn render( &mut self, scene: &Scene, camera: &Camera2d, ) -> Result<RenderStatus, RendererFrameError>

Draws a scene using the supplied camera.

Scene positions and sizes are in world units unless a style explicitly says logical screen pixels. The renderer converts world coordinates to logical screen coordinates through Camera2d, then to normalized device coordinates for wgpu. Scissor rectangles are converted to physical surface pixels at the final backend boundary.

Source

pub fn render_with_metrics( &mut self, scene: &Scene, camera: &Camera2d, ) -> Result<RenderReport, RendererFrameError>

Draws a scene and returns a CPU-side timing breakdown.

Metrics separate tessellation, buffer upload, surface acquisition, and encode/submit/present dispatch. They do not measure GPU completion or the monitor scanout timestamp. Use this for diagnostics; WgpuRenderer::render is the simpler equivalent when stage timings are not needed.

Source

pub fn prepare_scene( &self, scene: &Scene, ) -> Result<PreparedScene, PreparedSceneError>

Tessellates a scene once and uploads immutable geometry to a dedicated GPU buffer.

Preparing is appropriate for geometry that will be drawn repeatedly while only the camera or target dimensions change. Any shape, style, gradient, ordering, or clipping change requires preparing a replacement scene.

Source

pub fn restore_prepared_scene( &self, source: &PreparedScene, ) -> Result<PreparedScene, PreparedSceneError>

Recreates prepared GPU resources on this renderer from a retained CPU snapshot.

This supports renderer recreation after device loss without requiring the original high-level Scene. The returned snapshot belongs to this renderer.

Source

pub fn restore_dynamic_mesh( &self, source: &DynamicMesh2d, ) -> Result<DynamicMesh2d, DynamicMeshError>

Recreates a dynamic mesh on this renderer from its retained CPU vertices.

Use this after recreating a renderer following device loss. The returned mesh belongs to this renderer and preserves the source mesh’s capacity so subsequent updates retain the same allocation behavior.

Source

pub fn create_dynamic_mesh( &self, vertices: &[DynamicVertex2d], ) -> Result<DynamicMesh2d, DynamicMeshError>

Creates mutable triangle-list geometry for a frequently changing visual.

The vertex count must be divisible by three. The mesh is owned by this renderer and may only be updated or drawn through it.

Source

pub fn update_dynamic_mesh( &self, mesh: &mut DynamicMesh2d, vertices: &[DynamicVertex2d], ) -> Result<(), DynamicMeshError>

Replaces all dynamic mesh vertices, growing GPU capacity only when needed.

Source

pub fn update_dynamic_mesh_with_metrics( &self, mesh: &mut DynamicMesh2d, vertices: &[DynamicVertex2d], ) -> Result<DynamicMeshUpdateReport, DynamicMeshError>

Replaces all dynamic mesh vertices and returns CPU-side update metrics.

Source

pub fn update_dynamic_mesh_range( &self, mesh: &mut DynamicMesh2d, first_vertex: usize, vertices: &[DynamicVertex2d], ) -> Result<(), DynamicMeshError>

Replaces a contiguous vertex range without reallocating the mesh buffer.

The range must stay inside the current triangle list and its length must preserve triangle alignment, so callers cannot create partial triangles.

Source

pub fn update_dynamic_mesh_range_with_metrics( &self, mesh: &mut DynamicMesh2d, first_vertex: usize, vertices: &[DynamicVertex2d], ) -> Result<DynamicMeshUpdateReport, DynamicMeshError>

Replaces a triangle-aligned range and returns CPU-side update metrics.

Source

pub fn render_dynamic_mesh( &mut self, mesh: &DynamicMesh2d, background: Color, camera: &Camera2d, ) -> Result<RenderStatus, DynamicMeshRenderError>

Draws dynamic triangle-list geometry with a normalized clear color.

Source

pub fn render_dynamic_mesh_with_metrics( &mut self, mesh: &DynamicMesh2d, background: Color, camera: &Camera2d, ) -> Result<RenderReport, DynamicMeshRenderError>

Draws dynamic triangle-list geometry and returns per-frame CPU metrics.

Source

pub fn create_particle_field( &self, instances: &[ParticleInstance2d], ) -> Result<ParticleField2d, ParticleFieldError>

Creates an instanced particle field owned by this renderer.

Each instance draws as one screen-space circle quad through a single GPU draw call. The retained CPU snapshot enables later restoration on a recreated renderer.

Source

pub fn create_particle_field_with_budget( &self, instances: &[ParticleInstance2d], budget: ParticleRenderBudget, ) -> Result<ParticleField2d, ParticleFieldError>

Creates an instanced particle field with hard GPU-memory and upload limits.

Source

pub fn set_particle_field_budget( &self, field: &mut ParticleField2d, budget: ParticleRenderBudget, ) -> Result<(), ParticleFieldError>

Replaces a particle field’s hard resource budget, shrinking memory when required.

Source

pub fn update_particle_field( &self, field: &mut ParticleField2d, instances: &[ParticleInstance2d], ) -> Result<ParticleStatistics, ParticleFieldError>

Replaces all particle instances, reusing GPU capacity whenever possible.

Source

pub fn update_particle_field_with_metrics( &self, field: &mut ParticleField2d, instances: &[ParticleInstance2d], ) -> Result<ParticleFieldUpdateReport, ParticleFieldError>

Replaces all particle instances and returns CPU-side preparation metrics.

Source

pub fn update_particle_field_range( &self, field: &mut ParticleField2d, first_instance: usize, instances: &[ParticleInstance2d], ) -> Result<ParticleStatistics, ParticleFieldError>

Replaces a contiguous particle range without reallocating its GPU buffer.

Source

pub fn update_particle_field_range_with_metrics( &self, field: &mut ParticleField2d, first_instance: usize, instances: &[ParticleInstance2d], ) -> Result<ParticleFieldUpdateReport, ParticleFieldError>

Replaces a particle range and returns CPU-side preparation metrics.

Source

pub fn restore_particle_field( &self, source: &ParticleField2d, ) -> Result<ParticleField2d, ParticleFieldError>

Recreates a particle field on this renderer from retained CPU instances.

Source

pub fn create_scalar_field_texture( &self, field: ScalarField, ) -> Result<ScalarFieldTexture, ScalarFieldTextureError>

Creates a single-channel floating-point texture from a validated scalar grid.

Source

pub fn create_render_target( &self, width: u32, height: u32, ) -> Result<RenderTarget2d, RenderTargetError>

Creates an offscreen target with physical texture dimensions.

Source

pub fn create_trail_buffer( &mut self, width: u32, height: u32, ) -> Result<TrailBuffer2d, RenderTargetError>

Creates an empty ping-pong target pair for temporal accumulation.

Source

pub fn restore_render_target( &self, source: &RenderTarget2d, ) -> Result<RenderTarget2d, RenderTargetError>

Recreates an empty target with the source dimensions after device recovery.

GPU pixels are deliberately not retained; callers redraw their source state into the restored target.

Source

pub fn restore_trail_buffer( &mut self, source: &TrailBuffer2d, ) -> Result<TrailBuffer2d, RenderTargetError>

Recreates an empty, deterministically cleared trail buffer after recovery.

Source

pub fn accumulate_trail_buffer( &mut self, trails: &mut TrailBuffer2d, source: &RenderTarget2d, retention: f32, source_opacity: f32, source_blend: BlendMode, ) -> Result<RenderReport, RenderTargetError>

Retains one bounded frame of history and composites a fresh source target.

retention and source_opacity must be finite values in 0.0..=1.0. The source must be a distinct target; use WgpuRenderer::clear_trail_buffer to discard accumulated history deterministically.

Source

pub fn clear_trail_buffer( &mut self, trails: &mut TrailBuffer2d, color: Color, ) -> Result<RenderReport, RenderTargetError>

Clears both ping-pong targets, resetting all retained temporal history.

Source

pub fn compose_trail_buffer( &mut self, trails: &TrailBuffer2d, blend_mode: BlendMode, opacity: f32, background: Color, ) -> Result<RenderReport, RenderTargetError>

Presents the most recently accumulated temporal target.

Source

pub fn update_scalar_field_texture( &self, texture: &mut ScalarFieldTexture, field: ScalarField, ) -> Result<ScalarFieldUploadReport, ScalarFieldTextureError>

Replaces scalar data, recreating the texture only when dimensions change.

Source

pub fn update_scalar_field_texture_region( &self, texture: &mut ScalarFieldTexture, x: usize, y: usize, width: usize, height: usize, values: &[f32], ) -> Result<ScalarFieldUploadReport, ScalarFieldTextureError>

Updates a rectangular scalar texture region without recreating it.

Source

pub fn restore_scalar_field_texture( &self, source: &ScalarFieldTexture, ) -> Result<ScalarFieldTexture, ScalarFieldTextureError>

Recreates a scalar texture on this renderer from its retained CPU grid.

Source

pub fn render_scalar_field_texture( &mut self, texture: &ScalarFieldTexture, color_map: &ColorMap, value_range: (f32, f32), background: Color, ) -> Result<RenderStatus, ScalarFieldRenderError>

Draws a scalar texture across the logical viewport through a color map.

value_range maps minimum to the first color-map entry and maximum to the last. Values outside the range clamp to those endpoints.

Source

pub fn render_scalar_field_texture_with_metrics( &mut self, texture: &ScalarFieldTexture, color_map: &ColorMap, (minimum, maximum): (f32, f32), background: Color, ) -> Result<RenderReport, ScalarFieldRenderError>

Draws a scalar texture and returns normal CPU-side renderer metrics.

Source

pub fn render_scalar_field_texture_with_sampling( &mut self, texture: &ScalarFieldTexture, color_map: &ColorMap, (minimum, maximum): (f32, f32), sampling: ScalarFieldSampling, background: Color, ) -> Result<RenderStatus, ScalarFieldRenderError>

Draws a scalar texture with an explicit texel sampling mode.

Source

pub fn render_scalar_field_texture_to_target( &mut self, target: &RenderTarget2d, texture: &ScalarFieldTexture, color_map: &ColorMap, value_range: (f32, f32), background: Color, ) -> Result<RenderReport, ScalarFieldRenderError>

Renders a scalar heatmap into an offscreen target instead of presenting it.

Source

pub fn render_scalar_field_texture_to_target_with_sampling( &mut self, target: &RenderTarget2d, texture: &ScalarFieldTexture, color_map: &ColorMap, (minimum, maximum): (f32, f32), sampling: ScalarFieldSampling, background: Color, ) -> Result<RenderReport, ScalarFieldRenderError>

Renders a scalar heatmap into a target with explicit source sampling.

Source

pub fn compose_render_target( &mut self, target: &RenderTarget2d, blend_mode: BlendMode, opacity: f32, background: Color, ) -> Result<RenderReport, RenderTargetError>

Composes a previously rendered target over the presentation surface.

Source

pub fn render_particle_field( &mut self, field: &mut ParticleField2d, background: Color, camera: &Camera2d, ) -> Result<RenderStatus, ParticleFieldRenderError>

Draws a particle field with a normalized clear color.

Source

pub fn render_particle_field_with_metrics( &mut self, field: &mut ParticleField2d, background: Color, camera: &Camera2d, ) -> Result<RenderReport, ParticleFieldRenderError>

Draws a particle field and returns regular per-frame renderer metrics.

Source

pub fn render_particle_field_to_target( &mut self, target: &RenderTarget2d, field: &mut ParticleField2d, camera: &Camera2d, load: RenderTargetLoad, ) -> Result<RenderReport, ParticleFieldRenderError>

Draws a particle field into an offscreen target.

Target dimensions may be lower than the presentation surface to bound raster cost. Camera coordinates and particle radii remain expressed in logical presentation pixels, so composing the target back to the surface preserves layout while trading only raster resolution.

Source

pub fn render_prepared( &mut self, scene: &PreparedScene, camera: &Camera2d, ) -> Result<RenderStatus, PreparedSceneRenderError>

Draws geometry previously uploaded by WgpuRenderer::prepare_scene.

Camera and viewport changes are applied by the vertex shader and do not rebuild or re-upload the prepared geometry.

Source

pub fn render_prepared_with_metrics( &mut self, scene: &PreparedScene, camera: &Camera2d, ) -> Result<RenderReport, PreparedSceneRenderError>

Draws prepared geometry and reports per-frame CPU timing.

Tessellation and geometry upload durations are zero because both happened in WgpuRenderer::prepare_scene. The camera uniform is still updated once per 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, 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, <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,