| <!DOCTYPE html> |
| <meta charset="utf-8"> |
| <title>WebGPU Hello Triangles</title> |
| <meta name="assert" content="WebGPU correctly renders a green canvas."> |
| <link rel="match" href="blend-color-triangle-strip-expected.html"> |
| <p>Pass if square canvas below is completely green.</p> |
| <canvas width="400" height="400"></canvas> |
| <script src="js/webgpu-functions.js"></script> |
| <script> |
| if (window.testRunner) |
| testRunner.waitUntilDone(); |
| |
| const shaderCode = ` |
| #include <metal_stdlib> |
| |
| using namespace metal; |
| |
| struct Vertex |
| { |
| float4 position [[position]]; |
| }; |
| |
| vertex Vertex vertex_main(uint vid [[vertex_id]]) |
| { |
| Vertex v; |
| switch (vid) { |
| case 0: |
| v.position = float4(-1, 1, 0, 1); |
| break; |
| case 1: |
| v.position = float4(-1, -1, 0, 1); |
| break; |
| case 2: |
| v.position = float4(1, 1, 0, 1); |
| break; |
| default: |
| v.position = float4(1, -1, 0, 1); |
| } |
| return v; |
| } |
| |
| fragment float4 fragment_main() |
| { |
| return float4(0, 1, 1, 1); |
| } |
| `; |
| |
| async function test() { |
| const device = await getBasicDevice(); |
| const canvas = document.querySelector("canvas"); |
| const swapChain = createBasicSwapChain(canvas, device); |
| // FIXME: Replace with non-MSL shaders. |
| const shaderModule = device.createShaderModule({ code: shaderCode }); |
| |
| const colorStates = [{ |
| format: "bgra8unorm", |
| alphaBlend: { |
| srcFactor: "blend-color", |
| dstFactor: "blend-color", |
| operation: "add" |
| }, |
| colorBlend: { |
| srcFactor: "blend-color", |
| dstFactor: "blend-color", |
| operation: "add" |
| }, |
| writeMask: GPUColorWriteBits.ALL |
| }]; |
| |
| const pipeline = createBasicPipeline(shaderModule, device, colorStates); |
| const commandEncoder = device.createCommandEncoder(); |
| const passEncoder = beginBasicRenderPass(swapChain, commandEncoder); |
| passEncoder.setBlendColor({ r: 0, g: 1, b: 0, a: 1 }); |
| encodeBasicCommands(passEncoder, pipeline); |
| const queue = device.getQueue(); |
| |
| queue.submit([commandEncoder.finish()]); |
| |
| requestAnimationFrame(() => { |
| if (window.testRunner) |
| testRunner.notifyDone(); |
| }); |
| } |
| |
| test(); |
| </script> |