支持 WebGPU API 的環境,可不止瀏覽器一個,
雖說 NodeJS 沒什么訊息說要支持,但是 Deno 這個 js/ts 運行時老早就支持了,能脫離瀏覽器直接在控制臺訪問 GPU,感覺十分有趣,
確保 Deno 版本
至少要高于 1.8,最好裝最新
> deno --version
deno 1.18.0 (release, x86_64-pc-windows-msvc)
v8 9.8.177.6
typescript 4.5.2
命令列互動式獲取 GPUDevice
發文時,WebGPU 尚未正式公布,仍需要加上不安全標記來運行命令列互動式環境:

隨后就可以輕松地獲取 GPUDevice 了,只需兩行代碼(假如不檢查錯誤情況):
const adapter = await navigator.gpu.requestAdapter()
const device = await adapter.requestDevice()
代碼檔案獲取 GPUDevice
原始碼非常簡單
navigator.gpu.requestAdapter().then((adapter) => {
if (adapter === null) {
console.log('GPUAdapter 請求失敗')
Deno.close(0)
} else {
return adapter.requestDevice()
}
}).then((device) => {
if (!device) {
console.log('GPUDevice 請求失敗')
Deno.close(0)
} else {
console.log(device.limits)
}
})
然后運行之:

Top-level Await 不生效
對于頂級 await 獲取的方式貌似不可行,不知道是不是 deno 的問題,
// gpu.ts
const adapter = await navigator.gpu.requestAdapter()
const device = await adapter.requestDevice()
console.log(device.limits)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418105.html
標籤:其他
上一篇:DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ | NEURAL NETWORKS
