star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.fr/star-3d.git
Log | Files | Refs | README | LICENSE

commit cf6a33010b3d1acaebafd81863da9ad519d6b45b
parent 80c1e00b87d2cc644866c9f614a40036c3f7fd6a
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 18 Oct 2019 16:30:19 +0200

Optimize the computation of the scene AABB

Compute the AABB only if the scene was updated between 2 scene views
creation.

Diffstat:
Msrc/s3d_scene_view.c | 59+++++++++++++++++++++++++++++++++++++++++++++++------------
Msrc/s3d_scene_view_c.h | 1+
2 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/src/s3d_scene_view.c b/src/s3d_scene_view.c @@ -118,11 +118,13 @@ on_shape_detach geom = *pgeom; if(scnview->mask == 0) { - /* The scnview is NOT in use. Directly rm the cached geometry */ + /* The scnview is NOT in use. Directly rm the cached geometry and notify + * that the scene AABB must be reevaluated */ size_t n; (void)n; scene_view_destroy_geometry(scnview, geom); n = htable_geom_erase(&scnview->cached_geoms, &shape_id); ASSERT(n == 1); + scnview->aabb_update = 1; } else { /* The scnview is in use. Delay the deletion of the cached geometry */ res_T res = darray_uint_push_back(&scnview->detached_shapes, &shape_id); @@ -528,6 +530,12 @@ scene_view_register_mesh geom->flip_surface = shape->flip_surface; + /* The geometry is updated => recompute the scene AABB */ + if(geom->embree_outdated_mask + & (EMBREE_VERTICES|EMBREE_INDICES|EMBREE_ENABLE)) { + scnview->aabb_update = 1; + } + exit: return res; error: @@ -583,6 +591,16 @@ scene_view_register_sphere geom->embree_outdated_mask |= EMBREE_USER_GEOMETRY; } + if(geom->is_enabled != shape->is_enabled) { + geom->is_enabled = shape->is_enabled; + geom->embree_outdated_mask |= EMBREE_ENABLE; + } + + /* The geometry is updated => recompute the scene AABB */ + if(geom->embree_outdated_mask & (EMBREE_USER_GEOMETRY|EMBREE_ENABLE)) { + scnview->aabb_update = 1; + } + /* Update the filter function. Actually, filter functions are supported for * built-in geometries only. For user defined geometries one has to * explicitly call the filter function in the user defined intersection @@ -596,11 +614,6 @@ scene_view_register_sphere geom->embree_outdated_mask |= EMBREE_USER_GEOMETRY; } - if(geom->is_enabled != shape->is_enabled) { - geom->is_enabled = shape->is_enabled; - geom->embree_outdated_mask |= EMBREE_ENABLE; - } - geom->flip_surface = shape->flip_surface; exit: @@ -680,6 +693,11 @@ scene_view_register_instance geom->embree_outdated_mask |= EMBREE_ENABLE; } + /* The instance transform was updated => recompute the scene AABB */ + if(geom->embree_outdated_mask & (EMBREE_TRANSFORM|EMBREE_ENABLE)) { + scnview->aabb_update = 1; + } + geom->flip_surface = shape->flip_surface; exit: @@ -945,8 +963,6 @@ scene_view_sync htable_shape_iterator_next(&it); } - scene_view_compute_scene_aabb(scnview); - /* Setup the scene for the S3D_TRACE scnview */ if((mask & S3D_TRACE) != 0) { res = scene_view_setup_embree(scnview, accel_struct_conf); @@ -957,6 +973,26 @@ scene_view_sync res = scene_view_compute_cdf(scnview); if(res != RES_OK) goto error; } + + /* Compute the scene AABB is updated */ + if(scnview->aabb_update) { + if((mask & S3D_TRACE) == 0) { + f3_splat(scnview->lower, FLT_MAX); + f3_splat(scnview->upper,-FLT_MAX); + scene_view_compute_scene_aabb(scnview); + } else { + struct RTCBounds bounds; + rtcGetSceneBounds(scnview->rtc_scn, &bounds); + scnview->lower[0] = bounds.lower_x; + scnview->lower[1] = bounds.lower_y; + scnview->lower[2] = bounds.lower_z; + scnview->upper[0] = bounds.upper_x; + scnview->upper[1] = bounds.upper_y; + scnview->upper[2] = bounds.upper_z; + } + scnview->aabb_update = 0; + } + /* Setup the scene for the scene_primitive_id/S3D_GET_PRIMITIVE scnview */ res = scene_view_compute_nprims_cdf(scnview, (mask & S3D_GET_PRIMITIVE)!=0); if(res != RES_OK) goto error; @@ -1056,15 +1092,14 @@ scene_view_release(ref_T* ref) scene_view_destroy_geometry(scnview, geom); tmp = htable_geom_erase(&scnview->cached_geoms, &shape_id); ASSERT(tmp == 1); + scnview->aabb_update = 1; /* The scene AABB must be reevaluated */ } darray_uint_clear(&scnview->detached_shapes); - /* Clear the scnview data structures excepted the cache of geometries that - * will be used to speed up the future scnview creation */ + /* Clear the scnview data structures excepted the cache of geometries and the + * scene AABB that will be used to speed up the future scnview creation */ darray_fltui_clear(&scnview->cdf); darray_nprims_cdf_clear(&scnview->nprims_cdf); - f3_splat(scnview->lower, FLT_MAX); - f3_splat(scnview->upper,-FLT_MAX); scnview->mask = 0; scnview->rtc_commit = 0; diff --git a/src/s3d_scene_view_c.h b/src/s3d_scene_view_c.h @@ -88,6 +88,7 @@ struct s3d_scene_view { /* Callback attached to the sig_shape_detach signal of scn */ scene_shape_cb_T on_shape_detach_cb; + int aabb_update; /* Define if the geometry AABB must be updated */ int mask; /* Combination of enum s3d_scene_view_flag */ int rtc_scn_update; /* Define if Embree geometries were deleted/added */ int rtc_commit; /* Define whether or not the Embree scene was committed */