Files
Seele/res/shaders/fluid/MassProbe.slang
T

69 lines
2.3 KiB
Plaintext

import FluidGridData;
// Diagnostics-only: sums the interior of several fields so we can watch
// "mass" over time on the CPU. Not part of the simulation.
struct Params
{
// read-only
FluidGridData<float> density;
FluidGridData<float> phi;
FluidGridData<float> velocityX;
FluidGridData<float> velocityY;
FluidGridData<float> velocityZ;
// read-write: [0]=density sum, [1]=phi sum, [2]=|velocity| sum
RWStructuredBuffer<float> out;
};
ParameterBlock<Params> params;
// Reduce the 32x8 thread group via shared memory, then one atomic add per group.
groupshared float laneD[256];
groupshared float laneP[256];
groupshared float laneV[256];
// One thread group per z-slice; each thread covers a 32x8 tile of the x/y plane.
[numthreads(32, 8, 1)]
[shader("compute")]
void massProbe(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> density = params.density;
FluidGridData<float> phi = params.phi;
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
RWStructuredBuffer<float> out = params.out;
// Interior only: skip the 1-cell boundary shell (matches the other passes).
int x = dispatchThreadID.x + 1;
int y = dispatchThreadID.y + 1;
int z = dispatchThreadID.z + 1;
if (x >= gridParams.gridSize.x - 1 || y >= gridParams.gridSize.y - 1 || z >= gridParams.gridSize.z - 1)
return;
float dSum = density[x, y, z];
float pSum = phi[x, y, z];
float vSum = sqrt(velocityX[x, y, z] * velocityX[x, y, z] +
velocityY[x, y, z] * velocityY[x, y, z] +
velocityZ[x, y, z] * velocityZ[x, y, z]);
uint lane = dispatchThreadID.x + dispatchThreadID.y * 32; // 0..255
laneD[lane] = dSum;
laneP[lane] = pSum;
laneV[lane] = vSum;
GroupMemoryBarrierWithGroupSync();
// Lane 0 of the group sums all 256 lanes and atomically adds to the global total.
if (lane == 0)
{
float td = 0.0f, tp = 0.0f, tv = 0.0f;
for (uint i = 0; i < 256; ++i)
{
td += laneD[i];
tp += laneP[i];
tv += laneV[i];
}
InterlockedAdd(out[0], td);
InterlockedAdd(out[1], tp);
InterlockedAdd(out[2], tv);
}
}