X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresources%2Fresources.cpp;h=ab4b0a8dd52d560282ef7ca11d1afcd41c1c6cda;hb=HEAD;hp=80a19e7c9bdd6aa3ed30f037930613defc39182b;hpb=23d4100160bfa33359ce297b6b36244abcaa5f82;p=libs%2Fgl.git diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 80a19e7c..ab4b0a8d 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -2,28 +2,35 @@ #include #include "animation.h" #include "armature.h" +#include "backend.h" +#include "basicmaterial.h" #include "camera.h" +#include "directionallight.h" #include "error.h" #include "font.h" #include "keyframe.h" -#include "light.h" #include "lighting.h" -#include "material.h" #include "mesh.h" #include "module.h" #include "object.h" +#include "occludedscene.h" +#include "orderedscene.h" +#include "pbrmaterial.h" #include "sequencetemplate.h" +#include "pointlight.h" #include "pose.h" #include "program.h" #include "resourcemanager.h" #include "resources.h" #include "sampler.h" -#include "scene.h" +#include "simplescene.h" #include "technique.h" #include "texture1d.h" #include "texture2d.h" #include "texture2darray.h" #include "texturecube.h" +#include "unlitmaterial.h" +#include "zsortedscene.h" #include "glsl/compiler.h" using namespace std; @@ -37,45 +44,68 @@ void init_builtin_data(DataFile::BuiltinSource &); Resources *Resources::global_resources = 0; Resources::Resources(bool set_as_global): - srgb_conversion(false), resource_manager(0) { add_type().suffix(".anim").keyword("animation"); add_type().suffix(".arma").keyword("armature"); + add_type().base().suffix(".mat") + .creator([this](const string &n) -> BasicMaterial * { create_generic(n); return 0; }) + .notify(&set_debug_name); add_type().keyword("camera") - .notify(&Resources::set_debug_name); + .notify(&set_debug_name); + add_type().base().suffix(".light") + .creator([this](const string &n) -> DirectionalLight * { create_generic(n); return 0; }); add_type().keyword("font"); add_type().suffix(".kframe").keyword("keyframe"); - add_type().keyword("light"); add_type().suffix(".lightn").keyword("lighting") - .notify(&Resources::set_debug_name); - add_type().suffix(".mat") - .creator(&Resources::create_generic).notify(&Resources::set_debug_name); + .notify(&set_debug_name); add_type().keyword("mesh") - .creator(&Resources::create_mesh).notify(&Resources::set_debug_name); + .creator([this](const string &n){ return create_mesh(n); }) + .notify(&set_debug_name); add_type().suffix(".glsl").suffix(".spv") - .creator(&Resources::create_module); - add_type().keyword("object"); + .creator([this](const string &n){ return create_module(n); }) + .notify(&set_debug_name); + add_type().base().keyword("object"); + add_type().base().base().suffix(".scene") + .creator([this](const string &n) -> OccludedScene * { create_generic(n); return 0; }); + add_type().base().base().suffix(".scene") + .creator([this](const string &n) -> OrderedScene * { create_generic(n); return 0; }); + add_type().base().suffix(".mat") + .creator([this](const string &n) -> PbrMaterial * { create_generic(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".light") + .creator([this](const string &n) -> PointLight * { create_generic(n); return 0; }); add_type().suffix(".seq").keyword("sequence"); add_type().keyword("pose"); add_type().keyword("shader") - .creator(&Resources::create_program).notify(&Resources::set_debug_name); + .creator([this](const string &n){ return create_program(n); }) + .notify(&set_debug_name); add_type().suffix(".samp").keyword("sampler") - .notify(&Resources::set_debug_name); - add_type().suffix(".scene") - .creator(&Resources::create_generic); + .notify(&set_debug_name); + add_type().base().base().suffix(".scene") + .creator([this](const string &n) -> SimpleScene * { create_generic(n); return 0; }); add_type().suffix(".tech").keyword("technique") - .notify(&Resources::set_debug_name); - add_type().base().suffix(".tex1d").keyword("texture1d") - .notify(&Resources::set_debug_name); - add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d") - .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name); - add_type().base().suffix(".tex3d").keyword("texture3d") - .notify(&Resources::set_debug_name); - add_type().base().suffix(".texcb").keyword("texture_cube") - .notify(&Resources::set_debug_name); - add_type().base().suffix(".tex2da").keyword("texture2d_array") - .notify(&Resources::set_debug_name); + .notify(&set_debug_name); + add_type().base().suffix(".tex") + .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".tex").suffix(".png").suffix(".jpg") + .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".tex") + .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".tex") + .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".tex") + .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; }) + .notify(&set_debug_name); + add_type().base().suffix(".mat") + .creator([this](const string &n) -> UnlitMaterial * { create_generic(n); return 0; }) + .notify(&set_debug_name); + add_type().base().base().suffix(".scene") + .creator([this](const string &n) -> ZSortedScene * { create_generic(n); return 0; }); add_source(get_builtins()); @@ -111,25 +141,20 @@ const DataFile::CollectionSource &Resources::get_builtins() return builtins; } -void Resources::set_srgb_conversion(bool c) -{ - srgb_conversion = c; -} - void Resources::set_resource_manager(ResourceManager *m) { resource_manager = m; } -template +template T *Resources::create_generic(const string &name) { if(RefPtr io = open_raw(name)) { DataFile::Parser parser(*io, name); - typename T::GenericLoader ldr(*this); + L ldr(*this); ldr.load(parser); - return ldr.get_object(); + ldr.store_object(*this, name); } return 0; @@ -137,12 +162,13 @@ T *Resources::create_generic(const string &name) Mesh *Resources::create_mesh(const string &name) { - if(!resource_manager) + if(!resource_manager || name[0]=='_') return 0; if(RefPtr io = open_raw(name)) { - RefPtr mesh = new Mesh(resource_manager); + RefPtr mesh = new Mesh; + mesh->set_manager(resource_manager); resource_manager->set_resource_location(*mesh, *this, name); return mesh.release(); } @@ -150,39 +176,40 @@ Mesh *Resources::create_mesh(const string &name) return 0; } -Texture2D *Resources::create_texture2d(const string &name) +Texture *Resources::create_texture(const string &name) { + bool managed = (resource_manager && name[0]!='_'); + string ext = FS::extpart(name); - if(ext==".tex2d" && !resource_manager) - return 0; + if(ext==".tex") + { + if(managed) + return create_generic>(name); + else + return create_generic(name); + } if(RefPtr io = open_raw(name)) { RefPtr tex; - if(ext==".tex2d") - { - tex = new Texture2D(resource_manager); - DataFile::Parser parser(*io, name); - Texture2D::Loader ldr(*tex, *this); - ldr.load(parser); - } - else - { - // Verify that the image is loadable - Graphics::Image image; - if(!resource_manager) - image.load_io(*io); + // Verify that the image is loadable + Graphics::Image image; + if(!managed) + image.load_io(*io); - tex = new Texture2D(resource_manager); + tex = new Texture2D; - if(resource_manager) - resource_manager->set_resource_location(*tex, *this, name); - else - tex->image(image); + if(managed) + { + tex->set_manager(resource_manager); + resource_manager->set_resource_location(*tex, *this, name); } + else + tex->image(image); - return tex.release(); + add(name, tex.get()); + tex.release(); } return 0; @@ -198,7 +225,11 @@ Module *Resources::create_module(const string &name) { if(ext==".glsl") { - RefPtr module = new GlslModule; + RefPtr module; + if(get_backend_api()==VULKAN) + module = new SpirVModule; + else + module = new GlslModule; module->load_source(*io, this, name); return module.release(); } @@ -250,14 +281,26 @@ void Resources::set_debug_name(const string &name, T &item) Resources::Loader::Loader(Resources &r): DerivedObjectLoader(r) { - add("scene", &Loader::scene); + add("light", &Loader::generic); + add("material", &Loader::generic); + add("scene", &Loader::generic); + add("texture", &Loader::generic>); } -void Resources::Loader::scene(const string &name) +template +void Resources::Loader::generic(const string &name) { - Scene::GenericLoader ldr(obj); + L ldr(obj); load_sub_with(ldr); - obj.add(name, ldr.get_object()); + ldr.store_object(obj, name); +} + + +template +void Resources::GenericResourceLoader::type(const DataFile::Symbol &t) +{ + T::GenericLoader::type(t); + this->object->set_manager(resources.resource_manager); } } // namespace GL