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

WebGPU BYOW: to submit to queue twice would cause the program to freeze in surface.present. #24313

Open
Tracked by #23563
chirsz-ever opened this issue Jun 22, 2024 · 0 comments
Labels
bug Something isn't working correctly webgpu WebGPU API

Comments

@chirsz-ever
Copy link
Contributor

deno --version:

deno 1.44.4 (release, x86_64-unknown-linux-gnu)
v8 12.6.228.9
typescript 5.4.5

Platform: ArchLinux, KDE Plasma 6, wayland

Example code:

// https://deno.com/blog/v1.40#webgpu-windowing--bring-your-own-window

import {
    EventType,
    WindowBuilder,
    getKeyName
} from "jsr:@divy/[email protected]";

const width = 800;
const height = 600;

const win = new WindowBuilder("Hello, World!", width, height).build();

const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
    console.error("WebGPU not supported!");
    Deno.exit(1);
}
const device = await adapter.requestDevice();

/* Returns a Deno.UnsafeWindowSurface */
let surface = win.windowSurface();
/* Returns a WebGPU GPUCanvasContext */
const context = surface.getContext("webgpu");

context.configure({
    device,
    format: navigator.gpu.getPreferredCanvasFormat(),
    width,
    height,
});

const SDL_WINDOWEVENT_RESIZED = 5;

for await (const event of win.events()) {
    // console.log("get event ", event);
    if (event.type === EventType.Quit) break;
    else if (event.type === EventType.KeyDown) {
        // console.log(`key down `, getKeyName(event.keysym.sym))
        if (getKeyName(event.keysym.sym) === "Escape") {
            break;
        }
        continue;
    }
    else if (event.type === EventType.WindowEvent) {
        switch (event.event) {
            case SDL_WINDOWEVENT_RESIZED:
                console.info("resized!")
                surface = win.windowSurface();
                continue;
        }
    }
    else if (event.type !== EventType.Draw) continue;

    // Sine wave
    const r = Math.sin(Date.now() / 1000) / 2 + 0.5;
    const g = Math.sin(Date.now() / 1000 + 2) / 2 + 0.5;
    const b = Math.sin(Date.now() / 1000 + 4) / 2 + 0.5;

    const encode = (textureView: GPUTextureView) => {
        const commandEncoder = device.createCommandEncoder();
        const passEncoder = commandEncoder.beginRenderPass({
            colorAttachments: [
                {
                    view: textureView,
                    clearValue: { r, g, b, a: 1.0 },
                    loadOp: "clear",
                    storeOp: "store",
                },
            ],
        });
        passEncoder.end();

        return commandEncoder.finish();
    };

    // this would cause to freeze when calling `surface.present()`
    {
        device.queue.submit([encode(context.getCurrentTexture().createView())]);
        device.queue.submit([encode(context.getCurrentTexture().createView())]);
    }

    // this would also cause to freeze
    // {
    //     const cmbBuffers = [];
    //     cmbBuffers.push(encode(context.getCurrentTexture().createView()));
    //     cmbBuffers.push(encode(context.getCurrentTexture().createView()));
    //     device.queue.submit(cmbBuffers);
    // }

    // this would not cause to freeze
    // {
    //     const cmbBuffers = [];
    //     cmbBuffers.push(encode(context.getCurrentTexture().createView()));
    //     encode(context.getCurrentTexture().createView());
    //     device.queue.submit(cmbBuffers);
    // }

    surface.present();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly webgpu WebGPU API
Projects
None yet
Development

No branches or pull requests

2 participants