bgfx/examples/24-nbody/cs_update_instances.sc

58 lines
1.8 KiB
Python
Raw Normal View History

2014-12-20 08:09:58 +03:00
/*
* Copyright 2014 Stanlo Slasinski. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2014-12-20 08:09:58 +03:00
*/
#include "bgfx_compute.sh"
#include "uniforms.sh"
BUFFER_RO(prevPositionBuffer, vec4, 0);
2014-12-23 07:14:48 +03:00
BUFFER_RO(currPositionBuffer, vec4, 1);
2014-12-20 08:09:58 +03:00
BUFFER_WR(outPrevPositionBuffer, vec4, 2);
2014-12-23 07:14:48 +03:00
BUFFER_WR(outCurrPositionBuffer, vec4, 3);
2014-12-20 08:09:58 +03:00
#define GROUP_SIZE 512
SHARED vec3 otherEntries[GROUP_SIZE];
2014-12-23 07:14:48 +03:00
vec3 calcAcceleration(vec3 _currPosition, vec3 _otherPosition)
2014-12-20 08:09:58 +03:00
{
2014-12-23 07:14:48 +03:00
vec3 difference = _otherPosition - _currPosition;
2014-12-20 08:09:58 +03:00
float dist2 = dot(difference, difference);
float dist6 = dist2 * dist2 * dist2;
float invDist3 = 1.0 / (sqrt(dist6) + 0.1);
2014-12-23 03:36:18 +03:00
return u_gravity * u_gravity * invDist3 * difference;
2014-12-20 08:09:58 +03:00
}
NUM_THREADS(GROUP_SIZE, 1, 1)
void main()
{
vec3 prevPosition = prevPositionBuffer[gl_GlobalInvocationID.x].xyz;
2014-12-23 07:14:48 +03:00
vec3 currPosition = currPositionBuffer[gl_GlobalInvocationID.x].xyz;
2014-12-20 08:09:58 +03:00
vec3 newAcceleration = vec3_splat(0.0);
for (int j = 0; j < int(u_dispatchSize); ++j)
{
2014-12-23 07:14:48 +03:00
otherEntries[gl_LocalInvocationIndex] = currPositionBuffer[j * GROUP_SIZE + int(gl_LocalInvocationIndex)].xyz;
2014-12-20 08:09:58 +03:00
barrier();
for (int i = 0; i < GROUP_SIZE; ++i)
{
2014-12-23 07:14:48 +03:00
newAcceleration += calcAcceleration(currPosition, otherEntries[i]);
2014-12-20 08:09:58 +03:00
}
}
2014-12-23 07:14:48 +03:00
newAcceleration += (prevPosition - currPosition) * u_damping;
2014-12-20 08:09:58 +03:00
float accelerationMagnitude = length(newAcceleration);
float color = pow(min(accelerationMagnitude / 3.0, 1.0), 0.25);
if (accelerationMagnitude > 0.0)
{
newAcceleration = normalize(newAcceleration) * min(accelerationMagnitude, u_maxAcceleration);
}
2014-12-23 07:14:48 +03:00
vec3 newPosition = 2.0 * currPosition - prevPosition + newAcceleration * u_timeStep;
2014-12-20 08:09:58 +03:00
2014-12-23 07:14:48 +03:00
outPrevPositionBuffer[gl_GlobalInvocationID.x] = vec4(currPosition, 0.0);
outCurrPositionBuffer[gl_GlobalInvocationID.x] = vec4(newPosition, color);
2014-12-20 08:09:58 +03:00
}