This commit is contained in:
Junkui Zhang 2025-08-05 17:45:08 +08:00
parent fc5bcf0ad1
commit dbba54d7f8
5 changed files with 57 additions and 10 deletions

8
Cargo.lock generated
View file

@ -12344,18 +12344,18 @@ dependencies = [
[[package]] [[package]]
name = "profiling" name = "profiling"
version = "1.0.16" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afbdc74edc00b6f6a218ca6a5364d6226a259d4b8ea1af4a0ea063f27e179f4d" checksum = "3eb8486b569e12e2c32ad3e204dbaba5e4b5b216e9367044f25f1dba42341773"
dependencies = [ dependencies = [
"profiling-procmacros", "profiling-procmacros",
] ]
[[package]] [[package]]
name = "profiling-procmacros" name = "profiling-procmacros"
version = "1.0.16" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a65f2e60fbf1063868558d69c6beacf412dc755f9fc020f514b7955fc914fe30" checksum = "52717f9a02b6965224f95ca2a81e2e0c5c43baacd28ca057577988930b6c3d5b"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.101", "syn 2.0.101",

View file

@ -537,7 +537,7 @@ portable-pty = "0.9.0"
postage = { version = "0.5", features = ["futures-traits"] } postage = { version = "0.5", features = ["futures-traits"] }
pretty_assertions = { version = "1.3.0", features = ["unstable"] } pretty_assertions = { version = "1.3.0", features = ["unstable"] }
proc-macro2 = "1.0.93" proc-macro2 = "1.0.93"
profiling = "1" profiling = "1.0.17"
prost = "0.9" prost = "0.9"
prost-build = "0.9" prost-build = "0.9"
prost-types = "0.9" prost-types = "0.9"

View file

@ -286,6 +286,7 @@ impl DirectXRenderer {
Ok(()) Ok(())
} }
#[profiling::function]
pub(crate) fn draw(&mut self, scene: &Scene) -> Result<()> { pub(crate) fn draw(&mut self, scene: &Scene) -> Result<()> {
self.pre_draw()?; self.pre_draw()?;
for batch in scene.batches() { for batch in scene.batches() {

View file

@ -238,6 +238,7 @@ fn handle_timer_msg(
} }
} }
#[profiling::function]
fn handle_paint_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> { fn handle_paint_msg(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
draw_window(handle, false, state_ptr) draw_window(handle, false, state_ptr)
} }
@ -373,6 +374,7 @@ fn handle_syskeyup_msg(
// It's a known bug that you can't trigger `ctrl-shift-0`. See: // It's a known bug that you can't trigger `ctrl-shift-0`. See:
// https://superuser.com/questions/1455762/ctrl-shift-number-key-combination-has-stopped-working-for-a-few-numbers // https://superuser.com/questions/1455762/ctrl-shift-number-key-combination-has-stopped-working-for-a-few-numbers
#[profiling::function]
fn handle_keydown_msg( fn handle_keydown_msg(
handle: HWND, handle: HWND,
wparam: WPARAM, wparam: WPARAM,
@ -1302,6 +1304,7 @@ fn translate_message(handle: HWND, wparam: WPARAM, lparam: LPARAM) {
unsafe { TranslateMessage(&msg).ok().log_err() }; unsafe { TranslateMessage(&msg).ok().log_err() };
} }
#[profiling::function]
fn handle_key_event<F>( fn handle_key_event<F>(
handle: HWND, handle: HWND,
wparam: WPARAM, wparam: WPARAM,

View file

@ -14,19 +14,20 @@ use itertools::Itertools;
use parking_lot::RwLock; use parking_lot::RwLock;
use smallvec::SmallVec; use smallvec::SmallVec;
use windows::{ use windows::{
UI::ViewManagement::UISettings, core::*, Win32::{
Win32::{
Foundation::*, Foundation::*,
Graphics::{ Graphics::{
DirectComposition::DCompositionWaitForCompositorClock, DirectComposition::DCompositionWaitForCompositorClock,
Dxgi::{
CreateDXGIFactory2, IDXGIAdapter1, IDXGIFactory6, IDXGIOutput, DXGI_CREATE_FACTORY_FLAGS, DXGI_GPU_PREFERENCE_MINIMUM_POWER
},
Gdi::*, Gdi::*,
Imaging::{CLSID_WICImagingFactory, IWICImagingFactory}, Imaging::{CLSID_WICImagingFactory, IWICImagingFactory},
}, },
Security::Credentials::*, Security::Credentials::*,
System::{Com::*, LibraryLoader::*, Ole::*, SystemInformation::*, Threading::*}, System::{Com::*, LibraryLoader::*, Ole::*, SystemInformation::*, Threading::*},
UI::{Input::KeyboardAndMouse::*, Shell::*, WindowsAndMessaging::*}, UI::{Input::KeyboardAndMouse::*, Shell::*, WindowsAndMessaging::*},
}, }, UI::ViewManagement::UISettings
core::*,
}; };
use crate::*; use crate::*;
@ -132,9 +133,11 @@ impl WindowsPlatform {
fn begin_vsync_thread(&self) { fn begin_vsync_thread(&self) {
let raw_window_handles = self.raw_window_handles.clone(); let raw_window_handles = self.raw_window_handles.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let vsync_provider = VSyncProvider::new();
loop { loop {
unsafe { unsafe {
DCompositionWaitForCompositorClock(None, INFINITE); // DCompositionWaitForCompositorClock(None, INFINITE);
vsync_provider.wait_for_vsync();
for handle in raw_window_handles.read().iter() { for handle in raw_window_handles.read().iter() {
RedrawWindow(Some(**handle), None, None, RDW_INVALIDATE) RedrawWindow(Some(**handle), None, None, RDW_INVALIDATE)
.ok() .ok()
@ -743,6 +746,46 @@ pub(crate) struct WindowCreationInfo {
pub(crate) main_thread_id_win32: u32, pub(crate) main_thread_id_win32: u32,
} }
struct VSyncProvider {
dxgi_output: IDXGIOutput,
}
impl VSyncProvider {
fn new() -> Self {
let dxgi_factory: IDXGIFactory6 =
unsafe { CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS::default()) }.unwrap();
let adapter: IDXGIAdapter1 = get_adapter(&dxgi_factory);
unsafe {
let dxgi_output = adapter.EnumOutputs(0).unwrap();
Self { dxgi_output }
}
}
fn wait_for_vsync(&self) {
unsafe {
self.dxgi_output.WaitForVBlank().unwrap();
}
}
}
fn get_adapter(dxgi_factory: &IDXGIFactory6) -> IDXGIAdapter1 {
unsafe {
for index in 0.. {
let adapter = dxgi_factory
.EnumAdapterByGpuPreference(index, DXGI_GPU_PREFERENCE_MINIMUM_POWER)
.unwrap();
return adapter;
}
}
unreachable!("No DXGI adapter found")
}
impl Default for VSyncProvider {
fn default() -> Self {
Self::new()
}
}
fn open_target(target: &str) { fn open_target(target: &str) {
unsafe { unsafe {
let ret = ShellExecuteW( let ret = ShellExecuteW(