]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor the structure of sequence template files
authorMikko Rasa <tdb@tdb.fi>
Tue, 19 Oct 2021 09:36:53 +0000 (12:36 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 19 Oct 2021 11:52:34 +0000 (14:52 +0300)
Renderables are now specified separately from steps.  Post-processors are
defined with a unified keyword, with the type determined by a statement
inside the block.

blender/io_mspgl/export_scene.py
source/builders/sequencebuilder.cpp
source/builders/sequencetemplate.cpp
source/builders/sequencetemplate.h

index 162a1882b5e8d135168600da6e0bd2550fd00f21..8666e98beee0ef9489557d28da89fdcf788ee036 100644 (file)
@@ -128,6 +128,8 @@ class SceneExporter:
                seq_res.statements.append(ss)
 
                scene_res = resources[scene.name+".scene"]
+               seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
+
                lighting_res = resources[scene.name+".lightn"]
 
                any_opaque = False
@@ -144,31 +146,35 @@ class SceneExporter:
                        ss = Statement("step", "", "content")
                        ss.sub.append(Statement("depth_test", Token("LEQUAL")))
                        ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
-                       ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
                        seq_res.statements.append(ss)
 
                if any_blended:
                        ss = Statement("step", "blended", "content")
                        ss.sub.append(Statement("depth_test", Token("LEQUAL")))
                        ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
-                       ss.sub.append(seq_res.create_reference_statement("scene", scene_res))
                        seq_res.statements.append(ss)
 
                if scene.use_ao:
-                       ss = Statement("ambient_occlusion")
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("ambient_occlusion")))
                        ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
                        ss.sub.append(Statement("samples", scene.ao_samples))
                        seq_res.statements.append(ss)
 
                if scene.use_hdr:
-                       seq_res.statements.append(Statement("bloom"))
-                       ss = Statement("colorcurve")
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("bloom")))
+                       seq_res.statements.append(ss)
+
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("colorcurve")))
                        ss.sub.append(Statement("exposure_adjust", scene.exposure))
                        ss.sub.append(Statement("srgb"))
                        seq_res.statements.append(ss)
                else:
                        # Add a colorcurve with linear response to convert into sRGB color space
-                       ss = Statement("colorcurve")
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("colorcurve")))
                        ss.sub.append(Statement("brightness_response", 1.0))
                        ss.sub.append(Statement("srgb"))
                        seq_res.statements.append(ss)
index bf3fef42dfb82f55989a21f8c325139ee569ba7d..5af62127ca4ff02712390793e70ec2735a401e7c 100644 (file)
@@ -16,8 +16,8 @@ namespace GL {
 SequenceBuilder::SequenceBuilder(const SequenceTemplate &t):
        tmpl(t)
 {
-       for(const SequenceTemplate::Step &s: tmpl.get_steps())
-               renderables[s.slot_name] = s.default_renderable;
+       for(const SequenceTemplate::Renderable &r: tmpl.get_renderables())
+               renderables[r.slot_name] = r.renderable;
        for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
                if(!p.slot_name.empty())
                        postprocessors[p.slot_name] = 0;
@@ -59,7 +59,7 @@ void SequenceBuilder::build(Sequence &sequence) const
 
        for(const SequenceTemplate::Step &s: tmpl.get_steps())
        {
-               Renderable *renderable = get_item(renderables, s.slot_name);
+               Renderable *renderable = get_item(renderables, s.renderable_name);
                if(!renderable)
                        continue;
 
index e48750e5f5df9299b25590658e1a6b2d3eae3603..a768d6bd8bc39ddca833735d7c248198631c9c98 100644 (file)
@@ -22,16 +22,16 @@ SequenceTemplate::~SequenceTemplate()
                delete p.postprocessor_template;
 }
 
-
-SequenceTemplate::PostProcessorRegistry &SequenceTemplate::get_postprocessor_registry()
+template<>
+SequenceTemplate::TemplateRegistry<GL::PostProcessor> &SequenceTemplate::get_registry<GL::PostProcessor>()
 {
-       static PostProcessorRegistry registry;
+       static TemplateRegistry<GL::PostProcessor> registry;
        static bool initialized = false;
        if(!initialized)
        {
-               registry.register_type<AmbientOcclusion>("ambient_occlusion");
-               registry.register_type<Bloom>("bloom");
-               registry.register_type<ColorCurve>("colorcurve");
+               registry.register_type<AmbientOcclusion::Template>("ambient_occlusion");
+               registry.register_type<Bloom::Template>("bloom");
+               registry.register_type<ColorCurve::Template>("colorcurve");
                initialized = true;
        }
        return registry;
@@ -43,14 +43,8 @@ SequenceTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt)
 { }
 
 
-SequenceTemplate::PostProcLoader::PostProcLoader()
-{
-       get_postprocessor_registry().invoke_all(*this);
-}
-
-
 SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
-       DataFile::CollectionObjectLoader<SequenceTemplate, Resources>(t, &c)
+       CollectionObjectLoader<SequenceTemplate>(t, &c)
 {
        add("hdr", &SequenceTemplate::hdr);
        add("alpha", &SequenceTemplate::alpha);
@@ -58,16 +52,10 @@ SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
        add("multisample", &Loader::multisample);
        add("multisample", &Loader::multisample_range);
        add("postprocessor", &Loader::postprocessor);
+       add("postprocessor", &Loader::postprocessor_with_slot);
+       add("renderable", &Loader::renderable);
+       add("renderable", &Loader::renderable_with_default);
        add("step", &Loader::step);
-       add("step", &Loader::step_with_slot);
-
-       // Deprecated
-       add("pass", &Loader::step_with_slot);
-}
-
-void SequenceTemplate::Loader::postprocessor_loaded()
-{
-       obj.postprocessors.push_back(get_postprocessor_template());
 }
 
 void SequenceTemplate::Loader::clear()
@@ -89,27 +77,42 @@ void SequenceTemplate::Loader::multisample_range(unsigned req, unsigned max)
        obj.max_multisample = max;
 }
 
-void SequenceTemplate::Loader::postprocessor(const string &slot)
+void SequenceTemplate::Loader::postprocessor()
 {
-       PostProcLoader ldr;
+       postprocessor_with_slot(string());
+}
+
+void SequenceTemplate::Loader::postprocessor_with_slot(const string &slot)
+{
+       TemplateLoader<GL::PostProcessor> ldr(get_collection());
        load_sub_with(ldr);
        PostProcessor pp;
-       pp.postprocessor_template = ldr.get_postprocessor_template();
+       pp.postprocessor_template = ldr.get_object();
        pp.slot_name = slot;
        obj.postprocessors.push_back(pp);
 }
 
-void SequenceTemplate::Loader::step(const string &tag)
+void SequenceTemplate::Loader::renderable(const string &slot)
 {
-       step_with_slot(tag, string());
+       Renderable rend;
+       rend.slot_name = slot;
+       obj.renderables.push_back(rend);
 }
 
-void SequenceTemplate::Loader::step_with_slot(const string &tag, const string &rend)
+void SequenceTemplate::Loader::renderable_with_default(const string &slot, const string &name)
+{
+       Renderable rend;
+       rend.renderable = &get_collection().get<GL::Renderable>(name);
+       rend.slot_name = slot;
+       obj.renderables.push_back(rend);
+}
+
+void SequenceTemplate::Loader::step(const string &tag, const string &rend)
 {
        Step stp;
        stp.tag = tag;
-       stp.slot_name = rend;
-       Step::Loader ldr(stp, *coll);
+       stp.renderable_name = rend;
+       Step::Loader ldr(stp, get_collection());
        ldr.set_inline_base_name(format("%s/%d.step", get_source(), obj.steps.size()));
        load_sub_with(ldr);
 
@@ -148,8 +151,6 @@ SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c):
        add("depth_test", &Loader::depth_compare);
        add("lighting", &Loader::lighting);
        add("lighting", &Loader::lighting_inline);
-       add("object", &Loader::object);
-       add("scene", &Loader::scene);
        add("stencil_test", &Loader::stencil_test);
 }
 
@@ -181,16 +182,6 @@ void SequenceTemplate::Step::Loader::lighting(const string &name)
        obj.lighting = &get_collection().get<Lighting>(name);
 }
 
-void SequenceTemplate::Step::Loader::object(const string &name)
-{
-       obj.default_renderable = &get_collection().get<GL::Object>(name);
-}
-
-void SequenceTemplate::Step::Loader::scene(const string &name)
-{
-       obj.default_renderable = &get_collection().get<Scene>(name);
-}
-
 void SequenceTemplate::Step::Loader::stencil_test()
 {
        load_sub(obj.stencil_test);
index acc852d4fa0ef0113e4a79a6ab6f46ffbead7087..6b943dd1499f962bea05968643a936d01e16a8f9 100644 (file)
@@ -4,6 +4,7 @@
 #include <string>
 #include <vector>
 #include <msp/core/typeregistry.h>
+#include <msp/datafile/dynamicobjectloader.h>
 #include <msp/datafile/objectloader.h>
 #include "depthtest.h"
 #include "postprocessor.h"
@@ -19,47 +20,42 @@ class Renderable;
 class SequenceTemplate
 {
 private:
-       class PostProcLoader: virtual public DataFile::Loader
+       template<typename T>
+       class TemplateLoader: public DataFile::DynamicObjectLoader<typename T::Template>
        {
-       private:
-               template<typename T>
-               struct AddPostProc
-               {
-                       void operator()(const std::string &kw, PostProcLoader &ldr) const { ldr.add(kw, &PostProcLoader::postprocessor<T>); }
-               };
-
-       protected:
-               RefPtr<PostProcessor::Template> postproc;
+               friend class SequenceTemplate;
 
        public:
-               PostProcLoader();
-
-               PostProcessor::Template *get_postprocessor_template() { return postproc.release(); }
-
-       protected:
-               virtual void postprocessor_loaded() { }
+               TemplateLoader(typename DataFile::DynamicObjectLoader<typename T::Template>::Collection &c): DataFile::DynamicObjectLoader<typename T::Template>(&c) { }
 
        private:
-               template<typename T>
-               void postprocessor();
-
-               friend class SequenceTemplate;
+               virtual typename DataFile::DynamicObjectLoader<typename T::Template>::TypeRegistry &get_type_registry() const { return get_registry<T>(); }
        };
 
+       template<typename T>
+       using TemplateRegistry = typename TemplateLoader<T>::TypeRegistry;
+
 public:
-       class Loader: public DataFile::CollectionObjectLoader<SequenceTemplate, Resources>, public PostProcLoader
+       class Loader: public DataFile::CollectionObjectLoader<SequenceTemplate>
        {
        public:
                Loader(SequenceTemplate &, Collection &);
 
        private:
-               virtual void postprocessor_loaded();
                void clear();
                void multisample(unsigned);
                void multisample_range(unsigned, unsigned);
-               void postprocessor(const std::string &);
-               void step(const std::string &);
-               void step_with_slot(const std::string &, const std::string &);
+               void postprocessor();
+               void postprocessor_with_slot(const std::string &);
+               void renderable(const std::string &);
+               void renderable_with_default(const std::string &, const std::string &);
+               void step(const std::string &, const std::string &);
+       };
+
+       struct Renderable
+       {
+               GL::Renderable *renderable = 0;
+               std::string slot_name;
        };
 
        struct Step
@@ -79,8 +75,6 @@ public:
                        void depth_compare(Predicate);
                        void lighting(const std::string &);
                        void lighting_inline();
-                       void object(const std::string &);
-                       void scene(const std::string &);
                        void stencil_test();
                };
 
@@ -88,8 +82,7 @@ public:
                const Lighting *lighting = 0;
                DepthTest depth_test;
                StencilTest stencil_test;
-               std::string slot_name;
-               Renderable *default_renderable = 0;
+               std::string renderable_name;
        };
 
        struct PostProcessor
@@ -112,12 +105,11 @@ private:
                void stencil(int);
        };
 
-       typedef TypeRegistry<PostProcLoader::AddPostProc, PostProcLoader &> PostProcessorRegistry;
-
        bool hdr = false;
        bool alpha = false;
        unsigned required_multisample = 0;
        unsigned max_multisample = 0;
+       std::vector<Renderable> renderables;
        std::vector<Step> steps;
        std::vector<PostProcessor> postprocessors;
        bool clear_enabled = false;
@@ -132,6 +124,7 @@ public:
        bool get_alpha() const { return alpha; }
        unsigned get_required_multisample() const { return required_multisample; }
        unsigned get_maximum_multisample() const { return max_multisample; }
+       const std::vector<Renderable> &get_renderables() const { return renderables; }
        const std::vector<Step> &get_steps() const { return steps; }
        const std::vector<PostProcessor> &get_postprocessors() const { return postprocessors; }
        bool is_clear_enabled() const { return clear_enabled; }
@@ -141,26 +134,16 @@ public:
 
        template<typename T>
        static void register_postprocessor(const std::string &);
+
 private:
-       static PostProcessorRegistry &get_postprocessor_registry();
+       template<typename T>
+       static TemplateRegistry<T> &get_registry();
 };
 
 template<typename T>
 void SequenceTemplate::register_postprocessor(const std::string &kw)
 {
-       get_postprocessor_registry().register_type<T>(kw);
-}
-
-template<typename T>
-void SequenceTemplate::PostProcLoader::postprocessor()
-{
-       if(postproc)
-               throw std::logic_error("Only one postprocessor allowed per slot");
-       RefPtr<typename T::Template> pp = new typename T::Template;
-       load_sub(*pp);
-       postproc = pp;
-       pp = 0;
-       postprocessor_loaded();
+       get_registry<GL::PostProcessor>().register_type<typename T::Template>(kw);
 }
 
 } // namespace GL