Skip to content

Commit

Permalink
lib: Add checks to bail out of traversal if BVH is not valid
Browse files Browse the repository at this point in the history
It's been a pretty common case where for one reason or another, I've
shuffled some code in a way that leaves objects without BVHs. The
traversal now bails out if this is the case, and we emit warnings if
trees are missing, instead of just crashing.
  • Loading branch information
vkoskiv committed Apr 4, 2024
1 parent 6fb776b commit 37469d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib/accelerators/bvh.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ static inline struct bvh *build_bvh_generic(
void (*get_bbox_and_center)(const void *, unsigned, struct boundingBox *, struct vector *),
size_t count)
{
if (count < 1)
return calloc(1, sizeof(struct bvh));
if (count < 1) {
logr(debug, "bvh count < 1\n");
return NULL;
}

struct vector *centers = malloc(sizeof(struct vector) * count);
struct boundingBox *bboxes = malloc(sizeof(struct boundingBox) * count);
Expand Down Expand Up @@ -653,7 +655,14 @@ void destroy_bvh(struct bvh *bvh) {
void bvh_build_task(void *arg) {
block_signals();
struct mesh *mesh = (struct mesh *)arg;
struct timeval timer = { 0 };
timer_start(&timer);
mesh->bvh = build_mesh_bvh(mesh);
if (mesh->bvh) {
logr(debug, "Built BVH for %s, took %lums\n", mesh->name, timer_get_ms(timer));
} else {
logr(debug, "BVH build FAILED for %s\n", mesh->name);
}
}

// FIXME: Add pthread_cancel() support
Expand Down
7 changes: 7 additions & 0 deletions src/lib/renderer/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ static bool intersectMesh(const struct instance *instance, const struct lightRay
struct lightRay copy = *ray;
tform_ray(&copy, instance->composite.Ainv);
struct mesh *mesh = &((struct mesh_arr *)instance->object_arr)->items[instance->object_idx];
if (!mesh->bvh) return false;
copy.start = vec_add(copy.start, vec_scale(copy.direction, mesh->rayOffset));
if (traverse_bottom_level_bvh(mesh, &copy, isect, sampler)) {
// Repopulate uv with actual texture mapping
Expand Down Expand Up @@ -207,6 +208,12 @@ bool isMesh(const struct instance *instance) {

static void getMeshBBoxAndCenter(const struct instance *instance, struct boundingBox *bbox, struct vector *center) {
struct mesh *mesh = &((struct mesh_arr *)instance->object_arr)->items[instance->object_idx];
if (!mesh->bvh) {
*bbox = (struct boundingBox){ 0 };
*center = vec_zero();
mesh->rayOffset = 0.0f;
return;
}
*bbox = get_root_bbox(mesh->bvh);
tform_bbox(bbox, instance->composite.A);
*center = bboxCenter(bbox);
Expand Down

0 comments on commit 37469d1

Please sign in to comment.