Tính năng mới trong WebGPU (Chrome 118)

François Beaufort
François Beaufort

HTMLImageElement và ImageData hỗ trợ trong copyExternalImageToTexture()

Phương thức copyExternalImageToTexture() trên GPUQueue cho phép bạn sao chép một ảnh chụp nhanh lấy từ hình ảnh, video hoặc canvas nguồn vào một GPUTexture nhất định. Giờ đây, bạn có thể truyền các đối tượng HTMLImageElementImageData làm nguồn. Hãy xem ví dụ sau và vấn đề 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);

Hỗ trợ thử nghiệm cho kết cấu bộ nhớ chỉ đọc và đọc-ghi

Loại liên kết kết cấu lưu trữ cho phép bạn thực hiện các thao tác đọc kết cấu mà không cần lấy mẫu và lưu trữ ở các vị trí tuỳ ý trong chương trình đổ bóng. Khi tính năng "chromium-experimental-read-write-storage-texture" có trong GPUAdapter, giờ đây, bạn có thể yêu cầu một GPUDevice có tính năng này và đặt quyền truy cập GPUStorageTexture thành "read-write" hoặc "read-only" khi tạo bố cục nhóm liên kết. Trước đây, việc này chỉ giới hạn ở "write-only".

Để tận dụng tính năng này, bạn phải bật tiện ích này một cách rõ ràng trong mã WGSL bằng enable chromium_experimental_read_write_storage_texture. Khi được bật, bạn có thể sử dụng trình đủ điều kiện truy cập read_writeread cho các hoạ tiết lưu trữ, các hàm tích hợp textureLoad()textureStore() sẽ hoạt động cho phù hợp và một hàm tích hợp textureBarrier() mới sẽ có sẵn để đồng bộ hoá các hoạt động truy cập bộ nhớ hoạ tiết trong một nhóm công việc. Hãy xem ví dụ sau và issue dawn:1972.

Tính năng này vẫn đang trong giai đoạn thử nghiệm và có thể thay đổi. Trong khi tính năng này đang được chuẩn hoá, hãy chạy Chrome bằng cờ --enable-dawn-features=allow_unsafe_apis để cung cấp tính năng này.

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.

Thông tin cập nhật về bình minh

API C webgpu.h đã đổi tên các trường sau để đảm bảo tính nhất quán: requiredFeaturesCount thành requiredFeatureCount, pipelineStatisticsCount thành pipelineStatisticCountcolorFormatsCount thành colorFormatCount. Xem vấn đề dawn:146040.

Một chương trình DawnInfo mới (tương tự như vulkaninfo) cho phép bạn liệt kê các nút bật/tắt, bộ chuyển đổi, tính năng bộ chuyển đổi và giới hạn bộ chuyển đổi. Có sẵn khi tạo samples bình minh. Sau đây là kết quả được cắt bớt đáng kể để ngắn gọn. Xem 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
[…]

Đây chỉ là một số điểm nổi bật chính. Xem danh sách đầy đủ các cam kết.

Tính năng mới trong WebGPU

Danh sách mọi nội dung đã được đề cập trong loạt bài Tính năng mới trong WebGPU.

Chrome 140

Chrome 139

Chrome 138

Chrome 137

Chrome 136

Chrome 135

Chrome 134

Chrome 133

Chrome 132

Chrome 131

Chrome 130

Chrome 129

Chrome 128

Chrome 127

Chrome 126

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