summaryrefslogtreecommitdiffstats
path: root/wgpu/src/image
diff options
context:
space:
mode:
authorLibravatar Malte Veerman <malte.veerman@gmail.com>2020-01-16 14:03:46 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-25 13:26:50 +0100
commit3f388351054c0961923b5f78e7dcf42289565a48 (patch)
treedaa82af28acc644da48ffd5209599521816bfe09 /wgpu/src/image
parent2f77a6bf5ac1b657c1f54ea0b589b1e115b95e6b (diff)
downloadiced-3f388351054c0961923b5f78e7dcf42289565a48.tar.gz
iced-3f388351054c0961923b5f78e7dcf42289565a48.tar.bz2
iced-3f388351054c0961923b5f78e7dcf42289565a48.zip
Implement allocating large images across multiple texture array layers.
Diffstat (limited to 'wgpu/src/image')
-rw-r--r--wgpu/src/image/raster.rs35
-rw-r--r--wgpu/src/image/vector.rs27
2 files changed, 22 insertions, 40 deletions
diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs
index 32afb749..387bd23a 100644
--- a/wgpu/src/image/raster.rs
+++ b/wgpu/src/image/raster.rs
@@ -1,19 +1,15 @@
-use crate::image::AtlasArray;
+use crate::image::{TextureArray, ImageAllocation};
use iced_native::image;
use std::{
collections::{HashMap, HashSet},
};
-use guillotiere::{Allocation, Size};
+use guillotiere::Size;
use debug_stub_derive::*;
#[derive(DebugStub)]
pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
- Device {
- layer: u32,
- #[debug_stub="ReplacementValue"]
- allocation: Allocation,
- },
+ Device(ImageAllocation),
NotFound,
Invalid,
}
@@ -22,10 +18,7 @@ impl Memory {
pub fn dimensions(&self) -> (u32, u32) {
match self {
Memory::Host(image) => image.dimensions(),
- Memory::Device { allocation, .. } => {
- let size = &allocation.rectangle.size();
- (size.width as u32, size.height as u32)
- },
+ Memory::Device(allocation) => allocation.size(),
Memory::NotFound => (1, 1),
Memory::Invalid => (1, 1),
}
@@ -77,7 +70,7 @@ impl Cache {
handle: &image::Handle,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
- atlas_array: &mut AtlasArray,
+ atlas_array: &mut TextureArray,
) -> &Memory {
let _ = self.load(handle);
@@ -87,29 +80,23 @@ impl Cache {
let (width, height) = image.dimensions();
let size = Size::new(width as i32, height as i32);
- let (layer, allocation) = atlas_array.allocate(size).unwrap_or_else(|| {
- atlas_array.grow(1, device, encoder);
- atlas_array.allocate(size).unwrap()
- });
+ let allocation = atlas_array.allocate(size);
- let flat_samples = image.as_flat_samples();
- let slice = flat_samples.as_slice();
+ atlas_array.upload(image, &allocation, device, encoder);
- atlas_array.upload(slice, layer, &allocation, device, encoder);
-
- *memory = Memory::Device { layer, allocation };
+ *memory = Memory::Device(allocation);
}
memory
}
- pub fn trim(&mut self, atlas_array: &mut AtlasArray) {
+ pub fn trim(&mut self, texture_array: &mut TextureArray) {
let hits = &self.hits;
for (id, mem) in &self.map {
- if let Memory::Device { layer, allocation } = mem {
+ if let Memory::Device(allocation) = mem {
if !hits.contains(&id) {
- atlas_array.deallocate(*layer, allocation);
+ texture_array.deallocate(allocation);
}
}
}
diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs
index 2972bc4a..a83d1a0a 100644
--- a/wgpu/src/image/vector.rs
+++ b/wgpu/src/image/vector.rs
@@ -1,9 +1,9 @@
-use crate::image::AtlasArray;
+use crate::image::{TextureArray, ImageAllocation};
use iced_native::svg;
use std::{
collections::{HashMap, HashSet},
};
-use guillotiere::{Allocation, Size};
+use guillotiere::Size;
use debug_stub_derive::*;
#[derive(DebugStub)]
@@ -32,7 +32,7 @@ impl Svg {
pub struct Cache {
svgs: HashMap<u64, Svg>,
#[debug_stub="ReplacementValue"]
- rasterized: HashMap<(u64, u32, u32), (u32, Allocation)>,
+ rasterized: HashMap<(u64, u32, u32), ImageAllocation>,
svg_hits: HashSet<u64>,
rasterized_hits: HashSet<(u64, u32, u32)>,
}
@@ -70,8 +70,8 @@ impl Cache {
scale: f32,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
- atlas_array: &mut AtlasArray,
- ) -> Option<&(u32, Allocation)> {
+ texture_array: &mut TextureArray,
+ ) -> Option<&ImageAllocation> {
let id = handle.id();
let (width, height) = (
@@ -100,10 +100,7 @@ impl Cache {
let size = Size::new(width as i32, height as i32);
- let (layer, allocation) = atlas_array.allocate(size).unwrap_or_else(|| {
- atlas_array.grow(1, device, encoder);
- atlas_array.allocate(size).unwrap()
- });
+ let array_allocation = texture_array.allocate(size);
// TODO: Optimize!
// We currently rerasterize the SVG when its size changes. This is slow
@@ -124,15 +121,13 @@ impl Cache {
&mut canvas,
);
- let slice = canvas.get_data();
-
- atlas_array.upload(slice, layer, &allocation, device, encoder);
+ texture_array.upload(&canvas, &array_allocation, device, encoder);
let _ = self.svg_hits.insert(id);
let _ = self.rasterized_hits.insert((id, width, height));
let _ = self
.rasterized
- .insert((id, width, height), (layer, allocation));
+ .insert((id, width, height), array_allocation);
self.rasterized.get(&(id, width, height))
}
@@ -140,13 +135,13 @@ impl Cache {
}
}
- pub fn trim(&mut self, atlas_array: &mut AtlasArray) {
+ pub fn trim(&mut self, texture_array: &mut TextureArray) {
let svg_hits = &self.svg_hits;
let rasterized_hits = &self.rasterized_hits;
- for (k, (layer, allocation)) in &self.rasterized {
+ for (k, allocation) in &self.rasterized {
if !rasterized_hits.contains(k) {
- atlas_array.deallocate(*layer, allocation);
+ texture_array.deallocate(allocation);
}
}