From c86e1f86bcf89869c1a833f3f1d85c70b05e7328 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 2 Aug 2023 23:02:14 +0300 Subject: [PATCH] fix crash when there are a lot of interactions --- compile_flags.txt | 1 + src/world.cpp | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/compile_flags.txt b/compile_flags.txt index 5947fa1..d7df871 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -5,3 +5,4 @@ -DRPROF_IMPLEMENTATION -DRAYGUI_IMPLEMENTATION -DDEBUG +-DSIMD256 diff --git a/src/world.cpp b/src/world.cpp index a18c6d3..ca56bd1 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -285,17 +285,27 @@ static void world_calc_distances_and_angles(World *world, BoidList *local_boids, RPROF_STOP(); } +#define B2B_CAPACITY 2048*10 +#define B2B_THRESHOLD B2B_CAPACITY * 0.5 + +static inline void append_b2b_cmp(World *world, BoidList *local_boids, boid_pair *b2b_cmps, int *b2b_cmps_count, boid_pair b2b_cmp) { + if (*b2b_cmps_count == B2B_CAPACITY) { + world_calc_distances_and_angles(world, local_boids, b2b_cmps, b2b_cmps_count); + } + + b2b_cmps[(*b2b_cmps_count)++] = b2b_cmp; +} + static void world_compute_local_boids(BoidList *local_boids, World *world, ChunkGrid *chunks) { Boid *boids = world->boids.data(); int boid_count = world->boids.size(); MemoryArena *arena = &world->frame_arena; int b2b_padding = 8; - int b2b_capacity = 2048*10; - boid_pair b2b_cmps[b2b_capacity + b2b_padding]; + boid_pair b2b_cmps[B2B_CAPACITY + b2b_padding]; int b2b_cmps_count = 0; for (int i = 0; i < b2b_padding; i++) { - memset(&b2b_cmps[b2b_capacity + i], 0, sizeof(boid_pair)); + memset(&b2b_cmps[B2B_CAPACITY + i], 0, sizeof(boid_pair)); } RPROF_START("Move chunk data to static arrays"); @@ -324,12 +334,11 @@ static void world_compute_local_boids(BoidList *local_boids, World *world, Chunk uboid_t to_boids_count = chunk->count-i-1; for (int j = 0; j < to_boids_count; j++) { - // TODO: - DEBUG_ASSERT(b2b_cmps_count < b2b_capacity-1); - b2b_cmps[b2b_cmps_count++] = { + boid_pair b2b_cmp = { .from = from_boid, .to = to_boids[j] }; + append_b2b_cmp(world, local_boids, b2b_cmps, &b2b_cmps_count, b2b_cmp); } } @@ -347,17 +356,16 @@ static void world_compute_local_boids(BoidList *local_boids, World *world, Chunk uboid_t *neighbour_boids = static_chunks[neighbour_idx]; for (int i = 0; i < chunk->count; i++) { for (int j = 0; j < neighbour_chunk->count; j++) { - // TODO: - DEBUG_ASSERT(b2b_cmps_count < b2b_capacity-1); - b2b_cmps[b2b_cmps_count++] = { + boid_pair b2b_cmp = { .from = chunk_boids[i], .to = neighbour_boids[j] }; + append_b2b_cmp(world, local_boids, b2b_cmps, &b2b_cmps_count, b2b_cmp); } } } - if (b2b_cmps_count > 2048*3) { + if (b2b_cmps_count > B2B_THRESHOLD) { world_calc_distances_and_angles(world, local_boids, b2b_cmps, &b2b_cmps_count); } }