2023-09-18 18:11:07 +00:00
|
|
|
// language: metal1.0
|
2020-12-11 05:50:36 +00:00
|
|
|
#include <metal_stdlib>
|
|
|
|
#include <simd/simd.h>
|
|
|
|
|
2022-02-21 21:22:54 +00:00
|
|
|
using metal::uint;
|
|
|
|
|
2021-05-01 02:40:32 +00:00
|
|
|
struct _mslBufferSizes {
|
2022-02-21 21:22:54 +00:00
|
|
|
uint size1;
|
|
|
|
uint size2;
|
2021-05-01 02:40:32 +00:00
|
|
|
};
|
|
|
|
|
2020-12-11 05:50:36 +00:00
|
|
|
struct Particle {
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::float2 pos;
|
|
|
|
metal::float2 vel;
|
2020-12-11 05:50:36 +00:00
|
|
|
};
|
|
|
|
struct SimParams {
|
2021-04-08 15:00:11 +00:00
|
|
|
float deltaT;
|
|
|
|
float rule1Distance;
|
|
|
|
float rule2Distance;
|
|
|
|
float rule3Distance;
|
|
|
|
float rule1Scale;
|
|
|
|
float rule2Scale;
|
|
|
|
float rule3Scale;
|
2020-12-11 05:50:36 +00:00
|
|
|
};
|
2021-11-09 14:24:41 +00:00
|
|
|
typedef Particle type_3[1];
|
2020-12-11 05:50:36 +00:00
|
|
|
struct Particles {
|
2021-11-09 14:24:41 +00:00
|
|
|
type_3 particles;
|
2020-12-11 05:50:36 +00:00
|
|
|
};
|
2023-04-05 15:38:03 +00:00
|
|
|
constant uint NUM_PARTICLES = 1500u;
|
2021-04-08 15:00:11 +00:00
|
|
|
|
2021-11-09 14:24:41 +00:00
|
|
|
struct main_Input {
|
2021-03-07 05:03:05 +00:00
|
|
|
};
|
2021-11-09 14:24:41 +00:00
|
|
|
kernel void main_(
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::uint3 global_invocation_id [[thread_position_in_grid]]
|
2021-03-07 05:03:05 +00:00
|
|
|
, constant SimParams& params [[buffer(0)]]
|
2022-03-06 17:08:40 +00:00
|
|
|
, device Particles const& particlesSrc [[buffer(1)]]
|
2021-03-07 05:03:05 +00:00
|
|
|
, device Particles& particlesDst [[buffer(2)]]
|
2021-05-04 01:51:44 +00:00
|
|
|
, constant _mslBufferSizes& _buffer_sizes [[buffer(3)]]
|
2020-12-11 05:50:36 +00:00
|
|
|
) {
|
2022-04-29 19:31:06 +00:00
|
|
|
metal::float2 vPos = {};
|
|
|
|
metal::float2 vVel = {};
|
2023-04-19 13:16:46 +00:00
|
|
|
metal::float2 cMass = metal::float2(0.0, 0.0);
|
|
|
|
metal::float2 cVel = metal::float2(0.0, 0.0);
|
|
|
|
metal::float2 colVel = metal::float2(0.0, 0.0);
|
|
|
|
int cMassCount = 0;
|
|
|
|
int cVelCount = 0;
|
2022-04-29 19:31:06 +00:00
|
|
|
metal::float2 pos = {};
|
|
|
|
metal::float2 vel = {};
|
2023-04-19 13:16:46 +00:00
|
|
|
uint i = 0u;
|
2022-02-21 21:22:54 +00:00
|
|
|
uint index = global_invocation_id.x;
|
2021-06-08 16:09:46 +00:00
|
|
|
if (index >= NUM_PARTICLES) {
|
2021-03-07 05:03:05 +00:00
|
|
|
return;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e8 = particlesSrc.particles[index].pos;
|
|
|
|
vPos = _e8;
|
|
|
|
metal::float2 _e14 = particlesSrc.particles[index].vel;
|
|
|
|
vVel = _e14;
|
2024-09-16 19:56:58 +00:00
|
|
|
#define LOOP_IS_REACHABLE if (volatile bool unpredictable_jump_over_loop = true; unpredictable_jump_over_loop)
|
2021-01-30 04:51:39 +00:00
|
|
|
bool loop_init = true;
|
2024-09-16 19:56:58 +00:00
|
|
|
LOOP_IS_REACHABLE while(true) {
|
2021-01-30 04:51:39 +00:00
|
|
|
if (!loop_init) {
|
2023-01-12 17:26:28 +00:00
|
|
|
uint _e91 = i;
|
|
|
|
i = _e91 + 1u;
|
2021-01-30 04:51:39 +00:00
|
|
|
}
|
|
|
|
loop_init = false;
|
2023-01-12 17:26:28 +00:00
|
|
|
uint _e36 = i;
|
|
|
|
if (_e36 >= NUM_PARTICLES) {
|
2021-01-26 00:00:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-21 21:22:54 +00:00
|
|
|
uint _e39 = i;
|
2021-06-08 16:09:46 +00:00
|
|
|
if (_e39 == index) {
|
2021-01-26 00:00:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
uint _e43 = i;
|
|
|
|
metal::float2 _e46 = particlesSrc.particles[_e43].pos;
|
|
|
|
pos = _e46;
|
|
|
|
uint _e49 = i;
|
|
|
|
metal::float2 _e52 = particlesSrc.particles[_e49].vel;
|
|
|
|
vel = _e52;
|
|
|
|
metal::float2 _e53 = pos;
|
|
|
|
metal::float2 _e54 = vPos;
|
|
|
|
float _e58 = params.rule1Distance;
|
|
|
|
if (metal::distance(_e53, _e54) < _e58) {
|
|
|
|
metal::float2 _e60 = cMass;
|
|
|
|
metal::float2 _e61 = pos;
|
|
|
|
cMass = _e60 + _e61;
|
|
|
|
int _e63 = cMassCount;
|
|
|
|
cMassCount = _e63 + 1;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e66 = pos;
|
|
|
|
metal::float2 _e67 = vPos;
|
|
|
|
float _e71 = params.rule2Distance;
|
|
|
|
if (metal::distance(_e66, _e67) < _e71) {
|
|
|
|
metal::float2 _e73 = colVel;
|
|
|
|
metal::float2 _e74 = pos;
|
|
|
|
metal::float2 _e75 = vPos;
|
|
|
|
colVel = _e73 - (_e74 - _e75);
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e78 = pos;
|
|
|
|
metal::float2 _e79 = vPos;
|
|
|
|
float _e83 = params.rule3Distance;
|
|
|
|
if (metal::distance(_e78, _e79) < _e83) {
|
|
|
|
metal::float2 _e85 = cVel;
|
|
|
|
metal::float2 _e86 = vel;
|
|
|
|
cVel = _e85 + _e86;
|
|
|
|
int _e88 = cVelCount;
|
|
|
|
cVelCount = _e88 + 1;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
int _e94 = cMassCount;
|
|
|
|
if (_e94 > 0) {
|
|
|
|
metal::float2 _e97 = cMass;
|
|
|
|
int _e98 = cMassCount;
|
|
|
|
metal::float2 _e102 = vPos;
|
|
|
|
cMass = (_e97 / metal::float2(static_cast<float>(_e98))) - _e102;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
int _e104 = cVelCount;
|
|
|
|
if (_e104 > 0) {
|
|
|
|
metal::float2 _e107 = cVel;
|
|
|
|
int _e108 = cVelCount;
|
|
|
|
cVel = _e107 / metal::float2(static_cast<float>(_e108));
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e112 = vVel;
|
|
|
|
metal::float2 _e113 = cMass;
|
|
|
|
float _e116 = params.rule1Scale;
|
|
|
|
metal::float2 _e119 = colVel;
|
|
|
|
float _e122 = params.rule2Scale;
|
|
|
|
metal::float2 _e125 = cVel;
|
|
|
|
float _e128 = params.rule3Scale;
|
|
|
|
vVel = ((_e112 + (_e113 * _e116)) + (_e119 * _e122)) + (_e125 * _e128);
|
|
|
|
metal::float2 _e131 = vVel;
|
|
|
|
metal::float2 _e133 = vVel;
|
2023-05-11 05:23:10 +00:00
|
|
|
vVel = metal::normalize(_e131) * metal::clamp(metal::length(_e133), 0.0, 0.1);
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e139 = vPos;
|
|
|
|
metal::float2 _e140 = vVel;
|
|
|
|
float _e143 = params.deltaT;
|
|
|
|
vPos = _e139 + (_e140 * _e143);
|
|
|
|
float _e147 = vPos.x;
|
|
|
|
if (_e147 < -1.0) {
|
2021-04-08 16:29:26 +00:00
|
|
|
vPos.x = 1.0;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
float _e153 = vPos.x;
|
|
|
|
if (_e153 > 1.0) {
|
2021-04-08 16:29:26 +00:00
|
|
|
vPos.x = -1.0;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
float _e159 = vPos.y;
|
|
|
|
if (_e159 < -1.0) {
|
2021-04-08 16:29:26 +00:00
|
|
|
vPos.y = 1.0;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
float _e165 = vPos.y;
|
|
|
|
if (_e165 > 1.0) {
|
2021-04-08 16:29:26 +00:00
|
|
|
vPos.y = -1.0;
|
2021-01-26 00:00:23 +00:00
|
|
|
}
|
2023-01-12 17:26:28 +00:00
|
|
|
metal::float2 _e174 = vPos;
|
|
|
|
particlesDst.particles[index].pos = _e174;
|
|
|
|
metal::float2 _e179 = vVel;
|
|
|
|
particlesDst.particles[index].vel = _e179;
|
2021-03-07 05:03:05 +00:00
|
|
|
return;
|
2020-12-11 05:50:36 +00:00
|
|
|
}
|