Skip to main content

GpuRenderer

Struct GpuRenderer 

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

A renderer that draws every frame on a GPU through vello.

The renderer owns one texture of its configured size and repaints the whole texture on every Renderer::render call. Damage is ignored: the GPU path redraws the full frame, so FrameOutput::damage and FrameOutput::full carry no meaning for it. FrameOutput::clear_color becomes the color the frame is cleared with, and FrameOutput::scale scales the logical display list coordinates into physical pixels.

To draw onto a window instead, use GpuSurface, which presents through a wgpu surface.

§Panics

Panics when the device cannot create or run the vello pipelines, which means the device cannot run the shaders vello needs. That is a broken device state rather than input a caller can provoke.

§Examples

use smudge_core::app::FrameOutput;
use smudge_core::geometry::{Color, Corners, Rect};
use smudge_core::paint::{DisplayList, PaintOp};
use smudge_core::renderer::Renderer;
use smudge_gpu::GpuRenderer;

// On a machine without a GPU adapter this draws nothing.
let Some(mut renderer) = GpuRenderer::headless(64, 64) else {
    return;
};
let mut list = DisplayList::new();
list.push_op(PaintOp::FillRect {
    rect: Rect::new(0.0, 0.0, 64.0, 64.0),
    radius: Corners::ZERO,
    color: Color::BLACK,
});
list.finish();
renderer.render(&FrameOutput {
    list: &list,
    damage: &[],
    full: true,
    clear_color: Color::WHITE,
    scale: 1.0,
});
let pixels = renderer.read_pixels();
assert_eq!(pixels.len(), 64 * 64 * 4);

Implementations§

Source§

impl GpuRenderer

Source

pub fn new(device: Device, queue: Queue, width: u32, height: u32) -> GpuRenderer

Creates a renderer for a device, drawing onto a texture of width by height physical pixels.

A surface with a zero extent holds no texture, and GpuRenderer::render and GpuRenderer::read_pixels then do nothing.

§Panics

Panics when width or height exceeds the device’s max_texture_dimension_2d limit, which a caller can read from Device::limits beforehand, and under the conditions of the # Panics section.

Source

pub fn headless(width: u32, height: u32) -> Option<GpuRenderer>

Creates a renderer on a GPU adapter chosen without a window, so tests and offscreen uses need no surface.

The adapter comes from an instance whose options, among them the WGPU_BACKEND variable, are read from the environment, exactly as the window path does. That lets a machine pin the backend without a rebuild, for example to measure a software rasterizer.

Returns None when no adapter can be created, or when the chosen adapter cannot provide a device, which is how a machine without a GPU reports itself.

§Panics

Panics under the conditions of GpuRenderer::new. The device has the default limits, which allow 8192 pixels on each side.

Source

pub fn adapter_summary(&self) -> AdapterSummary

Returns the adapter the renderer draws on, as plain strings.

The renderer keeps no adapter of its own: wgpu reports the adapter its device was created from, and that is the adapter every render runs on.

Source

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

Returns the rendered frame as straight-alpha RGBA8 pixels, row-major with no padding between rows, which is the layout vello writes.

The result is empty when the surface has no pixel.

§Panics

Panics when the device is lost, or when the readback buffer cannot be mapped after the copy completed. Both are broken device states rather than input a caller can provoke.

Source

pub fn wait(&self)

Blocks until the GPU has executed every render submitted so far.

A render returns once its work is submitted, so a caller that times a frame calls this to count the GPU’s execution and not only the submission.

§Panics

Panics when the device is lost, which is a broken device state rather than input a caller can provoke.

Trait Implementations§

Source§

impl Renderer for GpuRenderer

Source§

fn render(&mut self, frame: &FrameOutput<'_>)

Repaints frame onto the renderer’s surface.

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>

Source§

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