- Home /
doubles and compute shaders
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;
}
Answer by TheyLeftMe4Dead · Feb 09, 2017 at 02:46 AM
One solution to your problem would be to change your units. Ex : instead of 12" , 1'. Another idea is to store the end result in scientific notation. Ex: 3.16 x 10^8 . To do this, you could get the result as a double, split it into the 2 parts of your scientific notation (two variables), then cast it back into a float.
You could also cut the size of everything in your scene by half. (I dont know if that messes wih your formula)
Does your number need to be exact? If not, You could consider "trimming" your numbers down in some way.
Converting my variables to scientific notation, and adding/subtracting the exponents and multiplying/dividing the mantissas worked (don't need to convert anything to doubles).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[Shader] Usage #ifdef DIRECTIONAL 0 Answers
Wavey Shaders, Where to start? 1 Answer
Replacing Shader At Runtime. 1 Answer
How to get Initial texture in Post-Processing Stack v2 1 Answer