blob: dd6738214209ea666e6cc52491058b683ccd82a4 [file] [log] [blame]
Ben Claytond1232672021-05-18 09:24:18 +00001#include <metal_stdlib>
2
3using namespace metal;
Ben Claytonf47887d2022-06-24 17:01:59 +00004
5template<typename T, size_t N>
6struct tint_array {
7 const constant T& operator[](size_t i) const constant { return elements[i]; }
8 device T& operator[](size_t i) device { return elements[i]; }
9 const device T& operator[](size_t i) const device { return elements[i]; }
10 thread T& operator[](size_t i) thread { return elements[i]; }
11 const thread T& operator[](size_t i) const thread { return elements[i]; }
12 threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
13 const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
14 T elements[N];
15};
16
Ben Claytond1232672021-05-18 09:24:18 +000017struct tint_symbol_1 {
18 float2 a_particlePos [[attribute(0)]];
19 float2 a_particleVel [[attribute(1)]];
20 float2 a_pos [[attribute(2)]];
21};
Ben Clayton8ec32a62022-02-09 23:55:51 +000022
Ben Claytond1232672021-05-18 09:24:18 +000023struct tint_symbol_2 {
24 float4 value [[position]];
25};
Ben Claytond1232672021-05-18 09:24:18 +000026
James Pricea5d73ce2021-08-04 22:15:28 +000027float4 vert_main_inner(float2 a_particlePos, float2 a_particleVel, float2 a_pos) {
Ben Clayton7103f512021-10-20 16:12:33 +000028 float angle = -(atan2(a_particleVel[0], a_particleVel[1]));
29 float2 pos = float2(((a_pos[0] * cos(angle)) - (a_pos[1] * sin(angle))), ((a_pos[0] * sin(angle)) + (a_pos[1] * cos(angle))));
James Pricea5d73ce2021-08-04 22:15:28 +000030 return float4((pos + a_particlePos), 0.0f, 1.0f);
31}
32
33vertex tint_symbol_2 vert_main(tint_symbol_1 tint_symbol [[stage_in]]) {
34 float4 const inner_result = vert_main_inner(tint_symbol.a_particlePos, tint_symbol.a_particleVel, tint_symbol.a_pos);
35 tint_symbol_2 wrapper_result = {};
36 wrapper_result.value = inner_result;
37 return wrapper_result;
38}
39
Ben Clayton8ec32a62022-02-09 23:55:51 +000040struct tint_symbol_3 {
41 float4 value [[color(0)]];
42};
43
James Pricea5d73ce2021-08-04 22:15:28 +000044float4 frag_main_inner() {
Ben Claytoncb6ddd22022-06-01 10:08:29 +000045 return float4(1.0f);
Ben Claytond1232672021-05-18 09:24:18 +000046}
47
48fragment tint_symbol_3 frag_main() {
James Pricea5d73ce2021-08-04 22:15:28 +000049 float4 const inner_result_1 = frag_main_inner();
50 tint_symbol_3 wrapper_result_1 = {};
51 wrapper_result_1.value = inner_result_1;
52 return wrapper_result_1;
Ben Claytond1232672021-05-18 09:24:18 +000053}
54
Ben Clayton8ec32a62022-02-09 23:55:51 +000055struct Particle {
56 /* 0x0000 */ float2 pos;
57 /* 0x0008 */ float2 vel;
58};
59
60struct SimParams {
61 /* 0x0000 */ float deltaT;
62 /* 0x0004 */ float rule1Distance;
63 /* 0x0008 */ float rule2Distance;
64 /* 0x000c */ float rule3Distance;
65 /* 0x0010 */ float rule1Scale;
66 /* 0x0014 */ float rule2Scale;
67 /* 0x0018 */ float rule3Scale;
68};
69
Ben Clayton8ec32a62022-02-09 23:55:51 +000070struct Particles {
Ben Claytonf47887d2022-06-24 17:01:59 +000071 /* 0x0000 */ tint_array<Particle, 5> particles;
Ben Clayton8ec32a62022-02-09 23:55:51 +000072};
73
James Pricee548db92021-10-28 15:00:39 +000074void comp_main_inner(uint3 gl_GlobalInvocationID, device Particles* const tint_symbol_4, const constant SimParams* const tint_symbol_5, device Particles* const tint_symbol_6) {
Ben Clayton7103f512021-10-20 16:12:33 +000075 uint index = gl_GlobalInvocationID[0];
Ben Claytond1232672021-05-18 09:24:18 +000076 if ((index >= 5u)) {
77 return;
78 }
Ben Claytonf47887d2022-06-24 17:01:59 +000079 float2 vPos = (*(tint_symbol_4)).particles[index].pos;
80 float2 vVel = (*(tint_symbol_4)).particles[index].vel;
Ben Claytoncb6ddd22022-06-01 10:08:29 +000081 float2 cMass = float2(0.0f);
82 float2 cVel = float2(0.0f);
83 float2 colVel = float2(0.0f);
Ben Claytond1232672021-05-18 09:24:18 +000084 int cMassCount = 0;
85 int cVelCount = 0;
86 float2 pos = 0.0f;
87 float2 vel = 0.0f;
Ben Clayton1b03f0a2021-07-08 21:23:33 +000088 for(uint i = 0u; (i < 5u); i = (i + 1u)) {
89 if ((i == index)) {
90 continue;
91 }
James Price4d3af662023-02-27 20:21:03 +000092 pos = (*(tint_symbol_4)).particles[i].pos.xy;
93 vel = (*(tint_symbol_4)).particles[i].vel.xy;
James Pricee548db92021-10-28 15:00:39 +000094 if ((distance(pos, vPos) < (*(tint_symbol_5)).rule1Distance)) {
Ben Clayton1b03f0a2021-07-08 21:23:33 +000095 cMass = (cMass + pos);
Antonio Maioranod388bc92021-07-30 18:59:06 +000096 cMassCount = as_type<int>((as_type<uint>(cMassCount) + as_type<uint>(1)));
Ben Clayton1b03f0a2021-07-08 21:23:33 +000097 }
James Pricee548db92021-10-28 15:00:39 +000098 if ((distance(pos, vPos) < (*(tint_symbol_5)).rule2Distance)) {
Ben Clayton1b03f0a2021-07-08 21:23:33 +000099 colVel = (colVel - (pos - vPos));
100 }
James Pricee548db92021-10-28 15:00:39 +0000101 if ((distance(pos, vPos) < (*(tint_symbol_5)).rule3Distance)) {
Ben Clayton1b03f0a2021-07-08 21:23:33 +0000102 cVel = (cVel + vel);
Antonio Maioranod388bc92021-07-30 18:59:06 +0000103 cVelCount = as_type<int>((as_type<uint>(cVelCount) + as_type<uint>(1)));
Ben Claytond1232672021-05-18 09:24:18 +0000104 }
105 }
106 if ((cMassCount > 0)) {
107 cMass = ((cMass / float2(float(cMassCount), float(cMassCount))) - vPos);
108 }
109 if ((cVelCount > 0)) {
110 cVel = (cVel / float2(float(cVelCount), float(cVelCount)));
111 }
James Pricee548db92021-10-28 15:00:39 +0000112 vVel = (((vVel + (cMass * (*(tint_symbol_5)).rule1Scale)) + (colVel * (*(tint_symbol_5)).rule2Scale)) + (cVel * (*(tint_symbol_5)).rule3Scale));
dan sinclair6cc183c2023-03-02 21:28:45 +0000113 vVel = (normalize(vVel) * clamp(length(vVel), 0.0f, 0.10000000149011611938f));
James Pricee548db92021-10-28 15:00:39 +0000114 vPos = (vPos + (vVel * (*(tint_symbol_5)).deltaT));
Ben Clayton7103f512021-10-20 16:12:33 +0000115 if ((vPos[0] < -1.0f)) {
116 vPos[0] = 1.0f;
Ben Claytond1232672021-05-18 09:24:18 +0000117 }
Ben Clayton7103f512021-10-20 16:12:33 +0000118 if ((vPos[0] > 1.0f)) {
119 vPos[0] = -1.0f;
Ben Claytond1232672021-05-18 09:24:18 +0000120 }
Ben Clayton7103f512021-10-20 16:12:33 +0000121 if ((vPos[1] < -1.0f)) {
122 vPos[1] = 1.0f;
Ben Claytond1232672021-05-18 09:24:18 +0000123 }
Ben Clayton7103f512021-10-20 16:12:33 +0000124 if ((vPos[1] > 1.0f)) {
125 vPos[1] = -1.0f;
Ben Claytond1232672021-05-18 09:24:18 +0000126 }
Ben Claytonf47887d2022-06-24 17:01:59 +0000127 (*(tint_symbol_6)).particles[index].pos = vPos;
128 (*(tint_symbol_6)).particles[index].vel = vVel;
James Pricea5d73ce2021-08-04 22:15:28 +0000129}
130
James Pricee548db92021-10-28 15:00:39 +0000131kernel void comp_main(device Particles* tint_symbol_7 [[buffer(1)]], const constant SimParams* tint_symbol_8 [[buffer(0)]], device Particles* tint_symbol_9 [[buffer(2)]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]]) {
132 comp_main_inner(gl_GlobalInvocationID, tint_symbol_7, tint_symbol_8, tint_symbol_9);
Ben Claytond1232672021-05-18 09:24:18 +0000133 return;
134}
135