ombre: Implement GPU benchmark utilities
7db6bd00da47f5dde7fb65b86531d178badd4e8c
1 parent
f8ee4687
ombre/Cargo.toml
+4 -0
| 33 | 33 | path = "examples/paint/basic.rs" |
|
| 34 | 34 | ||
| 35 | 35 | [[example]] |
|
| 36 | 36 | name = "paint-showcase" |
|
| 37 | 37 | path = "examples/paint/showcase.rs" |
|
| 38 | + | ||
| 39 | + | [[example]] |
|
| 40 | + | name = "paint-benchmark" |
|
| 41 | + | path = "examples/paint/benchmark.rs" |
ombre/examples/button.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/buttons.rs
+2 -2
| 12 | 12 | red: (u64, bool), |
|
| 13 | 13 | blue: (u64, bool), |
|
| 14 | 14 | green: (u64, bool), |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | - | pub struct Example<T> { |
|
| 18 | - | ui: gui::Interface<Box<dyn Widget<Data>>, Data>, |
|
| 17 | + | pub struct Example<T: RenderBackend> { |
|
| 18 | + | ui: gui::Interface<Box<dyn Widget<Data>>, Data, T>, |
|
| 19 | 19 | data: Data, |
|
| 20 | 20 | backend: T, |
|
| 21 | 21 | } |
|
| 22 | 22 | ||
| 23 | 23 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/canvas.rs
+2 -2
| 6 | 6 | use ombre::ui as gui; |
|
| 7 | 7 | use ombre::ui::widgets::*; |
|
| 8 | 8 | ||
| 9 | 9 | type State = Vec<Point2D<f32, Local>>; |
|
| 10 | 10 | ||
| 11 | - | pub struct Example<T> { |
|
| 12 | - | ui: gui::Interface<Click<Canvas<State>, State>, State>, |
|
| 11 | + | pub struct Example<T: RenderBackend> { |
|
| 12 | + | ui: gui::Interface<Click<Canvas<State>, State>, State, T>, |
|
| 13 | 13 | state: State, |
|
| 14 | 14 | backend: T, |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/checkbox.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<bool>>, bool>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<bool>>, bool, T>, |
|
| 12 | 12 | checked: bool, |
|
| 13 | 13 | backend: T, |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | const POINTER: &[u8] = include_bytes!("common/pointer.rgba"); |
ombre/examples/counter.rs
+2 -2
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | ||
| 9 | 9 | type State = usize; |
|
| 10 | 10 | ||
| 11 | - | pub struct Example<R> { |
|
| 12 | - | ui: gui::Interface<Box<dyn Widget<State>>, State>, |
|
| 11 | + | pub struct Example<R: RenderBackend> { |
|
| 12 | + | ui: gui::Interface<Box<dyn Widget<State>>, State, R>, |
|
| 13 | 13 | state: State, |
|
| 14 | 14 | backend: R, |
|
| 15 | 15 | } |
|
| 16 | 16 | ||
| 17 | 17 | impl<R: gfx::RenderBackend> Example<R> { |
ombre/examples/cursors.rs
+2 -2
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::{CursorDescriptor, Widget, WidgetExt as _}; |
|
| 9 | 9 | ||
| 10 | 10 | type State = usize; |
|
| 11 | 11 | ||
| 12 | - | pub struct Example<R> { |
|
| 13 | - | ui: gui::Interface<Box<dyn Widget<State>>, State>, |
|
| 12 | + | pub struct Example<R: RenderBackend> { |
|
| 13 | + | ui: gui::Interface<Box<dyn Widget<State>>, State, R>, |
|
| 14 | 14 | state: State, |
|
| 15 | 15 | backend: R, |
|
| 16 | 16 | } |
|
| 17 | 17 | ||
| 18 | 18 | const POINTER: &[u8] = include_bytes!("common/pointer.rgba"); |
ombre/examples/fonts.rs
+2 -2
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | use ombre::ui::TextAlign; |
|
| 10 | 10 | ||
| 11 | - | pub struct Example<T> { |
|
| 12 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 11 | + | pub struct Example<T: RenderBackend> { |
|
| 12 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 13 | 13 | backend: T, |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 17 | 17 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/hover.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/hstack.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/image.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<widgets::Image, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<widgets::Image, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/input.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui::widgets::*; |
|
| 8 | 8 | use ombre::ui::{self as gui}; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<String>>, String>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<String>>, String, T>, |
|
| 12 | 12 | data: String, |
|
| 13 | 13 | backend: T, |
|
| 14 | 14 | } |
|
| 15 | 15 | ||
| 16 | 16 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/paint/basic.rs
+2 -2
| 4 | 4 | ||
| 5 | 5 | use ombre::paint::{canvas, shape, Canvas, PaintSurface}; |
|
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::rng::Rng; |
|
| 8 | 8 | ||
| 9 | - | pub struct Example<T> { |
|
| 10 | - | canvas: Canvas, |
|
| 9 | + | pub struct Example<T: RenderBackend> { |
|
| 10 | + | canvas: Canvas<T>, |
|
| 11 | 11 | rng: Rng, |
|
| 12 | 12 | delta: f64, |
|
| 13 | 13 | backend: T, |
|
| 14 | 14 | } |
|
| 15 | 15 |
ombre/examples/paint/benchmark.rs
added
+160 -0
| 1 | + | #![deny(clippy::all)] |
|
| 2 | + | #![allow(clippy::unwrap_used)] |
|
| 3 | + | use std::time::Duration; |
|
| 4 | + | ||
| 5 | + | use ombre::paint::{canvas, shape, Canvas, PaintSurface}; |
|
| 6 | + | use ombre::prelude::*; |
|
| 7 | + | use ombre::rng::Rng; |
|
| 8 | + | use runtime::{RuntimeCtx, Timings}; |
|
| 9 | + | ||
| 10 | + | const NUM_SHAPES: usize = 2048; |
|
| 11 | + | const SHAPE_SIZE_MIN: u64 = 4; |
|
| 12 | + | const SHAPE_SIZE_MAX: u64 = 32; |
|
| 13 | + | ||
| 14 | + | pub struct Example<T: RenderBackend> { |
|
| 15 | + | canvas: Canvas<T>, |
|
| 16 | + | shapes: Vec<ShapeInfo>, |
|
| 17 | + | backend: T, |
|
| 18 | + | timings: Timings, |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | struct ShapeInfo { |
|
| 22 | + | position: Point2D<f32, Local>, |
|
| 23 | + | velocity: Vector2D<f32, Local>, |
|
| 24 | + | size: f32, |
|
| 25 | + | color: Rgba<f32>, |
|
| 26 | + | fill: bool, |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | impl<T: gfx::RenderBackend> Example<T> { |
|
| 30 | + | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, canvas::Error<T>> { |
|
| 31 | + | let canvas = ombre::canvas(win.size().into(), &mut backend).build()?; |
|
| 32 | + | let size = win.size(); |
|
| 33 | + | let mut rng = Rng::default(); |
|
| 34 | + | ||
| 35 | + | // Initialize shapes with random properties |
|
| 36 | + | let shapes = (0..NUM_SHAPES) |
|
| 37 | + | .map(|_| { |
|
| 38 | + | let position = Point2D::new( |
|
| 39 | + | rng.fraction() as f32 * size.width as f32, |
|
| 40 | + | rng.fraction() as f32 * size.height as f32, |
|
| 41 | + | ); |
|
| 42 | + | let velocity = Vector2D::new(rng.gen(-50..50) as f32, rng.gen(-50..50) as f32); |
|
| 43 | + | let size = rng.gen(SHAPE_SIZE_MIN..SHAPE_SIZE_MAX) as f32; |
|
| 44 | + | let color = Rgba::new( |
|
| 45 | + | rng.fraction() as f32, |
|
| 46 | + | rng.fraction() as f32, |
|
| 47 | + | 1.0, |
|
| 48 | + | 0.7 + 0.3 * rng.fraction() as f32, |
|
| 49 | + | ); |
|
| 50 | + | ShapeInfo { |
|
| 51 | + | position, |
|
| 52 | + | velocity, |
|
| 53 | + | size, |
|
| 54 | + | color, |
|
| 55 | + | fill: rng.flip(), |
|
| 56 | + | } |
|
| 57 | + | }) |
|
| 58 | + | .collect(); |
|
| 59 | + | ||
| 60 | + | Ok(Self { |
|
| 61 | + | canvas, |
|
| 62 | + | shapes, |
|
| 63 | + | backend, |
|
| 64 | + | timings: Timings::default(), |
|
| 65 | + | }) |
|
| 66 | + | } |
|
| 67 | + | } |
|
| 68 | + | ||
| 69 | + | impl<R: RenderBackend> Application for Example<R> { |
|
| 70 | + | type Error = R::Error; |
|
| 71 | + | ||
| 72 | + | fn event( |
|
| 73 | + | &mut self, |
|
| 74 | + | event: platform::WindowEvent, |
|
| 75 | + | _ctx: &mut RuntimeCtx<'_>, |
|
| 76 | + | ) -> Result<(), R::Error> { |
|
| 77 | + | if let platform::WindowEvent::Resized(size) = event { |
|
| 78 | + | self.canvas.resize(size, &mut self.backend)?; |
|
| 79 | + | } |
|
| 80 | + | Ok(()) |
|
| 81 | + | } |
|
| 82 | + | ||
| 83 | + | fn tick(&mut self, delta: Duration, ctx: &mut RuntimeCtx<'_>) -> Result<(), Self::Error> { |
|
| 84 | + | self.timings = ctx.timings.clone(); |
|
| 85 | + | ||
| 86 | + | let size = self.canvas.size(); |
|
| 87 | + | let dt = delta.as_secs_f32(); |
|
| 88 | + | ||
| 89 | + | for shape in &mut self.shapes { |
|
| 90 | + | // Update position. |
|
| 91 | + | shape.position = shape.position + shape.velocity * dt; |
|
| 92 | + | ||
| 93 | + | // Bounce off walls. |
|
| 94 | + | if shape.position.x < 0.0 || shape.position.x > size.w { |
|
| 95 | + | shape.velocity.x = -shape.velocity.x; |
|
| 96 | + | } |
|
| 97 | + | if shape.position.y < 0.0 || shape.position.y > size.h { |
|
| 98 | + | shape.velocity.y = -shape.velocity.y; |
|
| 99 | + | } |
|
| 100 | + | // Clamp position to window bounds. |
|
| 101 | + | shape.position.x = shape.position.x.clamp(0.0, size.w); |
|
| 102 | + | shape.position.y = shape.position.y.clamp(0.0, size.h); |
|
| 103 | + | } |
|
| 104 | + | ctx.window.redraw(); |
|
| 105 | + | ||
| 106 | + | Ok(()) |
|
| 107 | + | } |
|
| 108 | + | ||
| 109 | + | fn frame(&mut self) -> Result<(), Self::Error> { |
|
| 110 | + | let mut frame = self.backend.frame(); |
|
| 111 | + | ||
| 112 | + | // Draw all shapes. |
|
| 113 | + | self.canvas.clear(); |
|
| 114 | + | for shape in &self.shapes { |
|
| 115 | + | let rect_size = Size::new(shape.size, shape.size); |
|
| 116 | + | let rect_pos = shape.position - Vector2D::new(shape.size / 2.0, shape.size / 2.0); |
|
| 117 | + | if shape.fill { |
|
| 118 | + | self.canvas.fill(shape::rectangle(rect_pos, rect_size), shape.color); |
|
| 119 | + | } else { |
|
| 120 | + | self.canvas |
|
| 121 | + | .stroke(shape::rectangle(rect_pos, rect_size), shape.color, 1.); |
|
| 122 | + | } |
|
| 123 | + | } |
|
| 124 | + | // Draw debug info. |
|
| 125 | + | self.canvas.text( |
|
| 126 | + | format!("frame: {}us", self.timings.avg_frame_time.as_micros()).as_str(), |
|
| 127 | + | Point2D::new(8., 8.), |
|
| 128 | + | ); |
|
| 129 | + | self.canvas.text( |
|
| 130 | + | format!("tick: {}us", self.timings.avg_tick_time.as_micros()).as_str(), |
|
| 131 | + | Point2D::new(8., 24.), |
|
| 132 | + | ); |
|
| 133 | + | self.canvas.text( |
|
| 134 | + | format!("draw: {}us", self.canvas.timer().average().as_micros()).as_str(), |
|
| 135 | + | Point2D::new(8., 40.), |
|
| 136 | + | ); |
|
| 137 | + | self.canvas.flush(&mut frame)?; |
|
| 138 | + | ||
| 139 | + | Ok(()) |
|
| 140 | + | } |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | fn main() -> Result<(), Box<dyn std::error::Error>> { |
|
| 144 | + | logger::init(log::Level::Debug)?; |
|
| 145 | + | ||
| 146 | + | let (mut win, events) = platform::init( |
|
| 147 | + | "paint/benchmark", |
|
| 148 | + | 800, |
|
| 149 | + | 600, |
|
| 150 | + | &[], |
|
| 151 | + | platform::GraphicsContext::Gl, |
|
| 152 | + | )?; |
|
| 153 | + | let ctx = unsafe { glow::Context::from_loader_function(|p| win.get_proc_address(p)) }; |
|
| 154 | + | let backend = gfx::gl::Context::new(ctx, win.size())?; |
|
| 155 | + | let app = Example::init(backend, &win)?; |
|
| 156 | + | ||
| 157 | + | ombre::runtime::default::run(win, events, app)?; |
|
| 158 | + | ||
| 159 | + | Ok(()) |
|
| 160 | + | } |
ombre/examples/paint/showcase.rs
+2 -2
| 4 | 4 | use ombre::paint::shape::Circle; |
|
| 5 | 5 | use ombre::paint::{canvas, Canvas, PaintSurface}; |
|
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::text::Text; |
|
| 8 | 8 | ||
| 9 | - | pub struct Example<T> { |
|
| 10 | - | canvas: Canvas, |
|
| 9 | + | pub struct Example<T: RenderBackend> { |
|
| 10 | + | canvas: Canvas<T>, |
|
| 11 | 11 | image: Image, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend + std::fmt::Debug> Example<T> { |
ombre/examples/picker.rs
+2 -2
| 9 | 9 | use ombre::ui as gui; |
|
| 10 | 10 | use ombre::ui::widgets::*; |
|
| 11 | 11 | ||
| 12 | 12 | type State = Rgba8; |
|
| 13 | 13 | ||
| 14 | - | pub struct Example<T> { |
|
| 15 | - | ui: gui::Interface<Box<dyn Widget<State>>, State>, |
|
| 14 | + | pub struct Example<T: RenderBackend> { |
|
| 15 | + | ui: gui::Interface<Box<dyn Widget<State>>, State, T>, |
|
| 16 | 16 | state: State, |
|
| 17 | 17 | backend: T, |
|
| 18 | 18 | } |
|
| 19 | 19 | ||
| 20 | 20 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/sprite.rs
+2 -2
| 17 | 17 | scale: f32, |
|
| 18 | 18 | started: time::Instant, |
|
| 19 | 19 | image: Image, |
|
| 20 | 20 | } |
|
| 21 | 21 | ||
| 22 | - | pub struct Example<T> { |
|
| 23 | - | ui: gui::Interface<gui::Canvas<State>, State>, |
|
| 22 | + | pub struct Example<T: RenderBackend> { |
|
| 23 | + | ui: gui::Interface<gui::Canvas<State>, State, T>, |
|
| 24 | 24 | state: State, |
|
| 25 | 25 | backend: T, |
|
| 26 | 26 | } |
|
| 27 | 27 | ||
| 28 | 28 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/state.rs
+2 -2
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::{Widget, WidgetExt}; |
|
| 9 | 9 | ||
| 10 | 10 | type State = usize; |
|
| 11 | 11 | ||
| 12 | - | pub struct Example<T> { |
|
| 13 | - | ui: gui::Interface<Box<dyn Widget<State>>, usize>, |
|
| 12 | + | pub struct Example<T: RenderBackend> { |
|
| 13 | + | ui: gui::Interface<Box<dyn Widget<State>>, State, T>, |
|
| 14 | 14 | state: State, |
|
| 15 | 15 | backend: T, |
|
| 16 | 16 | } |
|
| 17 | 17 | ||
| 18 | 18 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/text.rs
+2 -2
| 39 | 39 | fn as_ref(&self) -> &str { |
|
| 40 | 40 | self.content.as_ref() |
|
| 41 | 41 | } |
|
| 42 | 42 | } |
|
| 43 | 43 | ||
| 44 | - | pub struct Example<T> { |
|
| 45 | - | ui: gui::Interface<Box<dyn Widget<RichText>>, RichText>, |
|
| 44 | + | pub struct Example<T: RenderBackend> { |
|
| 45 | + | ui: gui::Interface<Box<dyn Widget<RichText>>, RichText, T>, |
|
| 46 | 46 | text: RichText, |
|
| 47 | 47 | backend: T, |
|
| 48 | 48 | } |
|
| 49 | 49 | ||
| 50 | 50 | impl<T: gfx::RenderBackend> Example<T> { |
ombre/examples/vstack.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/examples/zstack.rs
+2 -2
| 5 | 5 | ||
| 6 | 6 | use ombre::prelude::*; |
|
| 7 | 7 | use ombre::ui as gui; |
|
| 8 | 8 | use ombre::ui::widgets::*; |
|
| 9 | 9 | ||
| 10 | - | pub struct Example<T> { |
|
| 11 | - | ui: gui::Interface<Box<dyn Widget<()>>, ()>, |
|
| 10 | + | pub struct Example<T: RenderBackend> { |
|
| 11 | + | ui: gui::Interface<Box<dyn Widget<()>>, (), T>, |
|
| 12 | 12 | backend: T, |
|
| 13 | 13 | } |
|
| 14 | 14 | ||
| 15 | 15 | impl<T: gfx::RenderBackend> Example<T> { |
|
| 16 | 16 | pub fn init(mut backend: T, win: &platform::Window) -> Result<Self, gui::Error<T>> { |
ombre/src/gfx/gl.rs
+6 -0
| 1 | 1 | //! OpenGL backend. |
|
| 2 | 2 | mod error; |
|
| 3 | + | mod timer; |
|
| 3 | 4 | mod types; |
|
| 4 | 5 | ||
| 5 | 6 | use std::mem; |
|
| 6 | 7 | ||
| 7 | 8 | use glow::HasContext as _; |
| 667 | 668 | impl RenderBackend for Context { |
|
| 668 | 669 | type Info = ContextInfo; |
|
| 669 | 670 | type Frame<'a> = FrameBuilder<'a>; |
|
| 670 | 671 | type Error = Error; |
|
| 671 | 672 | type TextureHandle = glow::Texture; |
|
| 673 | + | type Timer = timer::GlTimer; |
|
| 672 | 674 | ||
| 673 | 675 | fn info(&self) -> ContextInfo { |
|
| 674 | 676 | let version = self.gl.version().clone(); |
|
| 675 | 677 | ContextInfo { version } |
|
| 676 | 678 | } |
| 680 | 682 | ctx: self, |
|
| 681 | 683 | active: Active::default(), |
|
| 682 | 684 | } |
|
| 683 | 685 | } |
|
| 684 | 686 | ||
| 687 | + | fn timer(&mut self) -> Result<Self::Timer, Error> { |
|
| 688 | + | timer::GlTimer::new(self) |
|
| 689 | + | } |
|
| 690 | + | ||
| 685 | 691 | fn shader( |
|
| 686 | 692 | &mut self, |
|
| 687 | 693 | shader: ShaderSource<'_>, |
|
| 688 | 694 | meta: ShaderDescriptor<'_>, |
|
| 689 | 695 | ) -> Result<ShaderId, Error> { |
ombre/src/gfx/gl/timer.rs
added
+79 -0
| 1 | + | use std::{collections::VecDeque, time::Duration}; |
|
| 2 | + | ||
| 3 | + | use glow::HasContext as _; |
|
| 4 | + | ||
| 5 | + | use super::{Context, Error, FrameBuilder, RenderFrame}; |
|
| 6 | + | use crate::gfx::traits::Timer; |
|
| 7 | + | ||
| 8 | + | /// Implementation of [`Timer`] for the OpenGL backend. |
|
| 9 | + | pub struct GlTimer { |
|
| 10 | + | query: glow::Query, |
|
| 11 | + | timings: VecDeque<u64>, |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | impl Timer for GlTimer { |
|
| 15 | + | type Backend = Context; |
|
| 16 | + | ||
| 17 | + | fn new(ctx: &Context) -> Result<Self, Error> { |
|
| 18 | + | unsafe { |
|
| 19 | + | let query = ctx.gl.create_query().map_err(Error::Internal)?; |
|
| 20 | + | ||
| 21 | + | Ok(Self { |
|
| 22 | + | query, |
|
| 23 | + | timings: VecDeque::with_capacity(128), |
|
| 24 | + | }) |
|
| 25 | + | } |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | fn measure<F, T>(&mut self, frame: &mut FrameBuilder<'_>, inner: F) -> Result<T, Error> |
|
| 29 | + | where |
|
| 30 | + | F: FnOnce(&mut FrameBuilder<'_>) -> Result<T, Error>, |
|
| 31 | + | { |
|
| 32 | + | unsafe { |
|
| 33 | + | frame.backend().gl.begin_query(glow::TIME_ELAPSED, self.query); |
|
| 34 | + | let result = inner(frame); |
|
| 35 | + | frame.backend().gl.end_query(glow::TIME_ELAPSED); |
|
| 36 | + | ||
| 37 | + | let nanos = frame |
|
| 38 | + | .backend() |
|
| 39 | + | .gl |
|
| 40 | + | .get_query_parameter_u32(self.query, glow::QUERY_RESULT); |
|
| 41 | + | if nanos > 0 { |
|
| 42 | + | self.timings.truncate(self.timings.capacity()); |
|
| 43 | + | self.timings.push_front(nanos as u64); |
|
| 44 | + | } |
|
| 45 | + | result |
|
| 46 | + | } |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | fn min(&self) -> Duration { |
|
| 50 | + | self.timings |
|
| 51 | + | .iter() |
|
| 52 | + | .min() |
|
| 53 | + | .map(|n| Duration::from_nanos(*n)) |
|
| 54 | + | .unwrap_or_default() |
|
| 55 | + | } |
|
| 56 | + | ||
| 57 | + | fn max(&self) -> Duration { |
|
| 58 | + | self.timings |
|
| 59 | + | .iter() |
|
| 60 | + | .max() |
|
| 61 | + | .map(|n| Duration::from_nanos(*n)) |
|
| 62 | + | .unwrap_or_default() |
|
| 63 | + | } |
|
| 64 | + | ||
| 65 | + | fn average(&self) -> Duration { |
|
| 66 | + | self.timings |
|
| 67 | + | .iter() |
|
| 68 | + | .sum::<u64>() |
|
| 69 | + | .checked_div(self.timings.len() as u64) |
|
| 70 | + | .map(Duration::from_nanos) |
|
| 71 | + | .unwrap_or_default() |
|
| 72 | + | } |
|
| 73 | + | ||
| 74 | + | fn destroy(&mut self, ctx: &Context) { |
|
| 75 | + | unsafe { |
|
| 76 | + | ctx.gl.delete_query(self.query); |
|
| 77 | + | } |
|
| 78 | + | } |
|
| 79 | + | } |
ombre/src/gfx/traits.rs
+40 -1
| 1 | 1 | //! Graphics rendering traits, implemented by backends. |
|
| 2 | + | #![warn(missing_docs)] |
|
| 3 | + | use std::time::Duration; |
|
| 4 | + | ||
| 2 | 5 | use crate::bytes::Packed; |
|
| 3 | 6 | use crate::collections::IntMap; |
|
| 4 | 7 | use crate::math::ZDepth; |
|
| 5 | 8 | ||
| 6 | 9 | use super::*; |
| 16 | 19 | Self: 'a; |
|
| 17 | 20 | /// Error returned by trait methods. |
|
| 18 | 21 | type Error: std::error::Error + 'static; |
|
| 19 | 22 | /// Texture handle. |
|
| 20 | 23 | type TextureHandle; |
|
| 24 | + | /// Timer, for measuring graphics operations. |
|
| 25 | + | type Timer: Timer<Backend = Self>; |
|
| 21 | 26 | ||
| 22 | 27 | /// Return information about the backend. |
|
| 23 | 28 | fn info(&self) -> Self::Info; |
|
| 24 | 29 | /// Create a [`Self::Frame`] object. |
|
| 25 | 30 | fn frame(&mut self) -> Self::Frame<'_>; |
|
| 31 | + | /// Create a [`Self::Timer`] object that can measure graphics operations. |
|
| 32 | + | fn timer(&mut self) -> Result<Self::Timer, Self::Error>; |
|
| 26 | 33 | /// Create a new shader program. |
|
| 27 | 34 | fn shader( |
|
| 28 | 35 | &mut self, |
|
| 29 | 36 | shader: ShaderSource<'_>, |
|
| 30 | 37 | meta: ShaderDescriptor<'_>, |
| 128 | 135 | /// [`RenderFrame::commit`]. |
|
| 129 | 136 | pub trait RenderFrame<'a> { |
|
| 130 | 137 | /// Error returned by trait methods. |
|
| 131 | 138 | type Error: std::error::Error + 'static; |
|
| 132 | 139 | /// The render backend. |
|
| 133 | - | type Backend: RenderBackend; |
|
| 140 | + | type Backend: RenderBackend<Frame<'a> = Self> + 'a; |
|
| 134 | 141 | ||
| 135 | 142 | /// Get a reference to the render backend. |
|
| 136 | 143 | fn backend(&self) -> &Self::Backend; |
|
| 137 | 144 | /// Get a mutable reference to the render backend. |
|
| 138 | 145 | fn backend_mut(&mut self) -> &mut Self::Backend; |
| 181 | 188 | ||
| 182 | 189 | impl TextureData for &[Rgba8] {} |
|
| 183 | 190 | impl TextureData for Empty {} |
|
| 184 | 191 | impl<const N: usize> TextureData for [Rgba8; N] {} |
|
| 185 | 192 | impl<T, const N: usize, const M: usize> TextureData for [[T; N]; M] where T: Packed {} |
|
| 193 | + | ||
| 194 | + | /// Type for render frames used in [`Timer`]. |
|
| 195 | + | type TimerFrame<'f, T> = <<T as Timer>::Backend as RenderBackend>::Frame<'f>; |
|
| 196 | + | /// Type for render errors used in [`Timer`]. |
|
| 197 | + | type TimerError<T> = <<T as Timer>::Backend as RenderBackend>::Error; |
|
| 198 | + | ||
| 199 | + | /// Objects that implement GPU timing functionality. |
|
| 200 | + | pub trait Timer { |
|
| 201 | + | /// The GPU backend. |
|
| 202 | + | type Backend: RenderBackend; |
|
| 203 | + | ||
| 204 | + | /// Create a new timer. |
|
| 205 | + | fn new(ctx: &Self::Backend) -> Result<Self, <Self::Backend as RenderBackend>::Error> |
|
| 206 | + | where |
|
| 207 | + | Self: Sized; |
|
| 208 | + | /// Measure the GPU time taken by the supplied closure. |
|
| 209 | + | fn measure<I, T>( |
|
| 210 | + | &mut self, |
|
| 211 | + | ctx: &mut TimerFrame<'_, Self>, |
|
| 212 | + | inner: I, |
|
| 213 | + | ) -> Result<T, TimerError<Self>> |
|
| 214 | + | where |
|
| 215 | + | I: FnOnce(&mut TimerFrame<'_, Self>) -> Result<T, TimerError<Self>>; |
|
| 216 | + | /// Get the minimum measured time in the measurement window. |
|
| 217 | + | fn min(&self) -> Duration; |
|
| 218 | + | /// Get the maximum measured time in the measurement window. |
|
| 219 | + | fn max(&self) -> Duration; |
|
| 220 | + | /// Get the average measured time in the measurement window. |
|
| 221 | + | fn average(&self) -> Duration; |
|
| 222 | + | /// Free all resources held by this timer. |
|
| 223 | + | fn destroy(&mut self, ctx: &Self::Backend); |
|
| 224 | + | } |
ombre/src/paint/canvas.rs
+50 -44
| 231 | 231 | self.debug = debug; |
|
| 232 | 232 | self |
|
| 233 | 233 | } |
|
| 234 | 234 | ||
| 235 | 235 | /// Build the interface by giving it the root of the widget tree. |
|
| 236 | - | pub fn build(mut self) -> Result<Canvas, Error<R>> { |
|
| 236 | + | pub fn build(mut self) -> Result<Canvas<R>, Error<R>> { |
|
| 237 | 237 | let default = self._font(font::uf2::DEFAULT)?; |
|
| 238 | 238 | // Adjust max vertices to fit complete triangles. |
|
| 239 | 239 | self.config.max_vertices -= self.config.max_vertices % pipeline::VERTEX_STRIDE; |
|
| 240 | 240 | self._build(default).map_err(Error::Render) |
|
| 241 | 241 | } |
|
| 242 | 242 | ||
| 243 | 243 | /// Inner build function. |
|
| 244 | - | fn _build(self, default: Rc<Font<Image>>) -> Result<Canvas, R::Error> { |
|
| 244 | + | fn _build(self, default: Rc<Font<Image>>) -> Result<Canvas<R>, R::Error> { |
|
| 245 | 245 | // Ensure the renderable viewport is a multiple of the scale. |
|
| 246 | 246 | let size = Size::new( |
|
| 247 | 247 | self.size.w - self.size.w % self.scale, |
|
| 248 | 248 | self.size.h - self.size.h % self.scale, |
|
| 249 | 249 | ); |
| 311 | 311 | let layers = TextureInfo { |
|
| 312 | 312 | id: layers, |
|
| 313 | 313 | size: self.config.max_texture_size, |
|
| 314 | 314 | }; |
|
| 315 | 315 | let render = RenderState::new(layers); |
|
| 316 | + | let timer = self.backend.timer()?; |
|
| 316 | 317 | let fonts = Fonts { |
|
| 317 | 318 | default, |
|
| 318 | 319 | loaded: self.fonts, |
|
| 319 | 320 | }; |
|
| 320 | 321 |
| 329 | 330 | size_scaled, |
|
| 330 | 331 | size_viewport: size_max, |
|
| 331 | 332 | scale: self.scale, |
|
| 332 | 333 | transform: Transform2D::identity(), |
|
| 333 | 334 | render, |
|
| 335 | + | timer, |
|
| 334 | 336 | fonts, |
|
| 335 | 337 | config: self.config, |
|
| 336 | 338 | }) |
|
| 337 | 339 | } |
|
| 338 | 340 |
| 363 | 365 | /// A 2D drawing canvas that provides batched rendering capabilities. |
|
| 364 | 366 | /// |
|
| 365 | 367 | /// The Canvas manages texture allocation, vertex batching, and provides a high-level |
|
| 366 | 368 | /// drawing API for shapes, images and text. It uses a fixed number of texture layers |
|
| 367 | 369 | /// specified by the type parameter `N`. |
|
| 368 | - | pub struct Canvas<const N: usize = DEFAULT_MAX_TEXTURE_LAYERS> { |
|
| 370 | + | pub struct Canvas<R: RenderBackend, const N: usize = DEFAULT_MAX_TEXTURE_LAYERS> { |
|
| 369 | 371 | /// Size of the canvas. This is the usable area of the viewport. |
|
| 370 | 372 | /// It might be smaller than the full viewport if the viewport is not a |
|
| 371 | 373 | /// multiple of the scaling factor. This avoids stretching pixels. |
|
| 372 | 374 | size: Size<f32>, |
|
| 373 | 375 | /// Size of renderable viewport. This is the size available to render the canvas. |
| 392 | 394 | debug: DebugState, |
|
| 393 | 395 | /// Next pass action. |
|
| 394 | 396 | action: Option<PassAction>, |
|
| 395 | 397 | /// Loaded fonts. |
|
| 396 | 398 | fonts: Fonts<Image>, |
|
| 399 | + | /// GPU timer. |
|
| 400 | + | timer: R::Timer, |
|
| 397 | 401 | /// Canvas configuration. |
|
| 398 | 402 | config: Config, |
|
| 399 | 403 | } |
|
| 400 | 404 | ||
| 401 | - | impl<const N: usize> Canvas<N> { |
|
| 405 | + | impl<R: RenderBackend, const N: usize> Canvas<R, N> { |
|
| 402 | 406 | /// Returns the current size of the drawable area in logical pixels. |
|
| 403 | 407 | pub fn size(&self) -> Size<f32> { |
|
| 404 | 408 | self.size |
|
| 405 | 409 | } |
|
| 406 | 410 |
| 418 | 422 | /// When enabled, additional debug information will be rendered on top of the canvas. |
|
| 419 | 423 | pub fn debug(&mut self, visible: bool) { |
|
| 420 | 424 | self.debug.visible = visible; |
|
| 421 | 425 | } |
|
| 422 | 426 | ||
| 427 | + | /// Get the frame timer. |
|
| 428 | + | pub fn timer(&self) -> &R::Timer { |
|
| 429 | + | &self.timer |
|
| 430 | + | } |
|
| 431 | + | ||
| 423 | 432 | /// Resize the canvas to the specified size. |
|
| 424 | - | pub fn resize<R: RenderBackend>( |
|
| 425 | - | &mut self, |
|
| 426 | - | size: platform::LogicalSize, |
|
| 427 | - | backend: &mut R, |
|
| 428 | - | ) -> Result<(), R::Error> { |
|
| 433 | + | pub fn resize(&mut self, size: platform::LogicalSize, backend: &mut R) -> Result<(), R::Error> { |
|
| 429 | 434 | // When the canvas is resized, we want to keep pixels aligned to the pixel grid, |
|
| 430 | 435 | // even when UI scaling is used. To do this, we render to a subarea of the canvas's |
|
| 431 | 436 | // framebuffer: the largest area with a size that is a multiple of the `scale` |
|
| 432 | 437 | // value. |
|
| 433 | 438 | let bounds = Size::new(size.width as f32, size.height as f32); |
| 462 | 467 | ||
| 463 | 468 | /// Renders the current batch of vertices using the shading pipeline. |
|
| 464 | 469 | /// |
|
| 465 | 470 | /// This handles splitting large batches into multiple draw calls if they exceed |
|
| 466 | 471 | /// the maximum vertex buffer size. |
|
| 467 | - | fn draw_render_state<'f, F: RenderFrame<'f>>( |
|
| 472 | + | fn draw_render_state( |
|
| 468 | 473 | &mut self, |
|
| 469 | 474 | viewport: Size<f32>, |
|
| 470 | - | frame: &mut F, |
|
| 471 | - | ) -> Result<(), F::Error> { |
|
| 472 | - | let batch = self.render.batch.vertices(); |
|
| 473 | - | if batch.is_empty() { |
|
| 474 | - | return Ok(()); |
|
| 475 | - | } |
|
| 476 | - | ||
| 477 | - | frame.apply_pipeline(&self.shade.pipeline)?; |
|
| 478 | - | frame.apply_uniforms(&pipeline::Uniforms::new( |
|
| 479 | - | viewport, |
|
| 480 | - | Transform3D::identity(), |
|
| 481 | - | self.render.sizes, |
|
| 482 | - | ))?; |
|
| 475 | + | frame: &mut <R as RenderBackend>::Frame<'_>, |
|
| 476 | + | ) -> Result<(), R::Error> { |
|
| 477 | + | self.timer.measure(frame, |frame| { |
|
| 478 | + | let batch = self.render.batch.vertices(); |
|
| 479 | + | if batch.is_empty() { |
|
| 480 | + | return Ok(()); |
|
| 481 | + | } |
|
| 483 | 482 | ||
| 484 | - | let bindings = &self.shade.bindings; |
|
| 485 | - | if batch.len() > self.config.max_vertices { |
|
| 486 | - | log::debug!( |
|
| 487 | - | target: "ui", |
|
| 488 | - | "vertex buffer capacity ({}) exceeded, \ |
|
| 489 | - | splitting batch render of {} vertices into {} draw calls..", |
|
| 490 | - | self.config.max_vertices, |
|
| 491 | - | batch.len(), |
|
| 492 | - | batch.len().div_ceil(self.config.max_vertices) |
|
| 493 | - | ); |
|
| 494 | - | } |
|
| 495 | - | for chunk in batch.chunks(self.config.max_vertices) { |
|
| 496 | - | frame.buffer_update(bindings.vertex[0], BufferKind::Vertex, 0, chunk)?; |
|
| 497 | - | frame.apply_bindings(bindings)?; |
|
| 498 | - | frame.draw(0, chunk.len(), 1)?; |
|
| 499 | - | } |
|
| 500 | - | Ok(()) |
|
| 483 | + | frame.apply_pipeline(&self.shade.pipeline)?; |
|
| 484 | + | frame.apply_uniforms(&pipeline::Uniforms::new( |
|
| 485 | + | viewport, |
|
| 486 | + | Transform3D::identity(), |
|
| 487 | + | self.render.sizes, |
|
| 488 | + | ))?; |
|
| 489 | + | ||
| 490 | + | let bindings = &self.shade.bindings; |
|
| 491 | + | if batch.len() > self.config.max_vertices { |
|
| 492 | + | log::debug!( |
|
| 493 | + | target: "ui", |
|
| 494 | + | "vertex buffer capacity ({}) exceeded, \ |
|
| 495 | + | splitting batch render of {} vertices into {} draw calls..", |
|
| 496 | + | self.config.max_vertices, |
|
| 497 | + | batch.len(), |
|
| 498 | + | batch.len().div_ceil(self.config.max_vertices) |
|
| 499 | + | ); |
|
| 500 | + | } |
|
| 501 | + | for chunk in batch.chunks(self.config.max_vertices) { |
|
| 502 | + | frame.buffer_update(bindings.vertex[0], BufferKind::Vertex, 0, chunk)?; |
|
| 503 | + | frame.apply_bindings(bindings)?; |
|
| 504 | + | frame.draw(0, chunk.len(), 1)?; |
|
| 505 | + | } |
|
| 506 | + | Ok(()) |
|
| 507 | + | }) |
|
| 501 | 508 | } |
|
| 502 | 509 | } |
|
| 503 | 510 | ||
| 504 | - | impl<const N: usize> PaintSurface for Canvas<N> { |
|
| 511 | + | impl<R: RenderBackend, const N: usize> PaintSurface<R> for Canvas<R, N> { |
|
| 505 | 512 | fn clear(&mut self) { |
|
| 506 | 513 | self.render.batch.clear(); |
|
| 507 | 514 | self.action = Some(PassAction::Clear { |
|
| 508 | 515 | color: Some(Rgba::TRANSPARENT), |
|
| 509 | 516 | depth: Some(ZDepth::from(1.)), |
| 581 | 588 | ||
| 582 | 589 | fn status(&mut self) -> Result<(), super::traits::Error> { |
|
| 583 | 590 | Ok(()) |
|
| 584 | 591 | } |
|
| 585 | 592 | ||
| 586 | - | fn flush<'f, F: RenderFrame<'f>>(&mut self, frame: &mut F) -> Result<(), F::Error> { |
|
| 593 | + | fn flush(&mut self, frame: &mut <R as RenderBackend>::Frame<'_>) -> Result<(), R::Error> { |
|
| 587 | 594 | self.transform = Transform2D::identity(); |
|
| 588 | 595 | self.stack.clear(); |
|
| 589 | 596 | ||
| 590 | 597 | for op in self.render.queue.drain(..) { |
|
| 591 | 598 | match op { |
| 600 | 607 | log::error!(target: "ui", "texture layer update error: {e}"); |
|
| 601 | 608 | } |
|
| 602 | 609 | } |
|
| 603 | 610 | } |
|
| 604 | 611 | } |
|
| 605 | - | ||
| 606 | 612 | // Begin offscreen pass. |
|
| 607 | 613 | frame.begin_pass( |
|
| 608 | 614 | self.offscreen, |
|
| 609 | 615 | self.action.take().unwrap_or(PassAction::Nothing), |
|
| 610 | 616 | ); |
| 614 | 620 | // Begin onscreen pass. |
|
| 615 | 621 | // |
|
| 616 | 622 | // The size of the pass is the full backbuffer size, so we clear to that size. But |
|
| 617 | 623 | // we're only going to be drawing to a viewport that is the size of the aligned |
|
| 618 | 624 | // window size. |
|
| 619 | - | frame.begin_default_pass(PassAction::default(), self.size_viewport.map(|n| n as u32)); |
|
| 625 | + | frame.begin_default_pass(PassAction::Nothing, self.size_viewport.map(|n| n as u32)); |
|
| 620 | 626 | frame.apply_viewport(Rect::new( |
|
| 621 | 627 | [0, self.size_viewport.h as i32 - self.size.h as i32], |
|
| 622 | 628 | self.size.map(|n| n as i32), |
|
| 623 | 629 | ))?; |
|
| 624 | 630 | frame.apply_pipeline(&self.present.pipeline)?; |
ombre/src/paint/traits.rs
+4 -4
| 1 | 1 | //! Traits relating to 2D drawing. |
|
| 2 | 2 | // (c) 2019 the Piet Authors, the traits and docs here are heavily drawn from `piet`. |
|
| 3 | 3 | #![warn(missing_docs)] |
|
| 4 | 4 | ||
| 5 | - | use crate::gfx::RenderFrame; |
|
| 5 | + | use crate::gfx::RenderBackend; |
|
| 6 | 6 | use crate::gfx::{Image, Point2D, Rect, Rgba, Rgba8, Transform2D}; |
|
| 7 | 7 | use crate::math::{Local, Screen}; |
|
| 8 | 8 | use crate::text::Text; |
|
| 9 | 9 | ||
| 10 | 10 | use super::shape::{Shape, ToGeometry}; |
| 23 | 23 | ||
| 24 | 24 | /// Objects that can be painted to the screen using a [`PaintSurface`] can implement |
|
| 25 | 25 | /// this trait. |
|
| 26 | 26 | pub trait Paint { |
|
| 27 | 27 | /// Paint the object using a [`PaintSurface`]. |
|
| 28 | - | fn paint(&self, surface: &mut impl PaintSurface) { |
|
| 28 | + | fn paint<R: RenderBackend>(&self, surface: &mut impl PaintSurface<R>) { |
|
| 29 | 29 | for op in self.ops() { |
|
| 30 | 30 | surface.op(op); |
|
| 31 | 31 | } |
|
| 32 | 32 | } |
|
| 33 | 33 | /// Return the operations required to paint this object. |
| 35 | 35 | } |
|
| 36 | 36 | ||
| 37 | 37 | /// Provides an API for drawing 2D graphics to a surface or recording context. |
|
| 38 | 38 | /// Implementations can target different rendering backends while providing |
|
| 39 | 39 | /// a consistent interface for drawing operations. |
|
| 40 | - | pub trait PaintSurface { |
|
| 40 | + | pub trait PaintSurface<R: RenderBackend> { |
|
| 41 | 41 | /// Reports any internal errors that occurred during rendering. |
|
| 42 | 42 | /// |
|
| 43 | 43 | /// Drawing operations may cause internal errors, which may also occur |
|
| 44 | 44 | /// asynchronously after the drawing command was issued. This method reports |
|
| 45 | 45 | /// any such error that has been detected. |
| 97 | 97 | /// Returns an error if there is no state to pop. |
|
| 98 | 98 | fn restore(&mut self); |
|
| 99 | 99 | ||
| 100 | 100 | /// Flush pending draw operations to the display. This should generally be called once |
|
| 101 | 101 | /// per frame. |
|
| 102 | - | fn flush<'f, F: RenderFrame<'f>>(&mut self, frame: &mut F) -> Result<(), F::Error>; |
|
| 102 | + | fn flush(&mut self, frame: &mut <R as RenderBackend>::Frame<'_>) -> Result<(), R::Error>; |
|
| 103 | 103 | ||
| 104 | 104 | /// Apply a transform. |
|
| 105 | 105 | /// |
|
| 106 | 106 | /// Apply a 2D transformation. The transformation remains in effect |
|
| 107 | 107 | /// until a [`Self::restore`] operation. |
ombre/src/ui/interface.rs
+24 -12
| 55 | 55 | } |
|
| 56 | 56 | } |
|
| 57 | 57 | ||
| 58 | 58 | /// User interface state. Wraps the root widget with additional state |
|
| 59 | 59 | /// required to render the interface. |
|
| 60 | - | pub struct Interface<W, T, const N: usize = { canvas::DEFAULT_MAX_TEXTURE_LAYERS }> { |
|
| 60 | + | pub struct Interface<W, T, R, const N: usize = { canvas::DEFAULT_MAX_TEXTURE_LAYERS }> |
|
| 61 | + | where |
|
| 62 | + | R: RenderBackend, |
|
| 63 | + | { |
|
| 61 | 64 | /// Root of the widget tree. |
|
| 62 | 65 | root: widgets::Pod<W, T>, |
|
| 63 | 66 | /// Paint surface. |
|
| 64 | - | canvas: Canvas, |
|
| 67 | + | canvas: Canvas<R>, |
|
| 65 | 68 | /// Cursor position. |
|
| 66 | 69 | cursor: Point2D<f32, Screen>, |
|
| 67 | 70 | /// Current window state. |
|
| 68 | 71 | window: WindowCtx, |
|
| 69 | 72 | /// Loaded cursors. |
| 72 | 75 | debug: bool, |
|
| 73 | 76 | /// Widget data type witness. |
|
| 74 | 77 | data: PhantomData<T>, |
|
| 75 | 78 | } |
|
| 76 | 79 | ||
| 77 | - | impl<W, T> Deref for Interface<W, T> { |
|
| 80 | + | impl<W, T, R> Deref for Interface<W, T, R> |
|
| 81 | + | where |
|
| 82 | + | R: RenderBackend, |
|
| 83 | + | { |
|
| 78 | 84 | type Target = W; |
|
| 79 | 85 | ||
| 80 | 86 | fn deref(&self) -> &Self::Target { |
|
| 81 | 87 | &self.root.child |
|
| 82 | 88 | } |
|
| 83 | 89 | } |
|
| 84 | 90 | ||
| 85 | - | impl<W, T> DerefMut for Interface<W, T> { |
|
| 91 | + | impl<W, T, R> DerefMut for Interface<W, T, R> |
|
| 92 | + | where |
|
| 93 | + | R: RenderBackend, |
|
| 94 | + | { |
|
| 86 | 95 | fn deref_mut(&mut self) -> &mut Self::Target { |
|
| 87 | 96 | &mut self.root.child |
|
| 88 | 97 | } |
|
| 89 | 98 | } |
|
| 90 | 99 | ||
| 91 | - | impl<T, W: Widget<T>> Interface<W, T> { |
|
| 100 | + | impl<W: Widget<T>, T, R> Interface<W, T, R> |
|
| 101 | + | where |
|
| 102 | + | R: RenderBackend, |
|
| 103 | + | { |
|
| 92 | 104 | /// Get the root of the widget tree. |
|
| 93 | 105 | pub fn root(&self) -> &W { |
|
| 94 | 106 | &self.root.child |
|
| 95 | 107 | } |
|
| 96 | 108 | ||
| 97 | 109 | /// UI Event. Call this for every event received. |
|
| 98 | - | pub fn event<R: RenderBackend>( |
|
| 110 | + | pub fn event( |
|
| 99 | 111 | &mut self, |
|
| 100 | 112 | event: WindowEvent, |
|
| 101 | 113 | data: &mut T, |
|
| 102 | 114 | backend: &mut R, |
|
| 103 | 115 | window: &mut platform::Window, |
| 181 | 193 | ||
| 182 | 194 | Ok(()) |
|
| 183 | 195 | } |
|
| 184 | 196 | ||
| 185 | 197 | /// UI frame. Call this on every frame. |
|
| 186 | - | pub fn frame<'f, F: RenderFrame<'f>>( |
|
| 187 | - | &mut self, |
|
| 198 | + | pub fn frame<'f>( |
|
| 199 | + | &'f mut self, |
|
| 188 | 200 | data: &T, |
|
| 189 | - | frame: &mut F, |
|
| 190 | - | ) -> Result<(), F::Error> { |
|
| 201 | + | frame: &mut <R as RenderBackend>::Frame<'f>, |
|
| 202 | + | ) -> Result<(), R::Error> { |
|
| 191 | 203 | let ctx = self.context(); |
|
| 192 | 204 | let mut ops = Vec::new(); // TODO: Used fixed size array. |
|
| 193 | 205 | ||
| 206 | + | self.canvas.clear(); |
|
| 194 | 207 | self.root.layout(data, ctx.as_layout(self.canvas.fonts())); |
|
| 195 | 208 | self.root.paint(data, ctx.as_paint(&mut ops)); |
|
| 196 | 209 | self.canvas.ops(ops.drain(..)); |
|
| 197 | 210 | self.canvas.flush(frame)?; |
|
| 198 | - | self.canvas.clear(); |
|
| 199 | 211 | ||
| 200 | 212 | Ok(()) |
|
| 201 | 213 | } |
|
| 202 | 214 | ||
| 203 | 215 | /// Create a context object. |
| 266 | 278 | self.debug = debug; |
|
| 267 | 279 | self |
|
| 268 | 280 | } |
|
| 269 | 281 | ||
| 270 | 282 | /// Build the interface by giving it the root of the widget tree. |
|
| 271 | - | pub fn build<T, W: Widget<T>>(self, root: W) -> Result<Interface<W, T>, Error<R>> { |
|
| 283 | + | pub fn build<T, W: Widget<T>>(self, root: W) -> Result<Interface<W, T, R>, Error<R>> { |
|
| 272 | 284 | let canvas = self |
|
| 273 | 285 | .canvas |
|
| 274 | 286 | .scale(self.scale) |
|
| 275 | 287 | .config(canvas::Config { |
|
| 276 | 288 | max_vertices: self.config.max_vertices, |