What's New in WebGPU (Chrome 118)

François Beaufort
François Beaufort

HTMLImageElement and ImageData support in copyExternalImageToTexture()

The copyExternalImageToTexture() method on GPUQueue allows you to copy a snapshot taken from a source image, video, or canvas into a given GPUTexture. You can now pass HTMLImageElement and ImageData objects as the source. See the following example and issue chromium:1471372.

// Fetch and decode image.
const source = document.createElement("img");
source.src = "my-image.png";
await source.decode();

// Create destination texture.
const size = [source.width, source.height];
const texture = myDevice.createTexture({
 size,
 format: "rgba8unorm",
 usage:
   GPUTextureUsage.COPY_DST |
   GPUTextureUsage.RENDER_ATTACHMENT |
   GPUTextureUsage.TEXTURE_BINDING,
});

// Copies a snapshot taken from the source image into a texture.
myDevice.queue.copyExternalImageToTexture({ source }, { texture }, size);

Experimental support for read-write and read-only storage texture

The storage texture binding type allows you to perform texture reads without sampling and store to arbitrary positions in shaders. When the "chromium-experimental-read-write-storage-texture" feature is available in a GPUAdapter, you can now request a GPUDevice with this feature and set GPUStorageTexture access to either "read-write" or "read-only" when creating a bind group layout. Previously this was restricted to "write-only".

To take advantage of this, you must explicitly enable this extension in your WGSL code with enable chromium_experimental_read_write_storage_texture. When enabled, you can use read_write and read access qualifier for storage textures, the textureLoad() and textureStore() built-in functions behave accordingly, and a new textureBarrier() built-in function is available to synchronize texture memory accesses in a workgroup. See the following example and issue dawn:1972.

This feature is still experimental and may change. While it’s getting standardized, run chrome with the --enable-dawn-features=allow_unsafe_apis flag to make it available.

const feature = "chromium-experimental-read-write-storage-texture";
const adapter = await navigator.gpu.requestAdapter();
if (!adapter.features.has(feature)) {
  throw new Error("Read-write storage texture support is not available");
}
// Explicitly request read-write storage texture support.
const device = await adapter.requestDevice({
  requiredFeatures: [feature],
});

const bindGroupLayout = device.createBindGroupLayout({
  entries: [{
    binding: 0,
    visibility: GPUShaderStage.COMPUTE,
    storageTexture: {
      access: "read-write", // <-- New!
      format: "r32uint",
    },
  }],
});

const shaderModule = device.createShaderModule({ code: `
  enable chromium_experimental_read_write_storage_texture;
  @group(0) @binding(0) var tex : texture_storage_2d<r32uint, read_write>;

  @compute @workgroup_size(1, 1)
  fn main(@builtin(local_invocation_id) local_id: vec3u) {
    var data = textureLoad(tex, vec2i(local_id.xy));
    data.x *= 2;
    textureStore(tex, vec2i(local_id.xy), data);
  }`,
});

// You can now create a compute pipeline with this shader module and
// send the appropriate commands to the GPU.

Dawn updates

The webgpu.h C API has renamed the following fields for consistency: requiredFeaturesCount to requiredFeatureCount, pipelineStatisticsCount to pipelineStatisticCount, and colorFormatsCount to colorFormatCount. See issue dawn:146040.

A new DawnInfo program (similar to vulkaninfo) allows you to list toggles, adapters, adapter features and adapter limits. It is available when building dawn samples. Here’s the output below heavily trimmed for brevity. See change dawn:149020.

./out/Debug/DawnInfo
Toggles
=======
  Name: allow_unsafe_apis
    Suppresses validation errors on API entry points or parameter combinations
    that aren't considered secure yet.
    http://crbug.com/1138528
[…]

Adapter
=======
VendorID: 0x106B
Vendor: apple
Architecture: common-3
DeviceID: 0x0000
Name: Apple M1 Pro
Driver description: Metal driver on macOS Version 13.5.1 (Build 22G90)
Adapter Type: discrete GPU
Backend Type: Metal
Power: <undefined>

  Features
  ========
   * depth_clip_control
      Disable depth clipping of primitives to the clip volume
      https://bugs.chromium.org/p/dawn/issues/detail?id=1178
[…]

  Adapter Limits
  ==============
    maxTextureDimension1D: 16,384
    maxTextureDimension2D: 16,384
[…]

This covers only some of the key highlights. Check out the exhaustive list of commits.

What's New in WebGPU

A list of everything that has been covered in the What's New in WebGPU series.

Chrome 122

Chrome 121

Chrome 120

Chrome 119

Chrome 118

Chrome 117

Chrome 116

Chrome 115

Chrome 114

Chrome 113