pub struct WgpuGfxCtx { /* private fields */ }Expand description
A DrawCtx that renders via wgpu
(Vulkan / DX12 / Metal / WebGL2).
Create with WgpuGfxCtx::new, passing a wgpu::Device and wgpu::Queue
that were obtained by the platform shell. Each frame: call
render_app_frame (which calls reset and
app.paint(ctx)), then call end_frame with the
current surface texture view to flush all deferred draw commands.
Implementations§
Source§impl WgpuGfxCtx
impl WgpuGfxCtx
Sourcepub fn set_surface_texture(&mut self, tex: Texture)
pub fn set_surface_texture(&mut self, tex: Texture)
Stash a handle to the current frame’s surface texture so a later
Self::read_screenshot call can copy from it. Called from the
platform shell with frame.texture.clone() BEFORE begin_frame.
wgpu::Texture is internally ref-counted, so the clone is cheap.
Sourcepub fn set_pending_screenshot(&mut self, captured: (Vec<u8>, u32, u32))
pub fn set_pending_screenshot(&mut self, captured: (Vec<u8>, u32, u32))
Stash captured screenshot pixels for the read-back closure to pick
up. See Self::take_pending_screenshot.
Sourcepub fn take_pending_screenshot(&mut self) -> (Vec<u8>, u32, u32)
pub fn take_pending_screenshot(&mut self) -> (Vec<u8>, u32, u32)
Consume the pending screenshot pixels — returns (Vec::new(), 0, 0)
when none are stashed (typical non-capture frames). Called by the
agg_gui::screenshot::run_frame_with_capture read-back closure.
Sourcepub fn read_screenshot(&self) -> (Vec<u8>, u32, u32)
pub fn read_screenshot(&self) -> (Vec<u8>, u32, u32)
Read the current frame’s rendered pixels back to CPU memory as a
top-down RGBA8 buffer. Returns (pixels, width, height).
The first width * 4 bytes are the TOP row (Y-down image order).
Must be called AFTER Self::end_frame has submitted the render and
BEFORE the platform shell calls frame.present(). Requires the
platform shell to have called Self::set_surface_texture earlier
in the frame so we hold a handle into the surface that’s still
valid post-render.
Returns an empty buffer if no surface texture is currently stashed.
Source§impl WgpuGfxCtx
impl WgpuGfxCtx
Sourcepub fn has_pending_readback(&self) -> bool
pub fn has_pending_readback(&self) -> bool
Whether a capture readback is already in flight.
Sourcepub fn begin_capture_readback(&mut self) -> bool
pub fn begin_capture_readback(&mut self) -> bool
Kick off an async readback of the current capture texture. Returns false
if nothing is captured or a readback is already in flight. Pure GPU
submit + map_async; never blocks.
Source§impl WgpuGfxCtx
impl WgpuGfxCtx
Sourcepub fn has_pending_scaled_readback(&self) -> bool
pub fn has_pending_scaled_readback(&self) -> bool
Whether a scaled capture readback is already in flight. Independent
of Self::has_pending_readback, which tracks the full-surface one.
Sourcepub fn begin_capture_readback_scaled(
&mut self,
src_region: Option<RectInPixels>,
dst_w: u32,
dst_h: u32,
) -> bool
pub fn begin_capture_readback_scaled( &mut self, src_region: Option<RectInPixels>, dst_w: u32, dst_h: u32, ) -> bool
Kick off an async readback of a scaled (and optionally cropped) copy of the current capture texture.
src_region is in framebuffer pixels (top-down, see RectInPixels)
and is clamped to the surface; None means the whole surface. The
result is resampled to dst_w x dst_h with linear filtering, so the
caller controls the readback cost directly: only
dst_w * dst_h * 4 bytes ever cross back to the CPU.
Returns false — queueing nothing — when there is nothing to capture:
no capture texture (call DrawCtx::capture_screenshot first), a
zero-sized output, an empty or entirely off-surface region, or a scaled
readback already in flight. Same “nothing captured” signal as
Self::begin_capture_readback.
Pure GPU submit + map_async; never blocks. Harvest with
Self::poll_capture_readback_scaled.
Sourcepub fn poll_capture_readback_scaled(&mut self) -> Option<(Vec<u8>, u32, u32)>
pub fn poll_capture_readback_scaled(&mut self) -> Option<(Vec<u8>, u32, u32)>
Harvest a completed scaled readback’s pixels as tight top-row-first
RGBA8, or None if still pending / none in flight. Never blocks.
Unlike Self::poll_capture_readback there is no per-pixel swizzle
here — the blit already produced RGBA in the surface’s own byte
encoding — so the only CPU work is stripping the row padding.
Source§impl WgpuGfxCtx
impl WgpuGfxCtx
Sourcepub fn new(
device: Arc<Device>,
queue: Arc<Queue>,
surface_format: TextureFormat,
width: f32,
height: f32,
) -> Self
pub fn new( device: Arc<Device>, queue: Arc<Queue>, surface_format: TextureFormat, width: f32, height: f32, ) -> Self
Create a new WgpuGfxCtx.
device and queue must come from a wgpu::Adapter whose surface
is already configured with surface_format. The caller retains
ownership of the surface; this struct only receives Arc refs to
the device and queue so both it and the platform shell can drive
buffer writes and texture uploads on the same queue.
Sourcepub fn last_end_frame_stats(&self) -> LastEndFrameStats
pub fn last_end_frame_stats(&self) -> LastEndFrameStats
Wall-clock breakdown of the most recent end_frame flush. Returns
zeroes until the first frame has been flushed. Designed for live perf
HUDs in platform shells — see atomartist’s record_frame_timings for
an example consumer.
Sourcepub fn reset(&mut self, width: f32, height: f32)
pub fn reset(&mut self, width: f32, height: f32)
Reset drawing state for a new frame. Preserves GPU resources.
Sourcepub fn set_lcd_mode(&mut self, on: bool)
pub fn set_lcd_mode(&mut self, on: bool)
Enable / disable LCD subpixel text for this context. Called each frame
from render_app_frame with font_settings::lcd_enabled().
Sourcepub fn begin_frame(&mut self, view: TextureView)
pub fn begin_frame(&mut self, view: TextureView)
Start a frame: take ownership of this frame’s render target and queue the clear.
view is the wgpu::TextureView for the surface texture (or any other
target) being painted this frame. It is stashed on the context so that
mid-frame GPU work — custom render passes, layer compositing — targets
the same attachment as the deferred 2-D pipeline without the caller
plumbing it through every method; Self::end_frame consumes it.
The clear itself happens inside end_frame, when the leading
DrawCommand::Clear is flushed into the first render pass; this
method only pushes the current theme’s background colour onto the
command list so the frame starts from a clean framebuffer.
Sourcepub fn end_frame(&mut self)
pub fn end_frame(&mut self)
Flush all deferred draw commands into a single wgpu command submission.
Must be called after the frame’s painting and before
surface.present(). The render target was stashed by
Self::begin_frame — the platform shell does not need to pass it
again here.
Sourcepub fn pipelines(&self) -> &WgpuPipelines
pub fn pipelines(&self) -> &WgpuPipelines
Borrow the shared 2-D pipeline collection. Exposed so platform
shells (currently demo-wasm) can drive a SsaaFramebuffer::blit_to
when they need to composite an intermediate scene texture onto the
real swap-chain surface — see the comment on
SsaaFramebuffer::resolve_texture for the WebGL2 scene-buffer
pattern. The returned &WgpuPipelines only exposes the fields
pub(crate) library code already uses; external callers can pass
it back into other library APIs but cannot reach in directly.
Sourcepub fn device(&self) -> &Arc<Device> ⓘ
pub fn device(&self) -> &Arc<Device> ⓘ
Borrow a clone-able handle to the wgpu device used for resource
allocation. Exposed alongside Self::pipelines so platform
shells driving an external blit pass (currently demo-wasm’s
scene-buffer → surface composite) don’t need to hold a duplicate
Arc<wgpu::Device> themselves.
Sourcepub fn queue(&self) -> &Arc<Queue> ⓘ
pub fn queue(&self) -> &Arc<Queue> ⓘ
Borrow a clone-able handle to the wgpu queue. Same rationale as
Self::device — the shell submits the scene-blit encoder
through this queue.
Sourcepub fn surface_format(&self) -> TextureFormat
pub fn surface_format(&self) -> TextureFormat
Format of the render target this context was configured for. Custom
renderers need it to build pipelines whose ColorTargetState matches
the surface they will be composited onto.
Sourcepub fn push_custom_render(
&mut self,
renderer: SharedCustomRenderer,
screen_rect: Rect,
)
pub fn push_custom_render( &mut self, renderer: SharedCustomRenderer, screen_rect: Rect, )
Queue a custom wgpu render pass to run at the current point in the
frame’s draw order. The user-supplied WgpuCustomRender is invoked
from end_frame after the active 2-D pass closes; subsequent 2-D
content reopens with LoadOp::Load so it composites on top.
screen_rect is the widget’s logical-pixel rect in agg-gui Y-up
coords. The implementor receives it via
WgpuCustomRenderCtx::screen_rect and is responsible for any
scissor / viewport conversions to wgpu’s Y-down convention.
Trait Implementations§
Source§impl DrawCtx for WgpuGfxCtx
impl DrawCtx for WgpuGfxCtx
Source§fn as_any_mut(&mut self) -> Option<&mut dyn Any>
fn as_any_mut(&mut self) -> Option<&mut dyn Any>
fn set_fill_color(&mut self, c: Color)
fn set_fill_linear_gradient(&mut self, gradient: LinearGradientPaint)
fn supports_fill_linear_gradient(&self) -> bool
fn set_fill_radial_gradient(&mut self, gradient: RadialGradientPaint)
fn supports_fill_radial_gradient(&self) -> bool
fn set_stroke_color(&mut self, c: Color)
fn set_stroke_linear_gradient(&mut self, gradient: LinearGradientPaint)
fn supports_stroke_linear_gradient(&self) -> bool
fn set_stroke_radial_gradient(&mut self, gradient: RadialGradientPaint)
fn supports_stroke_radial_gradient(&self) -> bool
fn set_line_width(&mut self, w: f64)
fn set_line_join(&mut self, j: LineJoin)
fn set_line_cap(&mut self, c: LineCap)
fn set_miter_limit(&mut self, limit: f64)
fn set_line_dash(&mut self, dashes: &[f64], offset: f64)
fn set_blend_mode(&mut self, _mode: CompOp)
fn set_global_alpha(&mut self, a: f64)
fn set_fill_rule(&mut self, rule: FillRule)
fn set_font(&mut self, font: Arc<Font>)
fn set_font_size(&mut self, size: f64)
fn clip_rect(&mut self, x: f64, y: f64, w: f64, h: f64)
fn reset_clip(&mut self)
Source§fn clear(&mut self, color: Color)
fn clear(&mut self, color: Color)
color, ignoring the current clip.fn begin_path(&mut self)
fn move_to(&mut self, x: f64, y: f64)
fn line_to(&mut self, x: f64, y: f64)
fn cubic_to(&mut self, cx1: f64, cy1: f64, cx2: f64, cy2: f64, x: f64, y: f64)
fn quad_to(&mut self, cx: f64, cy: f64, x: f64, y: f64)
fn arc_to( &mut self, cx: f64, cy: f64, r: f64, start_angle: f64, end_angle: f64, ccw: bool, )
Source§fn rect(&mut self, x: f64, y: f64, w: f64, h: f64)
fn rect(&mut self, x: f64, y: f64, w: f64, h: f64)
Source§fn rounded_rect(&mut self, x: f64, y: f64, w: f64, h: f64, r: f64)
fn rounded_rect(&mut self, x: f64, y: f64, w: f64, h: f64, r: f64)
fn close_path(&mut self)
fn fill(&mut self)
fn stroke(&mut self)
fn fill_and_stroke(&mut self)
Source§fn draw_triangles_aa(
&mut self,
vertices: &[[f32; 3]],
indices: &[u32],
color: Color,
)
fn draw_triangles_aa( &mut self, vertices: &[[f32; 3]], indices: &[u32], color: Color, )
x, y, alpha) and triangle indices. Read moreSource§fn fill_text(&mut self, text: &str, x: f64, y: f64)
fn fill_text(&mut self, text: &str, x: f64, y: f64)
text with the bottom of the baseline at (x, y).Source§fn fill_text_gsv(&mut self, _text: &str, _x: f64, _y: f64, _size: f64)
fn fill_text_gsv(&mut self, _text: &str, _x: f64, _y: f64, _size: f64)
Source§fn measure_text(&self, text: &str) -> Option<TextMetrics>
fn measure_text(&self, text: &str) -> Option<TextMetrics>
text with the current font and font-size settings.Source§fn has_image_blit(&self) -> bool
fn has_image_blit(&self) -> bool
true if this context implements draw_image_rgba with actual
pixel blitting. Label (and any other widget that uses a software
backbuffer) gates its cache path on this method so it can fall back to
direct fill_text() on render targets that don’t support blitting
(e.g. the GL path). Read moreSource§fn draw_image_rgba(
&mut self,
data: &[u8],
img_w: u32,
img_h: u32,
dst_x: f64,
dst_y: f64,
dst_w: f64,
dst_h: f64,
)
fn draw_image_rgba( &mut self, data: &[u8], img_w: u32, img_h: u32, dst_x: f64, dst_y: f64, dst_w: f64, dst_h: f64, )
dst_rect (Y-up local coordinates). Read moreSource§fn draw_image_rgba_arc(
&mut self,
data: &Arc<Vec<u8>>,
img_w: u32,
img_h: u32,
dst_x: f64,
dst_y: f64,
dst_w: f64,
dst_h: f64,
)
fn draw_image_rgba_arc( &mut self, data: &Arc<Vec<u8>>, img_w: u32, img_h: u32, dst_x: f64, dst_y: f64, dst_w: f64, dst_h: f64, )
draw_image_rgba] but accepts an Arc<Vec<u8>> so the GL
backend can key its texture cache on the Arc’s pointer identity and
hold a Weak ref for automatic cleanup when the underlying buffer is
dropped — the pattern MatterCAD implements with C# ConditionalWeakTable. Read moreSource§fn draw_image_rgba_corners(
&mut self,
data: &Arc<Vec<u8>>,
img_w: u32,
img_h: u32,
corners: [(f64, f64); 4],
)
fn draw_image_rgba_corners( &mut self, data: &Arc<Vec<u8>>, img_w: u32, img_h: u32, corners: [(f64, f64); 4], )
data as a textured quad whose four destination corners
are supplied explicitly. Caller is responsible for choosing the
corners (typically the projection of a 3-D rotated card onto
the 2-D viewport). corners is ordered bottom-left,
bottom-right, top-right, top-left in agg-gui’s Y-up local
coordinate space, and is fed through the current CTM the same
way axis-aligned blits are. Read moreSource§fn has_lcd_mask_composite(&self) -> bool
fn has_lcd_mask_composite(&self) -> bool
true if this backend supports [draw_lcd_mask] — i.e.
it can composite per-channel LCD coverage onto the active target.
Label queries this to decide between the LCD and grayscale AA
paths; a backend that returns false will never see LCD text.Source§fn draw_lcd_mask(
&mut self,
mask: &[u8],
mask_w: u32,
mask_h: u32,
src_color: Color,
dst_x: f64,
dst_y: f64,
)
fn draw_lcd_mask( &mut self, mask: &[u8], mask_w: u32, mask_h: u32, src_color: Color, dst_x: f64, dst_y: f64, )
src_color into the destination through
per-channel coverage. Read moreSource§fn draw_lcd_mask_arc(
&mut self,
mask: &Arc<Vec<u8>>,
mask_w: u32,
mask_h: u32,
src_color: Color,
dst_x: f64,
dst_y: f64,
)
fn draw_lcd_mask_arc( &mut self, mask: &Arc<Vec<u8>>, mask_w: u32, mask_h: u32, src_color: Color, dst_x: f64, dst_y: f64, )
Arc’s pointer identity — one glTexImage2D per unique
raster, lifetime tied to the mask’s strong-ref count. Software
backends fall through to the slice path.Source§fn draw_lcd_backbuffer_arc(
&mut self,
color: &Arc<Vec<u8>>,
alpha: &Arc<Vec<u8>>,
content_version: u64,
w: u32,
h: u32,
dst_x: f64,
dst_y: f64,
dst_w: f64,
dst_h: f64,
)
fn draw_lcd_backbuffer_arc( &mut self, color: &Arc<Vec<u8>>, alpha: &Arc<Vec<u8>>, content_version: u64, w: u32, h: u32, dst_x: f64, dst_y: f64, dst_w: f64, dst_h: f64, )
LcdCoverage-mode backbuffer onto the active
render target at (dst_x, dst_y) with size (dst_w, dst_h) (in
local coords). Inputs are two Arc<Vec<u8>>, each 3 bytes per
pixel, top-row-first: Read moreSource§fn transform(&self) -> TransAffine
fn transform(&self) -> TransAffine
Source§fn root_transform(&self) -> TransAffine
fn root_transform(&self) -> TransAffine
fn save(&mut self)
fn restore(&mut self)
fn translate(&mut self, tx: f64, ty: f64)
fn rotate(&mut self, radians: f64)
fn scale(&mut self, sx: f64, sy: f64)
fn set_transform(&mut self, m: TransAffine)
fn reset_transform(&mut self)
Source§fn supports_compositing_layers(&self) -> bool
fn supports_compositing_layers(&self) -> bool
Source§fn supports_retained_layers(&self) -> bool
fn supports_retained_layers(&self) -> bool
Source§fn push_layer(&mut self, width: f64, height: f64)
fn push_layer(&mut self, width: f64, height: f64)
Source§fn push_layer_with_alpha(&mut self, width: f64, height: f64, alpha: f64)
fn push_layer_with_alpha(&mut self, width: f64, height: f64, alpha: f64)
alpha when composited back into the parent target. Read moreSource§fn pop_layer(&mut self)
fn pop_layer(&mut self)
Source§fn set_layer_rounded_clip(&mut self, x: f64, y: f64, w: f64, h: f64, r: f64)
fn set_layer_rounded_clip(&mut self, x: f64, y: f64, w: f64, h: f64, r: f64)
Source§fn set_layer_opaque_backdrop(&mut self, opaque: bool)
fn set_layer_opaque_backdrop(&mut self, opaque: bool)
Source§fn composite_retained_layer(
&mut self,
key: u64,
width: f64,
height: f64,
alpha: f64,
) -> bool
fn composite_retained_layer( &mut self, key: u64, width: f64, height: f64, alpha: f64, ) -> bool
true when
the backend had a retained surface for key and drew it.Source§fn push_retained_layer_with_alpha(
&mut self,
key: u64,
width: f64,
height: f64,
alpha: f64,
)
fn push_retained_layer_with_alpha( &mut self, key: u64, width: f64, height: f64, alpha: f64, )
key.
Backends that do not retain layers may fall back to a transient layer.Source§fn gl_paint(&mut self, _screen_rect: Rect, _painter: &mut dyn GlPaint)
fn gl_paint(&mut self, _screen_rect: Rect, _painter: &mut dyn GlPaint)
Source§fn capture_screenshot(&mut self) -> bool
fn capture_screenshot(&mut self) -> bool
end_frame has flushed the
2-D render but before the platform shell calls present. Read moreSource§fn has_captured_screenshot(&self) -> bool
fn has_captured_screenshot(&self) -> bool
Self::draw_captured_screenshot.Source§fn captured_screenshot_size(&self) -> Option<(u32, u32)>
fn captured_screenshot_size(&self) -> Option<(u32, u32)>
None when no capture exists.Source§fn draw_captured_screenshot(
&mut self,
dst_x: f64,
dst_y: f64,
dst_w: f64,
dst_h: f64,
) -> bool
fn draw_captured_screenshot( &mut self, dst_x: f64, dst_y: f64, dst_w: f64, dst_h: f64, ) -> bool
(dst_x, dst_y, dst_w, dst_h) using the
backend’s preferred filtered sampling. Returns true if the
capture exists and was drawn.Source§fn read_captured_screenshot(&mut self) -> (Vec<u8>, u32, u32)
fn read_captured_screenshot(&mut self) -> (Vec<u8>, u32, u32)
(empty, 0, 0) on backends without a capture or without GPU
readback support.fn set_fill_pattern(&mut self, _pattern: PatternPaint)
fn set_stroke_pattern(&mut self, _pattern: PatternPaint)
fn supports_fill_pattern(&self) -> bool
fn supports_stroke_pattern(&self) -> bool
Source§fn snap_to_pixel(&mut self)
fn snap_to_pixel(&mut self)
rect / fill /
stroke / draw_image_rgba* calls land exactly on the physical pixel
grid — no AA fringe on edges, no LINEAR-filter blur on 1:1 texture
blits. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for WgpuGfxCtx
impl !Send for WgpuGfxCtx
impl !Sync for WgpuGfxCtx
impl !UnwindSafe for WgpuGfxCtx
impl Freeze for WgpuGfxCtx
impl Unpin for WgpuGfxCtx
impl UnsafeUnpin for WgpuGfxCtx
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more