]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Guard against hitting the end of the array in get_gl_type
[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").suffix(".spv").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                 RefPtr<Texture2D> tex;
136
137                 if(ext==".tex2d")
138                 {
139                         tex = new Texture2D(resource_manager);
140                         DataFile::Parser parser(*io, name);
141                         Texture2D::Loader ldr(*tex, *this);
142                         ldr.load(parser);
143                 }
144                 else
145                 {
146                         // Verify that the image is loadable
147                         Graphics::Image image;
148                         if(!resource_manager)
149                                 image.load_io(*io);
150
151                         tex = new Texture2D(resource_manager);
152                         Sampler &samp = tex->get_default_sampler();
153                         if(is_mipmapped(default_tex_filter))
154                         {
155                                 tex->set_auto_generate_mipmap(true);
156                                 samp.set_mag_filter(LINEAR);
157                         }
158                         else
159                                 samp.set_mag_filter(default_tex_filter);
160                         samp.set_min_filter(default_tex_filter);
161                         samp.set_max_anisotropy(default_tex_anisotropy);
162
163                         if(resource_manager)
164                                 resource_manager->set_resource_location(*tex, *this, name);
165                         else
166                                 tex->image(image);
167                 }
168
169                 return tex.release();
170         }
171
172         return 0;
173 }
174
175 Module *Resources::create_module(const string &name)
176 {
177         string ext = FS::extpart(name);
178         if(ext!=".glsl" && ext!=".spv")
179                 return 0;
180
181         if(RefPtr<IO::Seekable> io = open_raw(name))
182         {
183                 if(ext==".glsl")
184                 {
185                         RefPtr<GlslModule> module = new GlslModule;
186                         module->load_source(*io, this, name);
187                         return module.release();
188                 }
189                 else if(ext==".spv")
190                 {
191                         RefPtr<SpirVModule> module = new SpirVModule;
192                         module->load_code(*io);
193                         return module.release();
194                 }
195         }
196
197         return 0;
198 }
199
200 Program *Resources::create_program(const string &name)
201 {
202         string ext = FS::extpart(name);
203         string base = FS::basepart(name);
204         string ext2 = FS::extpart(base);
205         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
206         {
207                 Module &module = get<Module>(base);
208                 RefPtr<Program> shprog = new Program;
209                 shprog->add_stages(module);
210                 shprog->link();
211                 return shprog.release();
212         }
213
214         return 0;
215 }
216
217 } // namespace GL
218 } // namespace Msp