Namespace: SpawnDev.SpawnJS.JSObjects.WebGPU
Inheritance: SpawnJSObject -> GPUCommandEncoder
MDN Reference: GPUCommandEncoder
The GPUCommandEncoder class records GPU commands into a GPUCommandBuffer for later submission. It provides methods for beginning render and compute passes, copying data between buffers and textures, and resolving query sets. Created via GPUDevice.CreateCommandEncoder().
| Signature |
Description |
GPUCommandEncoder(SpawnJSObjectReference _ref) |
Deserialization constructor. |
| Property |
Type |
Description |
Label |
string? |
A label for debugging. |
| Method |
Return Type |
Description |
BeginRenderPass(GPURenderPassDescriptor descriptor) |
GPURenderPassEncoder |
Begins recording a render pass. |
BeginComputePass(GPUComputePassDescriptor? descriptor = null) |
GPUComputePassEncoder |
Begins recording a compute pass. |
| Method |
Return Type |
Description |
CopyBufferToBuffer(GPUBuffer source, long sourceOffset, GPUBuffer destination, long destinationOffset, long size) |
void |
Copies data between buffers. |
CopyBufferToTexture(GPUTexelCopyBufferInfo source, GPUTexelCopyTextureInfo destination, GPUExtent3DDict copySize) |
void |
Copies buffer data to a texture. |
CopyTextureToBuffer(GPUTexelCopyTextureInfo source, GPUTexelCopyBufferInfo destination, GPUExtent3DDict copySize) |
void |
Copies texture data to a buffer. |
CopyTextureToTexture(GPUTexelCopyTextureInfo source, GPUTexelCopyTextureInfo destination, GPUExtent3DDict copySize) |
void |
Copies between textures. |
| Method |
Return Type |
Description |
ClearBuffer(GPUBuffer buffer) |
void |
Clears an entire buffer to zero. |
ClearBuffer(GPUBuffer buffer, long offset, long size) |
void |
Clears a sub-range of a buffer to zero. |
ResolveQuerySet(GPUQuerySet querySet, int firstQuery, int queryCount, GPUBuffer destination, long destinationOffset) |
void |
Resolves query results into a buffer. |
| Method |
Return Type |
Description |
Finish() |
GPUCommandBuffer |
Finishes recording and returns a GPUCommandBuffer for submission. |
Finish(GPUCommandBufferDescriptor descriptor) |
GPUCommandBuffer |
Finishes with a descriptor (label). |
| Method |
Return Type |
Description |
PushDebugGroup(string groupLabel) |
void |
Begins a debug group. |
PopDebugGroup() |
void |
Ends the current debug group. |
InsertDebugMarker(string markerLabel) |
void |
Inserts a debug marker. |
using var encoder = device.CreateCommandEncoder(new GPUCommandEncoderDescriptor
{
Label = "Frame Encoder"
});
// Copy data between buffers
encoder.CopyBufferToBuffer(sourceBuffer, 0, destBuffer, 0, 4096);
// Begin a render pass
using var renderPass = encoder.BeginRenderPass(new GPURenderPassDescriptor
{
ColorAttachments = new GPURenderPassColorAttachment[]
{
new GPURenderPassColorAttachment
{
View = textureView,
LoadOp = "clear",
StoreOp = "store",
ClearValue = new GPUColorDict { R = 0, G = 0, B = 0, A = 1 }
}
}
});
renderPass.SetPipeline(renderPipeline);
renderPass.Draw(3);
renderPass.End();
// Begin a compute pass
using var computePass = encoder.BeginComputePass();
computePass.SetPipeline(computePipeline);
computePass.SetBindGroup(0, bindGroup);
computePass.DispatchWorkgroups(256);
computePass.End();
// Finish and submit
using var commands = encoder.Finish();
device.Queue.Submit(new[] { commands });