fd930c863c967f0df6e98b47ff524374bad3a0b3
[WebKit-https.git] / LayoutTests / webgpu / whlsl-ensure-proper-variable-lifetime-2.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 float assignToPtr(thread float* ptr, float value) {
15     return *ptr = value;
16 }
17
18 thread float* realPtr(float value)
19 {
20     return &value;
21 }
22
23 thread float* firstPtr(float value) {
24     return realPtr(value);
25 }
26
27 thread float* secondPtr(float value) {
28     return firstPtr(value);
29 }
30
31 vertex VertexOut vertexShader(float4 position : attribute(0), float shade : attribute(1)) {
32     VertexOut result;
33
34     thread float* ptrToShade = firstPtr(shade);
35     thread float* ptrToShade2 = secondPtr(1.0);
36
37     float dummy;
38     thread float* dummyPtr = &dummy;
39
40     dummy = assignToPtr(ptrToShade2, 0.0);
41     thread float* dummy2 = realPtr(shade);
42
43     result.position = position;
44
45     result.shade = *ptrToShade2;
46
47     return result;
48 }
49
50 fragment float4 fragmentShader(float shade : attribute(0)) : SV_Target 0 {
51     return float4(shade, shade, shade, 1.0);
52 }
53 `;
54 async function start() {
55     const adapter = await navigator.gpu.requestAdapter();
56     const device = await adapter.requestDevice();
57
58     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
59     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
60     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
61     const primitiveTopology = "triangle-strip";
62     const rasterizationState = {frontFace: "cw", cullMode: "none"};
63     const alphaBlend = {};
64     const colorBlend = {};
65     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
66     const depthStencilState = null;
67
68     const attribute0 = {shaderLocation: 0, format: "float4"};
69     const attribute1 = {shaderLocation: 1, format: "float", offset: 16};
70     const attributes = [attribute0, attribute1];
71     const input0 = {stride: 20, attributeSet: attributes};
72     const inputs = [input0];
73     const vertexInput = {vertexBuffers: inputs};
74
75     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
76     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
77     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
78     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
79
80     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
81     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
82
83     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 5 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
84     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
85     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
86     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
87     vertexBuffer0Float32Array[0] = -0.5;
88     vertexBuffer0Float32Array[1] = -0.5;
89     vertexBuffer0Float32Array[2] = 1.0;
90     vertexBuffer0Float32Array[3] = 1.0;
91     vertexBuffer0Float32Array[4] = 1.0;
92
93     vertexBuffer0Float32Array[5] = -0.5;
94     vertexBuffer0Float32Array[6] = 0.5;
95     vertexBuffer0Float32Array[7] = 1.0;
96     vertexBuffer0Float32Array[8] = 1.0;
97     vertexBuffer0Float32Array[9] = 1.0;
98
99     vertexBuffer0Float32Array[10] = 0.5;
100     vertexBuffer0Float32Array[11] = -0.5;
101     vertexBuffer0Float32Array[12] = 1.0;
102     vertexBuffer0Float32Array[13] = 1.0;
103     vertexBuffer0Float32Array[14] = 1.0;
104
105     vertexBuffer0Float32Array[15] = 0.5;
106     vertexBuffer0Float32Array[16] = 0.5;
107     vertexBuffer0Float32Array[17] = 1.0;
108     vertexBuffer0Float32Array[18] = 1.0;
109     vertexBuffer0Float32Array[19] = 1.0;
110     vertexBuffer0.unmap();
111
112     const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
113     const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
114     const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
115     const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
116     resourceBufferFloat32Array[0] = 1;
117     resourceBuffer.unmap();
118
119     const bufferBinding = {buffer: resourceBuffer, size: 4};
120     const bindGroupBinding = {binding: 0, resource: bufferBinding};
121     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
122     const bindGroup = device.createBindGroup(bindGroupDescriptor);
123
124     const canvas = document.getElementById("canvas");
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 window.addEventListener("load", function() {
148     start().then(function() {
149         if (window.testRunner)
150             testRunner.notifyDone();
151     }, function() {
152         if (window.testRunner)
153             testRunner.notifyDone();
154     });
155 });
156 </script>
157 </body>
158 </html>