From 58f403f585f3e547940d89ea608f56995d7f71ba Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 11 Dec 2023 20:56:11 +0200 Subject: [PATCH] Use unique_ptr to manage memory outside the shader compiler The compiler has its own complexities and will be converted in a later commit. --- demos/desertpillars/source/desertpillars.cpp | 4 +- demos/forestpond/source/forestpond.cpp | 4 +- source/animation/animation.cpp | 55 ++++++------- source/animation/animation.h | 18 ++--- source/animation/keyframe.cpp | 7 +- source/animation/keyframe.h | 1 - source/backends/opengl/texture1d_backend.h | 2 +- source/backends/opengl/texture2d_backend.cpp | 22 +----- source/backends/opengl/texture2d_backend.h | 8 +- .../opengl/texture2dmultisample_backend.h | 2 +- source/backends/opengl/texture3d_backend.h | 2 +- source/backends/opengl/texturecube_backend.h | 2 +- source/backends/vulkan/device_backend.cpp | 4 +- source/backends/vulkan/device_backend.h | 4 +- source/backends/vulkan/program_backend.cpp | 4 +- source/backends/vulkan/texture1d_backend.h | 2 +- .../vulkan/texture2dmultisample_backend.h | 2 +- source/backends/vulkan/texture3d_backend.h | 2 +- source/backends/vulkan/texturecube_backend.h | 2 +- source/backends/vulkan/windowview_backend.cpp | 13 ++- source/backends/vulkan/windowview_backend.h | 3 +- source/builders/font.cpp | 7 +- source/builders/meshbuilder.cpp | 5 +- source/builders/meshbuilder.h | 3 +- source/builders/sequencebuilder.cpp | 30 +++---- source/builders/sequencebuilder.h | 11 +-- source/builders/sequencetemplate.cpp | 30 +++---- source/builders/sequencetemplate.h | 9 +-- source/core/bufferable.cpp | 4 +- source/core/bufferable.h | 5 +- source/core/mesh.cpp | 52 +++--------- source/core/mesh.h | 14 ++-- source/core/module.cpp | 4 +- source/core/module.h | 3 +- source/core/program.cpp | 15 +--- source/core/program.h | 6 +- source/core/texture.cpp | 2 +- source/core/texture2d.cpp | 20 ++--- source/core/texture2d.h | 2 +- source/core/texturecube.cpp | 2 +- source/effects/ambientocclusion.cpp | 7 +- source/effects/bloom.cpp | 10 +-- source/effects/bloom.h | 3 +- source/effects/colorcurve.cpp | 2 +- source/effects/environmentmap.cpp | 2 +- source/effects/shadowmap.cpp | 2 +- source/effects/sky.cpp | 2 +- source/glsl/builtin.cpp | 2 +- source/glsl/modulecache.cpp | 22 ++---- source/glsl/modulecache.h | 4 +- source/materials/material.cpp | 14 +--- source/materials/pbrmaterial.cpp | 5 +- source/materials/programdata.cpp | 23 +++--- source/materials/programdata.h | 5 +- source/materials/rendermethod.cpp | 20 ++--- source/materials/rendermethod.h | 4 +- source/materials/splatmaterial.cpp | 26 +++--- source/materials/splatmaterial.h | 2 +- source/materials/technique.cpp | 6 +- source/render/instancearray.cpp | 5 +- source/render/instancearray.h | 3 +- source/render/object.cpp | 14 ++-- source/render/rendertarget.cpp | 20 ++--- source/render/rendertarget.h | 3 +- source/render/scene.cpp | 16 ++-- source/render/sequence.cpp | 18 ++--- source/render/sequence.h | 7 +- source/render/view.cpp | 18 ++--- source/render/view.h | 6 +- source/resources/resource.h | 3 +- source/resources/resourcemanager.cpp | 11 +-- source/resources/resourcemanager.h | 4 +- source/resources/resources.cpp | 79 +++++++++---------- source/resources/resources.h | 10 +-- tools/viewer.cpp | 51 ++++++------ 75 files changed, 340 insertions(+), 476 deletions(-) diff --git a/demos/desertpillars/source/desertpillars.cpp b/demos/desertpillars/source/desertpillars.cpp index 7c285185..a6ebeccd 100644 --- a/demos/desertpillars/source/desertpillars.cpp +++ b/demos/desertpillars/source/desertpillars.cpp @@ -43,12 +43,12 @@ DesertPillars::DesertPillars(int, char **): GL::SequenceBuilder seq_bld(resources.get("Desert.seq")); seq_bld.set_renderable("content", content); seq_bld.set_debug_name("Main sequence"); - sequence.reset(seq_bld.build(view)); + sequence = seq_bld.build(view); GL::SequenceBuilder env_bld(resources.get("Desert_environment.seq")); env_bld.set_renderable("content", *sequence->get_steps().front().get_renderable()); env_bld.set_debug_name("Environment sequence"); - env_seq.reset(env_bld.build()); + env_seq = env_bld.build(); env_map = make_unique(256, GL::RGBA16F, 7, sphere, *env_seq); env_map->set_debug_name("Environment map"); diff --git a/demos/forestpond/source/forestpond.cpp b/demos/forestpond/source/forestpond.cpp index 613189e0..02bbe96e 100644 --- a/demos/forestpond/source/forestpond.cpp +++ b/demos/forestpond/source/forestpond.cpp @@ -23,12 +23,12 @@ ForestPond::ForestPond(int, char **): GL::SequenceBuilder seq_bld(resources.get("Forest.seq")); seq_bld.set_debug_name("Main sequence"); seq_bld.set_renderable("content", content); - sequence.reset(seq_bld.build(view)); + sequence = seq_bld.build(view); GL::SequenceBuilder env_bld(resources.get("Forest_environment.seq")); env_bld.set_debug_name("Environment sequence"); env_bld.set_renderable("content", *sequence->get_steps().front().get_renderable()); - env_seq.reset(env_bld.build()); + env_seq = env_bld.build(); env_map = std::make_unique(512, GL::RGBA16F, 5, water, *env_seq); env_map->set_debug_name("Water environment"); diff --git a/source/animation/animation.cpp b/source/animation/animation.cpp index a733e7c1..0a908583 100644 --- a/source/animation/animation.cpp +++ b/source/animation/animation.cpp @@ -16,15 +16,6 @@ using namespace std; namespace Msp { namespace GL { -Animation::~Animation() -{ - for(TimedKeyFrame &k: keyframes) - if(k.owned) - delete k.keyframe; - for(Curve *c: curves) - delete c; -} - void Animation::set_armature(const Armature &a) { if(!keyframes.empty() && &a!=armature) @@ -53,21 +44,22 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf) create_curves(); } -void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf) +void Animation::add_keyframe(const Time::TimeDelta &t, unique_ptr kf) { - add_keyframe(t, kf, false, true); + add_keyframe(t, kf.get(), false, true); + owned_keyframes.emplace_back(move(kf)); create_curves(); } void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope) { - add_keyframe(t, &kf, slope, slope, false); + add_keyframe(t, &kf, slope, slope); create_curves(); } void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es) { - add_keyframe(t, &kf, ss, es, false); + add_keyframe(t, &kf, ss, es); create_curves(); } @@ -79,18 +71,19 @@ void Animation::add_control_keyframe(const KeyFrame &kf) add_keyframe(keyframes.back().time, &kf, true, false); } -void Animation::add_control_keyframe_owned(const KeyFrame *kf) +void Animation::add_control_keyframe(unique_ptr kf) { if(keyframes.empty()) throw invalid_operation("Animation::add_control_keyframe_owned"); - add_keyframe(keyframes.back().time, kf, true, true); + add_keyframe(keyframes.back().time, kf.get(), true, true); + owned_keyframes.emplace_back(move(kf)); } -void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned) +void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es) { if(keyframes.empty()) - return add_keyframe(t, kf, false, owned); + return add_keyframe(t, kf, false); if(keyframes.back().control) throw invalid_operation("Animation::add_keyframe"); @@ -103,7 +96,7 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float for(unsigned i=1; i<=2; ++i) { float x = (i==1 ? ss/3 : 1-es/3); - KeyFrame *ckf = new KeyFrame; + unique_ptr ckf = make_unique(); Transform ctrn; ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x); const Transform::AngleVector3 &e1 = last_trn.get_euler(); @@ -125,13 +118,14 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ckf->set_uniform(kvp.first, uni); } - add_keyframe(t, ckf, true, true); + add_keyframe(t, ckf.get(), true); + owned_keyframes.emplace_back(move(ckf)); } - add_keyframe(t, kf, false, owned); + add_keyframe(t, kf, false); } -void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned) +void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c) { if(c && keyframes.empty()) throw invalid_argument("Animation::add_keyframe"); @@ -157,7 +151,6 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool tkf.time = t; tkf.keyframe = kf; tkf.control = c; - tkf.owned = owned; keyframes.push_back(tkf); @@ -170,8 +163,6 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool void Animation::create_curves() { - for(Curve *c: curves) - delete c; curves.clear(); curves.reserve(6+uniforms.size()); @@ -271,7 +262,7 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract knots.push_back(knots.back()); } - curves.push_back(new ValueCurve(target, component, knots)); + curves.emplace_back(make_unique>(target, component, knots)); } bool Animation::extract_position(const KeyFrame &kf, Vector3 &value) @@ -522,12 +513,12 @@ void Animation::Loader::check_slopes_and_control(bool s, bool c) throw logic_error("can't use both slopes and control keyframes in same segment"); } -void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned) +void Animation::Loader::add_kf(const KeyFrame *kf, bool c) { if(slopes_set && !c) - obj.add_keyframe(current_time, kf, start_slope, end_slope, owned); + obj.add_keyframe(current_time, kf, start_slope, end_slope); else - obj.add_keyframe(current_time, kf, c, owned); + obj.add_keyframe(current_time, kf, c); start_slope = end_slope; end_slope = 1; @@ -536,12 +527,12 @@ void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned) void Animation::Loader::load_kf(const string &n, bool c) { - add_kf(&get_collection().get(n), c, false); + add_kf(&get_collection().get(n), c); } void Animation::Loader::load_kf_inline(bool c) { - RefPtr kf = new KeyFrame; + unique_ptr kf = make_unique(); if(coll) { KeyFrame::Loader ldr(*kf, get_collection()); @@ -551,8 +542,8 @@ void Animation::Loader::load_kf_inline(bool c) else load_sub(*kf); - add_kf(kf.get(), c, true); - kf.release(); + add_kf(kf.get(), c); + obj.owned_keyframes.emplace_back(move(kf)); } void Animation::Loader::control_keyframe(const string &n) diff --git a/source/animation/animation.h b/source/animation/animation.h index 8be5ff61..3a760454 100644 --- a/source/animation/animation.h +++ b/source/animation/animation.h @@ -1,7 +1,7 @@ #ifndef MSP_GL_ANIMATION_H_ #define MSP_GL_ANIMATION_H_ -#include +#include #include #include #include @@ -39,7 +39,7 @@ public: void finish() override; void check_slopes_and_control(bool, bool); - void add_kf(const KeyFrame *, bool, bool); + void add_kf(const KeyFrame *, bool); void load_kf(const std::string &, bool); void load_kf_inline(bool); @@ -124,7 +124,6 @@ private: Time::TimeDelta time; const KeyFrame *keyframe; bool control; - bool owned; }; struct Event @@ -166,15 +165,14 @@ public: private: const Armature *armature = nullptr; std::vector keyframes; + std::vector> owned_keyframes; std::vector events; bool looping = false; std::vector uniforms; - std::vector curves; + std::vector> curves; unsigned uniform_curve_offset = 0; public: - ~Animation(); - void set_armature(const Armature &); const Armature *get_armature() const { return armature; } @@ -183,14 +181,14 @@ public: const std::string &get_uniform_name(unsigned) const; void add_keyframe(const Time::TimeDelta &, const KeyFrame &); - void add_keyframe_owned(const Time::TimeDelta &, const KeyFrame *); + void add_keyframe(const Time::TimeDelta &, std::unique_ptr); DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float); DEPRECATED void add_keyframe(const Time::TimeDelta &, const KeyFrame &, float, float); void add_control_keyframe(const KeyFrame &); - void add_control_keyframe_owned(const KeyFrame *); + void add_control_keyframe(std::unique_ptr); private: - void add_keyframe(const Time::TimeDelta &, const KeyFrame *, float, float, bool); - void add_keyframe(const Time::TimeDelta &, const KeyFrame *, bool, bool); + void add_keyframe(const Time::TimeDelta &, const KeyFrame *, float, float); + void add_keyframe(const Time::TimeDelta &, const KeyFrame *, bool); void prepare_keyframe(TimedKeyFrame &); void create_curves(); void create_curve(CurveTarget, Transform::ComponentMask, ExtractComponent::Extract); diff --git a/source/animation/keyframe.cpp b/source/animation/keyframe.cpp index d1f1a7ea..d6466505 100644 --- a/source/animation/keyframe.cpp +++ b/source/animation/keyframe.cpp @@ -67,10 +67,11 @@ void KeyFrame::Loader::pose(const string &n) void KeyFrame::Loader::pose_inline() { - RefPtr p = new Pose; + unique_ptr p = make_unique(); load_sub(*p, get_collection()); - get_collection().add((inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name)+".pose", p.get()); - obj.pose = p.release(); + Pose *ptr = p.get(); + get_collection().add((inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name)+".pose", move(p)); + obj.pose = ptr; } void KeyFrame::Loader::position(float x, float y, float z) diff --git a/source/animation/keyframe.h b/source/animation/keyframe.h index 55f8c341..2abe7167 100644 --- a/source/animation/keyframe.h +++ b/source/animation/keyframe.h @@ -1,7 +1,6 @@ #ifndef MSP_GL_KEYFRAME_H_ #define MSP_GL_KEYFRAME_H_ -#include #include #include "matrix.h" #include "mspgl_api.h" diff --git a/source/backends/opengl/texture1d_backend.h b/source/backends/opengl/texture1d_backend.h index 54ceed58..67c67414 100644 --- a/source/backends/opengl/texture1d_backend.h +++ b/source/backends/opengl/texture1d_backend.h @@ -16,7 +16,7 @@ protected: void sub_image(unsigned, int, unsigned, const void *); public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/opengl/texture2d_backend.cpp b/source/backends/opengl/texture2d_backend.cpp index 88720c3c..84dff968 100644 --- a/source/backends/opengl/texture2d_backend.cpp +++ b/source/backends/opengl/texture2d_backend.cpp @@ -85,31 +85,11 @@ void OpenGLTexture2D::unload() } -OpenGLTexture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other): - pixel_buffer(other.pixel_buffer) -{ - other.pixel_buffer = nullptr; -} - -OpenGLTexture2D::AsyncTransfer &OpenGLTexture2D::AsyncTransfer::operator=(AsyncTransfer &&other) -{ - delete pixel_buffer; - pixel_buffer = other.pixel_buffer; - other.pixel_buffer = nullptr; - - return *this; -} - -OpenGLTexture2D::AsyncTransfer::~AsyncTransfer() -{ - delete pixel_buffer; -} - void *OpenGLTexture2D::AsyncTransfer::allocate() { const Texture2D::AsyncTransfer &self = *static_cast(this); - pixel_buffer = new Buffer; + pixel_buffer = make_unique(); pixel_buffer->storage(self.data_size, STREAMING); return pixel_buffer->map(); } diff --git a/source/backends/opengl/texture2d_backend.h b/source/backends/opengl/texture2d_backend.h index c7d0fa5f..5423cfe3 100644 --- a/source/backends/opengl/texture2d_backend.h +++ b/source/backends/opengl/texture2d_backend.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_TEXTURE2D_BACKEND_H_ #define MSP_GL_TEXTURE2D_BACKEND_H_ +#include #include "mspgl_api.h" #include "texture.h" @@ -15,12 +16,7 @@ protected: class MSPGL_API AsyncTransfer: public NonCopyable { protected: - Buffer *pixel_buffer = nullptr; - - AsyncTransfer() = default; - AsyncTransfer(AsyncTransfer &&); - AsyncTransfer &operator=(AsyncTransfer &&); - ~AsyncTransfer(); + std::unique_ptr pixel_buffer; void *allocate(); void finalize(); diff --git a/source/backends/opengl/texture2dmultisample_backend.h b/source/backends/opengl/texture2dmultisample_backend.h index 17fb8411..cad6ab6c 100644 --- a/source/backends/opengl/texture2dmultisample_backend.h +++ b/source/backends/opengl/texture2dmultisample_backend.h @@ -15,7 +15,7 @@ protected: void allocate(); public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/opengl/texture3d_backend.h b/source/backends/opengl/texture3d_backend.h index fc17fb89..45da329c 100644 --- a/source/backends/opengl/texture3d_backend.h +++ b/source/backends/opengl/texture3d_backend.h @@ -19,7 +19,7 @@ protected: bool is_array() const; public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/opengl/texturecube_backend.h b/source/backends/opengl/texturecube_backend.h index b75acf8c..1767ab02 100644 --- a/source/backends/opengl/texturecube_backend.h +++ b/source/backends/opengl/texturecube_backend.h @@ -16,7 +16,7 @@ protected: void sub_image(unsigned, unsigned, int, int, unsigned, unsigned, const void *); public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/vulkan/device_backend.cpp b/source/backends/vulkan/device_backend.cpp index d80afaab..50e34c4f 100644 --- a/source/backends/vulkan/device_backend.cpp +++ b/source/backends/vulkan/device_backend.cpp @@ -14,7 +14,7 @@ VulkanDevice::VulkanDevice(Graphics::Window &wnd, const Graphics::VulkanOptions context(wnd, opts), device(handle_cast(context.get_private().device)), graphics_queue(handle_cast(context.get_private().graphics_queue)), - functions(new VulkanFunctions(context)), + functions(make_unique(context)), allocator(*static_cast(this)), destroy_queue(*static_cast(this)), synchronizer(*static_cast(this)), @@ -23,7 +23,7 @@ VulkanDevice::VulkanDevice(Graphics::Window &wnd, const Graphics::VulkanOptions descriptor_pool(*static_cast(this)) { } -// Cause the destructor of RefPtr to be emitted here +// Cause the destructor of unique_ptr to be emitted here VulkanDevice::~VulkanDevice() { } diff --git a/source/backends/vulkan/device_backend.h b/source/backends/vulkan/device_backend.h index 8066df64..4f9b17d0 100644 --- a/source/backends/vulkan/device_backend.h +++ b/source/backends/vulkan/device_backend.h @@ -1,8 +1,8 @@ #ifndef MSP_GL_DEVICE_BACKEND_H_ #define MSP_GL_DEVICE_BACKEND_H_ +#include #include -#include #include #include "descriptorpool.h" #include "destroyqueue.h" @@ -26,7 +26,7 @@ protected: Graphics::VulkanContext context; VkDevice device; VkQueue graphics_queue; - RefPtr functions; + std::unique_ptr functions; MemoryAllocator allocator; DestroyQueue destroy_queue; Synchronizer synchronizer; diff --git a/source/backends/vulkan/program_backend.cpp b/source/backends/vulkan/program_backend.cpp index b6ddb12d..2ee660ce 100644 --- a/source/backends/vulkan/program_backend.cpp +++ b/source/backends/vulkan/program_backend.cpp @@ -103,7 +103,7 @@ void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map(this)->specialized_spirv) + if(SpirVModule *spirv = static_cast(this)->specialized_spirv.get()) spirv->set_debug_name(debug_name); #endif } @@ -194,7 +194,7 @@ void VulkanProgram::set_debug_name(const string &name) #ifdef DEBUG debug_name = name; set_vulkan_object_name(); - if(SpirVModule *spirv = static_cast(this)->specialized_spirv) + if(SpirVModule *spirv = static_cast(this)->specialized_spirv.get()) spirv->set_debug_name(debug_name); #else (void)name; diff --git a/source/backends/vulkan/texture1d_backend.h b/source/backends/vulkan/texture1d_backend.h index 9d807f71..2a7d7346 100644 --- a/source/backends/vulkan/texture1d_backend.h +++ b/source/backends/vulkan/texture1d_backend.h @@ -17,7 +17,7 @@ protected: void fill_mipmap_blit(unsigned, void *) override; public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/vulkan/texture2dmultisample_backend.h b/source/backends/vulkan/texture2dmultisample_backend.h index 108acda7..a2bf43c2 100644 --- a/source/backends/vulkan/texture2dmultisample_backend.h +++ b/source/backends/vulkan/texture2dmultisample_backend.h @@ -17,7 +17,7 @@ protected: void fill_mipmap_blit(unsigned, void *) override { } public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/vulkan/texture3d_backend.h b/source/backends/vulkan/texture3d_backend.h index dc52829f..9a6c4359 100644 --- a/source/backends/vulkan/texture3d_backend.h +++ b/source/backends/vulkan/texture3d_backend.h @@ -20,7 +20,7 @@ protected: bool is_array() const; public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/vulkan/texturecube_backend.h b/source/backends/vulkan/texturecube_backend.h index 9d824411..6926f0ea 100644 --- a/source/backends/vulkan/texturecube_backend.h +++ b/source/backends/vulkan/texturecube_backend.h @@ -17,7 +17,7 @@ protected: void fill_mipmap_blit(unsigned, void *) override; public: - AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; } std::size_t get_data_size() const override; void unload() override { } }; diff --git a/source/backends/vulkan/windowview_backend.cpp b/source/backends/vulkan/windowview_backend.cpp index 433d372a..c820f1ba 100644 --- a/source/backends/vulkan/windowview_backend.cpp +++ b/source/backends/vulkan/windowview_backend.cpp @@ -4,13 +4,13 @@ #include "windowview.h" #include "windowview_backend.h" +using namespace std; + namespace Msp { namespace GL { -VulkanWindowView::~VulkanWindowView() -{ - delete swap_chain; -} +// Hide Semaphore destructor since it's not exported +VulkanWindowView::~VulkanWindowView() = default; void VulkanWindowView::render() { @@ -21,7 +21,7 @@ void VulkanWindowView::render() current_target = &framebuffers[image_index]; if(!internal_renderer) - internal_renderer = new Renderer; + internal_renderer = make_unique(); internal_renderer->begin(sem[0]); View::render(*internal_renderer); internal_renderer->end(sem[1]); @@ -37,9 +37,8 @@ void VulkanWindowView::resize_framebuffer(unsigned w, unsigned h) Device &device = static_cast(this)->device; framebuffers.clear(); - delete swap_chain; - swap_chain = new SwapChain(w, h, device.get_n_frames_in_flight()); + swap_chain = make_unique(w, h, device.get_n_frames_in_flight()); unsigned n_images = swap_chain->get_n_images(); framebuffers.reserve(n_images); diff --git a/source/backends/vulkan/windowview_backend.h b/source/backends/vulkan/windowview_backend.h index f98a6563..b7b4c9b3 100644 --- a/source/backends/vulkan/windowview_backend.h +++ b/source/backends/vulkan/windowview_backend.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_WINDOWVIEW_BACKEND_H_ #define MSP_GL_WINDOWVIEW_BACKEND_H_ +#include #include "device.h" #include "framebuffer.h" #include "mspgl_api.h" @@ -14,7 +15,7 @@ namespace GL { class MSPGL_API VulkanWindowView: public View { protected: - SwapChain *swap_chain = nullptr; + std::unique_ptr swap_chain; std::vector framebuffers; Framebuffer *current_target = nullptr; Semaphore semaphores[MAX_FRAMES_IN_FLIGHT*2]; diff --git a/source/builders/font.cpp b/source/builders/font.cpp index aac416ba..89a70cb3 100644 --- a/source/builders/font.cpp +++ b/source/builders/font.cpp @@ -152,10 +152,11 @@ void Font::Loader::ligature(unsigned l, unsigned r, unsigned g) void Font::Loader::texture() { - RefPtr tex = new Texture2D; + unique_ptr tex = make_unique(); load_sub(*tex); - get_collection().add(FS::basename(get_source())+".tex", tex.get()); - obj.texture = tex.release(); + Texture2D *ptr = tex.get(); + get_collection().add(FS::basename(get_source())+".tex", move(tex)); + obj.texture = ptr; } void Font::Loader::texture_ref(const string &name) diff --git a/source/builders/meshbuilder.cpp b/source/builders/meshbuilder.cpp index a8a47117..6971b5d5 100644 --- a/source/builders/meshbuilder.cpp +++ b/source/builders/meshbuilder.cpp @@ -23,14 +23,13 @@ void MeshBuilder::auto_offset() void MeshBuilder::begin_() { - batch = new Batch(type); + batch = make_unique(type); } void MeshBuilder::end_() { mesh.add_batch(move(*batch)); - delete batch; - batch = nullptr; + batch.reset(); } void MeshBuilder::element_(unsigned i) diff --git a/source/builders/meshbuilder.h b/source/builders/meshbuilder.h index 082db774..2b8d7c5d 100644 --- a/source/builders/meshbuilder.h +++ b/source/builders/meshbuilder.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_MESHBUILDER_H_ #define MSP_GL_MESHBUILDER_H_ +#include #include "mspgl_api.h" #include "primitivebuilder.h" @@ -14,7 +15,7 @@ class MSPGL_API MeshBuilder: public PrimitiveBuilder { private: Mesh &mesh; - Batch *batch = nullptr; + std::unique_ptr batch; public: MeshBuilder(Mesh &); diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index 4947e252..c5523d13 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -137,38 +137,38 @@ void SequenceBuilder::build(Sequence &sequence) const } } -Sequence *SequenceBuilder::build() const +std::unique_ptr SequenceBuilder::build() const { - RefPtr sequence = new Sequence(); + unique_ptr sequence = make_unique(); build(*sequence); - return sequence.release(); + return sequence; } -Sequence *SequenceBuilder::build(unsigned w, unsigned h) const +std::unique_ptr SequenceBuilder::build(unsigned w, unsigned h) const { - RefPtr sequence = create_sequence(w, h); + unique_ptr sequence = create_sequence(w, h); build(*sequence); - return sequence.release(); + return sequence; } -Sequence *SequenceBuilder::build(const View &view) const +std::unique_ptr SequenceBuilder::build(const View &view) const { - RefPtr sequence = create_sequence(view.get_width(), view.get_height()); + unique_ptr sequence = create_sequence(view.get_width(), view.get_height()); build(*sequence); - return sequence.release(); + return sequence; } -Sequence *SequenceBuilder::build(const Framebuffer &fbo) const +std::unique_ptr SequenceBuilder::build(const Framebuffer &fbo) const { - RefPtr sequence = create_sequence(fbo.get_width(), fbo.get_height()); + unique_ptr sequence = create_sequence(fbo.get_width(), fbo.get_height()); build(*sequence); - return sequence.release(); + return sequence; } -Sequence * SequenceBuilder::create_sequence(unsigned w, unsigned h) const +std::unique_ptr SequenceBuilder::create_sequence(unsigned w, unsigned h) const { if(!w || !h || tmpl.get_postprocessors().empty()) - return new Sequence; + return make_unique(); unsigned samples = min(tmpl.get_maximum_multisample(), Device::get_current().get_info().limits.max_samples); if(samples(w, h, format); } } // namespace GL diff --git a/source/builders/sequencebuilder.h b/source/builders/sequencebuilder.h index a34f3603..cd5b1aff 100644 --- a/source/builders/sequencebuilder.h +++ b/source/builders/sequencebuilder.h @@ -2,6 +2,7 @@ #define SEQUENCEBUILDER_H_ #include +#include #include #include "mspgl_api.h" @@ -33,11 +34,11 @@ public: void set_debug_name(const std::string &); void build(Sequence &) const; - Sequence *build() const; - Sequence *build(unsigned, unsigned) const; - Sequence *build(const View &) const; - Sequence *build(const Framebuffer &) const; - Sequence *create_sequence(unsigned, unsigned) const; + std::unique_ptr build() const; + std::unique_ptr build(unsigned, unsigned) const; + std::unique_ptr build(const View &) const; + std::unique_ptr build(const Framebuffer &) const; + std::unique_ptr create_sequence(unsigned, unsigned) const; }; } // namespace GL diff --git a/source/builders/sequencetemplate.cpp b/source/builders/sequencetemplate.cpp index 67948b8a..80c8039c 100644 --- a/source/builders/sequencetemplate.cpp +++ b/source/builders/sequencetemplate.cpp @@ -19,14 +19,6 @@ using namespace std; namespace Msp { namespace GL { -SequenceTemplate::~SequenceTemplate() -{ - for(const Renderable &r: renderables) - delete r.effect_template; - for(const PostProcessor &p: postprocessors) - delete p.postprocessor_template; -} - template<> SequenceTemplate::TemplateRegistry &SequenceTemplate::get_registry() { @@ -58,11 +50,6 @@ SequenceTemplate::TemplateRegistry &SequenceTemplate::get_registry(name); rend.slot_name = slot; - obj.renderables.push_back(rend); + obj.renderables.emplace_back(move(rend)); } void SequenceTemplate::Loader::sequence(const string &slot, const string &name) @@ -152,7 +139,7 @@ void SequenceTemplate::Loader::sequence(const string &slot, const string &name) rend.slot_name = slot; SequenceLoader ldr(rend); load_sub_with(ldr); - obj.renderables.push_back(rend); + obj.renderables.emplace_back(move(rend)); } void SequenceTemplate::Loader::step(const string &tag, const string &rend) @@ -252,10 +239,11 @@ void SequenceTemplate::Step::Loader::depth_compare(Predicate c) void SequenceTemplate::Step::Loader::lighting_inline() { - RefPtr lightn = new Lighting; + unique_ptr lightn = make_unique(); load_sub(*lightn, get_collection()); - get_collection().add(inline_base_name+".lightn", lightn.get()); - obj.lighting = lightn.release(); + Lighting *ptr = lightn.get(); + get_collection().add(inline_base_name+".lightn", move(lightn)); + obj.lighting = ptr; } void SequenceTemplate::Step::Loader::lighting(const string &name) diff --git a/source/builders/sequencetemplate.h b/source/builders/sequencetemplate.h index b5601a68..4c2114a4 100644 --- a/source/builders/sequencetemplate.h +++ b/source/builders/sequencetemplate.h @@ -1,6 +1,7 @@ #ifndef SEQUENCETEMPLATE_H_ #define SEQUENCETEMPLATE_H_ +#include #include #include #include @@ -63,7 +64,7 @@ public: struct Renderable { - Effect::Template *effect_template = nullptr; + std::unique_ptr effect_template = nullptr; GL::Renderable *renderable = nullptr; SequenceTemplate *sequence_template = nullptr; std::map sequence_renderables; @@ -104,10 +105,8 @@ public: struct PostProcessor { - GL::PostProcessor::Template *postprocessor_template; + std::unique_ptr postprocessor_template; std::string slot_name; - - PostProcessor(GL::PostProcessor::Template * = nullptr); }; private: @@ -151,8 +150,6 @@ private: int clear_stencil = 0; public: - ~SequenceTemplate(); - bool get_hdr() const { return hdr; } unsigned get_required_multisample() const { return required_multisample; } unsigned get_maximum_multisample() const { return max_multisample; } diff --git a/source/core/bufferable.cpp b/source/core/bufferable.cpp index 0cfd04c6..9f1c94a0 100644 --- a/source/core/bufferable.cpp +++ b/source/core/bufferable.cpp @@ -160,11 +160,11 @@ void Bufferable::upload_data(unsigned frame, char *target) const dirty = 0; } -Bufferable::AsyncUpdater *Bufferable::create_async_updater() const +unique_ptr Bufferable::create_async_updater() const { if(!buffer || buffer->get_multiplicity()>1) throw invalid_operation("Bufferable::create_async_updater"); - return new AsyncUpdater(*this); + return make_unique(*this); } diff --git a/source/core/bufferable.h b/source/core/bufferable.h index 3504972d..c6e582df 100644 --- a/source/core/bufferable.h +++ b/source/core/bufferable.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "buffer.h" #include "mspgl_api.h" @@ -68,7 +69,7 @@ public: /** Returns an object which can be used to upload data to the buffer using mapped memory. If data is not dirty, returns null. */ - AsyncUpdater *refresh_async() const { return dirty ? create_async_updater() : nullptr; } + std::unique_ptr refresh_async() const { return dirty ? create_async_updater() : nullptr; } private: void unlink_from_buffer(); @@ -106,7 +107,7 @@ private: parameter, or null to use the buffer upload interface. */ void upload_data(unsigned, char *) const; - AsyncUpdater *create_async_updater() const; + std::unique_ptr create_async_updater() const; }; } // namespace GL diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index d69178dd..2f1fcd88 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -14,28 +14,10 @@ Mesh::Mesh(const VertexFormat &f) storage(f); } -Mesh::Mesh(Mesh &&other): - Resource(move(other)), - vertices(move(other.vertices)), - batches(move(other.batches)), - vbuf(other.vbuf), - ibuf(other.ibuf), - vtx_setup(move(other.vtx_setup)), - dirty(other.dirty), - disallow_rendering(other.disallow_rendering), - face_winding(other.face_winding), - debug_name(move(other.debug_name)) -{ - other.vbuf = nullptr; - other.ibuf = nullptr; -} - Mesh::~Mesh() { set_manager(nullptr); batches.clear(); - delete vbuf; - delete ibuf; } void Mesh::storage(const VertexFormat &fmt) @@ -62,9 +44,8 @@ void Mesh::check_buffers(unsigned mask) unsigned req_size = vertices.get_required_buffer_size(); if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()(); + vertices.use_buffer(vbuf.get()); if(!vertices.get_format().empty()) vtx_setup.set_vertex_array(vertices); dirty |= VERTEX_BUFFER; @@ -81,10 +62,9 @@ void Mesh::check_buffers(unsigned mask) unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size()); if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()(); if(!batches.empty()) - batches.front().change_buffer(ibuf); + batches.front().change_buffer(ibuf.get()); dirty |= INDEX_BUFFER; #ifdef DEBUG @@ -116,7 +96,7 @@ void Mesh::add_batch(Batch &&b) { batches.emplace_back(move(b)); if(ibuf) - batches.back().use_buffer(ibuf); + batches.back().use_buffer(ibuf.get()); } else if(batches.back().can_append(b.get_type())) batches.back().append(b); @@ -136,12 +116,12 @@ void Mesh::add_batch(Batch &&b) prev = nullptr; for(Batch &a: batches) { - a.use_buffer(ibuf, prev); + a.use_buffer(ibuf.get(), prev); prev = &a; } } else - batches.back().use_buffer(ibuf, prev); + batches.back().use_buffer(ibuf.get(), prev); } DataType existing_type = batches.front().get_index_type(); @@ -217,9 +197,9 @@ void Mesh::resize_buffers() const dirty = 0; } -Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *) +unique_ptr Mesh::load(IO::Seekable &io, const Resources *) { - return new AsyncLoader(*this, io); + return make_unique(*this, io); } uint64_t Mesh::get_data_size() const @@ -238,10 +218,8 @@ void Mesh::unload() vertices.use_buffer(nullptr); batches.clear(); vtx_setup.unload(); - delete vbuf; - delete ibuf; - vbuf = nullptr; - ibuf = nullptr; + vbuf.reset(); + ibuf.reset(); } void Mesh::set_debug_name(const string &name) @@ -312,8 +290,6 @@ Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i): Mesh::AsyncLoader::~AsyncLoader() { mesh.disallow_rendering = false; - delete vertex_updater; - delete index_updater; } bool Mesh::AsyncLoader::needs_sync() const @@ -347,10 +323,8 @@ bool Mesh::AsyncLoader::process() } else if(phase==3) { - delete vertex_updater; - vertex_updater = nullptr; - delete index_updater; - index_updater = nullptr; + vertex_updater.reset(); + index_updater.reset(); } ++phase; diff --git a/source/core/mesh.h b/source/core/mesh.h index 6d517f85..7d7ce3b9 100644 --- a/source/core/mesh.h +++ b/source/core/mesh.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_MESH_H_ #define MSP_GL_MESH_H_ +#include #include #include #include @@ -53,8 +54,8 @@ private: private: Mesh &mesh; IO::Seekable &io; - Bufferable::AsyncUpdater *vertex_updater = nullptr; - Bufferable::AsyncUpdater *index_updater = nullptr; + std::unique_ptr vertex_updater; + std::unique_ptr index_updater; unsigned phase = 0; public: @@ -73,8 +74,8 @@ private: VertexArray vertices; std::vector batches; - Buffer *vbuf = nullptr; - Buffer *ibuf = nullptr; + std::unique_ptr vbuf; + std::unique_ptr ibuf; VertexSetup vtx_setup; mutable unsigned short dirty = 0; bool disallow_rendering = false; @@ -84,7 +85,6 @@ private: public: Mesh() = default; Mesh(const VertexFormat &); - Mesh(Mesh &&); ~Mesh() override; /** Sets the vertex format for the mesh. It cannot be changed once set. */ @@ -98,7 +98,7 @@ private: public: const VertexArray &get_vertices() const { return vertices; } const VertexSetup &get_vertex_setup() const { return vtx_setup; } - const Buffer *get_index_buffer() const { return ibuf; } + const Buffer *get_index_buffer() const { return ibuf.get(); } std::size_t get_n_vertices() const; /** Returns a pointer to a vertex. Offsets of individual attributes can be @@ -125,7 +125,7 @@ private: public: int get_load_priority() const override { return 1; } - Resource::AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override; + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override; std::uint64_t get_data_size() const override; void unload() override; diff --git a/source/core/module.cpp b/source/core/module.cpp index e987c165..249ee0ff 100644 --- a/source/core/module.cpp +++ b/source/core/module.cpp @@ -259,7 +259,7 @@ void SpirVModule::reflect() } } -SpirVModule *SpirVModule::specialize(const map &spec_values) const +unique_ptr SpirVModule::specialize(const map &spec_values) const { vector flags(code[3], 1); @@ -426,7 +426,7 @@ SpirVModule *SpirVModule::specialize(const map &spec_values) const op += word_count; } - SpirVModule *spec_mod = new SpirVModule; + unique_ptr spec_mod = make_unique(); spec_mod->code = move(new_code); spec_mod->reflect(); spec_mod->create(); diff --git a/source/core/module.h b/source/core/module.h index 9c8d5e0b..26e2be33 100644 --- a/source/core/module.h +++ b/source/core/module.h @@ -2,6 +2,7 @@ #define MSP_GL_MODULE_H_ #include +#include #include #include #include @@ -281,7 +282,7 @@ public: bool is_specializable() const { return specializable; } /** Creates a new module which is a specialized version of this one. */ - SpirVModule *specialize(const std::map &) const; + std::unique_ptr specialize(const std::map &) const; private: std::vector collect_visited_blocks(const std::map &) const; diff --git a/source/core/program.cpp b/source/core/program.cpp index 3c0022ad..9f014aae 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -12,19 +12,6 @@ Program::Program(const Module &mod, const map &spec_values) add_stages(mod, spec_values); } -Program::Program(Program &&other): - ProgramBackend(move(other)), - reflect_data(move(other.reflect_data)), - specialized_spirv(other.specialized_spirv) -{ - other.specialized_spirv = nullptr; -} - -Program::~Program() -{ - delete specialized_spirv; -} - void Program::add_stages(const Module &mod, const map &spec_values) { if(has_stages()) @@ -42,7 +29,7 @@ void Program::add_stages(const Module &mod, const map &spec_values) if(static_cast(mod).is_specializable()) { specialized_spirv = static_cast(mod).specialize(spec_values); - final_module = specialized_spirv; + final_module = specialized_spirv.get(); } add_spirv_stages(*static_cast(final_module), spec_values); break; diff --git a/source/core/program.h b/source/core/program.h index a664f9b5..5ea146c7 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -2,6 +2,7 @@ #define MSP_GL_PROGRAM_H_ #include +#include #include #include #include @@ -51,7 +52,7 @@ private: }; ReflectData reflect_data; - SpirVModule *specialized_spirv = nullptr; + std::unique_ptr specialized_spirv; public: /// Constructs an empty Program with no shader stages attached. @@ -60,9 +61,6 @@ public: /// Constructs a Program from a Module, with specialization constants. Program(const Module &, const std::map & = std::map()); - Program(Program &&); - ~Program(); - void add_stages(const Module &, const std::map & = std::map()); private: void collect_uniforms(const SpirVModule &); diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 3c5e58aa..d6e2f2bd 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -121,7 +121,7 @@ void Texture::Loader::finish() void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn) { - RefPtr io = get_collection().open_raw(fn); + unique_ptr io = get_collection().open_raw(fn); if(!io) throw IO::file_not_found(fn); img.load_io(*io); diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index bf57b224..aea88358 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "error.h" @@ -15,14 +16,13 @@ private: IO::Seekable &io; Texture2D::AsyncTransfer transfer; Graphics::Image image; - Graphics::ImageLoader *img_loader = nullptr; - DataFile::RawData *raw_data = nullptr; + std::unique_ptr img_loader; + std::unique_ptr raw_data; unsigned n_bytes = 0; int phase = 0; public: AsyncLoader(Texture2D &, IO::Seekable &); - ~AsyncLoader(); bool needs_sync() const override; bool process() override; @@ -95,9 +95,9 @@ LinAl::Vector Texture2D::get_level_size(unsigned level) const return LinAl::Vector(w, h); } -Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *) +unique_ptr Texture2D::load(IO::Seekable &io, const Resources *) { - return new AsyncLoader(static_cast(*this), io); + return make_unique(*this, io); } @@ -195,17 +195,11 @@ Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i): if(DataFile::RawData::detect_signature(string(magic, 4))) { - raw_data = new DataFile::RawData; + raw_data = make_unique(); raw_data->open_io(io, "async"); } else - img_loader = Graphics::ImageLoader::open_io(io); -} - -Texture2D::AsyncLoader::~AsyncLoader() -{ - delete img_loader; - delete raw_data; + img_loader.reset(Graphics::ImageLoader::open_io(io)); } bool Texture2D::AsyncLoader::needs_sync() const diff --git a/source/core/texture2d.h b/source/core/texture2d.h index 01b0c9cb..722da906 100644 --- a/source/core/texture2d.h +++ b/source/core/texture2d.h @@ -95,7 +95,7 @@ private: LinAl::Vector get_level_size(unsigned) const; public: - Resource::AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override; + std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) override; }; } // namespace GL diff --git a/source/core/texturecube.cpp b/source/core/texturecube.cpp index bfa63873..e9a6f4ed 100644 --- a/source/core/texturecube.cpp +++ b/source/core/texturecube.cpp @@ -156,7 +156,7 @@ void TextureCube::Loader::init() void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn) { Graphics::Image img; - RefPtr io = get_collection().open_raw(fn); + unique_ptr io = get_collection().open_raw(fn); img.load_io(*io); obj.image(face, img); diff --git a/source/effects/ambientocclusion.cpp b/source/effects/ambientocclusion.cpp index a50177fe..d7ed6ba1 100644 --- a/source/effects/ambientocclusion.cpp +++ b/source/effects/ambientocclusion.cpp @@ -39,9 +39,9 @@ const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup() if(rotate_lookup) return *rotate_lookup; - rotate_lookup = new Texture2D; + unique_ptr owned_ptr = make_unique(); + rotate_lookup = owned_ptr.get(); rotate_lookup->storage(RGBA8, 4, 4, 1); - resources.add(name, rotate_lookup); unsigned char data[64]; for(unsigned i=0; i<16; ++i) @@ -56,6 +56,7 @@ const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup() } rotate_lookup->image(0, data); + resources.add(name, move(owned_ptr)); return *rotate_lookup; } @@ -136,7 +137,7 @@ void AmbientOcclusion::set_debug_name(const string &name) AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const { - RefPtr ao = new AmbientOcclusion(width/size_divisor, height/size_divisor); + unique_ptr ao = make_unique(width/size_divisor, height/size_divisor); ao->set_n_samples(n_samples); ao->set_occlusion_radius(occlusion_radius); ao->set_darkness(darkness); diff --git a/source/effects/bloom.cpp b/source/effects/bloom.cpp index 43a22b3e..3a52d6a4 100644 --- a/source/effects/bloom.cpp +++ b/source/effects/bloom.cpp @@ -24,18 +24,12 @@ Bloom::Bloom(unsigned w, unsigned h): blur_shdata[1].uniform("delta", 0.0f, 1.0f/h); for(unsigned i=0; i<2; ++i) - target[i] = new RenderTarget(w, h, (COLOR_ATTACHMENT,RGBA16F)); + target[i] = make_unique(w, h, (COLOR_ATTACHMENT,RGBA16F)); set_radius(2.0f); set_strength(0.2f); } -Bloom::~Bloom() -{ - for(unsigned i=0; i<2; ++i) - delete target[i]; -} - void Bloom::set_radius(float r) { if(r<=0.0f) @@ -101,7 +95,7 @@ void Bloom::set_debug_name(const string &name) Bloom *Bloom::Template::create(unsigned width, unsigned height) const { - RefPtr bloom = new Bloom(width/size_divisor, height/size_divisor); + unique_ptr bloom = make_unique(width/size_divisor, height/size_divisor); bloom->set_radius(radius); bloom->set_strength(strength); return bloom.release(); diff --git a/source/effects/bloom.h b/source/effects/bloom.h index 51299a97..dee6b073 100644 --- a/source/effects/bloom.h +++ b/source/effects/bloom.h @@ -36,7 +36,7 @@ public: }; private: - RenderTarget *target[2]; + std::unique_ptr target[2]; ProgramData common_shdata; const Program &blur_shader; ProgramData blur_shdata[2]; @@ -47,7 +47,6 @@ private: public: Bloom(unsigned, unsigned); - ~Bloom() override; /** Sets the σ value of the gaussian blur. Values much larger than 4.0 are likely to cause artifacts. */ diff --git a/source/effects/colorcurve.cpp b/source/effects/colorcurve.cpp index b1371794..a71eb44c 100644 --- a/source/effects/colorcurve.cpp +++ b/source/effects/colorcurve.cpp @@ -92,7 +92,7 @@ void ColorCurve::set_debug_name(const string &name) ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const { - RefPtr colorcurve = new ColorCurve(); + unique_ptr colorcurve = make_unique(); colorcurve->set_exposure_adjust(exposure_adjust); colorcurve->set_brightness_response(brightness_response); if(srgb) diff --git a/source/effects/environmentmap.cpp b/source/effects/environmentmap.cpp index c9bfb0d0..d3ec23d5 100644 --- a/source/effects/environmentmap.cpp +++ b/source/effects/environmentmap.cpp @@ -225,7 +225,7 @@ EnvironmentMap *EnvironmentMap::Template::create(const map if(!content || !environment) throw invalid_operation("EnvironmentMap::Template::create"); - RefPtr env_map = new EnvironmentMap(size, format, roughness_levels, *content, *environment); + unique_ptr env_map = make_unique(size, format, roughness_levels, *content, *environment); if(use_fixed_position) env_map->set_fixed_position(fixed_position); env_map->set_depth_clip(near_clip, far_clip); diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index d9ee677e..e8289f03 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -281,7 +281,7 @@ ShadowMap *ShadowMap::Template::create(const map &renderab if(!content || !lighting) throw invalid_operation("ShadowMap::Template::create"); - RefPtr shadow_map = new ShadowMap(width, height, *content, *lighting); + unique_ptr shadow_map = make_unique(width, height, *content, *lighting); shadow_map->set_target(target, radius); shadow_map->set_depth_bias(depth_bias); shadow_map->set_darkness(darkness); diff --git a/source/effects/sky.cpp b/source/effects/sky.cpp index 1d1699db..d9bbe6fc 100644 --- a/source/effects/sky.cpp +++ b/source/effects/sky.cpp @@ -188,7 +188,7 @@ Sky *Sky::Template::create(const map &renderables) const if(!content || !sun) throw invalid_operation("Sky::Template::create"); - RefPtr sky = new Sky(*content, *sun); + unique_ptr sky = make_unique(*content, *sun); create_base(*sky); return sky.release(); diff --git a/source/glsl/builtin.cpp b/source/glsl/builtin.cpp index d8540aae..35042460 100644 --- a/source/glsl/builtin.cpp +++ b/source/glsl/builtin.cpp @@ -30,7 +30,7 @@ Module *get_builtins_module() { initialized = true; - RefPtr io = Resources::get_builtins().open("_builtin.glsl"); + unique_ptr io = Resources::get_builtins().open("_builtin.glsl"); if(!io) return nullptr; diff --git a/source/glsl/modulecache.cpp b/source/glsl/modulecache.cpp index ef864dc0..e8b88d2d 100644 --- a/source/glsl/modulecache.cpp +++ b/source/glsl/modulecache.cpp @@ -16,41 +16,33 @@ ModuleCache::ModuleCache(DataFile::Collection *r): ModuleCache::ModuleCache(const ModuleCache &other) { for(const auto &kvp: other.modules) - modules[kvp.first] = new Module(*kvp.second); + modules[kvp.first] = make_unique(*kvp.second); } ModuleCache &ModuleCache::operator=(const ModuleCache &other) { - for(auto &kvp: modules) - delete kvp.second; modules.clear(); for(const auto &kvp: other.modules) - modules[kvp.first] = new Module(*kvp.second); + modules[kvp.first] = make_unique(*kvp.second); return *this; } -ModuleCache::~ModuleCache() -{ - for(auto &kvp: modules) - delete kvp.second; -} - const Module &ModuleCache::add_module(const string &source, const string &src_name) { - RefPtr module = new Module; + unique_ptr module = make_unique(); Parser parser(this); unsigned src_index = next_source++; parser.parse(*module, source, src_name, src_index); - return *(modules[src_name] = module.release()); + return *(modules[src_name] = move(module)); } const Module &ModuleCache::add_module(IO::Base &io, const string &src_name) { - RefPtr module = new Module; + unique_ptr module = make_unique(); Parser parser(this); unsigned src_index = next_source++; parser.parse(*module, io, src_name, src_index); - return *(modules[src_name] = module.release()); + return *(modules[src_name] = move(module)); } const Module &ModuleCache::get_module(const string &name) @@ -60,7 +52,7 @@ const Module &ModuleCache::get_module(const string &name) if(i!=modules.end()) return *i->second; - RefPtr io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn)); + unique_ptr io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn)); if(!io) throw runtime_error(format("module %s not found", name)); diff --git a/source/glsl/modulecache.h b/source/glsl/modulecache.h index 91f6a5e5..a01a5ce4 100644 --- a/source/glsl/modulecache.h +++ b/source/glsl/modulecache.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_SL_MODULECACHE_H_ #define MSP_GL_SL_MODULECACHE_H_ +#include #include #include "syntax.h" @@ -12,14 +13,13 @@ class ModuleCache { private: DataFile::Collection *resources; - std::map modules; + std::map> modules; unsigned next_source = 1; public: ModuleCache(DataFile::Collection *); ModuleCache(const ModuleCache &); ModuleCache &operator=(const ModuleCache &); - ~ModuleCache(); const Module &add_module(const std::string &, const std::string &); const Module &add_module(IO::Base &, const std::string &); diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 8e565c53..dab0afe3 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -45,17 +45,9 @@ const Program *Material::create_compatible_shader(const map &extra_ return shprog; const Module &module = res.get(module_name); - shprog = new Program(module, spec_values); - try - { - res.add(name, shprog); - } - catch(...) - { - delete shprog; - throw; - } - + unique_ptr owned_ptr = make_unique(module, spec_values); + shprog = owned_ptr.get(); + res.add(name, move(owned_ptr)); return shprog; } diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp index 43da3491..3c852264 100644 --- a/source/materials/pbrmaterial.cpp +++ b/source/materials/pbrmaterial.cpp @@ -44,9 +44,9 @@ const Texture2D &PbrMaterial::get_or_create_fresnel_lookup() if(fresnel_lookup) return *fresnel_lookup; - fresnel_lookup = new Texture2D; + unique_ptr owned_ptr = make_unique(); + fresnel_lookup = owned_ptr.get(); fresnel_lookup->storage(RG8, 128, 128, 1); - resources.add(name, fresnel_lookup); const Program &shprog = resources.get("_pbr_fresnel_lookup.glsl.shader"); ProgramData shdata; @@ -64,6 +64,7 @@ const Texture2D &PbrMaterial::get_or_create_fresnel_lookup() mesh.draw(renderer); renderer.end(); + resources.add(name, move(owned_ptr)); return *fresnel_lookup; } diff --git a/source/materials/programdata.cpp b/source/materials/programdata.cpp index e8cec667..4422fcaf 100644 --- a/source/materials/programdata.cpp +++ b/source/materials/programdata.cpp @@ -28,12 +28,11 @@ ProgramData::ProgramData(ProgramData &&other): blocks(move(other.blocks)), programs(move(other.programs)), last_buffer_block(other.last_buffer_block), - buffer(other.buffer), + buffer(move(other.buffer)), dirty(other.dirty), debug_name(move(other.debug_name)) { other.blocks.clear(); - other.buffer = nullptr; } ProgramData::~ProgramData() @@ -42,9 +41,7 @@ ProgramData::~ProgramData() { if(b.indices.type_flag==0xFE) delete[] b.indices.dynamic.values; - delete b.block; } - delete buffer; } void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value) @@ -519,12 +516,12 @@ vector::iterator ProgramData::get_program(const Progr void ProgramData::recreate_buffer() const { - Buffer *old_buffer = buffer; - // Create the new buffer first to ensure it has a different address - buffer = new Buffer; - delete old_buffer; + /* Ensure that the lifetimes of the buffers overlap so they have different + addresses */ + unique_ptr old_buffer = move(buffer); + buffer = make_unique(); if(last_buffer_block) - last_buffer_block->change_buffer(buffer); + last_buffer_block->change_buffer(buffer.get()); #ifdef DEBUG if(!debug_name.empty()) @@ -577,14 +574,14 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Reflect if(block.used && !block.block) { - block.block = new UniformBlock(info); + block.block = make_unique(info); if(info.bind_point>=0) { if(!buffer) recreate_buffer(); - block.block->use_buffer(buffer, last_buffer_block); - last_buffer_block = block.block; + block.block->use_buffer(buffer.get(), last_buffer_block); + last_buffer_block = block.block.get(); } } } @@ -644,7 +641,7 @@ vector::const_iterator ProgramData::prepare_program(c if(shared.dirty==ALL_ONES) update_block_uniform_indices(shared, b); prog_begin->masks.used |= shared.used; - j->block = (shared.used ? shared.block : nullptr); + j->block = (shared.used ? shared.block.get() : nullptr); ++j; } } diff --git a/source/materials/programdata.h b/source/materials/programdata.h index d0229b34..6e0ce6db 100644 --- a/source/materials/programdata.h +++ b/source/materials/programdata.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_PROGRAMDATA_H_ #define MSP_GL_PROGRAMDATA_H_ +#include #include #include #include @@ -114,7 +115,7 @@ private: ReflectData::LayoutHash block_hash = 0; Mask used = 0; Mask dirty = 0; - UniformBlock *block = nullptr; + std::unique_ptr block; union { std::uint8_t type_flag; @@ -157,7 +158,7 @@ private: mutable std::vector blocks; mutable std::vector programs; mutable UniformBlock *last_buffer_block = nullptr; - mutable Buffer *buffer = nullptr; + mutable std::unique_ptr buffer; bool streaming = false; mutable Mask dirty = 0; std::string debug_name; diff --git a/source/materials/rendermethod.cpp b/source/materials/rendermethod.cpp index 9a7b637b..789fa0d5 100644 --- a/source/materials/rendermethod.cpp +++ b/source/materials/rendermethod.cpp @@ -42,8 +42,8 @@ void RenderMethod::maybe_create_material_shader() if(shdata) { - RefPtr old_shdata = shdata; - shdata = new ProgramData(shprog); + shared_ptr old_shdata = move(shdata); + shdata = make_shared(shprog); shdata->copy_uniforms(*old_shdata); } @@ -56,7 +56,7 @@ void RenderMethod::set_shader_program(const Program *prog, const ProgramData *da shprog_from_material = false; if(data) { - shdata = new ProgramData; + shdata = make_shared(); shdata->copy_uniforms(*data); } else @@ -146,7 +146,7 @@ void RenderMethod::apply(Renderer &renderer) const void RenderMethod::set_debug_name(const string &name) { #ifdef DEBUG - if(shdata.refcount()==1) + if(shdata.use_count()==1) shdata->set_debug_name(name+": UBO"); #else (void)name; @@ -221,8 +221,8 @@ void RenderMethod::Loader::shader(const string &n) obj.shprog_from_material = false; if(obj.shdata) { - RefPtr old_shdata = obj.shdata; - obj.shdata = new ProgramData(obj.shprog); + shared_ptr old_shdata = obj.shdata; + obj.shdata = make_shared(obj.shprog); obj.shdata->copy_uniforms(*old_shdata); } } @@ -244,11 +244,11 @@ void RenderMethod::Loader::uniforms() if(!obj.shprog || obj.shprog_from_material) throw runtime_error("Shader is required for uniforms"); if(!obj.shdata) - obj.shdata = new ProgramData(obj.shprog); - else if(obj.shdata.refcount()>1) + obj.shdata = make_shared(obj.shprog); + else if(obj.shdata.use_count()>1) { - RefPtr old_shdata = obj.shdata; - obj.shdata = new ProgramData; + shared_ptr old_shdata = obj.shdata; + obj.shdata = make_shared(); obj.shdata->copy_uniforms(*old_shdata); } load_sub(*obj.shdata); diff --git a/source/materials/rendermethod.h b/source/materials/rendermethod.h index 14d7541a..f41a0b42 100644 --- a/source/materials/rendermethod.h +++ b/source/materials/rendermethod.h @@ -1,7 +1,7 @@ #ifndef MSP_GL_RENDERPASS_H_ #define MSP_GL_RENDERPASS_H_ -#include +#include #include #include "blend.h" #include "cullface.h" @@ -81,7 +81,7 @@ private: const Program *shprog = nullptr; bool shprog_from_material = false; - RefPtr shdata; + std::shared_ptr shdata; std::map uniform_slots; const Material *material = nullptr; std::string material_slot; diff --git a/source/materials/splatmaterial.cpp b/source/materials/splatmaterial.cpp index 70af9bbb..a86d3092 100644 --- a/source/materials/splatmaterial.cpp +++ b/source/materials/splatmaterial.cpp @@ -30,15 +30,7 @@ SplatMaterial::SplatMaterial(): { } -SplatMaterial::~SplatMaterial() -{ - delete base_color_array.texture; - delete normal_array.texture; - delete metalness_array.texture; - delete roughness_array.texture; - delete occlusion_array.texture; - delete emission_array.texture; -} +SplatMaterial::~SplatMaterial() = default; void SplatMaterial::fill_program_info(string &module_name, map &spec_values) const { @@ -58,17 +50,17 @@ void SplatMaterial::fill_program_info(string &module_name, map &spe const Texture *SplatMaterial::get_texture(Tag tag) const { if(tag==texture_tags[0]) - return base_color_array.texture; + return base_color_array.texture.get(); else if(tag==texture_tags[1]) - return normal_array.texture; + return normal_array.texture.get(); else if(tag==texture_tags[2]) - return metalness_array.texture; + return metalness_array.texture.get(); else if(tag==texture_tags[3]) - return roughness_array.texture; + return roughness_array.texture.get(); else if(tag==texture_tags[4]) - return occlusion_array.texture; + return occlusion_array.texture.get(); else if(tag==texture_tags[5]) - return emission_array.texture; + return emission_array.texture.get(); else if(tag==texture_tags[6]) return &fresnel_lookup; else @@ -123,7 +115,7 @@ void SplatMaterial::upload_sub_map(DataFile::Collection &coll, unsigned index, S for(; lowest_free_bit>1; lowest_free_bit>>=1) ++sub.layer; - RefPtr io = coll.open_raw(sub.source_fn); + unique_ptr io = coll.open_raw(sub.source_fn); char magic[4] = { }; io->read(magic, 4); io->seek(0, IO::S_BEG); @@ -177,7 +169,7 @@ void SplatMaterial::MapArray::create() { if(!texture && format!=NO_PIXELFORMAT && max_layers>0) { - texture = new Texture2DArray; + texture = make_unique(); texture->storage(format, width, height, max_layers); } } diff --git a/source/materials/splatmaterial.h b/source/materials/splatmaterial.h index 70662837..9928440b 100644 --- a/source/materials/splatmaterial.h +++ b/source/materials/splatmaterial.h @@ -73,7 +73,7 @@ private: struct MapArray { - Texture2DArray *texture = nullptr; + std::unique_ptr texture; PixelFormat format = NO_PIXELFORMAT; unsigned width = 0; unsigned height = 0; diff --git a/source/materials/technique.cpp b/source/materials/technique.cpp index 55914f35..4418b1df 100644 --- a/source/materials/technique.cpp +++ b/source/materials/technique.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -71,7 +71,7 @@ bool Technique::replace_uniforms(const ProgramData &shdata) const vector &uniform_tags = shdata.get_uniform_tags(); for(auto &kvp: methods) { - RefPtr new_shdata; + unique_ptr new_shdata; for(Tag t: uniform_tags) { Tag tag = kvp.second.get_slotted_uniform_tag(t); @@ -80,7 +80,7 @@ bool Technique::replace_uniforms(const ProgramData &shdata) if(!new_shdata) { - new_shdata = new ProgramData; + new_shdata = make_unique(); new_shdata->copy_uniforms(*kvp.second.get_shader_data()); } diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index 20dcb888..fee3b862 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -37,8 +37,8 @@ InstanceArrayBase::InstanceArrayBase(const Object &o, size_t s): const VertexFormat &fmt = instance_data.get_format(); matrix_offset = fmt.offset((RAW_ATTRIB4,matrix_location)); - instance_buffer = new Buffer; - instance_data.use_buffer(instance_buffer); + instance_buffer = make_unique(); + instance_data.use_buffer(instance_buffer.get()); const Mesh *mesh = object.get_mesh(); @@ -50,7 +50,6 @@ InstanceArrayBase::InstanceArrayBase(const Object &o, size_t s): InstanceArrayBase::~InstanceArrayBase() { - delete instance_buffer; for(Block &b: storage) delete[] b.begin; } diff --git a/source/render/instancearray.h b/source/render/instancearray.h index bc89f570..c622f6e7 100644 --- a/source/render/instancearray.h +++ b/source/render/instancearray.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_INSTANCEARRAY_H_ #define MSP_GL_INSTANCEARRAY_H_ +#include #include #include #include "mspgl_api.h" @@ -55,7 +56,7 @@ private: const Object &object; VertexArray instance_data; - Buffer *instance_buffer = nullptr; + std::unique_ptr instance_buffer; VertexSetup vtx_setup; int matrix_location = -1; unsigned matrix_offset = 0; diff --git a/source/render/object.cpp b/source/render/object.cpp index 0ae739a3..9a75ff55 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -249,10 +249,11 @@ void Object::LodLoader::mesh(const string &n) void Object::LodLoader::mesh_inline() { - RefPtr msh = new Mesh; + unique_ptr msh = make_unique(); load_sub(*msh); - get_collection().add(format("%s/lod%d.mesh", FS::basename(get_source()), index), msh.get()); - lod.mesh = msh.release(); + Mesh *ptr = msh.get(); + get_collection().add(format("%s/lod%d.mesh", FS::basename(get_source()), index), move(msh)); + lod.mesh = ptr; } void Object::LodLoader::technique(const string &n) @@ -262,13 +263,14 @@ void Object::LodLoader::technique(const string &n) void Object::LodLoader::technique_inline() { - RefPtr tech = new Technique; + unique_ptr tech = make_unique(); Technique::Loader ldr(*tech, get_collection()); string name = format("%s/lod%d.tech", FS::basename(get_source()), index); ldr.set_inline_base_name(name); load_sub(*tech, get_collection()); - get_collection().add(name, tech.get()); - lod.technique = tech.release(); + Technique *ptr = tech.get(); + get_collection().add(name, move(tech)); + lod.technique = ptr; } } // namespace GL diff --git a/source/render/rendertarget.cpp b/source/render/rendertarget.cpp index 28fd5669..0eef1061 100644 --- a/source/render/rendertarget.cpp +++ b/source/render/rendertarget.cpp @@ -22,30 +22,26 @@ RenderTarget::RenderTarget(unsigned w, unsigned h, const FrameFormat &f): { PixelFormat pf = get_attachment_pixelformat(a); - Texture2D *tex2d = new Texture2D; + unique_ptr tex2d = make_unique(); tex2d->storage(pf, width, height, 1); if(multisample) { - Texture2DMultisample *tex2d_ms = new Texture2DMultisample; + unique_ptr tex2d_ms = make_unique(); tex2d_ms->storage(pf, width, height, samples); - fbo.attach(a, *tex2d_ms, tex2d); - textures.push_back(tex2d_ms); - textures.push_back(tex2d); + fbo.attach(a, *tex2d_ms, tex2d.get()); + textures.push_back(move(tex2d_ms)); + textures.push_back(move(tex2d)); } else { fbo.attach(a, *tex2d); - textures.push_back(tex2d); + textures.push_back(move(tex2d)); } } } -RenderTarget::~RenderTarget() -{ - for(Texture *t: textures) - delete t; -} +RenderTarget::~RenderTarget() = default; const Texture2D &RenderTarget::get_target_texture(unsigned i) const { @@ -54,7 +50,7 @@ const Texture2D &RenderTarget::get_target_texture(unsigned i) const if(fbo.get_format().get_samples()>1) i = i*2+1; - return *static_cast(textures[i]); + return *static_cast(textures[i].get()); } const Texture2D &RenderTarget::get_target_texture(FrameAttachment fa) const diff --git a/source/render/rendertarget.h b/source/render/rendertarget.h index f3406f0b..bcb9a2a4 100644 --- a/source/render/rendertarget.h +++ b/source/render/rendertarget.h @@ -1,6 +1,7 @@ #ifndef RENDERTARGET_H_ #define RENDERTARGET_H_ +#include #include #include "framebuffer.h" #include "mspgl_api.h" @@ -23,7 +24,7 @@ class MSPGL_API RenderTarget: public NonCopyable private: unsigned width; unsigned height; - std::vector textures; + std::vector> textures; Framebuffer fbo; public: diff --git a/source/render/scene.cpp b/source/render/scene.cpp index a05e7110..fd3ce9ba 100644 --- a/source/render/scene.cpp +++ b/source/render/scene.cpp @@ -47,10 +47,11 @@ Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m): void Scene::Loader::array(const string &n) { - RefPtr > arr = new InstanceArray<>(get_collection().get(n)); + unique_ptr> arr = make_unique>(get_collection().get(n)); load_sub(*arr); - get_collection().add(format("_scene_array_%d.array", ++inline_counter), arr.get()); - obj.add(*arr.release()); + InstanceArray<> *ptr = arr.get(); + get_collection().add(format("_scene_array_%d.array", ++inline_counter), move(arr)); + obj.add(*ptr); } void Scene::Loader::object(const string &n) @@ -60,12 +61,13 @@ void Scene::Loader::object(const string &n) void Scene::Loader::object_tagged(const string &n, const string &t) { - RefPtr inst = new ObjectInstance(get_collection().get(n)); + unique_ptr inst = make_unique(get_collection().get(n)); load_sub(*inst); - get_collection().add(format("_scene_object_%d.inst", ++inline_counter), inst.get()); + ObjectInstance *ptr = inst.get(); + get_collection().add(format("_scene_object_%d.inst", ++inline_counter), move(inst)); if(content && !t.empty()) - (*content)[t] = inst.get(); - obj.add(*inst.release()); + (*content)[t] = ptr; + obj.add(*ptr); } void Scene::Loader::scene(const string &n) diff --git a/source/render/sequence.cpp b/source/render/sequence.cpp index 582e63e9..c355020e 100644 --- a/source/render/sequence.cpp +++ b/source/render/sequence.cpp @@ -14,6 +14,9 @@ namespace GL { const Tag Sequence::noclear_tag = "noclear"; +// Hide std::unique_ptr from the header +Sequence::Sequence() = default; + Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f): width(w), height(h), @@ -24,20 +27,17 @@ Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f): FrameFormat postproc_fmt = target_format; postproc_fmt.set_samples(1); - target[0] = new RenderTarget(width, height, postproc_fmt); - target[1] = new RenderTarget(width, height, postproc_fmt); + target[0] = make_unique(width, height, postproc_fmt); + target[1] = make_unique(width, height, postproc_fmt); if(target_format.get_samples()>1) - target_ms = new RenderTarget(width, height, target_format); + target_ms = make_unique(width, height, target_format); } Sequence::~Sequence() { for(OwnedObject &o: owned_data) o.delete_func(o.pointer); - delete target[0]; - delete target[1]; - delete target_ms; } void Sequence::set_clear_enabled(bool c) @@ -143,11 +143,11 @@ void Sequence::render(Renderer &renderer, Tag tag) const if(target[0]) { - RenderTarget *source = target[0]; + RenderTarget *source = target[0].get(); if(target_ms) { renderer.resolve_multisample(); - source = target_ms; + source = target_ms.get(); } renderer.set_depth_test(nullptr); @@ -161,7 +161,7 @@ void Sequence::render(Renderer &renderer, Tag tag) const const Texture2D &color = source->get_target_texture(COLOR_ATTACHMENT); const Texture2D &depth = source->get_target_texture(DEPTH_ATTACHMENT); postproc[i]->render(renderer, color, depth); - source = target[j]; + source = target[j].get(); } } } diff --git a/source/render/sequence.h b/source/render/sequence.h index 0c7c869c..8fa398a7 100644 --- a/source/render/sequence.h +++ b/source/render/sequence.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_SEQUENCE_H_ #define MSP_GL_SEQUENCE_H_ +#include #include #include "color.h" #include "depthtest.h" @@ -75,8 +76,8 @@ private: unsigned width = 0; unsigned height = 0; FrameFormat target_format; - RenderTarget *target[2] = { nullptr, nullptr }; - RenderTarget *target_ms = nullptr; + std::unique_ptr target[2]; + std::unique_ptr target_ms; bool clear_enabled = false; std::vector clear_colors; float clear_depth = 1.0f; @@ -86,7 +87,7 @@ private: static const Tag noclear_tag; public: - Sequence() = default; + Sequence(); Sequence(unsigned, unsigned, const FrameFormat &); ~Sequence() override; diff --git a/source/render/view.cpp b/source/render/view.cpp index b55c5b4c..256d02a6 100644 --- a/source/render/view.cpp +++ b/source/render/view.cpp @@ -3,21 +3,13 @@ #include "renderer.h" #include "view.h" +using namespace std; + namespace Msp { namespace GL { -View::~View() -{ - delete internal_renderer; -} - -View::View(View &&other): - camera(other.camera), - content(other.content), - internal_renderer(other.internal_renderer) -{ - other.internal_renderer = nullptr; -} +View::View() = default; +View::~View() = default; void View::set_camera(Camera *c) { @@ -34,7 +26,7 @@ void View::set_content(Renderable *r) void View::render() { if(!internal_renderer) - internal_renderer = new Renderer; + internal_renderer = make_unique(); internal_renderer->begin(); render(*internal_renderer); internal_renderer->end(); diff --git a/source/render/view.h b/source/render/view.h index 1c63f84b..20c3fa30 100644 --- a/source/render/view.h +++ b/source/render/view.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_VIEW_H_ #define MSP_GL_VIEW_H_ +#include #include #include "framebuffer.h" #include "mspgl_api.h" @@ -24,11 +25,10 @@ class MSPGL_API View: public NonCopyable protected: Camera *camera = nullptr; Renderable *content = nullptr; - Renderer *internal_renderer = nullptr; + std::unique_ptr internal_renderer; - View() = default; + View(); public: - View(View &&); virtual ~View(); virtual unsigned get_width() const { return get_target().get_width(); } diff --git a/source/resources/resource.h b/source/resources/resource.h index 465f81dc..37703fe8 100644 --- a/source/resources/resource.h +++ b/source/resources/resource.h @@ -2,6 +2,7 @@ #define MSP_GL_RESOURCE_H_ #include +#include #include #include #include "mspgl_api.h" @@ -39,7 +40,7 @@ public: ResourceManager *get_manager() const { return manager; } void *get_manager_data() const { return manager_data; } virtual int get_load_priority() const { return 0; } - virtual AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) = 0; + virtual std::unique_ptr load(IO::Seekable &, const Resources * = nullptr) = 0; virtual bool is_loaded() const; /** Returns the amount of graphics memory used by this resource. The diff --git a/source/resources/resourcemanager.cpp b/source/resources/resourcemanager.cpp index a2023f5d..b9b093bf 100644 --- a/source/resources/resourcemanager.cpp +++ b/source/resources/resourcemanager.cpp @@ -73,7 +73,7 @@ void ResourceManager::move_resource(Resource &from, Resource &to) throw invalid_operation("ResourceManager::move_resource"); ManagedResource *managed = reinterpret_cast(to.get_manager_data()); MutexLock lock(map_mutex); - insert_unique(resources, &to, *managed); + insert_unique(resources, &to, move(*managed)); resources.erase(&from); } @@ -304,8 +304,7 @@ void ResourceManager::ManagedResource::start_loading() loader = resource->load(*io, res); if(!loader) { - delete io; - io = nullptr; + io.reset(); throw logic_error("no loader created"); } state = LOADING; @@ -322,10 +321,8 @@ bool ResourceManager::ManagedResource::process(bool sync) void ResourceManager::ManagedResource::finish_loading(bool successful) { - delete loader; - loader = nullptr; - delete io; - io = nullptr; + loader.reset(); + io.reset(); if(successful) { diff --git a/source/resources/resourcemanager.h b/source/resources/resourcemanager.h index 8dfd6978..859c9403 100644 --- a/source/resources/resourcemanager.h +++ b/source/resources/resourcemanager.h @@ -60,8 +60,8 @@ private: Resource *resource = nullptr; ResourceLocation location; int load_priority = 0; - IO::Seekable *io = nullptr; - Resource::AsyncLoader *loader = nullptr; + std::unique_ptr io; + std::unique_ptr loader; State state = NOT_LOADED; unsigned last_used = 0; std::uint64_t data_size = 0; diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index ec17ec86..fd6c306b 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -49,12 +49,12 @@ Resources::Resources(bool set_as_global) add_type().suffix(".anim").keyword("animation"); add_type().suffix(".arma").keyword("armature"); add_type().base().suffix(".mat") - .creator([this](const string &n) -> BasicMaterial * { create_generic(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }) .notify(&set_debug_name); add_type().keyword("camera") .notify(&set_debug_name); add_type().base().suffix(".light") - .creator([this](const string &n) -> DirectionalLight * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_type().keyword("font"); add_type().suffix(".kframe").keyword("keyframe"); add_type().suffix(".lightn").keyword("lighting") @@ -67,14 +67,14 @@ Resources::Resources(bool set_as_global) .notify(&set_debug_name); add_type().base().keyword("object"); add_type().base().base().suffix(".scene") - .creator([this](const string &n) -> OccludedScene * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_type().base().base().suffix(".scene") - .creator([this](const string &n) -> OrderedScene * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_type().base().suffix(".mat") - .creator([this](const string &n) -> PbrMaterial * { create_generic(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".light") - .creator([this](const string &n) -> PointLight * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_type().suffix(".seq").keyword("sequence"); add_type().keyword("pose"); add_type().keyword("shader") @@ -83,32 +83,32 @@ Resources::Resources(bool set_as_global) add_type().suffix(".samp").keyword("sampler") .notify(&set_debug_name); add_type().base().base().suffix(".scene") - .creator([this](const string &n) -> SimpleScene * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_type().base().suffix(".mat") - .creator([this](const string &n) -> SplatMaterial * { create_generic(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }) .notify(&set_debug_name); add_type().suffix(".tech").keyword("technique") .notify(&set_debug_name); add_type().base().suffix(".tex") - .creator([this](const string &n) -> Texture1D * { create_texture(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_texture(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".tex").suffix(".png").suffix(".jpg") - .creator([this](const string &n) -> Texture2D * { create_texture(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_texture(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".tex") - .creator([this](const string &n) -> Texture3D * { create_texture(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_texture(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".tex") - .creator([this](const string &n) -> TextureCube * { create_texture(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_texture(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".tex") - .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_texture(n); return nullptr; }) .notify(&set_debug_name); add_type().base().suffix(".mat") - .creator([this](const string &n) -> UnlitMaterial * { create_generic(n); return nullptr; }) + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }) .notify(&set_debug_name); add_type().base().base().suffix(".scene") - .creator([this](const string &n) -> ZSortedScene * { create_generic(n); return nullptr; }); + .creator([this](const string &n) -> unique_ptr { create_generic(n); return nullptr; }); add_source(get_builtins()); @@ -150,9 +150,9 @@ void Resources::set_resource_manager(ResourceManager *m) } template -T *Resources::create_generic(const string &name) +unique_ptr Resources::create_generic(const string &name) { - if(RefPtr io = open_raw(name)) + if(unique_ptr io = open_raw(name)) { DataFile::Parser parser(*io, name); L ldr(*this); @@ -163,23 +163,23 @@ T *Resources::create_generic(const string &name) return nullptr; } -Mesh *Resources::create_mesh(const string &name) +unique_ptr Resources::create_mesh(const string &name) { if(!resource_manager || name[0]=='_') return nullptr; - if(RefPtr io = open_raw(name)) + if(unique_ptr io = open_raw(name)) { - RefPtr mesh = new Mesh; + unique_ptr mesh = make_unique(); mesh->set_manager(resource_manager); resource_manager->set_resource_location(*mesh, *this, name); - return mesh.release(); + return mesh; } return nullptr; } -Texture *Resources::create_texture(const string &name) +unique_ptr Resources::create_texture(const string &name) { bool managed = (resource_manager && name[0]!='_'); @@ -192,16 +192,16 @@ Texture *Resources::create_texture(const string &name) return create_generic(name); } - if(RefPtr io = open_raw(name)) + if(unique_ptr io = open_raw(name)) { - RefPtr tex; + unique_ptr tex; // Verify that the image is loadable Graphics::Image image; if(!managed) image.load_io(*io); - tex = new Texture2D; + tex = make_unique(); if(managed) { @@ -211,52 +211,51 @@ Texture *Resources::create_texture(const string &name) else tex->image(image); - add(name, tex.get()); - tex.release(); + add(name, move(tex)); } return nullptr; } -Module *Resources::create_module(const string &name) +unique_ptr Resources::create_module(const string &name) { string ext = FS::extpart(name); if(ext!=".glsl" && ext!=".spv") return nullptr; - if(RefPtr io = open_raw(name)) + if(unique_ptr io = open_raw(name)) { if(ext==".glsl") { - RefPtr module; + unique_ptr module; if(get_backend_api()==VULKAN) - module = new SpirVModule; + module = make_unique(); else - module = new GlslModule; + module = make_unique(); module->load_source(*io, this, name); - return module.release(); + return module; } else if(ext==".spv") { - RefPtr module = new SpirVModule; + unique_ptr module = make_unique(); module->load_code(*io); - return module.release(); + return module; } } else if(ext==".spv") { if((io = open_raw(FS::basepart(name)+".glsl"))) { - RefPtr module = new SpirVModule; + unique_ptr module = make_unique(); module->load_source(*io, this, name); - return module.release(); + return module; } } return nullptr; } -Program *Resources::create_program(const string &name) +unique_ptr Resources::create_program(const string &name) { string ext = FS::extpart(name); string base = FS::basepart(name); @@ -264,9 +263,9 @@ Program *Resources::create_program(const string &name) if(ext==".shader" && (ext2==".glsl" || ext2==".spv")) { Module &module = get(base); - RefPtr shprog = new Program; + unique_ptr shprog = make_unique(); shprog->add_stages(module); - return shprog.release(); + return shprog; } return nullptr; diff --git a/source/resources/resources.h b/source/resources/resources.h index ff2bcd5f..962c560f 100644 --- a/source/resources/resources.h +++ b/source/resources/resources.h @@ -98,12 +98,12 @@ public: protected: template - T *create_generic(const std::string &); + std::unique_ptr create_generic(const std::string &); - Mesh *create_mesh(const std::string &); - Texture *create_texture(const std::string &); - Module *create_module(const std::string &); - Program *create_program(const std::string &); + std::unique_ptr create_mesh(const std::string &); + std::unique_ptr create_texture(const std::string &); + std::unique_ptr create_module(const std::string &); + std::unique_ptr create_program(const std::string &); template static void set_debug_name(const std::string &, T &); diff --git a/tools/viewer.cpp b/tools/viewer.cpp index 8ac41a23..410b5969 100644 --- a/tools/viewer.cpp +++ b/tools/viewer.cpp @@ -69,10 +69,10 @@ private: Input::Mouse mouse; Resources resources; GL::WindowView view; - GL::Sequence *sequence = nullptr; + unique_ptr sequence; GL::Renderable *renderable = nullptr; - GL::AnimatedObject *anim_object = nullptr; - GL::AnimationPlayer *anim_player = nullptr; + unique_ptr anim_object; + unique_ptr anim_player; GL::DirectionalLight light; GL::Lighting lighting; GL::Camera camera; @@ -89,9 +89,8 @@ public: private: template T *load(const string &); -public: - ~Viewer(); +public: int main() override; private: void tick() override; @@ -158,22 +157,24 @@ Viewer::Viewer(int argc, char **argv): GL::Mesh *mesh = nullptr; if(FS::exists(opts.renderable_name)) { - mesh = new GL::Mesh; + unique_ptr owned_mesh = make_unique(); + mesh = owned_mesh.get(); DataFile::load(*mesh, opts.renderable_name); - resources.add("__"+opts.renderable_name, mesh); + resources.add("__"+opts.renderable_name, move(owned_mesh)); } else mesh = &resources.get(opts.renderable_name); - object = new GL::Object; - GL::Technique *tech = new GL::Technique; + unique_ptr owned_object = make_unique(); + object = owned_object.get(); + unique_ptr tech = make_unique(); tech->add_method(GL::Tag()); object->set_mesh(mesh); - object->set_technique(tech); + object->set_technique(tech.get()); renderable = object; - resources.add("__.tech", tech); - resources.add("__.object", object); + resources.add("__.tech", move(tech)); + resources.add("__.object", move(owned_object)); } else if(ext==".object") renderable = load(opts.renderable_name); @@ -185,7 +186,9 @@ Viewer::Viewer(int argc, char **argv): IO::BufferedFile in(opts.renderable_name); DataFile::Parser parser(in, opts.renderable_name); ldr.load(parser); - renderable = ldr.get_object(); + unique_ptr scene = ldr.get_object(); + renderable = scene.get(); + resources.add("__.scene", move(scene)); } else renderable = &resources.get(opts.renderable_name); @@ -206,10 +209,10 @@ Viewer::Viewer(int argc, char **argv): throw usage_error("Must have an object to animate"); GL::Animation *anim = load(opts.animation_name); - anim_player = new GL::AnimationPlayer; - anim_object = new GL::AnimatedObject(*object); + anim_player = make_unique(); + anim_object = make_unique(*object); anim_player->play(*anim_object, *anim); - renderable = anim_object; + renderable = anim_object.get(); } window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0)); @@ -226,7 +229,7 @@ Viewer::Viewer(int argc, char **argv): if(!sequence) { - sequence = new GL::Sequence(); + sequence = make_unique(); sequence->set_debug_name("Sequence"); sequence->set_clear_enabled(true); GL::Sequence::Step &step = sequence->add_step(GL::Tag(), *renderable); @@ -234,7 +237,7 @@ Viewer::Viewer(int argc, char **argv): step.set_depth_test(GL::LEQUAL); } - view.set_content(sequence); + view.set_content(sequence.get()); view.set_camera(&camera); } @@ -243,22 +246,16 @@ T *Viewer::load(const string &name) { if(FS::exists(name)) { - T *thing = new T; + unique_ptr owned_thing = make_unique(); + T *thing = owned_thing.get(); DataFile::load(*thing, name, resources); - resources.add("__"+name, thing); + resources.add("__"+name, move(owned_thing)); return thing; } else return &resources.get(name); } -Viewer::~Viewer() -{ - delete anim_player; - delete anim_object; - delete sequence; -} - int Viewer::main() { window.show(); -- 2.45.2