From 7a77854bf096eefde28bea4d00d28e4339d26980 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 17 Nov 2022 08:24:56 -0700 Subject: [PATCH] Add MappedVertexBuffer abstraction --- wezterm-gui/src/renderstate.rs | 84 ++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/wezterm-gui/src/renderstate.rs b/wezterm-gui/src/renderstate.rs index bd5ec2e79..6ee9d1fc0 100644 --- a/wezterm-gui/src/renderstate.rs +++ b/wezterm-gui/src/renderstate.rs @@ -1,6 +1,7 @@ use super::glyphcache::GlyphCache; use super::quad::*; use super::utilsprites::{RenderMetrics, UtilSprites}; +use crate::termwindow::webgpu::WebGpuState; use ::window::bitmaps::atlas::OutOfTextureSpace; use ::window::glium::backend::Context as GliumContext; use ::window::glium::buffer::{BufferMutSlice, Mapping}; @@ -10,15 +11,88 @@ use anyhow::Context; use std::cell::{Ref, RefCell, RefMut}; use std::rc::Rc; use wezterm_font::FontConfiguration; +use wgpu::util::DeviceExt; const INDICES_PER_CELL: usize = 6; +enum MappedVertexBuffer { + Glium(GliumMappedVertexBuffer), + WebGpu(WebGpuMappedVertexBuffer), +} + +impl MappedVertexBuffer { + fn slice_mut(&mut self, range: std::ops::Range) -> &mut [Vertex] { + match self { + Self::Glium(g) => &mut g.mapping[range], + Self::WebGpu(g) => { + let mapping: &mut [Vertex] = bytemuck::cast_slice_mut(&mut g.mapping); + &mut mapping[range] + } + } + } +} + pub struct MappedQuads<'a> { - mapping: GliumMappedVertexBuffer, + mapping: MappedVertexBuffer, next: RefMut<'a, usize>, capacity: usize, } +pub struct WebGpuMappedVertexBuffer { + mapping: wgpu::BufferViewMut<'static>, + // Owner mapping, must be dropped after mapping + _slice: wgpu::BufferSlice<'static>, +} + +pub struct WebGpuVertexBuffer { + buf: wgpu::Buffer, +} + +impl WebGpuVertexBuffer { + pub fn new(num_vertices: usize, state: &WebGpuState) -> Self { + Self { + buf: state.device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Vertex Buffer"), + size: (num_vertices * std::mem::size_of::()) as wgpu::BufferAddress, + usage: wgpu::BufferUsages::VERTEX, + mapped_at_creation: true, + }), + } + } + + pub fn map(&self) -> WebGpuMappedVertexBuffer { + unsafe { + let slice: wgpu::BufferSlice<'static> = std::mem::transmute(self.buf.slice(..)); + let mapping = slice.get_mapped_range_mut(); + + WebGpuMappedVertexBuffer { + mapping, + _slice: slice, + } + } + } +} + +pub struct WebGpuIndexBuffer { + buf: wgpu::Buffer, + num_indices: usize, +} + +impl WebGpuIndexBuffer { + pub fn new(indices: &[u32], state: &WebGpuState) -> Self { + Self { + buf: state + .device + .create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Index Buffer"), + usage: wgpu::BufferUsages::INDEX, + contents: bytemuck::cast_slice(indices), + }), + num_indices: indices.len(), + } + } +} + /// 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 @@ -45,7 +119,7 @@ impl<'a> QuadAllocator for MappedQuads<'a> { let idx = idx * VERTICES_PER_CELL; let mut quad = Quad { - vert: &mut self.mapping.mapping[idx..idx + VERTICES_PER_CELL], + vert: self.mapping.slice_mut(idx..idx + VERTICES_PER_CELL), }; quad.set_has_color(false); @@ -63,7 +137,9 @@ 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.mapping[idx..idx + vertices.len()].copy_from_slice(vertices); + self.mapping + .slice_mut(idx..idx + vertices.len()) + .copy_from_slice(vertices); } } } @@ -121,7 +197,7 @@ impl TripleVertexBuffer { }; MappedQuads { - mapping, + mapping: MappedVertexBuffer::Glium(mapping), next: self.next_quad.borrow_mut(), capacity: self.capacity, }