import type { Vector3 } from '@holoscript/core'; /** * Spatial Hash Grid for GPU Particle Collision Detection * * Implements a 3D spatial hash grid to enable O(N) particle-particle collision detection. * Uses a multi-pass approach: * 1. Clear grid counters * 2. Build grid by hashing particles into cells * 3. Detect collisions using neighbor cells * * @module gpu/SpatialGrid */ import type { WebGPUContext } from './WebGPUContext.js'; export interface SpatialGridOptions { /** Grid cell size (should be ~2× max particle radius) */ cellSize: number; /** Grid dimensions (cells in X/Y/Z) */ gridDimensions: Vector3; /** Max particles per cell (for array sizing) */ maxParticlesPerCell?: number; /** WGSL shader code */ shaderCode: string; } /** * Spatial Hash Grid Manager * * Manages GPU buffers and compute passes for spatial grid collision detection. * * @example * ```typescript * const grid = new SpatialGrid(context, particleCount, { * cellSize: 0.2, // 2× max particle radius * gridDimensions: [50, 50, 50 ], * shaderCode: spatialGridWGSL, * }); * * await grid.initialize(); * * // Each frame: * grid.clearGrid(); * grid.buildGrid(positionBuffer, velocityBuffer); * const forces = await grid.detectCollisions(); * ``` */ export declare class SpatialGrid { private context; private device; private particleCount; private options; private totalCells; private clearPipeline; private buildPipeline; private collisionPipeline; private uniformBuffer; private gridCellStartBuffer; private gridCellEndBuffer; private gridParticleIndicesBuffer; private collisionForcesBuffer; private clearBindGroup; private buildBindGroup; private collisionBindGroup; constructor(context: WebGPUContext, particleCount: number, options: SpatialGridOptions); /** * Initialize spatial grid buffers and pipelines */ initialize(): Promise; /** * Create GPU buffers for spatial grid */ private createBuffers; /** * Upload uniform data to GPU */ private uploadUniforms; /** * Create compute pipelines */ private createPipelines; /** * Create bind group layout for clear pass */ private createClearBindGroupLayout; /** * Create bind group layout for build pass */ private createBuildBindGroupLayout; /** * Create bind group layout for collision pass */ private createCollisionBindGroupLayout; /** * Clear grid counters (run before buildGrid each frame) */ clearGrid(commandEncoder?: GPUCommandEncoder): GPUCommandEncoder; /** * Build spatial grid from particle positions */ buildGrid(positionBuffer: GPUBuffer, commandEncoder?: GPUCommandEncoder): GPUCommandEncoder; /** * Detect collisions using spatial grid */ detectCollisions(positionBuffer: GPUBuffer, velocityBuffer: GPUBuffer, commandEncoder?: GPUCommandEncoder): GPUCommandEncoder; /** * Execute full collision detection pipeline * * Convenience method that runs all three passes: clear → build → detect */ execute(positionBuffer: GPUBuffer, velocityBuffer: GPUBuffer): Promise; /** * Download collision forces from GPU */ downloadCollisionForces(): Promise; /** * Get collision forces buffer (for use in other compute passes) */ getCollisionForcesBuffer(): GPUBuffer; /** * Calculate memory usage */ private calculateMemoryUsage; /** * Get grid statistics */ getStats(): { cellSize: number; gridDimensions: Vector3; totalCells: number; maxParticlesPerCell: number; memoryUsage: string; }; /** * Cleanup resources */ destroy(): void; } //# sourceMappingURL=SpatialGrid.d.ts.map