summaryrefslogtreecommitdiffstats
path: root/wgpu/src/primitive
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-08 15:04:35 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-08 15:04:35 +0200
commitd922b478156488a7bc03c6e791e05c040d702634 (patch)
tree767e9b9fa2c6527a0b3e3b3dd1c21b29cd533ee8 /wgpu/src/primitive
parent6ea763c2a79292e5b10be2240b4b57b920223616 (diff)
downloadiced-d922b478156488a7bc03c6e791e05c040d702634.tar.gz
iced-d922b478156488a7bc03c6e791e05c040d702634.tar.bz2
iced-d922b478156488a7bc03c6e791e05c040d702634.zip
Reintroduce support for custom primitives in `iced_wgpu`
Diffstat (limited to '')
-rw-r--r--wgpu/src/primitive.rs103
-rw-r--r--wgpu/src/primitive/pipeline.rs115
2 files changed, 89 insertions, 129 deletions
diff --git a/wgpu/src/primitive.rs b/wgpu/src/primitive.rs
index 8e311d2b..4ba1ed9a 100644
--- a/wgpu/src/primitive.rs
+++ b/wgpu/src/primitive.rs
@@ -1,20 +1,95 @@
-//! Draw using different graphical primitives.
-pub mod pipeline;
+//! Draw custom primitives.
+use crate::core::{self, Rectangle};
+use crate::graphics::Viewport;
-pub use pipeline::Pipeline;
+use rustc_hash::FxHashMap;
+use std::any::{Any, TypeId};
+use std::fmt::Debug;
-use crate::graphics::Mesh;
+/// A batch of primitives.
+pub type Batch = Vec<Instance>;
-use std::fmt::Debug;
+/// A set of methods which allows a [`Primitive`] to be rendered.
+pub trait Primitive: Debug + Send + Sync + 'static {
+ /// Processes the [`Primitive`], allowing for GPU buffer allocation.
+ fn prepare(
+ &self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ format: wgpu::TextureFormat,
+ storage: &mut Storage,
+ bounds: &Rectangle,
+ viewport: &Viewport,
+ );
+
+ /// Renders the [`Primitive`].
+ fn render(
+ &self,
+ encoder: &mut wgpu::CommandEncoder,
+ storage: &Storage,
+ target: &wgpu::TextureView,
+ clip_bounds: &Rectangle<u32>,
+ );
+}
+
+#[derive(Debug)]
+/// An instance of a specific [`Primitive`].
+pub struct Instance {
+ /// The bounds of the [`Instance`].
+ pub bounds: Rectangle,
+
+ /// The [`Primitive`] to render.
+ pub primitive: Box<dyn Primitive>,
+}
+
+impl Instance {
+ /// Creates a new [`Pipeline`] with the given [`Primitive`].
+ pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
+ Instance {
+ bounds,
+ primitive: Box::new(primitive),
+ }
+ }
+}
+
+/// A renderer than can draw custom primitives.
+pub trait Renderer: core::Renderer {
+ /// Draws a custom primitive.
+ fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive);
+}
+
+/// Stores custom, user-provided types.
+#[derive(Default, Debug)]
+pub struct Storage {
+ pipelines: FxHashMap<TypeId, Box<dyn Any + Send>>,
+}
+
+impl Storage {
+ /// Returns `true` if `Storage` contains a type `T`.
+ pub fn has<T: 'static>(&self) -> bool {
+ self.pipelines.get(&TypeId::of::<T>()).is_some()
+ }
+
+ /// Inserts the data `T` in to [`Storage`].
+ pub fn store<T: 'static + Send>(&mut self, data: T) {
+ let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
+ }
-/// The graphical primitives supported by `iced_wgpu`.
-pub type Primitive = crate::graphics::Primitive<Custom>;
+ /// Returns a reference to the data with type `T` if it exists in [`Storage`].
+ pub fn get<T: 'static>(&self) -> Option<&T> {
+ self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
+ pipeline
+ .downcast_ref::<T>()
+ .expect("Pipeline with this type does not exist in Storage.")
+ })
+ }
-/// The custom primitives supported by `iced_wgpu`.
-#[derive(Debug, Clone, PartialEq)]
-pub enum Custom {
- /// A mesh primitive.
- Mesh(Mesh),
- /// A custom pipeline primitive.
- Pipeline(Pipeline),
+ /// Returns a mutable reference to the data with type `T` if it exists in [`Storage`].
+ pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
+ self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
+ pipeline
+ .downcast_mut::<T>()
+ .expect("Pipeline with this type does not exist in Storage.")
+ })
+ }
}
diff --git a/wgpu/src/primitive/pipeline.rs b/wgpu/src/primitive/pipeline.rs
index 59c54db9..8b137891 100644
--- a/wgpu/src/primitive/pipeline.rs
+++ b/wgpu/src/primitive/pipeline.rs
@@ -1,116 +1 @@
-//! Draw primitives using custom pipelines.
-use crate::core::{self, Rectangle, Size};
-use rustc_hash::FxHashMap;
-use std::any::{Any, TypeId};
-use std::fmt::Debug;
-use std::sync::Arc;
-
-#[derive(Clone, Debug)]
-/// A custom primitive which can be used to render primitives associated with a custom pipeline.
-pub struct Pipeline {
- /// The bounds of the [`Pipeline`].
- pub bounds: Rectangle,
-
- /// The [`Primitive`] to render.
- pub primitive: Arc<dyn Primitive>,
-}
-
-impl Pipeline {
- /// Creates a new [`Pipeline`] with the given [`Primitive`].
- pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
- Pipeline {
- bounds,
- primitive: Arc::new(primitive),
- }
- }
-}
-
-impl PartialEq for Pipeline {
- fn eq(&self, other: &Self) -> bool {
- self.primitive.type_id() == other.primitive.type_id()
- }
-}
-
-/// A set of methods which allows a [`Primitive`] to be rendered.
-pub trait Primitive: Debug + Send + Sync + 'static {
- /// Processes the [`Primitive`], allowing for GPU buffer allocation.
- fn prepare(
- &self,
- format: wgpu::TextureFormat,
- device: &wgpu::Device,
- queue: &wgpu::Queue,
- bounds: Rectangle,
- target_size: Size<u32>,
- scale_factor: f32,
- storage: &mut Storage,
- );
-
- /// Renders the [`Primitive`].
- fn render(
- &self,
- storage: &Storage,
- target: &wgpu::TextureView,
- target_size: Size<u32>,
- viewport: Rectangle<u32>,
- encoder: &mut wgpu::CommandEncoder,
- );
-}
-
-/// A renderer than can draw custom pipeline primitives.
-pub trait Renderer: core::Renderer {
- /// Draws a custom pipeline primitive.
- fn draw_pipeline_primitive(
- &mut self,
- bounds: Rectangle,
- primitive: impl Primitive,
- );
-}
-
-impl Renderer for crate::Renderer {
- fn draw_pipeline_primitive(
- &mut self,
- bounds: Rectangle,
- primitive: impl Primitive,
- ) {
- self.draw_primitive(super::Primitive::Custom(super::Custom::Pipeline(
- Pipeline::new(bounds, primitive),
- )));
- }
-}
-
-/// Stores custom, user-provided pipelines.
-#[derive(Default, Debug)]
-pub struct Storage {
- pipelines: FxHashMap<TypeId, Box<dyn Any + Send>>,
-}
-
-impl Storage {
- /// Returns `true` if `Storage` contains a pipeline with type `T`.
- pub fn has<T: 'static>(&self) -> bool {
- self.pipelines.get(&TypeId::of::<T>()).is_some()
- }
-
- /// Inserts the pipeline `T` in to [`Storage`].
- pub fn store<T: 'static + Send>(&mut self, pipeline: T) {
- let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(pipeline));
- }
-
- /// Returns a reference to pipeline with type `T` if it exists in [`Storage`].
- pub fn get<T: 'static>(&self) -> Option<&T> {
- self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
- pipeline
- .downcast_ref::<T>()
- .expect("Pipeline with this type does not exist in Storage.")
- })
- }
-
- /// Returns a mutable reference to pipeline `T` if it exists in [`Storage`].
- pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
- self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
- pipeline
- .downcast_mut::<T>()
- .expect("Pipeline with this type does not exist in Storage.")
- })
- }
-}