Skip to content

Commit b93e18c

Browse files
committed
Support for windows surfaces.
1 parent 2232a0d commit b93e18c

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

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
@@ -14,4 +14,7 @@ raw-window-handle = "0.6"
1414

1515
[target.'cfg(target_os = "macos")'.dependencies]
1616
objc2 = { version = "0.6", default-features = false }
17-
objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] }
17+
objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] }
18+
19+
[target.'cfg(target_os = "windows")'.dependencies]
20+
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
@@ -128,8 +128,41 @@ pub fn create_surface(
128128
};
129129

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

134167
#[cfg(target_os = "linux")]
135168
let (raw_window_handle, raw_display_handle) =

0 commit comments

Comments
 (0)