Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 1 | #include <metal_stdlib> |
| 2 | |
| 3 | using namespace metal; |
Ben Clayton | f47887d | 2022-06-24 17:01:59 +0000 | [diff] [blame] | 4 | |
| 5 | template<typename T, size_t N> |
| 6 | struct 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 Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 17 | struct tint_symbol_1 { |
| 18 | float2 a_particlePos [[attribute(0)]]; |
| 19 | float2 a_particleVel [[attribute(1)]]; |
| 20 | float2 a_pos [[attribute(2)]]; |
| 21 | }; |
Ben Clayton | 8ec32a6 | 2022-02-09 23:55:51 +0000 | [diff] [blame] | 22 | |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 23 | struct tint_symbol_2 { |
| 24 | float4 value [[position]]; |
| 25 | }; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 26 | |
James Price | a5d73ce | 2021-08-04 22:15:28 +0000 | [diff] [blame] | 27 | float4 vert_main_inner(float2 a_particlePos, float2 a_particleVel, float2 a_pos) { |
Ben Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 28 | 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 Price | a5d73ce | 2021-08-04 22:15:28 +0000 | [diff] [blame] | 30 | return float4((pos + a_particlePos), 0.0f, 1.0f); |
| 31 | } |
| 32 | |
| 33 | vertex 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 Clayton | 8ec32a6 | 2022-02-09 23:55:51 +0000 | [diff] [blame] | 40 | struct tint_symbol_3 { |
| 41 | float4 value [[color(0)]]; |
| 42 | }; |
| 43 | |
James Price | a5d73ce | 2021-08-04 22:15:28 +0000 | [diff] [blame] | 44 | float4 frag_main_inner() { |
Ben Clayton | cb6ddd2 | 2022-06-01 10:08:29 +0000 | [diff] [blame] | 45 | return float4(1.0f); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | fragment tint_symbol_3 frag_main() { |
James Price | a5d73ce | 2021-08-04 22:15:28 +0000 | [diff] [blame] | 49 | 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 Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ben Clayton | 8ec32a6 | 2022-02-09 23:55:51 +0000 | [diff] [blame] | 55 | struct Particle { |
| 56 | /* 0x0000 */ float2 pos; |
| 57 | /* 0x0008 */ float2 vel; |
| 58 | }; |
| 59 | |
| 60 | struct 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 Clayton | 8ec32a6 | 2022-02-09 23:55:51 +0000 | [diff] [blame] | 70 | struct Particles { |
Ben Clayton | f47887d | 2022-06-24 17:01:59 +0000 | [diff] [blame] | 71 | /* 0x0000 */ tint_array<Particle, 5> particles; |
Ben Clayton | 8ec32a6 | 2022-02-09 23:55:51 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 74 | void 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 Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 75 | uint index = gl_GlobalInvocationID[0]; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 76 | if ((index >= 5u)) { |
| 77 | return; |
| 78 | } |
Ben Clayton | f47887d | 2022-06-24 17:01:59 +0000 | [diff] [blame] | 79 | float2 vPos = (*(tint_symbol_4)).particles[index].pos; |
| 80 | float2 vVel = (*(tint_symbol_4)).particles[index].vel; |
Ben Clayton | cb6ddd2 | 2022-06-01 10:08:29 +0000 | [diff] [blame] | 81 | float2 cMass = float2(0.0f); |
| 82 | float2 cVel = float2(0.0f); |
| 83 | float2 colVel = float2(0.0f); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 84 | int cMassCount = 0; |
| 85 | int cVelCount = 0; |
| 86 | float2 pos = 0.0f; |
| 87 | float2 vel = 0.0f; |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 88 | for(uint i = 0u; (i < 5u); i = (i + 1u)) { |
| 89 | if ((i == index)) { |
| 90 | continue; |
| 91 | } |
James Price | 4d3af66 | 2023-02-27 20:21:03 +0000 | [diff] [blame] | 92 | pos = (*(tint_symbol_4)).particles[i].pos.xy; |
| 93 | vel = (*(tint_symbol_4)).particles[i].vel.xy; |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 94 | if ((distance(pos, vPos) < (*(tint_symbol_5)).rule1Distance)) { |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 95 | cMass = (cMass + pos); |
Antonio Maiorano | d388bc9 | 2021-07-30 18:59:06 +0000 | [diff] [blame] | 96 | cMassCount = as_type<int>((as_type<uint>(cMassCount) + as_type<uint>(1))); |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 97 | } |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 98 | if ((distance(pos, vPos) < (*(tint_symbol_5)).rule2Distance)) { |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 99 | colVel = (colVel - (pos - vPos)); |
| 100 | } |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 101 | if ((distance(pos, vPos) < (*(tint_symbol_5)).rule3Distance)) { |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 102 | cVel = (cVel + vel); |
Antonio Maiorano | d388bc9 | 2021-07-30 18:59:06 +0000 | [diff] [blame] | 103 | cVelCount = as_type<int>((as_type<uint>(cVelCount) + as_type<uint>(1))); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 104 | } |
| 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 Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 112 | vVel = (((vVel + (cMass * (*(tint_symbol_5)).rule1Scale)) + (colVel * (*(tint_symbol_5)).rule2Scale)) + (cVel * (*(tint_symbol_5)).rule3Scale)); |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 113 | vVel = (normalize(vVel) * clamp(length(vVel), 0.0f, 0.10000000149011611938f)); |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 114 | vPos = (vPos + (vVel * (*(tint_symbol_5)).deltaT)); |
Ben Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 115 | if ((vPos[0] < -1.0f)) { |
| 116 | vPos[0] = 1.0f; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 117 | } |
Ben Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 118 | if ((vPos[0] > 1.0f)) { |
| 119 | vPos[0] = -1.0f; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 120 | } |
Ben Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 121 | if ((vPos[1] < -1.0f)) { |
| 122 | vPos[1] = 1.0f; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 123 | } |
Ben Clayton | 7103f51 | 2021-10-20 16:12:33 +0000 | [diff] [blame] | 124 | if ((vPos[1] > 1.0f)) { |
| 125 | vPos[1] = -1.0f; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 126 | } |
Ben Clayton | f47887d | 2022-06-24 17:01:59 +0000 | [diff] [blame] | 127 | (*(tint_symbol_6)).particles[index].pos = vPos; |
| 128 | (*(tint_symbol_6)).particles[index].vel = vVel; |
James Price | a5d73ce | 2021-08-04 22:15:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
James Price | e548db9 | 2021-10-28 15:00:39 +0000 | [diff] [blame] | 131 | kernel 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 Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |