]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Use persistent uniform blocks for Camera, Lighting and Clipping
[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                 .notify(&Resources::set_debug_name<Camera>);
49         add_type<Font>().keyword("font");
50         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
51         add_type<Light>().keyword("light");
52         add_type<Lighting>().suffix(".lightn").keyword("lighting")
53                 .notify(&Resources::set_debug_name<Lighting>);
54         add_type<Material>().suffix(".mat")
55                 .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
56         add_type<Mesh>().keyword("mesh")
57                 .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
58         add_type<Module>().suffix(".glsl").suffix(".spv")
59                 .creator(&Resources::create_module);
60         add_type<Object>().keyword("object");
61         add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
62         add_type<Pose>().keyword("pose");
63         add_type<Program>().keyword("shader")
64                 .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
65         add_type<Sampler>().suffix(".samp").keyword("sampler")
66                 .notify(&Resources::set_debug_name<Sampler>);
67         add_type<Scene>().suffix(".scene")
68                 .creator(&Resources::create_scene);
69         add_type<Technique>().suffix(".tech").keyword("technique")
70                 .notify(&Resources::set_debug_name<Technique>);
71         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
72                 .notify(&Resources::set_debug_name<Texture1D>);
73         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
74                 .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
75         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
76                 .notify(&Resources::set_debug_name<Texture3D>);
77         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
78                 .notify(&Resources::set_debug_name<TextureCube>);
79         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
80                 .notify(&Resources::set_debug_name<Texture2DArray>);
81
82         add_source(get_builtins());
83
84         if(set_as_global && !global_resources)
85                 global_resources = this;
86 }
87
88 Resources::~Resources()
89 {
90         if(this==global_resources)
91                 global_resources = 0;
92 }
93
94 Resources &Resources::get_global()
95 {
96         if(!global_resources)
97                 throw invalid_operation("no global resources");
98         return *global_resources;
99 }
100
101 const DataFile::CollectionSource &Resources::get_builtins()
102 {
103         static DataFile::BuiltinSource builtins;
104         bool init_done = false;
105
106         if(!init_done)
107         {
108                 init_builtin_data(builtins);
109                 init_shaderlib(builtins);
110                 init_done = true;
111         }
112
113         return builtins;
114 }
115
116 void Resources::set_default_texture_filter(TextureFilter tf)
117 {
118         default_tex_filter = tf;
119 }
120
121 void Resources::set_default_texture_anisotropy(float a)
122 {
123         default_tex_anisotropy = a;
124 }
125
126 void Resources::set_srgb_conversion(bool c)
127 {
128         srgb_conversion = c;
129 }
130
131 void Resources::set_resource_manager(ResourceManager *m)
132 {
133         resource_manager = m;
134 }
135
136 Material *Resources::create_material(const string &name)
137 {
138         if(RefPtr<IO::Seekable> io = open_raw(name))
139         {
140                 DataFile::Parser parser(*io, name);
141                 Material::GenericLoader ldr(this);
142                 ldr.load(parser);
143                 return ldr.get_material();
144         }
145
146         return 0;
147 }
148
149 Mesh *Resources::create_mesh(const string &name)
150 {
151         if(!resource_manager)
152                 return 0;
153
154         if(RefPtr<IO::Seekable> io = open_raw(name))
155         {
156                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
157                 resource_manager->set_resource_location(*mesh, *this, name);
158                 return mesh.release();
159         }
160
161         return 0;
162 }
163
164 Scene *Resources::create_scene(const string &name)
165 {
166         if(RefPtr<IO::Seekable> io = open_raw(name))
167         {
168                 DataFile::Parser parser(*io, name);
169                 Scene::GenericLoader ldr(*this);
170                 ldr.load(parser);
171                 return ldr.get_scene();
172         }
173
174         return 0;
175 }
176
177 Texture2D *Resources::create_texture2d(const string &name)
178 {
179         string ext = FS::extpart(name);
180         if(ext==".tex2d" && !resource_manager)
181                 return 0;
182
183         if(RefPtr<IO::Seekable> io = open_raw(name))
184         {
185                 RefPtr<Texture2D> tex;
186
187                 if(ext==".tex2d")
188                 {
189                         tex = new Texture2D(resource_manager);
190                         DataFile::Parser parser(*io, name);
191                         Texture2D::Loader ldr(*tex, *this);
192                         ldr.load(parser);
193                 }
194                 else
195                 {
196                         // Verify that the image is loadable
197                         Graphics::Image image;
198                         if(!resource_manager)
199                                 image.load_io(*io);
200
201                         tex = new Texture2D(resource_manager);
202                         Sampler &samp = tex->get_default_sampler();
203                         if(is_mipmapped(default_tex_filter))
204                         {
205                                 tex->set_auto_generate_mipmap(true);
206                                 samp.set_mag_filter(LINEAR);
207                         }
208                         else
209                                 samp.set_mag_filter(default_tex_filter);
210                         samp.set_min_filter(default_tex_filter);
211                         samp.set_max_anisotropy(default_tex_anisotropy);
212
213                         if(resource_manager)
214                                 resource_manager->set_resource_location(*tex, *this, name);
215                         else
216                                 tex->image(image);
217                 }
218
219                 return tex.release();
220         }
221
222         return 0;
223 }
224
225 Module *Resources::create_module(const string &name)
226 {
227         string ext = FS::extpart(name);
228         if(ext!=".glsl" && ext!=".spv")
229                 return 0;
230
231         if(RefPtr<IO::Seekable> io = open_raw(name))
232         {
233                 if(ext==".glsl")
234                 {
235                         RefPtr<GlslModule> module = new GlslModule;
236                         module->load_source(*io, this, name);
237                         return module.release();
238                 }
239                 else if(ext==".spv")
240                 {
241                         RefPtr<SpirVModule> module = new SpirVModule;
242                         module->load_code(*io);
243                         return module.release();
244                 }
245         }
246         else if(ext==".spv")
247         {
248                 if((io = open_raw(FS::basepart(name)+".glsl")))
249                 {
250                         RefPtr<SpirVModule> module = new SpirVModule;
251                         module->load_source(*io, this, name);
252                         return module.release();
253                 }
254         }
255
256         return 0;
257 }
258
259 Program *Resources::create_program(const string &name)
260 {
261         string ext = FS::extpart(name);
262         string base = FS::basepart(name);
263         string ext2 = FS::extpart(base);
264         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
265         {
266                 Module &module = get<Module>(base);
267                 RefPtr<Program> shprog = new Program;
268                 shprog->add_stages(module);
269                 shprog->link();
270                 return shprog.release();
271         }
272
273         return 0;
274 }
275
276 template<typename T>
277 void Resources::set_debug_name(const string &name, T &item)
278 {
279 #ifdef DEBUG
280         item.set_debug_name(name);
281 #endif
282 }
283
284
285 Resources::Loader::Loader(Resources &r):
286         DerivedObjectLoader<Resources, Collection::Loader>(r)
287 {
288         add("scene", &Loader::scene);
289 }
290
291 void Resources::Loader::scene(const string &name)
292 {
293         Scene::GenericLoader ldr(obj);
294         load_sub_with(ldr);
295         obj.add(name, ldr.get_scene());
296 }
297
298 } // namespace GL
299 } // namespace Msp