Add a runtime-disabled dialog element skeleton
[WebKit-https.git] / LayoutTests / webgpu / whlsl-textures-load.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 vertex float4 vertexShader(float4 position : attribute(0)) : SV_Position {
11     return position;
12 }
13
14 fragment float4 fragmentShader(Texture2D<float4> theTexture : register(t0)) : SV_Target 0 {
15     return Load(theTexture, int2(0, 0));
16 }
17 `;
18
19 const canvas = document.getElementById("canvas");
20
21 async function start(device) {
22     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
23     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
24     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
25     const primitiveTopology = "triangle-strip";
26     const rasterizationState = {frontFace: "cw", cullMode: "none"};
27     const alphaBlend = {};
28     const colorBlend = {};
29     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
30     const depthStencilState = null;
31     
32     const attribute0 = {shaderLocation: 0, format: "float4"};
33     const input0 = {stride: 16, attributeSet: [attribute0]};
34     const inputs = [input0];
35     const vertexInput = {vertexBuffers: inputs};
36
37     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "sampled-texture"}]};
38     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
39     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
40     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
41
42     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
43     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
44
45     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
46     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
47     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
48     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
49     vertexBuffer0Float32Array[0] = -0.5;
50     vertexBuffer0Float32Array[1] = -0.5;
51     vertexBuffer0Float32Array[2] = 1.0;
52     vertexBuffer0Float32Array[3] = 1;
53     vertexBuffer0Float32Array[4] = -0.5;
54     vertexBuffer0Float32Array[5] = 0.5;
55     vertexBuffer0Float32Array[6] = 1.0;
56     vertexBuffer0Float32Array[7] = 1;
57     vertexBuffer0Float32Array[8] = 0.5;
58     vertexBuffer0Float32Array[9] = -0.5;
59     vertexBuffer0Float32Array[10] = 1.0;
60     vertexBuffer0Float32Array[11] = 1;
61     vertexBuffer0Float32Array[12] = 0.5;
62     vertexBuffer0Float32Array[13] = 0.5;
63     vertexBuffer0Float32Array[14] = 1.0;
64     vertexBuffer0Float32Array[15] = 1;
65     vertexBuffer0.unmap();
66
67     const textureDataBufferDescriptor = {size: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2, usage: GPUBufferUsage.TRANSFER_SRC | GPUBufferUsage.MAP_WRITE};
68     const textureDataBuffer = device.createBuffer(textureDataBufferDescriptor);
69     const textureDataBufferArrayBuffer = await textureDataBuffer.mapWriteAsync();
70     const textureDataBufferUint8Array = new Uint8Array(textureDataBufferArrayBuffer);
71     textureDataBufferUint8Array[0] = 255;
72     textureDataBufferUint8Array[1] = 255;
73     textureDataBufferUint8Array[2] = 255;
74     textureDataBufferUint8Array[3] = 255;
75     textureDataBufferUint8Array[4] = 128;
76     textureDataBufferUint8Array[5] = 128;
77     textureDataBufferUint8Array[6] = 128;
78     textureDataBufferUint8Array[7] = 255;
79     textureDataBufferUint8Array[8] = 128;
80     textureDataBufferUint8Array[9] = 128;
81     textureDataBufferUint8Array[10] = 128;
82     textureDataBufferUint8Array[11] = 255;
83     textureDataBufferUint8Array[12] = 128;
84     textureDataBufferUint8Array[13] = 128;
85     textureDataBufferUint8Array[14] = 128;
86     textureDataBufferUint8Array[15] = 255;
87     textureDataBuffer.unmap();
88
89     const textureDescriptor = {size: {width: 2, height: 2, depth: 1}, format: "rgba8unorm", usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.TRANSFER_DST};
90     const texture = device.createTexture(textureDescriptor);
91     const textureView = texture.createDefaultView();
92
93     const bindGroupBindings = [{binding: 0, resource: textureView}];
94     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: bindGroupBindings};
95     const bindGroup = device.createBindGroup(bindGroupDescriptor);
96
97     const context = canvas.getContext("gpu");
98     const swapChainDescriptor = {device, format: "bgra8unorm"};
99     const swapChain = context.configureSwapChain(swapChainDescriptor);
100     const outputTexture = swapChain.getCurrentTexture();
101     const outputTextureView = outputTexture.createDefaultView();
102
103     const commandEncoder = device.createCommandEncoder(); // {}
104
105     const bufferCopyView = {buffer: textureDataBuffer, rowPitch: Uint8Array.BYTES_PER_ELEMENT * 4 * 2, imageHeight: Uint8Array.BYTES_PER_ELEMENT * 4 * 2 * 2};
106     const textureCopyView = {texture: texture};
107     const copySize = {width: 2, height: 2, depth: 1};
108     commandEncoder.copyBufferToTexture(bufferCopyView, textureCopyView, copySize);
109
110     const clearColor = {r: 0, g: 0, b: 1, a: 1};
111     const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor}];
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.notifyDone();
125 }
126 if (window.testRunner)
127     testRunner.waitUntilDone();
128 getBasicDevice().then(function(device) {
129     start(device).then(function() {
130         if (window.testRunner)
131             testRunner.notifyDone();
132     }, function() {
133         if (window.testRunner)
134             testRunner.notifyDone();
135     });
136 }, function() {
137     drawWhiteSquareOnBlueBackgroundInSoftware(canvas);
138     if (window.testRunner)
139         testRunner.notifyDone();
140 });
141 </script>
142 </body>
143 </html>