--- /dev/null
+import fluidsim;
+
+#pragma MSP stage(compute)
+void main()
+{
+ ivec2 size = imageSize(clamping_out);
+ ivec2 coord = ivec2(gl_GlobalInvocationID.xy)+ivec2(1, 1);
+ if(coord.x>=size.x-1 || coord.y>=size.y-1)
+ return;
+
+ float depth = get_depth(coord);
+ vec2 velocity = texelFetch(velocity_in, coord, 0).xy;
+ float left_vx = texelFetch(velocity_in, coord-ivec2(1, 0), 0).x;
+ float down_vy = texelFetch(velocity_in, coord-ivec2(0, 1), 0).y;
+
+ // Clamping step: prevent fluid depth from going negative
+ float total_out = max(velocity.x, 0.0)+max(velocity.y, 0.0)-min(left_vx, 0.0)-min(down_vy, 0.0);
+ float scale = min(max_flow_fraction/(total_out*delta_time), depth/residual_depth);
+
+ imageStore(clamping_out, coord, vec4(clamp(scale, 0.0, 1.0), 0.0, 0.0, 0.0));
+}