1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
5 #include "basicmaterial.h"
7 #include "directionallight.h"
15 #include "occludedscene.h"
16 #include "orderedscene.h"
17 #include "pbrmaterial.h"
18 #include "sequencetemplate.h"
19 #include "pointlight.h"
22 #include "resourcemanager.h"
23 #include "resources.h"
25 #include "simplescene.h"
26 #include "technique.h"
27 #include "texture1d.h"
28 #include "texture2d.h"
29 #include "texture2darray.h"
30 #include "texturecube.h"
31 #include "unlitmaterial.h"
32 #include "zsortedscene.h"
33 #include "glsl/compiler.h"
40 void init_shaderlib(DataFile::BuiltinSource &);
41 void init_builtin_data(DataFile::BuiltinSource &);
43 Resources *Resources::global_resources = 0;
45 Resources::Resources(bool set_as_global):
46 srgb_conversion(false),
49 add_type<Animation>().suffix(".anim").keyword("animation");
50 add_type<Armature>().suffix(".arma").keyword("armature");
51 add_type<BasicMaterial>().base<Material>().suffix(".mat")
52 .creator([this](const string &n) -> BasicMaterial * { create_generic<Material>(n); return 0; })
53 .notify(&set_debug_name<Material>);
54 add_type<Camera>().keyword("camera")
55 .notify(&set_debug_name<Camera>);
56 add_type<DirectionalLight>().base<Light>().suffix(".light")
57 .creator([this](const string &n) -> DirectionalLight * { create_generic<Light>(n); return 0; });
58 add_type<Font>().keyword("font");
59 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
60 add_type<Lighting>().suffix(".lightn").keyword("lighting")
61 .notify(&set_debug_name<Lighting>);
62 add_type<Mesh>().keyword("mesh")
63 .creator([this](const string &n){ return create_mesh(n); })
64 .notify(&set_debug_name<Mesh>);
65 add_type<Module>().suffix(".glsl").suffix(".spv")
66 .creator([this](const string &n){ return create_module(n); });
67 add_type<Object>().keyword("object");
68 add_type<OccludedScene>().base<Scene>().suffix(".scene")
69 .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
70 add_type<OrderedScene>().base<Scene>().suffix(".scene")
71 .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return 0; });
72 add_type<PbrMaterial>().base<Material>().suffix(".mat")
73 .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return 0; })
74 .notify(&set_debug_name<Material>);
75 add_type<PointLight>().base<Light>().suffix(".light")
76 .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return 0; });
77 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
78 add_type<Pose>().keyword("pose");
79 add_type<Program>().keyword("shader")
80 .creator([this](const string &n){ return create_program(n); })
81 .notify(&set_debug_name<Program>);
82 add_type<Sampler>().suffix(".samp").keyword("sampler")
83 .notify(&set_debug_name<Sampler>);
84 add_type<SimpleScene>().base<Scene>().suffix(".scene")
85 .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
86 add_type<Technique>().suffix(".tech").keyword("technique")
87 .notify(&set_debug_name<Technique>);
88 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
89 .notify(&set_debug_name<Texture1D>);
90 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
91 .creator([this](const string &n){ return create_texture2d(n); })
92 .notify(&set_debug_name<Texture2D>);
93 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
94 .notify(&set_debug_name<Texture3D>);
95 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
96 .notify(&set_debug_name<TextureCube>);
97 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
98 .notify(&set_debug_name<Texture2DArray>);
99 add_type<UnlitMaterial>().base<Material>().suffix(".mat")
100 .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
101 .notify(&set_debug_name<Material>);
102 add_type<ZSortedScene>().base<Scene>().suffix(".scene")
103 .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
105 add_source(get_builtins());
107 if(set_as_global && !global_resources)
108 global_resources = this;
111 Resources::~Resources()
113 if(this==global_resources)
114 global_resources = 0;
117 Resources &Resources::get_global()
119 if(!global_resources)
120 throw invalid_operation("no global resources");
121 return *global_resources;
124 const DataFile::CollectionSource &Resources::get_builtins()
126 static DataFile::BuiltinSource builtins;
127 bool init_done = false;
131 init_builtin_data(builtins);
132 init_shaderlib(builtins);
139 void Resources::set_srgb_conversion(bool c)
144 void Resources::set_resource_manager(ResourceManager *m)
146 resource_manager = m;
150 T *Resources::create_generic(const string &name)
152 if(RefPtr<IO::Seekable> io = open_raw(name))
154 DataFile::Parser parser(*io, name);
155 typename T::GenericLoader ldr(*this);
157 ldr.store_object(*this, name);
163 Mesh *Resources::create_mesh(const string &name)
165 if(!resource_manager)
168 if(RefPtr<IO::Seekable> io = open_raw(name))
170 RefPtr<Mesh> mesh = new Mesh;
171 mesh->set_manager(resource_manager);
172 resource_manager->set_resource_location(*mesh, *this, name);
173 return mesh.release();
179 Texture2D *Resources::create_texture2d(const string &name)
181 string ext = FS::extpart(name);
182 if(ext==".tex2d" && !resource_manager)
185 if(RefPtr<IO::Seekable> io = open_raw(name))
187 RefPtr<Texture2D> tex;
192 tex->set_manager(resource_manager);
193 DataFile::Parser parser(*io, name);
194 Texture2D::Loader ldr(*tex, *this);
199 // Verify that the image is loadable
200 Graphics::Image image;
201 if(!resource_manager)
204 tex = new Texture2D(resource_manager);
207 resource_manager->set_resource_location(*tex, *this, name);
212 return tex.release();
218 Module *Resources::create_module(const string &name)
220 string ext = FS::extpart(name);
221 if(ext!=".glsl" && ext!=".spv")
224 if(RefPtr<IO::Seekable> io = open_raw(name))
228 RefPtr<GlslModule> module = new GlslModule;
229 module->load_source(*io, this, name);
230 return module.release();
234 RefPtr<SpirVModule> module = new SpirVModule;
235 module->load_code(*io);
236 return module.release();
241 if((io = open_raw(FS::basepart(name)+".glsl")))
243 RefPtr<SpirVModule> module = new SpirVModule;
244 module->load_source(*io, this, name);
245 return module.release();
252 Program *Resources::create_program(const string &name)
254 string ext = FS::extpart(name);
255 string base = FS::basepart(name);
256 string ext2 = FS::extpart(base);
257 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
259 Module &module = get<Module>(base);
260 RefPtr<Program> shprog = new Program;
261 shprog->add_stages(module);
262 return shprog.release();
269 void Resources::set_debug_name(const string &name, T &item)
272 item.set_debug_name(name);
277 Resources::Loader::Loader(Resources &r):
278 DerivedObjectLoader<Resources, Collection::Loader>(r)
280 add("scene", &Loader::scene);
283 void Resources::Loader::scene(const string &name)
285 Scene::GenericLoader ldr(obj);
287 obj.add(name, ldr.get_object());