Merge branch 'master' into feature/test-recorder

This commit is contained in:
Héctor Ramón Jiménez 2025-09-11 04:57:17 +02:00
commit a052ce58b0
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
69 changed files with 1555 additions and 833 deletions

View file

@ -235,13 +235,13 @@ impl Layer {
pub fn draw_primitive(
&mut self,
bounds: Rectangle,
primitive: Box<dyn Primitive>,
primitive: impl Primitive,
transformation: Transformation,
) {
let bounds = bounds * transformation;
self.primitives
.push(primitive::Instance { bounds, primitive });
.push(primitive::Instance::new(bounds, primitive));
}
fn flush_meshes(&mut self) {

View file

@ -302,7 +302,7 @@ impl Renderer {
encoder: &mut wgpu::CommandEncoder,
viewport: &Viewport,
) {
let scale_factor = viewport.scale_factor() as f32;
let scale_factor = viewport.scale_factor();
self.text_viewport
.update(&self.engine.queue, viewport.physical_size());
@ -365,10 +365,10 @@ impl Renderer {
for instance in &layer.primitives {
instance.primitive.prepare(
&mut primitive_storage,
&self.engine.device,
&self.engine.queue,
self.engine.format,
&mut primitive_storage,
&instance.bounds,
viewport,
);
@ -464,7 +464,7 @@ impl Renderer {
#[cfg(any(feature = "svg", feature = "image"))]
let image_cache = self.image_cache.borrow();
let scale_factor = viewport.scale_factor() as f32;
let scale_factor = viewport.scale_factor();
let physical_bounds = Rectangle::<f32>::from(Rectangle::with_size(
viewport.physical_size(),
));
@ -534,7 +534,6 @@ impl Renderer {
if !layer.primitives.is_empty() {
let render_span = debug::render(debug::Primitive::Shader);
let _ = ManuallyDrop::into_inner(render_pass);
let primitive_storage = self
.engine
@ -542,41 +541,91 @@ impl Renderer {
.read()
.expect("Read primitive storage");
let mut need_render = Vec::new();
for instance in &layer.primitives {
let bounds = instance.bounds * scale;
if let Some(clip_bounds) = (instance.bounds * scale)
.intersection(&physical_bounds)
.and_then(Rectangle::snap)
{
render_pass.set_viewport(
bounds.x,
bounds.y,
bounds.width,
bounds.height,
0.0,
1.0,
);
render_pass.set_scissor_rect(
clip_bounds.x,
clip_bounds.y,
clip_bounds.width,
clip_bounds.height,
);
let drawn = instance
.primitive
.draw(&primitive_storage, &mut render_pass);
if !drawn {
need_render.push((instance, clip_bounds));
}
}
}
render_pass.set_viewport(
0.0,
0.0,
viewport.physical_width() as f32,
viewport.physical_height() as f32,
0.0,
1.0,
);
render_pass.set_scissor_rect(
0,
0,
viewport.physical_width(),
viewport.physical_height(),
);
if !need_render.is_empty() {
let _ = ManuallyDrop::into_inner(render_pass);
for (instance, clip_bounds) in need_render {
instance.primitive.render(
encoder,
&primitive_storage,
encoder,
frame,
&clip_bounds,
);
}
render_pass = ManuallyDrop::new(encoder.begin_render_pass(
&wgpu::RenderPassDescriptor {
label: Some("iced_wgpu render pass"),
color_attachments: &[Some(
wgpu::RenderPassColorAttachment {
view: frame,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
},
)],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
},
));
}
render_span.finish();
render_pass = ManuallyDrop::new(encoder.begin_render_pass(
&wgpu::RenderPassDescriptor {
label: Some("iced_wgpu render pass"),
color_attachments: &[Some(
wgpu::RenderPassColorAttachment {
view: frame,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
},
)],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
},
));
}
#[cfg(any(feature = "svg", feature = "image"))]
@ -804,7 +853,7 @@ impl graphics::geometry::Renderer for Renderer {
impl primitive::Renderer for Renderer {
fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive) {
let (layer, transformation) = self.layers.current_mut();
layer.draw_primitive(bounds, Box::new(primitive), transformation);
layer.draw_primitive(bounds, primitive, transformation);
}
}
@ -878,7 +927,7 @@ impl renderer::Headless for Renderer {
background_color: Color,
) -> Vec<u8> {
self.screenshot(
&Viewport::with_physical_size(size, f64::from(scale_factor)),
&Viewport::with_physical_size(size, scale_factor),
background_color,
)
}

View file

@ -1,6 +1,7 @@
//! Draw custom primitives.
use crate::core::{self, Rectangle};
use crate::graphics::Viewport;
use crate::graphics::futures::{MaybeSend, MaybeSync};
use rustc_hash::FxHashMap;
use std::any::{Any, TypeId};
@ -10,36 +11,172 @@ use std::fmt::Debug;
pub type Batch = Vec<Instance>;
/// 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(
pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
/// The shared renderer of this [`Primitive`].
///
/// Normally, this will contain a bunch of [`wgpu`] state; like
/// a rendering pipeline, buffers, and textures.
///
/// All instances of this [`Primitive`] type will share the same
/// [`Renderer`].
type Renderer: MaybeSend + MaybeSync;
/// Initializes the [`Renderer`](Self::Renderer) of the [`Primitive`].
///
/// This will only be called once, when the first [`Primitive`] of this kind
/// is encountered.
fn initialize(
&self,
device: &wgpu::Device,
queue: &wgpu::Queue,
format: wgpu::TextureFormat,
storage: &mut Storage,
) -> Self::Renderer;
/// Processes the [`Primitive`], allowing for GPU buffer allocation.
fn prepare(
&self,
renderer: &mut Self::Renderer,
device: &wgpu::Device,
queue: &wgpu::Queue,
bounds: &Rectangle,
viewport: &Viewport,
);
/// Renders the [`Primitive`].
/// Draws the [`Primitive`] in the given [`wgpu::RenderPass`].
///
/// When possible, this should be implemented over [`render`](Self::render)
/// since reusing the existing render pass should be considerably more
/// efficient than issuing a new one.
///
/// The viewport and scissor rect of the render pass provided is set
/// to the bounds and clip bounds of the [`Primitive`], respectively.
///
/// If you have complex composition needs, then you can leverage
/// [`render`](Self::render) by returning `false` here.
///
/// By default, it does nothing and returns `false`.
fn draw(
&self,
_renderer: &Self::Renderer,
_render_pass: &mut wgpu::RenderPass<'_>,
) -> bool {
false
}
/// Renders the [`Primitive`], using the given [`wgpu::CommandEncoder`].
///
/// This will only be called if [`draw`](Self::draw) returns `false`.
///
/// By default, it does nothing.
fn render(
&self,
encoder: &mut wgpu::CommandEncoder,
_renderer: &Self::Renderer,
_encoder: &mut wgpu::CommandEncoder,
_target: &wgpu::TextureView,
_clip_bounds: &Rectangle<u32>,
) {
}
}
pub(crate) trait Stored:
Debug + MaybeSend + MaybeSync + 'static
{
fn prepare(
&self,
storage: &mut Storage,
device: &wgpu::Device,
queue: &wgpu::Queue,
format: wgpu::TextureFormat,
bounds: &Rectangle,
viewport: &Viewport,
);
fn draw(
&self,
storage: &Storage,
render_pass: &mut wgpu::RenderPass<'_>,
) -> bool;
fn render(
&self,
storage: &Storage,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
clip_bounds: &Rectangle<u32>,
);
}
#[derive(Debug)]
struct BlackBox<P: Primitive> {
primitive: P,
}
impl<P: Primitive> Stored for BlackBox<P> {
fn prepare(
&self,
storage: &mut Storage,
device: &wgpu::Device,
queue: &wgpu::Queue,
format: wgpu::TextureFormat,
bounds: &Rectangle,
viewport: &Viewport,
) {
if !storage.has::<P>() {
storage.store::<P, _>(
self.primitive.initialize(device, queue, format),
);
}
let renderer = storage
.get_mut::<P>()
.expect("renderer should be initialized")
.downcast_mut::<P::Renderer>()
.expect("renderer should have the proper type");
self.primitive
.prepare(renderer, device, queue, bounds, viewport);
}
fn draw(
&self,
storage: &Storage,
render_pass: &mut wgpu::RenderPass<'_>,
) -> bool {
let renderer = storage
.get::<P>()
.expect("renderer should be initialized")
.downcast_ref::<P::Renderer>()
.expect("renderer should have the proper type");
self.primitive.draw(renderer, render_pass)
}
fn render(
&self,
storage: &Storage,
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::TextureView,
clip_bounds: &Rectangle<u32>,
) {
let renderer = storage
.get::<P>()
.expect("renderer should be initialized")
.downcast_ref::<P::Renderer>()
.expect("renderer should have the proper type");
self.primitive
.render(renderer, encoder, target, clip_bounds);
}
}
#[derive(Debug)]
/// An instance of a specific [`Primitive`].
pub struct Instance {
/// The bounds of the [`Instance`].
pub bounds: Rectangle,
pub(crate) bounds: Rectangle,
/// The [`Primitive`] to render.
pub primitive: Box<dyn Primitive>,
pub(crate) primitive: Box<dyn Stored>,
}
impl Instance {
@ -47,7 +184,7 @@ impl Instance {
pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
Instance {
bounds,
primitive: Box::new(primitive),
primitive: Box::new(BlackBox { primitive }),
}
}
}
@ -59,9 +196,10 @@ pub trait Renderer: core::Renderer {
}
/// Stores custom, user-provided types.
#[derive(Default, Debug)]
#[derive(Default)]
#[allow(missing_debug_implementations)]
pub struct Storage {
pipelines: FxHashMap<TypeId, Box<dyn Any + Send + Sync>>,
pipelines: FxHashMap<TypeId, Box<dyn AnyConcurrent>>,
}
impl Storage {
@ -71,25 +209,28 @@ impl Storage {
}
/// Inserts the data `T` in to [`Storage`].
pub fn store<T: 'static + Send + Sync>(&mut self, data: T) {
pub fn store<T: 'static, D: Any + MaybeSend + MaybeSync>(
&mut self,
data: D,
) {
let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
}
/// 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("Value with this type does not exist in Storage.")
})
pub fn get<T: 'static>(&self) -> Option<&dyn Any> {
self.pipelines
.get(&TypeId::of::<T>())
.map(|pipeline| pipeline.as_ref() as &dyn Any)
}
/// 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("Value with this type does not exist in Storage.")
})
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut dyn Any> {
self.pipelines
.get_mut(&TypeId::of::<T>())
.map(|pipeline| pipeline.as_mut() as &mut dyn Any)
}
}
trait AnyConcurrent: Any + MaybeSend + MaybeSync {}
impl<T> AnyConcurrent for T where T: Any + MaybeSend + MaybeSync {}

View file

@ -332,7 +332,7 @@ impl graphics::Compositor for Compositor {
);
}
fn fetch_information(&self) -> compositor::Information {
fn information(&self) -> compositor::Information {
let information = self.adapter.get_info();
compositor::Information {