Created
November 26, 2025 09:22
-
-
Save greggman/f02ba797a75f2e95ae89b95e2c8022d7 to your computer and use it in GitHub Desktop.
WebGPU: Test IndirectDraw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*bug-in-github-api-content-can-not-be-empty*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*bug-in-github-api-content-can-not-be-empty*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {GPUDevice} device | |
| */ | |
| async function testDrawCommand(device, fn) { | |
| const dstTexture = device.createTexture({ | |
| format: 'rgba8uint', | |
| size: [1], | |
| usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC, | |
| }); | |
| const module = device.createShaderModule({ | |
| code: ` | |
| struct VOut { | |
| @builtin(position) pos: vec4f, | |
| @location(0) @interpolate(flat, either) params: vec4u, | |
| }; | |
| @vertex fn vs( | |
| @builtin(vertex_index) vNdx: u32, | |
| @builtin(instance_index) iNdx: u32) -> VOut | |
| { | |
| return VOut( | |
| vec4f(0, 0, 0, 1), | |
| vec4u(vNdx, iNdx, 0x33, 0x44)); | |
| } | |
| @fragment fn fs(v: VOut) -> @location(0) vec4u { | |
| return v.params; | |
| } | |
| `, | |
| }); | |
| const pipeline = device.createRenderPipeline({ | |
| layout: 'auto', | |
| vertex: { module }, | |
| fragment: { module, targets: [{ format: 'rgba8uint' }] }, | |
| primitive: { topology: 'point-list' }, | |
| }); | |
| const encoder = device.createCommandEncoder(); | |
| const pass = encoder.beginRenderPass({ | |
| colorAttachments: [ | |
| { | |
| view: dstTexture.createView(), | |
| loadOp: 'clear', | |
| storeOp: 'store', | |
| }, | |
| ], | |
| }); | |
| pass.setPipeline(pipeline); | |
| fn(pass); | |
| pass.end(); | |
| const result = device.createBuffer({ | |
| size: 4, | |
| usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, | |
| }); | |
| encoder.copyTextureToBuffer( | |
| { texture: dstTexture }, | |
| { buffer: result }, | |
| [1], | |
| ); | |
| device.queue.submit([encoder.finish()]); | |
| await result.mapAsync(GPUMapMode.READ); | |
| const data = new Uint8Array(result.getMappedRange()).slice(); | |
| result.unmap(); | |
| console.log([...data].map(v => v.toString(16).padStart(2, '0')).join(', ')); | |
| } | |
| async function main(){ | |
| const adapter = await navigator.gpu.requestAdapter(); | |
| const device = await adapter.requestDevice(); | |
| const indirect = new Uint32Array([ | |
| 10, // vertexCount | |
| 11, // instanceCount | |
| 0, // firstVertex | |
| 0x22, // firstInstance | |
| ]); | |
| const indirectBuffer = device.createBuffer({ | |
| size: indirect.byteLength, | |
| usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.INDIRECT, | |
| }); | |
| device.queue.writeBuffer(indirectBuffer, 0, indirect); | |
| await testDrawCommand(device, (pass) => { | |
| pass.draw(1, 1, 0x11, 0x22); | |
| }); | |
| await testDrawCommand(device, (pass) => { | |
| pass.drawIndirect(indirectBuffer, 0); | |
| }); | |
| } | |
| main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"name":"WebGPU: Test IndirectDraw","settings":{},"filenames":["index.html","index.css","index.js"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment