]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Fix various issues with constant condition elimination
[libs/gl.git] / source / 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 "object.h"
12 #include "pipelinetemplate.h"
13 #include "pose.h"
14 #include "program.h"
15 #include "programcompiler.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
18 #include "technique.h"
19 #include "texture1d.h"
20 #include "texture2d.h"
21 #include "texture2darray.h"
22 #include "texturecube.h"
23
24 using namespace std;
25
26 namespace Msp {
27 namespace GL {
28
29 void init_shaderlib(DataFile::BuiltinSource &);
30
31 Resources::Resources():
32         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
33         default_tex_anisotropy(1.0f),
34         srgb_conversion(false),
35         resource_manager(0)
36 {
37         add_type<Animation>().suffix(".anim").keyword("animation");
38         add_type<Armature>().suffix(".arma").keyword("armature");
39         add_type<Camera>().keyword("camera");
40         add_type<Font>().keyword("font");
41         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
42         add_type<Lighting>().suffix(".lightn").keyword("lighting");
43         add_type<Material>().suffix(".mat").keyword("material");
44         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
45         add_type<Object>().keyword("object");
46         add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
47         add_type<Pose>().keyword("pose");
48         add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
49         add_type<Technique>().suffix(".tech").keyword("technique");
50         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
51         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
52         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
53         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
54         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
55
56         add_source(get_builtins());
57 }
58
59 const DataFile::CollectionSource &Resources::get_builtins()
60 {
61         static DataFile::BuiltinSource builtins;
62         bool init_done = false;
63
64         if(!init_done)
65         {
66                 init_shaderlib(builtins);
67                 init_done = true;
68         }
69
70         return builtins;
71 }
72
73 void Resources::set_default_texture_filter(TextureFilter tf)
74 {
75         default_tex_filter = tf;
76 }
77
78 void Resources::set_default_texture_anisotropy(float a)
79 {
80         default_tex_anisotropy = a;
81 }
82
83 void Resources::set_srgb_conversion(bool c)
84 {
85         srgb_conversion = c;
86 }
87
88 void Resources::set_resource_manager(ResourceManager *m)
89 {
90         resource_manager = m;
91 }
92
93 Mesh *Resources::create_mesh(const string &name)
94 {
95         if(!resource_manager)
96                 return 0;
97
98         if(RefPtr<IO::Seekable> io = open_from_sources(name))
99         {
100                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
101                 resource_manager->set_resource_location(*mesh, *this, name);
102                 return mesh.release();
103         }
104
105         return 0;
106 }
107
108 Texture2D *Resources::create_texture2d(const string &name)
109 {
110         string ext = FS::extpart(name);
111         if(ext==".tex2d")
112                 return 0;
113
114         if(RefPtr<IO::Seekable> io = open_from_sources(name))
115         {
116                 Graphics::Image image;
117                 if(!resource_manager)
118                         image.load_io(*io);
119
120                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
121
122                 if(is_mipmapped(default_tex_filter))
123                 {
124                         tex->set_generate_mipmap(true);
125                         tex->set_mag_filter(LINEAR);
126                 }
127                 else
128                         tex->set_mag_filter(default_tex_filter);
129                 tex->set_min_filter(default_tex_filter);
130                 tex->set_max_anisotropy(default_tex_anisotropy);
131
132                 if(resource_manager)
133                         resource_manager->set_resource_location(*tex, *this, name);
134                 else
135                         tex->image(image, srgb_conversion);
136                 return tex.release();
137         }
138
139         return 0;
140 }
141
142 Program *Resources::create_program(const string &name)
143 {
144         string ext = FS::extpart(name);
145         if(ext==".shader")
146                 return 0;
147
148         if(RefPtr<IO::Seekable> io = open_from_sources(name))
149         {
150                 ProgramCompiler compiler;
151                 compiler.compile(*io, this, name);
152                 RefPtr<Program> program = new Program;
153                 compiler.add_shaders(*program);
154                 program->link();
155                 return program.release();
156         }
157
158         return 0;
159 }
160
161 } // namespace GL
162 } // namespace Msp