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 add_type<Object>().base<Renderable>().keyword("object");
68 add_type<OccludedScene>().base<Scene>().base<Renderable>().suffix(".scene")
69 .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
70 add_type<OrderedScene>().base<Scene>().base<Renderable>().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>().base<Renderable>().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(".tex")
89 .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; })
90 .notify(&set_debug_name<Texture1D>);
91 add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
92 .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; })
93 .notify(&set_debug_name<Texture2D>);
94 add_type<Texture3D>().base<Texture>().suffix(".tex")
95 .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; })
96 .notify(&set_debug_name<Texture3D>);
97 add_type<TextureCube>().base<Texture>().suffix(".tex")
98 .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; })
99 .notify(&set_debug_name<TextureCube>);
100 add_type<Texture2DArray>().base<Texture>().suffix(".tex")
101 .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; })
102 .notify(&set_debug_name<Texture2DArray>);
103 add_type<UnlitMaterial>().base<Material>().suffix(".mat")
104 .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
105 .notify(&set_debug_name<Material>);
106 add_type<ZSortedScene>().base<Scene>().base<Renderable>().suffix(".scene")
107 .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
109 add_source(get_builtins());
111 if(set_as_global && !global_resources)
112 global_resources = this;
115 Resources::~Resources()
117 if(this==global_resources)
118 global_resources = 0;
121 Resources &Resources::get_global()
123 if(!global_resources)
124 throw invalid_operation("no global resources");
125 return *global_resources;
128 const DataFile::CollectionSource &Resources::get_builtins()
130 static DataFile::BuiltinSource builtins;
131 bool init_done = false;
135 init_builtin_data(builtins);
136 init_shaderlib(builtins);
143 void Resources::set_resource_manager(ResourceManager *m)
145 resource_manager = m;
148 template<typename T, typename L>
149 T *Resources::create_generic(const string &name)
151 if(RefPtr<IO::Seekable> io = open_raw(name))
153 DataFile::Parser parser(*io, name);
156 ldr.store_object(*this, name);
162 Mesh *Resources::create_mesh(const string &name)
164 if(!resource_manager || name[0]=='_')
167 if(RefPtr<IO::Seekable> io = open_raw(name))
169 RefPtr<Mesh> mesh = new Mesh;
170 mesh->set_manager(resource_manager);
171 resource_manager->set_resource_location(*mesh, *this, name);
172 return mesh.release();
178 Texture *Resources::create_texture(const string &name)
180 bool managed = (resource_manager && name[0]!='_');
182 string ext = FS::extpart(name);
186 return create_generic<Texture, GenericResourceLoader<Texture>>(name);
188 return create_generic<Texture>(name);
191 if(RefPtr<IO::Seekable> io = open_raw(name))
193 RefPtr<Texture2D> tex;
195 // Verify that the image is loadable
196 Graphics::Image image;
204 tex->set_manager(resource_manager);
205 resource_manager->set_resource_location(*tex, *this, name);
210 add(name, tex.get());
217 Module *Resources::create_module(const string &name)
219 string ext = FS::extpart(name);
220 if(ext!=".glsl" && ext!=".spv")
223 if(RefPtr<IO::Seekable> io = open_raw(name))
227 RefPtr<Module> module;
228 if(get_backend_api()==VULKAN)
229 module = new SpirVModule;
231 module = new GlslModule;
232 module->load_source(*io, this, name);
233 return module.release();
237 RefPtr<SpirVModule> module = new SpirVModule;
238 module->load_code(*io);
239 return module.release();
244 if((io = open_raw(FS::basepart(name)+".glsl")))
246 RefPtr<SpirVModule> module = new SpirVModule;
247 module->load_source(*io, this, name);
248 return module.release();
255 Program *Resources::create_program(const string &name)
257 string ext = FS::extpart(name);
258 string base = FS::basepart(name);
259 string ext2 = FS::extpart(base);
260 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
262 Module &module = get<Module>(base);
263 RefPtr<Program> shprog = new Program;
264 shprog->add_stages(module);
265 return shprog.release();
272 void Resources::set_debug_name(const string &name, T &item)
275 item.set_debug_name(name);
280 Resources::Loader::Loader(Resources &r):
281 DerivedObjectLoader<Resources, Collection::Loader>(r)
283 add("light", &Loader::generic<Light>);
284 add("material", &Loader::generic<Material>);
285 add("scene", &Loader::generic<Scene>);
286 add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
289 template<typename T, typename L>
290 void Resources::Loader::generic(const string &name)
294 ldr.store_object(obj, name);
299 void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
301 T::GenericLoader::type(t);
302 this->object->set_manager(resources.resource_manager);