1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
6 #include "basicmaterial.h"
8 #include "directionallight.h"
16 #include "occludedscene.h"
17 #include "orderedscene.h"
18 #include "pbrmaterial.h"
19 #include "sequencetemplate.h"
20 #include "pointlight.h"
23 #include "resourcemanager.h"
24 #include "resources.h"
26 #include "simplescene.h"
27 #include "technique.h"
28 #include "texture1d.h"
29 #include "texture2d.h"
30 #include "texture2darray.h"
31 #include "texturecube.h"
32 #include "unlitmaterial.h"
33 #include "zsortedscene.h"
34 #include "glsl/compiler.h"
41 void init_shaderlib(DataFile::BuiltinSource &);
42 void init_builtin_data(DataFile::BuiltinSource &);
44 Resources *Resources::global_resources = 0;
46 Resources::Resources(bool set_as_global):
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 .notify(&set_debug_name<Module>);
68 add_type<Object>().base<Renderable>().keyword("object");
69 add_type<OccludedScene>().base<Scene>().base<Renderable>().suffix(".scene")
70 .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
71 add_type<OrderedScene>().base<Scene>().base<Renderable>().suffix(".scene")
72 .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return 0; });
73 add_type<PbrMaterial>().base<Material>().suffix(".mat")
74 .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return 0; })
75 .notify(&set_debug_name<Material>);
76 add_type<PointLight>().base<Light>().suffix(".light")
77 .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return 0; });
78 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
79 add_type<Pose>().keyword("pose");
80 add_type<Program>().keyword("shader")
81 .creator([this](const string &n){ return create_program(n); })
82 .notify(&set_debug_name<Program>);
83 add_type<Sampler>().suffix(".samp").keyword("sampler")
84 .notify(&set_debug_name<Sampler>);
85 add_type<SimpleScene>().base<Scene>().base<Renderable>().suffix(".scene")
86 .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
87 add_type<Technique>().suffix(".tech").keyword("technique")
88 .notify(&set_debug_name<Technique>);
89 add_type<Texture1D>().base<Texture>().suffix(".tex")
90 .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; })
91 .notify(&set_debug_name<Texture1D>);
92 add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
93 .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; })
94 .notify(&set_debug_name<Texture2D>);
95 add_type<Texture3D>().base<Texture>().suffix(".tex")
96 .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; })
97 .notify(&set_debug_name<Texture3D>);
98 add_type<TextureCube>().base<Texture>().suffix(".tex")
99 .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; })
100 .notify(&set_debug_name<TextureCube>);
101 add_type<Texture2DArray>().base<Texture>().suffix(".tex")
102 .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; })
103 .notify(&set_debug_name<Texture2DArray>);
104 add_type<UnlitMaterial>().base<Material>().suffix(".mat")
105 .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
106 .notify(&set_debug_name<Material>);
107 add_type<ZSortedScene>().base<Scene>().base<Renderable>().suffix(".scene")
108 .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
110 add_source(get_builtins());
112 if(set_as_global && !global_resources)
113 global_resources = this;
116 Resources::~Resources()
118 if(this==global_resources)
119 global_resources = 0;
122 Resources &Resources::get_global()
124 if(!global_resources)
125 throw invalid_operation("no global resources");
126 return *global_resources;
129 const DataFile::CollectionSource &Resources::get_builtins()
131 static DataFile::BuiltinSource builtins;
132 bool init_done = false;
136 init_builtin_data(builtins);
137 init_shaderlib(builtins);
144 void Resources::set_resource_manager(ResourceManager *m)
146 resource_manager = m;
149 template<typename T, typename L>
150 T *Resources::create_generic(const string &name)
152 if(RefPtr<IO::Seekable> io = open_raw(name))
154 DataFile::Parser parser(*io, name);
157 ldr.store_object(*this, name);
163 Mesh *Resources::create_mesh(const string &name)
165 if(!resource_manager || name[0]=='_')
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 Texture *Resources::create_texture(const string &name)
181 bool managed = (resource_manager && name[0]!='_');
183 string ext = FS::extpart(name);
187 return create_generic<Texture, GenericResourceLoader<Texture>>(name);
189 return create_generic<Texture>(name);
192 if(RefPtr<IO::Seekable> io = open_raw(name))
194 RefPtr<Texture2D> tex;
196 // Verify that the image is loadable
197 Graphics::Image image;
205 tex->set_manager(resource_manager);
206 resource_manager->set_resource_location(*tex, *this, name);
211 add(name, tex.get());
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<Module> module;
229 if(get_backend_api()==VULKAN)
230 module = new SpirVModule;
232 module = new GlslModule;
233 module->load_source(*io, this, name);
234 return module.release();
238 RefPtr<SpirVModule> module = new SpirVModule;
239 module->load_code(*io);
240 return module.release();
245 if((io = open_raw(FS::basepart(name)+".glsl")))
247 RefPtr<SpirVModule> module = new SpirVModule;
248 module->load_source(*io, this, name);
249 return module.release();
256 Program *Resources::create_program(const string &name)
258 string ext = FS::extpart(name);
259 string base = FS::basepart(name);
260 string ext2 = FS::extpart(base);
261 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
263 Module &module = get<Module>(base);
264 RefPtr<Program> shprog = new Program;
265 shprog->add_stages(module);
266 return shprog.release();
273 void Resources::set_debug_name(const string &name, T &item)
276 item.set_debug_name(name);
281 Resources::Loader::Loader(Resources &r):
282 DerivedObjectLoader<Resources, Collection::Loader>(r)
284 add("light", &Loader::generic<Light>);
285 add("material", &Loader::generic<Material>);
286 add("scene", &Loader::generic<Scene>);
287 add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
290 template<typename T, typename L>
291 void Resources::Loader::generic(const string &name)
295 ldr.store_object(obj, name);
300 void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
302 T::GenericLoader::type(t);
303 this->object->set_manager(resources.resource_manager);