improve texture upload performance

Two issues highlighted by profiling:

* Clearing the texture takes a non-trivial percentage of the profile.
  The docs suggest that it is better to create a new texture than
  to update large portions of a texture, so add some plumbing so
  that we can do that in the first texture-full case.

* Next on the list is the code that translates from linear BGRA to
  SRGBA.  This is present for reasons that I believe are now legacy,
  but for the moment: those two primitives now have faster and
  easier implementations, so simplify to those.

This improves the timg video playback performance by ~10% for me.

refs: #537
This commit is contained in:
Wez Furlong committed 2021-03-13 23:18:15 -08:00
1 parent 693a717db2
commit b8dcfba9a4
4 files changed
+32 -22

No files matched your search

+5 -2
View File
@@ -70,10 +70,12 @@ impl RenderState {
quads,
});
}
Err(OutOfTextureSpace { size: Some(size) }) => {
Err(OutOfTextureSpace {
size: Some(size), ..
}) => {
atlas_size = size;
}
Err(OutOfTextureSpace { size: None }) => {
Err(OutOfTextureSpace { size: None, .. }) => {
anyhow::bail!("requested texture size is impossible!?")
}
};
@@ -326,6 +328,7 @@ impl RenderState {
if let Some(&OutOfTextureSpace {
size: Some(needed_size),
..
}) = err.downcast_ref::<OutOfTextureSpace>()
{
size.replace(needed_size);
+6 -3
View File
@@ -85,12 +85,15 @@ impl super::TermWindow {
match self.paint_opengl_pass() {
Ok(_) => break,
Err(err) => {
if let Some(&OutOfTextureSpace { size: Some(size) }) =
err.downcast_ref::<OutOfTextureSpace>()
if let Some(&OutOfTextureSpace {
size: Some(size),
current_size,
}) = err.downcast_ref::<OutOfTextureSpace>()
{
let result = if pass == 0 {
// Let's try clearing out the atlas and trying again
self.clear_texture_atlas()
// self.clear_texture_atlas()
self.recreate_texture_atlas(Some(current_size))
} else {
log::trace!("grow texture atlas to {}", size);
self.recreate_texture_atlas(Some(size))
+10 -6
View File
@@ -12,6 +12,7 @@ const PADDING: i32 = 1;
#[error("Texture Size exceeded, need {:?}", size)]
pub struct OutOfTextureSpace {
pub size: Option<usize>,
pub current_size: usize,
}
/// Atlases are bitmaps of srgba data that are sized as a power of 2.
@@ -72,12 +73,14 @@ where
// If we can't convert the sizes to i32, then we'll never
// be able to store this image
let reserve_width: i32 = width
.try_into()
.map_err(|_| OutOfTextureSpace { size: None })?;
let reserve_height: i32 = height
.try_into()
.map_err(|_| OutOfTextureSpace { size: None })?;
let reserve_width: i32 = width.try_into().map_err(|_| OutOfTextureSpace {
size: None,
current_size: self.side,
})?;
let reserve_height: i32 = height.try_into().map_err(|_| OutOfTextureSpace {
size: None,
current_size: self.side,
})?;
// We pad each sprite reservation with blank space to avoid
// surprising and unexpected artifacts when the texture is
@@ -107,6 +110,7 @@ where
let size = (reserve_width.max(reserve_height) as usize).next_power_of_two();
Err(OutOfTextureSpace {
size: Some((self.side * 2).max(size)),
current_size: self.side,
})
}
}
+11 -11
View File
@@ -55,7 +55,6 @@ impl Texture2d for SrgbTexture2d {
.pixels()
.iter()
.map(|&p| {
let (r, g, b, a) = Color(p).as_rgba();
// convert from linear to srgb.
// This brightens up the emoji glyphs so that the
// colors match those of the software renderer and
@@ -63,22 +62,23 @@ impl Texture2d for SrgbTexture2d {
// I haven't run down exactly why this is needed but
// suspect that it would be resolved if we could teach
// glium to use SRGB for the texture.
fn conv(v: u8) -> u8 {
let f = (v as f32) / 255.;
let c = if f <= 0.003_130_8 {
f * 12.92
} else {
f.powf(1.0 / 2.4) * 1.055 - 0.055
};
(c * 255.).ceil() as u8
}
Color::rgba(conv(b), conv(g), conv(r), conv(a)).0
let (r, g, b, a) = Color(p).as_rgba();
// Switch from bgra to rgba
Color::with_linear_rgba_u8(b, g, r, a).0
})
.collect(),
width: im_width as u32,
height: im_height as u32,
format: glium::texture::ClientFormat::U8U8U8U8,
};
/* Aspirationally, we want this:
let source = glium::texture::RawImage2d {
data: std::borrow::Cow::Borrowed(im .pixels()),
width: im_width as u32,
height: im_height as u32,
format: glium::texture::ClientFormat::U8U8U8U8,
};
*/
SrgbTexture2d::write(
self,