import { Memory } from "./memory"; /** A pointer to points to the raw address space. */ export type GPUPointer = number; export interface GPUDeviceDetectOutput { adapter: GPUAdapter; adapterInfo: GPUAdapterInfo; device: GPUDevice; } /** * DetectGPU device in the environment. */ export declare function detectGPUDevice(powerPreference?: "low-power" | "high-performance"): Promise; /** * Function info from the API */ export interface FunctionInfo { name: string; arg_types: Array; launch_param_tags: Array; } /** * WebGPU context * Manages all the webgpu resources here. */ export declare class WebGPUContext { device: GPUDevice; memory: Memory; private bufferTable; private bufferTableFreeId; private canvasRenderManager?; private readStagingBufferPool; private maxReadStagingBuffers; private pendingGPUToCPUCopy; private pendingGPUToCPUCopyIsQueueTail; private pendingEncoder; private uniformBufferPool; private uniformBufferPoolSizes; private pendingDispatchCount; private peakAllocatedBytes; private currAllocatedBytes; private allAllocatedBytes; private shaderSubmitCounter; protected debugShaderSubmitLimit: number; protected debugLogFinish: boolean; constructor(memory: Memory, device: GPUDevice); /** * Flush all pending GPU commands by finishing and submitting the * accumulated command encoder. * * Must be called before: * - GPU→CPU readback (deviceCopyFromGPU) * - CPU→GPU writes (deviceCopyToGPU, copyRawBytesToBuffer) * - Buffer deallocation (deviceFreeDataSpace) * - Canvas drawing (drawImageFromBuffer) * - Queue sync (sync) */ flushCommands(): void; /** * Dispose context. */ dispose(): void; /** * Wait for all pending GPU tasks to complete */ sync(): Promise; /** * Obtain the runtime information in readable format. */ runtimeStatsText(): string; /** * Draw image from data in storage buffer. * @param ptr The GPU ptr * @param height The height of the image. * @param width The width of the image. */ drawImageFromBuffer(ptr: GPUPointer, height: number, width: number): void; /** * Copy raw bytes into buffer ptr. * * @param rawBytes The raw bytes * @param toPtr The target gpu buffer ptr * @param toOffset The beginning offset * @param nbytes Number of bytes */ copyRawBytesToBuffer(rawBytes: Uint8Array, toPtr: GPUPointer, toOffset: number, nbytes: number): void; /** * Clear canvas */ clearCanvas(): void; /** * Bind a canvas element to the runtime. * @param canvas The HTML canvas/ */ bindCanvas(canvas: HTMLCanvasElement): void; /** * Create a PackedFunc that runs the given shader * via createComputePipeline * * @param finfo The function information already parsed as a record. * @param code The shader data(in WGSL) * @returns The shader */ createShader(finfo: FunctionInfo, code: string): Function; /** * Create a PackedFunc that runs the given shader asynchronously * via createComputePipelineAsync * * @param finfo The function information already parsed as a record. * @param code The shader data(in WGSL) * @returns The shader */ createShaderAsync(finfo: FunctionInfo, code: string): Promise; /** * Get a uniform buffer from the per-dispatch pool. * * Each dispatch in a batched encoder needs its own uniform buffer because * queue.writeBuffer() executes immediately while compute passes are deferred. * Reusing a shared buffer would overwrite data before earlier dispatches * consume it. * * The pool grows as needed. Buffers are reused across flushes (indexed by * dispatch position within the current batch). If the pool has no slot for * this dispatch, we flush first — this submits all pending passes, resets * pendingDispatchCount to 0, and allows reuse from the start of the pool. * * State after flush: the pending encoder and all bind group / buffer * references from prior dispatches are submitted and consumed. The new * dispatch starts a fresh encoder, so no stale state carries over. * * @param nbytes Minimum buffer size in bytes. * @returns A GPUBuffer with UNIFORM | COPY_DST usage, at least nbytes large. */ private getUniformFromPool; /** * Internal impl of createShader for both async and sync mode. * * @param finfo The function information already parsed as a record. * @param code The shader data(in WGSL) * @param asyncMode Whether use async mode. * @returns The shader function or promise of shader func. */ private createShadeInternal; /** * Get the device API according to its name * @param name The name of the API. * @returns The corresponding device api. */ getDeviceAPI(name: string): Function; private deviceAllocDataSpace; private deviceFreeDataSpace; private deviceCopyToGPU; /** * Get a MAP_READ staging buffer from the pool, or create one if none fits. * Uses first-fit-by-size: returns the first pooled buffer >= nbytes. */ private getOrCreateReadStagingBuffer; /** * Return a MAP_READ staging buffer to the pool for reuse. * Evicts the smallest buffer if the pool is full. */ private recycleReadStagingBuffer; private deviceCopyFromGPU; private deviceCopyWithinGPU; private gpuBufferFromPtr; private attachToBufferTable; }