ferrochart_render_wgpu/native.rs
1//! Native drawing of custom series: a plugin
2//! registered with [`crate::WgpuRenderer::register_series`] draws the
3//! [`CustomDraw`] commands of its kind with its own pipelines.
4
5use ferrochart_core::chart::ChartDataView;
6use ferrochart_core::frame::{CustomDraw, Frame};
7
8/// What a [`GpuSeries`] gets to prepare a draw.
9pub struct GpuSeriesCtx<'a> {
10 /// The renderer's device.
11 pub device: &'a wgpu::Device,
12 /// The renderer's queue. Compute work may be submitted here in
13 /// [`GpuSeries::prepare`]; it runs before the frame's render passes.
14 pub queue: &'a wgpu::Queue,
15 /// Format of the color target the draw goes into.
16 pub format: wgpu::TextureFormat,
17 /// Target size in physical pixels (for pixel-to-clip-space math).
18 pub viewport: [f32; 2],
19 /// The frame, e.g. for `frame.styles[draw.style]`.
20 pub frame: &'a Frame,
21}
22
23/// A native implementation of a custom series kind.
24///
25/// For every [`CustomDraw`] of its kind, the renderer calls
26/// [`prepare`](Self::prepare) while recording the frame and
27/// [`draw`](Self::draw) when replaying it, in the order of the frame's
28/// commands. A series may be drawn once per frame; key per-series state by
29/// `draw.series` and upload incrementally with
30/// `data.changes_since(draw.series, synced)`. Read the plugin's data with
31/// `data.custom(draw.series)` (downcast through `&dyn Any`) and point
32/// positions with `data.custom_index`.
33///
34/// The output must match the plugin's portable path within the golden
35/// tolerance; keep CPU helpers and shaders in the same operation
36/// order. Implementations must contain no chart logic (colors and
37/// thresholds come from the style and `draw.params`).
38pub trait GpuSeries {
39 /// The custom series kind it draws (`CustomSeries::kind`).
40 fn kind(&self) -> &'static str;
41 /// Uploads data and runs compute work for `draw`.
42 fn prepare(&mut self, ctx: &GpuSeriesCtx<'_>, draw: &CustomDraw, data: &ChartDataView<'_>);
43 /// Records the draw into `pass`. The scissor is already set to the clip;
44 /// it may set pipelines, bind groups and vertex buffers freely, but must
45 /// not change the scissor or viewport.
46 fn draw(&self, pass: &mut wgpu::RenderPass<'_>, draw: &CustomDraw);
47}