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(resource_manager);
171 resource_manager->set_resource_location(*mesh, *this, name);
172 return mesh.release();
178 Texture2D *Resources::create_texture2d(const string &name)
180 string ext = FS::extpart(name);
181 if(ext==".tex2d" && !resource_manager)
184 if(RefPtr<IO::Seekable> io = open_raw(name))
186 RefPtr<Texture2D> tex;
190 tex = new Texture2D(resource_manager);
191 DataFile::Parser parser(*io, name);
192 Texture2D::Loader ldr(*tex, *this);
197 // Verify that the image is loadable
198 Graphics::Image image;
199 if(!resource_manager)
202 tex = new Texture2D(resource_manager);
205 resource_manager->set_resource_location(*tex, *this, name);
210 return tex.release();
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("scene", &Loader::scene);
281 void Resources::Loader::scene(const string &name)
283 Scene::GenericLoader ldr(obj);
285 obj.add(name, ldr.get_object());