1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
14 #include "sequencetemplate.h"
17 #include "resourcemanager.h"
18 #include "resources.h"
21 #include "technique.h"
22 #include "texture1d.h"
23 #include "texture2d.h"
24 #include "texture2darray.h"
25 #include "texturecube.h"
26 #include "glsl/compiler.h"
33 void init_shaderlib(DataFile::BuiltinSource &);
34 void init_builtin_data(DataFile::BuiltinSource &);
36 Resources::Resources():
37 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
38 default_tex_anisotropy(1.0f),
39 srgb_conversion(false),
42 add_type<Animation>().suffix(".anim").keyword("animation");
43 add_type<Armature>().suffix(".arma").keyword("armature");
44 add_type<Camera>().keyword("camera");
45 add_type<Font>().keyword("font");
46 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
47 add_type<Light>().keyword("light");
48 add_type<Lighting>().suffix(".lightn").keyword("lighting");
49 add_type<Material>().suffix(".mat")
50 .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
51 add_type<Mesh>().keyword("mesh")
52 .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
53 add_type<Module>().suffix(".glsl").suffix(".spv")
54 .creator(&Resources::create_module);
55 add_type<Object>().keyword("object");
56 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
57 add_type<Pose>().keyword("pose");
58 add_type<Program>().keyword("shader")
59 .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
60 add_type<Sampler>().suffix(".samp").keyword("sampler")
61 .notify(&Resources::set_debug_name<Sampler>);
62 add_type<Scene>().suffix(".scene")
63 .creator(&Resources::create_scene);
64 add_type<Technique>().suffix(".tech").keyword("technique")
65 .notify(&Resources::set_debug_name<Technique>);
66 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
67 .notify(&Resources::set_debug_name<Texture1D>);
68 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
69 .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
70 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
71 .notify(&Resources::set_debug_name<Texture3D>);
72 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
73 .notify(&Resources::set_debug_name<TextureCube>);
74 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
75 .notify(&Resources::set_debug_name<Texture2DArray>);
77 add_source(get_builtins());
80 const DataFile::CollectionSource &Resources::get_builtins()
82 static DataFile::BuiltinSource builtins;
83 bool init_done = false;
87 init_builtin_data(builtins);
88 init_shaderlib(builtins);
95 void Resources::set_default_texture_filter(TextureFilter tf)
97 default_tex_filter = tf;
100 void Resources::set_default_texture_anisotropy(float a)
102 default_tex_anisotropy = a;
105 void Resources::set_srgb_conversion(bool c)
110 void Resources::set_resource_manager(ResourceManager *m)
112 resource_manager = m;
115 Material *Resources::create_material(const string &name)
117 if(RefPtr<IO::Seekable> io = open_raw(name))
119 DataFile::Parser parser(*io, name);
120 Material::GenericLoader ldr(this);
122 return ldr.get_material();
128 Mesh *Resources::create_mesh(const string &name)
130 if(!resource_manager)
133 if(RefPtr<IO::Seekable> io = open_raw(name))
135 RefPtr<Mesh> mesh = new Mesh(resource_manager);
136 resource_manager->set_resource_location(*mesh, *this, name);
137 return mesh.release();
143 Scene *Resources::create_scene(const string &name)
145 if(RefPtr<IO::Seekable> io = open_raw(name))
147 DataFile::Parser parser(*io, name);
148 Scene::GenericLoader ldr(*this);
150 return ldr.get_scene();
156 Texture2D *Resources::create_texture2d(const string &name)
158 string ext = FS::extpart(name);
159 if(ext==".tex2d" && !resource_manager)
162 if(RefPtr<IO::Seekable> io = open_raw(name))
164 RefPtr<Texture2D> tex;
168 tex = new Texture2D(resource_manager);
169 DataFile::Parser parser(*io, name);
170 Texture2D::Loader ldr(*tex, *this);
175 // Verify that the image is loadable
176 Graphics::Image image;
177 if(!resource_manager)
180 tex = new Texture2D(resource_manager);
181 Sampler &samp = tex->get_default_sampler();
182 if(is_mipmapped(default_tex_filter))
184 tex->set_auto_generate_mipmap(true);
185 samp.set_mag_filter(LINEAR);
188 samp.set_mag_filter(default_tex_filter);
189 samp.set_min_filter(default_tex_filter);
190 samp.set_max_anisotropy(default_tex_anisotropy);
193 resource_manager->set_resource_location(*tex, *this, name);
198 return tex.release();
204 Module *Resources::create_module(const string &name)
206 string ext = FS::extpart(name);
207 if(ext!=".glsl" && ext!=".spv")
210 if(RefPtr<IO::Seekable> io = open_raw(name))
214 RefPtr<GlslModule> module = new GlslModule;
215 module->load_source(*io, this, name);
216 return module.release();
220 RefPtr<SpirVModule> module = new SpirVModule;
221 module->load_code(*io);
222 return module.release();
227 if((io = open_raw(FS::basepart(name)+".glsl")))
229 RefPtr<SpirVModule> module = new SpirVModule;
230 module->load_source(*io, this, name);
231 return module.release();
238 Program *Resources::create_program(const string &name)
240 string ext = FS::extpart(name);
241 string base = FS::basepart(name);
242 string ext2 = FS::extpart(base);
243 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
245 Module &module = get<Module>(base);
246 RefPtr<Program> shprog = new Program;
247 shprog->add_stages(module);
249 return shprog.release();
256 void Resources::set_debug_name(const string &name, T &item)
259 item.set_debug_name(name);
264 Resources::Loader::Loader(Resources &r):
265 DerivedObjectLoader<Resources, Collection::Loader>(r)
267 add("scene", &Loader::scene);
270 void Resources::Loader::scene(const string &name)
272 Scene::GenericLoader ldr(obj);
274 obj.add(name, ldr.get_scene());