From 27b099c895825ee03555fd7bdaa8cefdb2125ead Mon Sep 17 00:00:00 2001
From: cel 🌸 <cel@bunny.garden>
Date: Thu, 20 Mar 2025 11:57:26 +0000
Subject: WIP: background image support

---
 wgpu/src/lib.rs                   |  2 ++
 wgpu/src/quad.rs                  | 16 ++++++++++++++++
 wgpu/src/quad/background_image.rs | 31 +++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 wgpu/src/quad/background_image.rs

(limited to 'wgpu')

diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index 2ef9992c..875cbcde 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -163,6 +163,8 @@ impl Renderer {
                     device,
                     encoder,
                     &mut engine.staging_belt,
+                    #[cfg(any(feature = "svg", feature = "image"))]
+                    &mut self.image_cache.borrow_mut(),
                     &layer.quads,
                     viewport.projection(),
                     scale_factor,
diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs
index de432d2f..311cd81f 100644
--- a/wgpu/src/quad.rs
+++ b/wgpu/src/quad.rs
@@ -1,12 +1,15 @@
+mod background_image;
 mod gradient;
 mod solid;
 
+use background_image::BackgroundImage;
 use gradient::Gradient;
 use solid::Solid;
 
 use crate::core::{Background, Rectangle, Transformation};
 use crate::graphics;
 use crate::graphics::color;
+use crate::image::Cache;
 
 use bytemuck::{Pod, Zeroable};
 
@@ -47,6 +50,7 @@ pub struct Quad {
 pub struct Pipeline {
     solid: solid::Pipeline,
     gradient: gradient::Pipeline,
+    background_image: background_image::Pipeline,
     constant_layout: wgpu::BindGroupLayout,
     layers: Vec<Layer>,
     prepare_layer: usize,
@@ -85,6 +89,7 @@ impl Pipeline {
         device: &wgpu::Device,
         encoder: &mut wgpu::CommandEncoder,
         belt: &mut wgpu::util::StagingBelt,
+        #[cfg(any(feature = "svg", feature = "image"))] cache: &mut Cache,
         quads: &Batch,
         transformation: Transformation,
         scale: f32,
@@ -139,6 +144,7 @@ impl Pipeline {
 
                         gradient_offset += count;
                     }
+                    Kind::Image => todo!(),
                 }
             }
         }
@@ -238,6 +244,9 @@ pub struct Batch {
     /// The gradient quads of the [`Layer`].
     gradients: Vec<Gradient>,
 
+    /// The image quads of the [`Layer`].
+    images: Vec<BackgroundImage>,
+
     /// The quad order of the [`Layer`].
     order: Order,
 }
@@ -273,6 +282,11 @@ impl Batch {
 
                 Kind::Gradient
             }
+            Background::Image(background_image) => {
+                self.images.push(BackgroundImage { quad });
+
+                Kind::Image
+            }
         };
 
         match self.order.last_mut() {
@@ -299,6 +313,8 @@ enum Kind {
     Solid,
     /// A gradient quad
     Gradient,
+    /// A background image quad
+    Image,
 }
 
 fn color_target_state(
diff --git a/wgpu/src/quad/background_image.rs b/wgpu/src/quad/background_image.rs
new file mode 100644
index 00000000..b9217686
--- /dev/null
+++ b/wgpu/src/quad/background_image.rs
@@ -0,0 +1,31 @@
+use crate::Buffer;
+use crate::graphics::image;
+use crate::quad::{self, Quad};
+
+use bytemuck::{Pod, Zeroable};
+
+/// A quad filled with a background image.
+#[derive(Clone, Debug)]
+#[repr(C)]
+pub struct BackgroundImage {
+    /// The image data of the quad
+    // TODO: representable image background
+    pub background_image: image::Image,
+
+    /// The [`Quad`] data of the [`BackgroundImage`].
+    pub quad: Quad,
+}
+
+pub struct InnerBackgroundImage {
+    image: image::Image,
+    attachment: Attachment,
+    clip: Sizing,
+}
+
+#[derive(Debug)]
+pub struct Pipeline {
+    pipeline: crate::image::Pipeline,
+    constant_layout: wgpu::BindGroupLayout,
+    layers: Vec<Layer>,
+    prepare_layer: usize,
+}
-- 
cgit