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