]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resources/resources.cpp
Access builtin resources through a global instance
[libs/gl.git] / source / resources / resources.cpp
index dc034746652c0559a60972b8613e4d93956bb054..79c95932b6c52b03f45c669d58a68f71d5e2bbeb 100644 (file)
@@ -3,18 +3,22 @@
 #include "animation.h"
 #include "armature.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 "sequencetemplate.h"
 #include "pose.h"
 #include "program.h"
 #include "resourcemanager.h"
 #include "resources.h"
 #include "sampler.h"
+#include "scene.h"
 #include "technique.h"
 #include "texture1d.h"
 #include "texture2d.h"
@@ -30,7 +34,9 @@ namespace GL {
 void init_shaderlib(DataFile::BuiltinSource &);
 void init_builtin_data(DataFile::BuiltinSource &);
 
-Resources::Resources():
+Resources *Resources::global_resources = 0;
+
+Resources::Resources(bool set_as_global):
        default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
        default_tex_anisotropy(1.0f),
        srgb_conversion(false),
@@ -41,22 +47,53 @@ Resources::Resources():
        add_type<Camera>().keyword("camera");
        add_type<Font>().keyword("font");
        add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
+       add_type<Light>().keyword("light");
        add_type<Lighting>().suffix(".lightn").keyword("lighting");
-       add_type<Material>().suffix(".mat").creator(&Resources::create_material);
-       add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
+       add_type<Material>().suffix(".mat")
+               .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
+       add_type<Mesh>().keyword("mesh")
+               .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
+       add_type<Module>().suffix(".glsl").suffix(".spv")
+               .creator(&Resources::create_module);
        add_type<Object>().keyword("object");
-       add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
+       add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
        add_type<Pose>().keyword("pose");
-       add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
-       add_type<Sampler>().suffix(".samp").keyword("sampler");
-       add_type<Technique>().suffix(".tech").keyword("technique");
-       add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
-       add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
-       add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
-       add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
-       add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
+       add_type<Program>().keyword("shader")
+               .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
+       add_type<Sampler>().suffix(".samp").keyword("sampler")
+               .notify(&Resources::set_debug_name<Sampler>);
+       add_type<Scene>().suffix(".scene")
+               .creator(&Resources::create_scene);
+       add_type<Technique>().suffix(".tech").keyword("technique")
+               .notify(&Resources::set_debug_name<Technique>);
+       add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
+               .notify(&Resources::set_debug_name<Texture1D>);
+       add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
+               .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
+       add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
+               .notify(&Resources::set_debug_name<Texture3D>);
+       add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
+               .notify(&Resources::set_debug_name<TextureCube>);
+       add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
+               .notify(&Resources::set_debug_name<Texture2DArray>);
 
        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()
@@ -122,6 +159,19 @@ Mesh *Resources::create_mesh(const string &name)
        return 0;
 }
 
+Scene *Resources::create_scene(const string &name)
+{
+       if(RefPtr<IO::Seekable> io = open_raw(name))
+       {
+               DataFile::Parser parser(*io, name);
+               Scene::GenericLoader ldr(*this);
+               ldr.load(parser);
+               return ldr.get_scene();
+       }
+
+       return 0;
+}
+
 Texture2D *Resources::create_texture2d(const string &name)
 {
        string ext = FS::extpart(name);
@@ -130,20 +180,23 @@ Texture2D *Resources::create_texture2d(const string &name)
 
        if(RefPtr<IO::Seekable> io = open_raw(name))
        {
-               Graphics::Image image;
-               if(!resource_manager)
-                       image.load_io(*io);
-
-               RefPtr<Texture2D> tex = new Texture2D(resource_manager);
+               RefPtr<Texture2D> 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);
+
+                       tex = new Texture2D(resource_manager);
                        Sampler &samp = tex->get_default_sampler();
                        if(is_mipmapped(default_tex_filter))
                        {
@@ -154,37 +207,91 @@ Texture2D *Resources::create_texture2d(const string &name)
                                samp.set_mag_filter(default_tex_filter);
                        samp.set_min_filter(default_tex_filter);
                        samp.set_max_anisotropy(default_tex_anisotropy);
+
+                       if(resource_manager)
+                               resource_manager->set_resource_location(*tex, *this, name);
+                       else
+                               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::Seekable> io = open_raw(name))
        {
-               SL::Compiler compiler;
-               compiler.load_source(*io, this, name);
-               compiler.compile();
-               RefPtr<Program> program = new Program;
-               compiler.add_shaders(*program);
-               program->link();
-               return program.release();
+               if(ext==".glsl")
+               {
+                       RefPtr<GlslModule> module = new GlslModule;
+                       module->load_source(*io, this, name);
+                       return module.release();
+               }
+               else if(ext==".spv")
+               {
+                       RefPtr<SpirVModule> module = new SpirVModule;
+                       module->load_code(*io);
+                       return module.release();
+               }
+       }
+       else if(ext==".spv")
+       {
+               if((io = open_raw(FS::basepart(name)+".glsl")))
+               {
+                       RefPtr<SpirVModule> 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<Module>(base);
+               RefPtr<Program> shprog = new Program;
+               shprog->add_stages(module);
+               shprog->link();
+               return shprog.release();
+       }
+
+       return 0;
+}
+
+template<typename T>
+void Resources::set_debug_name(const string &name, T &item)
+{
+#ifdef DEBUG
+       item.set_debug_name(name);
+#endif
+}
+
+
+Resources::Loader::Loader(Resources &r):
+       DerivedObjectLoader<Resources, Collection::Loader>(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_scene());
+}
+
 } // namespace GL
 } // namespace Msp