1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-14 19:37:59 -05:00
denoland-deno/ext/webgpu/byow.rs

166 lines
4.4 KiB
Rust
Raw Normal View History

feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::OpState;
use deno_core::ResourceId;
use std::ffi::c_void;
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd"
))]
2024-05-05 10:22:18 -04:00
use std::ptr::NonNull;
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
use crate::surface::WebGpuSurface;
#[op2(fast)]
#[smi]
pub fn op_webgpu_surface_create(
state: &mut OpState,
#[string] system: &str,
p1: *const c_void,
p2: *const c_void,
) -> Result<ResourceId, AnyError> {
let instance = state.try_borrow::<super::Instance>().ok_or_else(|| {
type_error("Cannot create surface outside of WebGPU context. Did you forget to call `navigator.gpu.requestAdapter()`?")
})?;
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
// Security note:
//
// The `p1` and `p2` parameters are pointers to platform-specific window
// handles.
//
// The code below works under the assumption that:
//
// - handles can only be created by the FFI interface which
// enforces --allow-ffi.
//
// - `*const c_void` deserizalizes null and v8::External.
//
// - Only FFI can export v8::External to user code.
if p1.is_null() {
return Err(type_error("Invalid parameters"));
}
let (win_handle, display_handle) = raw_window(system, p1, p2)?;
2024-05-05 10:22:18 -04:00
// SAFETY: see above comment
let surface = unsafe {
instance.instance_create_surface(display_handle, win_handle, None)?
};
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
let rid = state
.resource_table
.add(WebGpuSurface(instance.clone(), surface));
Ok(rid)
}
type RawHandles = (
raw_window_handle::RawWindowHandle,
raw_window_handle::RawDisplayHandle,
);
#[cfg(target_os = "macos")]
fn raw_window(
system: &str,
2024-05-05 10:22:18 -04:00
_ns_window: *const c_void,
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
ns_view: *const c_void,
) -> Result<RawHandles, AnyError> {
if system != "cocoa" {
return Err(type_error("Invalid system on macOS"));
}
2024-05-05 10:22:18 -04:00
let win_handle = raw_window_handle::RawWindowHandle::AppKit(
raw_window_handle::AppKitWindowHandle::new(
NonNull::new(ns_view as *mut c_void)
.ok_or(type_error("ns_view is null"))?,
),
);
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
let display_handle = raw_window_handle::RawDisplayHandle::AppKit(
2024-05-05 10:22:18 -04:00
raw_window_handle::AppKitDisplayHandle::new(),
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
);
Ok((win_handle, display_handle))
}
#[cfg(target_os = "windows")]
fn raw_window(
system: &str,
window: *const c_void,
hinstance: *const c_void,
) -> Result<RawHandles, AnyError> {
use raw_window_handle::WindowsDisplayHandle;
if system != "win32" {
return Err(type_error("Invalid system on Windows"));
}
let win_handle = {
2024-05-05 10:22:18 -04:00
let mut handle = raw_window_handle::Win32WindowHandle::new(
std::num::NonZeroIsize::new(window as isize)
.ok_or(type_error("window is null"))?,
);
handle.hinstance = std::num::NonZeroIsize::new(hinstance as isize);
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
raw_window_handle::RawWindowHandle::Win32(handle)
};
let display_handle =
2024-05-05 10:22:18 -04:00
raw_window_handle::RawDisplayHandle::Windows(WindowsDisplayHandle::new());
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
Ok((win_handle, display_handle))
}
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
fn raw_window(
system: &str,
window: *const c_void,
display: *const c_void,
) -> Result<RawHandles, AnyError> {
let (win_handle, display_handle);
if system == "x11" {
2024-05-05 10:22:18 -04:00
win_handle = raw_window_handle::RawWindowHandle::Xlib(
raw_window_handle::XlibWindowHandle::new(window as *mut c_void as _),
);
display_handle = raw_window_handle::RawDisplayHandle::Xlib(
raw_window_handle::XlibDisplayHandle::new(
NonNull::new(display as *mut c_void),
0,
),
);
} else if system == "wayland" {
2024-05-05 10:22:18 -04:00
win_handle = raw_window_handle::RawWindowHandle::Wayland(
raw_window_handle::WaylandWindowHandle::new(
NonNull::new(window as *mut c_void)
.ok_or(type_error("window is null"))?,
),
);
display_handle = raw_window_handle::RawDisplayHandle::Wayland(
raw_window_handle::WaylandDisplayHandle::new(
NonNull::new(display as *mut c_void)
.ok_or(type_error("display is null"))?,
),
);
} else {
return Err(type_error("Invalid system on Linux/BSD"));
feat:: External webgpu surfaces / BYOW (#21835) This PR contains the implementation of the External webgpu surfaces / BYOW proposal. BYOW stands for "Bring your own window". Closes #21713 Adds `Deno.UnsafeWindowSurface` ( `--unstable-webgpu` API) to the `Deno` namespace: ```typescript class UnsafeWindowSurface { constructor( system: "cocoa" | "x11" | "win32", winHandle: Deno.PointerValue, displayHandle: Deno.PointerValue | null ); getContext(type: "webgpu"): GPUCanvasContext; present(): void; } ``` For the initial pass, I've opted to support the three major windowing systems. The parameters correspond to the table below: | system | winHandle | displayHandle | | ----------------- | ---------- | ------- | | "cocoa" (macOS) | `NSView*` | - | | "win32" (Windows) | `HWND` | `HINSTANCE` | | "x11" (Linux) | Xlib `Window` | Xlib `Display*` | Ecosystem support: - [x] deno_sdl2 (sdl2) - [mod.ts#L1209](https://github.com/littledivy/deno_sdl2/blob/7e177bc6524750a8849c25ce421798b2e71ec943/mod.ts#L1209) - [x] dwm (glfw) - https://github.com/deno-windowing/dwm/issues/29 - [ ] pane (winit) <details> <summary>Example</summary> ```typescript // A simple clear screen pass, colors based on mouse position. import { EventType, WindowBuilder } from "https://deno.land/x/sdl2@0.7.0/mod.ts"; const window = new WindowBuilder("sdl2 + deno + webgpu", 640, 480).build(); const [system, windowHandle, displayHandle] = window.rawHandle(); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); const context = Deno.createWindowSurface(system, windowHandle, displayHandle); context.configure({ device: device, format: "bgra8unorm", height: 480, width: 640, }); let r = 0.0; let g = 0.0; let b = 0.0; for (const event of window.events()) { if (event.type === EventType.Quit) { break; } else if (event.type === EventType.Draw) { const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { colorAttachments: [ { view: textureView, clearValue: { r, g, b, a: 1.0 }, loadOp: "clear", storeOp: "store", }, ], }; const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); Deno.presentGPUCanvasContext(context); } if (event.type === EventType.MouseMotion) { r = event.x / 640; g = event.y / 480; b = 1.0 - r - g; } } ``` You can find more examples in the linked tracking issue. </details> --------- Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2024-01-19 12:19:14 -05:00
}
Ok((win_handle, display_handle))
}
#[cfg(not(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd",
)))]
fn raw_window(
_system: &str,
_window: *const c_void,
_display: *const c_void,
) -> Result<RawHandles, AnyError> {
Err(type_error("Unsupported platform"))
}