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