commit e4d25a403770258112dc5b782dd1ec224c4b15d3
parent b26f2117222d4821c825127521ee0d183acd48fd
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 2 Sep 2016 09:56:12 +0200
Minor updates of the shape implementation
Diffstat:
3 files changed, 72 insertions(+), 85 deletions(-)
diff --git a/src/ssol_shape.c b/src/ssol_shape.c
@@ -34,58 +34,50 @@ shape_release(ref_T* ref)
dev = shape->dev;
ASSERT(dev && dev->allocator);
if(shape->s3d_shape) S3D(shape_ref_put(shape->s3d_shape));
- MEM_RM(dev->allocator, shape->quadric);
MEM_RM(dev->allocator, shape);
SSOL(device_ref_put(dev));
}
static INLINE res_T
-check_plane_ok(const struct ssol_quadric_plane* plane)
+check_plane(const struct ssol_quadric_plane* plane)
{
- if (!plane)
- return RES_BAD_ARG;
- return RES_OK;
+ return !plane ? RES_BAD_ARG : RES_OK;
}
static INLINE res_T
-check_parabol_ok(const struct ssol_quadric_parabol* parabol)
+check_parabol(const struct ssol_quadric_parabol* parabol)
{
- if (!parabol || parabol->focal <= 0)
- return RES_BAD_ARG;
- return RES_OK;
+ return !parabol || parabol->focal <= 0 ? RES_BAD_ARG : RES_OK;
}
static INLINE res_T
-check_parabolic_cylinder_ok
+check_parabolic_cylinder
(const struct ssol_quadric_parabolic_cylinder* parabolic_cylinder)
{
- if (!parabolic_cylinder || parabolic_cylinder->focal <= 0)
- return RES_BAD_ARG;
- return RES_OK;
+ return !parabolic_cylinder || parabolic_cylinder->focal <= 0
+ ? RES_BAD_ARG : RES_OK;
}
static INLINE res_T
-quadric_ok(const struct ssol_quadric* quadric)
+check_quadric(const struct ssol_quadric* quadric)
{
- if (!quadric)
- return RES_BAD_ARG;
+ if(!quadric) return RES_BAD_ARG;
+
switch (quadric->type) {
case SSOL_QUADRIC_PLANE:
- return check_plane_ok(&quadric->data.plane);
+ return check_plane(&quadric->data.plane);
case SSOL_QUADRIC_PARABOL:
- return check_parabol_ok(&quadric->data.parabol);
+ return check_parabol(&quadric->data.parabol);
case SSOL_QUADRIC_PARABOLIC_CYLINDER:
- return check_parabolic_cylinder_ok(&quadric->data.parabolic_cylinder);
+ return check_parabolic_cylinder(&quadric->data.parabolic_cylinder);
default: return RES_BAD_ARG;
}
}
static INLINE res_T
-check_carving_ok(const struct ssol_carving* polygon)
+check_carving(const struct ssol_carving* polygon)
{
- if(!polygon
- || !polygon->get
- || polygon->nb_vertices <= 0)
+ if(!polygon || !polygon->get || polygon->nb_vertices <= 0)
return RES_BAD_ARG;
/* we don't check that the polygon defines a not empty area
* in such case, the quadric is valid but can have zero surface */
@@ -93,18 +85,23 @@ check_carving_ok(const struct ssol_carving* polygon)
}
static INLINE res_T
-punched_surface_ok(const struct ssol_punched_surface* punched_surface)
+check_punched_surface(const struct ssol_punched_surface* punched_surface)
{
size_t i;
+ res_T res = RES_OK;
+
if(!punched_surface
|| punched_surface->nb_carvings == 0
|| !punched_surface->carvings
- || !punched_surface->quadric
- || quadric_ok(punched_surface->quadric) != RES_OK)
+ || !punched_surface->quadric)
return RES_BAD_ARG;
- for (i = 0; i < punched_surface->nb_carvings; i++) {
- if (check_carving_ok(&punched_surface->carvings[i]))
- return RES_BAD_ARG;
+
+ res = check_quadric(punched_surface->quadric);
+ if(res != RES_OK) return res;
+
+ FOR_EACH(i, 0, punched_surface->nb_carvings) {
+ res = check_carving(&punched_surface->carvings[i]);
+ if(res != RES_OK) return res;
}
/* we don't check that carvings define a non empty area
* in such case, the quadric is valid but has zero surface */
@@ -112,14 +109,10 @@ punched_surface_ok(const struct ssol_punched_surface* punched_surface)
}
static INLINE res_T
-shape_ok(const struct ssol_shape* shape)
+check_shape(const struct ssol_shape* shape)
{
- if(!shape
- || !shape->dev
- || SHAPE_FIRST_TYPE > shape->type
- || shape->type > SHAPE_LAST_TYPE)
- return RES_BAD_ARG;
- return RES_OK;
+ return !shape || !shape->dev || (unsigned)shape->type >= SHAPE_TYPES_COUNT__
+ ? RES_BAD_ARG : RES_OK;
}
/*******************************************************************************
@@ -134,16 +127,13 @@ shape_create
struct ssol_shape* shape = NULL;
res_T res = RES_OK;
- if(!dev
- || !out_shape
- || type < SHAPE_FIRST_TYPE
- || type > SHAPE_LAST_TYPE) {
- return RES_BAD_ARG;
+ if(!dev || !out_shape || type >= SHAPE_TYPES_COUNT__) {
+ res = RES_BAD_ARG;
+ goto error;
}
- shape = (struct ssol_shape*)MEM_CALLOC
- (dev->allocator, 1, sizeof(struct ssol_shape));
- if (!shape) {
+ shape = MEM_CALLOC(dev->allocator, 1, sizeof(struct ssol_shape));
+ if(!shape) {
res = RES_MEM_ERR;
goto error;
}
@@ -161,10 +151,10 @@ shape_create
shape->type = type;
exit:
- if (out_shape) *out_shape = shape;
+ if(out_shape) *out_shape = shape;
return res;
error:
- if (shape) {
+ if(shape) {
SSOL(shape_ref_put(shape));
shape = NULL;
}
@@ -193,8 +183,7 @@ ssol_shape_create_punched_surface
res_T
ssol_shape_ref_get(struct ssol_shape* shape)
{
- if (!shape) return RES_BAD_ARG;
- ASSERT(SHAPE_FIRST_TYPE <= shape->type && shape->type <= SHAPE_LAST_TYPE);
+ if(!shape) return RES_BAD_ARG;
ref_get(&shape->ref);
return RES_OK;
}
@@ -202,8 +191,7 @@ ssol_shape_ref_get(struct ssol_shape* shape)
res_T
ssol_shape_ref_put(struct ssol_shape* shape)
{
- if (!shape) return RES_BAD_ARG;
- ASSERT(SHAPE_FIRST_TYPE <= shape->type && shape->type <= SHAPE_LAST_TYPE);
+ if(!shape) return RES_BAD_ARG;
ref_put(&shape->ref, shape_release);
return RES_OK;
}
@@ -216,18 +204,15 @@ ssol_punched_surface_setup
res_T res = RES_OK;
struct mem_allocator * allocator;
- if((res = shape_ok(shape)) != RES_OK) return res;
- if((res = punched_surface_ok(punched_surface)) != RES_OK) return res;
+ if((res = check_shape(shape)) != RES_OK) return res;
+ if((res = check_punched_surface(punched_surface)) != RES_OK) return res;
if(shape->type != SHAPE_PUNCHED) return RES_BAD_ARG;
ASSERT(shape->ref);
ASSERT(shape->dev && shape->dev->allocator);
/* save quadric for further object instancing */
- MEM_RM(shape->dev->allocator, shape->quadric);
- shape->quadric = (struct ssol_quadric*)MEM_CALLOC
- (shape->dev->allocator, 1, sizeof(struct ssol_quadric));
- *shape->quadric = *punched_surface->quadric;
+ shape->quadric = *punched_surface->quadric;
/* mesh the surface: TODO */
(void) allocator; /* will be used later */
@@ -245,49 +230,53 @@ ssol_mesh_setup
const unsigned nattribs,
void* data)
{
- struct s3d_vertex_data* attrib3 = NULL;
+ struct s3d_vertex_data attrs[SSOL_ATTRIBS_COUNT__];
res_T res = RES_OK;
unsigned i;
- if((res = shape_ok(shape)) != RES_OK) return res;
+ if((res = check_shape(shape)) != RES_OK)
+ goto error;
+
if(shape->type != SHAPE_MESH || !get_indices) {
- return RES_BAD_ARG;
+ res = RES_BAD_ARG;
+ goto error;
}
- if (!ntris || !nverts || !attribs || !nattribs) {
- return RES_BAD_ARG;
+
+ if(!ntris || !nverts || !attribs || !nattribs) {
+ res = RES_BAD_ARG;
+ goto error;
}
- ASSERT(shape->ref);
- ASSERT(shape->dev && shape->dev->allocator);
- attrib3 = (struct s3d_vertex_data*)MEM_CALLOC
- (shape->dev->allocator, nattribs, sizeof(struct s3d_vertex_data));
- if (!attrib3) {
- return RES_MEM_ERR;
+ if(nattribs > SSOL_ATTRIBS_COUNT__) {
+ res = RES_MEM_ERR;
+ goto error;
}
- for (i = 0; i < nattribs; i++) {
- attrib3[i].get = attribs[i].get;
+ FOR_EACH(i, 0, nattribs) {
+ attrs[i].get = attribs[i].get;
switch (attribs[i].usage) {
case SSOL_POSITION:
- attrib3[i].usage = SSOL_TO_S3D_POSITION;
- attrib3[i].type = S3D_FLOAT3;
+ attrs[i].usage = SSOL_TO_S3D_POSITION;
+ attrs[i].type = S3D_FLOAT3;
break;
case SSOL_NORMAL:
- attrib3[i].usage = SSOL_TO_S3D_NORMAL;
- attrib3[i].type = S3D_FLOAT3;
+ attrs[i].usage = SSOL_TO_S3D_NORMAL;
+ attrs[i].type = S3D_FLOAT3;
break;
case SSOL_TEXCOORD:
- attrib3[i].usage = SSOL_TO_S3D_TEXCOORD;
- attrib3[i].type = S3D_FLOAT2;
- break;
- default:
- FATAL("Unreachable code \n");
+ attrs[i].usage = SSOL_TO_S3D_TEXCOORD;
+ attrs[i].type = S3D_FLOAT2;
break;
+ default: FATAL("Unreachable code \n"); break;
}
}
res = s3d_mesh_setup_indexed_vertices
- (shape->s3d_shape, ntris, get_indices, nverts, attrib3, nattribs, data);
- MEM_RM(shape->dev->allocator, attrib3);
+ (shape->s3d_shape, ntris, get_indices, nverts, attrs, nattribs, data);
+ if(res != RES_OK) goto error;
+
+exit:
return res;
+error:
+ goto exit;
}
diff --git a/src/ssol_shape_c.h b/src/ssol_shape_c.h
@@ -21,16 +21,14 @@
enum shape_type {
SHAPE_MESH,
SHAPE_PUNCHED,
-
- SHAPE_FIRST_TYPE = SHAPE_MESH,
- SHAPE_LAST_TYPE = SHAPE_PUNCHED
+ SHAPE_TYPES_COUNT__
};
struct ssol_shape {
enum shape_type type;
struct s3d_shape* s3d_shape; /* 3D shape for the mesh */
- struct ssol_quadric* quadric; /* NULL if type != SHAPE_PUNCHED */
+ struct ssol_quadric quadric;
struct ssol_device* dev;
ref_T ref;
diff --git a/src/ssol_solver.c b/src/ssol_solver.c
@@ -51,7 +51,7 @@ static INLINE const struct ssol_quadric*
get_quadric(const struct ssol_object_instance* instance)
{
ASSERT(instance && is_instance_punched(instance));
- return instance->object->shape->quadric;
+ return &instance->object->shape->quadric;
}
static INLINE struct s3d_scene*