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