Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cull images and free VRAM #163

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ pub struct Rectangle<T = f32> {
pub height: T,
}

impl<T: std::ops::Add<Output = T> + PartialOrd + Copy> Rectangle<T> {
/// Returns true if the current [`Rectangle`] intersects with the given one.
///
/// [`Rectangle`]: struct.Rectangle.html
pub fn intersects(&self, b: &Rectangle<T>) -> bool {
self.x < b.x + b.width
&& self.x + self.width > b.x
&& self.y < b.y + b.height
&& self.y + self.height > b.y
}
}

impl Rectangle<f32> {
/// Returns true if the given [`Point`] is contained in the [`Rectangle`].
///
Expand Down
3 changes: 1 addition & 2 deletions wgpu/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,8 @@ impl Pipeline {
let uploaded_texture = match &image.handle {
Handle::Raster(handle) => {
let mut cache = self.raster_cache.borrow_mut();
let memory = cache.load(&handle);

memory.upload(device, encoder, &self.texture_layout)
cache.upload(handle, device, encoder, &self.texture_layout)
}
Handle::Vector(_handle) => {
#[cfg(feature = "svg")]
Expand Down
122 changes: 81 additions & 41 deletions wgpu/src/image/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use std::{
rc::Rc,
};

type ImageBuffer = ::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>;

#[derive(Debug)]
pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
Host(Rc<ImageBuffer>),
Device {
image: Rc<ImageBuffer>,
bind_group: Rc<wgpu::BindGroup>,
width: u32,
height: u32,
Expand All @@ -25,14 +28,68 @@ impl Memory {
Memory::Invalid => (1, 1),
}
}
}

#[derive(Debug)]
pub struct Cache {
map: HashMap<u64, Memory>,
load_hits: HashSet<u64>,
upload_hits: HashSet<u64>,
is_dirty: bool,
}

impl Cache {
pub fn new() -> Self {
Self {
map: HashMap::new(),
load_hits: HashSet::new(),
upload_hits: HashSet::new(),
is_dirty: false,
}
}

pub fn load(&mut self, handle: &image::Handle) -> &mut Memory {
if self.contains(handle) {
return self.get(handle).unwrap();
}

let memory = match handle.data() {
image::Data::Path(path) => {
if let Ok(image) = ::image::open(path) {
Memory::Host(Rc::new(image.to_bgra()))
} else {
Memory::NotFound
}
}
image::Data::Bytes(bytes) => {
if let Ok(image) = ::image::load_from_memory(&bytes) {
Memory::Host(Rc::new(image.to_bgra()))
} else {
Memory::Invalid
}
}
};

self.insert(handle, memory);
self.is_dirty = true;

self.get(handle).unwrap()
}

pub fn upload(
&mut self,
handle: &image::Handle,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
texture_layout: &wgpu::BindGroupLayout,
) -> Option<Rc<wgpu::BindGroup>> {
match self {
let memory = {
let _ = self.upload_hits.insert(handle.id());

self.load(handle)
};

match memory {
Memory::Host(image) => {
let (width, height) = image.dimensions();

Expand Down Expand Up @@ -98,7 +155,8 @@ impl Memory {

let bind_group = Rc::new(bind_group);

*self = Memory::Device {
*memory = Memory::Device {
image: image.clone(),
bind_group: bind_group.clone(),
width,
height,
Expand All @@ -111,57 +169,39 @@ impl Memory {
Memory::Invalid => None,
}
}
}

#[derive(Debug)]
pub struct Cache {
map: HashMap<u64, Memory>,
hits: HashSet<u64>,
}

impl Cache {
pub fn new() -> Self {
Self {
map: HashMap::new(),
hits: HashSet::new(),
pub fn trim(&mut self) {
if !self.is_dirty {
return;
}
}

pub fn load(&mut self, handle: &image::Handle) -> &mut Memory {
if self.contains(handle) {
return self.get(handle).unwrap();
}
let load_hits = &self.load_hits;
let upload_hits = &self.upload_hits;

let memory = match handle.data() {
image::Data::Path(path) => {
if let Ok(image) = ::image::open(path) {
Memory::Host(image.to_bgra())
} else {
Memory::NotFound
}
self.map.retain(|k, memory| {
if upload_hits.contains(k) {
return true;
}
image::Data::Bytes(bytes) => {
if let Ok(image) = ::image::load_from_memory(&bytes) {
Memory::Host(image.to_bgra())
} else {
Memory::Invalid

let retain = load_hits.contains(k);

if retain {
if let Memory::Device { image, .. } = memory {
*memory = Memory::Host(image.clone());
}
}
};

self.insert(handle, memory);
self.get(handle).unwrap()
}
retain
});

pub fn trim(&mut self) {
let hits = &self.hits;
self.load_hits.clear();
self.upload_hits.clear();

self.map.retain(|k, _| hits.contains(k));
self.hits.clear();
self.is_dirty = false;
}

fn get(&mut self, handle: &image::Handle) -> Option<&mut Memory> {
let _ = self.hits.insert(handle.id());
let _ = self.load_hits.insert(handle.id());

self.map.get_mut(&handle.id())
}
Expand Down
21 changes: 16 additions & 5 deletions wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ impl<'a> Layer<'a> {
meshes: Vec::new(),
}
}

pub fn visible_bounds(&self) -> Rectangle {
Rectangle {
x: (self.bounds.x + self.offset.x) as f32,
y: (self.bounds.y + self.offset.y) as f32,
width: self.bounds.width as f32,
height: self.bounds.height as f32,
}
}
}

impl Renderer {
Expand Down Expand Up @@ -244,11 +253,13 @@ impl Renderer {
});
}
Primitive::Image { handle, bounds } => {
layer.images.push(Image {
handle: image::Handle::Raster(handle.clone()),
position: [bounds.x, bounds.y],
scale: [bounds.width, bounds.height],
});
if bounds.intersects(&layer.visible_bounds()) {
layer.images.push(Image {
handle: image::Handle::Raster(handle.clone()),
position: [bounds.x, bounds.y],
scale: [bounds.width, bounds.height],
});
}
}
Primitive::Svg { handle, bounds } => {
layer.images.push(Image {
Expand Down