a6c0126fab527f7b683c61cae1db0df64bd171ed
[WebKit-https.git] / LayoutTests / webgpu / propertyresolver / ander.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)) : SV_Position {
10     return position;
11 }
12
13 struct Foo {
14     float x;
15 }
16
17 thread float* operator&.y(thread Foo* f) {
18     return &f->x;
19 }
20
21 Foo generateFoo() {
22     Foo f;
23     f.x = 1.0;
24     return f;
25 }
26
27 fragment float4 fragmentShader() : SV_Target 0 {
28     return float4(generateFoo().y, generateFoo().y, generateFoo().y, 1.0);
29 }
30 `;
31 async function start() {
32     const adapter = await navigator.gpu.requestAdapter();
33     const device = await adapter.requestDevice();
34
35     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
36     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
37     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
38     const primitiveTopology = "triangle-strip";
39     const rasterizationState = {frontFace: "cw", cullMode: "none"};
40     const alphaBlend = {};
41     const colorBlend = {};
42     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
43     const depthStencilState = null;
44     
45     const attribute0 = {shaderLocation: 0, format: "float4"};
46     const input0 = {stride: 16, attributeSet: [attribute0]};
47     const inputs = [input0];
48     const vertexInput = {vertexBuffers: inputs};
49
50     const pipelineLayoutDescriptor = {bindGroupLayouts: []};
51     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
52
53     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
54     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
55
56     const vertexBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
57     const vertexBuffer = device.createBuffer(vertexBufferDescriptor);
58     const vertexBufferArrayBuffer = await vertexBuffer.mapWriteAsync();
59     const vertexBufferFloat32Array = new Float32Array(vertexBufferArrayBuffer);
60     vertexBufferFloat32Array[0] = -0.5;
61     vertexBufferFloat32Array[1] = -0.5;
62     vertexBufferFloat32Array[2] = 1.0;
63     vertexBufferFloat32Array[3] = 1;
64     vertexBufferFloat32Array[4] = -0.5;
65     vertexBufferFloat32Array[5] = 0.5;
66     vertexBufferFloat32Array[6] = 1.0;
67     vertexBufferFloat32Array[7] = 1;
68     vertexBufferFloat32Array[8] = 0.5;
69     vertexBufferFloat32Array[9] = -0.5;
70     vertexBufferFloat32Array[10] = 1.0;
71     vertexBufferFloat32Array[11] = 1;
72     vertexBufferFloat32Array[12] = 0.5;
73     vertexBufferFloat32Array[13] = 0.5;
74     vertexBufferFloat32Array[14] = 1.0;
75     vertexBufferFloat32Array[15] = 1;
76     vertexBuffer.unmap();
77
78     const canvas = document.getElementById("canvas");
79     const context = canvas.getContext("gpu");
80     const swapChainDescriptor = {device, format: "bgra8unorm"};
81     const swapChain = context.configureSwapChain(swapChainDescriptor);
82     const outputTexture = swapChain.getCurrentTexture();
83     const outputTextureView = outputTexture.createDefaultView();
84
85     const commandEncoder = device.createCommandEncoder(); // {}
86     const red = {r: 0, g: 0, b: 1, a: 1};
87     const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}];
88     const depthStencilAttachment = null;
89     const renderPassDescriptor = {colorAttachments, depthStencilAttachment};
90     const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
91     renderPassEncoder.setPipeline(renderPipeline);
92     renderPassEncoder.setVertexBuffers(0, [vertexBuffer], [0]);
93     renderPassEncoder.draw(4, 1, 0, 0);
94     renderPassEncoder.endPass();
95     const commandBuffer = commandEncoder.finish();
96     device.getQueue().submit([commandBuffer]);
97 }
98 if (window.testRunner)
99     testRunner.waitUntilDone();
100 window.addEventListener("load", function() {
101     start().then(function() {
102         if (window.testRunner)
103             testRunner.notifyDone();
104     }, function() {
105         if (window.testRunner)
106             testRunner.notifyDone();
107     });
108 });
109 </script>
110 </body>
111 </html>