From: Mikko Rasa Date: Sun, 17 Oct 2021 08:26:13 +0000 (+0300) Subject: Refactor owned postprocessors in Sequence into a more generic mechanism X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0bd265b929f0b2a9289dd72262968ad8e7bc1a0d;p=libs%2Fgl.git Refactor owned postprocessors in Sequence into a more generic mechanism --- diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index 5fca6140..bf3fef42 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -74,7 +74,7 @@ void SequenceBuilder::build(Sequence &sequence) const #endif for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors()) { - PostProcessor *proc = 0; + RefPtr 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()); } } } diff --git a/source/render/sequence.cpp b/source/render/sequence.cpp index 4ccf5408..5d003753 100644 --- a/source/render/sequence.cpp +++ b/source/render/sequence.cpp @@ -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+1get_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); } } } diff --git a/source/render/sequence.h b/source/render/sequence.h index 13431ac4..f51d28b9 100644 --- a/source/render/sequence.h +++ b/source/render/sequence.h @@ -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 steps; - std::vector postproc; + std::vector postproc; unsigned width = 0; unsigned height = 0; FrameFormat target_format; @@ -75,6 +75,7 @@ private: std::vector clear_colors; float clear_depth = 1.0f; int clear_stencil = 0; + std::vector 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 + void add_owned(T *p) + { owned_data.push_back({ p, [](void *ptr){ delete static_cast(ptr); } }); } -private: - void add_postprocessor(PostProcessor *, bool); - -public: virtual void setup_frame(Renderer &); virtual void finish_frame();