WebGPGPU, or WGU for short, is a WebGL2 based library enabling general purpose computation on the GPU.
const WGU = WebGPGPU(context); new WGU.VertexFeedbackComputer({ units: 8e5, struct: { position: "vec2", velocity: "vec2", mass: "float", color: "vec3" }, initialize: (i, buffer) => {...} updateStep: { glsl: ` void main() { o_position = i_position + i_velocity; o_velocity = i_velocity - 0.001 * i_position / i_mass; o_mass = i_mass; o_color = i_color; } ` }, renderStep: { glsl: ` void main() { gl_Position = vec4(i_position, 0.0, 1.0); vertexColor = vec4(i_color, .5); gl_PointSize = i_mass/2.0; } ` } });See this example in action here
The code above uses a VertexFeedbackComputer (a particular way of computing stuff provided by the library, relying on Vertex Buffers and OpenGL ES 3.0's Transform Feedback) to simulate a simple 800,000 particle system. Each particle has its own position, velocity, mass, and color, initially random, and is represented by a coloured point as it falls toward the origin.
Documentation is available online here or in markdown form at master/build/docs
Include the library from rawgit.com or download it locally
<script src="https://rawgit.com/npny/webgpgpu/master/src/webgpgpu.js"></script> <script src="src/webgpgu.js"></script>In order to use WebGPGPU in your page, you then need to initialize it with an existing, valid WebGL2 context:
<canvas id="canvas"></canvas> <script> const canvas = document.getElementById("canvas"); const context = canvas.getContext("webgl2"); const WGU = WebGPGPU(context); // Let's get rolling new WGU.VertexFeedbackComputer(); </script>WebGPGPU is released under the MIT license. Pierre Boyer, 2017.