Skip to content

Instantly share code, notes, and snippets.

@greggman
Created January 15, 2026 20:01
Show Gist options
  • Select an option

  • Save greggman/585fe2f42cc909047ce232a2ddb6888c to your computer and use it in GitHub Desktop.

Select an option

Save greggman/585fe2f42cc909047ce232a2ddb6888c to your computer and use it in GitHub Desktop.
WebGPU: copy depth16unorm via copyT2B
/*bug-in-github-api-content-can-not-be-empty*/
<canvas width="3" height="1" style="width: 300px; height: 100px; image-rendering: pixelated;"></canvas>
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')));
{"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