iced_wgpu/
primitive.rs
1use crate::core::{self, Rectangle};
3use crate::graphics::Viewport;
4
5use rustc_hash::FxHashMap;
6use std::any::{Any, TypeId};
7use std::fmt::Debug;
8
9pub type Batch = Vec<Instance>;
11
12pub trait Primitive: Debug + Send + Sync + 'static {
14 fn prepare(
16 &self,
17 device: &wgpu::Device,
18 queue: &wgpu::Queue,
19 format: wgpu::TextureFormat,
20 storage: &mut Storage,
21 bounds: &Rectangle,
22 viewport: &Viewport,
23 );
24
25 fn render(
27 &self,
28 encoder: &mut wgpu::CommandEncoder,
29 storage: &Storage,
30 target: &wgpu::TextureView,
31 clip_bounds: &Rectangle<u32>,
32 );
33}
34
35#[derive(Debug)]
36pub struct Instance {
38 pub bounds: Rectangle,
40
41 pub primitive: Box<dyn Primitive>,
43}
44
45impl Instance {
46 pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
48 Instance {
49 bounds,
50 primitive: Box::new(primitive),
51 }
52 }
53}
54
55pub trait Renderer: core::Renderer {
57 fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive);
59}
60
61#[derive(Default, Debug)]
63pub struct Storage {
64 pipelines: FxHashMap<TypeId, Box<dyn Any + Send>>,
65}
66
67impl Storage {
68 pub fn has<T: 'static>(&self) -> bool {
70 self.pipelines.contains_key(&TypeId::of::<T>())
71 }
72
73 pub fn store<T: 'static + Send>(&mut self, data: T) {
75 let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
76 }
77
78 pub fn get<T: 'static>(&self) -> Option<&T> {
80 self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
81 pipeline
82 .downcast_ref::<T>()
83 .expect("Value with this type does not exist in Storage.")
84 })
85 }
86
87 pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
89 self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
90 pipeline
91 .downcast_mut::<T>()
92 .expect("Value with this type does not exist in Storage.")
93 })
94 }
95}