From f44366c8785eb7a88755cd71250a8b85289eeed2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 19 Oct 2021 12:36:53 +0300 Subject: [PATCH] Refactor the structure of sequence template files 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 | 18 ++++--- source/builders/sequencebuilder.cpp | 6 +-- source/builders/sequencetemplate.cpp | 75 ++++++++++++---------------- source/builders/sequencetemplate.h | 73 +++++++++++---------------- 4 files changed, 76 insertions(+), 96 deletions(-) diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 162a1882..8666e98b 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -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) diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index bf3fef42..5af62127 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -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; diff --git a/source/builders/sequencetemplate.cpp b/source/builders/sequencetemplate.cpp index e48750e5..a768d6bd 100644 --- a/source/builders/sequencetemplate.cpp +++ b/source/builders/sequencetemplate.cpp @@ -22,16 +22,16 @@ SequenceTemplate::~SequenceTemplate() delete p.postprocessor_template; } - -SequenceTemplate::PostProcessorRegistry &SequenceTemplate::get_postprocessor_registry() +template<> +SequenceTemplate::TemplateRegistry &SequenceTemplate::get_registry() { - static PostProcessorRegistry registry; + static TemplateRegistry registry; static bool initialized = false; if(!initialized) { - registry.register_type("ambient_occlusion"); - registry.register_type("bloom"); - registry.register_type("colorcurve"); + registry.register_type("ambient_occlusion"); + registry.register_type("bloom"); + registry.register_type("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(t, &c) + CollectionObjectLoader(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 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(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(name); } -void SequenceTemplate::Step::Loader::object(const string &name) -{ - obj.default_renderable = &get_collection().get(name); -} - -void SequenceTemplate::Step::Loader::scene(const string &name) -{ - obj.default_renderable = &get_collection().get(name); -} - void SequenceTemplate::Step::Loader::stencil_test() { load_sub(obj.stencil_test); diff --git a/source/builders/sequencetemplate.h b/source/builders/sequencetemplate.h index acc852d4..6b943dd1 100644 --- a/source/builders/sequencetemplate.h +++ b/source/builders/sequencetemplate.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "depthtest.h" #include "postprocessor.h" @@ -19,47 +20,42 @@ class Renderable; class SequenceTemplate { private: - class PostProcLoader: virtual public DataFile::Loader + template + class TemplateLoader: public DataFile::DynamicObjectLoader { - private: - template - struct AddPostProc - { - void operator()(const std::string &kw, PostProcLoader &ldr) const { ldr.add(kw, &PostProcLoader::postprocessor); } - }; - - protected: - RefPtr postproc; + friend class SequenceTemplate; public: - PostProcLoader(); - - PostProcessor::Template *get_postprocessor_template() { return postproc.release(); } - - protected: - virtual void postprocessor_loaded() { } + TemplateLoader(typename DataFile::DynamicObjectLoader::Collection &c): DataFile::DynamicObjectLoader(&c) { } private: - template - void postprocessor(); - - friend class SequenceTemplate; + virtual typename DataFile::DynamicObjectLoader::TypeRegistry &get_type_registry() const { return get_registry(); } }; + template + using TemplateRegistry = typename TemplateLoader::TypeRegistry; + public: - class Loader: public DataFile::CollectionObjectLoader, public PostProcLoader + class Loader: public DataFile::CollectionObjectLoader { 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 PostProcessorRegistry; - bool hdr = false; bool alpha = false; unsigned required_multisample = 0; unsigned max_multisample = 0; + std::vector renderables; std::vector steps; std::vector 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 &get_renderables() const { return renderables; } const std::vector &get_steps() const { return steps; } const std::vector &get_postprocessors() const { return postprocessors; } bool is_clear_enabled() const { return clear_enabled; } @@ -141,26 +134,16 @@ public: template static void register_postprocessor(const std::string &); + private: - static PostProcessorRegistry &get_postprocessor_registry(); + template + static TemplateRegistry &get_registry(); }; template void SequenceTemplate::register_postprocessor(const std::string &kw) { - get_postprocessor_registry().register_type(kw); -} - -template -void SequenceTemplate::PostProcLoader::postprocessor() -{ - if(postproc) - throw std::logic_error("Only one postprocessor allowed per slot"); - RefPtr pp = new typename T::Template; - load_sub(*pp); - postproc = pp; - pp = 0; - postprocessor_loaded(); + get_registry().register_type(kw); } } // namespace GL -- 2.45.2