Skip to main content

densa_render/
lib.rs

1//! wgpu renderer for densa.
2//!
3//! [`GpuRenderer`] draws a frame's `densa_draw::Instance`s, as built by a `Canvas` from any
4//! painter (grids, paragraphs, your own), with one instanced draw call: each quad is a solid
5//! rectangle or a glyph sampled from the atlas. It keeps the atlas texture in step with the
6//! glyph cache's updates, growing it on the GPU as the cache grows.
7//!
8//! Applications built on `densa-window` never call it. A frame loop of your own does:
9//!
10//! ```no_run
11//! use densa_draw::{Canvas, InstanceBuffer, Rect};
12//! use densa_render::{AtlasError, Color, GpuRenderer};
13//! use densa_text::{AtlasConfig, GlyphCache};
14//!
15//! /// Once per device: the renderer, and a glyph cache kept within the device's texture limit.
16//! fn setup(device: &wgpu::Device, format: wgpu::TextureFormat) -> (GpuRenderer, GlyphCache) {
17//!     let renderer = GpuRenderer::new(device, format, 65_536);
18//!     let mut cache = GlyphCache::new(AtlasConfig::default());
19//!     let _ = cache.limit_side(GpuRenderer::max_atlas_side(device));
20//!     (renderer, cache)
21//! }
22//!
23//! /// Each frame: paint through a canvas, follow the glyph atlas, draw in one call.
24//! fn frame(
25//!     device: &wgpu::Device,
26//!     queue: &wgpu::Queue,
27//!     (renderer, cache): (&mut GpuRenderer, &mut GlyphCache),
28//!     instances: &mut InstanceBuffer,
29//!     target: &wgpu::TextureView,
30//!     size: (u32, u32),
31//! ) -> Result<(), AtlasError> {
32//!     let mut canvas = Canvas::begin(cache, instances);
33//!     canvas.fill_rect(Rect::new(10.0, 10.0, 100.0, 20.0), Color::WHITE);
34//!     // Grids, paragraphs and charts paint onto the same canvas here.
35//!     renderer.apply_atlas_updates(device, queue, cache.drain_updates())?;
36//!     let count = renderer.prepare(queue, size, instances.as_slice());
37//!     let mut encoder = device.create_command_encoder(&Default::default());
38//!     renderer.render(&mut encoder, target, Color::BLACK, count);
39//!     queue.submit([encoder.finish()]);
40//!     Ok(())
41//! }
42//! ```
43
44mod atlas;
45mod gpu;
46
47pub use atlas::AtlasError;
48pub use densa_core::Color;
49pub use gpu::GpuRenderer;