directx - Calculation failing in the compute shader. HLSL DX11 -


i'm new compute shaders , i've started implementation of 1 nbody simulation , i've come across problem can't solve on own.

here's contained in compute file , entry point particlecomputeshader. dispatching 1 thread , creating 1024 in shader. there 1024 particles while debug , tweak each thread has it's own particle relate to.

the problem seems distance != 0.0f , calculation related distance. before had check in returning position 1.qnan dividing 0 somewhere in code. thoughts on i'm incorrectly accessing structuredbuffer using j , it's screwing next few calculations.

another note: position.w mass of particle.

struct constantparticledata {     float4 position;     float4 velocity; };  struct particledata {     float4 position;     float4 velocity; };  namespace constants {     float big_g = 6.674e-11f;     float soften = 0.01f; }  structuredbuffer<constantparticledata> inputconstantparticledata : register( t0 );  rwstructuredbuffer<particledata> outputparticledata : register( u0 );   [numthreads(1024, 1, 1)] void particlecomputeshader( int3 dispatchthreadid : sv_dispatchthreadid ) {     float3 acceleration = float3(0.0f, 0.0f, 0.0f);      for(int j = 0; j < 1024; j++)     {         float3 r_ij;         r_ij.x = inputconstantparticledata[j].position.x - inputconstantparticledata[dispatchthreadid.x].position.x;         r_ij.y = inputconstantparticledata[j].position.y - inputconstantparticledata[dispatchthreadid.x].position.y;         r_ij.z = inputconstantparticledata[j].position.z - inputconstantparticledata[dispatchthreadid.x].position.z;          float distance = 0.0f;         distance = length(r_ij);            if(distance != 0.0f)         {             float bottomline = pow(distance, 2) + pow(constants::soften, 2);              acceleration += constants::big_g * ((inputconstantparticledata[j].position.w * r_ij) /                              pow(bottomline, 1.5));         }     }      acceleration = acceleration / inputconstantparticledata[dispatchthreadid.x].position.w;      outputparticledata[dispatchthreadid.x].velocity = inputconstantparticledata[dispatchthreadid.x].velocity +                                                       float4(acceleration.x, acceleration.y, acceleration.z, 0.0f);      outputparticledata[dispatchthreadid.x].position = inputconstantparticledata[dispatchthreadid.x].position +                                                float4(outputparticledata[dispatchthreadid.x].velocity.x,                                                       outputparticledata[dispatchthreadid.x].velocity.y,                                                       outputparticledata[dispatchthreadid.x].velocity.z,                                                       0.0f);   } 

any appreciated. shader works simple input -> output , started begin giving troubles when tried use more of input buffer inputconstantparticledata[dispatchthreadid.x] @ 1 time.

you can't really set global variables in hlsl. compiler allows them because in case use shader through fx, set globals through constant buffers. glad see solved it, wanted post why having float defined local variable fixed issue.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -