]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
ec5f431ddba048cf441b2024de70fc84737b92f3
[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;
171                 mesh->set_manager(resource_manager);
172                 resource_manager->set_resource_location(*mesh, *this, name);
173                 return mesh.release();
174         }
175
176         return 0;
177 }
178
179 Texture2D *Resources::create_texture2d(const string &name)
180 {
181         string ext = FS::extpart(name);
182         if(ext==".tex2d" && !resource_manager)
183                 return 0;
184
185         if(RefPtr<IO::Seekable> io = open_raw(name))
186         {
187                 RefPtr<Texture2D> tex;
188
189                 if(ext==".tex2d")
190                 {
191                         tex = new Texture2D;
192                         tex->set_manager(resource_manager);
193                         DataFile::Parser parser(*io, name);
194                         Texture2D::Loader ldr(*tex, *this);
195                         ldr.load(parser);
196                 }
197                 else
198                 {
199                         // Verify that the image is loadable
200                         Graphics::Image image;
201                         if(!resource_manager)
202                                 image.load_io(*io);
203
204                         tex = new Texture2D(resource_manager);
205
206                         if(resource_manager)
207                                 resource_manager->set_resource_location(*tex, *this, name);
208                         else
209                                 tex->image(image);
210                 }
211
212                 return tex.release();
213         }
214
215         return 0;
216 }
217
218 Module *Resources::create_module(const string &name)
219 {
220         string ext = FS::extpart(name);
221         if(ext!=".glsl" && ext!=".spv")
222                 return 0;
223
224         if(RefPtr<IO::Seekable> io = open_raw(name))
225         {
226                 if(ext==".glsl")
227                 {
228                         RefPtr<GlslModule> module = new GlslModule;
229                         module->load_source(*io, this, name);
230                         return module.release();
231                 }
232                 else if(ext==".spv")
233                 {
234                         RefPtr<SpirVModule> module = new SpirVModule;
235                         module->load_code(*io);
236                         return module.release();
237                 }
238         }
239         else if(ext==".spv")
240         {
241                 if((io = open_raw(FS::basepart(name)+".glsl")))
242                 {
243                         RefPtr<SpirVModule> module = new SpirVModule;
244                         module->load_source(*io, this, name);
245                         return module.release();
246                 }
247         }
248
249         return 0;
250 }
251
252 Program *Resources::create_program(const string &name)
253 {
254         string ext = FS::extpart(name);
255         string base = FS::basepart(name);
256         string ext2 = FS::extpart(base);
257         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
258         {
259                 Module &module = get<Module>(base);
260                 RefPtr<Program> shprog = new Program;
261                 shprog->add_stages(module);
262                 return shprog.release();
263         }
264
265         return 0;
266 }
267
268 template<typename T>
269 void Resources::set_debug_name(const string &name, T &item)
270 {
271 #ifdef DEBUG
272         item.set_debug_name(name);
273 #endif
274 }
275
276
277 Resources::Loader::Loader(Resources &r):
278         DerivedObjectLoader<Resources, Collection::Loader>(r)
279 {
280         add("scene", &Loader::scene);
281 }
282
283 void Resources::Loader::scene(const string &name)
284 {
285         Scene::GenericLoader ldr(obj);
286         load_sub_with(ldr);
287         obj.add(name, ldr.get_object());
288 }
289
290 } // namespace GL
291 } // namespace Msp