Skip to content

Commit 1ee012e

Browse files
authored
Support for windows surfaces. (#1310)
1 parent 89f8a65 commit 1ee012e

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

core/src/processing/core/NativeLibrary.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public static String getPlatform() {
8282
*/
8383
private static void loadNativeLibrary() throws IOException {
8484
String platformTarget = platform + "-" + architecture;
85-
String libraryFileName = "lib" + LIBRARY_NAME + "." + libraryExtension;
85+
String libraryFileName = platform.equals("windows")
86+
? LIBRARY_NAME + "." + libraryExtension
87+
: "lib" + LIBRARY_NAME + "." + libraryExtension;
8688
String resourcePath = "/native/" + platformTarget + "/" + libraryFileName;
8789

8890
// check classloader for resource in jar

core/src/processing/webgpu/PSurfaceGLFW.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.lwjgl.glfw.GLFWErrorCallback;
55
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
66
import org.lwjgl.glfw.GLFWNativeCocoa;
7+
import org.lwjgl.glfw.GLFWNativeWin32;
78
import org.lwjgl.glfw.GLFWWindowPosCallback;
89
import org.lwjgl.system.MemoryUtil;
910
import org.lwjgl.system.Platform;
@@ -120,6 +121,8 @@ public Object getNative() {
120121
public long getWindowHandle() {
121122
if (Platform.get() == Platform.MACOSX) {
122123
return GLFWNativeCocoa.glfwGetCocoaWindow(window);
124+
} else if (Platform.get() == Platform.WINDOWS) {
125+
return GLFWNativeWin32.glfwGetWin32Window(window);
123126
} else {
124127
throw new UnsupportedOperationException("Window handle retrieval not implemented for this platform");
125128
}

libProcessing/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.

libProcessing/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"] }

libProcessing/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)