Skip to content

Commit af81b42

Browse files
authored
Support for windows surfaces. (#1310)
1 parent 8b80eb1 commit af81b42

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

renderer/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ raw-window-handle = "0.6"
1212

1313
[target.'cfg(target_os = "macos")'.dependencies]
1414
objc2 = { version = "0.6", default-features = false }
15-
objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] }
15+
objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] }
16+
17+
[target.'cfg(target_os = "windows")'.dependencies]
18+
windows = { version = "0.58", features = ["Win32_Foundation", "Win32_System_LibraryLoader"] }

renderer/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,41 @@ pub fn create_surface(
131131
};
132132

133133
#[cfg(target_os = "windows")]
134-
let (raw_window_handle, raw_display_handle) =
135-
{ todo!("implemnt windows raw window handle conversion") };
134+
let (raw_window_handle, raw_display_handle) = {
135+
use raw_window_handle::{Win32WindowHandle, WindowsDisplayHandle};
136+
use std::num::NonZeroIsize;
137+
use windows::Win32::Foundation::HINSTANCE;
138+
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
139+
140+
if window_handle == 0 {
141+
return Err(error::ProcessingError::InvalidWindowHandle);
142+
}
143+
144+
// HWND is isize, so cast it
145+
let hwnd_isize = window_handle as isize;
146+
let hwnd_nonzero = match NonZeroIsize::new(hwnd_isize) {
147+
Some(nz) => nz,
148+
None => return Err(error::ProcessingError::InvalidWindowHandle),
149+
};
150+
151+
let mut window = Win32WindowHandle::new(hwnd_nonzero);
152+
153+
// VK_KHR_win32_surface requires hinstance *and* hwnd
154+
// SAFETY: GetModuleHandleW(NULL) is safe
155+
let hinstance = unsafe { GetModuleHandleW(None) }
156+
.map_err(|_| error::ProcessingError::InvalidWindowHandle)?;
157+
158+
let hinstance_nonzero = NonZeroIsize::new(hinstance.0 as isize)
159+
.ok_or(error::ProcessingError::InvalidWindowHandle)?;
160+
window.hinstance = Some(hinstance_nonzero);
161+
162+
let display = WindowsDisplayHandle::new();
163+
164+
(
165+
RawWindowHandle::Win32(window),
166+
RawDisplayHandle::Windows(display),
167+
)
168+
};
136169

137170
#[cfg(target_os = "linux")]
138171
let (raw_window_handle, raw_display_handle) =

0 commit comments

Comments
 (0)