Add a runtime-disabled dialog element skeleton
[WebKit-https.git] / LayoutTests / webgpu / whlsl / whlsl.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../js/webgpu-functions.js"></script>
5 </head>
6 <body>
7 <canvas id="canvas" width="400" height="400"></canvas>
8 <script>
9 const shaderSource = `
10 vertex float4 vertexShader(float4 position : attribute(0), float i : attribute(1)) : SV_Position {
11     return position;
12 }
13
14 fragment float4 fragmentShader(float4 position : SV_Position) : SV_Target 0 {
15     return position;
16 }
17 `;
18
19 const canvas = document.getElementById("canvas");
20
21 async function start(device) {
22     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
23     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
24     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
25     const primitiveTopology = "triangle-strip";
26     const rasterizationState = {frontFace: "cw", cullMode: "none"};
27     const alphaBlend = {};
28     const colorBlend = {};
29     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
30     const depthStencilState = null;
31     
32     const attribute0 = {shaderLocation: 0, format: "float4"};
33     const attribute1 = {shaderLocation: 1, format: "float"};
34     const input0 = {stride: 16, attributeSet: [attribute0]};
35     const input1 = {stride: 4, attributeSet: [attribute1]};
36     const inputs = [input0, input1];
37     const vertexInput = {vertexBuffers: inputs};
38
39     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
40     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
41     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
42     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
43
44     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
45     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
46
47     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
48     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
49     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
50     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
51     vertexBuffer0Float32Array[0] = -0.5;
52     vertexBuffer0Float32Array[1] = -0.5;
53     vertexBuffer0Float32Array[2] = 1.0;
54     vertexBuffer0Float32Array[3] = 1;
55     vertexBuffer0Float32Array[4] = -0.5;
56     vertexBuffer0Float32Array[5] = 0.5;
57     vertexBuffer0Float32Array[6] = 1.0;
58     vertexBuffer0Float32Array[7] = 1;
59     vertexBuffer0Float32Array[8] = 0.5;
60     vertexBuffer0Float32Array[9] = -0.5;
61     vertexBuffer0Float32Array[10] = 1.0;
62     vertexBuffer0Float32Array[11] = 1;
63     vertexBuffer0Float32Array[12] = 0.5;
64     vertexBuffer0Float32Array[13] = 0.5;
65     vertexBuffer0Float32Array[14] = 1.0;
66     vertexBuffer0Float32Array[15] = 1;
67     vertexBuffer0.unmap();
68
69     const vertexBuffer1Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
70     const vertexBuffer1 = device.createBuffer(vertexBuffer1Descriptor);
71     const vertexBuffer1ArrayBuffer = await vertexBuffer1.mapWriteAsync();
72     const vertexBuffer1Float32Array = new Float32Array(vertexBuffer1ArrayBuffer);
73     vertexBuffer1Float32Array[0] = 1;
74     vertexBuffer1Float32Array[1] = 1;
75     vertexBuffer1Float32Array[2] = 1;
76     vertexBuffer1Float32Array[3] = 1;
77     vertexBuffer1.unmap();
78
79     const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
80     const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
81     const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
82     const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
83     resourceBufferFloat32Array[0] = 1;
84     resourceBuffer.unmap();
85
86     const bufferBinding = {buffer: resourceBuffer, size: 4};
87     const bindGroupBinding = {binding: 0, resource: bufferBinding};
88     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
89     const bindGroup = device.createBindGroup(bindGroupDescriptor);
90
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 getBasicDevice().then(function(device) {
114     start(device).then(function() {
115         if (window.testRunner)
116             testRunner.notifyDone();
117     }, function() {
118         if (window.testRunner)
119             testRunner.notifyDone();
120     });
121 }, function() {
122     drawWhiteSquareOnBlueBackgroundInSoftware(canvas);
123     if (window.testRunner)
124         testRunner.notifyDone();
125 });
126 </script>
127 </body>
128 </html>