Skip to content

Commit 9625270

Browse files
committed
Update some memory management and plugin unload timing
- Change to unique_ptr for BasePlugin creator - PostProcessRegion creates unique_ptr, not raw pointer - rpcore::Globals has unload function and is unloaded before removing showbase - Fix #18
1 parent 107b878 commit 9625270

12 files changed

+92
-69
lines changed

render_pipeline/rpcore/globals.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class RENDER_PIPELINE_DECL Globals
4444
{
4545
public:
4646
static void load(rppanda::ShowBase* showbase);
47+
static void unload();
4748

4849
static rppanda::ShowBase* base;
4950
static NodePath render;

render_pipeline/rpcore/pluginbase/base_plugin.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
#include <render_pipeline/rpcore/pluginbase/day_setting_types.hpp>
3131

3232
#define RENDER_PIPELINE_PLUGIN_CREATOR(PLUGIN_TYPE) \
33-
static std::shared_ptr<::rpcore::BasePlugin> rpcpp_plugin_creator__(::rpcore::RenderPipeline& pipeline) \
33+
static std::unique_ptr<::rpcore::BasePlugin> rpcpp_plugin_creator__(::rpcore::RenderPipeline& pipeline) \
3434
{ \
35-
return std::make_shared<PLUGIN_TYPE>(pipeline); \
35+
return std::make_unique<PLUGIN_TYPE>(pipeline); \
3636
} \
3737
BOOST_DLL_ALIAS(::rpcpp_plugin_creator__, create_plugin)
3838

render_pipeline/rpcore/pluginbase/manager.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RENDER_PIPELINE_DECL PluginManager : public RPObject
5050
using SettingsDataType = std::map<std::string, std::shared_ptr<BaseType>>;
5151
using DaySettingsDataType = std::map<std::string, std::shared_ptr<DayBaseType>>;
5252

53-
typedef std::shared_ptr<BasePlugin> (PluginCreatorType)(RenderPipeline&);
53+
typedef std::unique_ptr<BasePlugin> (PluginCreatorType)(RenderPipeline&);
5454

5555
public:
5656
/**

render_pipeline/rpcore/util/post_process_region.hpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@
2323
#pragma once
2424

2525
#include <nodePath.h>
26-
#include <graphicsOutput.h>
2726

2827
#include <unordered_map>
2928
#include <functional>
3029

3130
#include <render_pipeline/rpcore/rpobject.hpp>
3231

32+
class GraphicsOutput;
3333
class CallbackObject;
3434

3535
namespace rpcore {
3636

3737
class RENDER_PIPELINE_DECL PostProcessRegion
3838
{
3939
public:
40-
static PostProcessRegion* make(GraphicsOutput* internal_buffer);
41-
static PostProcessRegion* make(GraphicsOutput* internal_buffer, const LVecBase4f& dimensions);
40+
static std::unique_ptr<PostProcessRegion> make(GraphicsOutput* internal_buffer);
41+
static std::unique_ptr<PostProcessRegion> make(GraphicsOutput* internal_buffer, const LVecBase4f& dimensions);
4242

4343
public:
4444
PostProcessRegion(GraphicsOutput* internal_buffer);
@@ -67,16 +67,15 @@ class RENDER_PIPELINE_DECL PostProcessRegion
6767
std::function<void(const RenderAttrib*,int)> set_attrib;
6868
///@}
6969

70-
DisplayRegion* get_region() { return region; }
71-
NodePath get_node() const { return node; }
70+
DisplayRegion* get_region() const;
71+
NodePath get_node() const;
7272

7373
private:
7474
void init_function_pointers();
7575
void make_fullscreen_tri();
7676
void make_fullscreen_cam();
7777

78-
PT(GraphicsOutput) _buffer;
79-
PT(DisplayRegion) region;
78+
PT(DisplayRegion) region_;
8079
NodePath node;
8180
NodePath tri;
8281
NodePath camera;
@@ -90,4 +89,14 @@ inline void PostProcessRegion::set_shader_input(const ShaderInput& inp, bool ove
9089
tri.set_shader_input(inp);
9190
}
9291

92+
inline DisplayRegion* PostProcessRegion::get_region() const
93+
{
94+
return region_;
95+
}
96+
97+
inline NodePath PostProcessRegion::get_node() const
98+
{
99+
return node;
100+
}
101+
93102
}

src/rpcore/globals.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ void Globals::load(rppanda::ShowBase* showbase)
4343
Globals::resolution = LVecBase2i(0, 0);
4444
}
4545

46+
void Globals::unload()
47+
{
48+
Globals::base = nullptr;
49+
Globals::render.clear();
50+
Globals::clock = nullptr;
51+
Globals::font = nullptr;
52+
}
53+
4654
}

src/rpcore/pluginbase/base_plugin.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ BasePlugin::BasePlugin(RenderPipeline& pipeline, const std::string& plugin_id):
5050
RPObject(std::string("plugin:") + plugin_id), pipeline_(pipeline), plugin_id_(plugin_id),
5151
impl_(std::make_unique<Impl>())
5252
{
53+
trace(fmt::format("Constructing '{}' plugin", plugin_id_));
5354
}
5455

5556
BasePlugin::~BasePlugin()
5657
{
58+
trace(fmt::format("Destructing '{}' plugin", plugin_id_));
59+
5760
auto manager = pipeline_.get_stage_mgr();
5861
if (!manager)
5962
return;

src/rpcore/pluginbase/manager.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class PluginManager::Impl
7272
void unload();
7373

7474
/** Internal method to load a plugin. */
75-
std::shared_ptr<BasePlugin> load_plugin(const std::string& plugin_id);
75+
std::unique_ptr<BasePlugin> load_plugin(const std::string& plugin_id);
7676

7777
void load_plugin_settings(const std::string& plugin_id, const Filename& plugin_pth);
7878

@@ -137,11 +137,10 @@ void PluginManager::Impl::unload()
137137
day_settings_.clear();
138138
enabled_plugins_.clear();
139139

140-
// unload DLL of plugins.
141-
plugin_creators_.clear();
140+
// NOTE: DLLs are not unloaded, yet.
142141
}
143142

144-
std::shared_ptr<BasePlugin> PluginManager::Impl::load_plugin(const std::string& plugin_id)
143+
std::unique_ptr<BasePlugin> PluginManager::Impl::load_plugin(const std::string& plugin_id)
145144
{
146145
boost::filesystem::path plugin_path = get_canonical_path(plugin_dir_ / plugin_id);
147146

@@ -381,6 +380,8 @@ PluginManager::~PluginManager()
381380
trace("Destructing PluginManager");
382381

383382
unload();
383+
384+
// unload DLL of plugins.
384385
}
385386

386387
void PluginManager::load()
@@ -401,10 +402,10 @@ void PluginManager::load()
401402
continue;
402403
}
403404

404-
std::shared_ptr<BasePlugin> handle = impl_->load_plugin(plugin_id);
405+
auto handle = impl_->load_plugin(plugin_id);
405406

406407
if (handle)
407-
impl_->instances_[plugin_id] = handle;
408+
impl_->instances_[plugin_id] = std::move(handle);
408409
else
409410
disable_plugin(plugin_id);
410411
}

src/rpcore/render_pipeline.cpp

+27-20
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,9 @@ class RenderPipeline::Impl
251251
std::unique_ptr<LoadingScreen> loading_screen_;
252252
std::unique_ptr<CommonResources> common_resources_;
253253

254-
std::unique_ptr<AmbientStage> ambient_stage_;
255-
std::unique_ptr<GBufferStage> gbuffer_stage_;
256-
std::unique_ptr<FinalStage> final_stage_;
257-
std::unique_ptr<DownscaleZStage> downscale_stage_;
258-
std::unique_ptr<CombineVelocityStage> combine_velocity_stage_;
259-
std::unique_ptr<UpscaleStage> upscale_stage_;
260-
261-
PT(rppanda::ShowBase) showbase_ = nullptr;
254+
std::vector<std::unique_ptr<RenderStage>> internal_stages_;
255+
256+
PT(rppanda::ShowBase) showbase_;
262257
std::unique_ptr<TaskScheduler> task_scheduler_;
263258
std::unique_ptr<TagStateManager> tag_mgr_;
264259
std::unique_ptr<PluginManager> plugin_mgr_;
@@ -289,6 +284,12 @@ RenderPipeline::Impl::~Impl()
289284
debugger_.reset();
290285
loading_screen_.reset();
291286

287+
plugin_mgr_->unload();
288+
289+
internal_stages_.clear();
290+
291+
Globals::unload();
292+
292293
showbase_.clear();
293294

294295
// should delete at last to delete resources in DLL module.
@@ -761,26 +762,32 @@ void RenderPipeline::Impl::init_common_stages()
761762
{
762763
self_.trace("Initailizing common stages ...");
763764

764-
ambient_stage_ = std::make_unique<AmbientStage>(self_);
765-
stage_mgr_->add_stage(ambient_stage_.get());
765+
auto ambient_stage = std::make_unique<AmbientStage>(self_);
766+
stage_mgr_->add_stage(ambient_stage.get());
767+
internal_stages_.push_back(std::move(ambient_stage));
766768

767-
gbuffer_stage_ = std::make_unique<GBufferStage>(self_);
768-
stage_mgr_->add_stage(gbuffer_stage_.get());
769+
auto gbuffer_stage = std::make_unique<GBufferStage>(self_);
770+
stage_mgr_->add_stage(gbuffer_stage.get());
771+
internal_stages_.push_back(std::move(gbuffer_stage));
769772

770-
final_stage_ = std::make_unique<FinalStage>(self_);
771-
stage_mgr_->add_stage(final_stage_.get());
773+
auto final_stage = std::make_unique<FinalStage>(self_);
774+
stage_mgr_->add_stage(final_stage.get());
775+
internal_stages_.push_back(std::move(final_stage));
772776

773-
downscale_stage_ = std::make_unique<DownscaleZStage>(self_);
774-
stage_mgr_->add_stage(downscale_stage_.get());
777+
auto downscale_stage = std::make_unique<DownscaleZStage>(self_);
778+
stage_mgr_->add_stage(downscale_stage.get());
779+
internal_stages_.push_back(std::move(downscale_stage));
775780

776-
combine_velocity_stage_ = std::make_unique<CombineVelocityStage>(self_);
777-
stage_mgr_->add_stage(combine_velocity_stage_.get());
781+
auto combine_velocity_stage = std::make_unique<CombineVelocityStage>(self_);
782+
stage_mgr_->add_stage(combine_velocity_stage.get());
783+
internal_stages_.push_back(std::move(combine_velocity_stage));
778784

779785
// Add an upscale/downscale stage in case we render at a different resolution
780786
if (Globals::resolution != Globals::native_resolution)
781787
{
782-
upscale_stage_ = std::make_unique<UpscaleStage>(self_);
783-
stage_mgr_->add_stage(upscale_stage_.get());
788+
auto upscale_stage = std::make_unique<UpscaleStage>(self_);
789+
stage_mgr_->add_stage(upscale_stage.get());
790+
internal_stages_.push_back(std::move(upscale_stage));
784791
}
785792
}
786793

src/rpcore/render_target.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class RenderTarget::Impl
6767
public:
6868
RenderTarget& self_;
6969

70-
PT(GraphicsBuffer) internal_buffer_ = nullptr;
70+
GraphicsBuffer* internal_buffer_ = nullptr;
7171
GraphicsWindow* source_window_ = nullptr;
7272

7373
PT(DisplayRegion) source_display_region_ = nullptr;
74-
PostProcessRegion* source_postprocess_region_ = nullptr;
74+
std::unique_ptr<PostProcessRegion> source_postprocess_region_;
7575

7676
GraphicsEngine* engine_ = nullptr;
7777

@@ -120,11 +120,7 @@ void RenderTarget::Impl::prepare_render(const NodePath& camera_np)
120120
create_default_region_ = false;
121121
create_buffer();
122122
source_display_region_ = internal_buffer_->get_display_region(0);
123-
if (source_postprocess_region_)
124-
{
125-
delete source_postprocess_region_;
126-
source_postprocess_region_ = nullptr;
127-
}
123+
source_postprocess_region_.reset();
128124

129125
if (!camera_np.is_empty())
130126
{
@@ -176,14 +172,11 @@ void RenderTarget::Impl::remove()
176172
engine_->remove_window(internal_buffer_);
177173

178174
RenderTarget::REGISTERED_TARGETS.erase(std::find(RenderTarget::REGISTERED_TARGETS.begin(), RenderTarget::REGISTERED_TARGETS.end(), &self_));
179-
internal_buffer_.clear();
180-
}
181-
else
182-
{
183-
delete source_postprocess_region_;
184-
source_postprocess_region_ = nullptr;
175+
internal_buffer_ = nullptr;
185176
}
186177

178+
source_postprocess_region_.reset();
179+
187180
active_ = false;
188181
for (const auto& target: targets_)
189182
target.second->release_all();
@@ -558,7 +551,7 @@ DisplayRegion* RenderTarget::get_display_region() const
558551

559552
PostProcessRegion* RenderTarget::get_postprocess_region() const
560553
{
561-
return impl_->source_postprocess_region_;
554+
return impl_->source_postprocess_region_.get();
562555
}
563556

564557
void RenderTarget::consider_resize()

src/rpcore/stages/gbuffer_stage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ GBufferStage::ProduceType GBufferStage::get_produced_pipes() const
4343

4444
std::shared_ptr<SimpleInputBlock> GBufferStage::make_gbuffer_ubo() const
4545
{
46-
std::shared_ptr<SimpleInputBlock> ubo(new SimpleInputBlock("GBuffer"));
46+
std::shared_ptr<SimpleInputBlock> ubo = std::make_shared<SimpleInputBlock>("GBuffer");
4747
ubo->add_input("Depth", target_->get_depth_tex());
4848
ubo->add_input("Data0", target_->get_color_tex());
4949
ubo->add_input("Data1", target_->get_aux_tex(0));

src/rpcore/util/post_process_region.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@
2626
#include <geomTriangles.h>
2727
#include <omniBoundingVolume.h>
2828
#include <orthographicLens.h>
29+
#include <graphicsOutput.h>
2930

3031
namespace rpcore {
3132

32-
PostProcessRegion* PostProcessRegion::make(GraphicsOutput* internal_buffer)
33+
std::unique_ptr<PostProcessRegion> PostProcessRegion::make(GraphicsOutput* internal_buffer)
3334
{
34-
return new PostProcessRegion(internal_buffer);
35+
return std::make_unique<PostProcessRegion>(internal_buffer);
3536
}
3637

37-
PostProcessRegion* PostProcessRegion::make(GraphicsOutput* internal_buffer, const LVecBase4f& dimensions)
38+
std::unique_ptr<PostProcessRegion> PostProcessRegion::make(GraphicsOutput* internal_buffer, const LVecBase4f& dimensions)
3839
{
39-
return new PostProcessRegion(internal_buffer, dimensions);
40+
return std::make_unique<PostProcessRegion>(internal_buffer, dimensions);
4041
}
4142

4243
PostProcessRegion::PostProcessRegion(GraphicsOutput* internal_buffer)
4344
{
44-
_buffer = internal_buffer;
45-
region = _buffer->make_display_region();
45+
region_ = internal_buffer->make_display_region();
4646
node = NodePath("RTRoot");
4747

4848
make_fullscreen_tri();
@@ -52,8 +52,7 @@ PostProcessRegion::PostProcessRegion(GraphicsOutput* internal_buffer)
5252

5353
PostProcessRegion::PostProcessRegion(GraphicsOutput* internal_buffer, const LVecBase4f& dimensions)
5454
{
55-
_buffer = internal_buffer;
56-
region = _buffer->make_display_region(dimensions);
55+
region_ = internal_buffer->make_display_region(dimensions);
5756
node = NodePath("RTRoot");
5857

5958
make_fullscreen_tri();
@@ -65,15 +64,15 @@ void PostProcessRegion::init_function_pointers()
6564
{
6665
using namespace std::placeholders;
6766

68-
set_sort = std::bind(&DisplayRegion::set_sort, region, _1);
69-
disable_clears = std::bind(&DisplayRegion::disable_clears, region);
70-
set_active = std::bind(&DisplayRegion::set_active, region, _1);
71-
set_clear_depth_active = std::bind(&DisplayRegion::set_clear_depth_active, region, _1);
72-
set_clear_depth = std::bind(&DisplayRegion::set_clear_depth, region, _1);
73-
set_camera = std::bind(&DisplayRegion::set_camera, region, _1);
74-
set_clear_color_active = std::bind(&DisplayRegion::set_clear_color_active, region, _1);
75-
set_clear_color = std::bind(&DisplayRegion::set_clear_color, region, _1);
76-
set_draw_callback = std::bind(&DisplayRegion::set_draw_callback, region, _1);
67+
set_sort = std::bind(&DisplayRegion::set_sort, region_, _1);
68+
disable_clears = std::bind(&DisplayRegion::disable_clears, region_);
69+
set_active = std::bind(&DisplayRegion::set_active, region_, _1);
70+
set_clear_depth_active = std::bind(&DisplayRegion::set_clear_depth_active, region_, _1);
71+
set_clear_depth = std::bind(&DisplayRegion::set_clear_depth, region_, _1);
72+
set_camera = std::bind(&DisplayRegion::set_camera, region_, _1);
73+
set_clear_color_active = std::bind(&DisplayRegion::set_clear_color_active, region_, _1);
74+
set_clear_color = std::bind(&DisplayRegion::set_clear_color, region_, _1);
75+
set_draw_callback = std::bind(&DisplayRegion::set_draw_callback, region_, _1);
7776

7877
set_instance_count = std::bind(&NodePath::set_instance_count, tri, _1);
7978
set_shader = std::bind(&NodePath::set_shader, tri, _1, _2);
@@ -119,7 +118,7 @@ void PostProcessRegion::make_fullscreen_cam()
119118
PT(OmniBoundingVolume) obv = new OmniBoundingVolume();
120119
buffer_cam->set_cull_bounds(obv);
121120
camera = node.attach_new_node(buffer_cam);
122-
region->set_camera(camera);
121+
region_->set_camera(camera);
123122
}
124123

125124
}

src/rpcore/util/shader_input_blocks.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "render_pipeline/rpcore/util/shader_input_blocks.hpp"
2424

25+
#include <graphicsOutput.h>
26+
2527
#include <regex>
2628

2729
#include <boost/format.hpp>

0 commit comments

Comments
 (0)