1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
15 #include "sequencetemplate.h"
18 #include "resourcemanager.h"
19 #include "resources.h"
22 #include "technique.h"
23 #include "texture1d.h"
24 #include "texture2d.h"
25 #include "texture2darray.h"
26 #include "texturecube.h"
27 #include "glsl/compiler.h"
34 void init_shaderlib(DataFile::BuiltinSource &);
35 void init_builtin_data(DataFile::BuiltinSource &);
37 Resources *Resources::global_resources = 0;
39 Resources::Resources(bool set_as_global):
40 srgb_conversion(false),
43 add_type<Animation>().suffix(".anim").keyword("animation");
44 add_type<Armature>().suffix(".arma").keyword("armature");
45 add_type<Camera>().keyword("camera")
46 .notify(&Resources::set_debug_name<Camera>);
47 add_type<Font>().keyword("font");
48 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
49 add_type<Light>().keyword("light");
50 add_type<Lighting>().suffix(".lightn").keyword("lighting")
51 .notify(&Resources::set_debug_name<Lighting>);
52 add_type<Material>().suffix(".mat")
53 .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
54 add_type<Mesh>().keyword("mesh")
55 .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
56 add_type<Module>().suffix(".glsl").suffix(".spv")
57 .creator(&Resources::create_module);
58 add_type<Object>().keyword("object");
59 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
60 add_type<Pose>().keyword("pose");
61 add_type<Program>().keyword("shader")
62 .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
63 add_type<Sampler>().suffix(".samp").keyword("sampler")
64 .notify(&Resources::set_debug_name<Sampler>);
65 add_type<Scene>().suffix(".scene")
66 .creator(&Resources::create_scene);
67 add_type<Technique>().suffix(".tech").keyword("technique")
68 .notify(&Resources::set_debug_name<Technique>);
69 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
70 .notify(&Resources::set_debug_name<Texture1D>);
71 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
72 .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
73 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
74 .notify(&Resources::set_debug_name<Texture3D>);
75 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
76 .notify(&Resources::set_debug_name<TextureCube>);
77 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
78 .notify(&Resources::set_debug_name<Texture2DArray>);
80 add_source(get_builtins());
82 if(set_as_global && !global_resources)
83 global_resources = this;
86 Resources::~Resources()
88 if(this==global_resources)
92 Resources &Resources::get_global()
95 throw invalid_operation("no global resources");
96 return *global_resources;
99 const DataFile::CollectionSource &Resources::get_builtins()
101 static DataFile::BuiltinSource builtins;
102 bool init_done = false;
106 init_builtin_data(builtins);
107 init_shaderlib(builtins);
114 void Resources::set_srgb_conversion(bool c)
119 void Resources::set_resource_manager(ResourceManager *m)
121 resource_manager = m;
124 Material *Resources::create_material(const string &name)
126 if(RefPtr<IO::Seekable> io = open_raw(name))
128 DataFile::Parser parser(*io, name);
129 Material::GenericLoader ldr(this);
131 return ldr.get_material();
137 Mesh *Resources::create_mesh(const string &name)
139 if(!resource_manager)
142 if(RefPtr<IO::Seekable> io = open_raw(name))
144 RefPtr<Mesh> mesh = new Mesh(resource_manager);
145 resource_manager->set_resource_location(*mesh, *this, name);
146 return mesh.release();
152 Scene *Resources::create_scene(const string &name)
154 if(RefPtr<IO::Seekable> io = open_raw(name))
156 DataFile::Parser parser(*io, name);
157 Scene::GenericLoader ldr(*this);
159 return ldr.get_scene();
165 Texture2D *Resources::create_texture2d(const string &name)
167 string ext = FS::extpart(name);
168 if(ext==".tex2d" && !resource_manager)
171 if(RefPtr<IO::Seekable> io = open_raw(name))
173 RefPtr<Texture2D> tex;
177 tex = new Texture2D(resource_manager);
178 DataFile::Parser parser(*io, name);
179 Texture2D::Loader ldr(*tex, *this);
184 // Verify that the image is loadable
185 Graphics::Image image;
186 if(!resource_manager)
189 tex = new Texture2D(resource_manager);
192 resource_manager->set_resource_location(*tex, *this, name);
197 return tex.release();
203 Module *Resources::create_module(const string &name)
205 string ext = FS::extpart(name);
206 if(ext!=".glsl" && ext!=".spv")
209 if(RefPtr<IO::Seekable> io = open_raw(name))
213 RefPtr<GlslModule> module = new GlslModule;
214 module->load_source(*io, this, name);
215 return module.release();
219 RefPtr<SpirVModule> module = new SpirVModule;
220 module->load_code(*io);
221 return module.release();
226 if((io = open_raw(FS::basepart(name)+".glsl")))
228 RefPtr<SpirVModule> module = new SpirVModule;
229 module->load_source(*io, this, name);
230 return module.release();
237 Program *Resources::create_program(const string &name)
239 string ext = FS::extpart(name);
240 string base = FS::basepart(name);
241 string ext2 = FS::extpart(base);
242 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
244 Module &module = get<Module>(base);
245 RefPtr<Program> shprog = new Program;
246 shprog->add_stages(module);
248 return shprog.release();
255 void Resources::set_debug_name(const string &name, T &item)
258 item.set_debug_name(name);
263 Resources::Loader::Loader(Resources &r):
264 DerivedObjectLoader<Resources, Collection::Loader>(r)
266 add("scene", &Loader::scene);
269 void Resources::Loader::scene(const string &name)
271 Scene::GenericLoader ldr(obj);
273 obj.add(name, ldr.get_scene());