f209b03343710f9bf5f45dabad8ac9dc165ddb6e
[WebKit-https.git] / LayoutTests / webgpu / blend-triangle-strip.html
1 <!DOCTYPE html>
2 <meta charset="utf-8">
3 <title>WebGPU Hello Triangles</title>
4 <meta name="assert" content="WebGPU correctly renders a green canvas.">
5 <link rel="match" href="blend-triangle-strip-expected.html">
6 <p>Pass if square canvas below is completely green.</p>
7 <canvas width="400" height="400"></canvas>
8 <script src="js/webgpu-functions.js"></script>
9 <script>
10 if (window.testRunner)
11     testRunner.waitUntilDone();
12
13 const positionAttributeNum = 0;
14
15 const shaderCode = `
16 #include <metal_stdlib>
17     
18 using namespace metal;
19
20 struct VertexIn
21 {
22     float4 position [[attribute(${positionAttributeNum})]];
23 };
24
25 struct VertexOut
26 {
27     float4 position [[position]];
28     float4 color;
29 };
30
31 vertex VertexOut vertex_main(VertexIn vertexIn [[stage_in]])
32 {
33     VertexOut vOut;
34     vOut.position = vertexIn.position;
35     vOut.color = float4(0, 0.5, 0, 0.5);
36     return vOut;
37 }
38
39 fragment float4 fragment_main(VertexOut v [[stage_in]])
40 {
41     return v.color;
42 }
43 `;
44
45 async function test() {
46     const device = await getBasicDevice();
47     const canvas = document.querySelector("canvas");
48
49     const shaderModule = device.createShaderModule({ code: shaderCode });
50
51     const colorStates = [{
52         format: "bgra8unorm",
53         alphaBlend: { dstFactor: "one" },
54         colorBlend: { dstFactor: "one" }
55     }];
56
57     const vertexInputDescriptor = {
58         indexFormat: "uint32",
59         vertexBuffers: [{
60             stride: 4 * 4,
61             attributeSet: [{
62                 format: "float4",
63                 shaderLocation: positionAttributeNum
64             }]
65         }]
66     };
67
68     const pipeline = createBasicPipeline(shaderModule, device, colorStates, null, vertexInputDescriptor);
69
70     const vertexData = new Float32Array([
71         -1, 1, 0, 1,
72         -1, -1, 0, 1,
73         1, 1, 0, 1,
74         1, -1, 0, 1
75     ]);
76     const vertexBuffer = createBufferWithData(device, { size: vertexData.byteLength, usage: GPUBufferUsage.VERTEX }, vertexData.buffer);
77
78     const context = canvas.getContext("gpu");
79     const swapChain = context.configureSwapChain({ device: device, format: "bgra8unorm" });
80     const colorAttachment = {
81         attachment: swapChain.getCurrentTexture().createDefaultView(),
82         loadOp: "clear",
83         storeOp: "store",
84         clearColor: { r: 0, g: 0, b: 0, a: 0 }
85     };
86
87     const commandEncoder = device.createCommandEncoder();
88     const passEncoder = commandEncoder.beginRenderPass({ colorAttachments: [colorAttachment] });
89     passEncoder.setPipeline(pipeline);
90     passEncoder.setVertexBuffers(0, [vertexBuffer], [0]);
91     passEncoder.draw(4, 1, 0, 0);
92     passEncoder.draw(4, 1, 0, 0);
93     passEncoder.endPass();
94
95     device.getQueue().submit([commandEncoder.finish()]);
96 }
97
98 window.addEventListener("load", function() {
99     test().then(function() {
100         if (window.testRunner)
101             testRunner.notifyDone();
102     }, function() {
103         if (window.testRunner)
104             testRunner.notifyDone();
105     });
106 }, false);
107 </script>