FluidCS11_BuildGridCS.hlsl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: threadId
  3. // CHECK: bufferLoad
  4. // CHECK: FMax
  5. // CHECK: FMin
  6. // CHECK: IMad
  7. // CHECK: bufferStore
  8. //--------------------------------------------------------------------------------------
  9. // File: FluidCS11.hlsl
  10. //
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //--------------------------------------------------------------------------------------
  13. //--------------------------------------------------------------------------------------
  14. // Smoothed Particle Hydrodynamics Algorithm Based Upon:
  15. // Particle-Based Fluid Simulation for Interactive Applications
  16. // Matthias Müller
  17. //--------------------------------------------------------------------------------------
  18. //--------------------------------------------------------------------------------------
  19. // Optimized Grid Algorithm Based Upon:
  20. // Broad-Phase Collision Detection with CUDA
  21. // Scott Le Grand
  22. //--------------------------------------------------------------------------------------
  23. struct Particle
  24. {
  25. float2 position;
  26. float2 velocity;
  27. };
  28. struct ParticleForces
  29. {
  30. float2 acceleration;
  31. };
  32. struct ParticleDensity
  33. {
  34. float density;
  35. };
  36. cbuffer cbSimulationConstants : register( b0 )
  37. {
  38. uint g_iNumParticles;
  39. float g_fTimeStep;
  40. float g_fSmoothlen;
  41. float g_fPressureStiffness;
  42. float g_fRestDensity;
  43. float g_fDensityCoef;
  44. float g_fGradPressureCoef;
  45. float g_fLapViscosityCoef;
  46. float g_fWallStiffness;
  47. float4 g_vGravity;
  48. float4 g_vGridDim;
  49. float3 g_vPlanes[4];
  50. };
  51. //--------------------------------------------------------------------------------------
  52. // Fluid Simulation
  53. //--------------------------------------------------------------------------------------
  54. #define SIMULATION_BLOCK_SIZE 256
  55. //--------------------------------------------------------------------------------------
  56. // Structured Buffers
  57. //--------------------------------------------------------------------------------------
  58. RWStructuredBuffer<Particle> ParticlesRW : register( u0 );
  59. StructuredBuffer<Particle> ParticlesRO : register( t0 );
  60. RWStructuredBuffer<ParticleDensity> ParticlesDensityRW : register( u0 );
  61. StructuredBuffer<ParticleDensity> ParticlesDensityRO : register( t1 );
  62. RWStructuredBuffer<ParticleForces> ParticlesForcesRW : register( u0 );
  63. StructuredBuffer<ParticleForces> ParticlesForcesRO : register( t2 );
  64. RWStructuredBuffer<unsigned int> GridRW : register( u0 );
  65. StructuredBuffer<unsigned int> GridRO : register( t3 );
  66. RWStructuredBuffer<uint2> GridIndicesRW : register( u0 );
  67. StructuredBuffer<uint2> GridIndicesRO : register( t4 );
  68. //--------------------------------------------------------------------------------------
  69. // Grid Construction
  70. //--------------------------------------------------------------------------------------
  71. // For simplicity, this sample uses a 16-bit hash based on the grid cell and
  72. // a 16-bit particle ID to keep track of the particles while sorting
  73. // This imposes a limitation of 64K particles and 256x256 grid work
  74. // You could extended the implementation to support large scenarios by using a uint2
  75. float2 GridCalculateCell(float2 position)
  76. {
  77. return clamp(position * g_vGridDim.xy + g_vGridDim.zw, float2(0, 0), float2(255, 255));
  78. }
  79. unsigned int GridConstuctKey(uint2 xy)
  80. {
  81. // Bit pack [-----UNUSED-----][----Y---][----X---]
  82. // 16-bit 8-bit 8-bit
  83. return dot(xy.yx, uint2(256, 1));
  84. }
  85. unsigned int GridConstuctKeyValuePair(uint2 xy, uint value)
  86. {
  87. // Bit pack [----Y---][----X---][-----VALUE------]
  88. // 8-bit 8-bit 16-bit
  89. return dot(uint3(xy.yx, value), uint3(256*256*256, 256*256, 1));
  90. }
  91. unsigned int GridGetKey(unsigned int keyvaluepair)
  92. {
  93. return (keyvaluepair >> 16);
  94. }
  95. unsigned int GridGetValue(unsigned int keyvaluepair)
  96. {
  97. return (keyvaluepair & 0xFFFF);
  98. }
  99. //--------------------------------------------------------------------------------------
  100. // Build Grid
  101. //--------------------------------------------------------------------------------------
  102. [numthreads(SIMULATION_BLOCK_SIZE, 1, 1)]
  103. void main( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )
  104. {
  105. const unsigned int P_ID = DTid.x; // Particle ID to operate on
  106. float2 position = ParticlesRO[P_ID].position;
  107. float2 grid_xy = GridCalculateCell( position );
  108. GridRW[P_ID] = GridConstuctKeyValuePair((uint2)grid_xy, P_ID);
  109. }