| <!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 uint[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) { |
| buffer[0] = buffer.length; |
| } |
| `; |
| |
| async function start(device) { |
| 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 = Uint32Array.BYTES_PER_ELEMENT * 1337; |
| |
| const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC}; |
| const buffer = device.createBuffer(bufferDescriptor); |
| const bufferArrayBuffer = await buffer.mapWriteAsync(); |
| const bufferUint32Array = new Uint32Array(bufferArrayBuffer); |
| bufferUint32Array[0] = 0; |
| buffer.unmap(); |
| |
| const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ}; |
| const resultsBuffer = device.createBuffer(resultsBufferDescriptor); |
| |
| const bufferBinding = {buffer: resultsBuffer, size}; |
| const bindGroupBinding = {binding: 0, resource: bufferBinding}; |
| const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]}; |
| const bindGroup = device.createBindGroup(bindGroupDescriptor); |
| |
| const commandEncoder = device.createCommandEncoder(); // {} |
| commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, 1 * Uint32Array.BYTES_PER_ELEMENT); |
| 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 resultsBuffer.mapReadAsync(); |
| const resultUint32Array = new Uint32Array(resultsArrayBuffer); |
| if (resultUint32Array[0] == 1337) |
| testPassed(""); |
| else |
| testFailed(""); |
| resultsBuffer.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> |