2020-10-19 19:22:02 +03:00
|
|
|
use super::glyphcache::GlyphCache;
|
2019-12-24 06:13:15 +03:00
|
|
|
use super::quad::*;
|
2019-10-27 02:59:53 +03:00
|
|
|
use super::utilsprites::{RenderMetrics, UtilSprites};
|
2020-10-26 02:09:47 +03:00
|
|
|
use ::window::bitmaps::atlas::OutOfTextureSpace;
|
2019-10-27 02:59:53 +03:00
|
|
|
use ::window::glium::backend::Context as GliumContext;
|
2021-07-30 22:59:15 +03:00
|
|
|
use ::window::glium::buffer::Mapping;
|
2019-10-27 02:59:53 +03:00
|
|
|
use ::window::glium::texture::SrgbTexture2d;
|
2021-08-10 09:08:22 +03:00
|
|
|
use ::window::glium::{CapabilitiesSource, IndexBuffer, VertexBuffer};
|
2019-10-27 02:59:53 +03:00
|
|
|
use ::window::*;
|
2022-05-26 06:51:36 +03:00
|
|
|
use anyhow::Context;
|
2021-07-30 08:21:46 +03:00
|
|
|
use std::cell::{Ref, RefCell, RefMut};
|
2019-10-27 02:59:53 +03:00
|
|
|
use std::rc::Rc;
|
2020-10-25 02:40:15 +03:00
|
|
|
use wezterm_font::FontConfiguration;
|
2019-10-27 02:59:53 +03:00
|
|
|
|
2021-07-30 22:59:15 +03:00
|
|
|
const INDICES_PER_CELL: usize = 6;
|
|
|
|
|
|
|
|
pub struct MappedQuads<'a> {
|
|
|
|
mapping: Mapping<'a, [Vertex]>,
|
|
|
|
next: RefMut<'a, usize>,
|
|
|
|
capacity: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> MappedQuads<'a> {
|
|
|
|
pub fn allocate<'b>(&'b mut self) -> anyhow::Result<Quad<'b>> {
|
|
|
|
let idx = *self.next;
|
|
|
|
*self.next += 1;
|
2021-07-31 02:59:47 +03:00
|
|
|
let idx = if idx >= self.capacity {
|
|
|
|
// We don't have enough quads, so we'll keep re-using
|
|
|
|
// the first quad until we reach the end of the render
|
|
|
|
// pass, at which point we'll detect this condition
|
|
|
|
// and re-allocate the quads.
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
idx
|
|
|
|
};
|
2021-07-30 22:59:15 +03:00
|
|
|
|
|
|
|
let idx = idx * VERTICES_PER_CELL;
|
|
|
|
let mut quad = Quad {
|
|
|
|
vert: &mut self.mapping[idx..idx + VERTICES_PER_CELL],
|
|
|
|
};
|
|
|
|
|
|
|
|
quad.set_has_color(false);
|
|
|
|
|
|
|
|
Ok(quad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 22:01:30 +03:00
|
|
|
pub struct TripleVertexBuffer {
|
2021-07-30 08:21:46 +03:00
|
|
|
pub index: RefCell<usize>,
|
|
|
|
pub bufs: RefCell<[VertexBuffer<Vertex>; 3]>,
|
|
|
|
pub indices: IndexBuffer<u32>,
|
|
|
|
pub capacity: usize,
|
2021-07-30 22:59:15 +03:00
|
|
|
pub next_quad: RefCell<usize>,
|
2021-07-30 08:21:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TripleVertexBuffer {
|
2021-07-30 22:59:15 +03:00
|
|
|
pub fn clear_quad_allocation(&self) {
|
|
|
|
*self.next_quad.borrow_mut() = 0;
|
|
|
|
}
|
|
|
|
|
2021-07-31 02:59:47 +03:00
|
|
|
pub fn need_more_quads(&self) -> Option<usize> {
|
|
|
|
let next = *self.next_quad.borrow();
|
|
|
|
if next > self.capacity {
|
|
|
|
Some(next)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:59:15 +03:00
|
|
|
pub fn vertex_index_count(&self) -> (usize, usize) {
|
|
|
|
let num_quads = *self.next_quad.borrow();
|
|
|
|
(num_quads * VERTICES_PER_CELL, num_quads * INDICES_PER_CELL)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map<'a>(&'a self, bufs: &'a mut RefMut<VertexBuffer<Vertex>>) -> MappedQuads<'a> {
|
|
|
|
let mapping = bufs.slice_mut(..).expect("to map vertex buffer").map();
|
|
|
|
MappedQuads {
|
|
|
|
mapping,
|
|
|
|
next: self.next_quad.borrow_mut(),
|
|
|
|
capacity: self.capacity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 08:21:46 +03:00
|
|
|
pub fn current_vb(&self) -> Ref<VertexBuffer<Vertex>> {
|
|
|
|
let index = *self.index.borrow();
|
|
|
|
let bufs = self.bufs.borrow();
|
|
|
|
Ref::map(bufs, |bufs| &bufs[index])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn current_vb_mut(&self) -> RefMut<VertexBuffer<Vertex>> {
|
|
|
|
let index = *self.index.borrow();
|
|
|
|
let bufs = self.bufs.borrow_mut();
|
|
|
|
RefMut::map(bufs, |bufs| &mut bufs[index])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_index(&self) {
|
|
|
|
let mut index = self.index.borrow_mut();
|
|
|
|
*index += 1;
|
|
|
|
if *index >= 3 {
|
|
|
|
*index = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 22:01:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-26 06:51:36 +03:00
|
|
|
pub struct RenderLayer {
|
2022-05-26 17:59:47 +03:00
|
|
|
pub vb: RefCell<[TripleVertexBuffer; 3]>,
|
|
|
|
context: Rc<GliumContext>,
|
|
|
|
zindex: i8,
|
2022-05-26 06:51:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderLayer {
|
2022-05-26 17:59:47 +03:00
|
|
|
pub fn new(context: &Rc<GliumContext>, num_quads: usize, zindex: i8) -> anyhow::Result<Self> {
|
2022-05-26 06:51:36 +03:00
|
|
|
let vb = [
|
|
|
|
Self::compute_vertices(context, 128)?,
|
|
|
|
Self::compute_vertices(context, num_quads)?,
|
|
|
|
Self::compute_vertices(context, 128)?,
|
|
|
|
];
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
context: Rc::clone(context),
|
2022-05-26 17:59:47 +03:00
|
|
|
vb: RefCell::new(vb),
|
|
|
|
zindex,
|
2022-05-26 06:51:36 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_quad_allocation(&self) {
|
2022-05-26 17:59:47 +03:00
|
|
|
for vb in self.vb.borrow().iter() {
|
2022-05-26 06:51:36 +03:00
|
|
|
vb.clear_quad_allocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
pub fn need_more_quads(&self, vb_idx: usize) -> Option<usize> {
|
|
|
|
self.vb.borrow()[vb_idx].need_more_quads()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reallocate_quads(&self, idx: usize, num_quads: usize) -> anyhow::Result<()> {
|
2022-05-26 06:51:36 +03:00
|
|
|
let vb = Self::compute_vertices(&self.context, num_quads)?;
|
2022-05-26 17:59:47 +03:00
|
|
|
self.vb.borrow_mut()[idx] = vb;
|
2022-05-26 06:51:36 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute a vertex buffer to hold the quads that comprise the visible
|
|
|
|
/// portion of the screen. We recreate this when the screen is resized.
|
|
|
|
/// The idea is that we want to minimize any heavy lifting and computation
|
|
|
|
/// and instead just poke some attributes into the offset that corresponds
|
|
|
|
/// to a changed cell when we need to repaint the screen, and then just
|
|
|
|
/// let the GPU figure out the rest.
|
|
|
|
fn compute_vertices(
|
|
|
|
context: &Rc<GliumContext>,
|
|
|
|
num_quads: usize,
|
|
|
|
) -> anyhow::Result<TripleVertexBuffer> {
|
|
|
|
let verts = vec![Vertex::default(); num_quads * VERTICES_PER_CELL];
|
|
|
|
let mut indices = vec![];
|
|
|
|
indices.reserve(num_quads * INDICES_PER_CELL);
|
|
|
|
|
|
|
|
for q in 0..num_quads {
|
|
|
|
let idx = (q * VERTICES_PER_CELL) as u32;
|
|
|
|
|
|
|
|
// Emit two triangles to form the glyph quad
|
|
|
|
indices.push(idx + V_TOP_LEFT as u32);
|
|
|
|
indices.push(idx + V_TOP_RIGHT as u32);
|
|
|
|
indices.push(idx + V_BOT_LEFT as u32);
|
|
|
|
|
|
|
|
indices.push(idx + V_TOP_RIGHT as u32);
|
|
|
|
indices.push(idx + V_BOT_LEFT as u32);
|
|
|
|
indices.push(idx + V_BOT_RIGHT as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = TripleVertexBuffer {
|
|
|
|
index: RefCell::new(0),
|
|
|
|
bufs: RefCell::new([
|
|
|
|
VertexBuffer::dynamic(context, &verts)?,
|
|
|
|
VertexBuffer::dynamic(context, &verts)?,
|
|
|
|
VertexBuffer::dynamic(context, &verts)?,
|
|
|
|
]),
|
|
|
|
capacity: num_quads,
|
|
|
|
indices: IndexBuffer::new(
|
|
|
|
context,
|
|
|
|
glium::index::PrimitiveType::TrianglesList,
|
|
|
|
&indices,
|
|
|
|
)?,
|
|
|
|
next_quad: RefCell::new(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:00:17 +03:00
|
|
|
pub struct RenderState {
|
2019-10-27 02:59:53 +03:00
|
|
|
pub context: Rc<GliumContext>,
|
|
|
|
pub glyph_cache: RefCell<GlyphCache<SrgbTexture2d>>,
|
|
|
|
pub util_sprites: UtilSprites<SrgbTexture2d>,
|
2021-02-21 04:02:21 +03:00
|
|
|
pub glyph_prog: glium::Program,
|
2022-05-26 17:59:47 +03:00
|
|
|
pub layers: RefCell<Vec<Rc<RenderLayer>>>,
|
2019-10-27 02:59:53 +03:00
|
|
|
}
|
|
|
|
|
2021-01-16 19:00:17 +03:00
|
|
|
impl RenderState {
|
2019-10-27 02:59:53 +03:00
|
|
|
pub fn new(
|
|
|
|
context: Rc<GliumContext>,
|
|
|
|
fonts: &Rc<FontConfiguration>,
|
|
|
|
metrics: &RenderMetrics,
|
2020-10-26 02:09:47 +03:00
|
|
|
mut atlas_size: usize,
|
2019-12-15 08:43:05 +03:00
|
|
|
) -> anyhow::Result<Self> {
|
2020-10-26 02:09:47 +03:00
|
|
|
loop {
|
2021-10-09 23:34:59 +03:00
|
|
|
let glyph_cache = RefCell::new(GlyphCache::new_gl(&context, fonts, atlas_size)?);
|
2020-10-26 02:09:47 +03:00
|
|
|
let result = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics);
|
|
|
|
match result {
|
|
|
|
Ok(util_sprites) => {
|
2021-08-06 06:33:56 +03:00
|
|
|
let glyph_prog = Self::compile_prog(&context, Self::glyph_shader)?;
|
2020-10-26 02:09:47 +03:00
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
let main_layer = Rc::new(RenderLayer::new(&context, 1024, 0)?);
|
2020-10-26 02:09:47 +03:00
|
|
|
|
|
|
|
return Ok(Self {
|
|
|
|
context,
|
|
|
|
glyph_cache,
|
|
|
|
util_sprites,
|
2021-02-21 04:02:21 +03:00
|
|
|
glyph_prog,
|
2022-05-26 17:59:47 +03:00
|
|
|
layers: RefCell::new(vec![main_layer]),
|
2020-10-26 02:09:47 +03:00
|
|
|
});
|
|
|
|
}
|
2021-03-14 10:18:15 +03:00
|
|
|
Err(OutOfTextureSpace {
|
|
|
|
size: Some(size), ..
|
|
|
|
}) => {
|
2020-10-26 02:09:47 +03:00
|
|
|
atlas_size = size;
|
|
|
|
}
|
2021-03-14 10:18:15 +03:00
|
|
|
Err(OutOfTextureSpace { size: None, .. }) => {
|
2020-10-26 02:09:47 +03:00
|
|
|
anyhow::bail!("requested texture size is impossible!?")
|
2019-10-27 02:59:53 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
pub fn layer_for_zindex(&self, zindex: i8) -> anyhow::Result<Rc<RenderLayer>> {
|
|
|
|
if let Some(layer) = self
|
|
|
|
.layers
|
|
|
|
.borrow()
|
|
|
|
.iter()
|
|
|
|
.find(|l| l.zindex == zindex)
|
|
|
|
.map(Rc::clone)
|
|
|
|
{
|
|
|
|
return Ok(layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
let layer = Rc::new(RenderLayer::new(&self.context, 128, zindex)?);
|
|
|
|
let mut layers = self.layers.borrow_mut();
|
|
|
|
layers.push(Rc::clone(&layer));
|
|
|
|
|
|
|
|
// Keep the layers sorted by zindex so that they are rendered in
|
|
|
|
// the correct order when the layers array is iterated.
|
|
|
|
layers.sort_by(|a, b| a.zindex.cmp(&b.zindex));
|
2022-05-26 06:51:36 +03:00
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
Ok(layer)
|
2022-05-26 06:51:36 +03:00
|
|
|
}
|
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
/// Returns true if any of the layers needed more quads to be allocated,
|
|
|
|
/// and if we successfully allocated them.
|
|
|
|
/// Returns false if the quads were sufficient.
|
|
|
|
/// Returns Err if we needed to allocate but failed.
|
2022-05-26 06:51:36 +03:00
|
|
|
pub fn allocated_more_quads(&mut self) -> anyhow::Result<bool> {
|
|
|
|
let mut allocated = false;
|
|
|
|
|
2022-05-26 17:59:47 +03:00
|
|
|
for layer in self.layers.borrow().iter() {
|
2022-05-26 06:51:36 +03:00
|
|
|
for vb_idx in 0..3 {
|
2022-05-26 17:59:47 +03:00
|
|
|
if let Some(need_quads) = layer.need_more_quads(vb_idx) {
|
2022-05-26 06:51:36 +03:00
|
|
|
// Round up to next multiple of 1024 that is >=
|
|
|
|
// the number of needed quads for this frame
|
|
|
|
let num_quads = (need_quads + 1023) & !1023;
|
|
|
|
layer.reallocate_quads(vb_idx, num_quads).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to allocate {} quads (needed {})",
|
|
|
|
num_quads, need_quads,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
log::trace!("Allocated {} quads (needed {})", num_quads, need_quads);
|
|
|
|
allocated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(allocated)
|
|
|
|
}
|
|
|
|
|
2021-02-21 04:02:21 +03:00
|
|
|
fn compile_prog(
|
|
|
|
context: &Rc<GliumContext>,
|
|
|
|
fragment_shader: fn(&str) -> (String, String),
|
|
|
|
) -> anyhow::Result<glium::Program> {
|
|
|
|
let mut errors = vec![];
|
2021-08-10 09:08:22 +03:00
|
|
|
|
|
|
|
let caps = context.get_capabilities();
|
|
|
|
log::trace!("Compiling shader. context.capabilities.srgb={}", caps.srgb);
|
|
|
|
|
2021-08-10 08:27:44 +03:00
|
|
|
for version in &["330 core", "330", "320 es", "300 es"] {
|
2021-02-21 04:02:21 +03:00
|
|
|
let (vertex_shader, fragment_shader) = fragment_shader(version);
|
|
|
|
let source = glium::program::ProgramCreationInput::SourceCode {
|
|
|
|
vertex_shader: &vertex_shader,
|
|
|
|
fragment_shader: &fragment_shader,
|
2021-12-23 04:14:52 +03:00
|
|
|
outputs_srgb: true,
|
2021-02-21 04:02:21 +03:00
|
|
|
tessellation_control_shader: None,
|
|
|
|
tessellation_evaluation_shader: None,
|
|
|
|
transform_feedback_varyings: None,
|
|
|
|
uses_point_size: false,
|
|
|
|
geometry_shader: None,
|
|
|
|
};
|
|
|
|
match glium::Program::new(context, source) {
|
|
|
|
Ok(prog) => {
|
|
|
|
return Ok(prog);
|
|
|
|
}
|
2021-08-10 08:27:44 +03:00
|
|
|
Err(err) => errors.push(format!("shader version: {}: {:#}", version, err)),
|
2021-02-21 04:02:21 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
anyhow::bail!("Failed to compile shaders: {}", errors.join("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn glyph_shader(version: &str) -> (String, String) {
|
|
|
|
(
|
|
|
|
format!(
|
2021-07-31 17:28:52 +03:00
|
|
|
"#version {}\n{}",
|
2021-02-21 04:02:21 +03:00
|
|
|
version,
|
|
|
|
include_str!("glyph-vertex.glsl")
|
|
|
|
),
|
2021-07-31 17:28:52 +03:00
|
|
|
format!("#version {}\n{}", version, include_str!("glyph-frag.glsl")),
|
2021-02-21 04:02:21 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-27 02:59:53 +03:00
|
|
|
pub fn recreate_texture_atlas(
|
|
|
|
&mut self,
|
|
|
|
fonts: &Rc<FontConfiguration>,
|
|
|
|
metrics: &RenderMetrics,
|
|
|
|
size: Option<usize>,
|
2021-01-16 19:12:45 +03:00
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
// We make a a couple of passes at resizing; if the user has selected a large
|
|
|
|
// font size (or a large scaling factor) then the `size==None` case will not
|
|
|
|
// be able to fit the initial utility glyphs and apply_scale_change won't
|
|
|
|
// be able to deal with that error situation. Rather than make every
|
|
|
|
// caller know how to deal with OutOfTextureSpace we try to absorb
|
|
|
|
// and accomodate that here.
|
|
|
|
let mut size = size;
|
|
|
|
let mut attempt = 10;
|
|
|
|
loop {
|
|
|
|
match self.recreate_texture_atlas_impl(fonts, metrics, size) {
|
|
|
|
Ok(_) => return Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
attempt -= 1;
|
|
|
|
if attempt == 0 {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(&OutOfTextureSpace {
|
|
|
|
size: Some(needed_size),
|
2021-03-14 10:18:15 +03:00
|
|
|
..
|
2021-01-16 19:12:45 +03:00
|
|
|
}) = err.downcast_ref::<OutOfTextureSpace>()
|
|
|
|
{
|
|
|
|
size.replace(needed_size);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recreate_texture_atlas_impl(
|
|
|
|
&mut self,
|
|
|
|
fonts: &Rc<FontConfiguration>,
|
|
|
|
metrics: &RenderMetrics,
|
|
|
|
size: Option<usize>,
|
2019-12-15 08:43:05 +03:00
|
|
|
) -> anyhow::Result<()> {
|
2021-01-16 19:00:17 +03:00
|
|
|
let size = size.unwrap_or_else(|| self.glyph_cache.borrow().atlas.size());
|
2021-10-09 23:34:59 +03:00
|
|
|
let mut new_glyph_cache = GlyphCache::new_gl(&self.context, fonts, size)?;
|
2021-03-14 20:29:02 +03:00
|
|
|
self.util_sprites = UtilSprites::new(&mut new_glyph_cache, metrics)?;
|
|
|
|
|
|
|
|
let mut glyph_cache = self.glyph_cache.borrow_mut();
|
|
|
|
|
|
|
|
// Steal the decoded image cache; without this, any animating gifs
|
|
|
|
// would reset back to frame 0 each time we filled the texture
|
|
|
|
std::mem::swap(
|
|
|
|
&mut glyph_cache.image_cache,
|
|
|
|
&mut new_glyph_cache.image_cache,
|
|
|
|
);
|
|
|
|
|
|
|
|
*glyph_cache = new_glyph_cache;
|
2019-10-27 02:59:53 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|