Skip to content

Commit

Permalink
Define a fast ViewportMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 6, 2023
1 parent 0ae7eeb commit 6a371e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
27 changes: 14 additions & 13 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp +
mod glow_integration {

use egui::{
epaint::ahash::HashMap, NumExt as _, ViewportIdPair, ViewportOutput, ViewportUiCallback,
epaint::ahash::HashMap, NumExt as _, ViewportIdPair, ViewportMap, ViewportOutput,
ViewportUiCallback,
};
use egui_winit::{
changes_between_builders, create_winit_window_builder, process_viewport_commands,
Expand Down Expand Up @@ -532,11 +533,11 @@ mod glow_integration {
current_gl_context: Option<glutin::context::PossiblyCurrentContext>,
not_current_gl_context: Option<glutin::context::NotCurrentContext>,

viewports: HashMap<ViewportId, Rc<RefCell<Viewport>>>,
viewports: ViewportMap<Rc<RefCell<Viewport>>>,
viewport_maps: HashMap<winit::window::WindowId, ViewportId>,
window_maps: HashMap<ViewportId, winit::window::WindowId>,
window_maps: ViewportMap<winit::window::WindowId>,

builders: HashMap<ViewportId, ViewportBuilder>,
builders: ViewportMap<ViewportBuilder>,
}

impl GlutinWindowContext {
Expand Down Expand Up @@ -659,13 +660,13 @@ mod glow_integration {
let not_current_gl_context = Some(gl_context);

let mut viewport_maps = HashMap::default();
let mut window_maps = HashMap::default();
let mut window_maps = ViewportMap::default();
if let Some(window) = &window {
viewport_maps.insert(window.id(), ViewportId::MAIN);
window_maps.insert(ViewportId::MAIN, window.id());
}

let mut windows = HashMap::default();
let mut windows = ViewportMap::default();
windows.insert(
ViewportId::MAIN,
Rc::new(RefCell::new(Viewport {
Expand All @@ -677,7 +678,7 @@ mod glow_integration {
})),
);

let mut builders = HashMap::default();
let mut builders = ViewportMap::default();
builders.insert(ViewportId::MAIN, window_builder);

// the fun part with opengl gl is that we never know whether there is an error. the context creation might have failed, but
Expand Down Expand Up @@ -1887,7 +1888,7 @@ pub use glow_integration::run_glow;

#[cfg(feature = "wgpu")]
mod wgpu_integration {
use egui::{ViewportIdPair, ViewportOutput, ViewportUiCallback};
use egui::{ViewportIdPair, ViewportMap, ViewportOutput, ViewportUiCallback};
use egui_winit::create_winit_window_builder;
use parking_lot::Mutex;

Expand All @@ -1906,10 +1907,10 @@ mod wgpu_integration {
}

#[derive(Clone, Default)]
pub struct Viewports(HashMap<ViewportId, Viewport>);
pub struct Viewports(ViewportMap<Viewport>);

impl std::ops::Deref for Viewports {
type Target = HashMap<ViewportId, Viewport>;
type Target = ViewportMap<Viewport>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -1930,7 +1931,7 @@ mod wgpu_integration {
integration: Rc<RefCell<epi_integration::EpiIntegration>>,
app: Box<dyn epi::App>,
viewports: Rc<RefCell<Viewports>>,
builders: Rc<RefCell<HashMap<ViewportId, ViewportBuilder>>>,
builders: Rc<RefCell<ViewportMap<ViewportBuilder>>>,
viewport_maps: Rc<RefCell<HashMap<winit::window::WindowId, ViewportId>>>,
}

Expand Down Expand Up @@ -2177,7 +2178,7 @@ mod wgpu_integration {
},
);

let builders = Rc::new(RefCell::new(HashMap::default()));
let builders = Rc::new(RefCell::new(ViewportMap::default()));
builders.borrow_mut().insert(ViewportId::MAIN, builder);

let painter = Rc::new(RefCell::new(painter));
Expand Down Expand Up @@ -2242,7 +2243,7 @@ mod wgpu_integration {
id_pair: ViewportIdPair,
viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>,
viewports: &RefCell<Viewports>,
builders: &RefCell<HashMap<ViewportId, ViewportBuilder>>,
builders: &RefCell<ViewportMap<ViewportBuilder>>,
beginning: Instant,
painter: &RefCell<egui_wgpu::winit::Painter>,
viewport_maps: &RefCell<HashMap<winit::window::WindowId, ViewportId>>,
Expand Down
15 changes: 7 additions & 8 deletions crates/egui-wgpu/src/winit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{num::NonZeroU32, sync::Arc};

use egui::ViewportId;
use epaint::ahash::HashMap;
use egui::{ViewportId, ViewportMap};

use crate::{renderer, RenderState, SurfaceErrorAction, WgpuConfiguration};

Expand Down Expand Up @@ -80,13 +79,13 @@ pub struct Painter {
msaa_samples: u32,
support_transparent_backbuffer: bool,
depth_format: Option<wgpu::TextureFormat>,
depth_texture_view: HashMap<ViewportId, wgpu::TextureView>,
msaa_texture_view: HashMap<ViewportId, wgpu::TextureView>,
depth_texture_view: ViewportMap<wgpu::TextureView>,
msaa_texture_view: ViewportMap<wgpu::TextureView>,
screen_capture_state: Option<CaptureState>,

instance: wgpu::Instance,
render_state: Option<RenderState>,
surfaces: HashMap<ViewportId, SurfaceState>,
surfaces: ViewportMap<SurfaceState>,
}

unsafe impl Send for Painter {}
Expand Down Expand Up @@ -121,13 +120,13 @@ impl Painter {
msaa_samples,
support_transparent_backbuffer,
depth_format,
depth_texture_view: HashMap::default(),
depth_texture_view: Default::default(),
screen_capture_state: None,

instance,
render_state: None,
surfaces: HashMap::default(),
msaa_texture_view: HashMap::default(),
surfaces: Default::default(),
msaa_texture_view: Default::default(),
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ struct Repaint {
/// The current frame number.
///
/// Incremented at the end of each frame.
viewports_frame_nr: HashMap<ViewportId, u64>,
viewports_frame_nr: ViewportMap<u64>,

/// While positive, keep requesting repaints. Decrement at the start of each frame.
repaint_request: HashMap<ViewportId, u8>,
repaint_request: ViewportMap<u8>,
request_repaint_callback: Option<Box<dyn Fn(RequestRepaintInfo) + Send + Sync>>,

requested_repaint_last_frame: HashMap<ViewportId, bool>,
requested_repaint_last_frame: ViewportMap<bool>,
}

impl Repaint {
Expand Down Expand Up @@ -154,35 +154,35 @@ struct ContextImpl {

os: OperatingSystem,

input: HashMap<ViewportId, InputState>,
input: ViewportMap<InputState>,

/// State that is collected during a frame and then cleared
frame_state: HashMap<ViewportId, FrameState>,
frame_state: ViewportMap<FrameState>,

/// How deeply nested are we?
viewport_stack: Vec<ViewportIdPair>,

// The output of a frame:
graphics: HashMap<ViewportId, GraphicLayers>,
output: HashMap<ViewportId, PlatformOutput>,
graphics: ViewportMap<GraphicLayers>,
output: ViewportMap<PlatformOutput>,

paint_stats: PaintStats,

repaint: Repaint,

viewports: HashMap<ViewportId, Viewport>,
viewports: ViewportMap<Viewport>,
viewport_commands: Vec<(ViewportId, ViewportCommand)>,

is_desktop: bool,
force_embedding: bool,

/// Written to during the frame.
layer_rects_this_frame: ahash::HashMap<LayerId, Vec<(Id, Rect)>>,
layer_rects_this_viewports: HashMap<ViewportId, HashMap<LayerId, Vec<(Id, Rect)>>>,
layer_rects_this_viewports: ViewportMap<HashMap<LayerId, Vec<(Id, Rect)>>>,

/// Read
layer_rects_prev_frame: ahash::HashMap<LayerId, Vec<(Id, Rect)>>,
layer_rects_prev_viewports: HashMap<ViewportId, HashMap<LayerId, Vec<(Id, Rect)>>>,
layer_rects_prev_viewports: ViewportMap<HashMap<LayerId, Vec<(Id, Rect)>>>,

#[cfg(feature = "accesskit")]
is_accesskit_enabled: bool,
Expand Down
11 changes: 6 additions & 5 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![warn(missing_docs)] // Let's keep this file well-documented.` to memory.rs

use epaint::{emath::Rangef, vec2, Vec2};

use crate::{
area, window, EventFilter, Id, IdMap, InputState, LayerId, Pos2, Rect, Style, ViewportId,
ViewportMap,
};
use ahash::HashMap;
use epaint::{emath::Rangef, vec2, Vec2};

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -77,7 +78,7 @@ pub struct Memory {
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interactions: HashMap<ViewportId, Interaction>,
pub(crate) interactions: ViewportMap<Interaction>,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interaction: Interaction,
Expand All @@ -90,7 +91,7 @@ pub struct Memory {
pub(crate) window_interaction: Option<window::WindowInteraction>,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) window_interactions: HashMap<ViewportId, window::WindowInteraction>,
pub(crate) window_interactions: ViewportMap<window::WindowInteraction>,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) drag_value: crate::widgets::drag_value::MonoState,
Expand All @@ -99,7 +100,7 @@ pub struct Memory {
pub(crate) areas: Areas,

#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) viewports_areas: HashMap<ViewportId, Areas>,
pub(crate) viewports_areas: ViewportMap<Areas>,

/// Which popup-window is open (if any)?
/// Could be a combo box, color picker, menu etc.
Expand Down
5 changes: 5 additions & 0 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl From<ViewportId> for Id {
}
}

impl nohash_hasher::IsEnabled for ViewportId {}

/// A fast hash map from [`ViewportId`] to `T`.
pub type ViewportMap<T> = nohash_hasher::IntMap<ViewportId, T>;

// ----------------------------------------------------------------------------

/// This will deref to [`Self::this`].
Expand Down

0 comments on commit 6a371e5

Please sign in to comment.