676bd062a265ef42c87e314750074e2c88d1c76d
[WebKit-https.git] / LayoutTests / webgpu / whlsl.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 </head>
5 <body>
6 <canvas id="canvas" width="400" height="400"></canvas>
7 <script>
8 const shaderSource = `
9 vertex float4 vertexShader(float4 position : attribute(0), float i : attribute(1)) : SV_Position {
10     return position;
11 }
12
13 fragment float4 fragmentShader(float4 position : SV_Position) : SV_Target 0 {
14     return position;
15 }
16 `;
17 async function start() {
18     const adapter = await navigator.gpu.requestAdapter();
19     const device = await adapter.requestDevice();
20
21     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
22     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
23     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
24     const primitiveTopology = "triangle-strip";
25     const rasterizationState = {frontFace: "cw", cullMode: "none"};
26     const alphaBlend = {};
27     const colorBlend = {};
28     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
29     const depthStencilState = null;
30     
31     const attribute0 = {shaderLocation: 0, format: "float4"};
32     const attribute1 = {shaderLocation: 1, format: "float"};
33     const input0 = {stride: 16, attributeSet: [attribute0]};
34     const input1 = {stride: 4, attributeSet: [attribute1]};
35     const inputs = [input0, input1];
36     const vertexInput = {vertexBuffers: inputs};
37
38     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
39     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
40     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
41     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
42
43     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
44     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
45
46     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
47     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
48     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
49     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
50     vertexBuffer0Float32Array[0] = -0.5;
51     vertexBuffer0Float32Array[1] = -0.5;
52     vertexBuffer0Float32Array[2] = 1.0;
53     vertexBuffer0Float32Array[3] = 1;
54     vertexBuffer0Float32Array[4] = -0.5;
55     vertexBuffer0Float32Array[5] = 0.5;
56     vertexBuffer0Float32Array[6] = 1.0;
57     vertexBuffer0Float32Array[7] = 1;
58     vertexBuffer0Float32Array[8] = 0.5;
59     vertexBuffer0Float32Array[9] = -0.5;
60     vertexBuffer0Float32Array[10] = 1.0;
61     vertexBuffer0Float32Array[11] = 1;
62     vertexBuffer0Float32Array[12] = 0.5;
63     vertexBuffer0Float32Array[13] = 0.5;
64     vertexBuffer0Float32Array[14] = 1.0;
65     vertexBuffer0Float32Array[15] = 1;
66     vertexBuffer0.unmap();
67
68     const vertexBuffer1Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
69     const vertexBuffer1 = device.createBuffer(vertexBuffer1Descriptor);
70     const vertexBuffer1ArrayBuffer = await vertexBuffer1.mapWriteAsync();
71     const vertexBuffer1Float32Array = new Float32Array(vertexBuffer1ArrayBuffer);
72     vertexBuffer1Float32Array[0] = 1;
73     vertexBuffer1Float32Array[1] = 1;
74     vertexBuffer1Float32Array[2] = 1;
75     vertexBuffer1Float32Array[3] = 1;
76     vertexBuffer1.unmap();
77
78     const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
79     const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
80     const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
81     const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
82     resourceBufferFloat32Array[0] = 1;
83     resourceBuffer.unmap();
84
85     const bufferBinding = {buffer: resourceBuffer, size: 4};
86     const bindGroupBinding = {binding: 0, resource: bufferBinding};
87     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
88     const bindGroup = device.createBindGroup(bindGroupDescriptor);
89
90     const canvas = document.getElementById("canvas");
91     const context = canvas.getContext("gpu");
92     const swapChainDescriptor = {device, format: "bgra8unorm"};
93     const swapChain = context.configureSwapChain(swapChainDescriptor);
94     const outputTexture = swapChain.getCurrentTexture();
95     const outputTextureView = outputTexture.createDefaultView();
96
97     const commandEncoder = device.createCommandEncoder(); // {}
98     const red = {r: 0, g: 0, b: 1, a: 1};
99     const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}];
100     const depthStencilAttachment = null;
101     const renderPassDescriptor = {colorAttachments, depthStencilAttachment};
102     const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
103     renderPassEncoder.setPipeline(renderPipeline);
104     renderPassEncoder.setBindGroup(0, bindGroup);
105     renderPassEncoder.setVertexBuffers(0, [vertexBuffer0, vertexBuffer1], [0, 0]);
106     renderPassEncoder.draw(4, 1, 0, 0);
107     renderPassEncoder.endPass();
108     const commandBuffer = commandEncoder.finish();
109     device.getQueue().submit([commandBuffer]);
110 }
111 if (window.testRunner)
112     testRunner.waitUntilDone();
113 window.addEventListener("load", function() {
114     start().then(function() {
115         if (window.testRunner)
116             testRunner.notifyDone();
117     }, function() {
118         if (window.testRunner)
119             testRunner.notifyDone();
120     });
121 });
122 </script>
123 </body>
124 </html>