diff --git a/src/lib/accelerators/bvh.c b/src/lib/accelerators/bvh.c index bd32f856..59123540 100644 --- a/src/lib/accelerators/bvh.c +++ b/src/lib/accelerators/bvh.c @@ -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); @@ -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 diff --git a/src/lib/renderer/instance.c b/src/lib/renderer/instance.c index d3ce84bc..7039ff0c 100644 --- a/src/lib/renderer/instance.c +++ b/src/lib/renderer/instance.c @@ -156,6 +156,7 @@ static bool intersectMesh(const struct instance *instance, const struct lightRay struct lightRay copy = *ray; tform_ray(©, 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, ©, isect, sampler)) { // Repopulate uv with actual texture mapping @@ -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);