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