]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Access builtin resources through a global instance
[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         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
41         default_tex_anisotropy(1.0f),
42         srgb_conversion(false),
43         resource_manager(0)
44 {
45         add_type<Animation>().suffix(".anim").keyword("animation");
46         add_type<Armature>().suffix(".arma").keyword("armature");
47         add_type<Camera>().keyword("camera");
48         add_type<Font>().keyword("font");
49         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
50         add_type<Light>().keyword("light");
51         add_type<Lighting>().suffix(".lightn").keyword("lighting");
52         add_type<Material>().suffix(".mat")
53                 .creator(&Resources::create_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_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_default_texture_filter(TextureFilter tf)
115 {
116         default_tex_filter = tf;
117 }
118
119 void Resources::set_default_texture_anisotropy(float a)
120 {
121         default_tex_anisotropy = a;
122 }
123
124 void Resources::set_srgb_conversion(bool c)
125 {
126         srgb_conversion = c;
127 }
128
129 void Resources::set_resource_manager(ResourceManager *m)
130 {
131         resource_manager = m;
132 }
133
134 Material *Resources::create_material(const string &name)
135 {
136         if(RefPtr<IO::Seekable> io = open_raw(name))
137         {
138                 DataFile::Parser parser(*io, name);
139                 Material::GenericLoader ldr(this);
140                 ldr.load(parser);
141                 return ldr.get_material();
142         }
143
144         return 0;
145 }
146
147 Mesh *Resources::create_mesh(const string &name)
148 {
149         if(!resource_manager)
150                 return 0;
151
152         if(RefPtr<IO::Seekable> io = open_raw(name))
153         {
154                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
155                 resource_manager->set_resource_location(*mesh, *this, name);
156                 return mesh.release();
157         }
158
159         return 0;
160 }
161
162 Scene *Resources::create_scene(const string &name)
163 {
164         if(RefPtr<IO::Seekable> io = open_raw(name))
165         {
166                 DataFile::Parser parser(*io, name);
167                 Scene::GenericLoader ldr(*this);
168                 ldr.load(parser);
169                 return ldr.get_scene();
170         }
171
172         return 0;
173 }
174
175 Texture2D *Resources::create_texture2d(const string &name)
176 {
177         string ext = FS::extpart(name);
178         if(ext==".tex2d" && !resource_manager)
179                 return 0;
180
181         if(RefPtr<IO::Seekable> io = open_raw(name))
182         {
183                 RefPtr<Texture2D> tex;
184
185                 if(ext==".tex2d")
186                 {
187                         tex = new Texture2D(resource_manager);
188                         DataFile::Parser parser(*io, name);
189                         Texture2D::Loader ldr(*tex, *this);
190                         ldr.load(parser);
191                 }
192                 else
193                 {
194                         // Verify that the image is loadable
195                         Graphics::Image image;
196                         if(!resource_manager)
197                                 image.load_io(*io);
198
199                         tex = new Texture2D(resource_manager);
200                         Sampler &samp = tex->get_default_sampler();
201                         if(is_mipmapped(default_tex_filter))
202                         {
203                                 tex->set_auto_generate_mipmap(true);
204                                 samp.set_mag_filter(LINEAR);
205                         }
206                         else
207                                 samp.set_mag_filter(default_tex_filter);
208                         samp.set_min_filter(default_tex_filter);
209                         samp.set_max_anisotropy(default_tex_anisotropy);
210
211                         if(resource_manager)
212                                 resource_manager->set_resource_location(*tex, *this, name);
213                         else
214                                 tex->image(image);
215                 }
216
217                 return tex.release();
218         }
219
220         return 0;
221 }
222
223 Module *Resources::create_module(const string &name)
224 {
225         string ext = FS::extpart(name);
226         if(ext!=".glsl" && ext!=".spv")
227                 return 0;
228
229         if(RefPtr<IO::Seekable> io = open_raw(name))
230         {
231                 if(ext==".glsl")
232                 {
233                         RefPtr<GlslModule> module = new GlslModule;
234                         module->load_source(*io, this, name);
235                         return module.release();
236                 }
237                 else if(ext==".spv")
238                 {
239                         RefPtr<SpirVModule> module = new SpirVModule;
240                         module->load_code(*io);
241                         return module.release();
242                 }
243         }
244         else if(ext==".spv")
245         {
246                 if((io = open_raw(FS::basepart(name)+".glsl")))
247                 {
248                         RefPtr<SpirVModule> module = new SpirVModule;
249                         module->load_source(*io, this, name);
250                         return module.release();
251                 }
252         }
253
254         return 0;
255 }
256
257 Program *Resources::create_program(const string &name)
258 {
259         string ext = FS::extpart(name);
260         string base = FS::basepart(name);
261         string ext2 = FS::extpart(base);
262         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
263         {
264                 Module &module = get<Module>(base);
265                 RefPtr<Program> shprog = new Program;
266                 shprog->add_stages(module);
267                 shprog->link();
268                 return shprog.release();
269         }
270
271         return 0;
272 }
273
274 template<typename T>
275 void Resources::set_debug_name(const string &name, T &item)
276 {
277 #ifdef DEBUG
278         item.set_debug_name(name);
279 #endif
280 }
281
282
283 Resources::Loader::Loader(Resources &r):
284         DerivedObjectLoader<Resources, Collection::Loader>(r)
285 {
286         add("scene", &Loader::scene);
287 }
288
289 void Resources::Loader::scene(const string &name)
290 {
291         Scene::GenericLoader ldr(obj);
292         load_sub_with(ldr);
293         obj.add(name, ldr.get_scene());
294 }
295
296 } // namespace GL
297 } // namespace Msp