Skip to main content

WgpuRenderer

Struct WgpuRenderer 

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

The wgpu renderer.

Implementations§

Source§

impl WgpuRenderer

Source

pub fn offscreen(ctx: &GpuContext) -> Self

A renderer that draws into its own texture; read it back with read_pixels. The texture is sized by each frame.

Source

pub fn with_surface( ctx: &GpuContext, surface: Surface<'static>, size: SizePx, ) -> Result<Self, RenderError>

A renderer that presents to surface (created from ctx.instance).

§Errors

RenderError::Backend if the surface is not supported by the adapter, RenderError::InvalidSize for a zero or too large size.

Source

pub fn take_gpu_error(&mut self) -> Option<String>

The first GPU error (validation, out of memory) reported since the last call, with wgpu’s message. A frame during which such an error occurred makes Renderer::render return RenderError::Backend.

The renderer installs the device’s uncaptured-error handler; with several renderers on one device, the last one created receives them.

Source

pub fn register_series(&mut self, series: Box<dyn GpuSeries>)

Registers the native implementation of a custom series kind, replacing an earlier one of the same kind. The kind is then in Renderer::native_kinds; pass those to Chart::set_native_kinds (the host helpers do it before every frame). Do not register an implementation that needs compute shaders on WebGL2.

Source

pub fn measurer(&self) -> EngineMeasurer<'_>

The text measurer to pass to Chart::build_frame.

Source

pub fn text_engine_mut(&mut self) -> &mut TextEngine

The text engine (to add fonts).

Source

pub fn series_chunks(&self) -> usize

Bytes of GPU vertex buffers held for series data.

Source

pub fn offscreen_texture(&self) -> Option<&Texture>

The texture an offscreen renderer drew the last frame into, for hosts that compose the chart into their own rendering (a UI toolkit’s paint callback, a game engine). None for a renderer created with with_surface and before the first frame.

The format is Rgba8Unorm holding sRGB-encoded colors (the CPU backend’s pixels): sample it as is and write the values to a non-sRGB target, or convert them to linear before writing to an sRGB one. The usages are RENDER_ATTACHMENT | COPY_SRC | TEXTURE_BINDING. The texture is replaced when the frame size changes, so look it up after each Renderer::render rather than keeping it.

use ferrochart_core::geom::SizeCss;
use ferrochart_core::render::Renderer;
use ferrochart_core::{Chart, ChartOptions};
use ferrochart_render_wgpu::{GpuContext, WgpuRenderer};

let ctx = GpuContext::headless()?;
let mut renderer = WgpuRenderer::offscreen(&ctx);
let mut chart = Chart::new(ChartOptions::default());
chart.resize(SizeCss { w: 800.0, h: 450.0 }, 2.0);
chart.build_frame(0.0, &renderer.measurer());
renderer.render(chart.frame(), &chart.data_view())?;
let texture = renderer.offscreen_texture().ok_or("no frame yet")?;
assert_eq!((texture.width(), texture.height()), (1600, 900));
// Bind `renderer.offscreen_view()` in the host's own pipeline.
Source

pub fn offscreen_view(&self) -> Option<&TextureView>

A view of the whole offscreen_texture (same lifetime and conditions), ready to bind for sampling.

Source

pub fn read_pixels(&self) -> Result<Vec<u8>, RenderError>

Reads the offscreen target back as tightly packed RGBA8 rows.

§Errors

RenderError::InvalidSize if this is not an offscreen renderer or nothing was rendered, RenderError::Backend if mapping fails.

Trait Implementations§

Source§

impl Debug for WgpuRenderer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Renderer for WgpuRenderer

Source§

fn render( &mut self, frame: &Frame, data: &ChartDataView<'_>, ) -> Result<(), RenderError>

Uploads whatever series data changed since this renderer last synced (it keeps its own synced generation per series and asks data.changes_since), then draws frame. On error the synced generations must not advance, so the next call catches up. Read more
Source§

fn resize(&mut self, size: SizePx)

Resizes the render target.
Source§

fn native_kinds(&self) -> &[&'static str]

Custom series kinds this renderer draws natively; pass them to crate::Chart::set_native_kinds. None by default.

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, !>

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>