]> git.tdb.fi Git - libs/gl.git/commitdiff
Use unique_ptr to manage memory outside the shader compiler
authorMikko Rasa <tdb@tdb.fi>
Mon, 11 Dec 2023 18:56:11 +0000 (20:56 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 11 Dec 2023 18:56:39 +0000 (20:56 +0200)
The compiler has its own complexities and will be converted in a later
commit.

75 files changed:
demos/desertpillars/source/desertpillars.cpp
demos/forestpond/source/forestpond.cpp
source/animation/animation.cpp
source/animation/animation.h
source/animation/keyframe.cpp
source/animation/keyframe.h
source/backends/opengl/texture1d_backend.h
source/backends/opengl/texture2d_backend.cpp
source/backends/opengl/texture2d_backend.h
source/backends/opengl/texture2dmultisample_backend.h
source/backends/opengl/texture3d_backend.h
source/backends/opengl/texturecube_backend.h
source/backends/vulkan/device_backend.cpp
source/backends/vulkan/device_backend.h
source/backends/vulkan/program_backend.cpp
source/backends/vulkan/texture1d_backend.h
source/backends/vulkan/texture2dmultisample_backend.h
source/backends/vulkan/texture3d_backend.h
source/backends/vulkan/texturecube_backend.h
source/backends/vulkan/windowview_backend.cpp
source/backends/vulkan/windowview_backend.h
source/builders/font.cpp
source/builders/meshbuilder.cpp
source/builders/meshbuilder.h
source/builders/sequencebuilder.cpp
source/builders/sequencebuilder.h
source/builders/sequencetemplate.cpp
source/builders/sequencetemplate.h
source/core/bufferable.cpp
source/core/bufferable.h
source/core/mesh.cpp
source/core/mesh.h
source/core/module.cpp
source/core/module.h
source/core/program.cpp
source/core/program.h
source/core/texture.cpp
source/core/texture2d.cpp
source/core/texture2d.h
source/core/texturecube.cpp
source/effects/ambientocclusion.cpp
source/effects/bloom.cpp
source/effects/bloom.h
source/effects/colorcurve.cpp
source/effects/environmentmap.cpp
source/effects/shadowmap.cpp
source/effects/sky.cpp
source/glsl/builtin.cpp
source/glsl/modulecache.cpp
source/glsl/modulecache.h
source/materials/material.cpp
source/materials/pbrmaterial.cpp
source/materials/programdata.cpp
source/materials/programdata.h
source/materials/rendermethod.cpp
source/materials/rendermethod.h
source/materials/splatmaterial.cpp
source/materials/splatmaterial.h
source/materials/technique.cpp
source/render/instancearray.cpp
source/render/instancearray.h
source/render/object.cpp
source/render/rendertarget.cpp
source/render/rendertarget.h
source/render/scene.cpp
source/render/sequence.cpp
source/render/sequence.h
source/render/view.cpp
source/render/view.h
source/resources/resource.h
source/resources/resourcemanager.cpp
source/resources/resourcemanager.h
source/resources/resources.cpp
source/resources/resources.h
tools/viewer.cpp

index 7c285185649aaeeed4e49c3a3b64dee3b1fc00e2..a6ebeccda35a6a34d7578df02f239bee082144f1 100644 (file)
@@ -43,12 +43,12 @@ DesertPillars::DesertPillars(int, char **):
        GL::SequenceBuilder seq_bld(resources.get<GL::SequenceTemplate>("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<GL::SequenceTemplate>("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<GL::EnvironmentMap>(256, GL::RGBA16F, 7, sphere, *env_seq);
        env_map->set_debug_name("Environment map");
index 613189e07c0c484dbf52d6a300752e5f2bc05929..02bbe96ef4110c2c57df5e32502a7082d9e92267 100644 (file)
@@ -23,12 +23,12 @@ ForestPond::ForestPond(int, char **):
        GL::SequenceBuilder seq_bld(resources.get<GL::SequenceTemplate>("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<GL::SequenceTemplate>("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<GL::EnvironmentMap>(512, GL::RGBA16F, 5, water, *env_seq);
        env_map->set_debug_name("Water environment");
index a733e7c1eaed3b4a1e0d79b67776b7c517f61a0d..0a9085834362f5d67b1632f63a9e95bde4f6cabe 100644 (file)
@@ -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<KeyFrame> 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<KeyFrame> 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<KeyFrame> ckf = make_unique<KeyFrame>();
                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<N>(target, component, knots));
+       curves.emplace_back(make_unique<ValueCurve<N>>(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<KeyFrame>(n), c, false);
+       add_kf(&get_collection().get<KeyFrame>(n), c);
 }
 
 void Animation::Loader::load_kf_inline(bool c)
 {
-       RefPtr<KeyFrame> kf = new KeyFrame;
+       unique_ptr<KeyFrame> kf = make_unique<KeyFrame>();
        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)
index 8be5ff61d46cdb55c45c87fe3fd49fab42baca58..3a7604542ff3f6f3ce34c2f132e6a575240a9829 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef MSP_GL_ANIMATION_H_
 #define MSP_GL_ANIMATION_H_
 
-#include <msp/core/refptr.h>
+#include <memory>
 #include <msp/datafile/objectloader.h>
 #include <msp/interpolate/spline.h>
 #include <msp/time/timedelta.h>
@@ -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<TimedKeyFrame> keyframes;
+       std::vector<std::unique_ptr<KeyFrame>> owned_keyframes;
        std::vector<Event> events;
        bool looping = false;
        std::vector<UniformInfo> uniforms;
-       std::vector<Curve *> curves;
+       std::vector<std::unique_ptr<Curve>> 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<KeyFrame>);
        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<KeyFrame>);
 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);
index d1f1a7ea298251818b60d2b4c401edd0f0b07a91..d6466505868eb8bc691f15969df8f0783d76e74b 100644 (file)
@@ -67,10 +67,11 @@ void KeyFrame::Loader::pose(const string &n)
 
 void KeyFrame::Loader::pose_inline()
 {
-       RefPtr<Pose> p = new Pose;
+       unique_ptr<Pose> p = make_unique<Pose>();
        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)
index 55f8c341ec463c509032f6ecbad6518563617915..2abe716794310d8d7b7eb27b35a3979fedaa73bb 100644 (file)
@@ -1,7 +1,6 @@
 #ifndef MSP_GL_KEYFRAME_H_
 #define MSP_GL_KEYFRAME_H_
 
-#include <msp/core/refptr.h>
 #include <msp/datafile/objectloader.h>
 #include "matrix.h"
 #include "mspgl_api.h"
index 54ceed5839a8a734b7f21630229b8a3aff17d773..67c674147e0961c421d1fec4d02130678c285e3e 100644 (file)
@@ -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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index 88720c3c59f248257a952ae00cee941f9812c959..84dff96866fc14fbeb7e062cfaed8416de4392d5 100644 (file)
@@ -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<const Texture2D::AsyncTransfer *>(this);
 
-       pixel_buffer = new Buffer;
+       pixel_buffer = make_unique<Buffer>();
        pixel_buffer->storage(self.data_size, STREAMING);
        return pixel_buffer->map();
 }
index c7d0fa5ff3c945f0115b4ca57b9eb6fe37d155ff..5423cfe3fe4f316d70bf53e2ac436710ccd4adba 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_TEXTURE2D_BACKEND_H_
 #define MSP_GL_TEXTURE2D_BACKEND_H_
 
+#include <memory>
 #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<Buffer> pixel_buffer;
 
                void *allocate();
                void finalize();
index 17fb84115597971033d391a29fa003418c14d88b..cad6ab6cb817b140d1e0ed478ce9a54d3da81e5b 100644 (file)
@@ -15,7 +15,7 @@ protected:
        void allocate();
 
 public:
-       AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
+       std::unique_ptr<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index fc17fb89fd4408a3229d50b470f8fb7b141bc7c4..45da329c24ac265bc4ae957e62bd293709f3da03 100644 (file)
@@ -19,7 +19,7 @@ protected:
        bool is_array() const;
 
 public:
-       AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
+       std::unique_ptr<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index b75acf8ce8c4d371ea772fb8c2c253152460cda3..1767ab028c8a02ddfc7caea201768ec7bf7cd1ae 100644 (file)
@@ -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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index d80afaab5d4975ee062ee6ea25c09d34cac206a2..50e34c4fafdad8c7d916054b5c2bba5d5e271562 100644 (file)
@@ -14,7 +14,7 @@ VulkanDevice::VulkanDevice(Graphics::Window &wnd, const Graphics::VulkanOptions
        context(wnd, opts),
        device(handle_cast<VkDevice>(context.get_private().device)),
        graphics_queue(handle_cast<VkQueue>(context.get_private().graphics_queue)),
-       functions(new VulkanFunctions(context)),
+       functions(make_unique<VulkanFunctions>(context)),
        allocator(*static_cast<Device *>(this)),
        destroy_queue(*static_cast<Device *>(this)),
        synchronizer(*static_cast<Device *>(this)),
@@ -23,7 +23,7 @@ VulkanDevice::VulkanDevice(Graphics::Window &wnd, const Graphics::VulkanOptions
        descriptor_pool(*static_cast<Device *>(this))
 { }
 
-// Cause the destructor of RefPtr<VulkanFunctions> to be emitted here
+// Cause the destructor of unique_ptr<VulkanFunctions> to be emitted here
 VulkanDevice::~VulkanDevice()
 { }
 
index 8066df64b3355ad513ed0b26271860a10cb9de99..4f9b17d0fa7c0820cd1a965d996ba1a9b1204d33 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef MSP_GL_DEVICE_BACKEND_H_
 #define MSP_GL_DEVICE_BACKEND_H_
 
+#include <memory>
 #include <msp/core/noncopyable.h>
-#include <msp/core/refptr.h>
 #include <msp/graphics/vulkancontext.h>
 #include "descriptorpool.h"
 #include "destroyqueue.h"
@@ -26,7 +26,7 @@ protected:
        Graphics::VulkanContext context;
        VkDevice device;
        VkQueue graphics_queue;
-       RefPtr<VulkanFunctions> functions;
+       std::unique_ptr<VulkanFunctions> functions;
        MemoryAllocator allocator;
        DestroyQueue destroy_queue;
        Synchronizer synchronizer;
index b6ddb12de07b1b9fe190cc51b35fcdde07804ccb..2ee660cee1b41c40309eeea743fa02c091b43755 100644 (file)
@@ -103,7 +103,7 @@ void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map<string, i
 
 #if DEBUG
        if(!debug_name.empty())
-               if(SpirVModule *spirv = static_cast<Program *>(this)->specialized_spirv)
+               if(SpirVModule *spirv = static_cast<Program *>(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<Program *>(this)->specialized_spirv)
+       if(SpirVModule *spirv = static_cast<Program *>(this)->specialized_spirv.get())
                spirv->set_debug_name(debug_name);
 #else
        (void)name;
index 9d807f71539940f6c67f6eb1ccdaa416fa418c77..2a7d73462f384996b016c81ff38afaf1a6642b9a 100644 (file)
@@ -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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index 108acda7e756e26242f0ca76f9c8078ee7ead694..a2bf43c22ce88beefb6b0f9da6c09090974891f4 100644 (file)
@@ -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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index dc52829f2963d03acb82200c786cdb0f2b570b57..9a6c4359303eece214e7587d5a3f0d0ccef5b0ea 100644 (file)
@@ -20,7 +20,7 @@ protected:
        bool is_array() const;
 
 public:
-       AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
+       std::unique_ptr<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index 9d824411def2583d250e12250d8f5d172d7693f7..6926f0ea672b19534852cae738bcfccd7a8ee3eb 100644 (file)
@@ -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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override { return nullptr; }
        std::size_t get_data_size() const override;
        void unload() override { }
 };
index 433d372a32ade8f7e89fd5caae450968ccc937be..c820f1ba6c8598be6163a443a740e744349a4217 100644 (file)
@@ -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<Renderer>();
        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<const WindowView *>(this)->device;
 
        framebuffers.clear();
-       delete swap_chain;
 
-       swap_chain = new SwapChain(w, h, device.get_n_frames_in_flight());
+       swap_chain = make_unique<SwapChain>(w, h, device.get_n_frames_in_flight());
 
        unsigned n_images = swap_chain->get_n_images();
        framebuffers.reserve(n_images);
index f98a65637c36436ce3f245efc32f6722765abd55..b7b4c9b358619230836feab68c9030281efce9e1 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_WINDOWVIEW_BACKEND_H_
 #define MSP_GL_WINDOWVIEW_BACKEND_H_
 
+#include <memory>
 #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<SwapChain> swap_chain;
        std::vector<Framebuffer> framebuffers;
        Framebuffer *current_target = nullptr;
        Semaphore semaphores[MAX_FRAMES_IN_FLIGHT*2];
index aac416ba5b55b4958b45bcfe67319bbbcd99120e..89a70cb34b46b174c19600f4d6e3efdf4a5d800a 100644 (file)
@@ -152,10 +152,11 @@ void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
 
 void Font::Loader::texture()
 {
-       RefPtr<Texture2D> tex = new Texture2D;
+       unique_ptr<Texture2D> tex = make_unique<Texture2D>();
        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)
index a8a4711755388255fe7ead8f35d558959f012d9e..6971b5d5d32e3920d4ebd9313726ce3b67a6d03e 100644 (file)
@@ -23,14 +23,13 @@ void MeshBuilder::auto_offset()
 
 void MeshBuilder::begin_()
 {
-       batch = new Batch(type);
+       batch = make_unique<Batch>(type);
 }
 
 void MeshBuilder::end_()
 {
        mesh.add_batch(move(*batch));
-       delete batch;
-       batch = nullptr;
+       batch.reset();
 }
 
 void MeshBuilder::element_(unsigned i)
index 082db7744b1215850eed45c9f1038226fae0f27b..2b8d7c5d98acc7ac9e77452aeb2775da49576bca 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_MESHBUILDER_H_
 #define MSP_GL_MESHBUILDER_H_
 
+#include <memory>
 #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> batch;
 
 public:
        MeshBuilder(Mesh &);
index 4947e252b711150a573bdfe817692a134477b697..c5523d1320c2bca09e5dd11804ba9a5e7b6c45b7 100644 (file)
@@ -137,38 +137,38 @@ void SequenceBuilder::build(Sequence &sequence) const
        }
 }
 
-Sequence *SequenceBuilder::build() const
+std::unique_ptr<Sequence> SequenceBuilder::build() const
 {
-       RefPtr<Sequence> sequence = new Sequence();
+       unique_ptr<Sequence> sequence = make_unique<Sequence>();
        build(*sequence);
-       return sequence.release();
+       return sequence;
 }
 
-Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
+std::unique_ptr<Sequence> SequenceBuilder::build(unsigned w, unsigned h) const
 {
-       RefPtr<Sequence> sequence = create_sequence(w, h);
+       unique_ptr<Sequence> sequence = create_sequence(w, h);
        build(*sequence);
-       return sequence.release();
+       return sequence;
 }
 
-Sequence *SequenceBuilder::build(const View &view) const
+std::unique_ptr<Sequence> SequenceBuilder::build(const View &view) const
 {
-       RefPtr<Sequence> sequence = create_sequence(view.get_width(), view.get_height());
+       unique_ptr<Sequence> 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<Sequence> SequenceBuilder::build(const Framebuffer &fbo) const
 {
-       RefPtr<Sequence> sequence = create_sequence(fbo.get_width(), fbo.get_height());
+       unique_ptr<Sequence> 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<Sequence> SequenceBuilder::create_sequence(unsigned w, unsigned h) const
 {
        if(!w || !h || tmpl.get_postprocessors().empty())
-               return new Sequence;
+               return make_unique<Sequence>();
 
        unsigned samples = min(tmpl.get_maximum_multisample(), Device::get_current().get_info().limits.max_samples);
        if(samples<tmpl.get_required_multisample())
@@ -178,7 +178,7 @@ Sequence * SequenceBuilder::create_sequence(unsigned w, unsigned h) const
        PixelFormat color_pf = make_pixelformat(RGBA, color_type);
 
        FrameFormat format = (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
-       return new Sequence(w, h, format);
+       return make_unique<Sequence>(w, h, format);
 }
 
 } // namespace GL
index a34f3603a45bd2884b60f7cc4b1def9307b91f1c..cd5b1aff73bc630e81c087d88bd859f52bd4dad7 100644 (file)
@@ -2,6 +2,7 @@
 #define SEQUENCEBUILDER_H_
 
 #include <map>
+#include <memory>
 #include <string>
 #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<Sequence> build() const;
+       std::unique_ptr<Sequence> build(unsigned, unsigned) const;
+       std::unique_ptr<Sequence> build(const View &) const;
+       std::unique_ptr<Sequence> build(const Framebuffer &) const;
+       std::unique_ptr<Sequence> create_sequence(unsigned, unsigned) const;
 };
 
 } // namespace GL
index 67948b8a35a91b25bff8ba86521ac90dc7f96c3d..80c8039c9760d5add887a4e8e87a57a39b1e372e 100644 (file)
@@ -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<GL::PostProcessor> &SequenceTemplate::get_registry<GL::PostProcessor>()
 {
@@ -58,11 +50,6 @@ SequenceTemplate::TemplateRegistry<Effect> &SequenceTemplate::get_registry<Effec
 }
 
 
-SequenceTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
-       postprocessor_template(ppt)
-{ }
-
-
 DataFile::Loader::ActionMap SequenceTemplate::Loader::shared_actions;
 
 SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
@@ -100,7 +87,7 @@ void SequenceTemplate::Loader::effect(const string &slot)
        Renderable rend;
        rend.slot_name = slot;
        rend.effect_template = ldr.get_object();
-       obj.renderables.push_back(rend);
+       obj.renderables.emplace_back(move(rend));
 }
 
 void SequenceTemplate::Loader::multisample(unsigned samples)
@@ -127,14 +114,14 @@ void SequenceTemplate::Loader::postprocessor_with_slot(const string &slot)
        PostProcessor pp;
        pp.postprocessor_template = ldr.get_object();
        pp.slot_name = slot;
-       obj.postprocessors.push_back(pp);
+       obj.postprocessors.emplace_back(move(pp));
 }
 
 void SequenceTemplate::Loader::renderable(const string &slot)
 {
        Renderable rend;
        rend.slot_name = slot;
-       obj.renderables.push_back(rend);
+       obj.renderables.emplace_back(move(rend));
 }
 
 void SequenceTemplate::Loader::renderable_with_default(const string &slot, const string &name)
@@ -142,7 +129,7 @@ void SequenceTemplate::Loader::renderable_with_default(const string &slot, const
        Renderable rend;
        rend.renderable = &get_collection().get<GL::Renderable>(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<Lighting> lightn = new Lighting;
+       unique_ptr<Lighting> lightn = make_unique<Lighting>();
        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)
index b5601a6884748edca7c6e817c46907f50b3ef55f..4c2114a4e37a75160bbcf5c4f236d1a16cbd2f04 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef SEQUENCETEMPLATE_H_
 #define SEQUENCETEMPLATE_H_
 
+#include <memory>
 #include <string>
 #include <vector>
 #include <msp/core/typeregistry.h>
@@ -63,7 +64,7 @@ public:
 
        struct Renderable
        {
-               Effect::Template *effect_template = nullptr;
+               std::unique_ptr<Effect::Template> effect_template = nullptr;
                GL::Renderable *renderable = nullptr;
                SequenceTemplate *sequence_template = nullptr;
                std::map<std::string, std::string> sequence_renderables;
@@ -104,10 +105,8 @@ public:
 
        struct PostProcessor
        {
-               GL::PostProcessor::Template *postprocessor_template;
+               std::unique_ptr<GL::PostProcessor::Template> 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; }
index 0cfd04c6ea43309ecfaf8f78c7aa9c537608e28f..9f1c94a0d74c1ad7b28a5e1e7957e8a3066c0991 100644 (file)
@@ -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::AsyncUpdater> 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<AsyncUpdater>(*this);
 }
 
 
index 3504972d0882890b3b5a1a3219879add766239e7..c6e582df01a6466785da49e3a5fe4dd707bc52fb 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <memory>
 #include <msp/core/noncopyable.h>
 #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<AsyncUpdater> 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<AsyncUpdater> create_async_updater() const;
 };
 
 } // namespace GL
index d69178dd2b45f5ba26a4801bce667f76cc770785..2f1fcd889c53fa5a41b976a22d9dc426f430b8c9 100644 (file)
@@ -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()<req_size))
                {
-                       delete vbuf;
-                       vbuf = new Buffer;
-                       vertices.use_buffer(vbuf);
+                       vbuf = make_unique<Buffer>();
+                       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()<req_size))
                {
-                       delete ibuf;
-                       ibuf = new Buffer;
+                       ibuf = make_unique<Buffer>();
                        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<Resource::AsyncLoader> Mesh::load(IO::Seekable &io, const Resources *)
 {
-       return new AsyncLoader(*this, io);
+       return make_unique<AsyncLoader>(*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;
index 6d517f8500dbc76b44cc4fd3e4c8c6f72c9e7710..7d7ce3b9108ce7eea6c737a650e5246e5dfafcad 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_MESH_H_
 #define MSP_GL_MESH_H_
 
+#include <memory>
 #include <string>
 #include <vector>
 #include <msp/datafile/objectloader.h>
@@ -53,8 +54,8 @@ private:
        private:
                Mesh &mesh;
                IO::Seekable &io;
-               Bufferable::AsyncUpdater *vertex_updater = nullptr;
-               Bufferable::AsyncUpdater *index_updater = nullptr;
+               std::unique_ptr<Bufferable::AsyncUpdater> vertex_updater;
+               std::unique_ptr<Bufferable::AsyncUpdater> index_updater;
                unsigned phase = 0;
 
        public:
@@ -73,8 +74,8 @@ private:
 
        VertexArray vertices;
        std::vector<Batch> batches;
-       Buffer *vbuf = nullptr;
-       Buffer *ibuf = nullptr;
+       std::unique_ptr<Buffer> vbuf;
+       std::unique_ptr<Buffer> 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<Resource::AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override;
        std::uint64_t get_data_size() const override;
        void unload() override;
 
index e987c16569817369f6db832cde17f733cf8a91af..249ee0ff1f3c1b4e9a4375d6ddf345855570c997 100644 (file)
@@ -259,7 +259,7 @@ void SpirVModule::reflect()
        }
 }
 
-SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
+unique_ptr<SpirVModule> SpirVModule::specialize(const map<string, int> &spec_values) const
 {
        vector<uint8_t> flags(code[3], 1);
 
@@ -426,7 +426,7 @@ SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
                op += word_count;
        }
 
-       SpirVModule *spec_mod = new SpirVModule;
+       unique_ptr<SpirVModule> spec_mod = make_unique<SpirVModule>();
        spec_mod->code = move(new_code);
        spec_mod->reflect();
        spec_mod->create();
index 9c8d5e0b72f02e8456af9eb25a67e8db39fb66a4..26e2be33500dd522463c47f239b1ea1447d8a47c 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_MODULE_H_
 
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 #include <msp/io/base.h>
@@ -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<std::string, int> &) const;
+       std::unique_ptr<SpirVModule> specialize(const std::map<std::string, int> &) const;
 
 private:
        std::vector<const InstructionBlock *> collect_visited_blocks(const std::map<unsigned, int> &) const;
index 3c0022ad19481b3f9e2b2b754baa65cf7638faf2..9f014aae71dbcfd411e6bf658cbf8c0c693a69d6 100644 (file)
@@ -12,19 +12,6 @@ Program::Program(const Module &mod, const map<string, int> &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<string, int> &spec_values)
 {
        if(has_stages())
@@ -42,7 +29,7 @@ void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
                if(static_cast<const SpirVModule &>(mod).is_specializable())
                {
                        specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
-                       final_module = specialized_spirv;
+                       final_module = specialized_spirv.get();
                }
                add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
                break;
index a664f9b5e41c5a84d5c126b287a8664f49ede888..5ea146c7ae7c1e2cd138a8af3162e070012b2d44 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_PROGRAM_H_
 
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 #include <msp/datafile/objectloader.h>
@@ -51,7 +52,7 @@ private:
        };
 
        ReflectData reflect_data;
-       SpirVModule *specialized_spirv = nullptr;
+       std::unique_ptr<SpirVModule> 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::string, int> & = std::map<std::string, int>());
 
-       Program(Program &&);
-       ~Program();
-
        void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
 private:
        void collect_uniforms(const SpirVModule &);
index 3c5e58aa3e208eb16e3d534ad9a8cd3f3d136761..d6e2f2bdc4605cfb526021f002210989807c3910 100644 (file)
@@ -121,7 +121,7 @@ void Texture::Loader::finish()
 
 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
 {
-       RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
+       unique_ptr<IO::Seekable> io = get_collection().open_raw(fn);
        if(!io)
                throw IO::file_not_found(fn);
        img.load_io(*io);
index bf57b22473f65e39e069683bf394bda80884d368..aea88358254feab02c8669e1baad1919a6cbcc6b 100644 (file)
@@ -1,3 +1,4 @@
+#include <memory>
 #include <msp/datafile/rawdata.h>
 #include <msp/graphics/imageloader.h>
 #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<Graphics::ImageLoader> img_loader;
+       std::unique_ptr<DataFile::RawData> 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<unsigned, 2> Texture2D::get_level_size(unsigned level) const
        return LinAl::Vector<unsigned, 2>(w, h);
 }
 
-Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
+unique_ptr<Resource::AsyncLoader> Texture2D::load(IO::Seekable &io, const Resources *)
 {
-       return new AsyncLoader(static_cast<Texture2D &>(*this), io);
+       return make_unique<AsyncLoader>(*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<DataFile::RawData>();
                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
index 01b0c9cb73950f3ad0aacbf373f3c60b8309f5c3..722da906e70cd23c9db52b566ed45f3596962794 100644 (file)
@@ -95,7 +95,7 @@ private:
        LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
 
 public:
-       Resource::AsyncLoader *load(IO::Seekable &, const Resources * = nullptr) override;
+       std::unique_ptr<Resource::AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) override;
 };
 
 } // namespace GL
index bfa63873b2e188f2f339940fc48bf09e4c38034e..e9a6f4ed855cd3de0f0e41994e45929cd4bb05c8 100644 (file)
@@ -156,7 +156,7 @@ void TextureCube::Loader::init()
 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
 {
        Graphics::Image img;
-       RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
+       unique_ptr<IO::Seekable> io = get_collection().open_raw(fn);
        img.load_io(*io);
 
        obj.image(face, img);
index a50177fe972f8f47536ecefdad6ee38f0f56b4c5..d7ed6ba1c320b806c005e2ed1a4643a4eb411ba7 100644 (file)
@@ -39,9 +39,9 @@ const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup()
        if(rotate_lookup)
                return *rotate_lookup;
 
-       rotate_lookup = new Texture2D;
+       unique_ptr<Texture2D> owned_ptr = make_unique<Texture2D>();
+       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<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
+       unique_ptr<AmbientOcclusion> ao = make_unique<AmbientOcclusion>(width/size_divisor, height/size_divisor);
        ao->set_n_samples(n_samples);
        ao->set_occlusion_radius(occlusion_radius);
        ao->set_darkness(darkness);
index 43a22b3e6ddff19a8665f9b286d60f662740cd9c..3a52d6a46808266a5b468231167af3cac951f019 100644 (file)
@@ -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<RenderTarget>(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> bloom = new Bloom(width/size_divisor, height/size_divisor);
+       unique_ptr<Bloom> bloom = make_unique<Bloom>(width/size_divisor, height/size_divisor);
        bloom->set_radius(radius);
        bloom->set_strength(strength);
        return bloom.release();
index 51299a977e5ac977fa0d0b58779c3619fcfe0c0f..dee6b073f2862766ea815ee32142cc27b937e0c7 100644 (file)
@@ -36,7 +36,7 @@ public:
        };
 
 private:
-       RenderTarget *target[2];
+       std::unique_ptr<RenderTarget> 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. */
index b1371794b83521598a105e64effd397d6fbd2bd6..a71eb44c6e2881b2fcf8678214491e04c2cc7bd3 100644 (file)
@@ -92,7 +92,7 @@ void ColorCurve::set_debug_name(const string &name)
 
 ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const
 {
-       RefPtr<ColorCurve> colorcurve = new ColorCurve();
+       unique_ptr<ColorCurve> colorcurve = make_unique<ColorCurve>();
        colorcurve->set_exposure_adjust(exposure_adjust);
        colorcurve->set_brightness_response(brightness_response);
        if(srgb)
index c9bfb0d0bf772451d43dd320b41b014d510e0deb..d3ec23d5d885612a9d8572261d05c68e203f4fb8 100644 (file)
@@ -225,7 +225,7 @@ EnvironmentMap *EnvironmentMap::Template::create(const map<string, Renderable *>
        if(!content || !environment)
                throw invalid_operation("EnvironmentMap::Template::create");
 
-       RefPtr<EnvironmentMap> env_map = new EnvironmentMap(size, format, roughness_levels, *content, *environment);
+       unique_ptr<EnvironmentMap> env_map = make_unique<EnvironmentMap>(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);
index d9ee677e695a4666fb9c7bf8958d648dce8b4ffb..e8289f03a5f2be9b7b1d074104517e18734002e4 100644 (file)
@@ -281,7 +281,7 @@ ShadowMap *ShadowMap::Template::create(const map<string, Renderable *> &renderab
        if(!content || !lighting)
                throw invalid_operation("ShadowMap::Template::create");
 
-       RefPtr<ShadowMap> shadow_map = new ShadowMap(width, height, *content, *lighting);
+       unique_ptr<ShadowMap> shadow_map = make_unique<ShadowMap>(width, height, *content, *lighting);
        shadow_map->set_target(target, radius);
        shadow_map->set_depth_bias(depth_bias);
        shadow_map->set_darkness(darkness);
index 1d1699db008ee691b5dc423842940456cd509d9a..d9bbe6fc1c6583b1b98c4db30b81c83f6d935c6d 100644 (file)
@@ -188,7 +188,7 @@ Sky *Sky::Template::create(const map<string, Renderable *> &renderables) const
        if(!content || !sun)
                throw invalid_operation("Sky::Template::create");
 
-       RefPtr<Sky> sky = new Sky(*content, *sun);
+       unique_ptr<Sky> sky = make_unique<Sky>(*content, *sun);
        create_base(*sky);
 
        return sky.release();
index d8540aaedfc5f4292bbf26d484bd45b99c0ca43a..35042460c7c481e17e2d67ac660dda1b861814ec 100644 (file)
@@ -30,7 +30,7 @@ Module *get_builtins_module()
        {
                initialized = true;
 
-               RefPtr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
+               unique_ptr<IO::Seekable> io = Resources::get_builtins().open("_builtin.glsl");
                if(!io)
                        return nullptr;
 
index ef864dc028a64dc96ad412c67bc44dba41c0d28a..e8b88d2d0419d8858ffb4c2db2b7f075aef99ddd 100644 (file)
@@ -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<Module>(*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<Module>(*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> module = new Module;
+       unique_ptr<Module> module = make_unique<Module>();
        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> module = new Module;
+       unique_ptr<Module> module = make_unique<Module>();
        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::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
+       unique_ptr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
        if(!io)
                throw runtime_error(format("module %s not found", name));
 
index 91f6a5e55d07878847d74326c5d7843b3787b369..a01a5ce43314c25f5fa4af0759fae2fd73a3d30f 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_SL_MODULECACHE_H_
 #define MSP_GL_SL_MODULECACHE_H_
 
+#include <memory>
 #include <msp/datafile/collection.h>
 #include "syntax.h"
 
@@ -12,14 +13,13 @@ class ModuleCache
 {
 private:
        DataFile::Collection *resources;
-       std::map<std::string, Module *> modules;
+       std::map<std::string, std::unique_ptr<Module>> 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 &);
index 8e565c53698784e39fa96da71ed7854740c19ef4..dab0afe361d1e6b29ea83ea6342cd9b033d93911 100644 (file)
@@ -45,17 +45,9 @@ const Program *Material::create_compatible_shader(const map<string, int> &extra_
                return shprog;
 
        const Module &module = res.get<Module>(module_name);
-       shprog = new Program(module, spec_values);
-       try
-       {
-               res.add(name, shprog);
-       }
-       catch(...)
-       {
-               delete shprog;
-               throw;
-       }
-
+       unique_ptr<Program> owned_ptr = make_unique<Program>(module, spec_values);
+       shprog = owned_ptr.get();
+       res.add(name, move(owned_ptr));
        return shprog;
 }
 
index 43da34917e61c4c3e56b16787d2d8ca44d4d4a64..3c85226445077c94678f3d15c4345574d9c4a634 100644 (file)
@@ -44,9 +44,9 @@ const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
        if(fresnel_lookup)
                return *fresnel_lookup;
 
-       fresnel_lookup = new Texture2D;
+       unique_ptr<Texture2D> owned_ptr = make_unique<Texture2D>();
+       fresnel_lookup = owned_ptr.get();
        fresnel_lookup->storage(RG8, 128, 128, 1);
-       resources.add(name, fresnel_lookup);
 
        const Program &shprog = resources.get<Program>("_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;
 }
 
index e8cec667159bebe9aabb825b3135ba9ba9b374b0..4422fcaf2b35302ad55df853be93f38674a21ad5 100644 (file)
@@ -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<ProgramData::ProgramBlock>::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<Buffer> old_buffer = move(buffer);
+       buffer = make_unique<Buffer>();
        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<UniformBlock>(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<ProgramData::ProgramBlock>::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;
                        }
                }
index d0229b34559bc885d2fdc62ef6d9cf5d17ba2c8c..6e0ce6db2f4814c4850ac35a45bca2d0704b2a44 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_PROGRAMDATA_H_
 #define MSP_GL_PROGRAMDATA_H_
 
+#include <memory>
 #include <stdexcept>
 #include <msp/core/noncopyable.h>
 #include <msp/datafile/objectloader.h>
@@ -114,7 +115,7 @@ private:
                ReflectData::LayoutHash block_hash = 0;
                Mask used = 0;
                Mask dirty = 0;
-               UniformBlock *block = nullptr;
+               std::unique_ptr<UniformBlock> block;
                union
                {
                        std::uint8_t type_flag;
@@ -157,7 +158,7 @@ private:
        mutable std::vector<SharedBlock> blocks;
        mutable std::vector<ProgramBlock> programs;
        mutable UniformBlock *last_buffer_block = nullptr;
-       mutable Buffer *buffer = nullptr;
+       mutable std::unique_ptr<Buffer> buffer;
        bool streaming = false;
        mutable Mask dirty = 0;
        std::string debug_name;
index 9a7b637b612040b3a088a2de94e50edacc4f2dcd..789fa0d52389bfd0e25adc6fdcfffce55301b323 100644 (file)
@@ -42,8 +42,8 @@ void RenderMethod::maybe_create_material_shader()
 
        if(shdata)
        {
-               RefPtr<ProgramData> old_shdata = shdata;
-               shdata = new ProgramData(shprog);
+               shared_ptr<ProgramData> old_shdata = move(shdata);
+               shdata = make_shared<ProgramData>(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<ProgramData>();
                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<ProgramData> old_shdata = obj.shdata;
-               obj.shdata = new ProgramData(obj.shprog);
+               shared_ptr<ProgramData> old_shdata = obj.shdata;
+               obj.shdata = make_shared<ProgramData>(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<ProgramData>(obj.shprog);
+       else if(obj.shdata.use_count()>1)
        {
-               RefPtr<ProgramData> old_shdata = obj.shdata;
-               obj.shdata = new ProgramData;
+               shared_ptr<ProgramData> old_shdata = obj.shdata;
+               obj.shdata = make_shared<ProgramData>();
                obj.shdata->copy_uniforms(*old_shdata);
        }
        load_sub(*obj.shdata);
index 14d7541a508caede2036810739bc30100ac024a8..f41a0b4240f09e41029b832c56f03017bf53f585 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef MSP_GL_RENDERPASS_H_
 #define MSP_GL_RENDERPASS_H_
 
-#include <msp/core/refptr.h>
+#include <memory>
 #include <msp/datafile/objectloader.h>
 #include "blend.h"
 #include "cullface.h"
@@ -81,7 +81,7 @@ private:
 
        const Program *shprog = nullptr;
        bool shprog_from_material = false;
-       RefPtr<ProgramData> shdata;
+       std::shared_ptr<ProgramData> shdata;
        std::map<Tag, Tag> uniform_slots;
        const Material *material = nullptr;
        std::string material_slot;
index 70af9bbbe8ca6a8a0e2d509c26f1fb918a183847..a86d30921f4040e1e98171209e1e808ac0dd7313 100644 (file)
@@ -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<string, int> &spec_values) const
 {
@@ -58,17 +50,17 @@ void SplatMaterial::fill_program_info(string &module_name, map<string, int> &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::Seekable> io = coll.open_raw(sub.source_fn);
+       unique_ptr<IO::Seekable> 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<Texture2DArray>();
                texture->storage(format, width, height, max_layers);
        }
 }
index 70662837212dc94aa0a372accf09011ee8e1673c..9928440ba353684587f349081f76897d30208668 100644 (file)
@@ -73,7 +73,7 @@ private:
 
        struct MapArray
        {
-               Texture2DArray *texture = nullptr;
+               std::unique_ptr<Texture2DArray> texture;
                PixelFormat format = NO_PIXELFORMAT;
                unsigned width = 0;
                unsigned height = 0;
index 55914f35313ee44a975738f2afdeda203aab764d..4418b1df2a3fee1aaba1e09b435a53ad3c57372e 100644 (file)
@@ -1,4 +1,4 @@
-#include <msp/core/refptr.h>
+#include <memory>
 #include <msp/datafile/collection.h>
 #include <msp/fs/utils.h>
 #include <msp/strings/format.h>
@@ -71,7 +71,7 @@ bool Technique::replace_uniforms(const ProgramData &shdata)
        const vector<Tag> &uniform_tags = shdata.get_uniform_tags();
        for(auto &kvp: methods)
        {
-               RefPtr<ProgramData> new_shdata;
+               unique_ptr<ProgramData> 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<ProgramData>();
                                new_shdata->copy_uniforms(*kvp.second.get_shader_data());
                        }
 
index 20dcb8885ea86061905949d8f995e47a0fd2ad46..fee3b862ff56a07e3f85a613da3a1d72a9a998e7 100644 (file)
@@ -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<Buffer>();
+       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;
 }
index bc89f57000048e954e6ef19bbb4bb0efbd076ec4..c622f6e70b18834f95fbfde1fd36530a393959dd 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_INSTANCEARRAY_H_
 #define MSP_GL_INSTANCEARRAY_H_
 
+#include <memory>
 #include <vector>
 #include <msp/core/noncopyable.h>
 #include "mspgl_api.h"
@@ -55,7 +56,7 @@ private:
 
        const Object &object;
        VertexArray instance_data;
-       Buffer *instance_buffer = nullptr;
+       std::unique_ptr<Buffer> instance_buffer;
        VertexSetup vtx_setup;
        int matrix_location = -1;
        unsigned matrix_offset = 0;
index 0ae739a38151eed4f8acf93152420065f3694ab7..9a75ff55197e93f655ba64582fdd8372cba34460 100644 (file)
@@ -249,10 +249,11 @@ void Object::LodLoader::mesh(const string &n)
 
 void Object::LodLoader::mesh_inline()
 {
-       RefPtr<Mesh> msh = new Mesh;
+       unique_ptr<Mesh> msh = make_unique<Mesh>();
        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<Technique> tech = new Technique;
+       unique_ptr<Technique> tech = make_unique<Technique>();
        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
index 28fd5669907133993813e748e2459cf4013958ff..0eef1061aaafbe297e990820cd8719d2ef2e5d92 100644 (file)
@@ -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<Texture2D> tex2d = make_unique<Texture2D>();
                tex2d->storage(pf, width, height, 1);
 
                if(multisample)
                {
-                       Texture2DMultisample *tex2d_ms = new Texture2DMultisample;
+                       unique_ptr<Texture2DMultisample> tex2d_ms = make_unique<Texture2DMultisample>();
                        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<const Texture2D *>(textures[i]);
+       return *static_cast<const Texture2D *>(textures[i].get());
 }
 
 const Texture2D &RenderTarget::get_target_texture(FrameAttachment fa) const
index f3406f0b051c6e7afbb15f26ce85d1509561a2e0..bcb9a2a4c329157d2167def67ab750b15f175bac 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef RENDERTARGET_H_
 #define RENDERTARGET_H_
 
+#include <memory>
 #include <msp/core/noncopyable.h>
 #include "framebuffer.h"
 #include "mspgl_api.h"
@@ -23,7 +24,7 @@ class MSPGL_API RenderTarget: public NonCopyable
 private:
        unsigned width;
        unsigned height;
-       std::vector<Texture *> textures;
+       std::vector<std::unique_ptr<Texture>> textures;
        Framebuffer fbo;
 
 public:
index a05e7110ce770def4ab7a49559b507151ed2312a..fd3ce9ba922be3f0892a94a01a266eeafc435a9f 100644 (file)
@@ -47,10 +47,11 @@ Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m):
 
 void Scene::Loader::array(const string &n)
 {
-       RefPtr<InstanceArray<> > arr = new InstanceArray<>(get_collection().get<GL::Object>(n));
+       unique_ptr<InstanceArray<>> arr = make_unique<InstanceArray<>>(get_collection().get<GL::Object>(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<ObjectInstance> inst = new ObjectInstance(get_collection().get<GL::Object>(n));
+       unique_ptr<ObjectInstance> inst = make_unique<ObjectInstance>(get_collection().get<GL::Object>(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)
index 582e63e9bd13fcc107290f5c9e15ed82678d0de1..c355020e1458f820d2dd3f1ce72d678d5ee051c2 100644 (file)
@@ -14,6 +14,9 @@ namespace GL {
 
 const Tag Sequence::noclear_tag = "noclear";
 
+// Hide std::unique_ptr<RenderTarget> 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<RenderTarget>(width, height, postproc_fmt);
+       target[1] = make_unique<RenderTarget>(width, height, postproc_fmt);
 
        if(target_format.get_samples()>1)
-               target_ms = new RenderTarget(width, height, target_format);
+               target_ms = make_unique<RenderTarget>(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();
                }
        }
 }
index 0c7c869c4b12fde77af74340cfcdd810a78be99f..8fa398a7aaeed7e47d80f3ca9c72a228e87e29ca 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_SEQUENCE_H_
 #define MSP_GL_SEQUENCE_H_
 
+#include <memory>
 #include <vector>
 #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<RenderTarget> target[2];
+       std::unique_ptr<RenderTarget> target_ms;
        bool clear_enabled = false;
        std::vector<Color> 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;
 
index b55c5b4c3f754a4b76d2a97a368bd82b892bcc71..256d02a649edc89815aebc34421c4e6f2ca5b7a4 100644 (file)
@@ -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<Renderer>();
        internal_renderer->begin();
        render(*internal_renderer);
        internal_renderer->end();
index 1c63f84bfc09be95b2a9199f27f8563f0ff180bf..20c3fa30ce1028226027a0437f6fa3e30ff9444a 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_VIEW_H_
 #define MSP_GL_VIEW_H_
 
+#include <memory>
 #include <msp/core/noncopyable.h>
 #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<Renderer> internal_renderer;
 
-       View() = default;
+       View();
 public:
-       View(View &&);
        virtual ~View();
 
        virtual unsigned get_width() const { return get_target().get_width(); }
index 465f81dc0ac407e40eb422dcc59e060a639e1d89..37703fe8dfab52c805a891921468ae3006fd8559 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_RESOURCE_H_
 
 #include <cstdint>
+#include <memory>
 #include <msp/core/noncopyable.h>
 #include <msp/io/seekable.h>
 #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<AsyncLoader> load(IO::Seekable &, const Resources * = nullptr) = 0;
        virtual bool is_loaded() const;
 
        /** Returns the amount of graphics memory used by this resource.  The
index a2023f5d760643ec1bef8afb4ccfde0d1940ba54..b9b093bf397a134a50a2025e7fe4a9831cacaeb4 100644 (file)
@@ -73,7 +73,7 @@ void ResourceManager::move_resource(Resource &from, Resource &to)
                throw invalid_operation("ResourceManager::move_resource");
        ManagedResource *managed = reinterpret_cast<ManagedResource *>(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)
        {
index 8dfd697868a0e3017214136b28fa16f28e0dddc9..859c940341519655f11476b5493efc83e6c3d2c7 100644 (file)
@@ -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::Seekable> io;
+               std::unique_ptr<Resource::AsyncLoader> loader;
                State state = NOT_LOADED;
                unsigned last_used = 0;
                std::uint64_t data_size = 0;
index ec17ec86a5aac0dc7807601bde25aeb242b974cd..fd6c306b0fc8fb91f4d00bfc214de2c07a61fbd8 100644 (file)
@@ -49,12 +49,12 @@ Resources::Resources(bool set_as_global)
        add_type<Animation>().suffix(".anim").keyword("animation");
        add_type<Armature>().suffix(".arma").keyword("armature");
        add_type<BasicMaterial>().base<Material>().suffix(".mat")
-               .creator([this](const string &n) -> BasicMaterial * { create_generic<Material>(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<BasicMaterial> { create_generic<Material>(n); return nullptr; })
                .notify(&set_debug_name<Material>);
        add_type<Camera>().keyword("camera")
                .notify(&set_debug_name<Camera>);
        add_type<DirectionalLight>().base<Light>().suffix(".light")
-               .creator([this](const string &n) -> DirectionalLight * { create_generic<Light>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<DirectionalLight> { create_generic<Light>(n); return nullptr; });
        add_type<Font>().keyword("font");
        add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
        add_type<Lighting>().suffix(".lightn").keyword("lighting")
@@ -67,14 +67,14 @@ Resources::Resources(bool set_as_global)
                .notify(&set_debug_name<Module>);
        add_type<Object>().base<Renderable>().keyword("object");
        add_type<OccludedScene>().base<Scene>().base<Renderable>().suffix(".scene")
-               .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<OccludedScene> { create_generic<Scene>(n); return nullptr; });
        add_type<OrderedScene>().base<Scene>().base<Renderable>().suffix(".scene")
-               .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<OrderedScene> { create_generic<Scene>(n); return nullptr; });
        add_type<PbrMaterial>().base<Material>().suffix(".mat")
-               .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<PbrMaterial> { create_generic<Material>(n); return nullptr; })
                .notify(&set_debug_name<Material>);
        add_type<PointLight>().base<Light>().suffix(".light")
-               .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<PointLight> { create_generic<Light>(n); return nullptr; });
        add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
        add_type<Pose>().keyword("pose");
        add_type<Program>().keyword("shader")
@@ -83,32 +83,32 @@ Resources::Resources(bool set_as_global)
        add_type<Sampler>().suffix(".samp").keyword("sampler")
                .notify(&set_debug_name<Sampler>);
        add_type<SimpleScene>().base<Scene>().base<Renderable>().suffix(".scene")
-               .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<SimpleScene> { create_generic<Scene>(n); return nullptr; });
        add_type<SplatMaterial>().base<Material>().suffix(".mat")
-               .creator([this](const string &n) -> SplatMaterial * { create_generic<Material>(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<SplatMaterial> { create_generic<Material>(n); return nullptr; })
                .notify(&set_debug_name<Material>);
        add_type<Technique>().suffix(".tech").keyword("technique")
                .notify(&set_debug_name<Technique>);
        add_type<Texture1D>().base<Texture>().suffix(".tex")
-               .creator([this](const string &n) -> Texture1D * { create_texture(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<Texture1D> { create_texture(n); return nullptr; })
                .notify(&set_debug_name<Texture1D>);
        add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
-               .creator([this](const string &n) -> Texture2D * { create_texture(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<Texture2D> { create_texture(n); return nullptr; })
                .notify(&set_debug_name<Texture2D>);
        add_type<Texture3D>().base<Texture>().suffix(".tex")
-               .creator([this](const string &n) -> Texture3D * { create_texture(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<Texture3D> { create_texture(n); return nullptr; })
                .notify(&set_debug_name<Texture3D>);
        add_type<TextureCube>().base<Texture>().suffix(".tex")
-               .creator([this](const string &n) -> TextureCube * { create_texture(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<TextureCube> { create_texture(n); return nullptr; })
                .notify(&set_debug_name<TextureCube>);
        add_type<Texture2DArray>().base<Texture>().suffix(".tex")
-               .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<Texture2DArray> { create_texture(n); return nullptr; })
                .notify(&set_debug_name<Texture2DArray>);
        add_type<UnlitMaterial>().base<Material>().suffix(".mat")
-               .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return nullptr; })
+               .creator([this](const string &n) -> unique_ptr<UnlitMaterial> { create_generic<Material>(n); return nullptr; })
                .notify(&set_debug_name<Material>);
        add_type<ZSortedScene>().base<Scene>().base<Renderable>().suffix(".scene")
-               .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return nullptr; });
+               .creator([this](const string &n) -> unique_ptr<ZSortedScene> { create_generic<Scene>(n); return nullptr; });
 
        add_source(get_builtins());
 
@@ -150,9 +150,9 @@ void Resources::set_resource_manager(ResourceManager *m)
 }
 
 template<typename T, typename L>
-T *Resources::create_generic(const string &name)
+unique_ptr<T> Resources::create_generic(const string &name)
 {
-       if(RefPtr<IO::Seekable> io = open_raw(name))
+       if(unique_ptr<IO::Seekable> 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<Mesh> Resources::create_mesh(const string &name)
 {
        if(!resource_manager || name[0]=='_')
                return nullptr;
 
-       if(RefPtr<IO::Seekable> io = open_raw(name))
+       if(unique_ptr<IO::Seekable> io = open_raw(name))
        {
-               RefPtr<Mesh> mesh = new Mesh;
+               unique_ptr<Mesh> mesh = make_unique<Mesh>();
                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<Texture> 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<Texture>(name);
        }
 
-       if(RefPtr<IO::Seekable> io = open_raw(name))
+       if(unique_ptr<IO::Seekable> io = open_raw(name))
        {
-               RefPtr<Texture2D> tex;
+               unique_ptr<Texture2D> tex;
 
                // Verify that the image is loadable
                Graphics::Image image;
                if(!managed)
                        image.load_io(*io);
 
-               tex = new Texture2D;
+               tex = make_unique<Texture2D>();
 
                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<Module> Resources::create_module(const string &name)
 {
        string ext = FS::extpart(name);
        if(ext!=".glsl" && ext!=".spv")
                return nullptr;
 
-       if(RefPtr<IO::Seekable> io = open_raw(name))
+       if(unique_ptr<IO::Seekable> io = open_raw(name))
        {
                if(ext==".glsl")
                {
-                       RefPtr<Module> module;
+                       unique_ptr<Module> module;
                        if(get_backend_api()==VULKAN)
-                               module = new SpirVModule;
+                               module = make_unique<SpirVModule>();
                        else
-                               module = new GlslModule;
+                               module = make_unique<GlslModule>();
                        module->load_source(*io, this, name);
-                       return module.release();
+                       return module;
                }
                else if(ext==".spv")
                {
-                       RefPtr<SpirVModule> module = new SpirVModule;
+                       unique_ptr<SpirVModule> module = make_unique<SpirVModule>();
                        module->load_code(*io);
-                       return module.release();
+                       return module;
                }
        }
        else if(ext==".spv")
        {
                if((io = open_raw(FS::basepart(name)+".glsl")))
                {
-                       RefPtr<SpirVModule> module = new SpirVModule;
+                       unique_ptr<SpirVModule> module = make_unique<SpirVModule>();
                        module->load_source(*io, this, name);
-                       return module.release();
+                       return module;
                }
        }
 
        return nullptr;
 }
 
-Program *Resources::create_program(const string &name)
+unique_ptr<Program> 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<Module>(base);
-               RefPtr<Program> shprog = new Program;
+               unique_ptr<Program> shprog = make_unique<Program>();
                shprog->add_stages(module);
-               return shprog.release();
+               return shprog;
        }
 
        return nullptr;
index ff2bcd5f597b51a5c82decbd51229223454bf05e..962c560ffaa6f5e085eb8eccf1e03104c64b7db0 100644 (file)
@@ -98,12 +98,12 @@ public:
 
 protected:
        template<typename T, typename L = typename T::GenericLoader>
-       T *create_generic(const std::string &);
+       std::unique_ptr<T> 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<Mesh> create_mesh(const std::string &);
+       std::unique_ptr<Texture> create_texture(const std::string &);
+       std::unique_ptr<Module> create_module(const std::string &);
+       std::unique_ptr<Program> create_program(const std::string &);
 
        template<typename T>
        static void set_debug_name(const std::string &, T &);
index 8ac41a23330070cb3ce574389c4230537153b89e..410b5969ff4e8e829e296cf933f9518c82733f52 100644 (file)
@@ -69,10 +69,10 @@ private:
        Input::Mouse mouse;
        Resources resources;
        GL::WindowView view;
-       GL::Sequence *sequence = nullptr;
+       unique_ptr<GL::Sequence> sequence;
        GL::Renderable *renderable = nullptr;
-       GL::AnimatedObject *anim_object = nullptr;
-       GL::AnimationPlayer *anim_player = nullptr;
+       unique_ptr<GL::AnimatedObject> anim_object;
+       unique_ptr<GL::AnimationPlayer> anim_player;
        GL::DirectionalLight light;
        GL::Lighting lighting;
        GL::Camera camera;
@@ -89,9 +89,8 @@ public:
 private:
        template<typename T>
        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<GL::Mesh> owned_mesh = make_unique<GL::Mesh>();
+                       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<GL::Mesh>(opts.renderable_name);
 
-               object = new GL::Object;
-               GL::Technique *tech = new GL::Technique;
+               unique_ptr<GL::Object> owned_object = make_unique<GL::Object>();
+               object = owned_object.get();
+               unique_ptr<GL::Technique> tech = make_unique<GL::Technique>();
                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<GL::Object>(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<GL::Scene> scene = ldr.get_object();
+                       renderable = scene.get();
+                       resources.add("__.scene", move(scene));
                }
                else
                        renderable = &resources.get<GL::Scene>(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<GL::Animation>(opts.animation_name);
-               anim_player = new GL::AnimationPlayer;
-               anim_object = new GL::AnimatedObject(*object);
+               anim_player = make_unique<GL::AnimationPlayer>();
+               anim_object = make_unique<GL::AnimatedObject>(*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<GL::Sequence>();
                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<T> owned_thing = make_unique<T>();
+               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<T>(name);
 }
 
-Viewer::~Viewer()
-{
-       delete anim_player;
-       delete anim_object;
-       delete sequence;
-}
-
 int Viewer::main()
 {
        window.show();