use a self-referential struct for mapping vertexbuffers

This commit is contained in:
Wez Furlong committed 2022-11-18 10:03:49 -07:00
1 parent 09f1ecbf82
commit 043606772f
4 files changed
+46 -35

No files matched your search

+40 -13
View File
@@ -3,22 +3,32 @@ use super::quad::*;
use super::utilsprites::{RenderMetrics, UtilSprites};
use ::window::bitmaps::atlas::OutOfTextureSpace;
use ::window::glium::backend::Context as GliumContext;
use ::window::glium::buffer::Mapping;
use ::window::glium::buffer::{BufferMutSlice, Mapping};
use ::window::glium::{CapabilitiesSource, IndexBuffer, VertexBuffer};
use ::window::*;
use anyhow::Context;
use std::cell::{Ref, RefCell, RefMut};
use std::cell::{RefCell, RefMut};
use std::rc::Rc;
use wezterm_font::FontConfiguration;
const INDICES_PER_CELL: usize = 6;
pub struct MappedQuads<'a> {
mapping: Mapping<'a, [Vertex]>,
mapping: GliumMappedVertexBuffer,
next: RefMut<'a, usize>,
capacity: usize,
}
/// This is a self-referential struct, but since those are not possible
/// to create safely in unstable rust, we transmute the lifetimes away
/// to static and store the owner (RefMut) and the derived Mapping object
/// in this struct
pub struct GliumMappedVertexBuffer {
mapping: Mapping<'static, [Vertex]>,
// Drop the owner after the mapping
_owner: RefMut<'static, VertexBuffer<Vertex>>,
}
impl<'a> QuadAllocator for MappedQuads<'a> {
fn allocate<'b>(&'b mut self) -> anyhow::Result<QuadImpl<'b>> {
let idx = *self.next;
@@ -35,7 +45,7 @@ impl<'a> QuadAllocator for MappedQuads<'a> {
let idx = idx * VERTICES_PER_CELL;
let mut quad = Quad {
vert: &mut self.mapping[idx..idx + VERTICES_PER_CELL],
vert: &mut self.mapping.mapping[idx..idx + VERTICES_PER_CELL],
};
quad.set_has_color(false);
@@ -53,7 +63,7 @@ impl<'a> QuadAllocator for MappedQuads<'a> {
let idx = idx * VERTICES_PER_CELL;
let len = self.capacity * VERTICES_PER_CELL;
if idx + vertices.len() < len {
self.mapping[idx..idx + vertices.len()].copy_from_slice(vertices);
self.mapping.mapping[idx..idx + vertices.len()].copy_from_slice(vertices);
}
}
}
@@ -85,8 +95,31 @@ impl TripleVertexBuffer {
(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();
pub fn map(&self) -> MappedQuads {
// To map the vertex buffer, we need to hold a mutable reference to
// the buffer and hold the mapping object alive for the duration
// of the access. Rust doesn't allow us to create a struct that
// holds both of those things, because one references the other
// and it doesn't permit self-referential structs.
// We use the very blunt instrument "transmute" to force Rust to
// treat the lifetimes of both of these things as static, which
// we can then store in the same struct.
// This is "safe" because we carry them around together and ensure
// that the owner is dropped after the derived data.
let mapping = unsafe {
let mut bufs: RefMut<'static, VertexBuffer<Vertex>> =
std::mem::transmute(self.current_vb_mut());
let buf_slice: BufferMutSlice<'static, [Vertex]> =
std::mem::transmute(bufs.slice_mut(..).expect("to map vertex buffer"));
let mapping = buf_slice.map();
GliumMappedVertexBuffer {
_owner: bufs,
mapping,
}
};
MappedQuads {
mapping,
next: self.next_quad.borrow_mut(),
@@ -94,12 +127,6 @@ impl TripleVertexBuffer {
}
}
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();
+1 -2
View File
@@ -406,8 +406,7 @@ impl crate::TermWindow {
) -> anyhow::Result<bool> {
let render_layer = gl_state.layer_for_zindex(layer_index)?;
let vbs = render_layer.vb.borrow();
let mut vb_mut0 = vbs[0].current_vb_mut();
let mut layer0 = vbs[0].map(&mut vb_mut0);
let mut layer0 = vbs[0].map();
let color = bg_color.mul_alpha(layer.def.opacity);
+2 -11
View File
@@ -815,14 +815,8 @@ impl super::TermWindow {
let layer = gl_state.layer_for_zindex(element.zindex)?;
let vbs = layer.vb.borrow();
let vb = [&vbs[0], &vbs[1], &vbs[2]];
let mut vb_mut0 = vb[0].current_vb_mut();
let mut vb_mut1 = vb[1].current_vb_mut();
let mut vb_mut2 = vb[2].current_vb_mut();
let mut layers = TripleLayerQuadAllocator::Gpu(BorrowedLayers([
vb[0].map(&mut vb_mut0),
vb[1].map(&mut vb_mut1),
vb[2].map(&mut vb_mut2),
]));
let mut layers =
TripleLayerQuadAllocator::Gpu(BorrowedLayers([vb[0].map(), vb[1].map(), vb[2].map()]));
let colors = match &element.hover_colors {
Some(hc) => {
@@ -913,9 +907,6 @@ impl super::TermWindow {
}
ComputedElementContent::Children(kids) => {
drop(layers);
drop(vb_mut0);
drop(vb_mut1);
drop(vb_mut2);
for kid in kids {
self.render_element(kid, gl_state, Some(colors))?;
+3 -9
View File
@@ -1984,7 +1984,7 @@ impl super::TermWindow {
let vb = &layer.vb.borrow()[idx];
let (vertex_count, index_count) = vb.vertex_index_count();
if vertex_count > 0 {
let vertices = vb.current_vb();
let vertices = vb.current_vb_mut();
let subpixel_aa = use_subpixel && idx == 1;
let mut uniforms = UniformBuilder::default();
@@ -2137,14 +2137,8 @@ impl super::TermWindow {
let layer = gl_state.layer_for_zindex(0)?;
let vbs = layer.vb.borrow();
let vb = [&vbs[0], &vbs[1], &vbs[2]];
let mut vb_mut0 = vb[0].current_vb_mut();
let mut vb_mut1 = vb[1].current_vb_mut();
let mut vb_mut2 = vb[2].current_vb_mut();
let mut layers = TripleLayerQuadAllocator::Gpu(BorrowedLayers([
vb[0].map(&mut vb_mut0),
vb[1].map(&mut vb_mut1),
vb[2].map(&mut vb_mut2),
]));
let mut layers =
TripleLayerQuadAllocator::Gpu(BorrowedLayers([vb[0].map(), vb[1].map(), vb[2].map()]));
log::trace!("quad map elapsed {:?}", start.elapsed());
metrics::histogram!("quad.map", start.elapsed());