This commit is contained in:
Branimir Karadžić 2014-12-22 20:14:48 -08:00
parent d79eab89ea
commit 798dd30e9f
7 changed files with 84 additions and 85 deletions

View file

@ -7,7 +7,7 @@
#include "uniforms.sh"
BUFFER_WR(prevPositionBuffer, vec4, 0);
BUFFER_WR(curPositionBuffer, vec4, 1);
BUFFER_WR(currPositionBuffer, vec4, 1);
uint rotl(uint _x, uint _r)
{
@ -101,5 +101,5 @@ void main()
vec3 velocity = u_initialSpeed * randomPointOnSphere(gl_GlobalInvocationID.x, u_baseSeed * 7u + 3u);
prevPositionBuffer[gl_GlobalInvocationID.x] = vec4(position - velocity * u_timeStep, 0.0);
curPositionBuffer[ gl_GlobalInvocationID.x] = vec4(position, 0.0);
currPositionBuffer[gl_GlobalInvocationID.x] = vec4(position, 0.0);
}

View file

@ -7,16 +7,16 @@
#include "uniforms.sh"
BUFFER_RO(prevPositionBuffer, vec4, 0);
BUFFER_RO(curPositionBuffer, vec4, 1);
BUFFER_RO(currPositionBuffer, vec4, 1);
BUFFER_WR(outPrevPositionBuffer, vec4, 2);
BUFFER_WR(outCurPositionBuffer, vec4, 3);
BUFFER_WR(outCurrPositionBuffer, vec4, 3);
#define GROUP_SIZE 512
SHARED vec3 otherEntries[GROUP_SIZE];
vec3 calcAcceleration(vec3 _curPosition, vec3 _otherPosition)
vec3 calcAcceleration(vec3 _currPosition, vec3 _otherPosition)
{
vec3 difference = _otherPosition - _curPosition;
vec3 difference = _otherPosition - _currPosition;
float dist2 = dot(difference, difference);
float dist6 = dist2 * dist2 * dist2;
float invDist3 = 1.0 / (sqrt(dist6) + 0.1);
@ -27,22 +27,22 @@ NUM_THREADS(GROUP_SIZE, 1, 1)
void main()
{
vec3 prevPosition = prevPositionBuffer[gl_GlobalInvocationID.x].xyz;
vec3 curPosition = curPositionBuffer[ gl_GlobalInvocationID.x].xyz;
vec3 currPosition = currPositionBuffer[gl_GlobalInvocationID.x].xyz;
vec3 newAcceleration = vec3_splat(0.0);
for (int j = 0; j < int(u_dispatchSize); ++j)
{
otherEntries[gl_LocalInvocationIndex] = curPositionBuffer[j * GROUP_SIZE + int(gl_LocalInvocationIndex)].xyz;
otherEntries[gl_LocalInvocationIndex] = currPositionBuffer[j * GROUP_SIZE + int(gl_LocalInvocationIndex)].xyz;
barrier();
for (int i = 0; i < GROUP_SIZE; ++i)
{
newAcceleration += calcAcceleration(curPosition, otherEntries[i]);
newAcceleration += calcAcceleration(currPosition, otherEntries[i]);
}
}
newAcceleration += (prevPosition - curPosition) * u_damping;
newAcceleration += (prevPosition - currPosition) * u_damping;
float accelerationMagnitude = length(newAcceleration);
float color = pow(min(accelerationMagnitude / 3.0, 1.0), 0.25);
if (accelerationMagnitude > 0.0)
@ -50,8 +50,8 @@ void main()
newAcceleration = normalize(newAcceleration) * min(accelerationMagnitude, u_maxAcceleration);
}
vec3 newPosition = 2.0 * curPosition - prevPosition + newAcceleration * u_timeStep;
vec3 newPosition = 2.0 * currPosition - prevPosition + newAcceleration * u_timeStep;
outPrevPositionBuffer[gl_GlobalInvocationID.x] = vec4(curPosition, 0.0);
outCurPositionBuffer[ gl_GlobalInvocationID.x] = vec4(newPosition, color);
outPrevPositionBuffer[gl_GlobalInvocationID.x] = vec4(currPosition, 0.0);
outCurrPositionBuffer[gl_GlobalInvocationID.x] = vec4(newPosition, color);
}

View file

@ -1,7 +1,7 @@
/*
* Copyright 2014 Stanlo Slasinski. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
* Copyright 2014 Stanlo Slasinski. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "common.h"
#include "bgfx_utils.h"
@ -22,69 +22,68 @@ struct u_paramsDataStruct
float initialSpeed;
int32_t initialShape;
float maxAccel;
};
void InitializeParams(unsigned mode, u_paramsDataStruct * params)
void InitializeParams(unsigned _mode, u_paramsDataStruct* _params)
{
switch(mode)
{
case 0:
params->timeStep = 0.0067f;
params->dispatchSize = 32;
params->gravity = 0.069f;
params->damping = 0.0f;
params->particleIntensity = 0.35f;
params->particleSize = 0.925f;
params->baseSeed = 0;
params->particlePower = 5.0f;
params->initialSpeed = 122.6f;
params->initialShape = 0;
params->maxAccel = 30.0;
break;
switch(_mode)
{
case 0:
_params->timeStep = 0.0067f;
_params->dispatchSize = 32;
_params->gravity = 0.069f;
_params->damping = 0.0f;
_params->particleIntensity = 0.35f;
_params->particleSize = 0.925f;
_params->baseSeed = 0;
_params->particlePower = 5.0f;
_params->initialSpeed = 122.6f;
_params->initialShape = 0;
_params->maxAccel = 30.0;
break;
case 1:
params->timeStep = 0.0157f;
params->dispatchSize = 32;
params->gravity = 0.109f;
params->damping = 0.25f;
params->particleIntensity = 0.64f;
params->particleSize = 0.279f;
params->baseSeed = 57;
params->particlePower = 3.5f;
params->initialSpeed = 3.2f;
params->initialShape = 1;
params->maxAccel = 100.0;
break;
case 1:
_params->timeStep = 0.0157f;
_params->dispatchSize = 32;
_params->gravity = 0.109f;
_params->damping = 0.25f;
_params->particleIntensity = 0.64f;
_params->particleSize = 0.279f;
_params->baseSeed = 57;
_params->particlePower = 3.5f;
_params->initialSpeed = 3.2f;
_params->initialShape = 1;
_params->maxAccel = 100.0;
break;
case 2:
params->timeStep = 0.02f;
params->dispatchSize = 32;
params->gravity = 0.24f;
params->damping = 0.12f;
params->particleIntensity = 1.0f;
params->particleSize = 1.0f;
params->baseSeed = 23;
params->particlePower = 4.0f;
params->initialSpeed = 31.1f;
params->initialShape = 2;
params->maxAccel = 39.29f;
break;
case 2:
_params->timeStep = 0.02f;
_params->dispatchSize = 32;
_params->gravity = 0.24f;
_params->damping = 0.12f;
_params->particleIntensity = 1.0f;
_params->particleSize = 1.0f;
_params->baseSeed = 23;
_params->particlePower = 4.0f;
_params->initialSpeed = 31.1f;
_params->initialShape = 2;
_params->maxAccel = 39.29f;
break;
case 3:
params->timeStep = 0.0118f;
params->dispatchSize = 32;
params->gravity = 0.141f;
params->damping = 1.0f;
params->particleIntensity = 0.64f;
params->particleSize = 0.28f;
params->baseSeed = 60;
params->particlePower = 1.97f;
params->initialSpeed = 69.7f;
params->initialShape = 3;
params->maxAccel = 3.21f;
break;
}
case 3:
_params->timeStep = 0.0118f;
_params->dispatchSize = 32;
_params->gravity = 0.141f;
_params->damping = 1.0f;
_params->particleIntensity = 0.64f;
_params->particleSize = 0.28f;
_params->baseSeed = 60;
_params->particlePower = 1.97f;
_params->initialSpeed = 69.7f;
_params->initialShape = 3;
_params->maxAccel = 3.21f;
break;
}
}
static const float s_quadVertices[] =
@ -129,8 +128,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Create static vertex buffer.
bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_quadVertices, sizeof(s_quadVertices) )
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_quadVertices, sizeof(s_quadVertices) )
, quadVertexDecl
);
@ -150,7 +149,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
.end();
const uint32_t threadGroupUpdateSize = 512;
const uint32_t maxParticleCount = 32 * 1024;
const uint32_t maxParticleCount = 32 * 1024;
bgfx::DynamicVertexBufferHandle currPositionBuffer0 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
bgfx::DynamicVertexBufferHandle currPositionBuffer1 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
@ -164,8 +163,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::ShaderHandle updateInstancesShader = loadShader("cs_update_instances");
bgfx::ProgramHandle updateInstancesProgram = bgfx::createProgram(updateInstancesShader, true);
u_paramsDataStruct u_paramsData;
InitializeParams(0, &u_paramsData);
u_paramsDataStruct u_paramsData;
InitializeParams(0, &u_paramsData);
bgfx::setUniform(u_params, &u_paramsData, 3);
bgfx::setBuffer(0, prevPositionBuffer0, bgfx::Access::Write);
@ -225,18 +224,18 @@ int _main_(int /*_argc*/, char** /*_argv*/)
imguiEndScrollArea();
imguiEndFrame();
// Modify parameters and reset if shape is changed
if (shape != u_paramsData.initialShape)
{
reset = true;
InitializeParams(shape, &u_paramsData);
}
// Modify parameters and reset if shape is changed
if (shape != u_paramsData.initialShape)
{
reset = true;
InitializeParams(shape, &u_paramsData);
}
if (reset)
{
bgfx::setBuffer(0, prevPositionBuffer0, bgfx::Access::Write);
bgfx::setBuffer(1, currPositionBuffer0, bgfx::Access::Write);
bgfx::setUniform(u_params, &u_paramsData, 3);
bgfx::setUniform(u_params, &u_paramsData, 3);
bgfx::dispatch(0, initInstancesProgram, maxParticleCount / threadGroupUpdateSize, 1, 1);
}