water_compute.glsl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #[compute]
  2. #version 450
  3. // Invocations in the (x, y, z) dimension.
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. // Our textures.
  6. layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D current_image;
  7. layout(r32f, set = 1, binding = 0) uniform restrict readonly image2D previous_image;
  8. layout(r32f, set = 2, binding = 0) uniform restrict writeonly image2D output_image;
  9. // Our push PushConstant.
  10. layout(push_constant, std430) uniform Params {
  11. vec4 add_wave_point;
  12. vec2 texture_size;
  13. float damp;
  14. float res2;
  15. } params;
  16. // The code we want to execute in each invocation.
  17. void main() {
  18. ivec2 tl = ivec2(0, 0);
  19. ivec2 size = ivec2(params.texture_size.x - 1, params.texture_size.y - 1);
  20. ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
  21. // Just in case the texture size is not divisable by 8.
  22. if ((uv.x > size.x) || (uv.y > size.y)) {
  23. return;
  24. }
  25. float current_v = imageLoad(current_image, uv).r;
  26. float up_v = imageLoad(current_image, clamp(uv - ivec2(0, 1), tl, size)).r;
  27. float down_v = imageLoad(current_image, clamp(uv + ivec2(0, 1), tl, size)).r;
  28. float left_v = imageLoad(current_image, clamp(uv - ivec2(1, 0), tl, size)).r;
  29. float right_v = imageLoad(current_image, clamp(uv + ivec2(1, 0), tl, size)).r;
  30. float previous_v = imageLoad(previous_image, uv).r;
  31. float new_v = 2.0 * current_v - previous_v + 0.25 * (up_v + down_v + left_v + right_v - 4.0 * current_v);
  32. new_v = new_v - (params.damp * new_v * 0.001);
  33. if (params.add_wave_point.z > 0.0 && uv.x == floor(params.add_wave_point.x) && uv.y == floor(params.add_wave_point.y)) {
  34. new_v = params.add_wave_point.z;
  35. }
  36. if (new_v < 0.0) {
  37. new_v = 0.0;
  38. }
  39. vec4 result = vec4(new_v, new_v, new_v, 1.0);
  40. imageStore(output_image, uv, result);
  41. }