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