X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresources%2Fresources.cpp;h=32f59d93d6e5a85f0bea998fd6bc446deb3fe88f;hb=e6e6f0fe81a2f92985e65ae0de708974194d81a3;hp=114a08ecf838f0f6fd6fbf5c517fa64e9a07fe5f;hpb=73bef37da97b6da0b99227f63235cb52c4e56c44;p=libs%2Fgl.git diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 114a08ec..32f59d93 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -2,24 +2,33 @@ #include #include "animation.h" #include "armature.h" +#include "basicmaterial.h" #include "camera.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 "pipelinetemplate.h" +#include "occludedscene.h" +#include "orderedscene.h" +#include "pbrmaterial.h" +#include "sequencetemplate.h" #include "pose.h" #include "program.h" #include "resourcemanager.h" #include "resources.h" #include "sampler.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; @@ -28,34 +37,84 @@ namespace Msp { namespace GL { void init_shaderlib(DataFile::BuiltinSource &); +void init_builtin_data(DataFile::BuiltinSource &); -Resources::Resources(): - default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR), - default_tex_anisotropy(1.0f), +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().keyword("camera"); + 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(&set_debug_name); add_type().keyword("font"); add_type().suffix(".kframe").keyword("keyframe"); - add_type().suffix(".lightn").keyword("lighting"); - add_type().suffix(".mat").creator(&Resources::create_material); - add_type().keyword("mesh").creator(&Resources::create_mesh); + add_type().keyword("light"); + add_type().suffix(".lightn").keyword("lighting") + .notify(&set_debug_name); + add_type().keyword("mesh") + .creator([this](const string &n){ return create_mesh(n); }) + .notify(&set_debug_name); + add_type().suffix(".glsl").suffix(".spv") + .creator([this](const string &n){ return create_module(n); }); add_type().keyword("object"); - add_type().suffix(".pipe").keyword("pipeline"); + add_type().base().suffix(".scene") + .creator([this](const string &n) -> OccludedScene * { create_generic(n); return 0; }); + add_type().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().suffix(".seq").keyword("sequence"); add_type().keyword("pose"); - add_type().keyword("shader").suffix(".glsl").creator(&Resources::create_program); - add_type().suffix(".samp").keyword("sampler"); - add_type().suffix(".tech").keyword("technique"); - add_type().base().suffix(".tex1d").keyword("texture1d"); - add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d); - add_type().base().suffix(".tex3d").keyword("texture3d"); - add_type().base().suffix(".texcb").keyword("texture_cube"); - add_type().base().suffix(".tex2da").keyword("texture2d_array"); + add_type().keyword("shader") + .creator([this](const string &n){ return create_program(n); }) + .notify(&set_debug_name); + add_type().suffix(".samp").keyword("sampler") + .notify(&set_debug_name); + add_type().base().suffix(".scene") + .creator([this](const string &n) -> SimpleScene * { create_generic(n); return 0; }); + add_type().suffix(".tech").keyword("technique") + .notify(&set_debug_name); + add_type().base().suffix(".tex1d").keyword("texture1d") + .notify(&set_debug_name); + add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d") + .creator([this](const string &n){ return create_texture2d(n); }) + .notify(&set_debug_name); + add_type().base().suffix(".tex3d").keyword("texture3d") + .notify(&set_debug_name); + add_type().base().suffix(".texcb").keyword("texture_cube") + .notify(&set_debug_name); + add_type().base().suffix(".tex2da").keyword("texture2d_array") + .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().suffix(".scene") + .creator([this](const string &n) -> ZSortedScene * { create_generic(n); return 0; }); add_source(get_builtins()); + + if(set_as_global && !global_resources) + global_resources = this; +} + +Resources::~Resources() +{ + if(this==global_resources) + global_resources = 0; +} + +Resources &Resources::get_global() +{ + if(!global_resources) + throw invalid_operation("no global resources"); + return *global_resources; } const DataFile::CollectionSource &Resources::get_builtins() @@ -65,6 +124,7 @@ const DataFile::CollectionSource &Resources::get_builtins() if(!init_done) { + init_builtin_data(builtins); init_shaderlib(builtins); init_done = true; } @@ -72,16 +132,6 @@ const DataFile::CollectionSource &Resources::get_builtins() return builtins; } -void Resources::set_default_texture_filter(TextureFilter tf) -{ - default_tex_filter = tf; -} - -void Resources::set_default_texture_anisotropy(float a) -{ - default_tex_anisotropy = a; -} - void Resources::set_srgb_conversion(bool c) { srgb_conversion = c; @@ -92,14 +142,15 @@ void Resources::set_resource_manager(ResourceManager *m) resource_manager = m; } -Material *Resources::create_material(const string &name) +template +T *Resources::create_generic(const string &name) { if(RefPtr io = open_raw(name)) { DataFile::Parser parser(*io, name); - Material::GenericLoader ldr(this); + typename T::GenericLoader ldr(*this); ldr.load(parser); - return ldr.get_material(); + ldr.store_object(*this, name); } return 0; @@ -128,61 +179,107 @@ Texture2D *Resources::create_texture2d(const string &name) if(RefPtr io = open_raw(name)) { - Graphics::Image image; - if(!resource_manager) - image.load_io(*io); - - RefPtr tex = new Texture2D(resource_manager); + RefPtr tex; if(ext==".tex2d") { + tex = new Texture2D(resource_manager); DataFile::Parser parser(*io, name); Texture2D::Loader ldr(*tex, *this); ldr.load(parser); } else { - Sampler &samp = tex->get_default_sampler(); - if(is_mipmapped(default_tex_filter)) - { - tex->set_auto_generate_mipmap(true); - samp.set_mag_filter(LINEAR); - } + // Verify that the image is loadable + Graphics::Image image; + if(!resource_manager) + image.load_io(*io); + + tex = new Texture2D(resource_manager); + + if(resource_manager) + resource_manager->set_resource_location(*tex, *this, name); else - samp.set_mag_filter(default_tex_filter); - samp.set_min_filter(default_tex_filter); - samp.set_max_anisotropy(default_tex_anisotropy); + tex->image(image); } - if(resource_manager) - resource_manager->set_resource_location(*tex, *this, name); - else - tex->image(image); return tex.release(); } return 0; } -Program *Resources::create_program(const string &name) +Module *Resources::create_module(const string &name) { string ext = FS::extpart(name); - if(ext==".shader") + if(ext!=".glsl" && ext!=".spv") return 0; if(RefPtr io = open_raw(name)) { - SL::Compiler compiler; - compiler.load_source(*io, this, name); - compiler.compile(); - RefPtr program = new Program; - compiler.add_shaders(*program); - program->link(); - return program.release(); + if(ext==".glsl") + { + RefPtr module = new GlslModule; + module->load_source(*io, this, name); + return module.release(); + } + else if(ext==".spv") + { + RefPtr module = new SpirVModule; + module->load_code(*io); + return module.release(); + } + } + else if(ext==".spv") + { + if((io = open_raw(FS::basepart(name)+".glsl"))) + { + RefPtr module = new SpirVModule; + module->load_source(*io, this, name); + return module.release(); + } } return 0; } +Program *Resources::create_program(const string &name) +{ + string ext = FS::extpart(name); + string base = FS::basepart(name); + string ext2 = FS::extpart(base); + if(ext==".shader" && (ext2==".glsl" || ext2==".spv")) + { + Module &module = get(base); + RefPtr shprog = new Program; + shprog->add_stages(module); + return shprog.release(); + } + + return 0; +} + +template +void Resources::set_debug_name(const string &name, T &item) +{ +#ifdef DEBUG + item.set_debug_name(name); +#endif +} + + +Resources::Loader::Loader(Resources &r): + DerivedObjectLoader(r) +{ + add("scene", &Loader::scene); +} + +void Resources::Loader::scene(const string &name) +{ + Scene::GenericLoader ldr(obj); + load_sub_with(ldr); + obj.add(name, ldr.get_object()); +} + } // namespace GL } // namespace Msp