使用 WebGPU 的虚拟管道模型的 GPU 浅水求解器
This is a shallow water equations solver using the virtual pipes method that runs on the GPU using the WebGPU API.
Shallow water equations describe e.g. water flow over terrain. They assume that water exists in columns without gaps, and as such can be represented as simply a heightmap of water surface over some terrain heightmap. As such, it doesn't allow vertical gaps in the water volume, or vertical water flow. To make sense of the visualization, imagine looking at a terrain with water on it directly from above.
It uses wgpu-native WebGPU implementation, and SDL2 to create a window to render to.
This project is licensed under the terms of the MIT license.
Build the project first (see instructions below), then simply run it. By default, a 256x256 empty grid is opened, and you can add water to it with the left mouse button.
On the left you'll see a bunch of settings:
VSync -- toggle vsync (the simulations runs faster with vsync off, since it does 1 simulation step per frame)Cells X -- the size of simulation grid (i.e. number of cells) in XCells Y -- the size of simulation grid (i.e. number of cells) in Ydt -- the time delta used for integration (note that larger values lead to instability)Gravity -- the strength of gravity (note that larger values lead to instability)Friction -- the fluid friction (note that larger values lead to instability)Particles -- the number of particles simulated to visualize the fluid flowLeft -- set the left border to be a wall, a source of fluid, a drain for fluid, or a wave generatorRight -- same for the right borderBottom -- same for the bottom borderTop -- same for the top borderAction -- select mouse button action from no action, adding bed (i.e. terrain below water), removing bed, adding water, removing water, or moving water aroundRadius -- select mouse action radius, in grid cells (it is visualized as a circle)Force -- select mouse action force (larger means stronger action, e.g. more water added)Load preset -- load one of available terrain presets (these are randomized!)Show velocity -- show velocity vectors (these are hard to see at grid sizes larger than 64)Show particles -- show particles advected by the fluid flowThe simulation solves the shallow water equations using the virtual pipes method with outflow scaling from this paper.
An NxN grid simulation uses four buffers:
For the flow buffers, the last (N-th) row/column (for X/Y flow, respectively) isn't used, and exists just for convenience. All buffers are stored as float32 textures, with the bed and water buffers being stored as a single RG32F texture. The flow textures cannot be merged into one texture due to the access paterns of the simulation (two different kernel invocation might want to modify the X and Y flow at the same texel independently).
Additionally, a velocity buffer & a particle buffer are stored, for visualization only (they don't affect the simulation in any way).
A simulation step consists of:
To build this project, you need
To install wgpu-native, download some release archive for your platform, and unpack it somewhere. This project was built with the v0.19.4.1 release, and might not work with older versions.
Don't forget to check out submodules:
You can do this at clone time, using git clone --recurse-submodules. Add --shallow-submodules to prevent loading the whole commit history of those submodules. Otherwise, you can checkout submodules at any time after cloning the repo with git submodule update --init --recursive.
Then, follow the usual steps for building something with CMake:
cmake -DWGPU_NATIVE_ROOT=cmake --build .暂无开放 Issues,或尚未同步最近议题。