bgfx/examples/24-nbody/cs_update_instances.sc

58 lines
1.8 KiB
Python
Raw Normal View History

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