From: Mikko Rasa Date: Tue, 12 Dec 2023 11:15:58 +0000 (+0200) Subject: Convert sub-object loading to use the new API in mspdatafile X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=87815f0f7907a4a908ec7e7a63b5f8a568853188;p=libs%2Fgl.git Convert sub-object loading to use the new API in mspdatafile --- diff --git a/source/animation/animation.cpp b/source/animation/animation.cpp index 0a908583..02a0c657 100644 --- a/source/animation/animation.cpp +++ b/source/animation/animation.cpp @@ -532,15 +532,11 @@ void Animation::Loader::load_kf(const string &n, bool c) void Animation::Loader::load_kf_inline(bool c) { - unique_ptr kf = make_unique(); + unique_ptr kf; if(coll) - { - KeyFrame::Loader ldr(*kf, get_collection()); - ldr.set_inline_base_name(format("%s: %d.kframe", FS::basename(get_source()), obj.keyframes.size())); - load_sub_with(ldr); - } + kf = make_sub().name(format("%d.kframe", obj.keyframes.size())).load(get_collection()); else - load_sub(*kf); + kf = make_sub().load(); add_kf(kf.get(), c); obj.owned_keyframes.emplace_back(move(kf)); diff --git a/source/animation/armature.cpp b/source/animation/armature.cpp index 5fb3403a..8de888b3 100644 --- a/source/animation/armature.cpp +++ b/source/animation/armature.cpp @@ -62,7 +62,7 @@ Armature::Loader::Loader(Armature &a): void Armature::Loader::link(const string &n) { Link lnk(n, obj.links.size()); - load_sub(lnk, obj); + sub(lnk).load(obj); obj.links.push_back(lnk); } diff --git a/source/animation/keyframe.cpp b/source/animation/keyframe.cpp index d6466505..e0d8d0d8 100644 --- a/source/animation/keyframe.cpp +++ b/source/animation/keyframe.cpp @@ -55,11 +55,6 @@ KeyFrame::Loader::Loader(KeyFrame &k, Collection *c): add("scaling", &Loader::scaling); } -void KeyFrame::Loader::set_inline_base_name(const string &n) -{ - inline_base_name = n; -} - void KeyFrame::Loader::pose(const string &n) { obj.pose = &get_collection().get(n); @@ -67,11 +62,7 @@ void KeyFrame::Loader::pose(const string &n) void KeyFrame::Loader::pose_inline() { - unique_ptr p = make_unique(); - load_sub(*p, get_collection()); - Pose *ptr = p.get(); - get_collection().add((inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name)+".pose", move(p)); - obj.pose = ptr; + obj.pose = make_sub().into(get_collection(), "pose").load(); } void KeyFrame::Loader::position(float x, float y, float z) @@ -96,7 +87,7 @@ void KeyFrame::Loader::scaling(float x, float y, float z) void KeyFrame::Loader::transform() { - load_sub(obj.transform); + sub(obj.transform).load(); } void KeyFrame::Loader::uniforms() diff --git a/source/animation/keyframe.h b/source/animation/keyframe.h index 2abe7167..78b145c1 100644 --- a/source/animation/keyframe.h +++ b/source/animation/keyframe.h @@ -19,19 +19,12 @@ class MSPGL_API KeyFrame public: class MSPGL_API Loader: public DataFile::CollectionObjectLoader { - private: - std::string inline_base_name; - public: Loader(KeyFrame &k): Loader(k, nullptr) { } Loader(KeyFrame &k, Collection &c): Loader(k, &c) { } private: Loader(KeyFrame &, Collection *); - public: - void set_inline_base_name(const std::string &); - - private: void pose(const std::string &); void pose_inline(); void position(float, float, float); diff --git a/source/builders/font.cpp b/source/builders/font.cpp index 89a70cb3..7f0335d8 100644 --- a/source/builders/font.cpp +++ b/source/builders/font.cpp @@ -136,7 +136,7 @@ void Font::Loader::glyph(unsigned c) { Glyph gl; gl.code = c; - load_sub(gl); + sub(gl).load(); obj.glyphs.insert(GlyphMap::value_type(c, gl)); } @@ -152,11 +152,7 @@ void Font::Loader::ligature(unsigned l, unsigned r, unsigned g) void Font::Loader::texture() { - unique_ptr tex = make_unique(); - load_sub(*tex); - Texture2D *ptr = tex.get(); - get_collection().add(FS::basename(get_source())+".tex", move(tex)); - obj.texture = ptr; + obj.texture = make_sub().into(get_collection(), "texture").load(); } void Font::Loader::texture_ref(const string &name) diff --git a/source/builders/sequencetemplate.cpp b/source/builders/sequencetemplate.cpp index 80c8039c..ec24ccad 100644 --- a/source/builders/sequencetemplate.cpp +++ b/source/builders/sequencetemplate.cpp @@ -82,11 +82,9 @@ void SequenceTemplate::Loader::clear() void SequenceTemplate::Loader::effect(const string &slot) { - TemplateLoader ldr(get_collection()); - load_sub_with(ldr); Renderable rend; rend.slot_name = slot; - rend.effect_template = ldr.get_object(); + rend.effect_template = dyn_sub>().load(get_collection()); obj.renderables.emplace_back(move(rend)); } @@ -109,10 +107,8 @@ void SequenceTemplate::Loader::postprocessor() void SequenceTemplate::Loader::postprocessor_with_slot(const string &slot) { - TemplateLoader ldr(get_collection()); - load_sub_with(ldr); PostProcessor pp; - pp.postprocessor_template = ldr.get_object(); + pp.postprocessor_template = dyn_sub>().load(get_collection()); pp.slot_name = slot; obj.postprocessors.emplace_back(move(pp)); } @@ -147,9 +143,7 @@ void SequenceTemplate::Loader::step(const string &tag, const string &rend) Step stp; stp.tag = tag; stp.renderable_name = rend; - Step::Loader ldr(stp, get_collection()); - ldr.set_inline_base_name(format("%s: step[%d]", get_source(), obj.steps.size())); - load_sub_with(ldr); + sub(stp).name(format("step_%d", obj.steps.size())).load(get_collection()); obj.steps.push_back(stp); } @@ -229,7 +223,7 @@ void SequenceTemplate::Step::Loader::set_inline_base_name(const string &n) void SequenceTemplate::Step::Loader::depth_test() { - load_sub(obj.depth_test); + sub(obj.depth_test).load(); } void SequenceTemplate::Step::Loader::depth_compare(Predicate c) @@ -239,11 +233,7 @@ void SequenceTemplate::Step::Loader::depth_compare(Predicate c) void SequenceTemplate::Step::Loader::lighting_inline() { - unique_ptr lightn = make_unique(); - load_sub(*lightn, get_collection()); - Lighting *ptr = lightn.get(); - get_collection().add(inline_base_name+".lightn", move(lightn)); - obj.lighting = ptr; + obj.lighting = make_sub().into(get_collection(), "lighting").load(); } void SequenceTemplate::Step::Loader::lighting(const string &name) @@ -253,7 +243,7 @@ void SequenceTemplate::Step::Loader::lighting(const string &name) void SequenceTemplate::Step::Loader::stencil_test() { - load_sub(obj.stencil_test); + sub(obj.stencil_test).load(); } } // namespace GL diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index 2f1fcd88..6e3a2b93 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -260,7 +260,7 @@ void Mesh::Loader::storage(const vector &attrs) void Mesh::Loader::vertices() { - load_sub(obj.vertices); + sub(obj.vertices).load(); if(allow_gl_calls) obj.check_buffers(VERTEX_BUFFER); } @@ -274,7 +274,7 @@ void Mesh::Loader::vertices_with_format(const vector &a) void Mesh::Loader::batch(PrimitiveType p) { Batch btc(p); - load_sub(btc); + sub(btc).load(); obj.add_batch(move(btc)); } diff --git a/source/core/vertexarray.h b/source/core/vertexarray.h index 1dd312a2..94a74e99 100644 --- a/source/core/vertexarray.h +++ b/source/core/vertexarray.h @@ -30,6 +30,8 @@ public: class MSPGL_API Loader: public DataFile::Loader, public VertexArrayBuilder { public: + using Object = VertexArray; + Loader(VertexArray &); }; diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index e8289f03..c0c308c2 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -330,7 +330,7 @@ void ShadowMap::Template::Loader::light(const string &name) { ShadowedLight light; light.light = &get_collection().get(name); - load_sub(light); + sub(light).load(); obj.lights.push_back(light); } diff --git a/source/materials/lighting.cpp b/source/materials/lighting.cpp index 016ee92e..2984f57c 100644 --- a/source/materials/lighting.cpp +++ b/source/materials/lighting.cpp @@ -143,10 +143,7 @@ void Lighting::Loader::light(const string &name) void Lighting::Loader::light_inline() { - Light::GenericLoader ldr(get_collection()); - load_sub_with(ldr); - Light *lgt = ldr.store_object(get_collection(), format("%s/%d.light", FS::basename(get_source()), obj.lights.size())); - obj.attach(*lgt); + obj.attach(*dyn_sub().into(get_collection(), format("%d.light", obj.lights.size())).load()); } } // namespace GL diff --git a/source/materials/rendermethod.cpp b/source/materials/rendermethod.cpp index 789fa0d5..de3656f8 100644 --- a/source/materials/rendermethod.cpp +++ b/source/materials/rendermethod.cpp @@ -180,11 +180,6 @@ void RenderMethod::Loader::init_actions() add("uniform_slot", &Loader::uniform_slot2); } -void RenderMethod::Loader::set_inline_base_name(const string &n) -{ - inline_base_name = n; -} - void RenderMethod::Loader::finish() { if(obj.material) @@ -193,7 +188,7 @@ void RenderMethod::Loader::finish() void RenderMethod::Loader::blend() { - load_sub(obj.blend); + sub(obj.blend).load(); } void RenderMethod::Loader::blend_factors(BlendFactor src, BlendFactor dest) @@ -203,9 +198,7 @@ void RenderMethod::Loader::blend_factors(BlendFactor src, BlendFactor dest) void RenderMethod::Loader::material_inline() { - Material::GenericLoader ldr(get_collection()); - load_sub_with(ldr); - obj.material = ldr.store_object(get_collection(), inline_base_name+".mat"); + obj.material = dyn_sub().into(get_collection(), "material.mat").load(); obj.set_material_textures(); } @@ -235,8 +228,7 @@ void RenderMethod::Loader::texture(const string &n) obj.textures.emplace_back(n); i = obj.textures.end()-1; } - TextureSlot::Loader ldr(*i, n, coll); - load_sub_with(ldr); + sub(*i).load(n, coll); } void RenderMethod::Loader::uniforms() @@ -251,7 +243,7 @@ void RenderMethod::Loader::uniforms() obj.shdata = make_shared(); obj.shdata->copy_uniforms(*old_shdata); } - load_sub(*obj.shdata); + sub(*obj.shdata).load(); } void RenderMethod::Loader::uniform_slot(const string &name) diff --git a/source/materials/rendermethod.h b/source/materials/rendermethod.h index f41a0b42..340a8620 100644 --- a/source/materials/rendermethod.h +++ b/source/materials/rendermethod.h @@ -30,8 +30,6 @@ public: class MSPGL_API Loader: public DataFile::CollectionObjectLoader { private: - std::string inline_base_name; - static ActionMap shared_actions; public: @@ -39,10 +37,6 @@ public: private: void init_actions() override; - public: - void set_inline_base_name(const std::string &); - - private: void finish() override; void blend(); diff --git a/source/materials/splatmaterial.cpp b/source/materials/splatmaterial.cpp index a86d3092..6a320284 100644 --- a/source/materials/splatmaterial.cpp +++ b/source/materials/splatmaterial.cpp @@ -192,7 +192,7 @@ void SplatMaterial::Loader::init_actions() add("normal_storage", &Loader::map_storage, &SplatMaterial::normal_array); add("occlusion_storage", &Loader::map_storage, &SplatMaterial::occlusion_array); add("roughness_storage", &Loader::map_storage, &SplatMaterial::roughness_array); - add("sub", &SplatMaterial::Loader::sub); + add("sub", &SplatMaterial::Loader::sub_mat); } void SplatMaterial::Loader::finish() @@ -236,11 +236,11 @@ void SplatMaterial::Loader::map_storage(MapArray SplatMaterial::*array, PixelFor (obj.*array).height = h; } -void SplatMaterial::Loader::sub() +void SplatMaterial::Loader::sub_mat() { SubMaterial sm; - load_sub(sm); - obj.sub_materials.push_back(sm); + sub(sm).load(); + obj.sub_materials.emplace_back(move(sm)); if(!sm.base_color_map.source_fn.empty()) ++obj.base_color_array.max_layers; diff --git a/source/materials/splatmaterial.h b/source/materials/splatmaterial.h index 9928440b..8d6eb973 100644 --- a/source/materials/splatmaterial.h +++ b/source/materials/splatmaterial.h @@ -29,7 +29,7 @@ public: void finish() override; void map_storage(MapArray SplatMaterial::*, PixelFormat, unsigned, unsigned); - void sub(); + void sub_mat(); }; private: diff --git a/source/materials/technique.cpp b/source/materials/technique.cpp index 4418b1df..ada0e8bb 100644 --- a/source/materials/technique.cpp +++ b/source/materials/technique.cpp @@ -123,11 +123,6 @@ void Technique::Loader::init_actions() add("method", &Loader::method); } -void Technique::Loader::set_inline_base_name(const string &n) -{ - inline_base_name = n; -} - void Technique::Loader::inherit(const string &n) { obj.methods = get_collection().get(n).get_methods(); @@ -138,10 +133,7 @@ void Technique::Loader::inherit(const string &n) void Technique::Loader::method(const string &n) { RenderMethod p; - RenderMethod::Loader ldr(p, get_collection()); - ldr.set_inline_base_name(format("%s: method[%s]", - (inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name), (n.empty() ? "\"\"" : n.c_str()))); - load_sub_with(ldr); + sub(p).name(format("method[%s]", (n.empty() ? "\"\"" : n.c_str()))).load(get_collection()); if(!p.get_shader_program()) throw logic_error("no shader program in method"); @@ -185,7 +177,7 @@ void Technique::InheritLoader::texture(const string &slot, const string &name) void Technique::InheritLoader::uniforms() { ProgramData shdata; - load_sub(shdata); + sub(shdata).load(); obj.replace_uniforms(shdata); } diff --git a/source/materials/technique.h b/source/materials/technique.h index 8656770e..92ee0935 100644 --- a/source/materials/technique.h +++ b/source/materials/technique.h @@ -21,8 +21,6 @@ public: class MSPGL_API Loader: public Msp::DataFile::CollectionObjectLoader { private: - std::string inline_base_name; - static ActionMap shared_actions; public: @@ -30,10 +28,6 @@ public: private: void init_actions() override; - public: - void set_inline_base_name(const std::string &); - - private: void inherit(const std::string &); void method(const std::string &); }; diff --git a/source/render/instancearray.h b/source/render/instancearray.h index c622f6e7..2afecd04 100644 --- a/source/render/instancearray.h +++ b/source/render/instancearray.h @@ -185,8 +185,7 @@ inline void InstanceArray::Instance::set_matrix(const Matrix &m) template void InstanceArray::Loader::instance() { - T &inst = this->obj.append(); - this->load_sub(inst); + this->sub(this->obj.append()).load(); } } // namespace GL diff --git a/source/render/object.cpp b/source/render/object.cpp index 9a75ff55..0888aa9f 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -249,11 +249,7 @@ void Object::LodLoader::mesh(const string &n) void Object::LodLoader::mesh_inline() { - unique_ptr msh = make_unique(); - load_sub(*msh); - Mesh *ptr = msh.get(); - get_collection().add(format("%s/lod%d.mesh", FS::basename(get_source()), index), move(msh)); - lod.mesh = ptr; + lod.mesh = make_sub().into(get_collection(), format("lod%d.mesh", index)).load(); } void Object::LodLoader::technique(const string &n) @@ -263,14 +259,7 @@ void Object::LodLoader::technique(const string &n) void Object::LodLoader::technique_inline() { - unique_ptr tech = make_unique(); - 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()); - Technique *ptr = tech.get(); - get_collection().add(name, move(tech)); - lod.technique = ptr; + lod.technique = make_sub().into(get_collection(), format("lod%d.tech", index)).load(); } } // namespace GL diff --git a/source/render/objectinstance.cpp b/source/render/objectinstance.cpp index a41f4e29..66f24185 100644 --- a/source/render/objectinstance.cpp +++ b/source/render/objectinstance.cpp @@ -36,7 +36,7 @@ ObjectInstance::Loader::Loader(ObjectInstance &o): void ObjectInstance::Loader::transform() { Transform trn; - load_sub(trn); + sub(trn).load(); obj.set_matrix(trn.to_matrix()); } diff --git a/source/render/scene.cpp b/source/render/scene.cpp index fd3ce9ba..4d8732aa 100644 --- a/source/render/scene.cpp +++ b/source/render/scene.cpp @@ -32,8 +32,6 @@ Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry() } -unsigned Scene::Loader::inline_counter = 0; - Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m): DataFile::CollectionObjectLoader(s, &c), content(m) @@ -47,11 +45,9 @@ Scene::Loader::Loader(Scene &s, Collection &c, ContentMap *m): void Scene::Loader::array(const string &n) { - unique_ptr> arr = make_unique>(get_collection().get(n)); - load_sub(*arr); - InstanceArray<> *ptr = arr.get(); - get_collection().add(format("_scene_array_%d.array", ++inline_counter), move(arr)); - obj.add(*ptr); + obj.add(*make_sub>(get_collection().get(n)) + .into(get_collection(), format("array %d", ++inline_counter)) + .load()); } void Scene::Loader::object(const string &n) @@ -61,13 +57,12 @@ void Scene::Loader::object(const string &n) void Scene::Loader::object_tagged(const string &n, const string &t) { - unique_ptr inst = make_unique(get_collection().get(n)); - load_sub(*inst); - ObjectInstance *ptr = inst.get(); - get_collection().add(format("_scene_object_%d.inst", ++inline_counter), move(inst)); + ObjectInstance *inst = make_sub(get_collection().get(n)) + .into(get_collection(), format("object %d", ++inline_counter)) + .load(); if(content && !t.empty()) - (*content)[t] = ptr; - obj.add(*ptr); + (*content)[t] = inst; + obj.add(*inst); } void Scene::Loader::scene(const string &n) @@ -77,10 +72,7 @@ void Scene::Loader::scene(const string &n) void Scene::Loader::scene_inline() { - GenericLoader ldr(get_collection()); - load_sub_with(ldr); - Scene *scene = ldr.store_object(get_collection(), format("_scene_%d.scene", ++inline_counter)); - obj.add(*scene); + obj.add(*dyn_sub().into(get_collection(), format("scene %d", ++inline_counter)).load()); } } // namespace GL diff --git a/source/render/scene.h b/source/render/scene.h index 353aa048..8dfd50d5 100644 --- a/source/render/scene.h +++ b/source/render/scene.h @@ -32,8 +32,7 @@ protected: private: ContentMap *content; - - static unsigned inline_counter; + unsigned inline_counter = 0; public: Loader(Scene &s, Collection &c): Loader(s, c, nullptr) { } diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index fd6c306b..2c38eac6 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -292,9 +292,7 @@ Resources::Loader::Loader(Resources &r): template void Resources::Loader::generic(const string &name) { - L ldr(obj); - load_sub_with(ldr); - ldr.store_object(obj, name); + dyn_sub().into(obj, name).load(); }