commit d757db52bbf9bc3cacbcc02cab3aad19c378a5b4
parent 2032710b9aab7c02cc55bf4af237d4ade1f479a3
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 17 Jun 2015 14:09:52 +0200
Fix the scene trace session
The Embree geometrical structures are identified by IDs. We map these
structures to their s3D representation with arrays indexed by the Embree
IDs. Some of these IDs may not exist and consequently some cells of the
arrays point to nothing.
Up to now, there was no way to known which cells map to something or
not, leading to memory corruption when a cell was accessed while it must
not. Such cells are now set to NULL.
Diffstat:
2 files changed, 60 insertions(+), 27 deletions(-)
diff --git a/src/s3d_scene_c.h b/src/s3d_scene_c.h
@@ -40,14 +40,32 @@
#include <rsys/list.h>
#include <rsys/ref_count.h>
+/*
+ * The mesh/instance pointers must be initialized to NULL in order to define
+ * which pointers are valid or not
+ */
+static FINLINE void
+mesh_ptr_init__(struct mem_allocator* alloc, struct mesh** mesh)
+{
+ (void)alloc; *mesh = NULL;
+}
+
+static FINLINE void
+instance_ptr_init__(struct mem_allocator* alloc, struct instance** inst)
+{
+ (void)alloc; *inst = NULL;
+}
+
/* Generate the darray_mesh dynamic array */
#define DARRAY_NAME mesh
#define DARRAY_DATA struct mesh*
+#define DARRAY_FUNCTOR_INIT mesh_ptr_init__
#include <rsys/dynamic_array.h>
/* Generate the darray_inst dynamic array */
#define DARRAY_NAME inst
#define DARRAY_DATA struct instance*
+#define DARRAY_FUNCTOR_INIT instance_ptr_init__
#include <rsys/dynamic_array.h>
/* Generate the htable_mesh hash table */
diff --git a/src/test_s3d_trace_ray.c b/src/test_s3d_trace_ray.c
@@ -89,7 +89,10 @@ main(int argc, char** argv)
struct s3d_hit hit;
struct s3d_scene* scn;
struct s3d_scene* scn2;
- struct s3d_shape* shape;
+ struct s3d_shape* inst;
+ struct s3d_shape* walls;
+ struct s3d_shape* tall_block;
+ struct s3d_shape* short_block;
struct s3d_vertex_data attribs[4];
struct camera cam;
struct cbox_desc desc;
@@ -109,7 +112,6 @@ main(int argc, char** argv)
CHECK(s3d_device_create(NULL, &allocator, &dev), RES_OK);
CHECK(s3d_scene_create(dev, &scn), RES_OK);
- CHECK(s3d_shape_create_mesh(dev, &shape), RES_OK);
attribs[0].usage = S3D_POSITION;
attribs[0].type = S3D_FLOAT3;
@@ -120,9 +122,11 @@ main(int argc, char** argv)
nverts = sizeof(cbox_walls)/sizeof(float[3]);
desc.vertices = cbox_walls;
desc.indices = cbox_walls_ids;
+ CHECK(s3d_shape_create_mesh(dev, &walls), RES_OK);
CHECK(s3d_mesh_setup_indexed_vertices
- (shape, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
- CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
+ (walls, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, walls), RES_OK);
+ CHECK(s3d_shape_ref_put(walls), RES_OK);
CHECK(s3d_scene_begin_trace(scn), RES_OK);
CHECK(s3d_scene_trace_ray(NULL, NULL, NULL, NULL, NULL), RES_BAD_ARG);
@@ -165,49 +169,46 @@ main(int argc, char** argv)
f3(dir, 0.f, 1.f, 0.f);
CHECK(s3d_scene_trace_ray(scn, org, dir, range, &hit), RES_BAD_OP);
- /* Update the shape with the CBox tall block mesh */
+ /* Update the inst 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_shape_create_mesh(dev, &tall_block), RES_OK);
CHECK(s3d_mesh_setup_indexed_vertices
- (shape, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
- CHECK(s3d_scene_begin_trace(scn), RES_OK);
- CHECK(s3d_scene_end_trace(scn), RES_OK);
+ (tall_block, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, tall_block), RES_OK);
- /* Update the shape vertices */
+ /* Update the inst 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);
+ (tall_block, ntris, S3D_KEEP, nverts, attribs, &desc), RES_OK);
- /* Create a the CBox short block shape */
+ /* Create a the CBox short block inst */
desc.vertices = cbox_short_block;
- CHECK(s3d_shape_create_mesh(dev, &shape), RES_OK);
+ CHECK(s3d_shape_create_mesh(dev, &short_block), RES_OK);
CHECK(s3d_mesh_setup_indexed_vertices
- (shape, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
- CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
- CHECK(s3d_shape_ref_put(shape), RES_OK);
+ (short_block, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, short_block), RES_OK);
- /* Create the CBox walls shape */
+ /* Create the CBox walls inst */
desc.indices = cbox_walls_ids;
desc.vertices = cbox_walls;
nverts = sizeof(cbox_walls)/sizeof(float[3]);
ntris = sizeof(cbox_walls_ids)/sizeof(unsigned[3]);
- CHECK(s3d_shape_create_mesh(dev, &shape), RES_OK);
+ CHECK(s3d_shape_create_mesh(dev, &walls), RES_OK);
CHECK(s3d_mesh_setup_indexed_vertices
- (shape, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
- CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
- CHECK(s3d_shape_ref_put(shape), RES_OK);
+ (walls, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, walls), 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_instantiate(scn, &inst), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn2, inst), RES_OK);
+ CHECK(s3d_instance_set_position(inst, org), RES_OK);
- CHECK(s3d_shape_enable(shape, 0), RES_OK);
+ CHECK(s3d_shape_enable(inst, 0), RES_OK);
CHECK(s3d_scene_begin_trace(scn), RES_OK);
CHECK(s3d_scene_begin_trace(scn2), RES_BAD_OP);
@@ -216,8 +217,19 @@ main(int argc, char** argv)
CHECK(s3d_scene_begin_trace(scn), RES_BAD_OP);
CHECK(s3d_scene_end_trace(scn), RES_BAD_OP);
+ CHECK(s3d_scene_clear(scn2), RES_BAD_OP);
+ CHECK(s3d_scene_clear(scn), RES_BAD_OP);
+
CHECK(s3d_scene_end_trace(scn2), RES_OK);
- CHECK(s3d_shape_enable(shape, 1), RES_OK);
+ CHECK(s3d_shape_enable(inst, 1), RES_OK);
+ CHECK(s3d_scene_begin_trace(scn2), RES_OK);
+ CHECK(s3d_scene_end_trace(scn2), RES_OK);
+
+ CHECK(s3d_scene_clear(scn), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, walls), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, short_block), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, tall_block), RES_OK);
+
CHECK(s3d_scene_begin_trace(scn2), RES_OK);
camera_init(&cam);
@@ -279,7 +291,10 @@ main(int argc, char** argv)
MEM_RM(&allocator, img);
CHECK(s3d_device_ref_put(dev), RES_OK);
- CHECK(s3d_shape_ref_put(shape), RES_OK);
+ CHECK(s3d_shape_ref_put(inst), RES_OK);
+ CHECK(s3d_shape_ref_put(short_block), RES_OK);
+ CHECK(s3d_shape_ref_put(tall_block), RES_OK);
+ CHECK(s3d_shape_ref_put(walls), RES_OK);
CHECK(s3d_scene_ref_put(scn), RES_OK);
CHECK(s3d_scene_ref_put(scn2), RES_OK);