]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Unify the loader wrappers for Material and Scene
[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 "camera.h"
6 #include "error.h"
7 #include "font.h"
8 #include "keyframe.h"
9 #include "light.h"
10 #include "lighting.h"
11 #include "material.h"
12 #include "mesh.h"
13 #include "module.h"
14 #include "object.h"
15 #include "sequencetemplate.h"
16 #include "pose.h"
17 #include "program.h"
18 #include "resourcemanager.h"
19 #include "resources.h"
20 #include "sampler.h"
21 #include "scene.h"
22 #include "technique.h"
23 #include "texture1d.h"
24 #include "texture2d.h"
25 #include "texture2darray.h"
26 #include "texturecube.h"
27 #include "glsl/compiler.h"
28
29 using namespace std;
30
31 namespace Msp {
32 namespace GL {
33
34 void init_shaderlib(DataFile::BuiltinSource &);
35 void init_builtin_data(DataFile::BuiltinSource &);
36
37 Resources *Resources::global_resources = 0;
38
39 Resources::Resources(bool set_as_global):
40         srgb_conversion(false),
41         resource_manager(0)
42 {
43         add_type<Animation>().suffix(".anim").keyword("animation");
44         add_type<Armature>().suffix(".arma").keyword("armature");
45         add_type<Camera>().keyword("camera")
46                 .notify(&Resources::set_debug_name<Camera>);
47         add_type<Font>().keyword("font");
48         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
49         add_type<Light>().keyword("light");
50         add_type<Lighting>().suffix(".lightn").keyword("lighting")
51                 .notify(&Resources::set_debug_name<Lighting>);
52         add_type<Material>().suffix(".mat")
53                 .creator(&Resources::create_generic<Material>).notify(&Resources::set_debug_name<Material>);
54         add_type<Mesh>().keyword("mesh")
55                 .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
56         add_type<Module>().suffix(".glsl").suffix(".spv")
57                 .creator(&Resources::create_module);
58         add_type<Object>().keyword("object");
59         add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
60         add_type<Pose>().keyword("pose");
61         add_type<Program>().keyword("shader")
62                 .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
63         add_type<Sampler>().suffix(".samp").keyword("sampler")
64                 .notify(&Resources::set_debug_name<Sampler>);
65         add_type<Scene>().suffix(".scene")
66                 .creator(&Resources::create_generic<Scene>);
67         add_type<Technique>().suffix(".tech").keyword("technique")
68                 .notify(&Resources::set_debug_name<Technique>);
69         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
70                 .notify(&Resources::set_debug_name<Texture1D>);
71         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
72                 .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
73         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
74                 .notify(&Resources::set_debug_name<Texture3D>);
75         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
76                 .notify(&Resources::set_debug_name<TextureCube>);
77         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
78                 .notify(&Resources::set_debug_name<Texture2DArray>);
79
80         add_source(get_builtins());
81
82         if(set_as_global && !global_resources)
83                 global_resources = this;
84 }
85
86 Resources::~Resources()
87 {
88         if(this==global_resources)
89                 global_resources = 0;
90 }
91
92 Resources &Resources::get_global()
93 {
94         if(!global_resources)
95                 throw invalid_operation("no global resources");
96         return *global_resources;
97 }
98
99 const DataFile::CollectionSource &Resources::get_builtins()
100 {
101         static DataFile::BuiltinSource builtins;
102         bool init_done = false;
103
104         if(!init_done)
105         {
106                 init_builtin_data(builtins);
107                 init_shaderlib(builtins);
108                 init_done = true;
109         }
110
111         return builtins;
112 }
113
114 void Resources::set_srgb_conversion(bool c)
115 {
116         srgb_conversion = c;
117 }
118
119 void Resources::set_resource_manager(ResourceManager *m)
120 {
121         resource_manager = m;
122 }
123
124 template<typename T>
125 T *Resources::create_generic(const string &name)
126 {
127         if(RefPtr<IO::Seekable> io = open_raw(name))
128         {
129                 DataFile::Parser parser(*io, name);
130                 typename T::GenericLoader ldr(*this);
131                 ldr.load(parser);
132                 return ldr.get_object();
133         }
134
135         return 0;
136 }
137
138 Mesh *Resources::create_mesh(const string &name)
139 {
140         if(!resource_manager)
141                 return 0;
142
143         if(RefPtr<IO::Seekable> io = open_raw(name))
144         {
145                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
146                 resource_manager->set_resource_location(*mesh, *this, name);
147                 return mesh.release();
148         }
149
150         return 0;
151 }
152
153 Texture2D *Resources::create_texture2d(const string &name)
154 {
155         string ext = FS::extpart(name);
156         if(ext==".tex2d" && !resource_manager)
157                 return 0;
158
159         if(RefPtr<IO::Seekable> io = open_raw(name))
160         {
161                 RefPtr<Texture2D> tex;
162
163                 if(ext==".tex2d")
164                 {
165                         tex = new Texture2D(resource_manager);
166                         DataFile::Parser parser(*io, name);
167                         Texture2D::Loader ldr(*tex, *this);
168                         ldr.load(parser);
169                 }
170                 else
171                 {
172                         // Verify that the image is loadable
173                         Graphics::Image image;
174                         if(!resource_manager)
175                                 image.load_io(*io);
176
177                         tex = new Texture2D(resource_manager);
178
179                         if(resource_manager)
180                                 resource_manager->set_resource_location(*tex, *this, name);
181                         else
182                                 tex->image(image);
183                 }
184
185                 return tex.release();
186         }
187
188         return 0;
189 }
190
191 Module *Resources::create_module(const string &name)
192 {
193         string ext = FS::extpart(name);
194         if(ext!=".glsl" && ext!=".spv")
195                 return 0;
196
197         if(RefPtr<IO::Seekable> io = open_raw(name))
198         {
199                 if(ext==".glsl")
200                 {
201                         RefPtr<GlslModule> module = new GlslModule;
202                         module->load_source(*io, this, name);
203                         return module.release();
204                 }
205                 else if(ext==".spv")
206                 {
207                         RefPtr<SpirVModule> module = new SpirVModule;
208                         module->load_code(*io);
209                         return module.release();
210                 }
211         }
212         else if(ext==".spv")
213         {
214                 if((io = open_raw(FS::basepart(name)+".glsl")))
215                 {
216                         RefPtr<SpirVModule> module = new SpirVModule;
217                         module->load_source(*io, this, name);
218                         return module.release();
219                 }
220         }
221
222         return 0;
223 }
224
225 Program *Resources::create_program(const string &name)
226 {
227         string ext = FS::extpart(name);
228         string base = FS::basepart(name);
229         string ext2 = FS::extpart(base);
230         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
231         {
232                 Module &module = get<Module>(base);
233                 RefPtr<Program> shprog = new Program;
234                 shprog->add_stages(module);
235                 return shprog.release();
236         }
237
238         return 0;
239 }
240
241 template<typename T>
242 void Resources::set_debug_name(const string &name, T &item)
243 {
244 #ifdef DEBUG
245         item.set_debug_name(name);
246 #endif
247 }
248
249
250 Resources::Loader::Loader(Resources &r):
251         DerivedObjectLoader<Resources, Collection::Loader>(r)
252 {
253         add("scene", &Loader::scene);
254 }
255
256 void Resources::Loader::scene(const string &name)
257 {
258         Scene::GenericLoader ldr(obj);
259         load_sub_with(ldr);
260         obj.add(name, ldr.get_object());
261 }
262
263 } // namespace GL
264 } // namespace Msp