blob: e8515a2205f7d0c2d3f18b0b5d5cd1bd5faa455f [file] [log] [blame]
dan sinclair42e873c2021-01-12 18:13:28 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Dan Sinclair6e581892020-03-02 15:47:43 -050014
dan sinclair42e873c2021-01-12 18:13:28 +000015// vertex shader
Dan Sinclair6e581892020-03-02 15:47:43 -050016
dan sinclairb29892b2022-06-07 13:55:34 +000017@vertex
Ben Clayton01e4b6f2022-01-19 22:46:57 +000018fn vert_main(@location(0) a_particlePos : vec2<f32>,
19 @location(1) a_particleVel : vec2<f32>,
20 @location(2) a_pos : vec2<f32>)
21 -> @builtin(position) vec4<f32> {
dan sinclairb4fee2f2020-09-22 19:42:13 +000022 var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y);
Dan Sinclair6e581892020-03-02 15:47:43 -050023 var pos : vec2<f32> = vec2<f32>(
dan sinclairb4fee2f2020-09-22 19:42:13 +000024 (a_pos.x * cos(angle)) - (a_pos.y * sin(angle)),
25 (a_pos.x * sin(angle)) + (a_pos.y * cos(angle)));
James Pricea2239c62021-04-06 18:08:47 +000026 return vec4<f32>(pos + a_particlePos, 0.0, 1.0);
Dan Sinclair6e581892020-03-02 15:47:43 -050027}
Dan Sinclair6e581892020-03-02 15:47:43 -050028
dan sinclair42e873c2021-01-12 18:13:28 +000029// fragment shader
Dan Sinclair6e581892020-03-02 15:47:43 -050030
dan sinclairb29892b2022-06-07 13:55:34 +000031@fragment
Ben Clayton01e4b6f2022-01-19 22:46:57 +000032fn frag_main() -> @location(0) vec4<f32> {
James Pricea2239c62021-04-06 18:08:47 +000033 return vec4<f32>(1.0, 1.0, 1.0, 1.0);
Dan Sinclair6e581892020-03-02 15:47:43 -050034}
Dan Sinclair6e581892020-03-02 15:47:43 -050035
dan sinclair42e873c2021-01-12 18:13:28 +000036// compute shader
Ben Clayton8db81882021-04-21 16:45:02 +000037struct Particle {
James Price3b671cb2022-03-28 14:31:22 +000038 pos : vec2<f32>,
39 vel : vec2<f32>,
Dan Sinclair6e581892020-03-02 15:47:43 -050040};
41
James Pricea5d39862021-12-09 15:45:03 +000042 struct SimParams {
James Price3b671cb2022-03-28 14:31:22 +000043 deltaT : f32,
44 rule1Distance : f32,
45 rule2Distance : f32,
46 rule3Distance : f32,
47 rule1Scale : f32,
48 rule2Scale : f32,
49 rule3Scale : f32,
Dan Sinclair6e581892020-03-02 15:47:43 -050050};
51
James Pricea5d39862021-12-09 15:45:03 +000052 struct Particles {
James Price3b671cb2022-03-28 14:31:22 +000053 particles : array<Particle, 5>,
Dan Sinclair6e581892020-03-02 15:47:43 -050054};
55
Ben Clayton01e4b6f2022-01-19 22:46:57 +000056@binding(0) @group(0) var<uniform> params : SimParams;
57@binding(1) @group(0) var<storage, read_write> particlesA : Particles;
58@binding(2) @group(0) var<storage, read_write> particlesB : Particles;
Dan Sinclair6e581892020-03-02 15:47:43 -050059
dan sinclair42e873c2021-01-12 18:13:28 +000060// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
dan sinclairb29892b2022-06-07 13:55:34 +000061@compute @workgroup_size(1)
James Pricea2239c62021-04-06 18:08:47 +000062fn comp_main(
Ben Clayton01e4b6f2022-01-19 22:46:57 +000063 @builtin(global_invocation_id) gl_GlobalInvocationID : vec3<u32>) {
Dan Sinclair6e581892020-03-02 15:47:43 -050064 var index : u32 = gl_GlobalInvocationID.x;
dan sinclaird7abd762020-09-23 14:40:54 +000065 if (index >= 5u) {
Dan Sinclair6e581892020-03-02 15:47:43 -050066 return;
67 }
68
69 var vPos : vec2<f32> = particlesA.particles[index].pos;
70 var vVel : vec2<f32> = particlesA.particles[index].vel;
71
David Netoac478072020-05-01 19:02:35 +000072 var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
73 var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
74 var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
Dan Sinclair6e581892020-03-02 15:47:43 -050075 var cMassCount : i32 = 0;
76 var cVelCount : i32 = 0;
77
78 var pos : vec2<f32>;
79 var vel : vec2<f32>;
Ben Clayton7862da92021-03-17 18:43:53 +000080 for(var i : u32 = 0u; i < 5u; i = i + 1u) {
Dan Sinclair6e581892020-03-02 15:47:43 -050081 if (i == index) {
82 continue;
83 }
84
85 pos = particlesA.particles[i].pos.xy;
86 vel = particlesA.particles[i].vel.xy;
87
dan sinclairb4fee2f2020-09-22 19:42:13 +000088 if (distance(pos, vPos) < params.rule1Distance) {
Dan Sinclair6e581892020-03-02 15:47:43 -050089 cMass = cMass + pos;
90 cMassCount = cMassCount + 1;
91 }
dan sinclairb4fee2f2020-09-22 19:42:13 +000092 if (distance(pos, vPos) < params.rule2Distance) {
Dan Sinclair6e581892020-03-02 15:47:43 -050093 colVel = colVel - (pos - vPos);
94 }
dan sinclairb4fee2f2020-09-22 19:42:13 +000095 if (distance(pos, vPos) < params.rule3Distance) {
Dan Sinclair6e581892020-03-02 15:47:43 -050096 cVel = cVel + vel;
97 cVelCount = cVelCount + 1;
98 }
Dan Sinclair6e581892020-03-02 15:47:43 -050099 }
100 if (cMassCount > 0) {
dan sinclair64db1d92020-05-01 19:05:43 +0000101 cMass =
dan sinclaird2d7ce62020-09-30 14:00:10 +0000102 (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
Dan Sinclair6e581892020-03-02 15:47:43 -0500103 }
104 if (cVelCount > 0) {
dan sinclaird2d7ce62020-09-30 14:00:10 +0000105 cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount));
Dan Sinclair6e581892020-03-02 15:47:43 -0500106 }
107
108 vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) +
109 (cVel * params.rule3Scale);
110
dan sinclair42e873c2021-01-12 18:13:28 +0000111 // clamp velocity for a more pleasing simulation
dan sinclairb4fee2f2020-09-22 19:42:13 +0000112 vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
Dan Sinclair6e581892020-03-02 15:47:43 -0500113
dan sinclair42e873c2021-01-12 18:13:28 +0000114 // kinematic update
Dan Sinclair6e581892020-03-02 15:47:43 -0500115 vPos = vPos + (vVel * params.deltaT);
116
dan sinclair42e873c2021-01-12 18:13:28 +0000117 // Wrap around boundary
Dan Sinclair6e581892020-03-02 15:47:43 -0500118 if (vPos.x < -1.0) {
119 vPos.x = 1.0;
120 }
121 if (vPos.x > 1.0) {
122 vPos.x = -1.0;
123 }
124 if (vPos.y < -1.0) {
125 vPos.y = 1.0;
126 }
127 if (vPos.y > 1.0) {
128 vPos.y = -1.0;
129 }
130
dan sinclair42e873c2021-01-12 18:13:28 +0000131 // Write back
Dan Sinclair6e581892020-03-02 15:47:43 -0500132 particlesB.particles[index].pos = vPos;
133 particlesB.particles[index].vel = vVel;
Dan Sinclair6e581892020-03-02 15:47:43 -0500134}