Няма описание

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. struct Particle
  5. {
  6. vec4 pos;
  7. vec4 vel;
  8. };
  9. // Binding 0 : Position storage buffer
  10. layout(std140, binding = 0) buffer Pos
  11. {
  12. Particle particles[ ];
  13. };
  14. layout (local_size_x = 256) in;
  15. layout (binding = 1) uniform UBO
  16. {
  17. float deltaT;
  18. float destX;
  19. float destY;
  20. int particleCount;
  21. } ubo;
  22. layout (constant_id = 0) const int SHARED_DATA_SIZE = 512;
  23. layout (constant_id = 1) const float GRAVITY = 0.002;
  24. layout (constant_id = 2) const float POWER = 0.75;
  25. layout (constant_id = 3) const float SOFTEN = 0.0075;
  26. // Share data between computer shader invocations to speed up caluclations
  27. shared vec4 sharedData[SHARED_DATA_SIZE];
  28. void main()
  29. {
  30. // Current SSBO index
  31. uint index = gl_GlobalInvocationID.x;
  32. if (index >= ubo.particleCount)
  33. return;
  34. vec4 position = particles[index].pos;
  35. vec4 velocity = particles[index].vel;
  36. vec4 acceleration = vec4(0.0);
  37. for (int i = 0; i < ubo.particleCount; i += SHARED_DATA_SIZE)
  38. {
  39. if (i + gl_LocalInvocationID.x < ubo.particleCount)
  40. {
  41. sharedData[gl_LocalInvocationID.x] = particles[i + gl_LocalInvocationID.x].pos;
  42. }
  43. else
  44. {
  45. sharedData[gl_LocalInvocationID.x] = vec4(0.0);
  46. }
  47. memoryBarrierShared();
  48. barrier();
  49. for (int j = 0; j < gl_WorkGroupSize.x; j++)
  50. {
  51. vec4 other = sharedData[j];
  52. vec3 len = other.xyz - position.xyz;
  53. acceleration.xyz += GRAVITY * len * other.w / pow(dot(len, len) + SOFTEN, POWER);
  54. }
  55. }
  56. particles[index].vel.xyz += ubo.deltaT * acceleration.xyz;
  57. // Gradient texture position
  58. particles[index].vel.w += 0.1 * ubo.deltaT;
  59. if (particles[index].vel.w > 1.0)
  60. particles[index].vel.w -= 1.0;
  61. }