commit 3446ed1eb3fd3f8ce1966c0f169847416afd5843
parent 547e4c9d03d7b1bccb72672b112e108bad13466f
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 27 Mar 2015 09:40:38 +0100
Make simpler the internal management of the meshes/instances
Diffstat:
7 files changed, 31 insertions(+), 88 deletions(-)
diff --git a/src/s3d.h b/src/s3d.h
@@ -132,7 +132,6 @@ static const struct s3d_hit S3D_HIT_NULL =
/* Forward declaration of s3d opaque data types */
struct s3d_device; /* Entry point of the library */
-struct s3d_rt_session; /* Ray tracing session */
struct s3d_scene; /* Collection of shapes */
struct s3d_shape; /* Untyped geometry */
@@ -318,22 +317,6 @@ s3d_instance_translate
const enum s3d_transform_space space,
const float translation[3]);
-/*******************************************************************************
- * RT Session
- ******************************************************************************/
-S3D_API res_T
-s3d_rt_session_create
- (struct s3d_scene* scn,
- struct s3d_rt_session** session);
-
-S3D_API res_T
-s3d_rt_session_ref_get
- (struct s3d_rt_session* session);
-
-S3D_API res_T
-s3d_rt_session_ref_put
- (struct s3d_rt_session* session);
-
END_DECLS
#endif /* S3D_H */
diff --git a/src/s3d_instance.c b/src/s3d_instance.c
@@ -49,8 +49,7 @@ instance_release(ref_T* ref)
ASSERT(ref);
inst = CONTAINER_OF(ref, struct instance, ref);
scn = inst->scene;
- if(inst->rtc_geom != RTC_INVALID_GEOMETRY_ID)
- instance_delete_rtc_geom(inst);
+ ASSERT(inst->rtc_geom == RTC_INVALID_GEOMETRY_ID);
MEM_FREE(scn->dev->allocator, inst);
S3D(scene_ref_put(scn));
}
@@ -105,19 +104,3 @@ instance_ref_put(struct instance* inst)
ref_put(&inst->ref, instance_release);
}
-void
-instance_create_rtc_geom(struct instance* inst, RTCScene scn)
-{
- ASSERT(inst && inst->rtc_geom == RTC_INVALID_GEOMETRY_ID && scn);
- inst->rtc_geom = rtcNewInstance(scn, inst->scene->rtc_scn);
- inst->rtc_scn = scn;
-}
-
-void
-instance_delete_rtc_geom(struct instance* inst)
-{
- ASSERT(inst && inst->rtc_geom != RTC_INVALID_GEOMETRY_ID && inst->rtc_scn);
- rtcDeleteGeometry(inst->rtc_scn, inst->rtc_geom);
- inst->rtc_geom = RTC_INVALID_GEOMETRY_ID;
- inst->rtc_scn = NULL;
-}
diff --git a/src/s3d_instance.h b/src/s3d_instance.h
@@ -39,7 +39,6 @@ struct instance {
float transform[12]; /* local to world 3x4 column major matrix */
char update_transform;
- RTCScene rtc_scn;
unsigned rtc_geom;
struct s3d_scene* scene;
@@ -59,14 +58,5 @@ extern LOCAL_SYM void
instance_ref_put
(struct instance* inst);
-extern LOCAL_SYM void
-instance_create_rtc_geom
- (struct instance* inst,
- RTCScene rtc_scn);
-
-extern LOCAL_SYM void
-instance_delete_rtc_geom
- (struct instance* inst);
-
#endif /* S3D_INSTANCE_H */
diff --git a/src/s3d_mesh.c b/src/s3d_mesh.c
@@ -215,8 +215,7 @@ mesh_release(ref_T* ref)
mesh = CONTAINER_OF(ref, struct mesh, ref);
mesh_clear(mesh);
dev = mesh->dev;
- if(mesh->rtc_geom != RTC_INVALID_GEOMETRY_ID)
- mesh_delete_rtc_geom(mesh);
+ ASSERT(mesh->rtc_geom == RTC_INVALID_GEOMETRY_ID);
MEM_FREE(dev->allocator, mesh);
S3D(device_ref_put(dev));
}
@@ -237,7 +236,6 @@ mesh_create(struct s3d_device* dev, struct mesh** out_mesh)
goto error;
}
mesh->rtc_geom = RTC_INVALID_GEOMETRY_ID;
- mesh->rtc_scn = NULL;
ref_init(&mesh->ref);
S3D(device_ref_get(dev));
mesh->dev = dev;
@@ -268,24 +266,6 @@ mesh_ref_put(struct mesh* mesh)
}
void
-mesh_create_rtc_geom(struct mesh* mesh, RTCScene scn)
-{
- ASSERT(mesh && mesh->rtc_geom == RTC_INVALID_GEOMETRY_ID && scn);
- mesh->rtc_geom = rtcNewTriangleMesh(scn, RTC_GEOMETRY_DYNAMIC,
- mesh_get_ntris(mesh), mesh_get_nverts(mesh));
- mesh->rtc_scn = scn;
-}
-
-void
-mesh_delete_rtc_geom(struct mesh* mesh)
-{
- ASSERT(mesh && mesh->rtc_geom != RTC_INVALID_GEOMETRY_ID && mesh->rtc_scn);
- rtcDeleteGeometry(mesh->rtc_scn, mesh->rtc_geom);
- mesh->rtc_geom = RTC_INVALID_GEOMETRY_ID;
- mesh->rtc_scn = NULL;
-}
-
-void
mesh_clear(struct mesh* mesh)
{
size_t iattr;
diff --git a/src/s3d_mesh.h b/src/s3d_mesh.h
@@ -64,7 +64,6 @@ struct mesh { /* Triangular mesh */
int resize_mask; /* Combination of buffer_type */
int update_mask; /* Combination of buffer_type */
- RTCScene rtc_scn; /* Embree scene from which rtc_geom was created */
unsigned rtc_geom; /* Embree geometry */
struct s3d_device* dev;
@@ -85,15 +84,6 @@ mesh_ref_put
(struct mesh* mesh);
extern LOCAL_SYM void
-mesh_create_rtc_geom
- (struct mesh* mesh,
- RTCScene rtc_scn);
-
-extern LOCAL_SYM void
-mesh_delete_rtc_geom
- (struct mesh* mesh);
-
-extern LOCAL_SYM void
mesh_clear
(struct mesh* mesh);
diff --git a/src/s3d_scene.c b/src/s3d_scene.c
@@ -49,7 +49,10 @@ scene_build_register_mesh(struct s3d_scene* scn, struct mesh* mesh)
ASSERT(scn && mesh);
/* Create the Embree geometry if it is not valid */
if(mesh->rtc_geom == RTC_INVALID_GEOMETRY_ID) {
- mesh_create_rtc_geom(mesh, scn->rtc_scn);
+ mesh->rtc_geom = rtcNewTriangleMesh(scn->rtc_scn, RTC_GEOMETRY_DYNAMIC,
+ mesh_get_ntris(mesh), mesh_get_nverts(mesh));
+ if(mesh->rtc_geom == RTC_INVALID_GEOMETRY_ID)
+ return RES_UNKNOWN_ERR;
scn->is_rtc_scn_outdated = 1;
}
@@ -73,8 +76,10 @@ scene_build_register_instance(struct s3d_scene* scn, struct instance* inst)
/* The instance should not contain instances */
ASSERT(!darray_geom2inst_size_get(&inst->scene->geom2inst));
- if(inst->rtc_geom) {
- instance_create_rtc_geom(inst, scn->rtc_scn);
+ if(inst->rtc_geom == RTC_INVALID_GEOMETRY_ID) {
+ inst->rtc_geom = rtcNewInstance(scn->rtc_scn, inst->scene->rtc_scn);
+ if(inst->rtc_geom == RTC_INVALID_GEOMETRY_ID)
+ return RES_UNKNOWN_ERR;
scn->is_rtc_scn_outdated = 1;
}
@@ -140,7 +145,8 @@ scene_setup_shape_mesh
/* Discard the shape mesh that is not geometrically valid */
if(!shape->data.mesh->indices || !shape->data.mesh->attribs[S3D_POSITION]) {
if(mesh->rtc_geom != RTC_INVALID_GEOMETRY_ID) {
- mesh_delete_rtc_geom(mesh);
+ rtcDeleteGeometry(scn->rtc_scn, mesh->rtc_geom);
+ mesh->rtc_geom = RTC_INVALID_GEOMETRY_ID;
scn->is_rtc_scn_outdated = 1;
}
mesh_clear(mesh);
@@ -183,7 +189,8 @@ scene_setup_shape_mesh
/* The shape mesh was resize => the Embree geometry is no more valid */
if(shape->data.mesh->resize_mask && mesh->rtc_geom!=RTC_INVALID_GEOMETRY_ID) {
- mesh_delete_rtc_geom(mesh);
+ rtcDeleteGeometry(scn->rtc_scn, mesh->rtc_geom);
+ mesh->rtc_geom = RTC_INVALID_GEOMETRY_ID;
scn->is_rtc_scn_outdated = 1;
}
@@ -278,6 +285,10 @@ scene_detach_shape_mesh(struct s3d_scene* scn, struct s3d_shape* shape)
pmesh = htable_mesh_find(&scn->meshes, &shape);
if(pmesh) { /* The shape mesh is cached into the scene */
struct mesh* mesh = *pmesh;
+ if(mesh->rtc_geom != RTC_INVALID_GEOMETRY_ID) {
+ rtcDeleteGeometry(scn->rtc_scn, mesh->rtc_geom);
+ mesh->rtc_geom = RTC_INVALID_GEOMETRY_ID;
+ }
mesh_ref_put(mesh);
htable_mesh_erase(&scn->meshes, &shape);
}
@@ -295,6 +306,10 @@ scene_detach_shape_instance(struct s3d_scene* scn, struct s3d_shape* shape)
pinst = htable_inst_find(&scn->instances, &shape);
if(pinst) { /* The shape instance is cached into the scene */
struct instance* inst = *pinst;
+ if(inst->rtc_geom != RTC_INVALID_GEOMETRY_ID) {
+ rtcDeleteGeometry(scn->rtc_scn, inst->rtc_geom);
+ inst->rtc_geom = RTC_INVALID_GEOMETRY_ID;
+ }
instance_ref_put(inst);
htable_inst_erase(&scn->instances, &shape);
}
@@ -555,10 +570,10 @@ s3d_scene_trace_ray
else hit->uv[1] += w;
w = 0.f;
}
- /* Embree stores on the u and v parameters the barycentric coordinate of
- * the hit intersection with respect to the second and third triangle
- * vertices, respectively. The following code maps the u and v coordinate
- * to the first and second triangle vertices */
+ /* Embree stores on the u and v ray parameters the barycentric coordinates
+ * of the hit with respect to the second and third triangle vertices,
+ * respectively. The following code computes the barycentric coordinates
+ * of the hit for the first and second triangle vertices */
hit->uv[1] = hit->uv[0];
hit->uv[0] = w;
diff --git a/src/test_s3d_trace_ray.c b/src/test_s3d_trace_ray.c
@@ -162,20 +162,22 @@ main(int argc, char** argv)
f3(dir, 1.f, 1.f, 1.f);
CHECK(s3d_scene_trace_ray(scn, org, dir, range, &hit), RES_BAD_ARG);
+ /* Update the shape with the CBox tall block mesh */
ntris = sizeof(cbox_block_ids)/sizeof(unsigned[3]);
nverts = sizeof(cbox_short_block)/sizeof(float[3]);
desc.vertices = cbox_short_block;
desc.indices = cbox_block_ids;
CHECK(s3d_mesh_setup_indexed_vertices
(shape, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
-
CHECK(s3d_scene_build(scn), RES_OK);
+ /* Update the shape vertices */
desc.vertices = cbox_tall_block;
CHECK(s3d_mesh_setup_indexed_vertices
(shape, ntris, S3D_KEEP, nverts, attribs, &desc), RES_OK);
CHECK(s3d_shape_ref_put(shape), RES_OK);
+ /* Create a the CBox short block shape */
desc.vertices = cbox_short_block;
CHECK(s3d_shape_create_mesh(dev, &shape), RES_OK);
CHECK(s3d_mesh_setup_indexed_vertices
@@ -183,6 +185,7 @@ main(int argc, char** argv)
CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
CHECK(s3d_shape_ref_put(shape), RES_OK);
+ /* Create the CBox walls shape */
desc.indices = cbox_walls_ids;
desc.vertices = cbox_walls;
nverts = sizeof(cbox_walls)/sizeof(float[3]);
@@ -193,13 +196,12 @@ main(int argc, char** argv)
CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
CHECK(s3d_shape_ref_put(shape), RES_OK);
+ /* Instantiate the whole CBox */
CHECK(s3d_scene_create(dev, &scn2), RES_OK);
-
f3(org, -100.f, 0.f, -2.f);
CHECK(s3d_scene_instantiate(scn, &shape), RES_OK);
CHECK(s3d_scene_attach_shape(scn2, shape), RES_OK);
CHECK(s3d_instance_set_position(shape, org), RES_OK);
-
CHECK(s3d_scene_build(scn2), RES_OK);
camera_init(&cam);