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").creator(&Resources::create_material);
50 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
51 add_type<Module>().suffix(".glsl").suffix(".spv").creator(&Resources::create_module);
52 add_type<Object>().keyword("object");
53 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
54 add_type<Pose>().keyword("pose");
55 add_type<Program>().keyword("shader").creator(&Resources::create_program);
56 add_type<Sampler>().suffix(".samp").keyword("sampler");
57 add_type<Scene>().suffix(".scene").creator(&Resources::create_scene);
58 add_type<Technique>().suffix(".tech").keyword("technique");
59 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
60 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
61 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
62 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
63 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
65 add_source(get_builtins());
68 const DataFile::CollectionSource &Resources::get_builtins()
70 static DataFile::BuiltinSource builtins;
71 bool init_done = false;
75 init_builtin_data(builtins);
76 init_shaderlib(builtins);
83 void Resources::set_default_texture_filter(TextureFilter tf)
85 default_tex_filter = tf;
88 void Resources::set_default_texture_anisotropy(float a)
90 default_tex_anisotropy = a;
93 void Resources::set_srgb_conversion(bool c)
98 void Resources::set_resource_manager(ResourceManager *m)
100 resource_manager = m;
103 Material *Resources::create_material(const string &name)
105 if(RefPtr<IO::Seekable> io = open_raw(name))
107 DataFile::Parser parser(*io, name);
108 Material::GenericLoader ldr(this);
110 return ldr.get_material();
116 Mesh *Resources::create_mesh(const string &name)
118 if(!resource_manager)
121 if(RefPtr<IO::Seekable> io = open_raw(name))
123 RefPtr<Mesh> mesh = new Mesh(resource_manager);
124 resource_manager->set_resource_location(*mesh, *this, name);
125 return mesh.release();
131 Scene *Resources::create_scene(const string &name)
133 if(RefPtr<IO::Seekable> io = open_raw(name))
135 DataFile::Parser parser(*io, name);
136 Scene::GenericLoader ldr(*this);
138 return ldr.get_scene();
144 Texture2D *Resources::create_texture2d(const string &name)
146 string ext = FS::extpart(name);
147 if(ext==".tex2d" && !resource_manager)
150 if(RefPtr<IO::Seekable> io = open_raw(name))
152 RefPtr<Texture2D> tex;
156 tex = new Texture2D(resource_manager);
157 DataFile::Parser parser(*io, name);
158 Texture2D::Loader ldr(*tex, *this);
163 // Verify that the image is loadable
164 Graphics::Image image;
165 if(!resource_manager)
168 tex = new Texture2D(resource_manager);
169 Sampler &samp = tex->get_default_sampler();
170 if(is_mipmapped(default_tex_filter))
172 tex->set_auto_generate_mipmap(true);
173 samp.set_mag_filter(LINEAR);
176 samp.set_mag_filter(default_tex_filter);
177 samp.set_min_filter(default_tex_filter);
178 samp.set_max_anisotropy(default_tex_anisotropy);
181 resource_manager->set_resource_location(*tex, *this, name);
186 return tex.release();
192 Module *Resources::create_module(const string &name)
194 string ext = FS::extpart(name);
195 if(ext!=".glsl" && ext!=".spv")
198 if(RefPtr<IO::Seekable> io = open_raw(name))
202 RefPtr<GlslModule> module = new GlslModule;
203 module->load_source(*io, this, name);
204 return module.release();
208 RefPtr<SpirVModule> module = new SpirVModule;
209 module->load_code(*io);
210 return module.release();
217 Program *Resources::create_program(const string &name)
219 string ext = FS::extpart(name);
220 string base = FS::basepart(name);
221 string ext2 = FS::extpart(base);
222 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
224 Module &module = get<Module>(base);
225 RefPtr<Program> shprog = new Program;
226 shprog->add_stages(module);
228 return shprog.release();
235 Resources::Loader::Loader(Resources &r):
236 DerivedObjectLoader<Resources, Collection::Loader>(r)
238 add("scene", &Loader::scene);
241 void Resources::Loader::scene(const string &name)
243 Scene::GenericLoader ldr(obj);
245 obj.add(name, ldr.get_scene());