FluidCS11_ForceCS_Simple.hlsl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: threadId
  3. // CHECK: bufferLoad
  4. // CHECK: dot2
  5. // CHECK: Log
  6. // CHECK: Exp
  7. // CHECK: FMax
  8. // CHECK: Sqrt
  9. // CHECK: bufferStore
  10. //--------------------------------------------------------------------------------------
  11. // File: FluidCS11.hlsl
  12. //
  13. // Copyright (c) Microsoft Corporation. All rights reserved.
  14. //--------------------------------------------------------------------------------------
  15. //--------------------------------------------------------------------------------------
  16. // Smoothed Particle Hydrodynamics Algorithm Based Upon:
  17. // Particle-Based Fluid Simulation for Interactive Applications
  18. // Matthias Müller
  19. //--------------------------------------------------------------------------------------
  20. //--------------------------------------------------------------------------------------
  21. // Optimized Grid Algorithm Based Upon:
  22. // Broad-Phase Collision Detection with CUDA
  23. // Scott Le Grand
  24. //--------------------------------------------------------------------------------------
  25. struct Particle
  26. {
  27. float2 position;
  28. float2 velocity;
  29. };
  30. struct ParticleForces
  31. {
  32. float2 acceleration;
  33. };
  34. struct ParticleDensity
  35. {
  36. float density;
  37. };
  38. cbuffer cbSimulationConstants : register( b0 )
  39. {
  40. uint g_iNumParticles;
  41. float g_fTimeStep;
  42. float g_fSmoothlen;
  43. float g_fPressureStiffness;
  44. float g_fRestDensity;
  45. float g_fDensityCoef;
  46. float g_fGradPressureCoef;
  47. float g_fLapViscosityCoef;
  48. float g_fWallStiffness;
  49. float4 g_vGravity;
  50. float4 g_vGridDim;
  51. float3 g_vPlanes[4];
  52. };
  53. //--------------------------------------------------------------------------------------
  54. // Fluid Simulation
  55. //--------------------------------------------------------------------------------------
  56. #define SIMULATION_BLOCK_SIZE 256
  57. //--------------------------------------------------------------------------------------
  58. // Structured Buffers
  59. //--------------------------------------------------------------------------------------
  60. RWStructuredBuffer<Particle> ParticlesRW : register( u0 );
  61. StructuredBuffer<Particle> ParticlesRO : register( t0 );
  62. RWStructuredBuffer<ParticleDensity> ParticlesDensityRW : register( u0 );
  63. StructuredBuffer<ParticleDensity> ParticlesDensityRO : register( t1 );
  64. RWStructuredBuffer<ParticleForces> ParticlesForcesRW : register( u0 );
  65. StructuredBuffer<ParticleForces> ParticlesForcesRO : register( t2 );
  66. RWStructuredBuffer<unsigned int> GridRW : register( u0 );
  67. StructuredBuffer<unsigned int> GridRO : register( t3 );
  68. RWStructuredBuffer<uint2> GridIndicesRW : register( u0 );
  69. StructuredBuffer<uint2> GridIndicesRO : register( t4 );
  70. //--------------------------------------------------------------------------------------
  71. // Grid Construction
  72. //--------------------------------------------------------------------------------------
  73. // For simplicity, this sample uses a 16-bit hash based on the grid cell and
  74. // a 16-bit particle ID to keep track of the particles while sorting
  75. // This imposes a limitation of 64K particles and 256x256 grid work
  76. // You could extended the implementation to support large scenarios by using a uint2
  77. float2 GridCalculateCell(float2 position)
  78. {
  79. return clamp(position * g_vGridDim.xy + g_vGridDim.zw, float2(0, 0), float2(255, 255));
  80. }
  81. unsigned int GridConstuctKey(uint2 xy)
  82. {
  83. // Bit pack [-----UNUSED-----][----Y---][----X---]
  84. // 16-bit 8-bit 8-bit
  85. return dot(xy.yx, uint2(256, 1));
  86. }
  87. unsigned int GridConstuctKeyValuePair(uint2 xy, uint value)
  88. {
  89. // Bit pack [----Y---][----X---][-----VALUE------]
  90. // 8-bit 8-bit 16-bit
  91. return dot(uint3(xy.yx, value), uint3(256*256*256, 256*256, 1));
  92. }
  93. unsigned int GridGetKey(unsigned int keyvaluepair)
  94. {
  95. return (keyvaluepair >> 16);
  96. }
  97. unsigned int GridGetValue(unsigned int keyvaluepair)
  98. {
  99. return (keyvaluepair & 0xFFFF);
  100. }
  101. //--------------------------------------------------------------------------------------
  102. // Force Calculation
  103. //--------------------------------------------------------------------------------------
  104. float CalculatePressure(float density)
  105. {
  106. // Implements this equation:
  107. // Pressure = B * ((rho / rho_0)^y - 1)
  108. return g_fPressureStiffness * max(pow(density / g_fRestDensity, 3) - 1, 0);
  109. }
  110. float2 CalculateGradPressure(float r, float P_pressure, float N_pressure, float N_density, float2 diff)
  111. {
  112. const float h = g_fSmoothlen;
  113. float avg_pressure = 0.5f * (N_pressure + P_pressure);
  114. // Implements this equation:
  115. // W_spkiey(r, h) = 15 / (pi * h^6) * (h - r)^3
  116. // GRAD( W_spikey(r, h) ) = -45 / (pi * h^6) * (h - r)^2
  117. // g_fGradPressureCoef = fParticleMass * -45.0f / (PI * fSmoothlen^6)
  118. return g_fGradPressureCoef * avg_pressure / N_density * (h - r) * (h - r) / r * (diff);
  119. }
  120. float2 CalculateLapVelocity(float r, float2 P_velocity, float2 N_velocity, float N_density)
  121. {
  122. const float h = g_fSmoothlen;
  123. float2 vel_diff = (N_velocity - P_velocity);
  124. // Implements this equation:
  125. // W_viscosity(r, h) = 15 / (2 * pi * h^3) * (-r^3 / (2 * h^3) + r^2 / h^2 + h / (2 * r) - 1)
  126. // LAPLACIAN( W_viscosity(r, h) ) = 45 / (pi * h^6) * (h - r)
  127. // g_fLapViscosityCoef = fParticleMass * fViscosity * 45.0f / (PI * fSmoothlen^6)
  128. return g_fLapViscosityCoef / N_density * (h - r) * vel_diff;
  129. }
  130. //--------------------------------------------------------------------------------------
  131. // Simple N^2 Algorithm
  132. //--------------------------------------------------------------------------------------
  133. [numthreads(SIMULATION_BLOCK_SIZE, 1, 1)]
  134. void main( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )
  135. {
  136. const unsigned int P_ID = DTid.x; // Particle ID to operate on
  137. float2 P_position = ParticlesRO[P_ID].position;
  138. float2 P_velocity = ParticlesRO[P_ID].velocity;
  139. float P_density = ParticlesDensityRO[P_ID].density;
  140. float P_pressure = CalculatePressure(P_density);
  141. const float h_sq = g_fSmoothlen * g_fSmoothlen;
  142. float2 acceleration = float2(0, 0);
  143. // Calculate the acceleration based on all neighbors
  144. for (uint N_ID = 0 ; N_ID < g_iNumParticles ; N_ID++)
  145. {
  146. float2 N_position = ParticlesRO[N_ID].position;
  147. float2 diff = N_position - P_position;
  148. float r_sq = dot(diff, diff);
  149. if (r_sq < h_sq && P_ID != N_ID)
  150. {
  151. float2 N_velocity = ParticlesRO[N_ID].velocity;
  152. float N_density = ParticlesDensityRO[N_ID].density;
  153. float N_pressure = CalculatePressure(N_density);
  154. float r = sqrt(r_sq);
  155. // Pressure Term
  156. acceleration += CalculateGradPressure(r, P_pressure, N_pressure, N_density, diff);
  157. // Viscosity Term
  158. acceleration += CalculateLapVelocity(r, P_velocity, N_velocity, N_density);
  159. }
  160. }
  161. ParticlesForcesRW[P_ID].acceleration = acceleration / P_density;
  162. }