From 688523b871d623d9e7d85d10f7d74dfaae51080c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 31 Oct 2020 08:41:15 -0500 Subject: [PATCH] code cleanup --- include/utils.h | 2 +- src/physics.cpp | 1 - src/sim.cpp | 57 ++++++++++++++++++++++--------------------------- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/include/utils.h b/include/utils.h index 6481b97..0af574f 100644 --- a/include/utils.h +++ b/include/utils.h @@ -80,7 +80,7 @@ struct AABB bool intersects(AABB other) const; }; -#define QUAD_TREE_POINTS_PER_NODE 8 +#define QUAD_TREE_POINTS_PER_NODE 16 template struct QuadTree { diff --git a/src/physics.cpp b/src/physics.cpp index a357cb7..9c497f1 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -76,7 +76,6 @@ void nocheckin_init_other_bodies() } } - other_bodies.init(); other_bodies.ensure_capacity(1024); } diff --git a/src/sim.cpp b/src/sim.cpp index f718c3a..b132a52 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -30,7 +30,7 @@ #define PARTICLE_COUNT 2500 // :ArbitraryConstant -#define NUM_THREADS 4 +#define NUM_THREADS 2 // TODO(Brendan): Maybe this can be removed because it isn't really necessary? internal void @@ -122,6 +122,13 @@ create_circle_mesh() return vao; } +struct SimState; +struct ThreadData +{ + i32 id; + SimState* state; +}; + struct SimState { Array bodies; @@ -129,6 +136,10 @@ struct SimState FixedArenaAllocator> qt_body_allocator; Camera camera; + + pthread_barrier_t thread_sync_barrier; + pthread_t threads[NUM_THREADS - 1]; + ThreadData thread_data[NUM_THREADS - 1]; }; internal void @@ -139,26 +150,10 @@ update_bodies_partial(SimState *state, i32 index) persist const f64 step = 0.01; - foreach (i, low, high) - { - body_accumulate_move(&state->bodies[i], &state->qt_bodies, step); - } - - foreach (i, low, high) - { - body_apply_move(&state->bodies[i], step); - } + foreach (i, low, high) body_accumulate_move(&state->bodies[i], &state->qt_bodies, step); + foreach (i, low, high) body_apply_move(&state->bodies[i], step); } -internal pthread_barrier_t thread_sync_barrier; -internal pthread_t threads[NUM_THREADS - 1]; - -struct ThreadData -{ - i32 id; - SimState* state; -}; - internal void* thread_start(void* data) { @@ -167,16 +162,16 @@ thread_start(void* data) while (true) { - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); update_bodies_partial(state, thread_id + 1); - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); if (thread_id == 0) { state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 }); state->qt_body_allocator.reset(); For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator); } - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); } return NULL; @@ -204,14 +199,12 @@ sim_state_init(SimState* state) state->camera.scale = 1.0f; - pthread_barrier_init(&thread_sync_barrier, NULL, NUM_THREADS); + pthread_barrier_init(&state->thread_sync_barrier, NULL, NUM_THREADS); foreach (i, 0, NUM_THREADS - 1) { - auto td = alloc(1); // LEAK - td->id = i; - td->state = state; - pthread_create(&threads[i], NULL, thread_start, (void *) td); + state->thread_data[i] = ThreadData { i, state }; + pthread_create(&state->threads[i], NULL, thread_start, (void *) &state->thread_data[i]); } } @@ -229,10 +222,10 @@ update(SimState* state, f64 dt) if (glfwGetKey(window, GLFW_KEY_Q)) state->camera.scale *= 1.02f; if (glfwGetKey(window, GLFW_KEY_A)) state->camera.scale /= 1.02f; - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); update_bodies_partial(state, 0); - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); } // NOTE CLEANUP(Brendan): Bunch of graphics state that should go elsewhere. @@ -280,7 +273,7 @@ draw(SimState* state) // NOTE(Brendan): Present the changes to the screen. glfwSwapBuffers(window); - pthread_barrier_wait(&thread_sync_barrier); + pthread_barrier_wait(&state->thread_sync_barrier); } internal void @@ -296,8 +289,6 @@ loop(SimState* state) while (!glfwWindowShouldClose(window)) { - glfwPollEvents(); - curr_time = glfwGetTime(); delta = curr_time - last_time; last_time = curr_time; @@ -318,6 +309,8 @@ loop(SimState* state) frame_rate = frame_count; frame_count = 0; } + + glfwPollEvents(); } } -- 2.25.1