From f8a499b596b8af46201b86a30c9807ca54363e25 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Mon, 13 Aug 2018 12:21:29 +0200 Subject: OpenSubdiv: Add stub implementation of C-API C-API is way smaller than the rest of the code which uses it. So better to conditionally compile stub implementation than to keep adding ifdef everywhere. --- source/blender/blenkernel/BKE_subdiv.h | 5 ++- source/blender/blenkernel/CMakeLists.txt | 2 +- source/blender/blenkernel/intern/subdiv.c | 24 ++--------- .../blender/blenkernel/intern/subdiv_converter.c | 13 +----- .../blenkernel/intern/subdiv_converter_mesh.c | 12 +----- source/blender/blenkernel/intern/subdiv_eval.c | 46 +++++++++------------- source/blender/blenkernel/intern/subdiv_mesh.c | 15 ++++++- 7 files changed, 43 insertions(+), 74 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_subdiv.h b/source/blender/blenkernel/BKE_subdiv.h index cd1e3adbc21..d81047563a7 100644 --- a/source/blender/blenkernel/BKE_subdiv.h +++ b/source/blender/blenkernel/BKE_subdiv.h @@ -130,8 +130,9 @@ void BKE_subdiv_free(Subdiv *subdiv); /* ============================= EVALUATION API ============================= */ -void BKE_subdiv_eval_begin(Subdiv *subdiv); -void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const struct Mesh *mesh); +/* Returns true if evaluator is ready for use. */ +bool BKE_subdiv_eval_begin(Subdiv *subdiv); +bool BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const struct Mesh *mesh); /* Single point queries. */ diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index 7169597f100..646665cdec2 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -53,6 +53,7 @@ set(INC ../../../intern/atomic ../../../intern/clog ../../../intern/libmv + ../../../intern/opensubdiv ../../../extern/curve_fit_nd ) @@ -530,7 +531,6 @@ endif() if(WITH_OPENSUBDIV) add_definitions(-DWITH_OPENSUBDIV) list(APPEND INC_SYS - ../../../intern/opensubdiv ${OPENSUBDIV_INCLUDE_DIRS} ) endif() diff --git a/source/blender/blenkernel/intern/subdiv.c b/source/blender/blenkernel/intern/subdiv.c index bae5491c079..207fdf43c55 100644 --- a/source/blender/blenkernel/intern/subdiv.c +++ b/source/blender/blenkernel/intern/subdiv.c @@ -37,17 +37,14 @@ #include "subdiv_converter.h" -#ifdef WITH_OPENSUBDIV -# include "opensubdiv_capi.h" -# include "opensubdiv_converter_capi.h" -# include "opensubdiv_evaluator_capi.h" -# include "opensubdiv_topology_refiner_capi.h" -#endif +#include "opensubdiv_capi.h" +#include "opensubdiv_converter_capi.h" +#include "opensubdiv_evaluator_capi.h" +#include "opensubdiv_topology_refiner_capi.h" Subdiv *BKE_subdiv_new_from_converter(const SubdivSettings *settings, struct OpenSubdiv_Converter *converter) { -#ifdef WITH_OPENSUBDIV SubdivStats stats; BKE_subdiv_stats_init(&stats); BKE_subdiv_stats_begin(&stats, SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME); @@ -74,16 +71,11 @@ Subdiv *BKE_subdiv_new_from_converter(const SubdivSettings *settings, BKE_subdiv_stats_end(&stats, SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME); subdiv->stats = stats; return subdiv; -#else - UNUSED_VARS(settings, converter); - return NULL; -#endif } Subdiv *BKE_subdiv_new_from_mesh(const SubdivSettings *settings, struct Mesh *mesh) { -#ifdef WITH_OPENSUBDIV if (mesh->totvert == 0) { return NULL; } @@ -92,15 +84,10 @@ Subdiv *BKE_subdiv_new_from_mesh(const SubdivSettings *settings, Subdiv *subdiv = BKE_subdiv_new_from_converter(settings, &converter); BKE_subdiv_converter_free(&converter); return subdiv; -#else - UNUSED_VARS(settings, mesh); - return NULL; -#endif } void BKE_subdiv_free(Subdiv *subdiv) { -#ifdef WITH_OPENSUBDIV if (subdiv->evaluator != NULL) { openSubdiv_deleteEvaluator(subdiv->evaluator); } @@ -108,7 +95,4 @@ void BKE_subdiv_free(Subdiv *subdiv) openSubdiv_deleteTopologyRefiner(subdiv->topology_refiner); } MEM_freeN(subdiv); -#else - UNUSED_VARS(subdiv); -#endif } diff --git a/source/blender/blenkernel/intern/subdiv_converter.c b/source/blender/blenkernel/intern/subdiv_converter.c index f6dabfa1c80..0ef32200bd3 100644 --- a/source/blender/blenkernel/intern/subdiv_converter.c +++ b/source/blender/blenkernel/intern/subdiv_converter.c @@ -27,25 +27,18 @@ #include "BLI_utildefines.h" -#ifdef WITH_OPENSUBDIV -# include "opensubdiv_converter_capi.h" -#endif +#include "opensubdiv_converter_capi.h" void BKE_subdiv_converter_free(struct OpenSubdiv_Converter *converter) { -#ifdef WITH_OPENSUBDIV if (converter->freeUserData) { converter->freeUserData(converter); } -#else - UNUSED_VARS(converter); -#endif } /*OpenSubdiv_FVarLinearInterpolation*/ int BKE_subdiv_converter_fvar_linear_from_settings(const SubdivSettings *settings) { -#ifdef WITH_OPENSUBDIV switch (settings->fvar_linear_interpolation) { case SUBDIV_FVAR_LINEAR_INTERPOLATION_NONE: return OSD_FVAR_LINEAR_INTERPOLATION_NONE; @@ -58,8 +51,4 @@ BKE_subdiv_converter_fvar_linear_from_settings(const SubdivSettings *settings) } BLI_assert(!"Unknown fvar linear interpolation"); return OSD_FVAR_LINEAR_INTERPOLATION_NONE; -#else - UNUSED_VARS(settings); - return 0; -#endif } diff --git a/source/blender/blenkernel/intern/subdiv_converter_mesh.c b/source/blender/blenkernel/intern/subdiv_converter_mesh.c index c5fb8afb6cd..32e829a9439 100644 --- a/source/blender/blenkernel/intern/subdiv_converter_mesh.c +++ b/source/blender/blenkernel/intern/subdiv_converter_mesh.c @@ -40,12 +40,9 @@ #include "MEM_guardedalloc.h" -#ifdef WITH_OPENSUBDIV -# include "opensubdiv_capi.h" -# include "opensubdiv_converter_capi.h" -#endif +#include "opensubdiv_capi.h" +#include "opensubdiv_converter_capi.h" -#ifdef WITH_OPENSUBDIV typedef struct ConverterStorage { SubdivSettings settings; const Mesh *mesh; @@ -391,16 +388,11 @@ static void init_user_data(OpenSubdiv_Converter *converter, initialize_manifold_indices(user_data); converter->user_data = user_data; } -#endif void BKE_subdiv_converter_init_for_mesh(struct OpenSubdiv_Converter *converter, const SubdivSettings *settings, const Mesh *mesh) { -#ifdef WITH_OPENSUBDIV init_functions(converter); init_user_data(converter, settings, mesh); -#else - UNUSED_VARS(converter, settings, mesh); -#endif } diff --git a/source/blender/blenkernel/intern/subdiv_eval.c b/source/blender/blenkernel/intern/subdiv_eval.c index e23be84ee26..e3754655ea6 100644 --- a/source/blender/blenkernel/intern/subdiv_eval.c +++ b/source/blender/blenkernel/intern/subdiv_eval.c @@ -40,32 +40,32 @@ #include "MEM_guardedalloc.h" -#ifdef WITH_OPENSUBDIV -# include "opensubdiv_evaluator_capi.h" -# include "opensubdiv_topology_refiner_capi.h" -#endif +#include "opensubdiv_evaluator_capi.h" +#include "opensubdiv_topology_refiner_capi.h" -void BKE_subdiv_eval_begin(Subdiv *subdiv) +bool BKE_subdiv_eval_begin(Subdiv *subdiv) { -#ifdef WITH_OPENSUBDIV if (subdiv->topology_refiner == NULL) { - /* Happens on input mesh with just loose geometry. */ + /* Happens on input mesh with just loose geometry, + * or when OpenSubdiv is disabled + */ + return false; } else if (subdiv->evaluator == NULL) { BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE); subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner( subdiv->topology_refiner); BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE); + if (subdiv->evaluator == NULL) { + return false; + } } else { /* TODO(sergey): Check for topology change. */ } -#else - UNUSED_VARS(subdiv); -#endif + return true; } -#ifdef WITH_OPENSUBDIV static void set_coarse_positions(Subdiv *subdiv, const Mesh *mesh) { const MVert *mvert = mesh->mvert; @@ -130,14 +130,16 @@ static void set_face_varying_data_from_uv(Subdiv *subdiv, } } } -#endif -void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh) +bool BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh) { -#ifdef WITH_OPENSUBDIV - BKE_subdiv_eval_begin(subdiv); + if (!BKE_subdiv_eval_begin(subdiv)) { + return false; + } if (subdiv->evaluator == NULL) { - return; + /* NOTE: This situation is supposed to be handled by begin(). */ + BLI_assert(!"Is not supposed to happen"); + return false; } /* Set coordinates of base mesh vertices. */ set_coarse_positions(subdiv, mesh); @@ -153,9 +155,7 @@ void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh) BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE); subdiv->evaluator->refine(subdiv->evaluator); BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE); -#else - UNUSED_VARS(subdiv, mesh); -#endif + return true; } /* ========================== Single point queries ========================== */ @@ -178,14 +178,10 @@ void BKE_subdiv_eval_limit_point_and_derivatives( const float u, const float v, float P[3], float dPdu[3], float dPdv[3]) { -#ifdef WITH_OPENSUBDIV subdiv->evaluator->evaluateLimit(subdiv->evaluator, ptex_face_index, u, v, P, dPdu, dPdv); -#else - UNUSED_VARS(subdiv, ptex_face_index, u, v, P, dPdu, dPdv); -#endif } void BKE_subdiv_eval_limit_point_and_normal( @@ -224,15 +220,11 @@ void BKE_subdiv_eval_face_varying( const float u, const float v, float face_varying[2]) { -#ifdef WITH_OPENSUBDIV subdiv->evaluator->evaluateFaceVarying(subdiv->evaluator, face_varying_channel, ptex_face_index, u, v, face_varying); -#else - UNUSED_VARS(subdiv, face_varying_channel, ptex_face_index, u, v, face_varying); -#endif } /* =================== Patch queries at given resolution =================== */ diff --git a/source/blender/blenkernel/intern/subdiv_mesh.c b/source/blender/blenkernel/intern/subdiv_mesh.c index bb27cb6a31e..3bc39732118 100644 --- a/source/blender/blenkernel/intern/subdiv_mesh.c +++ b/source/blender/blenkernel/intern/subdiv_mesh.c @@ -227,7 +227,7 @@ static void subdiv_mesh_ctx_count(SubdivMeshContext *ctx) num_polys_per_ptex_get(no_quad_patch_resolution); } } - /* Calculate extra edges createdd by loose edges. */ + /* Calculate extra vertices createdd by loose edges. */ for (int edge_index = 0; edge_index < coarse_mesh->totedge; edge_index++) { if (!BLI_BITMAP_TEST_BOOL(ctx->coarse_edges_used_map, edge_index)) { ctx->num_subdiv_vertices += num_subdiv_vertices_per_coarse_edge; @@ -2388,7 +2388,18 @@ Mesh *BKE_subdiv_to_mesh( /* Make sure evaluator is up to date with possible new topology, and that * is is refined for the new positions of coarse vertices. */ - BKE_subdiv_eval_update_from_mesh(subdiv, coarse_mesh); + if (!BKE_subdiv_eval_update_from_mesh(subdiv, coarse_mesh)) { + /* This could happen in two situations: + * - OpenSubdiv is disabled. + * - Something totally bad happened, and OpenSubdiv rejected our + * topology. + * In either way, we can't safely continue. + */ + if (coarse_mesh->totpoly) { + BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH); + return NULL; + } + } SubdivMeshContext ctx = {0}; ctx.coarse_mesh = coarse_mesh; ctx.subdiv = subdiv; -- cgit v1.2.3