]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Load textures in a type-generic way
[libs/gl.git] / source / resources / resources.cpp
1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
3 #include "animation.h"
4 #include "armature.h"
5 #include "basicmaterial.h"
6 #include "camera.h"
7 #include "directionallight.h"
8 #include "error.h"
9 #include "font.h"
10 #include "keyframe.h"
11 #include "lighting.h"
12 #include "mesh.h"
13 #include "module.h"
14 #include "object.h"
15 #include "occludedscene.h"
16 #include "orderedscene.h"
17 #include "pbrmaterial.h"
18 #include "sequencetemplate.h"
19 #include "pointlight.h"
20 #include "pose.h"
21 #include "program.h"
22 #include "resourcemanager.h"
23 #include "resources.h"
24 #include "sampler.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"
34
35 using namespace std;
36
37 namespace Msp {
38 namespace GL {
39
40 void init_shaderlib(DataFile::BuiltinSource &);
41 void init_builtin_data(DataFile::BuiltinSource &);
42
43 Resources *Resources::global_resources = 0;
44
45 Resources::Resources(bool set_as_global):
46         srgb_conversion(false),
47         resource_manager(0)
48 {
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(".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>().suffix(".scene")
107                 .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
108
109         add_source(get_builtins());
110
111         if(set_as_global && !global_resources)
112                 global_resources = this;
113 }
114
115 Resources::~Resources()
116 {
117         if(this==global_resources)
118                 global_resources = 0;
119 }
120
121 Resources &Resources::get_global()
122 {
123         if(!global_resources)
124                 throw invalid_operation("no global resources");
125         return *global_resources;
126 }
127
128 const DataFile::CollectionSource &Resources::get_builtins()
129 {
130         static DataFile::BuiltinSource builtins;
131         bool init_done = false;
132
133         if(!init_done)
134         {
135                 init_builtin_data(builtins);
136                 init_shaderlib(builtins);
137                 init_done = true;
138         }
139
140         return builtins;
141 }
142
143 void Resources::set_srgb_conversion(bool c)
144 {
145         srgb_conversion = c;
146 }
147
148 void Resources::set_resource_manager(ResourceManager *m)
149 {
150         resource_manager = m;
151 }
152
153 template<typename T, typename L>
154 T *Resources::create_generic(const string &name)
155 {
156         if(RefPtr<IO::Seekable> io = open_raw(name))
157         {
158                 DataFile::Parser parser(*io, name);
159                 L ldr(*this);
160                 ldr.load(parser);
161                 ldr.store_object(*this, name);
162         }
163
164         return 0;
165 }
166
167 Mesh *Resources::create_mesh(const string &name)
168 {
169         if(!resource_manager)
170                 return 0;
171
172         if(RefPtr<IO::Seekable> io = open_raw(name))
173         {
174                 RefPtr<Mesh> mesh = new Mesh;
175                 mesh->set_manager(resource_manager);
176                 resource_manager->set_resource_location(*mesh, *this, name);
177                 return mesh.release();
178         }
179
180         return 0;
181 }
182
183 Texture *Resources::create_texture(const string &name)
184 {
185         string ext = FS::extpart(name);
186         if(ext==".tex")
187                 return create_generic<Texture, GenericResourceLoader<Texture>>(name);
188
189         if(RefPtr<IO::Seekable> io = open_raw(name))
190         {
191                 RefPtr<Texture2D> tex;
192
193                 // Verify that the image is loadable
194                 Graphics::Image image;
195                 if(!resource_manager)
196                         image.load_io(*io);
197
198                 tex = new Texture2D;
199                 tex->set_manager(resource_manager);
200
201                 if(resource_manager)
202                         resource_manager->set_resource_location(*tex, *this, name);
203                 else
204                         tex->image(image);
205
206                 add(name, tex.get());
207                 tex.release();
208         }
209
210         return 0;
211 }
212
213 Module *Resources::create_module(const string &name)
214 {
215         string ext = FS::extpart(name);
216         if(ext!=".glsl" && ext!=".spv")
217                 return 0;
218
219         if(RefPtr<IO::Seekable> io = open_raw(name))
220         {
221                 if(ext==".glsl")
222                 {
223                         RefPtr<GlslModule> module = new GlslModule;
224                         module->load_source(*io, this, name);
225                         return module.release();
226                 }
227                 else if(ext==".spv")
228                 {
229                         RefPtr<SpirVModule> module = new SpirVModule;
230                         module->load_code(*io);
231                         return module.release();
232                 }
233         }
234         else if(ext==".spv")
235         {
236                 if((io = open_raw(FS::basepart(name)+".glsl")))
237                 {
238                         RefPtr<SpirVModule> module = new SpirVModule;
239                         module->load_source(*io, this, name);
240                         return module.release();
241                 }
242         }
243
244         return 0;
245 }
246
247 Program *Resources::create_program(const string &name)
248 {
249         string ext = FS::extpart(name);
250         string base = FS::basepart(name);
251         string ext2 = FS::extpart(base);
252         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
253         {
254                 Module &module = get<Module>(base);
255                 RefPtr<Program> shprog = new Program;
256                 shprog->add_stages(module);
257                 return shprog.release();
258         }
259
260         return 0;
261 }
262
263 template<typename T>
264 void Resources::set_debug_name(const string &name, T &item)
265 {
266 #ifdef DEBUG
267         item.set_debug_name(name);
268 #endif
269 }
270
271
272 Resources::Loader::Loader(Resources &r):
273         DerivedObjectLoader<Resources, Collection::Loader>(r)
274 {
275         add("light", &Loader::generic<Light>);
276         add("material", &Loader::generic<Material>);
277         add("scene", &Loader::generic<Scene>);
278         add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
279 }
280
281 template<typename T, typename L>
282 void Resources::Loader::generic(const string &name)
283 {
284         L ldr(obj);
285         load_sub_with(ldr);
286         ldr.store_object(obj, name);
287 }
288
289
290 template<typename T>
291 void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
292 {
293         T::GenericLoader::type(t);
294         this->object->set_manager(resources.resource_manager);
295 }
296
297 } // namespace GL
298 } // namespace Msp