Skip to content

External WebGPU surfaces / BYOW #21713

Closed
@littledivy

Description

@littledivy

Goals:

  • De-facto windowing solution.
  • BYOW: Bring your own window.
  • Deno should not link to native windowing systems like X11.
  • Deno should not have to be forked for windowing support.
/* any windowing library */
import { WindowBuilder } from "https://deno.land/x/sdl2/mod.ts";
const win = new WindowBuilder("Hello WGPU", 512, 512).build();

/* [string, Deno.PointerValue, Deno.PointerValue] */
const [system, windowHandle, displayHandle] = win.rawHandle();

const surface = new Deno.UnsafeWindowSurface(system, windowHandle, displayHandle);

/* GPUCanvasContext */
const context = surface.getContext();
context.configure({ /* ... */ });

for (const events of win.events()) {
  const texture = context.getCurrentTexture();
  // ...
  surface.present();
}

Proposed: Add Deno.UnsafeWindowSurface API:

  1. Creates a raw_window_handle::RawWindowHandle implementation from windowHandle and displayHandle
  2. Internally calls instance->create_surface(&win)
  3. Surface is returned wrapped in as a GPUCanvasContext (@crowlKats has already implemented presentation APIs)
class UnsafeWindowSurface {
  constructor(
    system: "cocoa" | "x11" | "win32",
    winHandle: Deno.PointerValue,
    displayHandle: Deno.PointerValue | null
  );
  
  getContext(type: "webgpu"): GPUCanvasContext;

  present(): void;
}

Pros:

  • Single binary. No extensions or Deno forks.
  • Does not link CLI to windowing systems.
  • Plug and play FFI windowing libraries with Deno WebGPU.
  • deno compile-able.

Cons:

  • Very low level
  • displayHandle and winHandle can be invalid handles. Creating them requires --allow-ffi --unstable

I have a working implementation and a few demos I will share shortly.

cc-ing for visibility: @DjDeveloperr @load1n9 @eliassjogreen (also @aapoalas since this touches FFI too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    suggestionsuggestions for new features (yet to be agreed)webgpuWebGPU API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions