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
impl GpuRenderer
Sourcepub fn new(device: Device, queue: Queue, width: u32, height: u32) -> GpuRenderer
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.
Sourcepub fn headless(width: u32, height: u32) -> Option<GpuRenderer>
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.
Sourcepub fn adapter_summary(&self) -> AdapterSummary
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.
Sourcepub fn read_pixels(&self) -> Vec<u8> ⓘ
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.
Sourcepub fn wait(&self)
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
impl Renderer for GpuRenderer
Source§fn render(&mut self, frame: &FrameOutput<'_>)
fn render(&mut self, frame: &FrameOutput<'_>)
frame onto the renderer’s surface.