Debian Codegraph @

tests/vim_24bitcolors_bce

raw
1 % [[email protected] ➜  ~/code/alacritty  [?1h=[?2004h
2 bck-i-search: _mv ../../../{grid.json,size.json,alacritty.recording} ./v_cd vim_large_window_scroll i_imm_     vvivim ssrrcc//rreenndderer/r [?1l>[?2004l [?1049h[?1h=▽ [?12;25h[?12l[?25h[?25l"src/renderer" is a directory[>c" ============================================================================  
3 " Netrw Directory Listing (netrw v156)  
4 " /Users/jwilm/code/alacritty/src/renderer 
5 " Sorted by name 
6 " Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
7 " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special 
8 " ============================================================================== 
9 ../ ./ 
10 mod.rs 
11 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 8,1All[?12l[?25h[?25l../ ./ 9[?12l[?25h[?25l./ mod.rs 10,1[?12l[?25h[?25l~/code/alacritty/src/renderer/mod.rs"1354L, 40527C 864  }  
12  865   
13  866  pub fn deactivate(&self) {   
14  867  unsafe {   
15  868  gl::UseProgram(0);   869  }  
16  870  }  
17  871   
18  872  pub fn new(  
19  873  config: &Config,  874 size: Size<Pixels<u32>>   875 ) -> Result<ShaderProgram, ShaderCreationError> {   876 let vertex_source = if cfg!(feature = "live-shader-reload") {   877 None   878 } else {   879 Some(TEXT_SHADER_V)   880 };   881 let vertex_shader = ShaderProgram::create_shader(   882 TEXT_SHADER_V_PATH,   883 gl::VERTEX_SHADER,   884 vertex_source   885 )?;   886 let frag_source = if cfg!(feature = "live-shader-reload") {   887 None   888 } else {   889 Some(TEXT_SHADER_F)   890 };   891 let fragment_shader = ShaderProgram::create_shader(   892 TEXT_SHADER_F_PATH,   893 gl::FRAGMENT_SHADER,   894 frag_source   895 )?;   896 let program = ShaderProgram::create_program(vertex_shader, fragment_shader);   897   898 unsafe {   899 gl::DeleteShader(vertex_shader);   900 gl::DeleteShader(fragment_shader);   901 gl::UseProgram(program);   902 }   903   904 macro_rules! cptr {   905 ($thing:expr) => { $thing.as_ptr() as *const _ }   906 }   907   908 macro_rules! assert_uniform_valid {   909 ($uniform:expr) => {   910 assert!($uniform != gl::INVALID_VALUE as i32);   911 assert!($uniform != gl::INVALID_OPERATION as i32);   912 };   913 ( $( $uniform:expr ),* ) => {   914 $( assert_uniform_valid!($uniform); )*   915 };   916 }   917   918 // get uniform locations   919 let (projection, term_dim, cell_dim, visual_bell, background) = unsafe {  891,166%[?12l[?25h[?25l 1 // Copyright 2016 Joe Wilm, The Alacritty Project Contributors 
20  2 // 
21  3 // Licensed under the Apache License, Version 2.0 (the "License"); 
22  4 // you may not use this file except in compliance with the License. 
23  5 // You may obtain a copy of the License at 
24  6 // 
25  7 // http://www.apache.org/licenses/LICENSE-2.0 
26  8 // 
27  9 // Unless required by applicable law or agreed to in writing, software 
28  10 // distributed under the License is distributed on an "AS IS" BASIS, 
29  11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
30  12 // See the License for the specific language governing permissions and 
31  13 // limitations under the License. 
32  14 use std::collections::HashMap; 
33  15 use std::hash::BuildHasherDefault; 
34  16 use std::fs::File; 
35  17 use std::io::{self, Read}; 
36  18 use std::mem::size_of; 
37  19 use std::path::{PathBuf}; 
38  20 use std::ptr; 
39  21 use std::sync::mpsc; 
40  22  
41  23 use cgmath; 
42  24 use fnv::FnvHasher; 
43  25 use font::{self, Rasterizer, Rasterize, RasterizedGlyph, FontDesc, GlyphKey, FontKey}; 
44  26 use gl::types::*; 
45  27 use gl; 
46  28 use index::{Line, Column, RangeInclusive}; 
47  29 use notify::{Watcher as WatcherApi, RecommendedWatcher as Watcher, op}; 
48  30  
49  31 use config::{self, Config, Delta}; 
50  32 use term::{self, cell, RenderableCell}; 
51  33 use window::{Size, Pixels}; 
52  34  
53  35 use Rgb; 
54  36  
55  37 // Shader paths for live reload 
56  38 static TEXT_SHADER_F_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.f.glsl");  
57  39 static TEXT_SHADER_V_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.v.glsl");  
58  40  
59  41 // Shader source which is used when live-shader-reload feature is disable 
60  42 static TEXT_SHADER_F: &'static str = include_str!( 
61  43  concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.f.glsl") 
62  44 ); 
63  45 static TEXT_SHADER_V: &'static str = include_str!( 
64  46  concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.v.glsl") 
65  47 ); 
66  48  
67  49 /// `LoadGlyph` allows for copying a rasterized glyph into graphics memory 
68  50 pub trait LoadGlyph { 
69  51  /// Load the rasterized glyph into GPU memory 
70  52  fn load_glyph(&mut self, rasterized: &RasterizedGlyph) -> Glyph; 
71  53 } 
72  54  
73  55 enum Msg { 
74  56  ShaderReload, 1,1Top[?12l[?25h[?25l:[?12l[?25hs[?25l[?12l[?25he[?25l[?12l[?25ht[?25l[?12l[?25h[?25l ft=yaml[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25hc[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25hc[?25l[?12l[?25ho[?25l[?12l[?25hl[?25l[?12l[?25ho[?25l[?12l[?25hr[?25l[?12l[?25hs[?25l[?12l[?25hh[?25l[?12l[?25h[?25l[?12l[?25hc[?25l[?12l[?25hh[?25l[?12l[?25he[?25l[?12l[?25hm[?25l[?12l[?25he[?25l[?12l[?25h[?25l [?12l[?25h...[?25lTomorrow-Night-Bright[?12l[?25h...[?25lblue[?12l[?25h...[?25ldarkblue[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25h[?25l[?12l[?25ht[?25l[?12l[?25h...[?25lTomorrow-Night-Bright[?12l[?25h...[?25ltende[?12l[?25h [?25l 1 // Copyright 2016 Joe Wilm, The Alacritty Project Contributors 
75  2 // 
76  3 // Licensed under the Apache License, Version 2.0 (the "License"); 
77  4 // you may not use this file except in compliance with the License. 
78  5 // You may obtain a copy of the License at 
79  6 // 
80  7 // http://www.apache.org/licenses/LICENSE-2.0 
81  8 // 
82  9 // Unless required by applicable law or agreed to in writing, software 
83  10 // distributed under the License is distributed on an "AS IS" BASIS, 
84  11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
85  12 // See the License for the specific language governing permissions and 
86  13 // limitations under the License. 
87  14 use std::collections::HashMap; 
88  15 use std::hash::BuildHasherDefault; 
89  16 use std::fs::File; 
90  17 use std::io::{self, Read}; 
91  18 use std::mem::size_of; 
92  19 use std::path::{PathBuf}; 
93  20 use std::ptr; 
94  21 use std::sync::mpsc; 
95  22  
96  23 use cgmath; 
97  24 use fnv::FnvHasher; 
98  25 use font::{self, Rasterizer, Rasterize, RasterizedGlyph, FontDesc, GlyphKey, FontKey}; 
99  26 use gl::types::*; 
100  27 use gl; 
101  28 use index::{Line, Column, RangeInclusive}; 
102  29 use notify::{Watcher as WatcherApi, RecommendedWatcher as Watcher, op}; 
103  30  
104  31 use config::{self, Config, Delta}; 
105  32 use term::{self, cell, RenderableCell}; 
106  33 use window::{Size, Pixels}; 
107  34  
108  35 use Rgb; 
109  36  
110  37 // Shader paths for live reload 
111  38 static TEXT_SHADER_F_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.f.glsl");  
112  39 static TEXT_SHADER_V_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.v.glsl");  
113  40  
114  41 // Shader source which is used when live-shader-reload feature is disable 
115  42 static TEXT_SHADER_F: &'static str = include_str!( 
116  43  concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.f.glsl") 
117  44 ); 
118  45 static TEXT_SHADER_V: &'static str = include_str!( 
119  46  concat!(env!("CARGO_MANIFEST_DIR"), "/res/text.v.glsl") 
120  47 ); 
121  48  
122  49 /// `LoadGlyph` allows for copying a rasterized glyph into graphics memory 
123  50 pub trait LoadGlyph { 
124  51  /// Load the rasterized glyph into GPU memory 
125  52  fn load_glyph(&mut self, rasterized: &RasterizedGlyph) -> Glyph; 
126  53 } 
127  54  
128  55 enum Msg { 
129  56  ShaderReload, 1,1Top[?12l[?25h[?25l2[?12l[?25h[?25l3[?12l[?25h[?25l4[?12l[?25h[?25l5[?12l[?25h[?25l6[?12l[?25h[?25l7[?12l[?25h[?25l8[?12l[?25h[?25l9[?12l[?25h[?25l10,1[?12l[?25h[?25l1[?12l[?25h[?25l2[?12l[?25h[?25l3[?12l[?25h[?25l4[?12l[?25h[?25l5[?12l[?25h[?25l6[?12l[?25h[?25l7[?12l[?25h[?25l8[?12l[?25h[?25l9[?12l[?25h[?25l20[?12l[?25h[?25l1[?12l[?25h[?25l2,0-1[?12l[?25h[?25l3,1 [?12l[?25h[?25l4[?12l[?25h[?25l5[?12l[?25h[?25l6[?12l[?25h[?25l7[?12l[?25h[?25l8[?12l[?25h[?25l9[?12l[?25h[?25l30,0-1[?12l[?25h[?25l1,1 [?12l[?25h[?25l2[?12l[?25h[?25l3[?12l[?25h[?25l4,0-1[?12l[?25h[?25l5,1 [?12l[?25h[?25l6,0-1[?12l[?25h[?25l7,1 [?12l[?25h[?25l8[?12l[?25h[?25l9[?12l[?25h[?25l40,0-1[?12l[?25h[?25l1,1 [?12l[?25h[?25l2[?12l[?25h[?25l3[?12l[?25h[?25l()4[?12l[?25h[?25l( );5[?12l[?25h[?25l6[?12l[?25h[?25l()7[?12l[?25h[?25l( );8,0-1[?12l[?25h[?25l9,1 [?12l[?25h[?25l50[?12l[?25h[?25l1[?12l[?25h[?25l2[?12l[?25h[?25l{}3[?12l[?25h[?25l{ } 4,0-1[?12l[?25h[?25l5,1 [?12l[?25h[?25l6[?12l[?25h[?25l
130 {
131
132  57 } 57,10%[?12l[?25h[?25l
133 { }
134  58  58,0-10%[?12l[?25h[?25l
135  59 #[derive(Debug)] 59,10%[?12l[?25h[?25l
136  60 pub enum Error { 60,10%[?12l[?25h[?25l
137  61  ShaderCreation(ShaderCreationError), 61,10%[?12l[?25h[?25l
138 {
139
140  62 } 62,10%[?12l[?25h[?25l
141 { }
142  63  63,0-10%[?12l[?25h[?25l
143  64 impl ::std::error::Error for Error { 64,10%[?12l[?25h[?25l
144  65  fn cause(&self) -> Option<&::std::error::Error> { 65,10%[?12l[?25h[?25l
145  66 match *self { 66,10%[?12l[?25h[?25l
146  67 Error::ShaderCreation(ref err) => Some(err), 67,10%[?12l[?25h[?25l
147  68 } 68,10%[?12l[?25h[?25l
148  69  } 69,11%[?12l[?25h[?25l
149  70  70,0-11%[?12l[?25h[?25l
150  71  fn description(&self) -> &str { 71,11%[?12l[?25h[?25l
151  72 match *self { 72,11%[?12l[?25h[?25l
152  73 Error::ShaderCreation(ref err) => err.description(), 73,11%[?12l[?25h[?25l
153  74 } 74,11%[?12l[?25h[?25l
154  75  } 75,11%[?12l[?25h[?25l
155 { 76 } 76,11%[?12l[?25h[?25l
156 { }
157  77  77,0-11%[?12l[?25h[?25l
158  78 impl ::std::fmt::Display for Error { 78,11%[?12l[?25h[?25l
159  79  fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { 79,11%[?12l[?25h[?25l
160  80 match *self { 80,11%[?12l[?25h[?25l
161  81 Error::ShaderCreation(ref err) => { 81,11%[?12l[?25h[?25l
162  82 write!(f, "There was an error initializing the shaders: {}", err) 82,12%[?12l[?25h[?25l
163  83 } 83,12%[?12l[?25h[?25l
164  84 } 84,12%[?12l[?25h[?25l
165  85  } 85,12%[?12l[?25h[?25l
166 { 86 } 86,12%[?12l[?25h[?25l
167 { }
168  87  87,0-12%[?12l[?25h[?25l
169  88 impl From<ShaderCreationError> for Error { 88,12%[?12l[?25h[?25l
170  89  fn from(val: ShaderCreationError) -> Error { 89,12%[?12l[?25h[?25l
171  90 Error::ShaderCreation(val) 90,12%[?12l[?25h[?25l
172  91  } 91,12%[?12l[?25h[?25l
173 { 92 } 92,12%[?12l[?25h[?25l
174 { }
175  93  93,0-12%[?12l[?25h[?25l
176  94  94,0-12%[?12l[?25h[?25l
177  95 /// Text drawing program 95,13%[?12l[?25h[?25l
178  96 /// 96,13%[?12l[?25h[?25l
179  97 /// Uniforms are prefixed with "u", and vertex attributes are prefixed with "a". 97,13%[?12l[?25h[?25l
180  98 #[derive(Debug)] 98,13%[?12l[?25h[?25l
181  99 pub struct ShaderProgram { 99,13%[?12l[?25h[?25l
182  100  // Program id 100,13%[?12l[?25h[?25l
183  101  id: GLuint, 101,13%[?12l[?25h[?25l
184  102  102,0-13%[?12l[?25h[?25l
185  103  /// projection matrix uniform 103,13%[?12l[?25h[?25l
186  104  u_projection: GLint, 104,13%[?12l[?25h[?25l
187  105  105,0-13%[?12l[?25h[?25l
188  106  /// Terminal dimensions (pixels) 106,13%[?12l[?25h[?25l
189  107  u_term_dim: GLint, 107,13%[?12l[?25h[?25l
190  108  108,0-14%[?12l[?25h[?25l
191  109  /// Cell dimensions (pixels) 109,14%[?12l[?25h[?25l
192  110  u_cell_dim: GLint, 110,14%[?12l[?25h[?25l
193  111  111,0-14%[?12l[?25h[?25l
194  112  /// Visual bell 112,14%[?12l[?25h[?25l
195  113  u_visual_bell: GLint, 113,14%[?12l[?25h[?25l
196  114  114,0-14%[?12l[?25h[?25l
197  115  /// Background pass flag 115,14%[?12l[?25h[?25l
198  116  /// 116,14%[?12l[?25h[?25l
199  117  /// Rendering is split into two passes; 1 for backgrounds, and one for text 117,14%[?12l[?25h[?25l
200  118  u_background: GLint, 118,14%[?12l[?25h[?25l
201  119  119,0-14%[?12l[?25h[?25l
202  120  padding_x: f32, 120,14%[?12l[?25h[?25l
203  121  padding_y: f32, 121,15%[?12l[?25h[?25l
204 { 122 } 122,15%[?12l[?25h[?25l
205 { }
206  123  123,0-15%[?12l[?25h[?25l
207  124  124,0-15%[?12l[?25h[?25l
208  125 #[derive(Debug, Clone)] 125,15%[?12l[?25h[?25l
209  126 pub struct Glyph { 126,15%[?12l[?25h[?25l
210  127  tex_id: GLuint, 127,15%[?12l[?25h[?25l
211  128  top: f32, 128,15%[?12l[?25h[?25l
212  129  left: f32, 129,15%[?12l[?25h[?25l
213  130  width: f32, 130,15%[?12l[?25h[?25l
214  131  height: f32, 131,15%[?12l[?25h[?25l
215  132  uv_bot: f32, 132,15%[?12l[?25h[?25l
216  133  uv_left: f32, 133,15%[?12l[?25h[?25l
217  134  uv_width: f32, 134,16%[?12l[?25h[?25l
218  135  uv_height: f32, 135,16%[?12l[?25h[?25l
219 { 136 } 136,16%[?12l[?25h[?25l
220 { }
221  137  137,0-16%[?12l[?25h[?25l
222  138 /// Naïve glyph cache 138,16%[?12l[?25h[?25l
223  139 /// 139,16%[?12l[?25h[?25l
224  140 /// Currently only keyed by `char`, and thus not possible to hold different 140,16%[?12l[?25h[?25l
225  141 /// representations of the same code point. 141,16%[?12l[?25h[?25l
226  142 pub struct GlyphCache { 142,16%[?12l[?25h[?25l
227  143  /// Cache of buffered glyphs 143,16%[?12l[?25h[?25l
228  144  cache: HashMap<GlyphKey, Glyph, BuildHasherDefault<FnvHasher>>, 144,16%[?12l[?25h[?25l
229  145  145,0-16%[?12l[?25h[?25l
230  146  /// Rasterizer for loading new glyphs 146,16%[?12l[?25h[?25l
231  147  rasterizer: Rasterizer, 147,17%[?12l[?25h[?25l
232  148  148,0-17%[?12l[?25h[?25l
233  149  /// regular font 149,17%[?12l[?25h[?25l
234  150  font_key: FontKey, 150,17%[?12l[?25h[?25l
235  151  151,0-17%[?12l[?25h[?25l
236  152  /// italic font 152,17%[?12l[?25h[?25l
237  153  italic_key: FontKey, 153,17%[?12l[?25h[?25l
238  154  154,0-17%[?12l[?25h[?25l
239  155  /// bold font 155,17%[?12l[?25h[?25l
240  156  bold_key: FontKey, 156,17%[?12l[?25h[?25l
241  157  157,0-17%[?12l[?25h[?25l
242  158  /// font size 158,17%[?12l[?25h[?25l
243  159  font_size: font::Size, 159,17%[?12l[?25h[?25l
244  160  160,0-18%[?12l[?25h[?25l
245  161  /// glyph offset 161,18%[?12l[?25h[?25l
246  162  glyph_offset: Delta, 162,18%[?12l[?25h[?25l
247  163  163,0-18%[?12l[?25h[?25l
248  164  metrics: ::font::Metrics, 164,18%[?12l[?25h[?25l
249 { 165 } 165,18%[?12l[?25h[?25l
250 { }
251  166  166,0-18%[?12l[?25h[?25l
252  167 impl GlyphCache { 167,18%[?12l[?25h[?25l
253  168  pub fn new<L>( 168,18%[?12l[?25h[?25l
254  169 mut rasterizer: Rasterizer, 169,18%[?12l[?25h[?25l
255  170 config: &Config, 170,18%[?12l[?25h[?25l
256  171 loader: &mut L 171,18%[?12l[?25h[?25l
257  172  ) -> Result<GlyphCache, font::Error> 172,18%[?12l[?25h[?25l
258  173 where L: LoadGlyph 173,19%[?12l[?25h[?25l
259  174  { 174,19%[?12l[?25h[?25l
260  175 let font = config.font(); 175,19%[?12l[?25h[?25l
261  176 let size = font.size(); 176,19%[?12l[?25h[?25l
262  177 let glyph_offset = *font.glyph_offset(); 177,19%[?12l[?25h[?25l
263  178  178,0-19%[?12l[?25h[?25l
264  179 fn make_desc( 179,19%[?12l[?25h[?25l
265  180 desc: &config::FontDescription, 180,19%[?12l[?25h[?25l
266  181 slant: font::Slant, 181,19%[?12l[?25h[?25l
267  182 weight: font::Weight, 182,19%[?12l[?25h[?25l
268  183 ) -> FontDesc 183,19%[?12l[?25h[?25l
269  184 { 184,19%[?12l[?25h[?25l
270  185 let style = if let Some(ref spec) = desc.style { 185,19%[?12l[?25h[?25l
271  186 font::Style::Specific(spec.to_owned()) 186,110%[?12l[?25h[?25l
272  187 } else { 187,110%[?12l[?25h[?25l
273  188 font::Style::Description {slant:slant, weight:weight} 188,110%[?12l[?25h[?25l
274  189 }; 189,110%[?12l[?25h[?25l
275  190 FontDesc::new(&desc.family[..], style) 190,110%[?12l[?25h[?25l
276  191 } 191,110%[?12l[?25h[?25l
277  192  192,0-110%[?12l[?25h[?25l
278  193 // Load regular font 193,110%[?12l[?25h[?25l
279  194 let regular_desc = make_desc(&font.normal, font::Slant::Normal, font::Weight::Normal);  194,110%[?12l[?25h[?25l
280  195  195,0-110%[?12l[?25h[?25l
281  196 let regular = rasterizer 196,110%[?12l[?25h[?25l
282  197 .load_font(&regular_desc, size)?; 197,110%[?12l[?25h[?25l
283  198  198,0-110%[?12l[?25h[?25l
284  199 // helper to load a description if it is not the regular_desc 199,111%[?12l[?25h[?25l
285  200 let load_or_regular = |desc:FontDesc, rasterizer: &mut Rasterizer| { 200,111%[?12l[?25h[?25l
286  201 if desc == regular_desc { 201,111%[?12l[?25h[?25l
287  202 regular 202,111%[?12l[?25h[?25l
288  203 } else { 203,111%[?12l[?25h[?25l
289  204 rasterizer.load_font(&desc, size).unwrap_or_else(|_| regular) 204,111%[?12l[?25h[?25l
290  205 } 205,111%[?12l[?25h[?25l
291  206 }; 206,111%[?12l[?25h[?25l
292  207  207,0-111%[?12l[?25h[?25l
293  208 // Load bold font 208,111%[?12l[?25h[?25l
294  209 let bold_desc = make_desc(&font.bold, font::Slant::Normal, font::Weight::Bold); 209,111%[?12l[?25h[?25l
295  210  210,0-111%[?12l[?25h[?25l
296  211 let bold = load_or_regular(bold_desc, &mut rasterizer); 211,111%[?12l[?25h[?25l
297  212  212,0-112%[?12l[?25h[?25l
298  213 // Load italic font 213,112%[?12l[?25h[?25l
299  214 let italic_desc = make_desc(&font.italic, font::Slant::Italic, font::Weight::Normal); 214,112%[?12l[?25h[?25l
300  215  215,0-112%[?12l[?25h[?25l
301  216 let italic = load_or_regular(italic_desc, &mut rasterizer); 216,112%[?12l[?25h[?25l
302  217  217,0-112%[?12l[?25h[?25l
303  218 // Need to load at least one glyph for the face before calling metrics. 218,112%[?12l[?25h[?25l
304  219 // The glyph requested here ('m' at the time of writing) has no special 219,112%[?12l[?25h[?25l
305  220 // meaning. 220,112%[?12l[?25h[?25l
306  221 rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm', size: font.size() })?; 221,112%[?12l[?25h[?25l
307  222 let metrics = rasterizer.metrics(regular)?; 222,112%[?12l[?25h[?25l
308  223  223,0-112%[?12l[?25h[?25l
309  224 let mut cache = GlyphCache { 224,112%[?12l[?25h[?25l
310  225 cache: HashMap::default(), 225,113%[?12l[?25h[?25l
311  226 rasterizer: rasterizer, 226,113%[?12l[?25h[?25l
312  227 font_size: font.size(), 227,113%[?12l[?25h[?25l
313  228 font_key: regular, 228,113%[?12l[?25h[?25l
314  229 bold_key: bold, 229,113%[?12l[?25h[?25l
315  230 italic_key: italic, 230,113%[?12l[?25h[?25l
316  231 glyph_offset: glyph_offset, 231,113%[?12l[?25h[?25l
317  232 metrics: metrics 232,113%[?12l[?25h[?25l
318  233 }; 233,113%[?12l[?25h[?25l
319  234  234,0-113%[?12l[?25h[?25l
320  235 macro_rules! load_glyphs_for_font { 235,113%[?12l[?25h[?25l
321  236 ($font:expr) => { 236,113%[?12l[?25h[?25l
322  237 for i in RangeInclusive::new(32u8, 128u8) { 237,113%[?12l[?25h[?25l
323  238 cache.get(&GlyphKey { 238,114%[?12l[?25h[?25l
324  239 font_key: $font, 239,114%[?12l[?25h[?25l
325  240 c: i as char, 240,114%[?12l[?25h[?25l
326  241 size: font.size() 241,114%[?12l[?25h[?25l
327  242 }, loader); 242,114%[?12l[?25h[?25l
328  243 } 243,114%[?12l[?25h[?25l
329  244 } 244,114%[?12l[?25h[?25l
330  245 } 245,114%[?12l[?25h[?25l
331  246  246,0-114%[?12l[?25h[?25l
332  247 load_glyphs_for_font!(regular); 247,114%[?12l[?25h[?25l
333  248 load_glyphs_for_font!(bold); 248,114%[?12l[?25h[?25l
334  249 load_glyphs_for_font!(italic); 249,114%[?12l[?25h[?25l
335  250  250,0-114%[?12l[?25h[?25l
336  251 Ok(cache) 251,115%[?12l[?25h[?25l
337  252  } 252,115%[?12l[?25h[?25l
338  253  253,0-115%[?12l[?25h[?25l
339  254  pub fn font_metrics(&self) -> font::Metrics { 254,115%[?12l[?25h[?25l
340  255 self.rasterizer 255,115%[?12l[?25h[?25l
341  256 .metrics(self.font_key) 256,115%[?12l[?25h[?25l
342  257 .expect("metrics load since font is loaded at glyph cache creation") 257,115%[?12l[?25h[?25l
343  258  } 258,115%[?12l[?25h[?25l
344  259  259,0-115%[?12l[?25h[?25l
345  260  pub fn get<'a, L>(&'a mut self, glyph_key: &GlyphKey, loader: &mut L) -> &'a Glyph 260,115%[?12l[?25h[?25l
346  261 where L: LoadGlyph 261,115%[?12l[?25h[?25l
347  262  { 262,115%[?12l[?25h[?25l
348  263 let glyph_offset = self.glyph_offset; 263,115%[?12l[?25h[?25l
349  264 let rasterizer = &mut self.rasterizer; 264,116%[?12l[?25h[?25l
350  265 let metrics = &self.metrics; 265,116%[?12l[?25h[?25l
351  266 self.cache 266,116%[?12l[?25h[?25l
352  267 .entry(*glyph_key) 267,116%[?12l[?25h[?25l
353  268 .or_insert_with(|| { 268,116%[?12l[?25h[?25l
354  269 let mut rasterized = rasterizer.get_glyph(&glyph_key) 269,116%[?12l[?25h[?25l
355  270 .unwrap_or_else(|_| Default::default()); 270,116%[?12l[?25h[?25l
356  271  271,0-116%[?12l[?25h[?25l
357  272 rasterized.left += glyph_offset.x as i32; 272,116%[?12l[?25h[?25l
358  273 rasterized.top += glyph_offset.y as i32; 273,116%[?12l[?25h[?25l
359  274 rasterized.top -= metrics.descent as i32; 274,116%[?12l[?25h[?25l
360  275  275,0-116%[?12l[?25h[?25l
361  276 loader.load_glyph(&rasterized) 276,116%[?12l[?25h[?25l
362  277 }) 277,117%[?12l[?25h[?25l
363  278  } 278,117%[?12l[?25h[?25l
364  279 } 279,117%[?12l[?25h[?25l
365  280  280,0-117%[?12l[?25h[?25l
366  281 #[derive(Debug)] 281,117%[?12l[?25h[?25l
367  282 #[repr(C)] 282,117%[?12l[?25h[?25l
368  283 struct InstanceData { 283,117%[?12l[?25h[?25l
369  284  // coords 284,117%[?12l[?25h[?25l
370  285  col: f32, 285,117%[?12l[?25h[?25l
371  286  row: f32, 286,117%[?12l[?25h[?25l
372  287  // glyph offset 287,117%[?12l[?25h[?25l
373  288  left: f32, 288,117%[?12l[?25h[?25l
374  289  top: f32, 289,117%[?12l[?25h[?25l
375  290  // glyph scale 290,118%[?12l[?25h[?25l
376  291  width: f32, 291,118%[?12l[?25h[?25l
377  292  height: f32, 292,118%[?12l[?25h[?25l
378  293  // uv offset 293,118%[?12l[?25h[?25l
379  294  uv_left: f32, 294,118%[?12l[?25h[?25l
380  295  uv_bot: f32, 295,118%[?12l[?25h[?25l
381  296  // uv scale 296,118%[?12l[?25h[?25l
382  297  uv_width: f32, 297,118%[?12l[?25h[?25l
383  298  uv_height: f32, 298,118%[?12l[?25h[?25l
384  299  // color 299,118%[?12l[?25h[?25l
385  300  r: f32, 300,118%[?12l[?25h[?25l
386  301  g: f32, 301,118%[?12l[?25h[?25l
387  302  b: f32, 302,118%[?12l[?25h[?25l
388  303  // background color 303,119%[?12l[?25h[?25l
389  304  bg_r: f32, 304,119%[?12l[?25h[?25l
390  305  bg_g: f32, 305,119%[?12l[?25h[?25l
391  306  bg_b: f32, 306,119%[?12l[?25h[?25l
392 { 307 } 307,119%[?12l[?25h[?25l
393 { }
394  308  308,0-119%[?12l[?25h[?25l
395  309 #[derive(Debug)] 309,119%[?12l[?25h[?25l
396  310 pub struct QuadRenderer { 310,119%[?12l[?25h[?25l
397  311  program: ShaderProgram, 311,119%[?12l[?25h[?25l
398  312  vao: GLuint, 312,119%[?12l[?25h[?25l
399  313  vbo: GLuint, 313,119%[?12l[?25h[?25l
400  314  ebo: GLuint, 314,119%[?12l[?25h[?25l
401  315  vbo_instance: GLuint, 315,119%[?12l[?25h[?25l
402  316  atlas: Vec<Atlas>, 316,120%[?12l[?25h[?25l
403  317  active_tex: GLuint, 317,120%[?12l[?25h[?25l
404  318  batch: Batch, 318,120%[?12l[?25h[?25l
405  319  rx: mpsc::Receiver<Msg>, 319,120%[?12l[?25h[?25l
406 { 320 } 320,120%[?12l[?25h[?25l
407 { }
408  321  321,0-120%[?12l[?25h[?25l
409  322 #[derive(Debug)] 322,120%[?12l[?25h[?25l
410  323 pub struct RenderApi<'a> { 323,120%[?12l[?25h[?25l
411  324  active_tex: &'a mut GLuint, 324,120%[?12l[?25h[?25l
412  325  batch: &'a mut Batch, 325,120%[?12l[?25h[?25l
413  326  atlas: &'a mut Vec<Atlas>, 326,120%[?12l[?25h[?25l
414  327  program: &'a mut ShaderProgram, 327,120%[?12l[?25h[?25l
415  328  config: &'a Config, 328,120%[?12l[?25h[?25l
416  329  visual_bell_intensity: f32 329,121%[?12l[?25h[?25l
417 { 330 } 330,121%[?12l[?25h[?25l
418 { }
419  331  331,0-121%[?12l[?25h[?25l
420  332 #[derive(Debug)] 332,121%[?12l[?25h[?25l
421  333 pub struct LoaderApi<'a> { 333,121%[?12l[?25h[?25l
422  334  active_tex: &'a mut GLuint, 334,121%[?12l[?25h[?25l
423  335  atlas: &'a mut Vec<Atlas>, 335,121%[?12l[?25h[?25l
424 {
425
426
427  336 } 336,121%[?12l[?25h[?25l
428 { }
429  337  337,0-121%[?12l[?25h[?25l
430  338 #[derive(Debug)] 338,121%[?12l[?25h[?25l
431  339 pub struct PackedVertex { 339,121%[?12l[?25h[?25l
432  340  x: f32, 340,121%[?12l[?25h[?25l
433  341  y: f32, 341,121%[?12l[?25h[?25l
434 {
435
436
437  342 } 342,122%[?12l[?25h[?25l
438 { }
439  343  343,0-122%[?12l[?25h[?25l
440  344 #[derive(Debug)] 344,122%[?12l[?25h[?25l
441  345 pub struct Batch { 345,122%[?12l[?25h[?25l
442  346  tex: GLuint, 346,122%[?12l[?25h[?25l
443  347  instances: Vec<InstanceData>, 347,122%[?12l[?25h[?25l 348 } 
444  349  
445  350 impl Batch { 
446  351  #[inline] 
447  352  pub fn new() -> Batch { 
448  353 Batch { 
449  354 tex: 0, 
450  355 instances: Vec::with_capacity(BATCH_MAX), 
451  356 } 
452  357  } 
453  358  
454  359  pub fn add_item( 
455  360 &mut self, 
456  361 cell: &RenderableCell, 
457  362 glyph: &Glyph, 
458  363  ) { 
459  364 if self.is_empty() { 
460  365 self.tex = glyph.tex_id; 
461  366 } 
462  367  
463  368 self.instances.push(InstanceData { 
464  369 col: cell.column.0 as f32, 
465  370 row: cell.line.0 as f32, 
466  371  
467  372 top: glyph.top, 
468  373 left: glyph.left, 
469  374 width: glyph.width, 
470  375 height: glyph.height, 375,1324%[?12l[?25h[?25l 376  
471  377 uv_bot: glyph.uv_bot, 
472  378 uv_left: glyph.uv_left, 
473  379 uv_width: glyph.uv_width, 
474  380 uv_height: glyph.uv_height, 
475  381  
476  382 r: cell.fg.r as f32, 
477  383 g: cell.fg.g as f32, 
478  384 b: cell.fg.b as f32, 
479  385  
480  386 bg_r: cell.bg.r as f32, 
481  387 bg_g: cell.bg.g as f32, 
482  388 bg_b: cell.bg.b as f32, 
483  389 }); 
484  390  } 
485  391  
486  392  #[inline] 
487  393  pub fn full(&self) -> bool { 
488  394 self.capacity() == self.len() 
489  395  } 
490  396  
491  397  #[inline] 
492  398  pub fn len(&self) -> usize { 
493  399 self.instances.len() 
494  400  } 
495  401  
496  402  #[inline] 
497  403  pub fn capacity(&self) -> usize { 403,526%[?12l[?25h[?25l 404 BATCH_MAX 
498  405  } 
499  406  
500  407  #[inline] 
501  408  pub fn is_empty(&self) -> bool { 
502  409 self.len() == 0 
503  410  } 
504  411  
505  412  #[inline] 
506  413  pub fn size(&self) -> usize { 
507  414 self.len() * size_of::<InstanceData>() 
508  415  } 
509  416  
510  417  pub fn clear(&mut self) { 
511  418 self.tex = 0; 
512  419 self.instances.clear(); 
513  420  } 
514  421 } 
515  422  
516  423 /// Maximum items to be drawn in a batch. 
517  424 const BATCH_MAX: usize = 65_536; 
518  425 const ATLAS_SIZE: i32 = 1024; 
519  426  
520  427 impl QuadRenderer { 
521  428  // TODO should probably hand this a transform instead of width/height 
522  429  pub fn new(config: &Config, size: Size<Pixels<u32>>) -> Result<QuadRenderer, Error> { 
523  430 let program = ShaderProgram::new(config, size)?; 
524  431  431,0-128%[?12l[?25h[?25l 432 let mut vao: GLuint = 0; 
525  433 let mut vbo: GLuint = 0; 
526  434 let mut ebo: GLuint = 0; 
527  435  
528  436 let mut vbo_instance: GLuint = 0; 
529  437  
530  438 unsafe { 
531  439 gl::Enable(gl::BLEND); 
532  440 gl::BlendFunc(gl::SRC1_COLOR, gl::ONE_MINUS_SRC1_COLOR); 
533  441 gl::Enable(gl::MULTISAMPLE); 
534  442  
535  443 gl::GenVertexArrays(1, &mut vao); 
536  444 gl::GenBuffers(1, &mut vbo); 
537  445 gl::GenBuffers(1, &mut ebo); 
538  446 gl::GenBuffers(1, &mut vbo_instance); 
539  447 gl::BindVertexArray(vao); 
540  448  
541  449 // ---------------------------- 
542  450 // setup vertex position buffer 
543  451 // ---------------------------- 
544  452 // Top right, Bottom right, Bottom left, Top left 
545  453 let vertices = [ 
546  454 PackedVertex { x: 1.0, y: 1.0 }, 
547  455 PackedVertex { x: 1.0, y: 0.0 }, 
548  456 PackedVertex { x: 0.0, y: 0.0 }, 
549  457 PackedVertex { x: 0.0, y: 1.0 }, 
550  458 ]; 
551  459  459,0-131%[?12l[?25h[?25l 460 gl::BindBuffer(gl::ARRAY_BUFFER, vbo); 
552  461  
553  462 gl::VertexAttribPointer(0, 2, 
554  463 gl::FLOAT, gl::FALSE, 
555  464 size_of::<PackedVertex>() as i32, 
556  465 ptr::null()); 
557  466 gl::EnableVertexAttribArray(0); 
558  467  
559  468 gl::BufferData(gl::ARRAY_BUFFER, 
560  469 (size_of::<PackedVertex>() * vertices.len()) as GLsizeiptr, 
561  470 vertices.as_ptr() as *const _, 
562  471 gl::STATIC_DRAW); 
563  472  
564  473 // --------------------- 
565  474 // Set up element buffer 
566  475 // --------------------- 
567  476 let indices: [u32; 6] = [0, 1, 3, 
568  477 1, 2, 3]; 
569  478  
570  479 gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ebo); 
571  480 gl::BufferData(gl::ELEMENT_ARRAY_BUFFER, 
572  481 (6 * size_of::<u32>()) as isize, 
573  482 indices.as_ptr() as *const _, 
574  483 gl::STATIC_DRAW); 
575  484  
576  485 // ---------------------------- 
577  486 // Setup vertex instance buffer 
578  487 // ---------------------------- 487,1333%[?12l[?25h[?25l 488 gl::BindBuffer(gl::ARRAY_BUFFER, vbo_instance); 
579  489 gl::BufferData(gl::ARRAY_BUFFER, 
580  490 (BATCH_MAX * size_of::<InstanceData>()) as isize, 
581  491 ptr::null(), gl::STREAM_DRAW); 
582  492 // coords 
583  493 gl::VertexAttribPointer(1, 2, 
584  494 gl::FLOAT, gl::FALSE, 
585  495 size_of::<InstanceData>() as i32, 
586  496 ptr::null()); 
587  497 gl::EnableVertexAttribArray(1); 
588  498 gl::VertexAttribDivisor(1, 1); 
589  499 // glyphoffset 
590  500 gl::VertexAttribPointer(2, 4, 
591  501 gl::FLOAT, gl::FALSE, 
592  502 size_of::<InstanceData>() as i32, 
593  503 (2 * size_of::<f32>()) as *const _); 
594  504 gl::EnableVertexAttribArray(2); 
595  505 gl::VertexAttribDivisor(2, 1); 
596  506 // uv 
597  507 gl::VertexAttribPointer(3, 4, 
598  508 gl::FLOAT, gl::FALSE, 
599  509 size_of::<InstanceData>() as i32, 
600  510 (6 * size_of::<f32>()) as *const _); 
601  511 gl::EnableVertexAttribArray(3); 
602  512 gl::VertexAttribDivisor(3, 1); 
603  513 // color 
604  514 gl::VertexAttribPointer(4, 3, 
605  515 gl::FLOAT, gl::FALSE, 515,3735%[?12l[?25h[?25l 516 size_of::<InstanceData>() as i32, 
606  517 (10 * size_of::<f32>()) as *const _); 
607  518 gl::EnableVertexAttribArray(4); 
608  519 gl::VertexAttribDivisor(4, 1); 
609  520 // color 
610  521 gl::VertexAttribPointer(5, 3, 
611  522 gl::FLOAT, gl::FALSE, 
612  523 size_of::<InstanceData>() as i32, 
613  524 (13 * size_of::<f32>()) as *const _); 
614  525 gl::EnableVertexAttribArray(5); 
615  526 gl::VertexAttribDivisor(5, 1); 
616  527  
617  528 gl::BindVertexArray(0); 
618  529 gl::BindBuffer(gl::ARRAY_BUFFER, 0); 
619  530 } 
620  531  
621  532 let (msg_tx, msg_rx) = mpsc::channel(); 
622  533  
623  534 if cfg!(feature = "live-shader-reload") { 
624  535 ::std::thread::spawn(move || { 
625  536 let (tx, rx) = ::std::sync::mpsc::channel(); 
626  537 let mut watcher = Watcher::new(tx).expect("create file watcher"); 
627  538 watcher.watch(TEXT_SHADER_F_PATH).expect("watch fragment shader"); 
628  539 watcher.watch(TEXT_SHADER_V_PATH).expect("watch vertex shader"); 
629  540  
630  541 loop { 
631  542 let event = rx.recv().expect("watcher event"); 
632  543 let ::notify::Event { path, op } = event; 543,2137%[?12l[?25h[?25l 544  
633  545 if let Ok(op) = op { 
634  546 if op.contains(op::RENAME) { 
635  547 continue; 
636  548 } 
637  549  
638  550 if op.contains(op::IGNORED) { 
639  551 if let Some(path) = path.as_ref() { 
640  552 if let Err(err) = watcher.watch(path) { 
641  553 warn!("failed to establish watch on {:?}: {:?}", path, err);  
642  554 } 
643  555 } 
644  556  
645  557 msg_tx.send(Msg::ShaderReload) 
646  558 .expect("msg send ok"); 
647  559 } 
648  560 } 
649  561 } 
650  562 }); 
651  563 } 
652  564  
653  565 let mut renderer = QuadRenderer { 
654  566 program: program, 
655  567 vao: vao, 
656  568 vbo: vbo, 
657  569 ebo: ebo, 
658  570 vbo_instance: vbo_instance, 
659  571 atlas: Vec::new(), 571,1339%[?12l[?25h[?25l 572 active_tex: 0, 
660  573 batch: Batch::new(), 
661  574 rx: msg_rx, 
662  575 }; 
663  576  
664  577 let atlas = Atlas::new(ATLAS_SIZE); 
665  578 renderer.atlas.push(atlas); 
666  579  
667  580 Ok(renderer) 
668  581  } 
669  582  
670  583  pub fn with_api<F, T>( 
671  584 &mut self, 
672  585 config: &Config, 
673  586 props: &term::SizeInfo, 
674  587 visual_bell_intensity: f64, 
675  588 func: F 
676  589  ) -> T 
677  590 where F: FnOnce(RenderApi) -> T 
678  591  { 
679  592 while let Ok(msg) = self.rx.try_recv() { 
680  593 match msg { 
681  594 Msg::ShaderReload => { 
682  595 self.reload_shaders(&config, Size { 
683  596 width: Pixels(props.width as u32), 
684  597 height: Pixels(props.height as u32) 
685  598 }); 
686  599 } 599,1741%[?12l[?25h[?25l{ }
687  600 } 
688  601 } 
689  602  
690  603 unsafe { 
691  604 self.program.activate(); 
692  605 self.program.set_term_uniforms(props); 
693  606 self.program.set_visual_bell(visual_bell_intensity as _); 
694  607  
695  608 gl::BindVertexArray(self.vao); 
696  609 gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.ebo); 
697  610 gl::BindBuffer(gl::ARRAY_BUFFER, self.vbo_instance); 
698  611 gl::ActiveTexture(gl::TEXTURE0); 
699  612 } 
700  613  
701  614 let res = func(RenderApi { 
702  615 active_tex: &mut self.active_tex, 
703  616 batch: &mut self.batch, 
704  617 atlas: &mut self.atlas, 
705  618 program: &mut self.program, 
706  619 visual_bell_intensity: visual_bell_intensity as _, 
707  620 config: config, 
708  621 }); 
709  622  
710  623 unsafe { 
711  624 gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0); 
712  625 gl::BindBuffer(gl::ARRAY_BUFFER, 0); 
713  626 gl::BindVertexArray(0); 
714  627  627,0-143%[?12l[?25h[?25l 628 self.program.deactivate(); 
715  629 } 
716  630  
717  631 res 
718  632  } 
719  633  
720  634  pub fn with_loader<F, T>(&mut self, func: F) -> T 
721  635 where F: FnOnce(LoaderApi) -> T 
722  636  { 
723  637 unsafe { 
724  638 gl::ActiveTexture(gl::TEXTURE0); 
725  639 } 
726  640  
727  641 func(LoaderApi { 
728  642 active_tex: &mut self.active_tex, 
729  643 atlas: &mut self.atlas, 
730  644 }) 
731  645  } 
732  646  
733  647  pub fn reload_shaders(&mut self, config: &Config, size: Size<Pixels<u32>>) { 
734  648 info!("Reloading shaders"); 
735  649 let program = match ShaderProgram::new(config, size) { 
736  650 Ok(program) => program, 
737  651 Err(err) => { 
738  652 match err { 
739  653 ShaderCreationError::Io(err) => { 
740  654 error!("Error reading shader file: {}", err); 
741  655 }, 655,2146%[?12l[?25h[?25l 572 active_tex: 0, 
742  573 batch: Batch::new(), 
743  574 rx: msg_rx, 
744  575 }; 
745  576  
746  577 let atlas = Atlas::new(ATLAS_SIZE); 
747  578 renderer.atlas.push(atlas); 
748  579  
749  580 Ok(renderer) 
750  581  } 
751  582  
752  583  pub fn with_api<F, T>( 
753  584 &mut self, 
754  585 config: &Config, 
755  586 props: &term::SizeInfo, 
756  587 visual_bell_intensity: f64, 
757  588 func: F 
758  589  ) -> T 
759  590 where F: FnOnce(RenderApi) -> T 
760  591  { 
761  592 while let Ok(msg) = self.rx.try_recv() { 
762  593 match msg { 
763  594 Msg::ShaderReload => { 
764  595 self.reload_shaders(&config, Size { 
765  596 width: Pixels(props.width as u32), 
766  597 height: Pixels(props.height as u32) 
767  598 }); 
768  599 } 627,0-143%[?12l[?25h[?25l 544  
769  545 if let Ok(op) = op { 
770  546 if op.contains(op::RENAME) { 
771  547 continue; 
772  548 } 
773  549  
774  550 if op.contains(op::IGNORED) { 
775  551 if let Some(path) = path.as_ref() { 
776  552 if let Err(err) = watcher.watch(path) { 
777  553 warn!("failed to establish watch on {:?}: {:?}", path, err);  
778  554 } 
779  555 } 
780  556  
781  557 msg_tx.send(Msg::ShaderReload) 
782  558 .expect("msg send ok"); 
783  559 } 
784  560 } 
785  561 } 
786  562 }); 
787  563 } 
788  564  
789  565 let mut renderer = QuadRenderer { 
790  566 program: program, 
791  567 vao: vao, 
792  568 vbo: vbo, 
793  569 ebo: ebo, 
794  570 vbo_instance: vbo_instance, 
795  571 atlas: Vec::new(), {}599,1741%[?12l[?25h[?25l 516 size_of::<InstanceData>() as i32, 
796  517 (10 * size_of::<f32>()) as *const _); 
797  518 gl::EnableVertexAttribArray(4); 
798  519 gl::VertexAttribDivisor(4, 1); 
799  520 // color 
800  521 gl::VertexAttribPointer(5, 3, 
801  522 gl::FLOAT, gl::FALSE, 
802  523 size_of::<InstanceData>() as i32, 
803  524 (13 * size_of::<f32>()) as *const _); 
804  525 gl::EnableVertexAttribArray(5); 
805  526 gl::VertexAttribDivisor(5, 1); 
806  527  
807  528 gl::BindVertexArray(0); 
808  529 gl::BindBuffer(gl::ARRAY_BUFFER, 0); 
809  530 } 
810  531  
811  532 let (msg_tx, msg_rx) = mpsc::channel(); 
812  533  
813  534 if cfg!(feature = "live-shader-reload") { 
814  535 ::std::thread::spawn(move || { 
815  536 let (tx, rx) = ::std::sync::mpsc::channel(); 
816  537 let mut watcher = Watcher::new(tx).expect("create file watcher"); 
817  538 watcher.watch(TEXT_SHADER_F_PATH).expect("watch fragment shader"); 
818  539 watcher.watch(TEXT_SHADER_V_PATH).expect("watch vertex shader"); 
819  540  
820  541 loop { 
821  542 let event = rx.recv().expect("watcher event"); 
822  543 let ::notify::Event { path, op } = event; 571,1339%[?12l[?25h[?25l 488 gl::BindBuffer(gl::ARRAY_BUFFER, vbo_instance); 
823  489 gl::BufferData(gl::ARRAY_BUFFER, 
824  490 (BATCH_MAX * size_of::<InstanceData>()) as isize, 
825  491 ptr::null(), gl::STREAM_DRAW); 
826  492 // coords 
827  493 gl::VertexAttribPointer(1, 2, 
828  494 gl::FLOAT, gl::FALSE, 
829  495 size_of::<InstanceData>() as i32, 
830  496 ptr::null()); 
831  497 gl::EnableVertexAttribArray(1); 
832  498 gl::VertexAttribDivisor(1, 1); 
833  499 // glyphoffset 
834  500 gl::VertexAttribPointer(2, 4, 
835  501 gl::FLOAT, gl::FALSE, 
836  502 size_of::<InstanceData>() as i32, 
837  503 (2 * size_of::<f32>()) as *const _); 
838  504 gl::EnableVertexAttribArray(2); 
839  505 gl::VertexAttribDivisor(2, 1); 
840  506 // uv 
841  507 gl::VertexAttribPointer(3, 4, 
842  508 gl::FLOAT, gl::FALSE, 
843  509 size_of::<InstanceData>() as i32, 
844  510 (6 * size_of::<f32>()) as *const _); 
845  511 gl::EnableVertexAttribArray(3); 
846  512 gl::VertexAttribDivisor(3, 1); 
847  513 // color 
848  514 gl::VertexAttribPointer(4, 3, 
849  515 gl::FLOAT, gl::FALSE, 543,2137%[?12l[?25h[?25l 460 gl::BindBuffer(gl::ARRAY_BUFFER, vbo); 
850  461  
851  462 gl::VertexAttribPointer(0, 2, 
852  463 gl::FLOAT, gl::FALSE, 
853  464 size_of::<PackedVertex>() as i32, 
854  465 ptr::null()); 
855  466 gl::EnableVertexAttribArray(0); 
856  467  
857  468 gl::BufferData(gl::ARRAY_BUFFER, 
858  469 (size_of::<PackedVertex>() * vertices.len()) as GLsizeiptr, 
859  470 vertices.as_ptr() as *const _, 
860  471 gl::STATIC_DRAW); 
861  472  
862  473 // --------------------- 
863  474 // Set up element buffer 
864  475 // --------------------- 
865  476 let indices: [u32; 6] = [0, 1, 3, 
866  477 1, 2, 3]; 
867  478  
868  479 gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ebo); 
869  480 gl::BufferData(gl::ELEMENT_ARRAY_BUFFER, 
870  481 (6 * size_of::<u32>()) as isize, 
871  482 indices.as_ptr() as *const _, 
872  483 gl::STATIC_DRAW); 
873  484  
874  485 // ---------------------------- 
875  486 // Setup vertex instance buffer 
876  487 // ---------------------------- 515,3735%[?12l[?25h[?25l 432 let mut vao: GLuint = 0; 
877  433 let mut vbo: GLuint = 0; 
878  434 let mut ebo: GLuint = 0; 
879  435  
880  436 let mut vbo_instance: GLuint = 0; 
881  437  
882  438 unsafe { 
883  439 gl::Enable(gl::BLEND); 
884  440 gl::BlendFunc(gl::SRC1_COLOR, gl::ONE_MINUS_SRC1_COLOR); 
885  441 gl::Enable(gl::MULTISAMPLE); 
886  442  
887  443 gl::GenVertexArrays(1, &mut vao); 
888  444 gl::GenBuffers(1, &mut vbo); 
889  445 gl::GenBuffers(1, &mut ebo); 
890  446 gl::GenBuffers(1, &mut vbo_instance); 
891  447 gl::BindVertexArray(vao); 
892  448  
893  449 // ---------------------------- 
894  450 // setup vertex position buffer 
895  451 // ---------------------------- 
896  452 // Top right, Bottom right, Bottom left, Top left 
897  453 let vertices = [ 
898  454 PackedVertex { x: 1.0, y: 1.0 }, 
899  455 PackedVertex { x: 1.0, y: 0.0 }, 
900  456 PackedVertex { x: 0.0, y: 0.0 }, 
901  457 PackedVertex { x: 0.0, y: 1.0 }, 
902  458 ]; 
903  459  487,1333%[?12l[?25h[?25l 404 BATCH_MAX 
904  405  } 
905  406  
906  407  #[inline] 
907  408  pub fn is_empty(&self) -> bool { 
908  409 self.len() == 0 
909  410  } 
910  411  
911  412  #[inline] 
912  413  pub fn size(&self) -> usize { 
913  414 self.len() * size_of::<InstanceData>() 
914  415  } 
915  416  
916  417  pub fn clear(&mut self) { 
917  418 self.tex = 0; 
918  419 self.instances.clear(); 
919  420  } 
920  421 } 
921  422  
922  423 /// Maximum items to be drawn in a batch. 
923  424 const BATCH_MAX: usize = 65_536; 
924  425 const ATLAS_SIZE: i32 = 1024; 
925  426  
926  427 impl QuadRenderer { 
927  428  // TODO should probably hand this a transform instead of width/height 
928  429  pub fn new(config: &Config, size: Size<Pixels<u32>>) -> Result<QuadRenderer, Error> { 
929  430 let program = ShaderProgram::new(config, size)?; 
930  431  459,0-131%[?12l[?25h[?25l 376  
931  377 uv_bot: glyph.uv_bot, 
932  378 uv_left: glyph.uv_left, 
933  379 uv_width: glyph.uv_width, 
934  380 uv_height: glyph.uv_height, 
935  381  
936  382 r: cell.fg.r as f32, 
937  383 g: cell.fg.g as f32, 
938  384 b: cell.fg.b as f32, 
939  385  
940  386 bg_r: cell.bg.r as f32, 
941  387 bg_g: cell.bg.g as f32, 
942  388 bg_b: cell.bg.b as f32, 
943  389 }); 
944  390  } 
945  391  
946  392  #[inline] 
947  393  pub fn full(&self) -> bool { 
948  394 self.capacity() == self.len() 
949  395  } 
950  396  
951  397  #[inline] 
952  398  pub fn len(&self) -> usize { 
953  399 self.instances.len() 
954  400  } 
955  401  
956  402  #[inline] 
957  403  pub fn capacity(&self) -> usize { 431,0-128%[?12l[?25h[?25l 348 } 
958  349  
959  350 impl Batch { 
960  351  #[inline] 
961  352  pub fn new() -> Batch { 
962  353 Batch { 
963  354 tex: 0, 
964  355 instances: Vec::with_capacity(BATCH_MAX), 
965  356 } 
966  357  } 
967  358  
968  359  pub fn add_item( 
969  360 &mut self, 
970  361 cell: &RenderableCell, 
971  362 glyph: &Glyph, 
972  363  ) { 
973  364 if self.is_empty() { 
974  365 self.tex = glyph.tex_id; 
975  366 } 
976  367  
977  368 self.instances.push(InstanceData { 
978  369 col: cell.column.0 as f32, 
979  370 row: cell.line.0 as f32, 
980  371  
981  372 top: glyph.top, 
982  373 left: glyph.left, 
983  374 width: glyph.width, 
984  375 height: glyph.height, 403,526%[?12l[?25h[?25l 320 } 
985  321  
986  322 #[derive(Debug)] 
987  323 pub struct RenderApi<'a> { 
988  324  active_tex: &'a mut GLuint, 
989  325  batch: &'a mut Batch, 
990  326  atlas: &'a mut Vec<Atlas>, 
991  327  program: &'a mut ShaderProgram, 
992  328  config: &'a Config, 
993  329  visual_bell_intensity: f32 
994  330 } 
995  331  
996  332 #[derive(Debug)] 
997  333 pub struct LoaderApi<'a> { 
998  334  active_tex: &'a mut GLuint, 
999  335  atlas: &'a mut Vec<Atlas>, 
1000  336 } 
1001  337  
1002  338 #[derive(Debug)] 
1003  339 pub struct PackedVertex { 
1004  340  x: f32, 
1005  341  y: f32, 
1006  342 } 
1007  343  
1008  344 #[derive(Debug)] 
1009  345 pub struct Batch { 
1010  346  tex: GLuint, 
1011  347  instances: Vec<InstanceData>, 375,1324%[?12l[?25h[?25l 292  height: f32, 
1012  293  // uv offset 
1013  294  uv_left: f32, 
1014  295  uv_bot: f32, 
1015  296  // uv scale 
1016  297  uv_width: f32, 
1017  298  uv_height: f32, 
1018  299  // color 
1019  300  r: f32, 
1020  301  g: f32, 
1021  302  b: f32, 
1022  303  // background color 
1023  304  bg_r: f32, 
1024  305  bg_g: f32, 
1025  306  bg_b: f32, 
1026  307 } 
1027  308  
1028  309 #[derive(Debug)] 
1029  310 pub struct QuadRenderer { 
1030  311  program: ShaderProgram, 
1031  312  vao: GLuint, 
1032  313  vbo: GLuint, 
1033  314  ebo: GLuint, 
1034  315  vbo_instance: GLuint, 
1035  316  atlas: Vec<Atlas>, 
1036  317  active_tex: GLuint, 
1037  318  batch: Batch, 
1038  319  rx: mpsc::Receiver<Msg>, 347,522%[?12l[?25h[?25l 264 let rasterizer = &mut self.rasterizer; 
1039  265 let metrics = &self.metrics; 
1040  266 self.cache 
1041  267 .entry(*glyph_key) 
1042  268 .or_insert_with(|| { 
1043  269 let mut rasterized = rasterizer.get_glyph(&glyph_key) 
1044  270 .unwrap_or_else(|_| Default::default()); 
1045  271  
1046  272 rasterized.left += glyph_offset.x as i32; 
1047  273 rasterized.top += glyph_offset.y as i32; 
1048  274 rasterized.top -= metrics.descent as i32; 
1049  275  
1050  276 loader.load_glyph(&rasterized) 
1051  277 }) 
1052  278  } 
1053  279 } 
1054  280  
1055  281 #[derive(Debug)] 
1056  282 #[repr(C)] 
1057  283 struct InstanceData { 
1058  284  // coords 
1059  285  col: f32, 
1060  286  row: f32, 
1061  287  // glyph offset 
1062  288  left: f32, 
1063  289  top: f32, 
1064  290  // glyph scale 
1065  291  width: f32, 319,520%[?12l[?25h[?25l 236 ($font:expr) => { 
1066  237 for i in RangeInclusive::new(32u8, 128u8) { 
1067  238 cache.get(&GlyphKey { 
1068  239 font_key: $font, 
1069  240 c: i as char, 
1070  241 size: font.size() 
1071  242 }, loader); 
1072  243 } 
1073  244 } 
1074  245 } 
1075  246  
1076  247 load_glyphs_for_font!(regular); 
1077  248 load_glyphs_for_font!(bold); 
1078  249 load_glyphs_for_font!(italic); 
1079  250  
1080  251 Ok(cache) 
1081  252  } 
1082  253  
1083  254  pub fn font_metrics(&self) -> font::Metrics { 
1084  255 self.rasterizer 
1085  256 .metrics(self.font_key) 
1086  257 .expect("metrics load since font is loaded at glyph cache creation") 
1087  258  } 
1088  259  
1089  260  pub fn get<'a, L>(&'a mut self, glyph_key: &GlyphKey, loader: &mut L) -> &'a Glyph 
1090  261 where L: LoadGlyph 
1091  262  { 
1092  263 let glyph_offset = self.glyph_offset; 291,518%[?12l[?25h[?25l 208 // Load bold font 
1093  209 let bold_desc = make_desc(&font.bold, font::Slant::Normal, font::Weight::Bold); 
1094  210  
1095  211 let bold = load_or_regular(bold_desc, &mut rasterizer); 
1096  212  
1097  213 // Load italic font 
1098  214 let italic_desc = make_desc(&font.italic, font::Slant::Italic, font::Weight::Normal); 
1099  215  
1100  216 let italic = load_or_regular(italic_desc, &mut rasterizer); 
1101  217  
1102  218 // Need to load at least one glyph for the face before calling metrics. 
1103  219 // The glyph requested here ('m' at the time of writing) has no special 
1104  220 // meaning. 
1105  221 rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm', size: font.size() })?; 
1106  222 let metrics = rasterizer.metrics(regular)?; 
1107  223  
1108  224 let mut cache = GlyphCache { 
1109  225 cache: HashMap::default(), 
1110  226 rasterizer: rasterizer, 
1111  227 font_size: font.size(), 
1112  228 font_key: regular, 
1113  229 bold_key: bold, 
1114  230 italic_key: italic, 
1115  231 glyph_offset: glyph_offset, 
1116  232 metrics: metrics 
1117  233 }; 
1118  234  
1119  235 macro_rules! load_glyphs_for_font { 263,915%[?12l[?25h[?25l 180 desc: &config::FontDescription, 
1120  181 slant: font::Slant, 
1121  182 weight: font::Weight, 
1122  183 ) -> FontDesc 
1123  184 { 
1124  185 let style = if let Some(ref spec) = desc.style { 
1125  186 font::Style::Specific(spec.to_owned()) 
1126  187 } else { 
1127  188 font::Style::Description {slant:slant, weight:weight} 
1128  189 }; 
1129  190 FontDesc::new(&desc.family[..], style) 
1130  191 } 
1131  192  
1132  193 // Load regular font 
1133  194 let regular_desc = make_desc(&font.normal, font::Slant::Normal, font::Weight::Normal);  
1134  195  
1135  196 let regular = rasterizer 
1136  197 .load_font(&regular_desc, size)?; 
1137  198  
1138  199 // helper to load a description if it is not the regular_desc 
1139  200 let load_or_regular = |desc:FontDesc, rasterizer: &mut Rasterizer| { 
1140  201 if desc == regular_desc { 
1141  202 regular 
1142  203 } else { 
1143  204 rasterizer.load_font(&desc, size).unwrap_or_else(|_| regular) 
1144  205 } 
1145  206 }; 
1146  207  235,913%[?12l[?25h[?25l 152  /// italic font 
1147  153  italic_key: FontKey, 
1148  154  
1149  155  /// bold font 
1150  156  bold_key: FontKey, 
1151  157  
1152  158  /// font size 
1153  159  font_size: font::Size, 
1154  160  
1155  161  /// glyph offset 
1156  162  glyph_offset: Delta, 
1157  163  
1158  164  metrics: ::font::Metrics, 
1159  165 } 
1160  166  
1161  167 impl GlyphCache { 
1162  168  pub fn new<L>( 
1163  169 mut rasterizer: Rasterizer, 
1164  170 config: &Config, 
1165  171 loader: &mut L 
1166  172  ) -> Result<GlyphCache, font::Error> 
1167  173 where L: LoadGlyph 
1168  174  { 
1169  175 let font = config.font(); 
1170  176 let size = font.size(); 
1171  177 let glyph_offset = *font.glyph_offset(); 
1172  178  
1173  179 fn make_desc( 207,0-111%[?12l[?25h[?25l 124  
1174  125 #[derive(Debug, Clone)] 
1175  126 pub struct Glyph { 
1176  127  tex_id: GLuint, 
1177  128  top: f32, 
1178  129  left: f32, 
1179  130  width: f32, 
1180  131  height: f32, 
1181  132  uv_bot: f32, 
1182  133  uv_left: f32, 
1183  134  uv_width: f32, 
1184  135  uv_height: f32, 
1185  136 } 
1186  137  
1187  138 /// Naïve glyph cache 
1188  139 /// 
1189  140 /// Currently only keyed by `char`, and thus not possible to hold different 
1190  141 /// representations of the same code point. 
1191  142 pub struct GlyphCache { 
1192  143  /// Cache of buffered glyphs 
1193  144  cache: HashMap<GlyphKey, Glyph, BuildHasherDefault<FnvHasher>>, 
1194  145  
1195  146  /// Rasterizer for loading new glyphs 
1196  147  rasterizer: Rasterizer, 
1197  148  
1198  149  /// regular font 
1199  150  font_key: FontKey, 
1200  151  179,99%[?12l[?25h[?25l 96 /// 
1201  97 /// Uniforms are prefixed with "u", and vertex attributes are prefixed with "a". 
1202  98 #[derive(Debug)] 
1203  99 pub struct ShaderProgram { 
1204  100  // Program id 
1205  101  id: GLuint, 
1206  102  
1207  103  /// projection matrix uniform 
1208  104  u_projection: GLint, 
1209  105  
1210  106  /// Terminal dimensions (pixels) 
1211  107  u_term_dim: GLint, 
1212  108  
1213  109  /// Cell dimensions (pixels) 
1214  110  u_cell_dim: GLint, 
1215  111  
1216  112  /// Visual bell 
1217  113  u_visual_bell: GLint, 
1218  114  
1219  115  /// Background pass flag 
1220  116  /// 
1221  117  /// Rendering is split into two passes; 1 for backgrounds, and one for text 
1222  118  u_background: GLint, 
1223  119  
1224  120  padding_x: f32, 
1225  121  padding_y: f32, 
1226  122 } 
1227  123  151,0-17%[?12l[?25h[?25l:[?12l[?25hq[?25l[?12l[?25h[?25l[?12l[?25h [?25l151,0-17%[?12l[?25h