From b93e18cb65390f103759c2adfed97d8e9ca4fe54 Mon Sep 17 00:00:00 2001 From: charlotte Date: Fri, 31 Oct 2025 21:56:00 -0700 Subject: [PATCH] Support for windows surfaces. --- core/src/processing/core/NativeLibrary.java | 4 ++- core/src/processing/webgpu/PSurfaceGLFW.java | 3 ++ libProcessing/Cargo.lock | 1 + libProcessing/renderer/Cargo.toml | 5 ++- libProcessing/renderer/src/lib.rs | 37 ++++++++++++++++++-- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/core/src/processing/core/NativeLibrary.java b/core/src/processing/core/NativeLibrary.java index 15b7331c2..208eeedd0 100644 --- a/core/src/processing/core/NativeLibrary.java +++ b/core/src/processing/core/NativeLibrary.java @@ -82,7 +82,9 @@ public static String getPlatform() { */ private static void loadNativeLibrary() throws IOException { String platformTarget = platform + "-" + architecture; - String libraryFileName = "lib" + LIBRARY_NAME + "." + libraryExtension; + String libraryFileName = platform.equals("windows") + ? LIBRARY_NAME + "." + libraryExtension + : "lib" + LIBRARY_NAME + "." + libraryExtension; String resourcePath = "/native/" + platformTarget + "/" + libraryFileName; // check classloader for resource in jar diff --git a/core/src/processing/webgpu/PSurfaceGLFW.java b/core/src/processing/webgpu/PSurfaceGLFW.java index 49f42230a..d9591d10e 100644 --- a/core/src/processing/webgpu/PSurfaceGLFW.java +++ b/core/src/processing/webgpu/PSurfaceGLFW.java @@ -4,6 +4,7 @@ import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWNativeCocoa; +import org.lwjgl.glfw.GLFWNativeWin32; import org.lwjgl.glfw.GLFWWindowPosCallback; import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.Platform; @@ -120,6 +121,8 @@ public Object getNative() { public long getWindowHandle() { if (Platform.get() == Platform.MACOSX) { return GLFWNativeCocoa.glfwGetCocoaWindow(window); + } else if (Platform.get() == Platform.WINDOWS) { + return GLFWNativeWin32.glfwGetWin32Window(window); } else { throw new UnsupportedOperationException("Window handle retrieval not implemented for this platform"); } diff --git a/libProcessing/Cargo.lock b/libProcessing/Cargo.lock index cd58b5248..72d956cec 100644 --- a/libProcessing/Cargo.lock +++ b/libProcessing/Cargo.lock @@ -4070,6 +4070,7 @@ dependencies = [ "thiserror 2.0.17", "tracing", "tracing-subscriber", + "windows 0.58.0", ] [[package]] diff --git a/libProcessing/renderer/Cargo.toml b/libProcessing/renderer/Cargo.toml index 4dea5a618..0c83b3f94 100644 --- a/libProcessing/renderer/Cargo.toml +++ b/libProcessing/renderer/Cargo.toml @@ -14,4 +14,7 @@ raw-window-handle = "0.6" [target.'cfg(target_os = "macos")'.dependencies] objc2 = { version = "0.6", default-features = false } -objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] } \ No newline at end of file +objc2-app-kit = { version = "0.3", features = ["NSWindow", "NSView"] } + +[target.'cfg(target_os = "windows")'.dependencies] +windows = { version = "0.58", features = ["Win32_Foundation", "Win32_System_LibraryLoader"] } \ No newline at end of file diff --git a/libProcessing/renderer/src/lib.rs b/libProcessing/renderer/src/lib.rs index ddbd5729c..b7bde1249 100644 --- a/libProcessing/renderer/src/lib.rs +++ b/libProcessing/renderer/src/lib.rs @@ -128,8 +128,41 @@ pub fn create_surface( }; #[cfg(target_os = "windows")] - let (raw_window_handle, raw_display_handle) = - { todo!("implemnt windows raw window handle conversion") }; + let (raw_window_handle, raw_display_handle) = { + use raw_window_handle::{Win32WindowHandle, WindowsDisplayHandle}; + use std::num::NonZeroIsize; + use windows::Win32::Foundation::HINSTANCE; + use windows::Win32::System::LibraryLoader::GetModuleHandleW; + + if window_handle == 0 { + return Err(error::ProcessingError::InvalidWindowHandle); + } + + // HWND is isize, so cast it + let hwnd_isize = window_handle as isize; + let hwnd_nonzero = match NonZeroIsize::new(hwnd_isize) { + Some(nz) => nz, + None => return Err(error::ProcessingError::InvalidWindowHandle), + }; + + let mut window = Win32WindowHandle::new(hwnd_nonzero); + + // VK_KHR_win32_surface requires hinstance *and* hwnd + // SAFETY: GetModuleHandleW(NULL) is safe + let hinstance = unsafe { GetModuleHandleW(None) } + .map_err(|_| error::ProcessingError::InvalidWindowHandle)?; + + let hinstance_nonzero = NonZeroIsize::new(hinstance.0 as isize) + .ok_or(error::ProcessingError::InvalidWindowHandle)?; + window.hinstance = Some(hinstance_nonzero); + + let display = WindowsDisplayHandle::new(); + + ( + RawWindowHandle::Win32(window), + RawDisplayHandle::Windows(display), + ) + }; #[cfg(target_os = "linux")] let (raw_window_handle, raw_display_handle) =