Skip to content

Commit

Permalink
feat: webgpu triangle experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Jan 24, 2024
1 parent 21d3fa9 commit 8e8679e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"example:capture": "deno run -A --unstable examples/mouse-capture.ts",
"example:custom_cursor": "deno run -A --unstable examples/custom-cursor.ts",
"example:custom_icon": "deno run -A --unstable examples/custom-icon.ts",
"example:webgpu": "deno run -A --unstable-ffi --unstable-webgpu examples/webgpu.ts"
"example:webgpu": "deno run -A --unstable-ffi --unstable-webgpu examples/webgpu.ts",
"example:webgpu-triangle": "deno run -A --unstable-ffi --unstable-webgpu examples/webgpu-triangle.ts"
},

"lint": {
Expand Down
82 changes: 82 additions & 0 deletions examples/webgpu-triangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { createWindow, mainloop } from "../mod.ts";

const adapter = await navigator.gpu.requestAdapter();
const device = await adapter!.requestDevice();

const window = createWindow({
title: "Deno Window Manager",
width: 512,
height: 512,
resizable: true,
});

const { width, height } = window.framebufferSize;

const surface = window.windowSurface();

const context = surface.getContext("webgpu");

context.configure({
device,
format: "bgra8unorm",
width,
height,
});

const shaderCode = `
@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
`;

const shaderModule = device.createShaderModule({
code: shaderCode,
});

const pipelineLayout = device.createPipelineLayout({
bindGroupLayouts: [],
});

const renderPipeline = device.createRenderPipeline({
layout: pipelineLayout,
vertex: {
module: shaderModule,
entryPoint: "vs_main",
},
fragment: {
module: shaderModule,
entryPoint: "fs_main",
targets: [
{
format: "bgra8unorm",
},
],
},
});
mainloop(() => {
const encoder = device.createCommandEncoder();
const texture = context.getCurrentTexture().createView();
const renderPass = encoder.beginRenderPass({
colorAttachments: [
{
view: texture,
storeOp: "store",
loadOp: "clear",
clearValue: { r: 0, g: 1, b: 0, a: 1.0 },
},
],
});
renderPass.setPipeline(renderPipeline);
renderPass.draw(3, 1);
renderPass.end();
device.queue.submit([encoder.finish()]);
surface.present();
}, false);
2 changes: 1 addition & 1 deletion examples/webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ await mainloop(() => {

device.queue.submit([commandEncoder.finish()]);
surface.present();
}, false);
}, false);

0 comments on commit 8e8679e

Please sign in to comment.