1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
13 #include "pipelinetemplate.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
20 #include "technique.h"
21 #include "texture1d.h"
22 #include "texture2d.h"
23 #include "texture2darray.h"
24 #include "texturecube.h"
25 #include "glsl/compiler.h"
32 void init_shaderlib(DataFile::BuiltinSource &);
33 void init_builtin_data(DataFile::BuiltinSource &);
35 Resources::Resources():
36 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
37 default_tex_anisotropy(1.0f),
38 srgb_conversion(false),
41 add_type<Animation>().suffix(".anim").keyword("animation");
42 add_type<Armature>().suffix(".arma").keyword("armature");
43 add_type<Camera>().keyword("camera");
44 add_type<Font>().keyword("font");
45 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
46 add_type<Lighting>().suffix(".lightn").keyword("lighting");
47 add_type<Material>().suffix(".mat").creator(&Resources::create_material);
48 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
49 add_type<Module>().suffix(".glsl").suffix(".spv").creator(&Resources::create_module);
50 add_type<Object>().keyword("object");
51 add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
52 add_type<Pose>().keyword("pose");
53 add_type<Program>().keyword("shader").creator(&Resources::create_program);
54 add_type<Sampler>().suffix(".samp").keyword("sampler");
55 add_type<Scene>().suffix(".scene").creator(&Resources::create_scene);
56 add_type<Technique>().suffix(".tech").keyword("technique");
57 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
58 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
59 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
60 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
61 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
63 add_source(get_builtins());
66 const DataFile::CollectionSource &Resources::get_builtins()
68 static DataFile::BuiltinSource builtins;
69 bool init_done = false;
73 init_builtin_data(builtins);
74 init_shaderlib(builtins);
81 void Resources::set_default_texture_filter(TextureFilter tf)
83 default_tex_filter = tf;
86 void Resources::set_default_texture_anisotropy(float a)
88 default_tex_anisotropy = a;
91 void Resources::set_srgb_conversion(bool c)
96 void Resources::set_resource_manager(ResourceManager *m)
101 Material *Resources::create_material(const string &name)
103 if(RefPtr<IO::Seekable> io = open_raw(name))
105 DataFile::Parser parser(*io, name);
106 Material::GenericLoader ldr(this);
108 return ldr.get_material();
114 Mesh *Resources::create_mesh(const string &name)
116 if(!resource_manager)
119 if(RefPtr<IO::Seekable> io = open_raw(name))
121 RefPtr<Mesh> mesh = new Mesh(resource_manager);
122 resource_manager->set_resource_location(*mesh, *this, name);
123 return mesh.release();
129 Scene *Resources::create_scene(const string &name)
131 if(RefPtr<IO::Seekable> io = open_raw(name))
133 DataFile::Parser parser(*io, name);
134 Scene::GenericLoader ldr(*this);
136 return ldr.get_scene();
142 Texture2D *Resources::create_texture2d(const string &name)
144 string ext = FS::extpart(name);
145 if(ext==".tex2d" && !resource_manager)
148 if(RefPtr<IO::Seekable> io = open_raw(name))
150 RefPtr<Texture2D> tex;
154 tex = new Texture2D(resource_manager);
155 DataFile::Parser parser(*io, name);
156 Texture2D::Loader ldr(*tex, *this);
161 // Verify that the image is loadable
162 Graphics::Image image;
163 if(!resource_manager)
166 tex = new Texture2D(resource_manager);
167 Sampler &samp = tex->get_default_sampler();
168 if(is_mipmapped(default_tex_filter))
170 tex->set_auto_generate_mipmap(true);
171 samp.set_mag_filter(LINEAR);
174 samp.set_mag_filter(default_tex_filter);
175 samp.set_min_filter(default_tex_filter);
176 samp.set_max_anisotropy(default_tex_anisotropy);
179 resource_manager->set_resource_location(*tex, *this, name);
184 return tex.release();
190 Module *Resources::create_module(const string &name)
192 string ext = FS::extpart(name);
193 if(ext!=".glsl" && ext!=".spv")
196 if(RefPtr<IO::Seekable> io = open_raw(name))
200 RefPtr<GlslModule> module = new GlslModule;
201 module->load_source(*io, this, name);
202 return module.release();
206 RefPtr<SpirVModule> module = new SpirVModule;
207 module->load_code(*io);
208 return module.release();
215 Program *Resources::create_program(const string &name)
217 string ext = FS::extpart(name);
218 string base = FS::basepart(name);
219 string ext2 = FS::extpart(base);
220 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
222 Module &module = get<Module>(base);
223 RefPtr<Program> shprog = new Program;
224 shprog->add_stages(module);
226 return shprog.release();