From 3ce3399bdecac08475a77e094b72e745dcb5c952 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 26 Oct 2019 16:59:53 -0700 Subject: [PATCH] move renderstate out of termwindow.rs --- src/frontend/software/mod.rs | 1 + src/frontend/software/renderstate.rs | 258 +++++++++++++++++++++++++ src/frontend/software/termwindow.rs | 274 ++------------------------- 3 files changed, 272 insertions(+), 261 deletions(-) create mode 100644 src/frontend/software/renderstate.rs diff --git a/src/frontend/software/mod.rs b/src/frontend/software/mod.rs index ce0e462f2..03ce05449 100644 --- a/src/frontend/software/mod.rs +++ b/src/frontend/software/mod.rs @@ -12,6 +12,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; mod glyphcache; +mod renderstate; mod termwindow; mod utilsprites; diff --git a/src/frontend/software/renderstate.rs b/src/frontend/software/renderstate.rs new file mode 100644 index 000000000..61856b643 --- /dev/null +++ b/src/frontend/software/renderstate.rs @@ -0,0 +1,258 @@ +use super::glyphcache::{CachedGlyph, GlyphCache}; +use super::utilsprites::{RenderMetrics, UtilSprites}; +use crate::config::TextStyle; +use crate::font::{FontConfiguration, GlyphInfo}; +use ::window::bitmaps::ImageTexture; +use ::window::glium::backend::Context as GliumContext; +use ::window::glium::texture::SrgbTexture2d; +use ::window::glium::{IndexBuffer, VertexBuffer}; +use ::window::*; +use failure::Fallible; +use std::cell::RefCell; +use std::rc::Rc; + +use super::termwindow::Vertex; + +pub struct SoftwareRenderState { + pub glyph_cache: RefCell>, + pub util_sprites: UtilSprites, +} + +impl SoftwareRenderState { + pub fn new( + fonts: &Rc, + metrics: &RenderMetrics, + size: usize, + ) -> Fallible { + let glyph_cache = RefCell::new(GlyphCache::new(fonts, size)); + let util_sprites = UtilSprites::new(&mut glyph_cache.borrow_mut(), metrics)?; + Ok(Self { + glyph_cache, + util_sprites, + }) + } +} + +pub struct OpenGLRenderState { + pub context: Rc, + pub glyph_cache: RefCell>, + pub util_sprites: UtilSprites, + pub program: glium::Program, + pub glyph_vertex_buffer: RefCell>, + pub glyph_index_buffer: IndexBuffer, +} + +impl OpenGLRenderState { + pub fn new( + context: Rc, + fonts: &Rc, + metrics: &RenderMetrics, + size: usize, + pixel_width: usize, + pixel_height: usize, + ) -> Fallible { + let glyph_cache = RefCell::new(GlyphCache::new_gl(&context, fonts, size)?); + let util_sprites = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics)?; + + let mut errors = vec![]; + let mut program = None; + for version in &["330", "300 es"] { + let source = glium::program::ProgramCreationInput::SourceCode { + vertex_shader: &Self::vertex_shader(version), + fragment_shader: &Self::fragment_shader(version), + outputs_srgb: true, + tessellation_control_shader: None, + tessellation_evaluation_shader: None, + transform_feedback_varyings: None, + uses_point_size: false, + geometry_shader: None, + }; + log::error!("compiling a prog with version {}", version); + match glium::Program::new(&context, source) { + Ok(prog) => { + program = Some(prog); + break; + } + Err(err) => errors.push(err.to_string()), + }; + } + + let program = program.ok_or_else(|| { + failure::format_err!("Failed to compile shaders: {}", errors.join("\n")) + })?; + + let (glyph_vertex_buffer, glyph_index_buffer) = + Self::compute_vertices(&context, metrics, pixel_width as f32, pixel_height as f32)?; + + Ok(Self { + context, + glyph_cache, + util_sprites, + program, + glyph_vertex_buffer: RefCell::new(glyph_vertex_buffer), + glyph_index_buffer, + }) + } + + pub fn advise_of_window_size_change( + &mut self, + metrics: &RenderMetrics, + pixel_width: usize, + pixel_height: usize, + ) -> Fallible<()> { + let (glyph_vertex_buffer, glyph_index_buffer) = Self::compute_vertices( + &self.context, + metrics, + pixel_width as f32, + pixel_height as f32, + )?; + + *self.glyph_vertex_buffer.borrow_mut() = glyph_vertex_buffer; + self.glyph_index_buffer = glyph_index_buffer; + Ok(()) + } + + fn vertex_shader(version: &str) -> String { + format!("#version {}\n{}", version, include_str!("vertex.glsl")) + } + + fn fragment_shader(version: &str) -> String { + format!("#version {}\n{}", version, include_str!("fragment.glsl")) + } + + /// 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 and 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, + metrics: &RenderMetrics, + width: f32, + height: f32, + ) -> Fallible<(VertexBuffer, IndexBuffer)> { + let cell_width = metrics.cell_size.width as f32; + let cell_height = metrics.cell_size.height as f32; + let mut verts = Vec::new(); + let mut indices = Vec::new(); + + let num_cols = (width as usize + 1) / cell_width as usize; + let num_rows = (height as usize + 1) / cell_height as usize; + + for y in 0..num_rows { + for x in 0..num_cols { + let y_pos = (height / -2.0) + (y as f32 * cell_height); + let x_pos = (width / -2.0) + (x as f32 * cell_width); + // Remember starting index for this position + let idx = verts.len() as u32; + verts.push(Vertex { + // Top left + position: (x_pos, y_pos), + ..Default::default() + }); + verts.push(Vertex { + // Top Right + position: (x_pos + cell_width, y_pos), + ..Default::default() + }); + verts.push(Vertex { + // Bottom Left + position: (x_pos, y_pos + cell_height), + ..Default::default() + }); + verts.push(Vertex { + // Bottom Right + position: (x_pos + cell_width, y_pos + cell_height), + ..Default::default() + }); + + // Emit two triangles to form the glyph quad + indices.push(idx); + indices.push(idx + 1); + indices.push(idx + 2); + indices.push(idx + 1); + indices.push(idx + 2); + indices.push(idx + 3); + } + } + + Ok(( + VertexBuffer::dynamic(context, &verts)?, + IndexBuffer::new( + context, + glium::index::PrimitiveType::TrianglesList, + &indices, + )?, + )) + } +} + +#[allow(clippy::large_enum_variant)] +pub enum RenderState { + Software(SoftwareRenderState), + GL(OpenGLRenderState), +} + +impl RenderState { + pub fn recreate_texture_atlas( + &mut self, + fonts: &Rc, + metrics: &RenderMetrics, + size: Option, + ) -> Fallible<()> { + match self { + RenderState::Software(software) => { + let size = size.unwrap_or_else(|| software.glyph_cache.borrow().atlas.size()); + let mut glyph_cache = GlyphCache::new(fonts, size); + software.util_sprites = UtilSprites::new(&mut glyph_cache, metrics)?; + *software.glyph_cache.borrow_mut() = glyph_cache; + } + RenderState::GL(gl) => { + let size = size.unwrap_or_else(|| gl.glyph_cache.borrow().atlas.size()); + let mut glyph_cache = GlyphCache::new_gl(&gl.context, fonts, size)?; + gl.util_sprites = UtilSprites::new(&mut glyph_cache, metrics)?; + *gl.glyph_cache.borrow_mut() = glyph_cache; + } + }; + Ok(()) + } + + pub fn advise_of_window_size_change( + &mut self, + metrics: &RenderMetrics, + pixel_width: usize, + pixel_height: usize, + ) -> Fallible<()> { + if let RenderState::GL(gl) = self { + gl.advise_of_window_size_change(metrics, pixel_width, pixel_height)?; + } + Ok(()) + } + + pub fn cached_software_glyph( + &self, + info: &GlyphInfo, + style: &TextStyle, + ) -> Fallible>> { + if let RenderState::Software(software) = self { + software.glyph_cache.borrow_mut().cached_glyph(info, style) + } else { + failure::bail!("attempted to call cached_software_glyph when in gl mode") + } + } + + pub fn software(&self) -> &SoftwareRenderState { + match self { + RenderState::Software(software) => software, + _ => panic!("only valid for software render mode"), + } + } + + pub fn opengl(&self) -> &OpenGLRenderState { + match self { + RenderState::GL(gl) => gl, + _ => panic!("only valid for opengl render mode"), + } + } +} diff --git a/src/frontend/software/termwindow.rs b/src/frontend/software/termwindow.rs index 9a3a09d16..7035009e2 100644 --- a/src/frontend/software/termwindow.rs +++ b/src/frontend/software/termwindow.rs @@ -1,8 +1,7 @@ -use super::glyphcache::{CachedGlyph, GlyphCache}; -use super::utilsprites::{RenderMetrics, UtilSprites}; +use super::renderstate::*; +use super::utilsprites::RenderMetrics; use crate::config::Config; -use crate::config::TextStyle; -use crate::font::{FontConfiguration, FontSystemSelection, GlyphInfo}; +use crate::font::{FontConfiguration, FontSystemSelection}; use crate::frontend::guicommon::clipboard::SystemClipboard; use crate::frontend::{front_end, gui_executor}; use crate::keyassignment::{KeyAssignment, KeyMap, SpawnTabDomain}; @@ -11,14 +10,11 @@ use crate::mux::tab::{Tab, TabId}; use crate::mux::window::WindowId as MuxWindowId; use crate::mux::Mux; use ::window::bitmaps::atlas::{OutOfTextureSpace, SpriteSlice}; -use ::window::bitmaps::{ImageTexture, Texture2d, TextureRect}; -use ::window::glium::backend::Context as GliumContext; -use ::window::glium::texture::SrgbTexture2d; -use ::window::glium::{uniform, IndexBuffer, Surface, VertexBuffer}; +use ::window::bitmaps::{Texture2d, TextureRect}; +use ::window::glium::{uniform, Surface}; use ::window::*; use failure::Fallible; use std::any::Any; -use std::cell::RefCell; use std::ops::Range; use std::rc::Rc; use std::sync::Arc; @@ -35,19 +31,19 @@ const V_BOT_LEFT: usize = 2; const V_BOT_RIGHT: usize = 3; #[derive(Copy, Clone, Default)] -struct Vertex { +pub struct Vertex { // Physical position of the corner of the character cell - position: (f32, f32), + pub position: (f32, f32), // bearing offset within the cell - adjust: (f32, f32), + pub adjust: (f32, f32), // glyph texture - tex: (f32, f32), + pub tex: (f32, f32), // underline texture - underline: (f32, f32), - bg_color: (f32, f32, f32, f32), - fg_color: (f32, f32, f32, f32), + pub underline: (f32, f32), + pub bg_color: (f32, f32, f32, f32), + pub fg_color: (f32, f32, f32, f32), // "bool can't be an in in the vertex shader" - has_color: f32, + pub has_color: f32, } ::window::glium::implement_vertex!( Vertex, position, adjust, tex, underline, bg_color, fg_color, has_color @@ -114,250 +110,6 @@ impl<'a> Quad<'a> { } } -struct SoftwareRenderState { - glyph_cache: RefCell>, - util_sprites: UtilSprites, -} - -impl SoftwareRenderState { - pub fn new( - fonts: &Rc, - metrics: &RenderMetrics, - size: usize, - ) -> Fallible { - let glyph_cache = RefCell::new(GlyphCache::new(fonts, size)); - let util_sprites = UtilSprites::new(&mut glyph_cache.borrow_mut(), metrics)?; - Ok(Self { - glyph_cache, - util_sprites, - }) - } -} - -struct OpenGLRenderState { - context: Rc, - glyph_cache: RefCell>, - util_sprites: UtilSprites, - program: glium::Program, - glyph_vertex_buffer: RefCell>, - glyph_index_buffer: IndexBuffer, -} - -impl OpenGLRenderState { - pub fn new( - context: Rc, - fonts: &Rc, - metrics: &RenderMetrics, - size: usize, - pixel_width: usize, - pixel_height: usize, - ) -> Fallible { - let glyph_cache = RefCell::new(GlyphCache::new_gl(&context, fonts, size)?); - let util_sprites = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics)?; - - let mut errors = vec![]; - let mut program = None; - for version in &["330", "300 es"] { - let source = glium::program::ProgramCreationInput::SourceCode { - vertex_shader: &Self::vertex_shader(version), - fragment_shader: &Self::fragment_shader(version), - outputs_srgb: true, - tessellation_control_shader: None, - tessellation_evaluation_shader: None, - transform_feedback_varyings: None, - uses_point_size: false, - geometry_shader: None, - }; - log::error!("compiling a prog with version {}", version); - match glium::Program::new(&context, source) { - Ok(prog) => { - program = Some(prog); - break; - } - Err(err) => errors.push(err.to_string()), - }; - } - - let program = program.ok_or_else(|| { - failure::format_err!("Failed to compile shaders: {}", errors.join("\n")) - })?; - - let (glyph_vertex_buffer, glyph_index_buffer) = - Self::compute_vertices(&context, metrics, pixel_width as f32, pixel_height as f32)?; - - Ok(Self { - context, - glyph_cache, - util_sprites, - program, - glyph_vertex_buffer: RefCell::new(glyph_vertex_buffer), - glyph_index_buffer, - }) - } - - pub fn advise_of_window_size_change( - &mut self, - metrics: &RenderMetrics, - pixel_width: usize, - pixel_height: usize, - ) -> Fallible<()> { - let (glyph_vertex_buffer, glyph_index_buffer) = Self::compute_vertices( - &self.context, - metrics, - pixel_width as f32, - pixel_height as f32, - )?; - - *self.glyph_vertex_buffer.borrow_mut() = glyph_vertex_buffer; - self.glyph_index_buffer = glyph_index_buffer; - Ok(()) - } - - fn vertex_shader(version: &str) -> String { - format!("#version {}\n{}", version, include_str!("vertex.glsl")) - } - - fn fragment_shader(version: &str) -> String { - format!("#version {}\n{}", version, include_str!("fragment.glsl")) - } - - /// 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 and 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, - metrics: &RenderMetrics, - width: f32, - height: f32, - ) -> Fallible<(VertexBuffer, IndexBuffer)> { - let cell_width = metrics.cell_size.width as f32; - let cell_height = metrics.cell_size.height as f32; - let mut verts = Vec::new(); - let mut indices = Vec::new(); - - let num_cols = (width as usize + 1) / cell_width as usize; - let num_rows = (height as usize + 1) / cell_height as usize; - - for y in 0..num_rows { - for x in 0..num_cols { - let y_pos = (height / -2.0) + (y as f32 * cell_height); - let x_pos = (width / -2.0) + (x as f32 * cell_width); - // Remember starting index for this position - let idx = verts.len() as u32; - verts.push(Vertex { - // Top left - position: (x_pos, y_pos), - ..Default::default() - }); - verts.push(Vertex { - // Top Right - position: (x_pos + cell_width, y_pos), - ..Default::default() - }); - verts.push(Vertex { - // Bottom Left - position: (x_pos, y_pos + cell_height), - ..Default::default() - }); - verts.push(Vertex { - // Bottom Right - position: (x_pos + cell_width, y_pos + cell_height), - ..Default::default() - }); - - // Emit two triangles to form the glyph quad - indices.push(idx); - indices.push(idx + 1); - indices.push(idx + 2); - indices.push(idx + 1); - indices.push(idx + 2); - indices.push(idx + 3); - } - } - - Ok(( - VertexBuffer::dynamic(context, &verts)?, - IndexBuffer::new( - context, - glium::index::PrimitiveType::TrianglesList, - &indices, - )?, - )) - } -} - -#[allow(clippy::large_enum_variant)] -enum RenderState { - Software(SoftwareRenderState), - GL(OpenGLRenderState), -} - -impl RenderState { - pub fn recreate_texture_atlas( - &mut self, - fonts: &Rc, - metrics: &RenderMetrics, - size: Option, - ) -> Fallible<()> { - match self { - RenderState::Software(software) => { - let size = size.unwrap_or_else(|| software.glyph_cache.borrow().atlas.size()); - let mut glyph_cache = GlyphCache::new(fonts, size); - software.util_sprites = UtilSprites::new(&mut glyph_cache, metrics)?; - *software.glyph_cache.borrow_mut() = glyph_cache; - } - RenderState::GL(gl) => { - let size = size.unwrap_or_else(|| gl.glyph_cache.borrow().atlas.size()); - let mut glyph_cache = GlyphCache::new_gl(&gl.context, fonts, size)?; - gl.util_sprites = UtilSprites::new(&mut glyph_cache, metrics)?; - *gl.glyph_cache.borrow_mut() = glyph_cache; - } - }; - Ok(()) - } - - pub fn advise_of_window_size_change( - &mut self, - metrics: &RenderMetrics, - pixel_width: usize, - pixel_height: usize, - ) -> Fallible<()> { - if let RenderState::GL(gl) = self { - gl.advise_of_window_size_change(metrics, pixel_width, pixel_height)?; - } - Ok(()) - } - - pub fn cached_software_glyph( - &self, - info: &GlyphInfo, - style: &TextStyle, - ) -> Fallible>> { - if let RenderState::Software(software) = self { - software.glyph_cache.borrow_mut().cached_glyph(info, style) - } else { - failure::bail!("attempted to call cached_software_glyph when in gl mode") - } - } - - pub fn software(&self) -> &SoftwareRenderState { - match self { - RenderState::Software(software) => software, - _ => panic!("only valid for software render mode"), - } - } - - pub fn opengl(&self) -> &OpenGLRenderState { - match self { - RenderState::GL(gl) => gl, - _ => panic!("only valid for opengl render mode"), - } - } -} - pub struct TermWindow { window: Option, fonts: Rc,