]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Use explicit mipmap generation if necessary
[libs/gl.git] / source / texture.cpp
1 #include <msp/gl/extensions/arb_shadow.h>
2 #include <msp/gl/extensions/ext_framebuffer_object.h>
3 #include <msp/gl/extensions/ext_texture3d.h>
4 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
5 #include <msp/gl/extensions/sgis_generate_mipmap.h>
6 #include <msp/strings/format.h>
7 #include "error.h"
8 #include "resourcemanager.h"
9 #include "resources.h"
10 #include "texture.h"
11 #include "texunit.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 void operator>>(const LexicalConverter &c, TextureFilter &tf)
19 {
20         if(c.get()=="NEAREST")
21                 tf = NEAREST;
22         else if(c.get()=="LINEAR")
23                 tf = LINEAR;
24         else if(c.get()=="NEAREST_MIPMAP_NEAREST")
25                 tf = NEAREST_MIPMAP_NEAREST;
26         else if(c.get()=="NEAREST_MIPMAP_LINEAR")
27                 tf = NEAREST_MIPMAP_LINEAR;
28         else if(c.get()=="LINEAR_MIPMAP_NEAREST")
29                 tf = LINEAR_MIPMAP_NEAREST;
30         else if(c.get()=="LINEAR_MIPMAP_LINEAR")
31                 tf = LINEAR_MIPMAP_LINEAR;
32         else
33                 throw lexical_error(format("conversion of '%s' to TextureFilter", c.get()));
34 }
35
36
37 void operator>>(const LexicalConverter &c, TextureWrap &tw)
38 {
39         if(c.get()=="REPEAT")
40                 tw = REPEAT;
41         else if(c.get()=="CLAMP_TO_EDGE")
42                 tw = CLAMP_TO_EDGE;
43         else if(c.get()=="MIRRORED_REPEAT")
44                 tw = MIRRORED_REPEAT;
45         else
46                 throw lexical_error(format("conversion of '%s' to TextureWrap", c.get()));
47 }
48
49
50 Texture::Texture(GLenum t, ResourceManager *m):
51         id(0),
52         target(t),
53         min_filter(NEAREST_MIPMAP_LINEAR),
54         mag_filter(LINEAR),
55         wrap_s(REPEAT),
56         wrap_t(REPEAT),
57         wrap_r(REPEAT),
58         gen_mipmap(false),
59         compare(false),
60         cmp_func(LEQUAL),
61         dirty_params(0)
62 {
63         if(m)
64                 set_manager(m);
65         else
66                 glGenTextures(1, &id);
67 }
68
69 Texture::~Texture()
70 {
71         while(TexUnit *unit = TexUnit::find_unit(this))
72                 unbind_from(unit->get_index());
73
74         if(id)
75                 glDeleteTextures(1, &id);
76 }
77
78 void Texture::update_parameter(int mask) const
79 {
80         if(TexUnit::current().get_texture()!=this)
81         {
82                 TexUnit *unit = TexUnit::find_unit(this);
83                 if(!unit)
84                 {
85                         dirty_params |= mask;
86                         return;
87                 }
88
89                 unit->bind();
90         }
91
92         if(mask&MIN_FILTER)
93                 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
94         if(mask&MAG_FILTER)
95                 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
96         if(mask&MAX_ANISOTROPY)
97                 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
98         if(mask&WRAP_S)
99                 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
100         if(mask&WRAP_T)
101                 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
102         if(mask&WRAP_R)
103                 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
104         if(mask&GENERATE_MIPMAP)
105                 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
106         if(mask&COMPARE)
107                 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
108         if(mask&COMPARE_FUNC)
109                 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
110 }
111
112 void Texture::set_min_filter(TextureFilter f)
113 {
114         min_filter = f;
115         update_parameter(MIN_FILTER);
116 }
117
118 void Texture::set_mag_filter(TextureFilter f)
119 {
120         mag_filter = f;
121         update_parameter(MAG_FILTER);
122 }
123
124 void Texture::set_filter(TextureFilter f)
125 {
126         set_min_filter(f);
127         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
128 }
129
130 void Texture::set_max_anisotropy(float a)
131 {
132         if(a<1.0f)
133                 throw invalid_argument("Texture::set_max_anisotropy");
134         else if(a>1.0f)
135                 static Require _req(EXT_texture_filter_anisotropic);
136         max_anisotropy = a;
137         update_parameter(MAX_ANISOTROPY);
138 }
139
140 void Texture::set_wrap(TextureWrap w)
141 {
142         set_wrap_s(w);
143         set_wrap_t(w);
144         if(EXT_texture3D)
145                 set_wrap_r(w);
146 }
147
148 void Texture::set_wrap_s(TextureWrap w)
149 {
150         wrap_s = w;
151         update_parameter(WRAP_S);
152 }
153
154 void Texture::set_wrap_t(TextureWrap w)
155 {
156         wrap_t = w;
157         update_parameter(WRAP_T);
158 }
159
160 void Texture::set_wrap_r(TextureWrap w)
161 {
162         static Require _req(EXT_texture3D);
163         wrap_r = w;
164         update_parameter(WRAP_R);
165 }
166
167 void Texture::set_generate_mipmap(bool gm)
168 {
169         if(gm)
170                 static Require _req(SGIS_generate_mipmap);
171         gen_mipmap = gm;
172         if(get_gl_api()!=OPENGL_ES2)
173                 update_parameter(GENERATE_MIPMAP);
174 }
175
176 void Texture::auto_generate_mipmap()
177 {
178         if(get_gl_api()==OPENGL_ES2)
179         {
180                 // glGenerateMipmap is defined here
181                 static Require _req(EXT_framebuffer_object);
182                 glGenerateMipmap(target);
183         }
184 }
185
186 void Texture::set_compare_enabled(bool c)
187 {
188         static Require _req(ARB_shadow);
189         compare = c;
190         update_parameter(COMPARE);
191 }
192
193 void Texture::set_compare_func(Predicate f)
194 {
195         static Require _req(ARB_shadow);
196         cmp_func = f;
197         update_parameter(COMPARE_FUNC);
198 }
199
200 void Texture::bind_to(unsigned i) const
201 {
202         if(!id)
203         {
204                 if(manager)
205                         manager->resource_used(*this);
206                 if(!id)
207                 {
208                         unbind_from(i);
209                         return;
210                 }
211         }
212
213         TexUnit &unit = TexUnit::get_unit(i);
214         const Texture *old = unit.get_texture();
215         if(unit.set_texture(this))
216         {
217                 if(manager)
218                         manager->resource_used(*this);
219
220                 unit.bind();
221                 if(unit.supports_legacy())
222                 {
223                         if(old && old->target!=target)
224                                 glDisable(old->target);
225                         if(!old || old->target!=target)
226                                 glEnable(target);
227                 }
228                 glBindTexture(target, id);
229
230                 if(dirty_params)
231                 {
232                         update_parameter(dirty_params);
233                         dirty_params = 0;
234                 }
235         }
236 }
237
238 const Texture *Texture::current(unsigned i)
239 {
240         return TexUnit::get_unit(i).get_texture();
241 }
242
243 void Texture::unbind_from(unsigned i)
244 {
245         TexUnit &unit = TexUnit::get_unit(i);
246         const Texture *cur = unit.get_texture();
247         if(unit.set_texture(0))
248         {
249                 unit.bind();
250                 glBindTexture(cur->target, 0);
251                 if(unit.supports_legacy())
252                         glDisable(cur->target);
253         }
254 }
255
256
257 Texture::Loader::Loader(Texture &t):
258         DataFile::CollectionObjectLoader<Texture>(t, 0)
259 {
260         init();
261 }
262
263 Texture::Loader::Loader(Texture &t, Collection &c):
264         DataFile::CollectionObjectLoader<Texture>(t, &c)
265 {
266         init();
267 }
268
269 void Texture::Loader::init()
270 {
271         if(Resources *res = dynamic_cast<Resources *>(coll))
272                 srgb = res->get_srgb_conversion();
273         else
274                 srgb = false;
275
276         add("filter", &Loader::filter);
277         add("max_anisotropy", &Loader::max_anisotropy);
278         add("generate_mipmap", &Loader::generate_mipmap);
279         add("mag_filter", &Loader::mag_filter);
280         add("min_filter", &Loader::min_filter);
281         add("wrap",       &Loader::wrap);
282         add("wrap_r",     &Loader::wrap_r);
283         add("wrap_s",     &Loader::wrap_s);
284         add("wrap_t",     &Loader::wrap_t);
285 }
286
287 void Texture::Loader::filter(TextureFilter f)
288 {
289         obj.set_filter(f);
290 }
291
292 void Texture::Loader::generate_mipmap(bool gm)
293 {
294         obj.set_generate_mipmap(gm);
295 }
296
297 void Texture::Loader::mag_filter(TextureFilter f)
298 {
299         obj.set_mag_filter(f);
300 }
301
302 void Texture::Loader::max_anisotropy(float a)
303 {
304         obj.set_max_anisotropy(a);
305 }
306
307 void Texture::Loader::min_filter(TextureFilter f)
308 {
309         obj.set_min_filter(f);
310 }
311
312 void Texture::Loader::wrap(TextureWrap w)
313 {
314         obj.set_wrap(w);
315 }
316
317 void Texture::Loader::wrap_r(TextureWrap w)
318 {
319         obj.set_wrap_r(w);
320 }
321
322 void Texture::Loader::wrap_s(TextureWrap w)
323 {
324         obj.set_wrap_s(w);
325 }
326
327 void Texture::Loader::wrap_t(TextureWrap w)
328 {
329         obj.set_wrap_t(w);
330 }
331
332 } // namespace GL
333 } // namespace Msp