e5d26b378e6ff035052ac13e471e561d5c81d2c5
[WebKit-https.git] / LayoutTests / webgpu / whlsl / separate-shader-modules / separate-shader-modules-2.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 vertexShaderSource = `
10 vertex float4 vertexShader(float4 position : attribute(0)) : SV_Position {
11     return position;
12 }`;
13
14 const fragmentShaderSource = `
15 float4 helper() {
16     return float4(1, 1, 1, 1);
17 }
18 fragment float4 fragmentShader(float4 position : SV_Position) : SV_Target 0 {
19     return helper();
20 }
21 `;
22
23 const canvas = document.getElementById("canvas");
24
25 async function start(device) {
26     const vertexShaderModule = device.createShaderModule({code: vertexShaderSource, isWHLSL: true});
27     const fragmentShaderModule = device.createShaderModule({code: fragmentShaderSource, isWHLSL: true});
28     const vertexStage = {module: vertexShaderModule, entryPoint: "vertexShader"};
29     const fragmentStage = {module: fragmentShaderModule, entryPoint: "fragmentShader"};
30     const primitiveTopology = "triangle-strip";
31     const rasterizationState = {frontFace: "cw", cullMode: "none"};
32     const alphaBlend = {};
33     const colorBlend = {};
34     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
35     const depthStencilState = null;
36     
37     const attribute = {shaderLocation: 0, format: "float4"};
38     const input = {stride: 16, attributeSet: [attribute]};
39     const inputs = [input];
40     const vertexInput = {vertexBuffers: inputs};
41
42     const pipelineLayoutDescriptor = {bindGroupLayouts: []};
43     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
44
45     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
46     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
47
48     const vertexBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
49     const vertexBuffer = device.createBuffer(vertexBufferDescriptor);
50     const vertexBufferArrayBuffer = await vertexBuffer.mapWriteAsync();
51     const vertexBufferFloat32Array = new Float32Array(vertexBufferArrayBuffer);
52     vertexBufferFloat32Array[0] = -0.5;
53     vertexBufferFloat32Array[1] = -0.5;
54     vertexBufferFloat32Array[2] = 1.0;
55     vertexBufferFloat32Array[3] = 1;
56     vertexBufferFloat32Array[4] = -0.5;
57     vertexBufferFloat32Array[5] = 0.5;
58     vertexBufferFloat32Array[6] = 1.0;
59     vertexBufferFloat32Array[7] = 1;
60     vertexBufferFloat32Array[8] = 0.5;
61     vertexBufferFloat32Array[9] = -0.5;
62     vertexBufferFloat32Array[10] = 1.0;
63     vertexBufferFloat32Array[11] = 1;
64     vertexBufferFloat32Array[12] = 0.5;
65     vertexBufferFloat32Array[13] = 0.5;
66     vertexBufferFloat32Array[14] = 1.0;
67     vertexBufferFloat32Array[15] = 1;
68     vertexBuffer.unmap();
69
70     const context = canvas.getContext("gpu");
71     const swapChainDescriptor = {device, format: "bgra8unorm"};
72     const swapChain = context.configureSwapChain(swapChainDescriptor);
73     const outputTexture = swapChain.getCurrentTexture();
74     const outputTextureView = outputTexture.createDefaultView();
75
76     const commandEncoder = device.createCommandEncoder(); // {}
77     const red = {r: 0, g: 0, b: 1, a: 1};
78     const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}];
79     const depthStencilAttachment = null;
80     const renderPassDescriptor = {colorAttachments, depthStencilAttachment};
81     const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
82     renderPassEncoder.setPipeline(renderPipeline);
83     renderPassEncoder.setVertexBuffers(0, [vertexBuffer], [0]);
84     renderPassEncoder.draw(4, 1, 0, 0);
85     renderPassEncoder.endPass();
86     const commandBuffer = commandEncoder.finish();
87     device.getQueue().submit([commandBuffer]);
88 }
89 if (window.testRunner)
90     testRunner.waitUntilDone();
91 getBasicDevice().then(function(device) {
92     start(device).then(function() {
93         if (window.testRunner)
94             testRunner.notifyDone();
95     }, function() {
96         if (window.testRunner)
97             testRunner.notifyDone();
98     });
99 }, function() {
100     drawWhiteSquareOnBlueBackgroundInSoftware(canvas);
101     if (window.testRunner)
102         testRunner.notifyDone();
103 });
104 </script>
105 </body>
106 </html>