Created
January 15, 2026 20:01
-
-
Save greggman/585fe2f42cc909047ce232a2ddb6888c to your computer and use it in GitHub Desktop.
WebGPU: copy depth16unorm via copyT2B
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
| <canvas width="3" height="1" style="width: 300px; height: 100px; image-rendering: pixelated;"></canvas> |
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
| const adapter = await navigator.gpu.requestAdapter({ featureLevel: 'compatibility' }); | |
| const device = await adapter.requestDevice(); | |
| console.log([...device.features].join('\n')); | |
| device.addEventListener('uncapturederror', e => console.error(e.error.message)); | |
| const depthTex = device.createTexture({ | |
| size: [4], | |
| format: 'depth16unorm', | |
| usage: GPUTextureUsage.RENDER_ATTACHMENT | | |
| GPUTextureUsage.TEXTURE_BINDING | | |
| GPUTextureUsage.COPY_SRC | | |
| GPUTextureUsage.COPY_DST, | |
| }); | |
| device.queue.writeTexture( | |
| { texture: depthTex }, | |
| new Uint16Array([0x4000, 0x8000, 0xC000, 0x1234]), | |
| { }, | |
| [4] | |
| ); | |
| const buffer = device.createBuffer({ | |
| size: 8, | |
| usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ | |
| }); | |
| const encoder = device.createCommandEncoder(); | |
| encoder.copyTextureToBuffer( | |
| { texture: depthTex }, | |
| { buffer }, | |
| [4], | |
| ); | |
| device.queue.submit([encoder.finish()]); | |
| await buffer.mapAsync(GPUMapMode.READ); | |
| const data = new Uint16Array(buffer.getMappedRange()).slice(); | |
| buffer.unmap(); | |
| console.log([...data].map(v => v.toString(16).padStart(4, '0'))); | |
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: copy depth16unorm via copyT2B","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