| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="js/webgpu-functions.js"></script> |
| <script src="../resources/js-test-pre.js"></script> |
| </head> |
| <body> |
| <script> |
| const shaderSource = ` |
| [numthreads(1, 1, 1)] |
| compute void computeShader(device int[] buffer : register(u0)) { |
| buffer[0] = 1; |
| buffer[1] = buffer[0]++; |
| |
| buffer[2] = 1; |
| buffer[3] = ++buffer[2]; |
| |
| buffer[4] = 1; |
| buffer[4] += 5; |
| |
| buffer[5] = 1; |
| buffer[5] *= 7; |
| |
| int total = 14; |
| for (int i = 0; i < 10; i += 2) { |
| total += i; |
| } |
| buffer[6] = total; |
| |
| total = 14; |
| for (int i = 0; i < 10; ++i) { |
| total += i; |
| } |
| buffer[7] = total; |
| } |
| `; |
| |
| async function start() { |
| const adapter = await navigator.gpu.requestAdapter(); |
| const device = await adapter.requestDevice(); |
| |
| const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true}); |
| const computeStage = {module: shaderModule, entryPoint: "computeShader"}; |
| |
| const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]}; |
| const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor); |
| const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]}; |
| const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor); |
| |
| const computePipelineDescriptor = {computeStage, layout: pipelineLayout}; |
| const computePipeline = device.createComputePipeline(computePipelineDescriptor); |
| |
| const size = Int32Array.BYTES_PER_ELEMENT * 8; |
| |
| const bufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ}; |
| const buffer = device.createBuffer(bufferDescriptor); |
| |
| const bufferBinding = {buffer: buffer, size}; |
| const bindGroupBinding = {binding: 0, resource: bufferBinding}; |
| const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]}; |
| const bindGroup = device.createBindGroup(bindGroupDescriptor); |
| |
| const commandEncoder = device.createCommandEncoder(); // {} |
| const computePassEncoder = commandEncoder.beginComputePass(); |
| computePassEncoder.setPipeline(computePipeline); |
| computePassEncoder.setBindGroup(0, bindGroup); |
| computePassEncoder.dispatch(1, 1, 1); |
| computePassEncoder.endPass(); |
| const commandBuffer = commandEncoder.finish(); |
| device.getQueue().submit([commandBuffer]); |
| |
| const resultsArrayBuffer = await buffer.mapReadAsync(); |
| const results = new Int32Array(resultsArrayBuffer); |
| if (results[0] == 2 |
| && results[1] == 1 |
| && results[2] == 2 |
| && results[3] == 2 |
| && results[4] == 6 |
| && results[5] == 7 |
| && results[6] == 34 |
| && results[7] == 59) |
| testPassed(""); |
| else |
| testFailed(""); |
| buffer.unmap(); |
| } |
| window.jsTestIsAsync = true; |
| getBasicDevice().then(function(device) { |
| start(device).then(function() { |
| finishJSTest(); |
| }, function() { |
| testFailed(""); |
| finishJSTest(); |
| }); |
| }, function() { |
| testPassed(""); |
| finishJSTest(); |
| }); |
| </script> |
| <script src="../resources/js-test-post.js"></script> |
| </body> |
| </html> |