]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Split the Light class into subclasses by light type
[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(".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; });
104
105         add_source(get_builtins());
106
107         if(set_as_global && !global_resources)
108                 global_resources = this;
109 }
110
111 Resources::~Resources()
112 {
113         if(this==global_resources)
114                 global_resources = 0;
115 }
116
117 Resources &Resources::get_global()
118 {
119         if(!global_resources)
120                 throw invalid_operation("no global resources");
121         return *global_resources;
122 }
123
124 const DataFile::CollectionSource &Resources::get_builtins()
125 {
126         static DataFile::BuiltinSource builtins;
127         bool init_done = false;
128
129         if(!init_done)
130         {
131                 init_builtin_data(builtins);
132                 init_shaderlib(builtins);
133                 init_done = true;
134         }
135
136         return builtins;
137 }
138
139 void Resources::set_srgb_conversion(bool c)
140 {
141         srgb_conversion = c;
142 }
143
144 void Resources::set_resource_manager(ResourceManager *m)
145 {
146         resource_manager = m;
147 }
148
149 template<typename T>
150 T *Resources::create_generic(const string &name)
151 {
152         if(RefPtr<IO::Seekable> io = open_raw(name))
153         {
154                 DataFile::Parser parser(*io, name);
155                 typename T::GenericLoader ldr(*this);
156                 ldr.load(parser);
157                 ldr.store_object(*this, name);
158         }
159
160         return 0;
161 }
162
163 Mesh *Resources::create_mesh(const string &name)
164 {
165         if(!resource_manager)
166                 return 0;
167
168         if(RefPtr<IO::Seekable> io = open_raw(name))
169         {
170                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
171                 resource_manager->set_resource_location(*mesh, *this, name);
172                 return mesh.release();
173         }
174
175         return 0;
176 }
177
178 Texture2D *Resources::create_texture2d(const string &name)
179 {
180         string ext = FS::extpart(name);
181         if(ext==".tex2d" && !resource_manager)
182                 return 0;
183
184         if(RefPtr<IO::Seekable> io = open_raw(name))
185         {
186                 RefPtr<Texture2D> tex;
187
188                 if(ext==".tex2d")
189                 {
190                         tex = new Texture2D(resource_manager);
191                         DataFile::Parser parser(*io, name);
192                         Texture2D::Loader ldr(*tex, *this);
193                         ldr.load(parser);
194                 }
195                 else
196                 {
197                         // Verify that the image is loadable
198                         Graphics::Image image;
199                         if(!resource_manager)
200                                 image.load_io(*io);
201
202                         tex = new Texture2D(resource_manager);
203
204                         if(resource_manager)
205                                 resource_manager->set_resource_location(*tex, *this, name);
206                         else
207                                 tex->image(image);
208                 }
209
210                 return tex.release();
211         }
212
213         return 0;
214 }
215
216 Module *Resources::create_module(const string &name)
217 {
218         string ext = FS::extpart(name);
219         if(ext!=".glsl" && ext!=".spv")
220                 return 0;
221
222         if(RefPtr<IO::Seekable> io = open_raw(name))
223         {
224                 if(ext==".glsl")
225                 {
226                         RefPtr<GlslModule> module = new GlslModule;
227                         module->load_source(*io, this, name);
228                         return module.release();
229                 }
230                 else if(ext==".spv")
231                 {
232                         RefPtr<SpirVModule> module = new SpirVModule;
233                         module->load_code(*io);
234                         return module.release();
235                 }
236         }
237         else if(ext==".spv")
238         {
239                 if((io = open_raw(FS::basepart(name)+".glsl")))
240                 {
241                         RefPtr<SpirVModule> module = new SpirVModule;
242                         module->load_source(*io, this, name);
243                         return module.release();
244                 }
245         }
246
247         return 0;
248 }
249
250 Program *Resources::create_program(const string &name)
251 {
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"))
256         {
257                 Module &module = get<Module>(base);
258                 RefPtr<Program> shprog = new Program;
259                 shprog->add_stages(module);
260                 return shprog.release();
261         }
262
263         return 0;
264 }
265
266 template<typename T>
267 void Resources::set_debug_name(const string &name, T &item)
268 {
269 #ifdef DEBUG
270         item.set_debug_name(name);
271 #endif
272 }
273
274
275 Resources::Loader::Loader(Resources &r):
276         DerivedObjectLoader<Resources, Collection::Loader>(r)
277 {
278         add("scene", &Loader::scene);
279 }
280
281 void Resources::Loader::scene(const string &name)
282 {
283         Scene::GenericLoader ldr(obj);
284         load_sub_with(ldr);
285         obj.add(name, ldr.get_object());
286 }
287
288 } // namespace GL
289 } // namespace Msp