From be29daeffd16f04e6ed604667563d2f4ce86588c Mon Sep 17 00:00:00 2001 From: Casey Bianco-Davis Date: Sat, 27 Jan 2024 12:18:56 -0800 Subject: [PATCH] Fix #117132: GPv3: edit mode overlay memory leaks The problem was that `grease_pencil_edit_batch_ensure` would not return early if the buffers already exist, meaning that multiple buffers would be create but only the last ones would be freed. --- .../intern/draw_cache_impl_grease_pencil.cc | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc b/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc index cbbaae26c26..9b552dc1893 100644 --- a/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc +++ b/source/blender/draw/intern/draw_cache_impl_grease_pencil.cc @@ -208,6 +208,15 @@ static void grease_pencil_edit_batch_ensure(Object &object, GreasePencilBatchCache *cache = static_cast( grease_pencil.runtime->batch_cache); + if (cache->edit_points_pos != nullptr) { + return; + } + + /* Should be discarded together. */ + BLI_assert(cache->edit_points_pos == nullptr && cache->edit_line_indices == nullptr && + cache->edit_points_indices == nullptr); + BLI_assert(cache->edit_points == nullptr && cache->edit_lines == nullptr); + /* Get the visible drawings. */ const Array drawings = ed::greasepencil::retrieve_visible_drawings(scene, grease_pencil); @@ -225,8 +234,10 @@ static void grease_pencil_edit_batch_ensure(Object &object, &format_edit_points_selection, "selection", GPU_COMP_F32, 1, GPU_FETCH_FLOAT); } - cache->edit_points_pos = GPU_vertbuf_create_with_format(&format_edit_points_pos); - cache->edit_points_selection = GPU_vertbuf_create_with_format(&format_edit_points_selection); + GPUUsageType vbo_flag = GPU_USAGE_STATIC | GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY; + cache->edit_points_pos = GPU_vertbuf_create_with_format_ex(&format_edit_points_pos, vbo_flag); + cache->edit_points_selection = GPU_vertbuf_create_with_format_ex(&format_edit_points_selection, + vbo_flag); int total_points_num = 0; for (const ed::greasepencil::DrawingInfo &info : drawings) { @@ -371,6 +382,9 @@ static void grease_pencil_edit_batch_ensure(Object &object, cache->edit_lines = GPU_batch_create( GPU_PRIM_LINE_STRIP, cache->edit_points_pos, cache->edit_line_indices); GPU_batch_vertbuf_add(cache->edit_lines, cache->edit_points_selection, false); + /* Allow creation of buffer texture. */ + GPU_vertbuf_use(cache->edit_points_pos); + GPU_vertbuf_use(cache->edit_points_selection); cache->is_dirty = false; } -- 2.30.2