diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-01-19 22:49:14 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-19 22:49:14 +0530 |
commit | 40febd9dd1224a15a3bc877e2fdf010c4c893e0e (patch) | |
tree | 9c53c67939e1f29b65d0e43cce6a93d654854187 /ext/webgpu/02_surface.js | |
parent | 47232f8a41532a7ea3ed05c87b52bbd66a68cdab (diff) |
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>
Diffstat (limited to 'ext/webgpu/02_surface.js')
-rw-r--r-- | ext/webgpu/02_surface.js | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/ext/webgpu/02_surface.js b/ext/webgpu/02_surface.js index 319179dc1..1c9751f62 100644 --- a/ext/webgpu/02_surface.js +++ b/ext/webgpu/02_surface.js @@ -16,6 +16,7 @@ const { ObjectPrototypeIsPrototypeOf, Symbol, SymbolFor, + TypeError, } = primordials; import * as webidl from "ext:deno_webidl/00_webidl.js"; @@ -166,8 +167,28 @@ function createCanvasContext(options) { return canvasContext; } -function presentGPUCanvasContext(ctx) { - ctx[_present](); +// External webgpu surfaces + +// TODO(@littledivy): This will extend `OffscreenCanvas` when we add it. +class UnsafeWindowSurface { + #ctx; + #surfaceRid; + + constructor(system, win, display) { + this.#surfaceRid = ops.op_webgpu_surface_create(system, win, display); + } + + getContext(context) { + if (context !== "webgpu") { + throw new TypeError("Only 'webgpu' context is supported."); + } + this.#ctx = createCanvasContext({ surfaceRid: this.#surfaceRid }); + return this.#ctx; + } + + present() { + this.#ctx[_present](); + } } -export { createCanvasContext, GPUCanvasContext, presentGPUCanvasContext }; +export { GPUCanvasContext, UnsafeWindowSurface }; |