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