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 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
41 default_tex_anisotropy(1.0f),
42 srgb_conversion(false),
45 add_type<Animation>().suffix(".anim").keyword("animation");
46 add_type<Armature>().suffix(".arma").keyword("armature");
47 add_type<Camera>().keyword("camera")
48 .notify(&Resources::set_debug_name<Camera>);
49 add_type<Font>().keyword("font");
50 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
51 add_type<Light>().keyword("light");
52 add_type<Lighting>().suffix(".lightn").keyword("lighting")
53 .notify(&Resources::set_debug_name<Lighting>);
54 add_type<Material>().suffix(".mat")
55 .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
56 add_type<Mesh>().keyword("mesh")
57 .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
58 add_type<Module>().suffix(".glsl").suffix(".spv")
59 .creator(&Resources::create_module);
60 add_type<Object>().keyword("object");
61 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
62 add_type<Pose>().keyword("pose");
63 add_type<Program>().keyword("shader")
64 .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
65 add_type<Sampler>().suffix(".samp").keyword("sampler")
66 .notify(&Resources::set_debug_name<Sampler>);
67 add_type<Scene>().suffix(".scene")
68 .creator(&Resources::create_scene);
69 add_type<Technique>().suffix(".tech").keyword("technique")
70 .notify(&Resources::set_debug_name<Technique>);
71 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
72 .notify(&Resources::set_debug_name<Texture1D>);
73 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
74 .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
75 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
76 .notify(&Resources::set_debug_name<Texture3D>);
77 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
78 .notify(&Resources::set_debug_name<TextureCube>);
79 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
80 .notify(&Resources::set_debug_name<Texture2DArray>);
82 add_source(get_builtins());
84 if(set_as_global && !global_resources)
85 global_resources = this;
88 Resources::~Resources()
90 if(this==global_resources)
94 Resources &Resources::get_global()
97 throw invalid_operation("no global resources");
98 return *global_resources;
101 const DataFile::CollectionSource &Resources::get_builtins()
103 static DataFile::BuiltinSource builtins;
104 bool init_done = false;
108 init_builtin_data(builtins);
109 init_shaderlib(builtins);
116 void Resources::set_default_texture_filter(TextureFilter tf)
118 default_tex_filter = tf;
121 void Resources::set_default_texture_anisotropy(float a)
123 default_tex_anisotropy = a;
126 void Resources::set_srgb_conversion(bool c)
131 void Resources::set_resource_manager(ResourceManager *m)
133 resource_manager = m;
136 Material *Resources::create_material(const string &name)
138 if(RefPtr<IO::Seekable> io = open_raw(name))
140 DataFile::Parser parser(*io, name);
141 Material::GenericLoader ldr(this);
143 return ldr.get_material();
149 Mesh *Resources::create_mesh(const string &name)
151 if(!resource_manager)
154 if(RefPtr<IO::Seekable> io = open_raw(name))
156 RefPtr<Mesh> mesh = new Mesh(resource_manager);
157 resource_manager->set_resource_location(*mesh, *this, name);
158 return mesh.release();
164 Scene *Resources::create_scene(const string &name)
166 if(RefPtr<IO::Seekable> io = open_raw(name))
168 DataFile::Parser parser(*io, name);
169 Scene::GenericLoader ldr(*this);
171 return ldr.get_scene();
177 Texture2D *Resources::create_texture2d(const string &name)
179 string ext = FS::extpart(name);
180 if(ext==".tex2d" && !resource_manager)
183 if(RefPtr<IO::Seekable> io = open_raw(name))
185 RefPtr<Texture2D> tex;
189 tex = new Texture2D(resource_manager);
190 DataFile::Parser parser(*io, name);
191 Texture2D::Loader ldr(*tex, *this);
196 // Verify that the image is loadable
197 Graphics::Image image;
198 if(!resource_manager)
201 tex = new Texture2D(resource_manager);
202 Sampler &samp = tex->get_default_sampler();
203 if(is_mipmapped(default_tex_filter))
205 tex->set_auto_generate_mipmap(true);
206 samp.set_mag_filter(LINEAR);
209 samp.set_mag_filter(default_tex_filter);
210 samp.set_min_filter(default_tex_filter);
211 samp.set_max_anisotropy(default_tex_anisotropy);
214 resource_manager->set_resource_location(*tex, *this, name);
219 return tex.release();
225 Module *Resources::create_module(const string &name)
227 string ext = FS::extpart(name);
228 if(ext!=".glsl" && ext!=".spv")
231 if(RefPtr<IO::Seekable> io = open_raw(name))
235 RefPtr<GlslModule> module = new GlslModule;
236 module->load_source(*io, this, name);
237 return module.release();
241 RefPtr<SpirVModule> module = new SpirVModule;
242 module->load_code(*io);
243 return module.release();
248 if((io = open_raw(FS::basepart(name)+".glsl")))
250 RefPtr<SpirVModule> module = new SpirVModule;
251 module->load_source(*io, this, name);
252 return module.release();
259 Program *Resources::create_program(const string &name)
261 string ext = FS::extpart(name);
262 string base = FS::basepart(name);
263 string ext2 = FS::extpart(base);
264 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
266 Module &module = get<Module>(base);
267 RefPtr<Program> shprog = new Program;
268 shprog->add_stages(module);
270 return shprog.release();
277 void Resources::set_debug_name(const string &name, T &item)
280 item.set_debug_name(name);
285 Resources::Loader::Loader(Resources &r):
286 DerivedObjectLoader<Resources, Collection::Loader>(r)
288 add("scene", &Loader::scene);
291 void Resources::Loader::scene(const string &name)
293 Scene::GenericLoader ldr(obj);
295 obj.add(name, ldr.get_scene());