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