]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Redesign the way shader programs are loaded
[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 "lighting.h"
9 #include "material.h"
10 #include "mesh.h"
11 #include "module.h"
12 #include "object.h"
13 #include "pipelinetemplate.h"
14 #include "pose.h"
15 #include "program.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
18 #include "sampler.h"
19 #include "technique.h"
20 #include "texture1d.h"
21 #include "texture2d.h"
22 #include "texture2darray.h"
23 #include "texturecube.h"
24 #include "glsl/compiler.h"
25
26 using namespace std;
27
28 namespace Msp {
29 namespace GL {
30
31 void init_shaderlib(DataFile::BuiltinSource &);
32 void init_builtin_data(DataFile::BuiltinSource &);
33
34 Resources::Resources():
35         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
36         default_tex_anisotropy(1.0f),
37         srgb_conversion(false),
38         resource_manager(0)
39 {
40         add_type<Animation>().suffix(".anim").keyword("animation");
41         add_type<Armature>().suffix(".arma").keyword("armature");
42         add_type<Camera>().keyword("camera");
43         add_type<Font>().keyword("font");
44         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
45         add_type<Lighting>().suffix(".lightn").keyword("lighting");
46         add_type<Material>().suffix(".mat").creator(&Resources::create_material);
47         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
48         add_type<Module>().suffix(".glsl").creator(&Resources::create_module);
49         add_type<Object>().keyword("object");
50         add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
51         add_type<Pose>().keyword("pose");
52         add_type<Program>().keyword("shader").creator(&Resources::create_program);
53         add_type<Sampler>().suffix(".samp").keyword("sampler");
54         add_type<Technique>().suffix(".tech").keyword("technique");
55         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
56         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
57         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
58         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
59         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
60
61         add_source(get_builtins());
62 }
63
64 const DataFile::CollectionSource &Resources::get_builtins()
65 {
66         static DataFile::BuiltinSource builtins;
67         bool init_done = false;
68
69         if(!init_done)
70         {
71                 init_builtin_data(builtins);
72                 init_shaderlib(builtins);
73                 init_done = true;
74         }
75
76         return builtins;
77 }
78
79 void Resources::set_default_texture_filter(TextureFilter tf)
80 {
81         default_tex_filter = tf;
82 }
83
84 void Resources::set_default_texture_anisotropy(float a)
85 {
86         default_tex_anisotropy = a;
87 }
88
89 void Resources::set_srgb_conversion(bool c)
90 {
91         srgb_conversion = c;
92 }
93
94 void Resources::set_resource_manager(ResourceManager *m)
95 {
96         resource_manager = m;
97 }
98
99 Material *Resources::create_material(const string &name)
100 {
101         if(RefPtr<IO::Seekable> io = open_raw(name))
102         {
103                 DataFile::Parser parser(*io, name);
104                 Material::GenericLoader ldr(this);
105                 ldr.load(parser);
106                 return ldr.get_material();
107         }
108
109         return 0;
110 }
111
112 Mesh *Resources::create_mesh(const string &name)
113 {
114         if(!resource_manager)
115                 return 0;
116
117         if(RefPtr<IO::Seekable> io = open_raw(name))
118         {
119                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
120                 resource_manager->set_resource_location(*mesh, *this, name);
121                 return mesh.release();
122         }
123
124         return 0;
125 }
126
127 Texture2D *Resources::create_texture2d(const string &name)
128 {
129         string ext = FS::extpart(name);
130         if(ext==".tex2d" && !resource_manager)
131                 return 0;
132
133         if(RefPtr<IO::Seekable> io = open_raw(name))
134         {
135                 Graphics::Image image;
136                 if(!resource_manager)
137                         image.load_io(*io);
138
139                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
140
141                 if(ext==".tex2d")
142                 {
143                         DataFile::Parser parser(*io, name);
144                         Texture2D::Loader ldr(*tex, *this);
145                         ldr.load(parser);
146                 }
147                 else
148                 {
149                         Sampler &samp = tex->get_default_sampler();
150                         if(is_mipmapped(default_tex_filter))
151                         {
152                                 tex->set_auto_generate_mipmap(true);
153                                 samp.set_mag_filter(LINEAR);
154                         }
155                         else
156                                 samp.set_mag_filter(default_tex_filter);
157                         samp.set_min_filter(default_tex_filter);
158                         samp.set_max_anisotropy(default_tex_anisotropy);
159                 }
160
161                 if(resource_manager)
162                         resource_manager->set_resource_location(*tex, *this, name);
163                 else
164                         tex->image(image);
165                 return tex.release();
166         }
167
168         return 0;
169 }
170
171 Module *Resources::create_module(const string &name)
172 {
173         if(RefPtr<IO::Seekable> io = open_raw(name))
174         {
175                 RefPtr<Module> module = new Module;
176                 module->load_source(*io, this, name);
177                 return module.release();
178         }
179
180         return 0;
181 }
182
183 Program *Resources::create_program(const string &name)
184 {
185         string ext = FS::extpart(name);
186         string base = FS::basepart(name);
187         string ext2 = FS::extpart(base);
188         if(ext==".shader" && ext2==".glsl")
189         {
190                 Module &module = get<Module>(base);
191                 RefPtr<Program> shprog = new Program;
192                 shprog->add_stages(module);
193                 shprog->link();
194                 return shprog.release();
195         }
196
197         return 0;
198 }
199
200 } // namespace GL
201 } // namespace Msp