-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
419 lines (330 loc) · 10.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include "Bigged/bigged.cpp"
#include "entt.hpp"
#include <chrono>
#include <cmath>
#include <execution>
#include <tuple>
#define TIME_HERE std::chrono::high_resolution_clock::now();
#define ELAPSEDuS(time_point) (uint)(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-time_point).count());
using namespace entt;
class FrustumTest : public TestBed {
DefaultRegistry registry;
virtual void Init() override;
virtual void Update(float dt) override;
virtual void Shutdown() override;
};
struct AABB
{
vec3 min, max;
AABB(vec3 min, vec3 max) : min(min), max(max) {}
};
struct Transform
{
vec3 scalexyz;
quat rotation;
vec3 position;
void Translate(vec3 local) {
position += rotation * local;
}
void Rotate(quat dr) {
rotation = dr * rotation;
}
mat4 GetMatrix() {
auto m = mat4(rotation);
m = scale(m, scalexyz);
return translate(m, position);
}
void DrawDebug() {
TestBed::DrawArrow(position, position + rotation * vec3(1, 0, 0), col32::red);
TestBed::DrawArrow(position, position + rotation * vec3(0, 1, 0), col32::green);
TestBed::DrawArrow(position, position + rotation * vec3(0, 0, 1), col32::blue);
TestBed::DrawOBB(GetMatrix(), col32::purple, true);
float sphereRad = length(scalexyz);
TestBed::DrawSphere(position, sphereRad, col32(255,255,255,40), 2);
}
};
struct BSphere
{
//vec3 localPosition; TODO
__m128 simdCacheX;
__m128 simdCacheY;
__m128 simdCacheZ;
__m128 simdCacheR;
float radius;
void UpdateCaches(Transform t)
{
simdCacheX = _mm_set_ps1(t.position.x);
simdCacheY = _mm_set_ps1(t.position.y);
simdCacheZ = _mm_set_ps1(t.position.z);
simdCacheR = _mm_set_ps1(-radius);
}
std::tuple<vec3, float> GetCachedDataSlow()
{
return {
vec3(
simdCacheX.m128_f32[0],
simdCacheY.m128_f32[0],
simdCacheZ.m128_f32[0]
),
radius
};
}
};
struct Velocity {
vec3 linear;
vec3 angular;
void ApplyToTransform(Transform& t, float dt) {
t.Rotate(quat(angular*dt));
t.Translate(linear*dt);
}
};
struct Frustum
{
float fov;
float nearPlane;
float farPlane;
float aspectRatio;
mat4 GetFrustumMatrix(mat4 viewMatrix) {
mat4 pm = glm::perspectiveFov(radians(fov), aspectRatio, 1.f, nearPlane, farPlane);
mat4 invView = inverse(viewMatrix);
vec3 nm = vec3(invView[3]);
invView[3] = vec4(0, 0, 0, 1);
viewMatrix = translate(invView, nm);
mat4 vpm = pm * viewMatrix;
return vpm;
}
mat4 GetFrustumMatrix(Transform holder) {
return GetFrustumMatrix(holder.GetMatrix());
}
};
void FrustumTest::Init()
{
//add stuff to cull
const int width = 100; //100
const int height = 50; //6
const float spacing = 2;
for (int z = 0; z < width; z++)
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
auto entity = registry.create();
auto& tf = registry.assign<Transform>(entity,
vec3(((10 + std::rand()) % 100) / 100.f, ((10 + std::rand()) % 100) / 100.f, ((10 + std::rand()) % 100) / 100.f),
quat(1, 0, 0, 0),
vec3((x - (width/2)) * spacing, (y - (height/2)) * spacing, (z - (width/2)) * spacing)
);
// registry.assign<Velocity>(entity,
// vec3(0, 0, (std::rand() % 1000) / 100.f) * 0.01f,
// vec3((std::rand() % 1000) / 1000.f, (std::rand() % 1000) / 1000.f, (std::rand() % 1000) / 1000.f) * 0.1f
// );
auto& sphere = registry.assign<BSphere>(entity//,
//vec3((x - (width/2)) * spacing, (y - (height/2)) * spacing, (z - (width/2)) * spacing),
//((20 + std::rand()) % 100) / 100.f
);
sphere.radius = ((20 + std::rand()) % 100) / 100.f;
sphere.UpdateCaches(tf);
}
//add cullers
auto entity = registry.create();
registry.assign<Transform>(entity, vec3(1), quat(1, 0, 0, 0), vec3(0));
registry.assign<Velocity>(entity, vec3(0, 0.f, 0), vec3(0, 0.1f, 0));
registry.assign<Frustum>(entity, 50.f, 0.01f, 500.f, 2.f);
SetCameraPosition(vec3(-5, 30, 5));
SetCameraYawPitch(-35, 90);
}
bool NaiveCull(vec3& pos, float& radius, vec4& plane)
{
return plane.x * pos.x + plane.y * pos.y + plane.z * pos.z + plane.w <= -radius;
}
__m128 _mm_add_ps(__m128& a, __m128& b, __m128& c)
{
return _mm_add_ps(_mm_add_ps(a, b), c);
}
__m128 _mm_add_ps(__m128& a, __m128& b, __m128& c, __m128& d)
{
return _mm_add_ps(_mm_add_ps(a, b), _mm_add_ps(c, d));
}
__m128 _mm_set_ps_bw(float x, float y, float z, float w)
{
return _mm_set_ps(w, z, y, x);
}
bool draw = false;
bool drawCulled = false;
bool simd = true;
bool mt = true;
std::vector<uint> times;
uint maxavg;
float lerpAvg;
std::vector<BSphere> inView;
std::vector<BSphere> culled;
int inViewCount = 0;
int culledCount = 0;
std::mutex drawListMtx;
void naiveCull(BSphere& s, vec4 &left, vec4 &right, vec4 &top, vec4 &bottom) {
auto[pos, r] = s.GetCachedDataSlow();
bool cull = false;
if (NaiveCull(pos, s.radius, right)) cull = true;
else if (NaiveCull(pos, s.radius, left)) cull = true;
else if (NaiveCull(pos, s.radius, bottom)) cull = true;
else if (NaiveCull(pos, s.radius, top)) cull = true;
if (cull)
culledCount++;
else
inViewCount++;
if (draw) {
if (cull) {
if (drawCulled) {
drawListMtx.lock();
culled.emplace_back(s);
drawListMtx.unlock();
}
}
else {
drawListMtx.lock();
inView.emplace_back(s);
drawListMtx.unlock();
}
}
}
void simdCull(BSphere& s, __m128* planes)
{
__m128 xs = _mm_mul_ps(planes[0], s.simdCacheX);
__m128 ys = _mm_mul_ps(planes[1], s.simdCacheY);
__m128 zs = _mm_mul_ps(planes[2], s.simdCacheZ);
__m128 added = _mm_add_ps(xs, ys, zs, planes[3]);
__m128 results = _mm_cmplt_ps(added, s.simdCacheR);
auto cull = _mm_movemask_ps(results);
if (cull)
culledCount++;
else
inViewCount++;
if (draw) {
if (cull) {
if (drawCulled) {
drawListMtx.lock();
culled.emplace_back(s);
drawListMtx.unlock();
}
}
else {
drawListMtx.lock();
inView.emplace_back(s);
drawListMtx.unlock();
}
}
}
void FrustumTest::Update(float dt)
{
inViewCount = 0;
culledCount = 0;
DrawGrid();
registry.view<Transform, Velocity>().each([this, &dt](auto entity, Transform& transform, Velocity& vel) {
vel.ApplyToTransform(transform, dt);
transform.DrawDebug();
});
registry.view<Transform, Frustum>().each([this](auto entity, Transform& transform, Frustum& fr) {
mat4 frustumMat4 = fr.GetFrustumMatrix(transform);
DrawFrustum(frustumMat4, col32::white);
mat4& m = frustumMat4;
vec4 right;
right.x = m[0][3] + m[0][0];
right.y = m[1][3] + m[1][0];
right.z = m[2][3] + m[2][0];
right.w = m[3][3] + m[3][0];
vec4 left;
left.x = m[0][3] - m[0][0];
left.y = m[1][3] - m[1][0];
left.z = m[2][3] - m[2][0];
left.w = m[3][3] - m[3][0];
vec4 top;
top.x = m[0][3] - m[0][1];
top.y = m[1][3] - m[1][1];
top.z = m[2][3] - m[2][1];
top.w = m[3][3] - m[3][1];
vec4 bottom;
bottom.x = m[0][3] + m[0][1];
bottom.y = m[1][3] + m[1][1];
bottom.z = m[2][3] + m[2][1];
bottom.w = m[3][3] + m[3][1];
//vec4 far;
//far.x = m[0][2];
//far.y = m[1][2];
//far.z = m[2][2];
//far.w = m[3][2];
//vec4 near;
//near.x = m[0][3] - m[0][2];
//near.y = m[1][3] - m[1][2];
//near.z = m[2][3] - m[2][2];
//near.w = m[3][3] - m[3][2];
__m128 planes[4] = {
_mm_set_ps_bw(left.x, right.x, top.x, bottom.x),
_mm_set_ps_bw(left.y, right.y, top.y, bottom.y),
_mm_set_ps_bw(left.z, right.z, top.z, bottom.z),
_mm_set_ps_bw(left.w, right.w, top.w, bottom.w),
};
auto view = registry.view<BSphere>();
auto tp = TIME_HERE;
if (simd) {
if (mt)
std::for_each(std::execution::par, view.begin(), view.end(), [&view, &planes](const auto entity)
{
BSphere& s = view.get(entity);
simdCull(s, &planes[0]);
});
else
registry.view<BSphere>().each([&planes](auto entity, BSphere& s) {simdCull(s, &planes[0]); });
}
else {
if (mt)
std::for_each(std::execution::par, view.begin(), view.end(), [&view, &right, &left, &bottom, &top](const auto entity) {
BSphere& s = view.get(entity);
naiveCull(s, left, right, top, bottom);
});
else
registry.view<BSphere>().each([this, &left, &right, &top, &bottom](auto entity, BSphere& s) {
naiveCull(s, left, right, top, bottom);
});
}
auto el = (int)ELAPSEDuS(tp);
for (BSphere sphere : inView)
{
auto[pos, r] = sphere.GetCachedDataSlow();
DrawSphere(pos, r, col32::white, 8);
}
for (BSphere sphere : culled)
{
auto[pos, r] = sphere.GetCachedDataSlow();
DrawSphere(pos, r, col32::red, 8);
}
times.emplace_back(el);
float avg = accumulate(times.begin(), times.end(), 0) / (float)times.size();
if (lerpAvg == 0)
lerpAvg = avg;
else
lerpAvg = bx::lerp(lerpAvg, avg, 0.01f);
if (times.size() > 120) {
times.erase(times.begin());
}
maxavg = fmax(maxavg, avg);
ImGui::Checkbox("draw", &draw);
ImGui::Checkbox("draw culled", &drawCulled);
ImGui::Checkbox("SIMD", &simd);
ImGui::Checkbox("Multi threading", &mt);
ImGui::SliderInt("microSeconds", &el, avg-100*abs(lerpAvg-avg), avg+100*abs(lerpAvg-avg));
ImGui::SliderFloat("AVG microSeconds", &avg, 0, maxavg, "%.1f");
ImGui::SliderFloat("Lpd microSeconds", &lerpAvg, 0, maxavg, "%.1f");
ImGui::Text("culled: %d\ninview: %d", culledCount, inViewCount);
inView.clear();
culled.clear();
});
};
void FrustumTest::Shutdown()
{
}
//todo macro?
int main(int argc, char** argv)
{
FrustumTest app;
return app.Run(argc, argv);
}