fix crash when there are a lot of interactions

This commit is contained in:
Rokas Puzonas 2023-08-02 23:02:14 +03:00
parent 7c8a972228
commit c86e1f86bc
2 changed files with 19 additions and 10 deletions

View File

@ -5,3 +5,4 @@
-DRPROF_IMPLEMENTATION
-DRAYGUI_IMPLEMENTATION
-DDEBUG
-DSIMD256

View File

@ -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);
}
}