]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor owned postprocessors in Sequence into a more generic mechanism
authorMikko Rasa <tdb@tdb.fi>
Sun, 17 Oct 2021 08:26:13 +0000 (11:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 17 Oct 2021 08:26:13 +0000 (11:26 +0300)
source/builders/sequencebuilder.cpp
source/render/sequence.cpp
source/render/sequence.h

index 5fca61400a03645941c06a053c1353ff16513ba2..bf3fef42dfb82f55989a21f8c325139ee569ba7d 100644 (file)
@@ -74,7 +74,7 @@ void SequenceBuilder::build(Sequence &sequence) const
 #endif
        for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
        {
-               PostProcessor *proc = 0;
+               RefPtr<PostProcessor> proc = 0;
                if(!p.slot_name.empty())
                        proc = get_item(postprocessors, p.slot_name);
                if(proc)
@@ -88,7 +88,8 @@ void SequenceBuilder::build(Sequence &sequence) const
                                if(!debug_name.empty())
                                        proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
 #endif
-                               sequence.add_postprocessor_owned(proc);
+                               sequence.add_postprocessor(*proc.get());
+                               sequence.add_owned(proc.release());
                        }
                }
        }
index 4ccf540809d4a3138f4e30e3d23a0ca3e0e35370..5d003753bc2738d89fc49d5e34d26956b466ca78 100644 (file)
@@ -33,9 +33,8 @@ Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
 
 Sequence::~Sequence()
 {
-       for(PostProcStep &p: postproc)
-               if(p.owned)
-                       delete p.postproc;
+       for(OwnedObject &o: owned_data)
+               o.delete_func(o.pointer);
        delete target[0];
        delete target[1];
        delete target_ms;
@@ -71,24 +70,10 @@ Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
 }
 
 void Sequence::add_postprocessor(PostProcessor &pp)
-{
-       add_postprocessor(&pp, false);
-}
-
-void Sequence::add_postprocessor_owned(PostProcessor *pp)
-{
-       add_postprocessor(pp, true);
-}
-
-void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
 {
        if(target_format.empty())
-       {
-               if(owned)
-                       delete pp;
                throw invalid_operation("Sequence::add_postprocessor");
-       }
-       postproc.push_back(PostProcStep(pp, owned));
+       postproc.push_back(&pp);
 }
 
 void Sequence::setup_frame(Renderer &renderer)
@@ -171,7 +156,7 @@ void Sequence::render(Renderer &renderer, Tag tag) const
                        renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
                        const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
                        const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
-                       postproc[i].postproc->render(renderer, color, depth);
+                       postproc[i]->render(renderer, color, depth);
                }
        }
 }
index 13431ac469e824c4b9e5d1a4283678d710011090..f51d28b948fc783520f124d44812cb78ebf75843 100644 (file)
@@ -56,16 +56,16 @@ public:
        };
 
 private:
-       struct PostProcStep
+       struct OwnedObject
        {
-               PostProcessor *postproc;
-               bool owned;
+               void *pointer = 0;
+               void (*delete_func)(void *) = 0;
 
-               PostProcStep(PostProcessor *pp, bool o): postproc(pp), owned(o) { }
+               OwnedObject(void *p, void (*d)(void *)): pointer(p), delete_func(d) { }
        };
 
        std::vector<Step> steps;
-       std::vector<PostProcStep> postproc;
+       std::vector<PostProcessor *> postproc;
        unsigned width = 0;
        unsigned height = 0;
        FrameFormat target_format;
@@ -75,6 +75,7 @@ private:
        std::vector<Color> clear_colors;
        float clear_depth = 1.0f;
        int clear_stencil = 0;
+       std::vector<OwnedObject> owned_data;
 
        static Tag noclear_tag;
 
@@ -99,15 +100,11 @@ public:
        /** Adds a postprocessor to the sequence. */
        void add_postprocessor(PostProcessor &);
 
-       /** Adds a postprocessor to the sequence, transferring ownership.  The
-       postprocessor will be deleted together with with sequence.  It is also
-       deleted if this call throws an exception. */
-       void add_postprocessor_owned(PostProcessor *);
+       /** Adds an owned object, which will be deleted together with the sequence. */
+       template<typename T>
+       void add_owned(T *p)
+       { owned_data.push_back({ p, [](void *ptr){ delete static_cast<T *>(ptr); } }); }
 
-private:
-       void add_postprocessor(PostProcessor *, bool);
-
-public:
        virtual void setup_frame(Renderer &);
        virtual void finish_frame();