Published
Edited
May 3, 2022
Insert cell
# WebGPU iOS [WIP]
## There's undocumented divergences between W3C spec and Safari WebGPU API
Insert cell
//
Insert cell
adapter = navigator.gpu.requestAdapter()
Insert cell
device = adapter.requestDevice()
Insert cell
module = await device.createShaderModule({
code: `
@stage(compute) @workgroup_size(64)
fn main() {
// Pointless!
}
`
})
Insert cell
BUFFER_SIZE = 1000;
Insert cell
output = device.createBuffer({
size: BUFFER_SIZE,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
})
Insert cell
stagingBuffer = device.createBuffer({
size: BUFFER_SIZE,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
})
Insert cell
// https://cs.github.com/BabylonJS/Babylon.js/blob/2723e30c319c23b4cc6c28bbc4979bd3ce630f29/packages/dev/core/src/LibDeclarations/webgpu.d.ts#L510
bindGroupLayout = device.createBindGroupLayout({
label: "hmm",
bindings: [
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
// https://github.com/gpuweb/gpuweb/issues/446
type: "storage-buffer"
}
]
})
Insert cell
bindGroup = device.createBindGroup({
layout: bindGroupLayout,
bindings: [
{
binding: 1,
resource: { buffer: output }
}
]
})
Insert cell
pipeline = await device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [bindGroupLayout]
}),
compute: {
module,
entryPoint: "main"
}
})
Insert cell
commandEncoder = device.createCommandEncoder()
Insert cell
passEncoder = commandEncoder.beginComputePass()
Insert cell
{
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, bindGroup);
passEncoder.dispatch(Math.ceil(BUFFER_SIZE / 64));
passEncoder.end();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more