Remove some support for < iOS 13
[WebKit-https.git] / LayoutTests / inspector / canvas / resources / shaderProgram-utilities-webgpu.js
1 if (window.internals)
2     window.internals.settings.setWebGPUEnabled(true);
3
4 if (window.testRunner) {
5     testRunner.dumpAsText();
6     testRunner.waitUntilDone();
7 }
8
9 const computeShaderSource = `
10 [numthreads(2, 1, 1)]
11 compute void computeShader(device float[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
12     buffer[uint(threadID.x)] = buffer[uint(threadID.x)] * 2.0;
13 }
14 `;
15
16 const vertexShaderSource = `
17 vertex float4 vertexShader(float4 position : attribute(0), float i : attribute(1)) : SV_Position {
18     return position;
19 }
20 `;
21
22 const fragmentShaderSource = `
23 fragment float4 fragmentShader(float4 position : SV_Position) : SV_Target 0 {
24     return position;
25 }
26 `;
27
28 let device = null;
29
30 async function createComputePipeline(computeCode = computeShaderSource) {
31     // Copied from webgpu/whlsl/compute.html.
32
33     const shaderModule = device.createShaderModule({code: computeCode});
34     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
35
36     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
37     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
38     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
39     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
40
41     device.createComputePipeline({computeStage, layout: pipelineLayout});
42 }
43
44 async function createRenderPipeline(vertexCode = vertexShaderSource, fragmentCode = fragmentShaderSource) {
45     // Copied from webgpu/whlsl/whlsl.html.
46
47     const vertexShaderModule = device.createShaderModule({code: vertexCode});
48     const vertexStage = {module: vertexShaderModule, entryPoint: "vertexShader"};
49
50     const fragmentShaderModule = device.createShaderModule({code: fragmentCode});
51     const fragmentStage = {module: fragmentShaderModule, entryPoint: "fragmentShader"};
52
53     const primitiveTopology = "triangle-strip";
54     const rasterizationState = {frontFace: "cw", cullMode: "none"};
55     const alphaBlend = {};
56     const colorBlend = {};
57     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWrite.ALL
58     const depthStencilState = null;
59     
60     const attribute0 = {shaderLocation: 0, format: "float4"};
61     const attribute1 = {shaderLocation: 1, format: "float"};
62     const input0 = {stride: 16, attributeSet: [attribute0]};
63     const input1 = {stride: 4, attributeSet: [attribute1]};
64     const inputs = [input0, input1];
65     const vertexInput = {vertexBuffers: inputs};
66
67     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
68     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
69     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
70     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
71
72     device.createRenderPipeline({vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout});
73 }
74
75 function deleteDevice() {
76     device = null;
77     // Force GC to make sure the device is destroyed, otherwise the frontend
78     // does not receive WI.CanvasManager.Event.CanvasRemoved events.
79     setTimeout(() => { GCController.collect(); }, 0);
80 }
81
82 async function load() {
83     let adapter = await navigator.gpu.requestAdapter();
84     device = await adapter.requestDevice();
85
86     await Promise.all([
87         createComputePipeline(),
88         createRenderPipeline(),
89     ]);
90
91     if (window.beforeTest)
92         await beforeTest();
93
94     runTest();
95 }
96
97 TestPage.registerInitializer(() => {
98     if (!InspectorTest.ShaderProgram)
99         InspectorTest.ShaderProgram = {};
100
101     InspectorTest.ShaderProgram.programForType = function(programType) {
102         for (let shaderProgram of WI.canvasManager.shaderPrograms) {
103             if (shaderProgram.programType === programType)
104                 return shaderProgram;
105         }
106         return null;
107     };
108 });