I have a compute shader I use to crunch the n-body gravity calculations for my project. when I use all floats in the shader, it runs fine and can process the gravity calculations of 10,000 objects in about 8 ms. However. I can't use floats because part the gravity equation ((G x mass1 x mass2) / d^2) can produce a number greater than what floats can hold with 2 sun sized masses. This leads me to need to use doubles for that part of the calculations. This wouldn't be a problem, except it seems to SEVERELY increase the time it takes to execute the shader for 8 ms to 130 ms. Any input is appreciated.
[numthreads(256,1,1)]
void GravityComp (uint3 id : SV_DispatchThreadID)
{
uint ind = id.x;
float2 gravResult = float3(0, 0);
for (uint i = 0; i < (uint)numAsteroids; i++) {
if (ind == i)
continue;
float distance = Distance(dataIn[ind].xy, dataIn[i].xy);
double G = (double)0.0000000000667408;
double m1 = (double)dataIn[ind].z; // mass
double m2 = (double)dataIn[i].z; // mass
double newt = (G * m1 * m2) / (double)pow(distance, 2);
float acc = (float)(newt / m1);
float2 dir = -normalize(dataIn[ind].xy - dataIn[i].xy);
float2 grav = dir.xy * acc;
gravResult.xy = gravResult.xy + grav.xy;
}
dataOut[ind].xy = gravResult.xy;
}
↧