painter.rs
1 #![allow(unsafe_code)] 2 3 use std::{collections::HashMap, rc::Rc}; 4 5 use egui::{ 6 emath::Rect, 7 epaint::{Color32, Mesh, Primitive, Vertex}, 8 }; 9 use glow::HasContext as _; 10 use memoffset::offset_of; 11 12 use crate::check_for_gl_error; 13 use crate::misc_util::{compile_shader, link_program}; 14 use crate::post_process::PostProcess; 15 use crate::shader_version::ShaderVersion; 16 use crate::vao; 17 18 pub use glow::Context; 19 20 const VERT_SRC: &str = include_str!("shader/vertex.glsl"); 21 const FRAG_SRC: &str = include_str!("shader/fragment.glsl"); 22 23 /// An OpenGL painter using [`glow`]. 24 /// 25 /// This is responsible for painting egui and managing egui textures. 26 /// You can access the underlying [`glow::Context`] with [`Self::gl`]. 27 /// 28 /// This struct must be destroyed with [`Painter::destroy`] before dropping, to ensure OpenGL 29 /// objects have been properly deleted and are not leaked. 30 pub struct Painter { 31 gl: Rc<glow::Context>, 32 33 max_texture_side: usize, 34 35 program: glow::Program, 36 u_screen_size: glow::UniformLocation, 37 u_sampler: glow::UniformLocation, 38 is_webgl_1: bool, 39 is_embedded: bool, 40 vao: crate::vao::VertexArrayObject, 41 srgb_support: bool, 42 /// The filter used for subsequent textures. 43 texture_filter: TextureFilter, 44 post_process: Option<PostProcess>, 45 vbo: glow::Buffer, 46 element_array_buffer: glow::Buffer, 47 48 textures: HashMap<egui::TextureId, glow::Texture>, 49 50 #[cfg(feature = "epi")] 51 next_native_tex_id: u64, // TODO: 128-bit texture space? 52 53 /// Stores outdated OpenGL textures that are yet to be deleted 54 textures_to_destroy: Vec<glow::Texture>, 55 56 /// Used to make sure we are destroyed correctly. 57 destroyed: bool, 58 } 59 60 #[derive(Copy, Clone)] 61 pub enum TextureFilter { 62 Linear, 63 Nearest, 64 } 65 66 impl Default for TextureFilter { 67 fn default() -> Self { 68 TextureFilter::Linear 69 } 70 } 71 72 impl TextureFilter { 73 pub(crate) fn glow_code(&self) -> u32 { 74 match self { 75 TextureFilter::Linear => glow::LINEAR, 76 TextureFilter::Nearest => glow::NEAREST, 77 } 78 } 79 } 80 81 impl Painter { 82 /// Create painter. 83 /// 84 /// Set `pp_fb_extent` to the framebuffer size to enable `sRGB` support on OpenGL ES and WebGL. 85 /// 86 /// Set `shader_prefix` if you want to turn on shader workaround e.g. `"#define APPLY_BRIGHTENING_GAMMA\n"` 87 /// (see <https://github.com/emilk/egui/issues/794>). 88 /// 89 /// # Errors 90 /// will return `Err` below cases 91 /// * failed to compile shader 92 /// * failed to create postprocess on webgl with `sRGB` support 93 /// * failed to create buffer 94 pub fn new( 95 gl: Rc<glow::Context>, 96 pp_fb_extent: Option<[i32; 2]>, 97 shader_prefix: &str, 98 ) -> Result<Painter, String> { 99 check_for_gl_error!(&gl, "before Painter::new"); 100 101 let max_texture_side = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as usize; 102 103 let shader_version = ShaderVersion::get(&gl); 104 let is_webgl_1 = shader_version == ShaderVersion::Es100; 105 let header = shader_version.version(); 106 tracing::debug!("Shader header: {:?}.", header); 107 let srgb_support = gl.supported_extensions().contains("EXT_sRGB"); 108 109 let (post_process, srgb_support_define) = match (shader_version, srgb_support) { 110 // WebGL2 support sRGB default 111 (ShaderVersion::Es300, _) | (ShaderVersion::Es100, true) => unsafe { 112 // Add sRGB support marker for fragment shader 113 if let Some([width, height]) = pp_fb_extent { 114 tracing::debug!("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending."); 115 // install post process to correct sRGB color: 116 ( 117 Some(PostProcess::new( 118 gl.clone(), 119 shader_prefix, 120 is_webgl_1, 121 width, 122 height, 123 )?), 124 "#define SRGB_SUPPORTED", 125 ) 126 } else { 127 tracing::debug!("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None"); 128 (None, "") 129 } 130 }, 131 132 // WebGL1 without sRGB support disable postprocess and use fallback shader 133 (ShaderVersion::Es100, false) => (None, ""), 134 135 // OpenGL 2.1 or above always support sRGB so add sRGB support marker 136 _ => (None, "#define SRGB_SUPPORTED"), 137 }; 138 139 unsafe { 140 let vert = compile_shader( 141 &gl, 142 glow::VERTEX_SHADER, 143 &format!( 144 "{}\n{}\n{}\n{}", 145 header, 146 shader_prefix, 147 shader_version.is_new_shader_interface(), 148 VERT_SRC 149 ), 150 )?; 151 let frag = compile_shader( 152 &gl, 153 glow::FRAGMENT_SHADER, 154 &format!( 155 "{}\n{}\n{}\n{}\n{}", 156 header, 157 shader_prefix, 158 srgb_support_define, 159 shader_version.is_new_shader_interface(), 160 FRAG_SRC 161 ), 162 )?; 163 let program = link_program(&gl, [vert, frag].iter())?; 164 gl.detach_shader(program, vert); 165 gl.detach_shader(program, frag); 166 gl.delete_shader(vert); 167 gl.delete_shader(frag); 168 let u_screen_size = gl.get_uniform_location(program, "u_screen_size").unwrap(); 169 let u_sampler = gl.get_uniform_location(program, "u_sampler").unwrap(); 170 171 let vbo = gl.create_buffer()?; 172 173 let a_pos_loc = gl.get_attrib_location(program, "a_pos").unwrap(); 174 let a_tc_loc = gl.get_attrib_location(program, "a_tc").unwrap(); 175 let a_srgba_loc = gl.get_attrib_location(program, "a_srgba").unwrap(); 176 177 let stride = std::mem::size_of::<Vertex>() as i32; 178 let buffer_infos = vec![ 179 vao::BufferInfo { 180 location: a_pos_loc, 181 vector_size: 2, 182 data_type: glow::FLOAT, 183 normalized: false, 184 stride, 185 offset: offset_of!(Vertex, pos) as i32, 186 }, 187 vao::BufferInfo { 188 location: a_tc_loc, 189 vector_size: 2, 190 data_type: glow::FLOAT, 191 normalized: false, 192 stride, 193 offset: offset_of!(Vertex, uv) as i32, 194 }, 195 vao::BufferInfo { 196 location: a_srgba_loc, 197 vector_size: 4, 198 data_type: glow::UNSIGNED_BYTE, 199 normalized: false, 200 stride, 201 offset: offset_of!(Vertex, color) as i32, 202 }, 203 ]; 204 let vao = crate::vao::VertexArrayObject::new(&gl, vbo, buffer_infos); 205 206 let element_array_buffer = gl.create_buffer()?; 207 208 check_for_gl_error!(&gl, "after Painter::new"); 209 210 Ok(Painter { 211 gl, 212 max_texture_side, 213 program, 214 u_screen_size, 215 u_sampler, 216 is_webgl_1, 217 is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300), 218 vao, 219 srgb_support, 220 texture_filter: Default::default(), 221 post_process, 222 vbo, 223 element_array_buffer, 224 textures: Default::default(), 225 #[cfg(feature = "epi")] 226 next_native_tex_id: 1 << 32, 227 textures_to_destroy: Vec::new(), 228 destroyed: false, 229 }) 230 } 231 } 232 233 /// Access the shared glow context. 234 pub fn gl(&self) -> &std::rc::Rc<glow::Context> { 235 &self.gl 236 } 237 238 pub fn max_texture_side(&self) -> usize { 239 self.max_texture_side 240 } 241 242 unsafe fn prepare_painting( 243 &mut self, 244 [width_in_pixels, height_in_pixels]: [u32; 2], 245 pixels_per_point: f32, 246 ) -> (u32, u32) { 247 self.gl.enable(glow::SCISSOR_TEST); 248 // egui outputs mesh in both winding orders 249 self.gl.disable(glow::CULL_FACE); 250 self.gl.disable(glow::DEPTH_TEST); 251 252 self.gl.color_mask(true, true, true, true); 253 254 self.gl.enable(glow::BLEND); 255 self.gl 256 .blend_equation_separate(glow::FUNC_ADD, glow::FUNC_ADD); 257 self.gl.blend_func_separate( 258 // egui outputs colors with premultiplied alpha: 259 glow::ONE, 260 glow::ONE_MINUS_SRC_ALPHA, 261 // Less important, but this is technically the correct alpha blend function 262 // when you want to make use of the framebuffer alpha (for screenshots, compositing, etc). 263 glow::ONE_MINUS_DST_ALPHA, 264 glow::ONE, 265 ); 266 267 if !cfg!(target_arch = "wasm32") { 268 self.gl.enable(glow::FRAMEBUFFER_SRGB); 269 check_for_gl_error!(&self.gl, "FRAMEBUFFER_SRGB"); 270 } 271 272 let width_in_points = width_in_pixels as f32 / pixels_per_point; 273 let height_in_points = height_in_pixels as f32 / pixels_per_point; 274 275 self.gl 276 .viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32); 277 self.gl.use_program(Some(self.program)); 278 279 self.gl 280 .uniform_2_f32(Some(&self.u_screen_size), width_in_points, height_in_points); 281 self.gl.uniform_1_i32(Some(&self.u_sampler), 0); 282 self.gl.active_texture(glow::TEXTURE0); 283 284 self.vao.bind(&self.gl); 285 self.gl 286 .bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer)); 287 288 check_for_gl_error!(&self.gl, "prepare_painting"); 289 290 (width_in_pixels, height_in_pixels) 291 } 292 293 pub fn paint_and_update_textures( 294 &mut self, 295 inner_size: [u32; 2], 296 pixels_per_point: f32, 297 clipped_primitives: &[egui::ClippedPrimitive], 298 textures_delta: &egui::TexturesDelta, 299 ) { 300 for (id, image_delta) in &textures_delta.set { 301 self.set_texture(*id, image_delta); 302 } 303 304 self.paint_primitives(inner_size, pixels_per_point, clipped_primitives); 305 306 for &id in &textures_delta.free { 307 self.free_texture(id); 308 } 309 } 310 311 /// Main entry-point for painting a frame. 312 /// You should call `target.clear_color(..)` before 313 /// and `target.finish()` after this. 314 /// 315 /// The following OpenGL features will be set: 316 /// - Scissor test will be enabled 317 /// - Cull face will be disabled 318 /// - Blend will be enabled 319 /// 320 /// The scissor area and blend parameters will be changed. 321 /// 322 /// As well as this, the following objects will be unset: 323 /// - Vertex Buffer 324 /// - Element Buffer 325 /// - Texture (and active texture will be set to 0) 326 /// - Program 327 /// 328 /// Please be mindful of these effects when integrating into your program, and also be mindful 329 /// of the effects your program might have on this code. Look at the source if in doubt. 330 pub fn paint_primitives( 331 &mut self, 332 inner_size: [u32; 2], 333 pixels_per_point: f32, 334 clipped_primitives: &[egui::ClippedPrimitive], 335 ) { 336 self.assert_not_destroyed(); 337 338 if let Some(ref mut post_process) = self.post_process { 339 unsafe { 340 post_process.begin(inner_size[0] as i32, inner_size[1] as i32); 341 } 342 } 343 let size_in_pixels = unsafe { self.prepare_painting(inner_size, pixels_per_point) }; 344 345 for egui::ClippedPrimitive { 346 clip_rect, 347 primitive, 348 } in clipped_primitives 349 { 350 set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, *clip_rect); 351 352 match primitive { 353 Primitive::Mesh(mesh) => { 354 self.paint_mesh(mesh); 355 } 356 Primitive::Callback(callback) => { 357 if callback.rect.is_positive() { 358 // Transform callback rect to physical pixels: 359 let rect_min_x = pixels_per_point * callback.rect.min.x; 360 let rect_min_y = pixels_per_point * callback.rect.min.y; 361 let rect_max_x = pixels_per_point * callback.rect.max.x; 362 let rect_max_y = pixels_per_point * callback.rect.max.y; 363 364 let rect_min_x = rect_min_x.round() as i32; 365 let rect_min_y = rect_min_y.round() as i32; 366 let rect_max_x = rect_max_x.round() as i32; 367 let rect_max_y = rect_max_y.round() as i32; 368 369 unsafe { 370 self.gl.viewport( 371 rect_min_x, 372 size_in_pixels.1 as i32 - rect_max_y, 373 rect_max_x - rect_min_x, 374 rect_max_y - rect_min_y, 375 ); 376 } 377 378 let info = egui::PaintCallbackInfo { 379 rect: callback.rect, 380 pixels_per_point, 381 screen_size_px: inner_size, 382 }; 383 384 callback.call(&info, self); 385 386 check_for_gl_error!(&self.gl, "callback"); 387 388 // Restore state: 389 unsafe { 390 if let Some(ref mut post_process) = self.post_process { 391 post_process.bind(); 392 } 393 self.prepare_painting(inner_size, pixels_per_point) 394 }; 395 } 396 } 397 } 398 } 399 400 unsafe { 401 self.vao.unbind(&self.gl); 402 self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); 403 404 if let Some(ref post_process) = self.post_process { 405 post_process.end(); 406 } 407 408 self.gl.disable(glow::SCISSOR_TEST); 409 410 check_for_gl_error!(&self.gl, "painting"); 411 } 412 } 413 414 #[inline(never)] // Easier profiling 415 fn paint_mesh(&mut self, mesh: &Mesh) { 416 debug_assert!(mesh.is_valid()); 417 if let Some(texture) = self.get_texture(mesh.texture_id) { 418 unsafe { 419 self.gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vbo)); 420 self.gl.buffer_data_u8_slice( 421 glow::ARRAY_BUFFER, 422 bytemuck::cast_slice(&mesh.vertices), 423 glow::STREAM_DRAW, 424 ); 425 426 self.gl 427 .bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer)); 428 self.gl.buffer_data_u8_slice( 429 glow::ELEMENT_ARRAY_BUFFER, 430 bytemuck::cast_slice(&mesh.indices), 431 glow::STREAM_DRAW, 432 ); 433 434 self.gl.bind_texture(glow::TEXTURE_2D, Some(texture)); 435 } 436 437 unsafe { 438 self.gl.draw_elements( 439 glow::TRIANGLES, 440 mesh.indices.len() as i32, 441 glow::UNSIGNED_INT, 442 0, 443 ); 444 } 445 446 check_for_gl_error!(&self.gl, "paint_mesh"); 447 } 448 } 449 450 // Set the filter to be used for any subsequent textures loaded via 451 // [`Self::set_texture`]. 452 pub fn set_texture_filter(&mut self, texture_filter: TextureFilter) { 453 self.texture_filter = texture_filter; 454 } 455 456 // ------------------------------------------------------------------------ 457 458 pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) { 459 self.assert_not_destroyed(); 460 461 let glow_texture = *self 462 .textures 463 .entry(tex_id) 464 .or_insert_with(|| unsafe { self.gl.create_texture().unwrap() }); 465 unsafe { 466 self.gl.bind_texture(glow::TEXTURE_2D, Some(glow_texture)); 467 } 468 469 match &delta.image { 470 egui::ImageData::Color(image) => { 471 assert_eq!( 472 image.width() * image.height(), 473 image.pixels.len(), 474 "Mismatch between texture size and texel count" 475 ); 476 477 let data: &[u8] = bytemuck::cast_slice(image.pixels.as_ref()); 478 479 self.upload_texture_srgb(delta.pos, image.size, data); 480 } 481 egui::ImageData::Font(image) => { 482 assert_eq!( 483 image.width() * image.height(), 484 image.pixels.len(), 485 "Mismatch between texture size and texel count" 486 ); 487 488 let gamma = if self.is_embedded && self.post_process.is_none() { 489 1.0 / 2.2 490 } else { 491 1.0 492 }; 493 let data: Vec<u8> = image 494 .srgba_pixels(gamma) 495 .flat_map(|a| a.to_array()) 496 .collect(); 497 498 self.upload_texture_srgb(delta.pos, image.size, &data); 499 } 500 }; 501 } 502 503 fn upload_texture_srgb(&mut self, pos: Option<[usize; 2]>, [w, h]: [usize; 2], data: &[u8]) { 504 assert_eq!(data.len(), w * h * 4); 505 assert!( 506 w >= 1 && h >= 1, 507 "Got a texture image of size {}x{}. A texture must at least be one texel wide.", 508 w, 509 h 510 ); 511 assert!( 512 w <= self.max_texture_side && h <= self.max_texture_side, 513 "Got a texture image of size {}x{}, but the maximum supported texture side is only {}", 514 w, 515 h, 516 self.max_texture_side 517 ); 518 519 unsafe { 520 self.gl.tex_parameter_i32( 521 glow::TEXTURE_2D, 522 glow::TEXTURE_MAG_FILTER, 523 self.texture_filter.glow_code() as i32, 524 ); 525 self.gl.tex_parameter_i32( 526 glow::TEXTURE_2D, 527 glow::TEXTURE_MIN_FILTER, 528 self.texture_filter.glow_code() as i32, 529 ); 530 531 self.gl.tex_parameter_i32( 532 glow::TEXTURE_2D, 533 glow::TEXTURE_WRAP_S, 534 glow::CLAMP_TO_EDGE as i32, 535 ); 536 self.gl.tex_parameter_i32( 537 glow::TEXTURE_2D, 538 glow::TEXTURE_WRAP_T, 539 glow::CLAMP_TO_EDGE as i32, 540 ); 541 check_for_gl_error!(&self.gl, "tex_parameter"); 542 543 let (internal_format, src_format) = if self.is_webgl_1 { 544 let format = if self.srgb_support { 545 glow::SRGB_ALPHA 546 } else { 547 glow::RGBA 548 }; 549 (format, format) 550 } else { 551 (glow::SRGB8_ALPHA8, glow::RGBA) 552 }; 553 554 self.gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1); 555 556 let level = 0; 557 if let Some([x, y]) = pos { 558 self.gl.tex_sub_image_2d( 559 glow::TEXTURE_2D, 560 level, 561 x as _, 562 y as _, 563 w as _, 564 h as _, 565 src_format, 566 glow::UNSIGNED_BYTE, 567 glow::PixelUnpackData::Slice(data), 568 ); 569 check_for_gl_error!(&self.gl, "tex_sub_image_2d"); 570 } else { 571 let border = 0; 572 self.gl.tex_image_2d( 573 glow::TEXTURE_2D, 574 level, 575 internal_format as _, 576 w as _, 577 h as _, 578 border, 579 src_format, 580 glow::UNSIGNED_BYTE, 581 Some(data), 582 ); 583 check_for_gl_error!(&self.gl, "tex_image_2d"); 584 } 585 } 586 } 587 588 pub fn free_texture(&mut self, tex_id: egui::TextureId) { 589 if let Some(old_tex) = self.textures.remove(&tex_id) { 590 unsafe { self.gl.delete_texture(old_tex) }; 591 } 592 } 593 594 /// Get the [`glow::Texture`] bound to a [`egui::TextureId`]. 595 pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> { 596 self.textures.get(&texture_id).copied() 597 } 598 599 unsafe fn destroy_gl(&self) { 600 self.gl.delete_program(self.program); 601 for tex in self.textures.values() { 602 self.gl.delete_texture(*tex); 603 } 604 self.gl.delete_buffer(self.vbo); 605 self.gl.delete_buffer(self.element_array_buffer); 606 for t in &self.textures_to_destroy { 607 self.gl.delete_texture(*t); 608 } 609 } 610 611 /// This function must be called before [`Painter`] is dropped, as [`Painter`] has some OpenGL objects 612 /// that should be deleted. 613 pub fn destroy(&mut self) { 614 if !self.destroyed { 615 unsafe { 616 self.destroy_gl(); 617 if let Some(ref post_process) = self.post_process { 618 post_process.destroy(); 619 } 620 } 621 self.destroyed = true; 622 } 623 } 624 625 fn assert_not_destroyed(&self) { 626 assert!(!self.destroyed, "the egui glow has already been destroyed!"); 627 } 628 } 629 630 pub fn clear(gl: &glow::Context, screen_size_in_pixels: [u32; 2], clear_color: egui::Rgba) { 631 unsafe { 632 gl.disable(glow::SCISSOR_TEST); 633 634 gl.viewport( 635 0, 636 0, 637 screen_size_in_pixels[0] as i32, 638 screen_size_in_pixels[1] as i32, 639 ); 640 641 let clear_color: Color32 = clear_color.into(); 642 gl.clear_color( 643 clear_color[0] as f32 / 255.0, 644 clear_color[1] as f32 / 255.0, 645 clear_color[2] as f32 / 255.0, 646 clear_color[3] as f32 / 255.0, 647 ); 648 gl.clear(glow::COLOR_BUFFER_BIT); 649 } 650 } 651 652 impl Drop for Painter { 653 fn drop(&mut self) { 654 if !self.destroyed { 655 tracing::warn!( 656 "You forgot to call destroy() on the egui glow painter. Resources will leak!" 657 ); 658 } 659 } 660 } 661 662 #[cfg(feature = "epi")] 663 impl epi::NativeTexture for Painter { 664 type Texture = glow::Texture; 665 666 fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId { 667 self.assert_not_destroyed(); 668 let id = egui::TextureId::User(self.next_native_tex_id); 669 self.next_native_tex_id += 1; 670 self.textures.insert(id, native); 671 id 672 } 673 674 fn replace_native_texture(&mut self, id: egui::TextureId, replacing: Self::Texture) { 675 if let Some(old_tex) = self.textures.insert(id, replacing) { 676 self.textures_to_destroy.push(old_tex); 677 } 678 } 679 } 680 681 fn set_clip_rect( 682 gl: &glow::Context, 683 size_in_pixels: (u32, u32), 684 pixels_per_point: f32, 685 clip_rect: Rect, 686 ) { 687 // Transform clip rect to physical pixels: 688 let clip_min_x = pixels_per_point * clip_rect.min.x; 689 let clip_min_y = pixels_per_point * clip_rect.min.y; 690 let clip_max_x = pixels_per_point * clip_rect.max.x; 691 let clip_max_y = pixels_per_point * clip_rect.max.y; 692 693 // Make sure clip rect can fit within a `u32`: 694 let clip_min_x = clip_min_x.clamp(0.0, size_in_pixels.0 as f32); 695 let clip_min_y = clip_min_y.clamp(0.0, size_in_pixels.1 as f32); 696 let clip_max_x = clip_max_x.clamp(clip_min_x, size_in_pixels.0 as f32); 697 let clip_max_y = clip_max_y.clamp(clip_min_y, size_in_pixels.1 as f32); 698 699 let clip_min_x = clip_min_x.round() as i32; 700 let clip_min_y = clip_min_y.round() as i32; 701 let clip_max_x = clip_max_x.round() as i32; 702 let clip_max_y = clip_max_y.round() as i32; 703 704 unsafe { 705 gl.scissor( 706 clip_min_x, 707 size_in_pixels.1 as i32 - clip_max_y, 708 clip_max_x - clip_min_x, 709 clip_max_y - clip_min_y, 710 ); 711 } 712 }