Space Sim
Loading...
Searching...
No Matches
main.comp
Go to the documentation of this file.
1#version 440
7layout(local_size_x = 128) in;
8uniform int frame;
9#define BODY_COUNT 15000
10#define MAX_NODES 45000
11
12struct Particle
13{
14 vec4 p_m;
15 vec4 mat;
16 vec4 vel;
17 vec4 acc;
18};
19
20layout(std430, binding=1) buffer ParticleData {
21
22 Particle p_spawn[BODY_COUNT];
23 vec4 p_colors[BODY_COUNT];
24 uint p_dead[BODY_COUNT];
25 uint claimed[BODY_COUNT];
26 int nodes_visited[BODY_COUNT];
27 uint star_particles[BODY_COUNT];
28 Particle p[];
29};
30
31struct MortonEntry
32{
33 uint pid;
34 uint morton;
35};
36
37layout(std430, binding=2) buffer MortonData {
38
39 MortonEntry entry[];
40};
41
42layout(std430, binding=3) buffer MortonPingData {
43
44 MortonEntry exit[];
45};
46
47
48struct Node {
49 vec4 com_m;
50 // min and size
51 vec4 min;
52 // max and next
53 vec3 max;
54 int next;
55 // left child, right child, parent, depth
56 ivec4 lrpd;
57 // ivec4 next;
58};
59#define MAX_DEPTH 64
60layout(std430, binding=4) buffer NodeData {
61 uint ready_queue[MAX_NODES];
62 uint children_remaining[MAX_NODES];
63 uint depth_offsets[MAX_DEPTH+1];
64 uint pad0;
65 uint pad1;
66 uint pad2;
67 vec4 com_m[MAX_NODES];
68 vec4 lrsn[MAX_NODES];
69 // min and parent
70 vec4 nmin[MAX_NODES];
71 // max and depth
72 vec4 nmax[MAX_NODES];
73};
74layout(std430, binding=6) buffer NodeCounter {
75 uint queue_out;
76 int p_dead_cnt;
77 int p_spawn_cnt;
78 uint star_pcnt;
79};
80const float THETA = 0.7;
81const float DT = 0.1;
82// uniform float DT;
83// #define T2 (THETA * THETA)
84const float T2 = (THETA * THETA);
85// const float DT2 = DT / 2;
86const float soft = 1.5 * 1.5;
87const float star_threshold = 4.0;
88const float acc_rate = 0.3;
89const float inv_acc_rate = 0.7;
90
91float hash(uint x)
92{
93 x ^= x >> 16;
94 x *= 0x7feb352d;
95 x ^= x >> 15;
96 x *= 0x846ca68b;
97 x ^= x >> 16;
98 return float(x) / float(0xffffffffu);
99}
100vec3 rand_vec(uint id, uint f)
101{
102 float x = hash(id * 73856093u ^ f);
103 float y = hash(id * 19349663u ^ f);
104 float z = hash(id * 83492791u ^ f);
105
106 return vec3(x,y,z) * 2.0 - 1.0;
107}
108void main()
109{
110 uint i = gl_GlobalInvocationID.x;
111 if (i >= BODY_COUNT) return;
112
113
114 uint ei = entry[i].pid;
115 Particle e = p[ei];
116 vec4 p_m = e.p_m;
117 if (p_m.w == 0) return;
118
119
120 vec4 a = vec4(0, 0, 0, 0);
121 float scale = 1.0;
122 float radius = pow(e.p_m.w, 1.0/3.0) * scale;
123
124 int ni = BODY_COUNT;
125 int maximum = 1000000;
126 int counter = 0;
127 while (ni >= 0)
128 {
129 counter++;
130 if (counter >= maximum) {
131 break;
132 }
133
134 vec4 com = com_m[ni];
135 vec4 lr = lrsn[ni];
136 int n = floatBitsToInt(lr.w);
137 if (com.w == 0)
138 {
139
140 ni = n;
141 continue;
142 }
143
144 vec4 r = com - p_m;
145 float dist2 = (r.x * r.x + r.y * r.y + r.z * r.z + soft);
146
147
148 float s = lr.z;
149 if (ni < BODY_COUNT)
150 {
151 uint j = entry[ni].pid;
152 if (ei == j) {
153
154 ni = n;
155 continue;
156 }
157 Particle je = p[j];
158 float r2 = pow(je.p_m.w, 1.0/3.0) * scale;
159 float rad = r2 + radius;
160 // if (dist2 - soft < rad * rad && ei < j && je.p_m.w > 0)
161 // if (dist2 - soft < rad * rad && ei < j && je.p_m.w > 0 && p[ei].p_m.w > je.p_m.w)
162 // {
163 // if (p[ei].p_m.w == 0) return;
164 // if (atomicExchange(claimed[j], 1) == 0 && atomicExchange(claimed[ei], 1) == 0)
165 // {
166 // p[j].p_m.w = max(p[j].p_m.w - p[j].p_m.w * acc_rate, 0);
167 // // Particle je = p[j];
168 // // p[j].p_m.w = 0.0;
169 // float mi = p_m.w;
170 // float mj = p[j].p_m.w;
171 // float mij = mi + mj;
172 // vec3 rel_vel = p[ei].vel.xyz - je.vel.xyz;
173 // // vec3 normal = normalize(p[ei].p_m.xyz - je.p_m.xyz);
174 // // vec3 tangent = normalize(rel_vel - dot(rel_vel, normal) * normal);
175 // // float tl = length(tangent);
176
177 // // if (tl < 1e-5)
178 // // {
179 // // tangent = normalize(cross(normal, vec3(0.73,0.42,0.19)));
180 // // }
181 // // else
182 // // {
183 // // tangent /= tl;
184 // // }
185 // // p[ei].vel = (mi * e.vel + mj * je.vel) / (mij);
186 // // p[ei].mat = (mi * e.mat + mj * je.mat) / (mij);
187 // // p[ei].p_m.w += mj;
188 // p[ei].vel = p[ei].vel * inv_acc_rate + ((mi * e.vel + mj * je.vel) / (mij)) * acc_rate;
189 // p[ei].mat = p[ei].mat * inv_acc_rate + ((mi * e.mat + mj * je.mat) / (mij)) * acc_rate;
190 // p[ei].mat = normalize(p[ei].mat + 1e-6);
191 // p[ei].p_m.w += mj * acc_rate;
192 // // int pcnt = 4;
193 // // Particle new = p[j];
194 // // new.p_m.w /= 50;
195 // // float nr = pow(new.p_m.w, 1.0/3.0) * scale;
196 // // new.p_m.xyz += normal.xyz * (nr * 1.5);
197 // // // new.vel.xyz = normal * 0.001 + tangent * 0.005;
198 // // int left = atomicAdd(p_spawn_cnt, pcnt);
199 // // for (int i = 0; i < pcnt; i++)
200 // // {
201 // // vec3 jitter = vec3(
202 // // fract(sin(float(left+i)*12.9898)*43758.5453),
203 // // fract(sin(float(left+i)*78.233)*43758.5453),
204 // // fract(sin(float(left+i)*39.425)*43758.5453)
205 // // );
206
207 // // jitter = (jitter - 0.5) * nr * 1.0;
208
209 // // new.vel.xyz = (normal * 0.004 + tangent * 0.003) * jitter;
210 // // new.p_m.xyz += jitter;
211 // // if (left + i < BODY_COUNT){
212 // // p_spawn[left + i] = new;
213 // // }
214 // // }
215 // ni = n;
216 // continue;
217
218
219 // }
220 // }
221 float inv = inversesqrt(dist2);
222 float inv3 = inv * inv * inv;
223 float scale = 1.0f * com.w * inv3;
224
225 r *= scale;
226 a += r;
227 ni = n;
228 continue;
229
230 }
231 if ((s / dist2) < T2)
232 {
233 float inv = inversesqrt(dist2);
234 float inv3 = inv * inv * inv;
235 float scale = 1.0f * com.w * inv3;
236
237 r *= scale;
238 a += r;
239 ni = n;
240 }
241 else
242 {
243 ni = floatBitsToInt(lr.x);
244 }
245 }
246
247 a *= DT;
248 p[ei].vel.xyz += a.xyz;
249
250
251}
252
float scale(float fCos)
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
const float radius
Definition camera.c:580
int main(int argc, char *argv[])
Definition main.c:1726
float DT
Definition main.c:231
vec4 mat
Definition main.c:261
int frame
Definition main.c:289
const float T2
Definition main.comp:84
const float acc_rate
Definition main.comp:88
const float star_threshold
Definition main.comp:87
vec3 rand_vec(uint id, uint f)
Definition main.comp:100
const float THETA
Definition main.comp:80
const float soft
Definition main.comp:86
float hash(uint x)
Definition main.comp:91
const float inv_acc_rate
Definition main.comp:89
uint morton
Definition bounds.comp:32
vec3 max
Definition com22.comp:49
vec4 com_m
Definition com22.comp:45
int next
Definition com22.comp:50
ivec4 lrpd
Definition com22.comp:52
vec4 min
Definition com22.comp:47
Definition bounds.comp:30
Definition types.h:109
bool s
Definition menu.c:6
vec4 acc
Definition types.h:97
vec4 p_m
Definition types.h:94
vec3 vel
Definition main.h:10
Definition main.h:8
float w
Definition types.h:47
float z
Definition types.h:47
float x
Definition types.h:47
#define MAX_DEPTH
Definition types.h:154
#define BODY_COUNT
Definition types.h:39
float y
Definition types.h:47
#define MAX_NODES
Definition types.h:40
Definition main.h:5
Definition types.h:47