Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
particles
main.comp
Go to the documentation of this file.
1
#version 440
7
layout
(local_size_x = 128) in;
8
uniform
int
frame
;
9
#define BODY_COUNT 15000
10
#define MAX_NODES 45000
11
12
struct
Particle
13
{
14
vec4
p_m
;
15
vec4
mat
;
16
vec4
vel
;
17
vec4
acc
;
18
};
19
20
layout
(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
31
struct
MortonEntry
32
{
33
uint
pid
;
34
uint
morton
;
35
};
36
37
layout
(std430, binding=2) buffer
MortonData
{
38
39
MortonEntry
entry[];
40
};
41
42
layout
(std430, binding=3) buffer MortonPingData {
43
44
MortonEntry
exit[];
45
};
46
47
48
struct
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
60
layout
(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
};
74
layout
(std430, binding=6) buffer
NodeCounter
{
75
uint queue_out;
76
int
p_dead_cnt;
77
int
p_spawn_cnt;
78
uint star_pcnt;
79
};
80
const
float
THETA
= 0.7;
81
const
float
DT
= 0.1;
82
// uniform float DT;
83
// #define T2 (THETA * THETA)
84
const
float
T2
= (
THETA
*
THETA
);
85
// const float DT2 = DT / 2;
86
const
float
soft
= 1.5 * 1.5;
87
const
float
star_threshold
= 4.0;
88
const
float
acc_rate
= 0.3;
89
const
float
inv_acc_rate
= 0.7;
90
91
float
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
}
100
vec3
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
}
108
void
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
scale
float scale(float fCos)
Definition
atmosphere.vert:95
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
radius
const float radius
Definition
camera.c:580
main
int main(int argc, char *argv[])
Definition
main.c:1726
DT
float DT
Definition
main.c:231
mat
vec4 mat
Definition
main.c:261
frame
int frame
Definition
main.c:289
T2
const float T2
Definition
main.comp:84
acc_rate
const float acc_rate
Definition
main.comp:88
star_threshold
const float star_threshold
Definition
main.comp:87
rand_vec
vec3 rand_vec(uint id, uint f)
Definition
main.comp:100
THETA
const float THETA
Definition
main.comp:80
soft
const float soft
Definition
main.comp:86
hash
float hash(uint x)
Definition
main.comp:91
inv_acc_rate
const float inv_acc_rate
Definition
main.comp:89
MortonEntry::morton
uint morton
Definition
bounds.comp:32
Node::max
vec3 max
Definition
com22.comp:49
MortonEntry::pid
uint pid
Definition
bounds.comp:31
Node::com_m
vec4 com_m
Definition
com22.comp:45
Node::next
int next
Definition
com22.comp:50
Node::lrpd
ivec4 lrpd
Definition
com22.comp:52
Node::min
vec4 min
Definition
com22.comp:47
MortonEntry
Definition
bounds.comp:30
Node
Definition
types.h:109
s
bool s
Definition
menu.c:6
Particle::acc
vec4 acc
Definition
types.h:97
Particle::p_m
vec4 p_m
Definition
types.h:94
Particle::vel
vec3 vel
Definition
main.h:10
Particle
Definition
main.h:8
vec4::w
float w
Definition
types.h:47
vec4::z
float z
Definition
types.h:47
vec4::x
float x
Definition
types.h:47
MAX_DEPTH
#define MAX_DEPTH
Definition
types.h:154
BODY_COUNT
#define BODY_COUNT
Definition
types.h:39
vec4::y
float y
Definition
types.h:47
MAX_NODES
#define MAX_NODES
Definition
types.h:40
MortonData
Definition
types.h:140
NodeCounter
Definition
types.h:204
NodeData
Definition
types.h:156
ParticleData
Definition
types.h:129
vec3
Definition
main.h:5
vec4
Definition
types.h:47
Generated by
1.18.0