Commit: 62ca6d46d32f0663a64d418ee29120a5d101494d Author: YimingWu Date: Wed Apr 21 18:00:23 2021 +0800 Branches: lineart-fn-cached https://developer.blender.org/rB62ca6d46d32f0663a64d418ee29120a5d101494d
LineArt: Modifier stack cache wip =================================================================== M source/blender/blenkernel/intern/gpencil_modifier.c M source/blender/gpencil_modifiers/MOD_gpencil_lineart.h M source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c M source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h M source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c M source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c M source/blender/gpencil_modifiers/intern/lineart/lineart_ops.c M source/blender/makesdna/DNA_gpencil_modifier_types.h M source/blender/makesdna/DNA_gpencil_types.h =================================================================== diff --git a/source/blender/blenkernel/intern/gpencil_modifier.c b/source/blender/blenkernel/intern/gpencil_modifier.c index 16386cac029..f3f0fd9e62e 100644 --- a/source/blender/blenkernel/intern/gpencil_modifier.c +++ b/source/blender/blenkernel/intern/gpencil_modifier.c @@ -55,6 +55,7 @@ #include "DEG_depsgraph.h" #include "DEG_depsgraph_query.h" +#include "MOD_gpencil_lineart.h" #include "MOD_gpencil_modifiertypes.h" #include "BLO_read_write.h" @@ -165,6 +166,21 @@ bool BKE_gpencil_has_geometry_modifiers(Object *ob) return false; } +/** + * Check if object has grease pencil Geometry modifiers. + * \param ob: Grease pencil object + * \return True if exist + */ +bool BKE_gpencil_has_lineart_modifiers(Object *ob) +{ + LISTBASE_FOREACH (GpencilModifierData *, md, &ob->greasepencil_modifiers) { + if (md->type == eGpencilModifierType_Lineart) { + return true; + } + } + return false; +} + /** * Check if object has grease pencil Time modifiers. * \param ob: Grease pencil object @@ -771,6 +787,7 @@ void BKE_gpencil_modifiers_calc(Depsgraph *depsgraph, Scene *scene, Object *ob) BKE_gpencil_lattice_init(ob); const bool time_remap = BKE_gpencil_has_time_modifiers(ob); + const bool has_lineart = BKE_gpencil_has_lineart_modifiers(ob); LISTBASE_FOREACH (GpencilModifierData *, md, &ob->greasepencil_modifiers) { @@ -806,6 +823,11 @@ void BKE_gpencil_modifiers_calc(Depsgraph *depsgraph, Scene *scene, Object *ob) /* Clear any lattice data. */ BKE_gpencil_lattice_clear(ob); + + if (has_lineart) { + MOD_lineart_clear_cache(gpd->runtime.lineart_cache); + gpd->runtime.lineart_cache = NULL; + } } void BKE_gpencil_modifier_blend_write(BlendWriter *writer, ListBase *modbase) diff --git a/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h b/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h index 685f0cb36cb..417e65e9d3f 100644 --- a/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h +++ b/source/blender/gpencil_modifiers/MOD_gpencil_lineart.h @@ -29,3 +29,7 @@ void OBJECT_OT_lineart_clear(struct wmOperatorType *ot); void OBJECT_OT_lineart_clear_all(struct wmOperatorType *ot); void WM_operatortypes_lineart(void); + +struct LineartCache; + +void MOD_lineart_clear_cache(struct LineartCache *lc); diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c index a2f35c8e575..5242f3575bd 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c @@ -88,7 +88,7 @@ static void generate_strokes_actual( } MOD_lineart_gpencil_generate( - lmd->render_buffer, + lmd->cache, depsgraph, ob, gpl, @@ -156,12 +156,13 @@ static void generateStrokes(GpencilModifierData *md, Depsgraph *depsgraph, Objec return; } - MOD_lineart_compute_feature_lines(depsgraph, lmd); + if (!gpd->runtime.lineart_cache) { + MOD_lineart_compute_feature_lines(depsgraph, lmd, &gpd->runtime.lineart_cache); + MOD_lineart_destroy_render_data(lmd); + } generate_strokes_actual(md, depsgraph, ob, gpl, gpf); - MOD_lineart_destroy_render_data(lmd); - WM_main_add_notifier(NA_EDITED | NC_GPENCIL, NULL); } @@ -182,11 +183,12 @@ static void bakeModifier(Main *UNUSED(bmain), return; } - MOD_lineart_compute_feature_lines(depsgraph, lmd); + if (!gpd->runtime.lineart_cache) { + MOD_lineart_compute_feature_lines(depsgraph, lmd, &gpd->runtime.lineart_cache); + MOD_lineart_destroy_render_data(lmd); + } generate_strokes_actual(md, depsgraph, ob, gpl, gpf); - - MOD_lineart_destroy_render_data(lmd); } static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams)) diff --git a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h index a1f3dbd34f1..bc8fb33065f 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h +++ b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h @@ -232,9 +232,9 @@ typedef struct LineartRenderBuffer { ListBase wasted_cuts; SpinLock lock_cuts; - /** Separate memory pool for chain data, this goes to the cache, so when we free the main pool, - * chains will still be available. */ - LineartStaticMemPool chain_data_pool; + /* This is just a reference to LineartCache::chain_data_pool, which is not cleared after line art + * completes which serves as a cache. */ + LineartStaticMemPool *chain_data_pool; /* Render status */ double view_vector[3]; @@ -312,6 +312,20 @@ typedef struct LineartRenderBuffer { } LineartRenderBuffer; +typedef struct LineartCache { + /** Separate memory pool for chain data, this goes to the cache, so when we free the main pool, + * chains will still be available. */ + LineartStaticMemPool chain_data_pool; + + /** A copy of rb->Chains after calculation is done, then we can destroy rb. */ + ListBase chains; + + /** Cache only contains edge types specified in this variable. + * TODO: it's a fixed value (LRT_EDGE_FLAG_ALL_TYPE) right now, allow further selections in the + * future. */ + char rb_edge_types; +} LineartCache; + #define DBL_TRIANGLE_LIM 1e-8 #define DBL_EDGE_LIM 1e-9 @@ -537,7 +551,8 @@ int MOD_lineart_chain_count(const LineartLineChain *rlc); void MOD_lineart_chain_clear_picked_flag(struct LineartRenderBuffer *rb); bool MOD_lineart_compute_feature_lines(struct Depsgraph *depsgraph, - struct LineartGpencilModifierData *lmd); + struct LineartGpencilModifierData *lmd, + LineartCache **cached_result); struct Scene; @@ -550,7 +565,7 @@ LineartBoundingArea *MOD_lineart_get_bounding_area(LineartRenderBuffer *rb, doub struct bGPDlayer; struct bGPDframe; -void MOD_lineart_gpencil_generate(LineartRenderBuffer *rb, +void MOD_lineart_gpencil_generate(LineartCache *cache, struct Depsgraph *depsgraph, struct Object *ob, struct bGPDlayer *gpl, diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c index d936509e5c5..bee002f8217 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c +++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_chain.c @@ -74,7 +74,7 @@ static LineartEdge *lineart_line_get_connected(LineartBoundingArea *ba, static LineartLineChain *lineart_chain_create(LineartRenderBuffer *rb) { LineartLineChain *rlc; - rlc = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChain)); + rlc = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChain)); BLI_addtail(&rb->chains, rlc); @@ -119,7 +119,7 @@ static LineartLineChainItem *lineart_chain_append_point(LineartRenderBuffer *rb, return old_rlci; } - rlci = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChainItem)); + rlci = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChainItem)); copy_v2_v2(rlci->pos, fbcoord); copy_v3_v3(rlci->gpos, gpos); @@ -149,7 +149,7 @@ static LineartLineChainItem *lineart_chain_prepend_point(LineartRenderBuffer *rb return rlc->chain.first; } - rlci = lineart_mem_aquire(&rb->chain_data_pool, sizeof(LineartLineChainItem)); + rlci = lineart_mem_aquire(rb->chain_data_pool, sizeof(LineartLineChainItem)); copy_v2_v2(rlci->pos, fbcoord); copy_v3_v3(rlci->gpos, gpos); diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c index 8e8c4785a25..ac0606f4358 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c +++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c @@ -21,6 +21,7 @@ * \ingroup editors */ +#include "MOD_gpencil_lineart.h" #include "MOD_lineart.h" #include "BLI_linklist.h" @@ -108,6 +109,8 @@ static bool lineart_triangle_edge_image_space_occlusion(SpinLock *spl, static void lineart_add_edge_to_list(LineartRenderBuffer *rb, LineartEdge *e); +static LineartCache *lineart_init_cache(void); + static void lineart_discard_segment(LineartRenderBuffer *rb, LineartLineSegment *rls) { BLI_spin_lock(&rb->lock_cuts); @@ -2628,18 +2631,17 @@ static void lineart_destroy_render_data(LineartRenderBuffer *rb) BLI_spin_end(&rb->render_data_pool.lock_mem); lineart_mem_destroy(&rb->render_data_pool); - lineart_mem_destroy(&rb->chain_data_pool); } void MOD_lineart_destroy_render_data(LineartGpencilModifierData *lmd) { - LineartRenderBuffer *rb = lmd->render_buffer; + LineartRenderBuffer *rb = lmd->render_buffer_onetime; lineart_destroy_render_data(rb); if (rb) { MEM_freeN(rb); - lmd->render_buffer = NULL; + lmd->render_buffer_onetime = NULL; } if (G.debug_value == 4000) { @@ -2647,14 +2649,32 @@ void MOD_lineart_destroy_render_data(LineartGpencilModifierData *lmd) } } +static LineartCache *lineart_init_cache() +{ + LineartCache *lc = MEM_callocN(sizeof(LineartCache *), "Lineart Cache"); + return lc; +} + +void MOD_lineart_clear_cache(struct LineartCache *lc) +{ + if (!lc) { + return; + } + lineart_mem_destroy(&lc->chain_data_pool); + MEM_freeN(lc); +} + static LineartRenderBuffer *lineart_create_render_buffer(Scene *scene, - LineartGpencilModifierData *lmd) + LineartGpencilModifierData *lmd, + LineartCache *lc) { LineartRenderBuffer *rb = MEM_callocN(sizeof(LineartRenderBuffer), "Line Art render buffer"); - lmd->render_buffer = rb; + lmd->cache = lc; + lmd->render_buffer_onetime = rb; + lc->rb_edge_types = LRT_EDGE_FLAG_ALL_TYPE; - if (!scene || !scene->camera) { + if (!scene || !scene->camera || !lc) { return NULL; } Camera *c = scene->camera->data; @@ -2695,6 +2715,8 @@ static LineartRenderBuffer *lineart_create_re @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs