]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Remove default sampler from Texture
[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(&Resources::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(&Resources::set_debug_name<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_srgb_conversion(bool c)
115 {
116         srgb_conversion = c;
117 }
118
119 void Resources::set_resource_manager(ResourceManager *m)
120 {
121         resource_manager = m;
122 }
123
124 Material *Resources::create_material(const string &name)
125 {
126         if(RefPtr<IO::Seekable> io = open_raw(name))
127         {
128                 DataFile::Parser parser(*io, name);
129                 Material::GenericLoader ldr(this);
130                 ldr.load(parser);
131                 return ldr.get_material();
132         }
133
134         return 0;
135 }
136
137 Mesh *Resources::create_mesh(const string &name)
138 {
139         if(!resource_manager)
140                 return 0;
141
142         if(RefPtr<IO::Seekable> io = open_raw(name))
143         {
144                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
145                 resource_manager->set_resource_location(*mesh, *this, name);
146                 return mesh.release();
147         }
148
149         return 0;
150 }
151
152 Scene *Resources::create_scene(const string &name)
153 {
154         if(RefPtr<IO::Seekable> io = open_raw(name))
155         {
156                 DataFile::Parser parser(*io, name);
157                 Scene::GenericLoader ldr(*this);
158                 ldr.load(parser);
159                 return ldr.get_scene();
160         }
161
162         return 0;
163 }
164
165 Texture2D *Resources::create_texture2d(const string &name)
166 {
167         string ext = FS::extpart(name);
168         if(ext==".tex2d" && !resource_manager)
169                 return 0;
170
171         if(RefPtr<IO::Seekable> io = open_raw(name))
172         {
173                 RefPtr<Texture2D> tex;
174
175                 if(ext==".tex2d")
176                 {
177                         tex = new Texture2D(resource_manager);
178                         DataFile::Parser parser(*io, name);
179                         Texture2D::Loader ldr(*tex, *this);
180                         ldr.load(parser);
181                 }
182                 else
183                 {
184                         // Verify that the image is loadable
185                         Graphics::Image image;
186                         if(!resource_manager)
187                                 image.load_io(*io);
188
189                         tex = new Texture2D(resource_manager);
190
191                         if(resource_manager)
192                                 resource_manager->set_resource_location(*tex, *this, name);
193                         else
194                                 tex->image(image);
195                 }
196
197                 return tex.release();
198         }
199
200         return 0;
201 }
202
203 Module *Resources::create_module(const string &name)
204 {
205         string ext = FS::extpart(name);
206         if(ext!=".glsl" && ext!=".spv")
207                 return 0;
208
209         if(RefPtr<IO::Seekable> io = open_raw(name))
210         {
211                 if(ext==".glsl")
212                 {
213                         RefPtr<GlslModule> module = new GlslModule;
214                         module->load_source(*io, this, name);
215                         return module.release();
216                 }
217                 else if(ext==".spv")
218                 {
219                         RefPtr<SpirVModule> module = new SpirVModule;
220                         module->load_code(*io);
221                         return module.release();
222                 }
223         }
224         else if(ext==".spv")
225         {
226                 if((io = open_raw(FS::basepart(name)+".glsl")))
227                 {
228                         RefPtr<SpirVModule> module = new SpirVModule;
229                         module->load_source(*io, this, name);
230                         return module.release();
231                 }
232         }
233
234         return 0;
235 }
236
237 Program *Resources::create_program(const string &name)
238 {
239         string ext = FS::extpart(name);
240         string base = FS::basepart(name);
241         string ext2 = FS::extpart(base);
242         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
243         {
244                 Module &module = get<Module>(base);
245                 RefPtr<Program> shprog = new Program;
246                 shprog->add_stages(module);
247                 shprog->link();
248                 return shprog.release();
249         }
250
251         return 0;
252 }
253
254 template<typename T>
255 void Resources::set_debug_name(const string &name, T &item)
256 {
257 #ifdef DEBUG
258         item.set_debug_name(name);
259 #endif
260 }
261
262
263 Resources::Loader::Loader(Resources &r):
264         DerivedObjectLoader<Resources, Collection::Loader>(r)
265 {
266         add("scene", &Loader::scene);
267 }
268
269 void Resources::Loader::scene(const string &name)
270 {
271         Scene::GenericLoader ldr(obj);
272         load_sub_with(ldr);
273         obj.add(name, ldr.get_scene());
274 }
275
276 } // namespace GL
277 } // namespace Msp