blob: 134424be8652e7a6fc6787d2341a37bc04ddf99e [file] [log] [blame]
justin_fan@apple.com988c5772019-02-12 20:11:49 +00001<!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.com7a34ac32019-02-27 21:10:24 +000010if (window.testRunner)
11 testRunner.waitUntilDone();
12
justin_fan@apple.com988c5772019-02-12 20:11:49 +000013const shaderCode = `
justin_fan@apple.com988c5772019-02-12 20:11:49 +000014struct VertexOut
15{
mmaxfield@apple.com882a29d2019-08-27 04:58:11 +000016 float4 position : SV_Position;
17 float4 color : attribute(0);
justin_fan@apple.com988c5772019-02-12 20:11:49 +000018};
19
mmaxfield@apple.com882a29d2019-08-27 04:58:11 +000020vertex VertexOut vertex_main(float4 position : attribute(0), uint iid : SV_InstanceID)
justin_fan@apple.com988c5772019-02-12 20:11:49 +000021{
22 VertexOut vOut;
mmaxfield@apple.com882a29d2019-08-27 04:58:11 +000023 vOut.position = position;
justin_fan@apple.com988c5772019-02-12 20:11:49 +000024
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.com882a29d2019-08-27 04:58:11 +000035fragment float4 fragment_main(float4 color : attribute(0)) : SV_Target 0
justin_fan@apple.com988c5772019-02-12 20:11:49 +000036{
mmaxfield@apple.com882a29d2019-08-27 04:58:11 +000037 return color;
justin_fan@apple.com988c5772019-02-12 20:11:49 +000038}
39`
40
justin_fan@apple.com7a34ac32019-02-27 21:10:24 +000041function createVertexBuffer(device) {
justin_fan@apple.com52c5b6a2019-06-07 22:24:55 +000042 const vertexArray = new Float32Array([
justin_fan@apple.com7a34ac32019-02-27 21:10:24 +000043 // 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.com52c5b6a2019-06-07 22:24:55 +000048 ]);
49 return createBufferWithData(device, { size: vertexArray.byteLength, usage: GPUBufferUsage.VERTEX }, vertexArray.buffer);
justin_fan@apple.com988c5772019-02-12 20:11:49 +000050}
51
justin_fan@apple.comd21f37e2019-05-30 21:05:16 +000052function createVertexInputDescriptor() {
justin_fan@apple.com988c5772019-02-12 20:11:49 +000053 return {
justin_fan@apple.com0d966272019-03-13 01:33:11 +000054 indexFormat: "uint32",
justin_fan@apple.comd21f37e2019-05-30 21:05:16 +000055 vertexBuffers: [{
56 stride: 4 * 4,
57 attributeSet: [{
58 format: "float4",
59 shaderLocation: 0
60 }]
justin_fan@apple.com988c5772019-02-12 20:11:49 +000061 }]
62 }
63}
64
mmaxfield@apple.come7ba53b2019-06-26 19:35:26 +000065const canvas = document.querySelector("canvas");
66
67async function test(device) {
justin_fan@apple.com4583cd02019-03-12 00:46:41 +000068 const swapChain = createBasicSwapChain(canvas, device);
justin_fan@apple.com988c5772019-02-12 20:11:49 +000069 // FIXME: Replace with non-MSL shaders.
70 const shaderModule = device.createShaderModule({ code: shaderCode });
justin_fan@apple.com7a34ac32019-02-27 21:10:24 +000071 const vertexBuffer = createVertexBuffer(device);
justin_fan@apple.comd21f37e2019-05-30 21:05:16 +000072 const vertexInputDescriptor = createVertexInputDescriptor();
justin_fan@apple.com988c5772019-02-12 20:11:49 +000073 const depthStateDescriptor = createBasicDepthStateDescriptor();
justin_fan@apple.comd21f37e2019-05-30 21:05:16 +000074 const pipeline = createBasicPipeline(shaderModule, device, null, null, vertexInputDescriptor, depthStateDescriptor);
justin_fan@apple.comaeece1f2019-03-14 23:04:18 +000075 const commandEncoder = device.createCommandEncoder();
justin_fan@apple.com988c5772019-02-12 20:11:49 +000076
77 const basicAttachment = {
justin_fan@apple.comfe0a7112019-03-18 19:07:56 +000078 attachment: swapChain.getCurrentTexture().createDefaultView(),
justin_fan@apple.com988c5772019-02-12 20:11:49 +000079 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.comfe0a7112019-03-18 19:07:56 +000085 attachment: createBasicDepthTexture(canvas, device).createDefaultView(),
justin_fan@apple.com988c5772019-02-12 20:11:49 +000086 depthLoadOp: "clear",
87 depthStoreOp: "store",
88 clearDepth: 1.0
89 };
90
justin_fan@apple.comaeece1f2019-03-14 23:04:18 +000091 const encoder = commandEncoder.beginRenderPass({
justin_fan@apple.com988c5772019-02-12 20:11:49 +000092 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.comaeece1f2019-03-14 23:04:18 +0000101 device.getQueue().submit([commandEncoder.finish()]);
justin_fan@apple.com988c5772019-02-12 20:11:49 +0000102}
103
mmaxfield@apple.come7ba53b2019-06-26 19:35:26 +0000104getBasicDevice().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.com58fa1652019-06-13 03:44:24 +0000112}, function() {
mmaxfield@apple.come7ba53b2019-06-26 19:35:26 +0000113 drawGreenSquareInSoftware(canvas);
mmaxfield@apple.com58fa1652019-06-13 03:44:24 +0000114 if (window.testRunner)
115 testRunner.notifyDone();
116});
117</script>