FluidCS11_IntegrateCS.hlsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: threadId
  3. // CHECK: bufferLoad
  4. // CHECK: dot3
  5. // CHECK: FMin
  6. // CHECK: bufferStore
  7. //--------------------------------------------------------------------------------------
  8. // File: FluidCS11.hlsl
  9. //
  10. // Copyright (c) Microsoft Corporation. All rights reserved.
  11. //--------------------------------------------------------------------------------------
  12. //--------------------------------------------------------------------------------------
  13. // Smoothed Particle Hydrodynamics Algorithm Based Upon:
  14. // Particle-Based Fluid Simulation for Interactive Applications
  15. // Matthias Müller
  16. //--------------------------------------------------------------------------------------
  17. //--------------------------------------------------------------------------------------
  18. // Optimized Grid Algorithm Based Upon:
  19. // Broad-Phase Collision Detection with CUDA
  20. // Scott Le Grand
  21. //--------------------------------------------------------------------------------------
  22. struct Particle
  23. {
  24. float2 position;
  25. float2 velocity;
  26. };
  27. struct ParticleForces
  28. {
  29. float2 acceleration;
  30. };
  31. struct ParticleDensity
  32. {
  33. float density;
  34. };
  35. cbuffer cbSimulationConstants : register( b0 )
  36. {
  37. uint g_iNumParticles;
  38. float g_fTimeStep;
  39. float g_fSmoothlen;
  40. float g_fPressureStiffness;
  41. float g_fRestDensity;
  42. float g_fDensityCoef;
  43. float g_fGradPressureCoef;
  44. float g_fLapViscosityCoef;
  45. float g_fWallStiffness;
  46. float4 g_vGravity;
  47. float4 g_vGridDim;
  48. float3 g_vPlanes[4];
  49. };
  50. //--------------------------------------------------------------------------------------
  51. // Fluid Simulation
  52. //--------------------------------------------------------------------------------------
  53. #define SIMULATION_BLOCK_SIZE 256
  54. //--------------------------------------------------------------------------------------
  55. // Structured Buffers
  56. //--------------------------------------------------------------------------------------
  57. RWStructuredBuffer<Particle> ParticlesRW : register( u0 );
  58. StructuredBuffer<Particle> ParticlesRO : register( t0 );
  59. RWStructuredBuffer<ParticleDensity> ParticlesDensityRW : register( u0 );
  60. StructuredBuffer<ParticleDensity> ParticlesDensityRO : register( t1 );
  61. RWStructuredBuffer<ParticleForces> ParticlesForcesRW : register( u0 );
  62. StructuredBuffer<ParticleForces> ParticlesForcesRO : register( t2 );
  63. RWStructuredBuffer<unsigned int> GridRW : register( u0 );
  64. StructuredBuffer<unsigned int> GridRO : register( t3 );
  65. RWStructuredBuffer<uint2> GridIndicesRW : register( u0 );
  66. StructuredBuffer<uint2> GridIndicesRO : register( t4 );
  67. //--------------------------------------------------------------------------------------
  68. // Integration
  69. //--------------------------------------------------------------------------------------
  70. [numthreads(SIMULATION_BLOCK_SIZE, 1, 1)]
  71. void main( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )
  72. {
  73. const unsigned int P_ID = DTid.x; // Particle ID to operate on
  74. float2 position = ParticlesRO[P_ID].position;
  75. float2 velocity = ParticlesRO[P_ID].velocity;
  76. float2 acceleration = ParticlesForcesRO[P_ID].acceleration;
  77. // Apply the forces from the map walls
  78. [unroll]
  79. for (unsigned int i = 0 ; i < 4 ; i++)
  80. {
  81. float dist = dot(float3(position, 1), g_vPlanes[i]);
  82. acceleration += min(dist, 0) * -g_fWallStiffness * g_vPlanes[i].xy;
  83. }
  84. // Apply gravity
  85. acceleration += g_vGravity.xy;
  86. // Integrate
  87. velocity += g_fTimeStep * acceleration;
  88. position += g_fTimeStep * velocity;
  89. // Update
  90. ParticlesRW[P_ID].position = position;
  91. ParticlesRW[P_ID].velocity = velocity;
  92. }