GPUContext API
WebGPU device and resource management.
Constructor
typescript
constructor()1
Creates a new GPUContext instance. Does not initialize the GPU device.
Methods
initialize
typescript
async initialize(config?: GPUContextConfig): Promise<void>1
Initialize WebGPU adapter and device.
Parameters:
typescript
interface GPUContextConfig {
powerPreference?: 'low-power' | 'high-performance';
forceFallbackAdapter?: boolean;
}1
2
3
4
2
3
4
Throws:
WebGPUNotSupportedError: If WebGPU is not availableDeviceInitializationError: If GPU device initialization fails
Example:
typescript
const context = new GPUContext();
await context.initialize({ powerPreference: 'high-performance' });1
2
2
getDevice
typescript
getDevice(): GPUDevice1
Get the WebGPU device. Must call initialize() first.
Throws:
Error: If context is not initialized
createBuffer
typescript
createBuffer(descriptor: GPUBufferDescriptor): GPUBuffer1
Create a GPU buffer.
Parameters:
descriptor: WebGPU buffer descriptor
Example:
typescript
const buffer = context.createBuffer({
size: 1024,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
});1
2
3
4
2
3
4
createShaderModule
typescript
createShaderModule(code: string, label?: string): GPUShaderModule1
Create a shader module from WGSL code.
Parameters:
code: WGSL shader codelabel: Optional debug label
createCommandEncoder
typescript
createCommandEncoder(descriptor?: GPUCommandEncoderDescriptor): GPUCommandEncoder1
Create a command encoder for recording GPU commands.
submit
typescript
submit(commandBuffers: GPUCommandBuffer[]): void1
Submit command buffers to the GPU queue.
Example:
typescript
const encoder = context.createCommandEncoder();
// ... encode commands
const commandBuffer = encoder.finish();
context.submit([commandBuffer]);1
2
3
4
2
3
4
writeBuffer
typescript
writeBuffer(buffer: GPUBuffer, data: ArrayBuffer | ArrayBufferView, bufferOffset?: number): void1
Write data to a GPU buffer.
Parameters:
buffer: Target GPU bufferdata: Data to writebufferOffset: Offset in buffer (default: 0)
deferDestroy
typescript
deferDestroy(buffer: GPUBuffer | null | undefined): void1
Queue a buffer for destruction after GPU work completes. Safe async cleanup pattern.
Example:
typescript
const tempBuffer = context.createBuffer({...});
context.submit(commandsUsingBuffer);
context.deferDestroy(tempBuffer);
// Buffer destroyed after GPU work completes1
2
3
4
2
3
4
sync
typescript
async sync(): Promise<void>1
Wait for all submitted GPU work to complete and flush deferred destructions.
destroy
typescript
destroy(): void1
Release all GPU resources. After calling this, the context cannot be reused.
Properties
isInitialized
typescript
readonly isInitialized: boolean1
Whether the context has been initialized.
Error Handling
typescript
import {
GPUContext,
WebGPUNotSupportedError,
DeviceInitializationError
} from 'tiny-dl-inference';
try {
const context = new GPUContext();
await context.initialize();
} catch (error) {
if (error instanceof WebGPUNotSupportedError) {
console.log('Please use a WebGPU-compatible browser');
} else if (error instanceof DeviceInitializationError) {
console.log('GPU initialization failed:', error.message);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16