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):
48 add_type<Animation>().suffix(".anim").keyword("animation");
49 add_type<Armature>().suffix(".arma").keyword("armature");
50 add_type<BasicMaterial>().base<Material>().suffix(".mat")
51 .creator([this](const string &n) -> BasicMaterial * { create_generic<Material>(n); return 0; })
52 .notify(&set_debug_name<Material>);
53 add_type<Camera>().keyword("camera")
54 .notify(&set_debug_name<Camera>);
55 add_type<DirectionalLight>().base<Light>().suffix(".light")
56 .creator([this](const string &n) -> DirectionalLight * { create_generic<Light>(n); return 0; });
57 add_type<Font>().keyword("font");
58 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
59 add_type<Lighting>().suffix(".lightn").keyword("lighting")
60 .notify(&set_debug_name<Lighting>);
61 add_type<Mesh>().keyword("mesh")
62 .creator([this](const string &n){ return create_mesh(n); })
63 .notify(&set_debug_name<Mesh>);
64 add_type<Module>().suffix(".glsl").suffix(".spv")
65 .creator([this](const string &n){ return create_module(n); });
66 add_type<Object>().base<Renderable>().keyword("object");
67 add_type<OccludedScene>().base<Scene>().base<Renderable>().suffix(".scene")
68 .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
69 add_type<OrderedScene>().base<Scene>().base<Renderable>().suffix(".scene")
70 .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return 0; });
71 add_type<PbrMaterial>().base<Material>().suffix(".mat")
72 .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return 0; })
73 .notify(&set_debug_name<Material>);
74 add_type<PointLight>().base<Light>().suffix(".light")
75 .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return 0; });
76 add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
77 add_type<Pose>().keyword("pose");
78 add_type<Program>().keyword("shader")
79 .creator([this](const string &n){ return create_program(n); })
80 .notify(&set_debug_name<Program>);
81 add_type<Sampler>().suffix(".samp").keyword("sampler")
82 .notify(&set_debug_name<Sampler>);
83 add_type<SimpleScene>().base<Scene>().base<Renderable>().suffix(".scene")
84 .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
85 add_type<Technique>().suffix(".tech").keyword("technique")
86 .notify(&set_debug_name<Technique>);
87 add_type<Texture1D>().base<Texture>().suffix(".tex")
88 .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; })
89 .notify(&set_debug_name<Texture1D>);
90 add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
91 .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; })
92 .notify(&set_debug_name<Texture2D>);
93 add_type<Texture3D>().base<Texture>().suffix(".tex")
94 .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; })
95 .notify(&set_debug_name<Texture3D>);
96 add_type<TextureCube>().base<Texture>().suffix(".tex")
97 .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; })
98 .notify(&set_debug_name<TextureCube>);
99 add_type<Texture2DArray>().base<Texture>().suffix(".tex")
100 .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; })
101 .notify(&set_debug_name<Texture2DArray>);
102 add_type<UnlitMaterial>().base<Material>().suffix(".mat")
103 .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
104 .notify(&set_debug_name<Material>);
105 add_type<ZSortedScene>().base<Scene>().base<Renderable>().suffix(".scene")
106 .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
108 add_source(get_builtins());
110 if(set_as_global && !global_resources)
111 global_resources = this;
114 Resources::~Resources()
116 if(this==global_resources)
117 global_resources = 0;
120 Resources &Resources::get_global()
122 if(!global_resources)
123 throw invalid_operation("no global resources");
124 return *global_resources;
127 const DataFile::CollectionSource &Resources::get_builtins()
129 static DataFile::BuiltinSource builtins;
130 bool init_done = false;
134 init_builtin_data(builtins);
135 init_shaderlib(builtins);
142 void Resources::set_resource_manager(ResourceManager *m)
144 resource_manager = m;
147 template<typename T, typename L>
148 T *Resources::create_generic(const string &name)
150 if(RefPtr<IO::Seekable> io = open_raw(name))
152 DataFile::Parser parser(*io, name);
155 ldr.store_object(*this, name);
161 Mesh *Resources::create_mesh(const string &name)
163 if(!resource_manager || name[0]=='_')
166 if(RefPtr<IO::Seekable> io = open_raw(name))
168 RefPtr<Mesh> mesh = new Mesh;
169 mesh->set_manager(resource_manager);
170 resource_manager->set_resource_location(*mesh, *this, name);
171 return mesh.release();
177 Texture *Resources::create_texture(const string &name)
179 bool managed = (resource_manager && name[0]!='_');
181 string ext = FS::extpart(name);
185 return create_generic<Texture, GenericResourceLoader<Texture>>(name);
187 return create_generic<Texture>(name);
190 if(RefPtr<IO::Seekable> io = open_raw(name))
192 RefPtr<Texture2D> tex;
194 // Verify that the image is loadable
195 Graphics::Image image;
203 tex->set_manager(resource_manager);
204 resource_manager->set_resource_location(*tex, *this, name);
209 add(name, tex.get());
216 Module *Resources::create_module(const string &name)
218 string ext = FS::extpart(name);
219 if(ext!=".glsl" && ext!=".spv")
222 if(RefPtr<IO::Seekable> io = open_raw(name))
226 RefPtr<GlslModule> module = new GlslModule;
227 module->load_source(*io, this, name);
228 return module.release();
232 RefPtr<SpirVModule> module = new SpirVModule;
233 module->load_code(*io);
234 return module.release();
239 if((io = open_raw(FS::basepart(name)+".glsl")))
241 RefPtr<SpirVModule> module = new SpirVModule;
242 module->load_source(*io, this, name);
243 return module.release();
250 Program *Resources::create_program(const string &name)
252 string ext = FS::extpart(name);
253 string base = FS::basepart(name);
254 string ext2 = FS::extpart(base);
255 if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
257 Module &module = get<Module>(base);
258 RefPtr<Program> shprog = new Program;
259 shprog->add_stages(module);
260 return shprog.release();
267 void Resources::set_debug_name(const string &name, T &item)
270 item.set_debug_name(name);
275 Resources::Loader::Loader(Resources &r):
276 DerivedObjectLoader<Resources, Collection::Loader>(r)
278 add("light", &Loader::generic<Light>);
279 add("material", &Loader::generic<Material>);
280 add("scene", &Loader::generic<Scene>);
281 add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
284 template<typename T, typename L>
285 void Resources::Loader::generic(const string &name)
289 ldr.store_object(obj, name);
294 void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
296 T::GenericLoader::type(t);
297 this->object->set_manager(resources.resource_manager);