GPUComputePassEncoder

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 Web Workers 中可用。

GPUComputePassEncoder 介面是 WebGPU API 的一部分,用於編碼與控制計算著色器階段相關的命令,這些命令由 GPUComputePipeline 發出。它構成了 GPUCommandEncoder 的整體編碼活動的一部分。

計算管道包含一個單一的計算階段,在該階段,計算著色器接收通用資料,跨指定數量的工作組並行處理這些資料,然後將結果返回到一個或多個緩衝區中。

GPUComputePassEncoder 物件例項透過 GPUCommandEncoder.beginComputePass() 屬性建立。

例項屬性

label

一個字串,提供可用於識別物件的標籤,例如在 GPUError 訊息或控制檯警告中。

例項方法

dispatchWorkgroups()

分派特定網格的工作組來執行當前 GPUComputePipeline 所執行的工作。

dispatchWorkgroupsIndirect()

分派由 GPUBuffer 引數定義的工作組網格,以執行當前 GPUComputePipeline 所執行的工作。

end()

完成當前計算通道命令序列的錄製。

insertDebugMarker()

使用標籤在一系列已編碼命令中的特定點進行標記。

popDebugGroup()

結束一個除錯組,該除錯組由 pushDebugGroup() 呼叫開始。

pushDebugGroup()

開始一個除錯組,該除錯組用指定的標籤標記,並將包含所有後續編碼的命令,直到呼叫 popDebugGroup() 方法為止。

setBindGroup()

為給定的索引設定要用於後續計算命令的 GPUBindGroup

setPipeline()

為該計算通道設定要使用的 GPUComputePipeline

示例

在我們 基本的計算演示 中,透過 GPUCommandEncoder 錄製了多個命令。其中大多數命令都源自透過 GPUCommandEncoder.beginComputePass() 建立的 GPUComputePassEncoder

js
// …

// Create GPUCommandEncoder to encode commands to issue to the GPU
const commandEncoder = device.createCommandEncoder();

// Create GPUComputePassEncoder to initiate compute pass
const passEncoder = commandEncoder.beginComputePass();

// Issue commands
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);
passEncoder.dispatchWorkgroups(Math.ceil(BUFFER_SIZE / 64));

// End the compute pass
passEncoder.end();

// Copy output buffer to staging buffer
commandEncoder.copyBufferToBuffer(
  output,
  0, // Source offset
  stagingBuffer,
  0, // Destination offset
  BUFFER_SIZE,
);

// End frame by passing array of command buffers to command queue for execution
device.queue.submit([commandEncoder.finish()]);

// …

規範

規範
WebGPU
# gpucomputepassencoder

瀏覽器相容性

另見