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