Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webgpu): external webgpu surfaces / byow #21835

Merged
merged 14 commits into from
Jan 19, 2024

Conversation

littledivy
Copy link
Member

@littledivy littledivy commented Jan 7, 2024

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:

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:

Example
// 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.

@littledivy littledivy marked this pull request as ready for review January 9, 2024 10:51
ext/webgpu/02_surface.js Outdated Show resolved Hide resolved
Copy link
Member

@crowlKats crowlKats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, though would like someone with more FFI knowledge than me to take a look as well.

Copy link
Collaborator

@aapoalas aapoalas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From an FFI point of view there isn't much to say here: "Pointers are bad mmkay" about sums it up but that's not really very useful :)

I had some questions but nothing really major. This is a very interesting feature in either case and I'm all for taking advantage of the opportunities that FFI provides.

ext/webgpu/byow.rs Outdated Show resolved Hide resolved
ext/webgpu/byow.rs Show resolved Hide resolved
ext/webgpu/byow.rs Show resolved Hide resolved
@littledivy
Copy link
Member Author

@bartlomieju Should this be part of the 1.40 milestone?

@littledivy littledivy merged commit 40febd9 into denoland:main Jan 19, 2024
14 checks passed
@littledivy littledivy added this to the 1.40 milestone Jan 20, 2024
@littledivy littledivy deleted the byow_part_2 branch January 20, 2024 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External WebGPU surfaces / BYOW
4 participants