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