-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwebgpu-triangle.ts
73 lines (65 loc) · 1.63 KB
/
webgpu-triangle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createWindowGPU, mainloop } from "../ext/webgpu.ts";
const window = await createWindowGPU({
title: "Deno Window Manager",
width: 512,
height: 512,
resizable: true,
});
const context = window.getContext("webgpu");
context.configure({
device: window.device,
format: "bgra8unorm",
});
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 = window.device.createShaderModule({
code: shaderCode,
});
const pipelineLayout = window.device.createPipelineLayout({
bindGroupLayouts: [],
});
const renderPipeline = window.device.createRenderPipeline({
layout: pipelineLayout,
vertex: {
module: shaderModule,
entryPoint: "vs_main",
},
fragment: {
module: shaderModule,
entryPoint: "fs_main",
targets: [
{
format: "bgra8unorm",
},
],
},
});
mainloop(() => {
const encoder = window.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();
window.device.queue.submit([encoder.finish()]);
window.surface.present();
}, false);