Abstract more over the render state, add RenderContext
This commit is contained in:
4 files changed
+134
-101
No files matched your search
@@ -1,21 +1,16 @@
|
||||
use super::utilsprites::RenderMetrics;
|
||||
use crate::customglyph::*;
|
||||
use crate::termwindow::webgpu::{WebGpuState, WebGpuTexture};
|
||||
use crate::renderstate::RenderContext;
|
||||
use ::window::bitmaps::atlas::{Atlas, OutOfTextureSpace, Sprite};
|
||||
use ::window::bitmaps::{BitmapImage, Image, ImageTexture, Texture2d};
|
||||
use ::window::color::SrgbaPixel;
|
||||
use ::window::glium::backend::Context as GliumContext;
|
||||
use ::window::glium::texture::SrgbTexture2d;
|
||||
use ::window::glium::CapabilitiesSource;
|
||||
use ::window::{glium, Point, Rect};
|
||||
use anyhow::Context;
|
||||
use ::window::{Point, Rect};
|
||||
use config::{AllowSquareGlyphOverflow, TextStyle};
|
||||
use euclid::num::Zero;
|
||||
use lfucache::LfuCacheU64;
|
||||
use ordered_float::NotNan;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, MutexGuard};
|
||||
use std::time::Instant;
|
||||
@@ -283,65 +278,13 @@ impl GlyphCache {
|
||||
}
|
||||
}
|
||||
|
||||
impl GlyphCache {
|
||||
pub fn new_webgpu(
|
||||
state: &WebGpuState,
|
||||
fonts: &Rc<FontConfiguration>,
|
||||
size: usize,
|
||||
) -> anyhow::Result<Self> {
|
||||
let texture: Rc<dyn Texture2d> =
|
||||
Rc::new(WebGpuTexture::new(size as u32, size as u32, state));
|
||||
let atlas = Atlas::new(&texture).expect("failed to create new texture atlas");
|
||||
|
||||
Ok(Self {
|
||||
fonts: Rc::clone(fonts),
|
||||
glyph_cache: HashMap::new(),
|
||||
image_cache: LfuCacheU64::new(
|
||||
"glyph_cache.image_cache.hit.rate",
|
||||
"glyph_cache.image_cache.miss.rate",
|
||||
|config| config.glyph_cache_image_cache_size,
|
||||
&fonts.config(),
|
||||
),
|
||||
frame_cache: HashMap::new(),
|
||||
atlas,
|
||||
line_glyphs: HashMap::new(),
|
||||
block_glyphs: HashMap::new(),
|
||||
cursor_glyphs: HashMap::new(),
|
||||
color: HashMap::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl GlyphCache {
|
||||
pub fn new_gl(
|
||||
backend: &Rc<GliumContext>,
|
||||
backend: &RenderContext,
|
||||
fonts: &Rc<FontConfiguration>,
|
||||
size: usize,
|
||||
) -> anyhow::Result<Self> {
|
||||
let caps = backend.get_capabilities();
|
||||
// You'd hope that allocating a texture would automatically
|
||||
// include this check, but it doesn't, and instead, the texture
|
||||
// silently fails to bind when attempting to render into it later.
|
||||
// So! We check and raise here for ourselves!
|
||||
let max_texture_size: usize = caps
|
||||
.max_texture_size
|
||||
.try_into()
|
||||
.context("represent Capabilities.max_texture_size as usize")?;
|
||||
if size > max_texture_size {
|
||||
anyhow::bail!(
|
||||
"Cannot use a texture of size {} as it is larger \
|
||||
than the max {} supported by your GPU",
|
||||
size,
|
||||
caps.max_texture_size
|
||||
);
|
||||
}
|
||||
let surface: Rc<dyn Texture2d> = Rc::new(SrgbTexture2d::empty_with_format(
|
||||
backend,
|
||||
glium::texture::SrgbFormat::U8U8U8U8,
|
||||
glium::texture::MipmapsOption::NoMipmap,
|
||||
size as u32,
|
||||
size as u32,
|
||||
)?);
|
||||
let surface = backend.allocate_texture_atlas(size)?;
|
||||
let atlas = Atlas::new(&surface).expect("failed to create new texture atlas");
|
||||
|
||||
Ok(Self {
|
||||
|
||||
+115
-18
@@ -1,7 +1,7 @@
|
||||
use super::glyphcache::GlyphCache;
|
||||
use super::quad::*;
|
||||
use super::utilsprites::{RenderMetrics, UtilSprites};
|
||||
use crate::termwindow::webgpu::WebGpuState;
|
||||
use crate::termwindow::webgpu::{WebGpuState, WebGpuTexture};
|
||||
use ::window::bitmaps::atlas::OutOfTextureSpace;
|
||||
use ::window::glium::backend::Context as GliumContext;
|
||||
use ::window::glium::buffer::{BufferMutSlice, Mapping};
|
||||
@@ -11,12 +11,108 @@ use ::window::glium::{
|
||||
use ::window::*;
|
||||
use anyhow::Context;
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
use wezterm_font::FontConfiguration;
|
||||
use wgpu::util::DeviceExt;
|
||||
use window::bitmaps::Texture2d;
|
||||
|
||||
const INDICES_PER_CELL: usize = 6;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RenderContext {
|
||||
Glium(Rc<GliumContext>),
|
||||
WebGpu(Rc<WebGpuState>),
|
||||
}
|
||||
|
||||
impl RenderContext {
|
||||
pub fn allocate_index_buffer(&self, indices: &[u32]) -> anyhow::Result<IndexBuffer> {
|
||||
match self {
|
||||
Self::Glium(context) => Ok(IndexBuffer::Glium(GliumIndexBuffer::new(
|
||||
context,
|
||||
glium::index::PrimitiveType::TrianglesList,
|
||||
indices,
|
||||
)?)),
|
||||
Self::WebGpu(state) => Ok(IndexBuffer::WebGpu(WebGpuIndexBuffer::new(indices, state))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocate_vertex_buffer_initializer(&self, num_quads: usize) -> Vec<Vertex> {
|
||||
match self {
|
||||
Self::Glium(_) => {
|
||||
vec![Vertex::default(); num_quads * VERTICES_PER_CELL]
|
||||
}
|
||||
Self::WebGpu(_) => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocate_vertex_buffer(
|
||||
&self,
|
||||
num_quads: usize,
|
||||
initializer: &[Vertex],
|
||||
) -> anyhow::Result<VertexBuffer> {
|
||||
match self {
|
||||
Self::Glium(context) => Ok(VertexBuffer::Glium(GliumVertexBuffer::dynamic(
|
||||
context,
|
||||
initializer,
|
||||
)?)),
|
||||
Self::WebGpu(state) => Ok(VertexBuffer::WebGpu(WebGpuVertexBuffer::new(
|
||||
num_quads * VERTICES_PER_CELL,
|
||||
state,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocate_texture_atlas(&self, size: usize) -> anyhow::Result<Rc<dyn Texture2d>> {
|
||||
match self {
|
||||
Self::Glium(context) => {
|
||||
let caps = context.get_capabilities();
|
||||
// You'd hope that allocating a texture would automatically
|
||||
// include this check, but it doesn't, and instead, the texture
|
||||
// silently fails to bind when attempting to render into it later.
|
||||
// So! We check and raise here for ourselves!
|
||||
let max_texture_size: usize = caps
|
||||
.max_texture_size
|
||||
.try_into()
|
||||
.context("represent Capabilities.max_texture_size as usize")?;
|
||||
if size > max_texture_size {
|
||||
anyhow::bail!(
|
||||
"Cannot use a texture of size {} as it is larger \
|
||||
than the max {} supported by your GPU",
|
||||
size,
|
||||
caps.max_texture_size
|
||||
);
|
||||
}
|
||||
use crate::glium::texture::SrgbTexture2d;
|
||||
let surface: Rc<dyn Texture2d> = Rc::new(SrgbTexture2d::empty_with_format(
|
||||
context,
|
||||
glium::texture::SrgbFormat::U8U8U8U8,
|
||||
glium::texture::MipmapsOption::NoMipmap,
|
||||
size as u32,
|
||||
size as u32,
|
||||
)?);
|
||||
Ok(surface)
|
||||
}
|
||||
Self::WebGpu(state) => {
|
||||
let texture: Rc<dyn Texture2d> =
|
||||
Rc::new(WebGpuTexture::new(size as u32, size as u32, state));
|
||||
Ok(texture)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn renderer_info(&self) -> String {
|
||||
match self {
|
||||
Self::Glium(ctx) => format!(
|
||||
"{} {}",
|
||||
ctx.get_opengl_renderer_string(),
|
||||
ctx.get_opengl_version_string()
|
||||
),
|
||||
Self::WebGpu(_) => "WebGPU".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum IndexBuffer {
|
||||
Glium(GliumIndexBuffer<u32>),
|
||||
WebGpu(WebGpuIndexBuffer),
|
||||
@@ -250,12 +346,12 @@ impl TripleVertexBuffer {
|
||||
|
||||
pub struct RenderLayer {
|
||||
pub vb: RefCell<[TripleVertexBuffer; 3]>,
|
||||
context: Rc<GliumContext>,
|
||||
context: RenderContext,
|
||||
zindex: i8,
|
||||
}
|
||||
|
||||
impl RenderLayer {
|
||||
pub fn new(context: &Rc<GliumContext>, num_quads: usize, zindex: i8) -> anyhow::Result<Self> {
|
||||
pub fn new(context: &RenderContext, num_quads: usize, zindex: i8) -> anyhow::Result<Self> {
|
||||
let vb = [
|
||||
Self::compute_vertices(context, 32)?,
|
||||
Self::compute_vertices(context, num_quads)?,
|
||||
@@ -263,7 +359,7 @@ impl RenderLayer {
|
||||
];
|
||||
|
||||
Ok(Self {
|
||||
context: Rc::clone(context),
|
||||
context: context.clone(),
|
||||
vb: RefCell::new(vb),
|
||||
zindex,
|
||||
})
|
||||
@@ -309,10 +405,10 @@ impl RenderLayer {
|
||||
/// 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>,
|
||||
context: &RenderContext,
|
||||
num_quads: usize,
|
||||
) -> anyhow::Result<TripleVertexBuffer> {
|
||||
let verts = vec![Vertex::default(); num_quads * VERTICES_PER_CELL];
|
||||
let verts = context.allocate_vertex_buffer_initializer(num_quads);
|
||||
log::trace!(
|
||||
"compute_vertices num_quads={}, allocated {} bytes",
|
||||
num_quads,
|
||||
@@ -337,16 +433,12 @@ impl RenderLayer {
|
||||
let buffer = TripleVertexBuffer {
|
||||
index: RefCell::new(0),
|
||||
bufs: RefCell::new([
|
||||
VertexBuffer::Glium(GliumVertexBuffer::dynamic(context, &verts)?),
|
||||
VertexBuffer::Glium(GliumVertexBuffer::dynamic(context, &verts)?),
|
||||
VertexBuffer::Glium(GliumVertexBuffer::dynamic(context, &verts)?),
|
||||
context.allocate_vertex_buffer(num_quads, &verts)?,
|
||||
context.allocate_vertex_buffer(num_quads, &verts)?,
|
||||
context.allocate_vertex_buffer(num_quads, &verts)?,
|
||||
]),
|
||||
capacity: num_quads,
|
||||
indices: IndexBuffer::Glium(GliumIndexBuffer::new(
|
||||
context,
|
||||
glium::index::PrimitiveType::TrianglesList,
|
||||
&indices,
|
||||
)?),
|
||||
indices: context.allocate_index_buffer(&indices)?,
|
||||
next_quad: RefCell::new(0),
|
||||
};
|
||||
|
||||
@@ -372,16 +464,16 @@ impl TripleLayerQuadAllocatorTrait for BorrowedLayers {
|
||||
}
|
||||
|
||||
pub struct RenderState {
|
||||
pub context: Rc<GliumContext>,
|
||||
pub context: RenderContext,
|
||||
pub glyph_cache: RefCell<GlyphCache>,
|
||||
pub util_sprites: UtilSprites,
|
||||
pub glyph_prog: glium::Program,
|
||||
pub glyph_prog: Option<glium::Program>,
|
||||
pub layers: RefCell<Vec<Rc<RenderLayer>>>,
|
||||
}
|
||||
|
||||
impl RenderState {
|
||||
pub fn new(
|
||||
context: Rc<GliumContext>,
|
||||
context: RenderContext,
|
||||
fonts: &Rc<FontConfiguration>,
|
||||
metrics: &RenderMetrics,
|
||||
mut atlas_size: usize,
|
||||
@@ -391,7 +483,12 @@ impl RenderState {
|
||||
let result = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics);
|
||||
match result {
|
||||
Ok(util_sprites) => {
|
||||
let glyph_prog = Self::compile_prog(&context, Self::glyph_shader)?;
|
||||
let glyph_prog = match &context {
|
||||
RenderContext::Glium(context) => {
|
||||
Some(Self::compile_prog(&context, Self::glyph_shader)?)
|
||||
}
|
||||
RenderContext::WebGpu(_) => None,
|
||||
};
|
||||
|
||||
let main_layer = Rc::new(RenderLayer::new(&context, 1024, 0)?);
|
||||
|
||||
|
||||
@@ -531,30 +531,23 @@ impl TermWindow {
|
||||
self.emit_window_event("window-focus-changed", None);
|
||||
}
|
||||
|
||||
fn created(
|
||||
&mut self,
|
||||
window: &Window,
|
||||
ctx: std::rc::Rc<glium::backend::Context>,
|
||||
) -> anyhow::Result<()> {
|
||||
fn created(&mut self, window: &Window, ctx: RenderContext) -> anyhow::Result<()> {
|
||||
self.render_state = None;
|
||||
|
||||
let render_info = ctx.renderer_info();
|
||||
self.opengl_info.replace(render_info.clone());
|
||||
|
||||
match RenderState::new(ctx, &self.fonts, &self.render_metrics, ATLAS_SIZE) {
|
||||
Ok(gl) => {
|
||||
self.opengl_info.replace(format!(
|
||||
"{} {}",
|
||||
gl.context.get_opengl_renderer_string(),
|
||||
gl.context.get_opengl_version_string()
|
||||
));
|
||||
Ok(render_state) => {
|
||||
log::debug!(
|
||||
"OpenGL initialized! {} {} wezterm version: {}",
|
||||
gl.context.get_opengl_renderer_string(),
|
||||
gl.context.get_opengl_version_string(),
|
||||
"OpenGL initialized! {} wezterm version: {}",
|
||||
render_info,
|
||||
config::wezterm_version(),
|
||||
);
|
||||
self.render_state.replace(gl);
|
||||
self.render_state.replace(render_state);
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("failed to create OpenGLRenderState: {}", err);
|
||||
log::error!("failed to create RenderState: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -833,14 +826,14 @@ impl TermWindow {
|
||||
|
||||
if let Some(gl) = gl {
|
||||
myself.gl.replace(Rc::clone(&gl));
|
||||
myself.created(&window, Rc::clone(&gl))?;
|
||||
myself.created(&window, RenderContext::Glium(Rc::clone(&gl)))?;
|
||||
}
|
||||
if let Some(webgpu) = webgpu {
|
||||
myself.webgpu.replace(Rc::clone(&webgpu));
|
||||
myself.created(&window, RenderContext::WebGpu(Rc::clone(&webgpu)))?;
|
||||
}
|
||||
myself.webgpu = webgpu;
|
||||
myself.load_os_parameters();
|
||||
window.show();
|
||||
if myself.webgpu.is_some() {
|
||||
log::info!("using webgpu");
|
||||
}
|
||||
myself.subscribe_to_pane_updates();
|
||||
myself.emit_window_event("window-config-reloaded", None);
|
||||
myself.emit_status_event();
|
||||
|
||||
@@ -2001,7 +2001,7 @@ impl super::TermWindow {
|
||||
frame.draw(
|
||||
vertices.glium().slice(0..vertex_count).unwrap(),
|
||||
vb.indices.glium().slice(0..index_count).unwrap(),
|
||||
&gl_state.glyph_prog,
|
||||
gl_state.glyph_prog.as_ref().unwrap(),
|
||||
&uniforms,
|
||||
if subpixel_aa {
|
||||
&dual_source_blending
|
||||
|
||||
Reference in new issue
Block a user