From c42fd6547afed0b65260ae6a8a07cf9dcf084b67 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 31 May 2014 14:08:36 -0400 Subject: finish: Use pool. --- libdimension-python/dimension.pxd | 18 +++--- libdimension-python/dimension.pyx | 16 ++--- libdimension/dimension/finish.h | 64 +++----------------- libdimension/dimension/finishes.h | 18 +++--- libdimension/finish.c | 122 ++++++-------------------------------- libdimension/lambertian.c | 4 +- libdimension/phong.c | 4 +- libdimension/reflection.c | 4 +- libdimension/tests/render.c | 15 ++--- libdimension/texture.c | 1 - 10 files changed, 59 insertions(+), 207 deletions(-) diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd index 45a081f..b822827 100644 --- a/libdimension-python/dimension.pxd +++ b/libdimension-python/dimension.pxd @@ -272,23 +272,19 @@ cdef extern from "../libdimension/dimension.h": ctypedef struct dmnsn_reflection ctypedef struct dmnsn_finish: - dmnsn_ambient *ambient - dmnsn_diffuse *diffuse - dmnsn_specular *specular + dmnsn_ambient *ambient + dmnsn_diffuse *diffuse + dmnsn_specular *specular dmnsn_reflection *reflection dmnsn_finish dmnsn_new_finish() - void dmnsn_delete_finish(dmnsn_finish finish) - - void dmnsn_finish_incref(dmnsn_finish *finish) void dmnsn_finish_cascade(dmnsn_finish *default_finish, dmnsn_finish *finish) - dmnsn_ambient *dmnsn_new_ambient(dmnsn_color ambient) - dmnsn_diffuse *dmnsn_new_lambertian(double diffuse) - dmnsn_specular *dmnsn_new_phong(double specular, double exp) - dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_color min, dmnsn_color max, - double falloff) + dmnsn_ambient *dmnsn_new_ambient(dmnsn_pool *pool, dmnsn_color ambient) + dmnsn_diffuse *dmnsn_new_lambertian(dmnsn_pool *pool, double diffuse) + dmnsn_specular *dmnsn_new_phong(dmnsn_pool *pool, double specular, double exp) + dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_pool *pool, dmnsn_color min, dmnsn_color max, double falloff) ############ # Textures # diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index 0a90b07..7c951d6 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -879,9 +879,6 @@ cdef class Finish: def __cinit__(self): self._finish = dmnsn_new_finish() - def __dealloc__(self): - dmnsn_delete_finish(self._finish) - def __add__(Finish lhs not None, Finish rhs not None): """ Combine two finishes. @@ -898,7 +895,6 @@ cdef Finish _Finish(dmnsn_finish finish): """Wrap a Finish object around a dmnsn_finish.""" cdef Finish self = Finish.__new__(Finish) self._finish = finish - dmnsn_finish_incref(&self._finish) return self cdef class Ambient(Finish): @@ -910,7 +906,7 @@ cdef class Ambient(Finish): Keyword arguments: color -- the color and intensity of the ambient light """ - self._finish.ambient = dmnsn_new_ambient(Color(color)._c) + self._finish.ambient = dmnsn_new_ambient(_get_pool(), Color(color)._c) cdef class Diffuse(Finish): """Lambertian diffuse reflection.""" @@ -921,7 +917,7 @@ cdef class Diffuse(Finish): Keyword arguments: diffuse -- the intensity of the diffuse reflection """ - self._finish.diffuse = dmnsn_new_lambertian(Color(diffuse).intensity()) + self._finish.diffuse = dmnsn_new_lambertian(_get_pool(), Color(diffuse).intensity()) cdef class Phong(Finish): """Phong specular highlight.""" @@ -933,7 +929,7 @@ cdef class Phong(Finish): strength -- the strength of the Phong highlight size -- the "shininess" of the material """ - self._finish.specular = dmnsn_new_phong(Color(strength).intensity(), size) + self._finish.specular = dmnsn_new_phong(_get_pool(), Color(strength).intensity(), size) cdef class Reflection(Finish): """Reflective finish.""" @@ -949,9 +945,7 @@ cdef class Reflection(Finish): if max is None: max = min - self._finish.reflection = dmnsn_new_basic_reflection(Color(min)._c, - Color(max)._c, - falloff) + self._finish.reflection = dmnsn_new_basic_reflection(_get_pool(), Color(min)._c, Color(max)._c, falloff) ############ # Textures # @@ -1000,9 +994,7 @@ cdef class Texture(_Transformable): def __get__(self): return _Finish(self._texture.finish) def __set__(self, Finish finish not None): - dmnsn_delete_finish(self._texture.finish) self._texture.finish = finish._finish - dmnsn_finish_incref(&self._texture.finish) def transform(self, Matrix trans not None): """Transform a texture.""" diff --git a/libdimension/dimension/finish.h b/libdimension/dimension/finish.h index 5f2443b..d975877 100644 --- a/libdimension/dimension/finish.h +++ b/libdimension/dimension/finish.h @@ -28,13 +28,10 @@ /** Ambient finish component. */ typedef struct dmnsn_ambient { dmnsn_color ambient; /**< Ambient light. */ - DMNSN_REFCOUNT; /**< Reference count. */ } dmnsn_ambient; -/** Allocate a dummy ambient component. */ -dmnsn_ambient *dmnsn_new_ambient(dmnsn_color ambient); -/** Delete an ambient component. */ -void dmnsn_delete_ambient(dmnsn_ambient *ambient); +/** Allocate an ambient component. */ +dmnsn_ambient *dmnsn_new_ambient(dmnsn_pool *pool, dmnsn_color ambient); /* Diffuse component */ @@ -53,26 +50,15 @@ typedef dmnsn_color dmnsn_diffuse_fn(const dmnsn_diffuse *diffuse, dmnsn_color light, dmnsn_color color, dmnsn_vector ray, dmnsn_vector normal); -/** - * Diffuse destruction callback. - * @param[in,out] diffuse The diffuse object to destroy. - */ -typedef void dmnsn_diffuse_free_fn(dmnsn_diffuse *diffuse); - /** Diffuse finish component. */ struct dmnsn_diffuse { dmnsn_diffuse_fn *diffuse_fn; /**< Diffuse callback. */ - dmnsn_diffuse_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy diffuse component. */ -dmnsn_diffuse *dmnsn_new_diffuse(void); +dmnsn_diffuse *dmnsn_new_diffuse(dmnsn_pool *pool); /** Initialize a dmnsn_diffuse field. */ void dmnsn_init_diffuse(dmnsn_diffuse *diffuse); -/** Delete a diffuse component. */ -void dmnsn_delete_diffuse(dmnsn_diffuse *diffuse); /* Specular component */ @@ -93,26 +79,15 @@ typedef dmnsn_color dmnsn_specular_fn(const dmnsn_specular *specular, dmnsn_vector ray, dmnsn_vector normal, dmnsn_vector viewer); -/** - * Specular destruction callback. - * @param[in,out] specular The specular object to destroy. - */ -typedef void dmnsn_specular_free_fn(dmnsn_specular *specular); - /** Specular finish component. */ struct dmnsn_specular { dmnsn_specular_fn *specular_fn; /**< Specular callback. */ - dmnsn_specular_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy specular component. */ -dmnsn_specular *dmnsn_new_specular(void); +dmnsn_specular *dmnsn_new_specular(dmnsn_pool *pool); /** Initialize a dmnsn_specular field. */ void dmnsn_init_specular(dmnsn_specular *specular); -/** Delete a specular component. */ -void dmnsn_delete_specular(dmnsn_specular *specular); /* Reflection component */ @@ -131,34 +106,23 @@ typedef dmnsn_color dmnsn_reflection_fn(const dmnsn_reflection *reflection, dmnsn_color reflect, dmnsn_color color, dmnsn_vector ray, dmnsn_vector normal); -/** - * Reflection destruction callback. - * @param[in,out] reflection The reflection object to destroy. - */ -typedef void dmnsn_reflection_free_fn(dmnsn_reflection *reflection); - /** The reflection component. */ struct dmnsn_reflection { dmnsn_reflection_fn *reflection_fn; /**< Reflection callback. */ - dmnsn_reflection_free_fn *free_fn; /**< Destructor callback. */ - - DMNSN_REFCOUNT; /**< Reference count. */ }; /** Allocate a dummy reflection component. */ -dmnsn_reflection *dmnsn_new_reflection(void); +dmnsn_reflection *dmnsn_new_reflection(dmnsn_pool *pool); /** Initialize a dmnsn_reflection field. */ void dmnsn_init_reflection(dmnsn_reflection *reflection); -/** Delete a reflection component. */ -void dmnsn_delete_reflection(dmnsn_reflection *reflection); /* Entire finishes */ /** A finish. */ typedef struct dmnsn_finish { - dmnsn_ambient *ambient; /**< Ambient component. */ - dmnsn_diffuse *diffuse; /**< Diffuse component. */ - dmnsn_specular *specular; /**< Specular component. */ + dmnsn_ambient *ambient; /**< Ambient component. */ + dmnsn_diffuse *diffuse; /**< Diffuse component. */ + dmnsn_specular *specular; /**< Specular component. */ dmnsn_reflection *reflection; /**< Reflection component. */ } dmnsn_finish; @@ -168,18 +132,6 @@ typedef struct dmnsn_finish { */ dmnsn_finish dmnsn_new_finish(void); -/** - * Delete a finish. - * @param[in,out] finish The finish to delete. - */ -void dmnsn_delete_finish(dmnsn_finish finish); - -/** - * Increment a finish's reference count. - * @param[in,out] finish The finish to acquire. - */ -void dmnsn_finish_incref(dmnsn_finish *finish); - /** * Fill missing finish properties from a default finish. * @param[in] default_finish The default finish. diff --git a/libdimension/dimension/finishes.h b/libdimension/dimension/finishes.h index 7ed66d3..e1f7b44 100644 --- a/libdimension/dimension/finishes.h +++ b/libdimension/dimension/finishes.h @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2011 Tavian Barnes * + * Copyright (C) 2009-2014 Tavian Barnes * * * * This file is part of The Dimension Library. * * * @@ -25,25 +25,27 @@ /** * Regular diffuse finish. + * @param[in] pool The memory pool to allocate from. * @param[in] diffuse The diffuse reflection coefficient. * @return A diffuse finish component. */ -dmnsn_diffuse *dmnsn_new_lambertian(double diffuse); +dmnsn_diffuse *dmnsn_new_lambertian(dmnsn_pool *pool, double diffuse); /** * A phong specular highlight. + * @param[in] pool The memory pool to allocate from. * @param[in] specular The specular reflection coefficient. - * @param[in] exp The exponent (roughly the highlight size). + * @param[in] exp The exponent (roughly the highlight size). * @return A phong specular finish component. */ -dmnsn_specular *dmnsn_new_phong(double specular, double exp); +dmnsn_specular *dmnsn_new_phong(dmnsn_pool *pool, double specular, double exp); /** * Specular (mirror) reflection. - * @param[in] min Reflection at paralell angles. - * @param[in] max Reflection at perpendicular angles (often == \p min). + * @param[in] pool The memory pool to allocate from. + * @param[in] min Reflection at paralell angles. + * @param[in] max Reflection at perpendicular angles (often == \p min). * @param[in] falloff Degree of exponential falloff (usually 1). * @return A reflective finish component. */ -dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_color min, dmnsn_color max, - double falloff); +dmnsn_reflection *dmnsn_new_basic_reflection(dmnsn_pool *pool, dmnsn_color min, dmnsn_color max, double falloff); diff --git a/libdimension/finish.c b/libdimension/finish.c index 276969d..13adefb 100644 --- a/libdimension/finish.c +++ b/libdimension/finish.c @@ -26,32 +26,17 @@ #include "dimension-internal.h" dmnsn_ambient * -dmnsn_new_ambient(dmnsn_color ambient_light) +dmnsn_new_ambient(dmnsn_pool *pool, dmnsn_color ambient_light) { - dmnsn_ambient *ambient = DMNSN_MALLOC(dmnsn_ambient); + dmnsn_ambient *ambient = DMNSN_PALLOC(pool, dmnsn_ambient); ambient->ambient = ambient_light; - DMNSN_REFCOUNT_INIT(ambient); return ambient; } -void -dmnsn_delete_ambient(dmnsn_ambient *ambient) -{ - if (DMNSN_DECREF(ambient)) { - dmnsn_free(ambient); - } -} - -static void -dmnsn_default_diffuse_free_fn(dmnsn_diffuse *diffuse) -{ - dmnsn_free(diffuse); -} - dmnsn_diffuse * -dmnsn_new_diffuse(void) +dmnsn_new_diffuse(dmnsn_pool *pool) { - dmnsn_diffuse *diffuse = DMNSN_MALLOC(dmnsn_diffuse); + dmnsn_diffuse *diffuse = DMNSN_PALLOC(pool, dmnsn_diffuse); dmnsn_init_diffuse(diffuse); return diffuse; } @@ -59,28 +44,12 @@ dmnsn_new_diffuse(void) void dmnsn_init_diffuse(dmnsn_diffuse *diffuse) { - diffuse->free_fn = dmnsn_default_diffuse_free_fn; - DMNSN_REFCOUNT_INIT(diffuse); -} - -void -dmnsn_delete_diffuse(dmnsn_diffuse *diffuse) -{ - if (DMNSN_DECREF(diffuse)) { - diffuse->free_fn(diffuse); - } -} - -static void -dmnsn_default_specular_free_fn(dmnsn_specular *specular) -{ - dmnsn_free(specular); } dmnsn_specular * -dmnsn_new_specular(void) +dmnsn_new_specular(dmnsn_pool *pool) { - dmnsn_specular *specular = DMNSN_MALLOC(dmnsn_specular); + dmnsn_specular *specular = DMNSN_PALLOC(pool, dmnsn_specular); dmnsn_init_specular(specular); return specular; } @@ -88,28 +57,12 @@ dmnsn_new_specular(void) void dmnsn_init_specular(dmnsn_specular *specular) { - specular->free_fn = dmnsn_default_specular_free_fn; - DMNSN_REFCOUNT_INIT(specular); -} - -void -dmnsn_delete_specular(dmnsn_specular *specular) -{ - if (DMNSN_DECREF(specular)) { - specular->free_fn(specular); - } -} - -static void -dmnsn_default_reflection_free_fn(dmnsn_reflection *reflection) -{ - dmnsn_free(reflection); } dmnsn_reflection * -dmnsn_new_reflection(void) +dmnsn_new_reflection(dmnsn_pool *pool) { - dmnsn_reflection *reflection = DMNSN_MALLOC(dmnsn_reflection); + dmnsn_reflection *reflection = DMNSN_PALLOC(pool, dmnsn_reflection); dmnsn_init_reflection(reflection); return reflection; } @@ -117,72 +70,33 @@ dmnsn_new_reflection(void) void dmnsn_init_reflection(dmnsn_reflection *reflection) { - reflection->free_fn = dmnsn_default_reflection_free_fn; - DMNSN_REFCOUNT_INIT(reflection); -} - -void -dmnsn_delete_reflection(dmnsn_reflection *reflection) -{ - if (DMNSN_DECREF(reflection)) { - reflection->free_fn(reflection); - } } dmnsn_finish dmnsn_new_finish(void) { - dmnsn_finish finish; - finish.ambient = NULL; - finish.diffuse = NULL; - finish.specular = NULL; - finish.reflection = NULL; + dmnsn_finish finish = { + .ambient = NULL, + .diffuse = NULL, + .specular = NULL, + .reflection = NULL, + }; return finish; } -void -dmnsn_delete_finish(dmnsn_finish finish) -{ - dmnsn_delete_reflection(finish.reflection); - dmnsn_delete_specular(finish.specular); - dmnsn_delete_diffuse(finish.diffuse); - dmnsn_delete_ambient(finish.ambient); -} - -void -dmnsn_finish_incref(dmnsn_finish *finish) -{ - if (finish->ambient) { - DMNSN_INCREF(finish->ambient); - } - if (finish->diffuse) { - DMNSN_INCREF(finish->diffuse); - } - if (finish->specular) { - DMNSN_INCREF(finish->specular); - } - if (finish->reflection) { - DMNSN_INCREF(finish->reflection); - } -} - void dmnsn_finish_cascade(const dmnsn_finish *default_finish, dmnsn_finish *finish) { - if (!finish->ambient && default_finish->ambient) { + if (!finish->ambient) { finish->ambient = default_finish->ambient; - DMNSN_INCREF(finish->ambient); } - if (!finish->diffuse && default_finish->diffuse) { + if (!finish->diffuse) { finish->diffuse = default_finish->diffuse; - DMNSN_INCREF(finish->diffuse); } - if (!finish->specular && default_finish->specular) { + if (!finish->specular) { finish->specular = default_finish->specular; - DMNSN_INCREF(finish->specular); } - if (!finish->reflection && default_finish->reflection) { + if (!finish->reflection) { finish->reflection = default_finish->reflection; - DMNSN_INCREF(finish->reflection); } } diff --git a/libdimension/lambertian.c b/libdimension/lambertian.c index 9b305df..9360872 100644 --- a/libdimension/lambertian.c +++ b/libdimension/lambertian.c @@ -45,9 +45,9 @@ dmnsn_lambertian_diffuse_fn(const dmnsn_diffuse *diffuse, } dmnsn_diffuse * -dmnsn_new_lambertian(double coeff) +dmnsn_new_lambertian(dmnsn_pool *pool, double coeff) { - dmnsn_lambertian *lambertian = DMNSN_MALLOC(dmnsn_lambertian); + dmnsn_lambertian *lambertian = DMNSN_PALLOC(pool, dmnsn_lambertian); lambertian->coeff = coeff; dmnsn_diffuse *diffuse = &lambertian->diffuse; diff --git a/libdimension/phong.c b/libdimension/phong.c index bbf521d..3536f45 100644 --- a/libdimension/phong.c +++ b/libdimension/phong.c @@ -56,9 +56,9 @@ dmnsn_phong_specular_fn(const dmnsn_specular *specular, /* A phong finish */ dmnsn_specular * -dmnsn_new_phong(double coeff, double exp) +dmnsn_new_phong(dmnsn_pool *pool, double coeff, double exp) { - dmnsn_phong *phong = DMNSN_MALLOC(dmnsn_phong); + dmnsn_phong *phong = DMNSN_PALLOC(pool, dmnsn_phong); phong->coeff = coeff; phong->exp = exp; diff --git a/libdimension/reflection.c b/libdimension/reflection.c index ca279b8..3b2aeeb 100644 --- a/libdimension/reflection.c +++ b/libdimension/reflection.c @@ -50,9 +50,9 @@ dmnsn_basic_reflection_fn(const dmnsn_reflection *reflection, } dmnsn_reflection * -dmnsn_new_basic_reflection(dmnsn_color min, dmnsn_color max, double falloff) +dmnsn_new_basic_reflection(dmnsn_pool *pool, dmnsn_color min, dmnsn_color max, double falloff) { - dmnsn_basic_reflection *basic = DMNSN_MALLOC(dmnsn_basic_reflection); + dmnsn_basic_reflection *basic = DMNSN_PALLOC(pool, dmnsn_basic_reflection); basic->min = min; basic->max = max; basic->falloff = falloff; diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index b5e2e41..38451f7 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -29,9 +29,9 @@ dmnsn_test_scene_set_defaults(dmnsn_pool *pool, dmnsn_scene *scene) scene->default_texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_black)); dmnsn_finish *default_finish = &scene->default_texture->finish; default_finish->ambient = dmnsn_new_ambient( - dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) + pool, dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) ); - default_finish->diffuse = dmnsn_new_lambertian(dmnsn_sRGB_inverse_gamma(0.7)); + default_finish->diffuse = dmnsn_new_lambertian(pool, dmnsn_sRGB_inverse_gamma(0.7)); } static void @@ -127,10 +127,8 @@ dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_tcolor cube_color = dmnsn_new_tcolor(dmnsn_blue, 0.75, 1.0/3.0); cube->texture->pigment = dmnsn_new_solid_pigment(pool, cube_color); - dmnsn_color reflect = - dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); - cube->texture->finish.reflection = - dmnsn_new_basic_reflection(dmnsn_black, reflect, 1.0); + dmnsn_color reflect = dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); + cube->texture->finish.reflection = dmnsn_new_basic_reflection(pool, dmnsn_black, reflect, 1.0); cube->interior = dmnsn_new_interior(pool); cube->interior->ior = 1.1; @@ -138,8 +136,7 @@ dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_object *sphere = dmnsn_new_sphere(); sphere->texture = dmnsn_new_texture(); sphere->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_green)); - sphere->texture->finish.specular = - dmnsn_new_phong(dmnsn_sRGB_inverse_gamma(0.2), 40.0); + sphere->texture->finish.specular = dmnsn_new_phong(pool, dmnsn_sRGB_inverse_gamma(0.2), 40.0); sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); dmnsn_object *hollow_cube = dmnsn_new_csg_difference(cube, sphere); @@ -208,7 +205,7 @@ dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) dmnsn_delete_array(torus_array); torii->texture = dmnsn_new_texture(); torii->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_blue)); - torii->texture->finish.ambient = dmnsn_new_ambient(dmnsn_white); + torii->texture->finish.ambient = dmnsn_new_ambient(pool, dmnsn_white); dmnsn_array *spike_array = DMNSN_NEW_ARRAY(dmnsn_object *); dmnsn_array_push(spike_array, &arrow); diff --git a/libdimension/texture.c b/libdimension/texture.c index 37a937b..0727ea1 100644 --- a/libdimension/texture.c +++ b/libdimension/texture.c @@ -43,7 +43,6 @@ void dmnsn_delete_texture(dmnsn_texture *texture) { if (DMNSN_DECREF(texture)) { - dmnsn_delete_finish(texture->finish); dmnsn_free(texture); } } -- cgit v1.2.3