[WebGPU] Move 'gpu' API entry point from DOMWindow to Navigator
[WebKit-https.git] / LayoutTests / webgpu / js / webgpu-functions.js
1 async function getBasicDevice() {
2     const adapter = await navigator.gpu.requestAdapter({ powerPreference: "low-power" });
3     const device = await adapter.requestDevice();
4     return device;
5 }
6
7 function createBasicSwapChain(canvas, device) {
8     const context = canvas.getContext("gpu");
9     return context.configureSwapChain({ device: device, format: "bgra8unorm" });
10 }
11
12 function createBasicDepthStateDescriptor() {
13     return {
14         depthWriteEnabled: true,
15         depthCompare: "less"
16     };
17 }
18
19 function createBasicDepthTexture(canvas, device) {
20     const depthSize = {
21         width: canvas.width,
22         height: canvas.height,
23         depth: 1
24     };
25
26     return device.createTexture({
27         size: depthSize,
28         format: "depth32float-stencil8",
29         usage: GPUTextureUsage.OUTPUT_ATTACHMENT
30     });
31 }
32
33 function createBasicPipeline(shaderModule, device, colorStates, pipelineLayout, inputStateDescriptor, depthStateDescriptor, primitiveTopology = "triangle-strip") {
34     const vertexStageDescriptor = {
35         module: shaderModule,
36         entryPoint: "vertex_main" 
37     };
38
39     const fragmentStageDescriptor = {
40         module: shaderModule,
41         entryPoint: "fragment_main"
42     };
43
44     if (!colorStates) {
45         colorStates = [{ 
46             format: "bgra8unorm",
47             alphaBlend: {},
48             colorBlend: {}
49         }];
50     }
51
52     if (!inputStateDescriptor)
53         inputStateDescriptor = { attributes: [], inputs: [] };
54
55     const pipelineDescriptor = {
56         vertexStage: vertexStageDescriptor,
57         fragmentStage: fragmentStageDescriptor,
58         primitiveTopology: primitiveTopology,
59         colorStates: colorStates,
60         inputState: inputStateDescriptor
61     };
62
63     if (pipelineLayout)
64         pipelineDescriptor.layout = pipelineLayout;
65
66     if (depthStateDescriptor)
67         pipelineDescriptor.depthStencilState = depthStateDescriptor;
68
69     return device.createRenderPipeline(pipelineDescriptor);
70 }
71
72 function beginBasicRenderPass(swapChain, commandEncoder) {
73     const basicAttachment = {
74         attachment: swapChain.getCurrentTexture().createDefaultView(),
75         loadOp: "clear",
76         storeOp: "store",
77         clearColor: { r: 1.0, g: 0, b: 0, a: 1.0 }
78     };
79
80     // FIXME: Flesh out the rest of WebGPURenderPassDescriptor. 
81     return commandEncoder.beginRenderPass({ colorAttachments : [basicAttachment] });
82 }
83
84 function encodeBasicCommands(renderPassEncoder, renderPipeline, vertexBuffer) {
85     if (vertexBuffer)
86         renderPassEncoder.setVertexBuffers(0, [vertexBuffer], [0]);
87     renderPassEncoder.setPipeline(renderPipeline);
88     renderPassEncoder.draw(4, 1, 0, 0);
89     renderPassEncoder.endPass();
90 }