-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-clear-value.js
More file actions
41 lines (35 loc) · 1.36 KB
/
01-clear-value.js
File metadata and controls
41 lines (35 loc) · 1.36 KB
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
async function main()
{
// get webgpu adapter and device
const adaptor = await navigator.gpu?.requestAdapter();
const device = await adaptor?.requestDevice();
if (!device) {
fail('your browser does not support WebGPU');
return;
}
// create a webgpu context with the canvas
const canvas = document.getElementById("webgpu-canvas");
const context = canvas.getContext("webgpu");
const format = navigator.gpu.getPreferredCanvasFormat();
context.configure({device, format});
const render = () => {
// The output is a texture, and we are getting a "view"
// of texture as the output of the render pass
const textureView = context.getCurrentTexture().createView();
const renderPassDescriptor = {
colorAttachments: [{ // "attachments" refer to target texture that GPU renders to
view: textureView,
clearValue: [1.0, 0.0, 0.0, 1.0], // an arbitrary color you prefer
storeOp: 'store', // defines what happens after rendering
loadOp: 'clear', // defines what happnes at the beginning
}],
};
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.end();
// fire up the GPU to render the load value to the output texture
device.queue.submit([commandEncoder.finish()]);
};
render();
}
main();