diff --git a/src/textureatlas.rs b/src/textureatlas.rs index 96fb14e1a..1e6e2681f 100644 --- a/src/textureatlas.rs +++ b/src/textureatlas.rs @@ -6,18 +6,19 @@ use glium::backend::Facade; use glium::texture::{SrgbTexture2d, Texture2dDataSource}; use std::rc::Rc; -/// Textures are bitmaps of srgba data that are sized as a power of 2. +pub const TEX_SIZE: u32 = 4096; + +/// Atlases are bitmaps of srgba data that are sized as a power of 2. /// We allocate sprites out of the available space, starting from the /// bottom left corner and working to the right until we run out of /// space, then we move up to the logical row above. Since sprites can /// have varying height the height of the rows can also vary. #[derive(Debug)] -pub struct Texture { +pub struct Atlas { texture: Rc, // Dimensions of the texture - width: u32, - height: u32, + side: u32, /// The bottom of the available space. bottom: u32, @@ -29,37 +30,41 @@ pub struct Texture { left: u32, } -impl Texture { - fn new(facade: &F, width: u32, height: u32) -> Result { +impl Atlas { + pub fn new(facade: &F, side: u32) -> Result { let texture = Rc::new(SrgbTexture2d::empty_with_format( facade, glium::texture::SrgbFormat::U8U8U8U8, glium::texture::MipmapsOption::NoMipmap, - width, - height, + side, + side, )?); Ok(Self { texture, - width, - height, + side, bottom: 0, tallest: 0, left: 0, }) } + #[inline] + pub fn texture(&self) -> Rc { + Rc::clone(&self.texture) + } + /// Reserve space for a sprite of the given size - fn reserve<'a, T: Texture2dDataSource<'a>>( + pub fn allocate<'a, T: Texture2dDataSource<'a>>( &mut self, width: u32, height: u32, data: T, - ) -> Result { - if width > self.width || height > self.height { + ) -> Result { + if width > self.side || height > self.side { // It's not possible to satisfy that request - return Err(data); + return Err(width.max(height).next_power_of_two()); } - let x_left = self.width - self.left; + let x_left = self.side - self.left; if x_left < width { // Bump up to next row self.bottom += self.tallest; @@ -68,10 +73,10 @@ impl Texture { } // Do we have vertical space? - let y_left = self.height - self.bottom; + let y_left = self.side - self.bottom; if y_left < height { // No room at the inn. - return Err(data); + return Err(self.side); } let rect = Rect { @@ -86,7 +91,10 @@ impl Texture { self.left += width; self.tallest = self.tallest.max(height); - Ok(rect) + Ok(Sprite { + texture: Rc::clone(&self.texture), + coords: rect, + }) } } @@ -214,60 +222,3 @@ impl Sprite { (self.right(slice), self.top(slice)) } } - -#[derive(Debug, Default)] -pub struct Atlas { - textures: Vec, -} - -const TEX_SIZE: u32 = 2048; - -impl Atlas { - pub fn new<'a, F: Facade>(facade: &F) -> Result { - let tex = Texture::new(facade, TEX_SIZE, TEX_SIZE)?; - Ok(Self { textures: vec![tex] }) - } - - // TODO: this is gross, need to tweak this API - pub fn texture(&self) -> Rc { - Rc::clone(&self.textures[0].texture) - } - - pub fn allocate<'a, F: Facade, T: Texture2dDataSource<'a>>( - &mut self, - facade: &F, - width: u32, - height: u32, - mut data: T, - ) -> Result { - for tex in self.textures.iter_mut() { - match tex.reserve(width, height, data) { - Ok(rect) => { - return Ok(Sprite { - texture: Rc::clone(&tex.texture), - coords: rect, - }); - } - Err(dat) => data = dat, - } - } - - // Round up to a reasonable size - let size = width.max(height).max(TEX_SIZE).next_power_of_two(); - - let mut tex = Texture::new(facade, size, size)?; - let rect = match tex.reserve(width, height, data) { - Ok(rect) => rect, - _ => unreachable!("impossible for Texture::reserve to fail on a fresh instance"), - }; - - let sprite = Sprite { - texture: Rc::clone(&tex.texture), - coords: rect, - }; - - self.textures.push(tex); - - Ok(sprite) - } -} diff --git a/src/xwin.rs b/src/xwin.rs index f33e0da5f..df6f6e8ee 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -23,7 +23,7 @@ use xcb_util; use xgfx::{self, Connection, Drawable}; use xkeysyms; -use textureatlas::{Atlas, Sprite, SpriteSlice}; +use textureatlas::{Atlas, Sprite, SpriteSlice, TEX_SIZE}; type Transform3D = euclid::Transform3D; @@ -531,7 +531,7 @@ impl<'a> TerminalWindow<'a> { let program = glium::Program::from_source(&host.window, VERTEX_SHADER, FRAGMENT_SHADER, None)?; - let atlas = RefCell::new(Atlas::new(&host.window)?); + let atlas = RefCell::new(Atlas::new(&host.window, TEX_SIZE)?); Ok(TerminalWindow { host, @@ -854,12 +854,22 @@ impl<'a> TerminalWindow<'a> { mode @ _ => bail!("unhandled pixel mode: {:?}", mode), }; - let tex = self.atlas.borrow_mut().allocate( - &self.host.window, + let tex = match self.atlas.borrow_mut().allocate( raw_im.width, raw_im.height, raw_im, - )?; + ) { + Ok(tex) => tex, + Err(size) => { + // TODO: this is a little tricky. We need to replace the texture + // atlas with a larger one, blow the font cache (that's the more + // tricky part) and arrange to re-render everything. + bail!( + "Ran out of space in the Atlas! Need to make another one of size {}", + size + ); + } + }; let bearing_x = (ft_glyph.bitmap_left as f64 * scale) as isize; let bearing_y = (ft_glyph.bitmap_top as f64 * scale) as isize;