WebGPU 的新变化 (Chrome 118)

François Beaufort
François Beaufort

在 copyExternalImageToTexture() 中支持 HTMLImageElement 和 ImageData

借助 GPUQueue 上的 copyExternalImageToTexture() 方法,您可以将从源图片、视频或画布截取的快照复制到给定 GPUTexture。您现在可以传递 HTMLImageElementImageData 对象作为来源。请参阅以下示例并问题 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);

对读写和只读存储纹理的实验性支持

借助存储纹理绑定类型,您可以在不采样的情况下执行纹理读取,并将其存储到着色器中的任意位置。当 GPUAdapter 中提供 "chromium-experimental-read-write-storage-texture" 功能时,您现在可以使用该功能请求 GPUDevice,并在创建绑定组布局时设置 GPUStorageTexture"read-write""read-only" 的访问权限。以前,只有 "write-only" 可以执行此操作。

为充分利用此功能,您必须使用 enable chromium_experimental_read_write_storage_texture 在 WGSL 代码中明确启用此扩展程序。启用后,您可以针对存储纹理使用 read_writeread 访问限定符,textureLoad()textureStore() 内置函数会相应地执行操作,并且新的 textureBarrier() 内置函数可用于同步工作组中的纹理内存访问。如需查看示例,请参阅问题 dawn:1972

此功能仍处于实验阶段,可能会发生变化。在标准化的过程中,运行 Chrome 并添加 --enable-dawn-features=allow_unsafe_apis 标志以使其可用。

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.

黎明动态

为保持一致,webgpu.h C API 已将以下字段重命名为 requiredFeatureCount,将 pipelineStatisticsCount 重命名为 pipelineStatisticCount,并将 colorFormatsCount 重命名为 colorFormatCountrequiredFeaturesCount请参阅问题 dawn:146040

新的 DawnInfo 程序(类似于 vulkaninfo)允许您列出切换开关、适配器、适配器功能和适配器限制。它在构建黎明 samples 时可用。为简洁起见,以下输出已经过大幅修剪。请参阅更改 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
[…]

这仅涵盖了部分重要的亮点。查看详尽的提交内容列表

WebGPU 的新变化

WebGPU 的新变化系列中涵盖的所有内容的列表。

Chrome 125

Chrome 124

Chrome 123

Chrome 122

Chrome 121

Chrome 120

Chrome 119

Chrome 118

Chrome 117

Chrome 116

Chrome 115

Chrome 114

Chrome 113