justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 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="vertex-buffer-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> |
justin_fan@apple.com | 7a34ac3 | 2019-02-27 21:10:24 +0000 | [diff] [blame] | 10 | if (window.testRunner) |
| 11 | testRunner.waitUntilDone(); |
| 12 | |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 13 | const shaderCode = ` |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 14 | struct VertexOut |
| 15 | { |
mmaxfield@apple.com | 882a29d | 2019-08-27 04:58:11 +0000 | [diff] [blame] | 16 | float4 position : SV_Position; |
| 17 | float4 color : attribute(0); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
mmaxfield@apple.com | 882a29d | 2019-08-27 04:58:11 +0000 | [diff] [blame] | 20 | vertex VertexOut vertex_main(float4 position : attribute(0), uint iid : SV_InstanceID) |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 21 | { |
| 22 | VertexOut vOut; |
mmaxfield@apple.com | 882a29d | 2019-08-27 04:58:11 +0000 | [diff] [blame] | 23 | vOut.position = position; |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 24 | |
| 25 | if (iid == 0) { |
| 26 | vOut.position.z = 0.5; |
| 27 | vOut.color = float4(1, 0, 0, 1); |
| 28 | } else { |
| 29 | vOut.color = float4(0, 1, 0, 1); |
| 30 | } |
| 31 | |
| 32 | return vOut; |
| 33 | } |
| 34 | |
mmaxfield@apple.com | 882a29d | 2019-08-27 04:58:11 +0000 | [diff] [blame] | 35 | fragment float4 fragment_main(float4 color : attribute(0)) : SV_Target 0 |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 36 | { |
mmaxfield@apple.com | 882a29d | 2019-08-27 04:58:11 +0000 | [diff] [blame] | 37 | return color; |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 38 | } |
| 39 | ` |
| 40 | |
justin_fan@apple.com | 7a34ac3 | 2019-02-27 21:10:24 +0000 | [diff] [blame] | 41 | function createVertexBuffer(device) { |
justin_fan@apple.com | 52c5b6a | 2019-06-07 22:24:55 +0000 | [diff] [blame] | 42 | const vertexArray = new Float32Array([ |
justin_fan@apple.com | 7a34ac3 | 2019-02-27 21:10:24 +0000 | [diff] [blame] | 43 | // float4 xyzw |
| 44 | -1, 1, 0, 1, |
| 45 | -1, -1, 0, 1, |
| 46 | 1, 1, 0, 1, |
| 47 | 1, -1, 0, 1 |
justin_fan@apple.com | 52c5b6a | 2019-06-07 22:24:55 +0000 | [diff] [blame] | 48 | ]); |
| 49 | return createBufferWithData(device, { size: vertexArray.byteLength, usage: GPUBufferUsage.VERTEX }, vertexArray.buffer); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 50 | } |
| 51 | |
justin_fan@apple.com | d21f37e | 2019-05-30 21:05:16 +0000 | [diff] [blame] | 52 | function createVertexInputDescriptor() { |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 53 | return { |
justin_fan@apple.com | 0d96627 | 2019-03-13 01:33:11 +0000 | [diff] [blame] | 54 | indexFormat: "uint32", |
justin_fan@apple.com | d21f37e | 2019-05-30 21:05:16 +0000 | [diff] [blame] | 55 | vertexBuffers: [{ |
| 56 | stride: 4 * 4, |
| 57 | attributeSet: [{ |
| 58 | format: "float4", |
| 59 | shaderLocation: 0 |
| 60 | }] |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 61 | }] |
| 62 | } |
| 63 | } |
| 64 | |
mmaxfield@apple.com | e7ba53b | 2019-06-26 19:35:26 +0000 | [diff] [blame] | 65 | const canvas = document.querySelector("canvas"); |
| 66 | |
| 67 | async function test(device) { |
justin_fan@apple.com | 4583cd0 | 2019-03-12 00:46:41 +0000 | [diff] [blame] | 68 | const swapChain = createBasicSwapChain(canvas, device); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 69 | // FIXME: Replace with non-MSL shaders. |
| 70 | const shaderModule = device.createShaderModule({ code: shaderCode }); |
justin_fan@apple.com | 7a34ac3 | 2019-02-27 21:10:24 +0000 | [diff] [blame] | 71 | const vertexBuffer = createVertexBuffer(device); |
justin_fan@apple.com | d21f37e | 2019-05-30 21:05:16 +0000 | [diff] [blame] | 72 | const vertexInputDescriptor = createVertexInputDescriptor(); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 73 | const depthStateDescriptor = createBasicDepthStateDescriptor(); |
justin_fan@apple.com | d21f37e | 2019-05-30 21:05:16 +0000 | [diff] [blame] | 74 | const pipeline = createBasicPipeline(shaderModule, device, null, null, vertexInputDescriptor, depthStateDescriptor); |
justin_fan@apple.com | aeece1f | 2019-03-14 23:04:18 +0000 | [diff] [blame] | 75 | const commandEncoder = device.createCommandEncoder(); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 76 | |
| 77 | const basicAttachment = { |
justin_fan@apple.com | fe0a711 | 2019-03-18 19:07:56 +0000 | [diff] [blame] | 78 | attachment: swapChain.getCurrentTexture().createDefaultView(), |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 79 | loadOp: "clear", |
| 80 | storeOp: "store", |
| 81 | clearColor: { r: 1.0, g: 0, b: 0, a: 1.0 } |
| 82 | }; |
| 83 | |
| 84 | const depthAttachment = { |
justin_fan@apple.com | fe0a711 | 2019-03-18 19:07:56 +0000 | [diff] [blame] | 85 | attachment: createBasicDepthTexture(canvas, device).createDefaultView(), |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 86 | depthLoadOp: "clear", |
| 87 | depthStoreOp: "store", |
| 88 | clearDepth: 1.0 |
| 89 | }; |
| 90 | |
justin_fan@apple.com | aeece1f | 2019-03-14 23:04:18 +0000 | [diff] [blame] | 91 | const encoder = commandEncoder.beginRenderPass({ |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 92 | colorAttachments: [basicAttachment], |
| 93 | depthStencilAttachment: depthAttachment |
| 94 | }); |
| 95 | |
| 96 | encoder.setVertexBuffers(0, [vertexBuffer], [0]); |
| 97 | encoder.setPipeline(pipeline); |
| 98 | encoder.draw(4, 2, 0, 0); |
| 99 | encoder.endPass(); |
| 100 | |
justin_fan@apple.com | aeece1f | 2019-03-14 23:04:18 +0000 | [diff] [blame] | 101 | device.getQueue().submit([commandEncoder.finish()]); |
justin_fan@apple.com | 988c577 | 2019-02-12 20:11:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
mmaxfield@apple.com | e7ba53b | 2019-06-26 19:35:26 +0000 | [diff] [blame] | 104 | getBasicDevice().then(function(device) { |
| 105 | test(device).then(function() { |
| 106 | if (window.testRunner) |
| 107 | testRunner.notifyDone(); |
| 108 | }, function() { |
| 109 | if (window.testRunner) |
| 110 | testRunner.notifyDone(); |
| 111 | }); |
mmaxfield@apple.com | 58fa165 | 2019-06-13 03:44:24 +0000 | [diff] [blame] | 112 | }, function() { |
mmaxfield@apple.com | e7ba53b | 2019-06-26 19:35:26 +0000 | [diff] [blame] | 113 | drawGreenSquareInSoftware(canvas); |
mmaxfield@apple.com | 58fa165 | 2019-06-13 03:44:24 +0000 | [diff] [blame] | 114 | if (window.testRunner) |
| 115 | testRunner.notifyDone(); |
| 116 | }); |
| 117 | </script> |