Smaller graphics cache that removes unused items

This commit is contained in:
Imbris 2019-07-02 21:21:08 -04:00
parent aeed04fe41
commit b5ed35fd27
5 changed files with 265 additions and 129 deletions

View File

@ -282,6 +282,11 @@ impl Renderer {
model.update(&mut self.encoder, mesh, offset)
}
/// Return the maximum supported texture size.
pub fn max_texture_size(&self) -> usize {
self.factory.get_capabilities().max_texture_size
}
/// Create a new texture from the provided image.
pub fn create_texture<P: Pipeline>(
&mut self,

View File

@ -20,14 +20,18 @@ impl Cache {
const SCALE_TOLERANCE: f32 = 0.1;
const POSITION_TOLERANCE: f32 = 0.1;
let graphic_cache_dims = Vec2::new(w * 4, h * 4);
let max_texture_size = renderer.max_texture_size();
let graphic_cache_dims = Vec2::new(w * 2, h * 2).map(|e| e.min(max_texture_size as u16));
let glyph_cache_dims = Vec2::new(w, h).map(|e| e.min(max_texture_size as u16));
Ok(Self {
glyph_cache: GlyphCache::builder()
.dimensions(w as u32, h as u32)
.dimensions(glyph_cache_dims.x as u32, glyph_cache_dims.y as u32)
.scale_tolerance(SCALE_TOLERANCE)
.position_tolerance(POSITION_TOLERANCE)
.build(),
glyph_cache_tex: renderer.create_dynamic_texture((w, h).into())?,
glyph_cache_tex: renderer.create_dynamic_texture(glyph_cache_dims.map(|e| e as u16))?,
graphic_cache: GraphicCache::new(graphic_cache_dims),
graphic_cache_tex: renderer.create_dynamic_texture(graphic_cache_dims)?,
})
@ -47,8 +51,11 @@ impl Cache {
pub fn add_graphic(&mut self, graphic: Graphic) -> GraphicId {
self.graphic_cache.add_graphic(graphic)
}
pub fn clear_graphic_cache(&mut self, renderer: &mut Renderer, new_size: Vec2<u16>) {
self.graphic_cache.clear_cache(new_size);
self.graphic_cache_tex = renderer.create_dynamic_texture(new_size).unwrap();
// new_window_size is in physical pixels
pub fn clear_graphic_cache(&mut self, renderer: &mut Renderer, new_window_size: Vec2<u16>) {
let max_texture_size = renderer.max_texture_size();
let cache_size = new_window_size.map(|e| (e * 2).min(max_texture_size as u16));
self.graphic_cache.clear_cache(cache_size);
self.graphic_cache_tex = renderer.create_dynamic_texture(cache_size).unwrap();
}
}

View File

@ -1,10 +1,18 @@
use dot_vox::DotVoxData;
use fnv::FnvHashMap;
use guillotiere::{size2, Allocation, AtlasAllocator};
use image::DynamicImage;
use std::sync::Arc;
use fnv::{FnvHashMap, FnvHashSet};
use guillotiere::{size2, AllocId, Allocation, AtlasAllocator};
use image::{DynamicImage, RgbaImage};
use log::{error, warn};
use std::{
sync::{
mpsc::{channel, Receiver, Sender},
Arc,
},
thread,
};
use vek::*;
#[derive(Clone)]
pub enum Graphic {
Image(Arc<DynamicImage>),
Voxel(Arc<DotVoxData>, Option<u8>),
@ -16,19 +24,39 @@ pub struct Id(u32);
type Parameters = (Id, Vec2<u16>, Aabr<u64>);
pub struct CachedDetails {
// Id used by AtlasAllocator
alloc_id: AllocId,
// Last frame this was used on
frame: u32,
// Where in the cache texture this is
aabr: Aabr<u16>,
}
pub struct GraphicCache {
atlas: AtlasAllocator,
graphic_map: FnvHashMap<Id, Graphic>,
rect_map: FnvHashMap<Parameters, Aabr<u16>>,
next_id: u32,
atlas: AtlasAllocator,
cache_map: FnvHashMap<Parameters, CachedDetails>,
// The current frame
current_frame: u32,
unused_entries_this_frame: Option<Vec<Option<(u32, Parameters)>>>,
soft_cache: FnvHashMap<Parameters, RgbaImage>,
transfer_ready: Vec<(Parameters, Aabr<u16>)>,
}
impl GraphicCache {
pub fn new(size: Vec2<u16>) -> Self {
Self {
atlas: AtlasAllocator::new(size2(i32::from(size.x), i32::from(size.y))),
graphic_map: FnvHashMap::default(),
rect_map: FnvHashMap::default(),
next_id: 0,
atlas: AtlasAllocator::new(size2(i32::from(size.x), i32::from(size.y))),
cache_map: FnvHashMap::default(),
current_frame: 0,
unused_entries_this_frame: None,
soft_cache: FnvHashMap::default(),
transfer_ready: Vec::new(),
}
}
pub fn add_graphic(&mut self, graphic: Graphic) -> Id {
@ -44,77 +72,177 @@ impl GraphicCache {
self.graphic_map.get(&id)
}
pub fn clear_cache(&mut self, new_size: Vec2<u16>) {
self.rect_map.clear();
self.soft_cache.clear();
self.transfer_ready.clear();
self.cache_map.clear();
self.atlas = AtlasAllocator::new(size2(i32::from(new_size.x), i32::from(new_size.y)));
}
pub fn cache_res<F>(
pub fn queue_res(
&mut self,
graphic_id: Id,
dims: Vec2<u16>,
source: Aabr<f64>,
mut cacher: F,
) -> Option<Aabr<u16>>
) -> Option<Aabr<u16>> {
let key = (graphic_id, dims, source.map(|e| e.to_bits())); // TODO: Replace this with rounded representation of source
if let Some(details) = self.cache_map.get_mut(&key) {
// Update frame
details.frame = self.current_frame;
Some(details.aabr)
} else {
// Create image if it doesn't already exist
if !self.soft_cache.contains_key(&key) {
self.soft_cache.insert(
key,
match self.graphic_map.get(&graphic_id) {
Some(Graphic::Blank) => return None,
// Render image at requested resolution
// TODO: Use source aabr.
Some(Graphic::Image(ref image)) => image
.resize_exact(
u32::from(dims.x),
u32::from(dims.y),
image::FilterType::Nearest,
)
.to_rgba(),
Some(Graphic::Voxel(ref vox, min_samples)) => {
super::renderer::draw_vox(&vox.as_ref().into(), dims, *min_samples)
}
None => {
warn!("A graphic was requested via an id which is not in use");
return None;
}
},
);
}
let aabr_from_alloc_rect = |rect: guillotiere::Rectangle| {
let (min, max) = (rect.min, rect.max);
Aabr {
min: Vec2::new(min.x as u16, min.y as u16),
max: Vec2::new(max.x as u16, max.y as u16),
}
};
// Allocate rectangle.
let (alloc_id, aabr) = match self
.atlas
.allocate(size2(i32::from(dims.x), i32::from(dims.y)))
{
Some(Allocation { id, rectangle }) => (id, aabr_from_alloc_rect(rectangle)),
// Out of room.
// 1) Remove unused allocations
// TODO: Make more room.
// 2) Rearrange rectangles (see comments below)
// 3) Expand cache size
None => {
// 1) Remove unused allocations
if self.unused_entries_this_frame.is_none() {
self.unused_entries_this_frame = {
let mut unused = self
.cache_map
.iter()
.filter_map(|(key, details)| {
if details.frame < self.current_frame - 1 {
Some(Some((details.frame, *key)))
} else {
None
}
})
.collect::<Vec<_>>();
unused
.sort_unstable_by(|a, b| a.map(|(f, _)| f).cmp(&b.map(|(f, _)| f)));
Some(unused)
};
}
let mut allocation = None;
// Fight the checker!
let current_frame = self.current_frame;
// Will always be Some
if let Some(ref mut unused_entries) = self.unused_entries_this_frame {
// Deallocate from oldest to newest
for key in unused_entries
.iter_mut()
.filter_map(|e| e.take().map(|(_, key)| key))
{
// Check if still in cache map and it has not been used since the vec was built
if self
.cache_map
.get(&key)
.filter(|d| d.frame != current_frame)
.is_some()
{
if let Some(alloc_id) =
self.cache_map.remove(&key).map(|d| d.alloc_id)
{
// Deallocate
self.atlas.deallocate(alloc_id);
// Try to allocate
allocation = self
.atlas
.allocate(size2(i32::from(dims.x), i32::from(dims.y)));
}
}
}
// 2) Rearrange rectangles
// This needs to be done infrequently and be based on whether rectangles have been removed
// Maybe find a way to calculate whether there is a significant amount of fragmentation
// Or consider dropping the use of an atlas and moving to a hashmap of individual textures :/
// if allocation.is_none() {
//
// }
}
match allocation {
Some(Allocation { id, rectangle }) => (id, aabr_from_alloc_rect(rectangle)),
None => {
warn!("Can't find space for an image in the graphic cache");
return None;
}
}
}
};
self.transfer_ready.push((key, aabr));
// Insert area into map for retrieval.
self.cache_map.insert(
key,
CachedDetails {
alloc_id,
frame: self.current_frame,
aabr,
},
);
Some(aabr)
}
}
// Anything not queued since the last call to this will be removed if there is not enough space in the cache
pub fn cache_queued<F>(&mut self, mut cacher: F)
where
F: FnMut(Aabr<u16>, &[[u8; 4]]),
{
match self
.rect_map
.get(&(graphic_id, dims, source.map(|e| e.to_bits()))) // TODO: Replace this with rounded representation of source
{
Some(aabr) => Some(*aabr),
None => match self.graphic_map.get(&graphic_id) {
Some(graphic) => {
// Allocate rectangle.
let aabr = match self
.atlas
.allocate(size2(i32::from(dims.x), i32::from(dims.y)))
{
Some(Allocation { id: _, rectangle }) => {
let (min, max) = (rectangle.min, rectangle.max);
Aabr {
min: Vec2::new(min.x as u16, min.y as u16),
max: Vec2::new(max.x as u16, max.y as u16),
}
}
// Out of room.
// TODO: Make more room.
// 1) Expand cache size
// 2) Remove unused allocations
// 3) Rearrange rectangles
None => return None,
};
// Render image.
// TODO: Use source.
let data = match graphic {
Graphic::Image(ref image) => image
.resize_exact(
u32::from(aabr.size().w),
u32::from(aabr.size().h),
image::FilterType::Nearest,
)
.to_rgba()
// TODO: might be a better way to do this
.pixels()
.map(|p| p.data)
.collect::<Vec<[u8; 4]>>(),
Graphic::Voxel(ref vox, min_samples) =>
super::renderer::draw_vox(&vox.as_ref().into(), aabr.size().into(), *min_samples),
Graphic::Blank => return None,
};
// Draw to allocated area.
cacher(aabr, &data);
// Insert area into map for retrieval.
self.rect_map
.insert((graphic_id, dims, source.map(|e| e.to_bits())), aabr);
// Return area.
Some(aabr)
}
None => None,
},
// Cached queued
// TODO: combine nearby transfers
for (key, target_aarb) in self.transfer_ready.drain(..) {
if let Some(image) = self.soft_cache.get(&key) {
cacher(
target_aarb,
&image.pixels().map(|p| p.data).collect::<Vec<[u8; 4]>>(),
);
} else {
error!("Image queued for transfer to gpu cache but it doesn't exist (this should never occur)");
}
}
// Increment frame
self.current_frame += 1;
// Reset unused entries
self.unused_entries_this_frame = None;
}
}

View File

@ -57,11 +57,7 @@ impl<'a> Pipeline for Voxel {
}
}
pub fn draw_vox(
segment: &Segment,
output_size: Vec2<u16>,
min_samples: Option<u8>,
) -> Vec<[u8; 4]> {
pub fn draw_vox(segment: &Segment, output_size: Vec2<u16>, min_samples: Option<u8>) -> RgbaImage {
let scale = min_samples.map_or(1.0, |s| s as f32).sqrt().ceil() as usize;
let dims = output_size.map(|e| e as usize * scale).into_array();
let mut color = Buffer2d::new(dims, [0; 4]);
@ -85,33 +81,29 @@ pub fn draw_vox(
&mut depth,
);
if scale > 1 {
DynamicImage::ImageRgba8(
RgbaImage::from_vec(
dims[0] as u32,
dims[1] as u32,
color
.as_ref()
.iter()
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.unwrap(),
let image = DynamicImage::ImageRgba8(
RgbaImage::from_vec(
dims[0] as u32,
dims[1] as u32,
color
.as_ref()
.iter()
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.resize_exact(
.unwrap(),
);
if scale > 1 {
image.resize_exact(
output_size.x as u32,
output_size.y as u32,
image::FilterType::Triangle,
)
.to_rgba()
.pixels()
.map(|p| p.data)
.collect::<Vec<[u8; 4]>>()
} else {
// TODO: Remove clone.
color.as_ref().to_vec()
image
}
.to_rgba()
}
fn ao_level(side1: bool, corner: bool, side2: bool) -> u8 {

View File

@ -391,35 +391,25 @@ impl Ui {
max: Vec2::new(uv_r, uv_t),
}
};
// TODO: get dims from graphic_cache (or have it return floats directly)
let (cache_w, cache_h) =
cache_tex.get_dimensions().map(|e| e as f32).into_tuple();
// Cache graphic at particular resolution.
let uv_aabr = match graphic_cache.cache_res(
*graphic_id,
resolution,
source_aabr,
|aabr, data| {
let offset = aabr.min.into_array();
let size = aabr.size().into_array();
if let Err(err) = renderer.update_texture(cache_tex, offset, size, data)
{
warn!("Failed to update texture: {:?}", err);
}
},
) {
Some(aabr) => Aabr {
min: Vec2::new(
aabr.min.x as f32 / cache_w,
aabr.max.y as f32 / cache_h,
),
max: Vec2::new(
aabr.max.x as f32 / cache_w,
aabr.min.y as f32 / cache_h,
),
},
None => continue,
};
let uv_aabr =
match graphic_cache.queue_res(*graphic_id, resolution, source_aabr) {
Some(aabr) => Aabr {
min: Vec2::new(
aabr.min.x as f32 / cache_w,
aabr.max.y as f32 / cache_h,
),
max: Vec2::new(
aabr.max.x as f32 / cache_w,
aabr.min.y as f32 / cache_h,
),
},
None => continue,
};
mesh.push_quad(create_ui_quad(gl_aabr(rect), uv_aabr, color, UiMode::Image));
}
@ -630,20 +620,34 @@ impl Ui {
.create_dynamic_model(mesh.vertices().len() * 4 / 3)
.unwrap();
}
renderer.update_model(&self.model, &mesh, 0).unwrap();
// Update model with new mesh.
renderer.update_model(&self.model, &mesh, 0).unwrap();
// Move cached graphics to the gpu
let (graphic_cache, cache_tex) = self.cache.graphic_cache_mut_and_tex();
graphic_cache.cache_queued(|aabr, data| {
let offset = aabr.min.into_array();
let size = aabr.size().into_array();
if let Err(err) = renderer.update_texture(cache_tex, offset, size, data) {
warn!("Failed to update texture: {:?}", err);
}
});
// Handle window resizing.
// TODO: avoid bluescreen
if let Some(new_dims) = self.window_resized.take() {
let (old_w, old_h) = self.scale.scaled_window_size().into_tuple();
self.scale.window_resized(new_dims, renderer);
let (w, h) = self.scale.scaled_window_size().into_tuple();
self.ui.handle_event(Input::Resize(w, h));
let res = renderer.get_resolution();
// Avoid panic in graphic cache when minimizing.
if res.x > 0 && res.y > 0 {
// Avoid resetting cache if window size didn't change
// Somewhat inefficient for elements that won't change size
if res.x > 0 && res.y > 0 && !(old_w == w && old_h == h) {
self.cache
.clear_graphic_cache(renderer, renderer.get_resolution().map(|e| e * 4));
.clear_graphic_cache(renderer, renderer.get_resolution());
}
// TODO: Probably need to resize glyph cache, see conrod's gfx backend for reference.
}