]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Use UNSIGNED_SHORT for allocating DEPTH_COMPONENT textures
[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 DataType Texture::get_alloc_type(PixelFormat fmt)
79 {
80         return (fmt==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
81 }
82
83 void Texture::update_parameter(int mask) const
84 {
85         if(TexUnit::current().get_texture()!=this)
86         {
87                 TexUnit *unit = TexUnit::find_unit(this);
88                 if(!unit)
89                 {
90                         dirty_params |= mask;
91                         return;
92                 }
93
94                 unit->bind();
95         }
96
97         if(mask&MIN_FILTER)
98                 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, min_filter);
99         if(mask&MAG_FILTER)
100                 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mag_filter);
101         if(mask&MAX_ANISOTROPY)
102                 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy);
103         if(mask&WRAP_S)
104                 glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_s);
105         if(mask&WRAP_T)
106                 glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_t);
107         if(mask&WRAP_R)
108                 glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap_r);
109         if(mask&GENERATE_MIPMAP)
110                 glTexParameteri(target, GL_GENERATE_MIPMAP, gen_mipmap);
111         if(mask&COMPARE)
112                 glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE));
113         if(mask&COMPARE_FUNC)
114                 glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func);
115 }
116
117 void Texture::set_min_filter(TextureFilter f)
118 {
119         min_filter = f;
120         update_parameter(MIN_FILTER);
121 }
122
123 void Texture::set_mag_filter(TextureFilter f)
124 {
125         mag_filter = f;
126         update_parameter(MAG_FILTER);
127 }
128
129 void Texture::set_filter(TextureFilter f)
130 {
131         set_min_filter(f);
132         set_mag_filter(f==NEAREST ? NEAREST : LINEAR);
133 }
134
135 void Texture::set_max_anisotropy(float a)
136 {
137         if(a<1.0f)
138                 throw invalid_argument("Texture::set_max_anisotropy");
139         else if(a>1.0f)
140                 static Require _req(EXT_texture_filter_anisotropic);
141         max_anisotropy = a;
142         update_parameter(MAX_ANISOTROPY);
143 }
144
145 void Texture::set_wrap(TextureWrap w)
146 {
147         set_wrap_s(w);
148         set_wrap_t(w);
149         if(EXT_texture3D)
150                 set_wrap_r(w);
151 }
152
153 void Texture::set_wrap_s(TextureWrap w)
154 {
155         wrap_s = w;
156         update_parameter(WRAP_S);
157 }
158
159 void Texture::set_wrap_t(TextureWrap w)
160 {
161         wrap_t = w;
162         update_parameter(WRAP_T);
163 }
164
165 void Texture::set_wrap_r(TextureWrap w)
166 {
167         static Require _req(EXT_texture3D);
168         wrap_r = w;
169         update_parameter(WRAP_R);
170 }
171
172 void Texture::set_generate_mipmap(bool gm)
173 {
174         if(gm)
175                 static Require _req(SGIS_generate_mipmap);
176         gen_mipmap = gm;
177         if(get_gl_api()!=OPENGL_ES2)
178                 update_parameter(GENERATE_MIPMAP);
179 }
180
181 void Texture::auto_generate_mipmap()
182 {
183         if(get_gl_api()==OPENGL_ES2)
184         {
185                 // glGenerateMipmap is defined here
186                 static Require _req(EXT_framebuffer_object);
187                 glGenerateMipmap(target);
188         }
189 }
190
191 void Texture::set_compare_enabled(bool c)
192 {
193         if(c)
194                 static Require _req(ARB_shadow);
195         compare = c;
196         update_parameter(COMPARE);
197 }
198
199 void Texture::set_compare_func(Predicate f)
200 {
201         static Require _req(ARB_shadow);
202         cmp_func = f;
203         update_parameter(COMPARE_FUNC);
204 }
205
206 void Texture::bind_to(unsigned i) const
207 {
208         if(!id)
209         {
210                 if(manager)
211                         manager->resource_used(*this);
212                 if(!id)
213                 {
214                         unbind_from(i);
215                         return;
216                 }
217         }
218
219         TexUnit &unit = TexUnit::get_unit(i);
220         const Texture *old = unit.get_texture();
221         if(unit.set_texture(this))
222         {
223                 if(manager)
224                         manager->resource_used(*this);
225
226                 unit.bind();
227                 if(unit.supports_legacy())
228                 {
229                         if(old && old->target!=target)
230                                 glDisable(old->target);
231                         if(!old || old->target!=target)
232                                 glEnable(target);
233                 }
234                 glBindTexture(target, id);
235
236                 if(dirty_params)
237                 {
238                         update_parameter(dirty_params);
239                         dirty_params = 0;
240                 }
241         }
242 }
243
244 const Texture *Texture::current(unsigned i)
245 {
246         return TexUnit::get_unit(i).get_texture();
247 }
248
249 void Texture::unbind_from(unsigned i)
250 {
251         TexUnit &unit = TexUnit::get_unit(i);
252         const Texture *cur = unit.get_texture();
253         if(unit.set_texture(0))
254         {
255                 unit.bind();
256                 glBindTexture(cur->target, 0);
257                 if(unit.supports_legacy())
258                         glDisable(cur->target);
259         }
260 }
261
262
263 Texture::Loader::Loader(Texture &t):
264         DataFile::CollectionObjectLoader<Texture>(t, 0)
265 {
266         init();
267 }
268
269 Texture::Loader::Loader(Texture &t, Collection &c):
270         DataFile::CollectionObjectLoader<Texture>(t, &c)
271 {
272         init();
273 }
274
275 void Texture::Loader::init()
276 {
277         if(Resources *res = dynamic_cast<Resources *>(coll))
278                 srgb = res->get_srgb_conversion();
279         else
280                 srgb = false;
281
282         add("filter", &Loader::filter);
283         add("max_anisotropy", &Loader::max_anisotropy);
284         add("generate_mipmap", &Loader::generate_mipmap);
285         add("mag_filter", &Loader::mag_filter);
286         add("min_filter", &Loader::min_filter);
287         add("wrap",       &Loader::wrap);
288         add("wrap_r",     &Loader::wrap_r);
289         add("wrap_s",     &Loader::wrap_s);
290         add("wrap_t",     &Loader::wrap_t);
291 }
292
293 void Texture::Loader::filter(TextureFilter f)
294 {
295         obj.set_filter(f);
296 }
297
298 void Texture::Loader::generate_mipmap(bool gm)
299 {
300         obj.set_generate_mipmap(gm);
301 }
302
303 void Texture::Loader::mag_filter(TextureFilter f)
304 {
305         obj.set_mag_filter(f);
306 }
307
308 void Texture::Loader::max_anisotropy(float a)
309 {
310         obj.set_max_anisotropy(a);
311 }
312
313 void Texture::Loader::min_filter(TextureFilter f)
314 {
315         obj.set_min_filter(f);
316 }
317
318 void Texture::Loader::wrap(TextureWrap w)
319 {
320         obj.set_wrap(w);
321 }
322
323 void Texture::Loader::wrap_r(TextureWrap w)
324 {
325         obj.set_wrap_r(w);
326 }
327
328 void Texture::Loader::wrap_s(TextureWrap w)
329 {
330         obj.set_wrap_s(w);
331 }
332
333 void Texture::Loader::wrap_t(TextureWrap w)
334 {
335         obj.set_wrap_t(w);
336 }
337
338 } // namespace GL
339 } // namespace Msp