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