1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
13 #include "pipelinetemplate.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
19 #include "technique.h"
20 #include "texture1d.h"
21 #include "texture2d.h"
22 #include "texture2darray.h"
23 #include "texturecube.h"
24 #include "glsl/compiler.h"
31 void init_shaderlib(DataFile::BuiltinSource &);
32 void init_builtin_data(DataFile::BuiltinSource &);
34 Resources::Resources():
35 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
36 default_tex_anisotropy(1.0f),
37 srgb_conversion(false),
40 add_type<Animation>().suffix(".anim").keyword("animation");
41 add_type<Armature>().suffix(".arma").keyword("armature");
42 add_type<Camera>().keyword("camera");
43 add_type<Font>().keyword("font");
44 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
45 add_type<Lighting>().suffix(".lightn").keyword("lighting");
46 add_type<Material>().suffix(".mat").creator(&Resources::create_material);
47 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
48 add_type<Module>().suffix(".glsl").suffix(".spv").creator(&Resources::create_module);
49 add_type<Object>().keyword("object");
50 add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
51 add_type<Pose>().keyword("pose");
52 add_type<Program>().keyword("shader").creator(&Resources::create_program);
53 add_type<Sampler>().suffix(".samp").keyword("sampler");
54 add_type<Technique>().suffix(".tech").keyword("technique");
55 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
56 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
57 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
58 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
59 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
61 add_source(get_builtins());
64 const DataFile::CollectionSource &Resources::get_builtins()
66 static DataFile::BuiltinSource builtins;
67 bool init_done = false;
71 init_builtin_data(builtins);
72 init_shaderlib(builtins);
79 void Resources::set_default_texture_filter(TextureFilter tf)
81 default_tex_filter = tf;
84 void Resources::set_default_texture_anisotropy(float a)
86 default_tex_anisotropy = a;
89 void Resources::set_srgb_conversion(bool c)
94 void Resources::set_resource_manager(ResourceManager *m)
99 Material *Resources::create_material(const string &name)
101 if(RefPtr<IO::Seekable> io = open_raw(name))
103 DataFile::Parser parser(*io, name);
104 Material::GenericLoader ldr(this);
106 return ldr.get_material();
112 Mesh *Resources::create_mesh(const string &name)
114 if(!resource_manager)
117 if(RefPtr<IO::Seekable> io = open_raw(name))
119 RefPtr<Mesh> mesh = new Mesh(resource_manager);
120 resource_manager->set_resource_location(*mesh, *this, name);
121 return mesh.release();
127 Texture2D *Resources::create_texture2d(const string &name)
129 string ext = FS::extpart(name);
130 if(ext==".tex2d" && !resource_manager)
133 if(RefPtr<IO::Seekable> io = open_raw(name))
135 RefPtr<Texture2D> tex;
139 tex = new Texture2D(resource_manager);
140 DataFile::Parser parser(*io, name);
141 Texture2D::Loader ldr(*tex, *this);
146 // Verify that the image is loadable
147 Graphics::Image image;
148 if(!resource_manager)
151 tex = new Texture2D(resource_manager);
152 Sampler &samp = tex->get_default_sampler();
153 if(is_mipmapped(default_tex_filter))
155 tex->set_auto_generate_mipmap(true);
156 samp.set_mag_filter(LINEAR);
159 samp.set_mag_filter(default_tex_filter);
160 samp.set_min_filter(default_tex_filter);
161 samp.set_max_anisotropy(default_tex_anisotropy);
164 resource_manager->set_resource_location(*tex, *this, name);
169 return tex.release();
175 Module *Resources::create_module(const string &name)
177 string ext = FS::extpart(name);
178 if(ext!=".glsl" && ext!=".spv")
181 if(RefPtr<IO::Seekable> io = open_raw(name))
185 RefPtr<GlslModule> module = new GlslModule;
186 module->load_source(*io, this, name);
187 return module.release();
191 RefPtr<SpirVModule> module = new SpirVModule;
192 module->load_code(*io);
193 return module.release();
200 Program *Resources::create_program(const string &name)
202 string ext = FS::extpart(name);
203 string base = FS::basepart(name);
204 string ext2 = FS::extpart(base);
205 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
207 Module &module = get<Module>(base);
208 RefPtr<Program> shprog = new Program;
209 shprog->add_stages(module);
211 return shprog.release();