GPU
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
Giao diện GPU của WebGPU API là điểm khởi đầu để sử dụng WebGPU. Giao diện này có thể dùng để trả về một GPUAdapter, từ đó bạn có thể yêu cầu device, cấu hình các tính năng và giới hạn, cùng nhiều thứ khác.
Đối tượng GPU cho ngữ cảnh hiện tại được truy cập qua các thuộc tính Navigator.gpu hoặc WorkerNavigator.gpu.
Thuộc tính thể hiện
wgslLanguageFeaturesRead only-
Một đối tượng
WGSLLanguageFeaturesbáo cáo các tiện ích ngôn ngữ WGSL được triển khai WebGPU hỗ trợ.
Phương thức thể hiện
requestAdapter()-
Trả về một
Promisesẽ được hoàn tất với một thể hiện đối tượngGPUAdapter. Từ đó bạn có thể yêu cầu mộtGPUDevice, là giao diện chính để sử dụng chức năng WebGPU. getPreferredCanvasFormat()-
Trả về định dạng texture canvas tối ưu để hiển thị nội dung có độ sâu 8 bit, dải động tiêu chuẩn trên hệ thống hiện tại.
Ví dụ
>Yêu cầu adapter và device
async function init() {
if (!navigator.gpu) {
throw Error("WebGPU not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw Error("Couldn't request WebGPU adapter.");
}
const device = await adapter.requestDevice();
// …
}
Cấu hình GPUCanvasContext với định dạng texture tối ưu
const canvas = document.querySelector("#gpuCanvas");
const context = canvas.getContext("webgpu");
context.configure({
device,
format: navigator.gpu.getPreferredCanvasFormat(),
alphaMode: "premultiplied",
});
Đặc tả
| Specification |
|---|
| WebGPU> # gpu-interface> |