]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Only require ARB_shadow when enabling texture compare mode
[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         if(c)
189                 static Require _req(ARB_shadow);
190         compare = c;
191         update_parameter(COMPARE);
192 }
193
194 void Texture::set_compare_func(Predicate f)
195 {
196         static Require _req(ARB_shadow);
197         cmp_func = f;
198         update_parameter(COMPARE_FUNC);
199 }
200
201 void Texture::bind_to(unsigned i) const
202 {
203         if(!id)
204         {
205                 if(manager)
206                         manager->resource_used(*this);
207                 if(!id)
208                 {
209                         unbind_from(i);
210                         return;
211                 }
212         }
213
214         TexUnit &unit = TexUnit::get_unit(i);
215         const Texture *old = unit.get_texture();
216         if(unit.set_texture(this))
217         {
218                 if(manager)
219                         manager->resource_used(*this);
220
221                 unit.bind();
222                 if(unit.supports_legacy())
223                 {
224                         if(old && old->target!=target)
225                                 glDisable(old->target);
226                         if(!old || old->target!=target)
227                                 glEnable(target);
228                 }
229                 glBindTexture(target, id);
230
231                 if(dirty_params)
232                 {
233                         update_parameter(dirty_params);
234                         dirty_params = 0;
235                 }
236         }
237 }
238
239 const Texture *Texture::current(unsigned i)
240 {
241         return TexUnit::get_unit(i).get_texture();
242 }
243
244 void Texture::unbind_from(unsigned i)
245 {
246         TexUnit &unit = TexUnit::get_unit(i);
247         const Texture *cur = unit.get_texture();
248         if(unit.set_texture(0))
249         {
250                 unit.bind();
251                 glBindTexture(cur->target, 0);
252                 if(unit.supports_legacy())
253                         glDisable(cur->target);
254         }
255 }
256
257
258 Texture::Loader::Loader(Texture &t):
259         DataFile::CollectionObjectLoader<Texture>(t, 0)
260 {
261         init();
262 }
263
264 Texture::Loader::Loader(Texture &t, Collection &c):
265         DataFile::CollectionObjectLoader<Texture>(t, &c)
266 {
267         init();
268 }
269
270 void Texture::Loader::init()
271 {
272         if(Resources *res = dynamic_cast<Resources *>(coll))
273                 srgb = res->get_srgb_conversion();
274         else
275                 srgb = false;
276
277         add("filter", &Loader::filter);
278         add("max_anisotropy", &Loader::max_anisotropy);
279         add("generate_mipmap", &Loader::generate_mipmap);
280         add("mag_filter", &Loader::mag_filter);
281         add("min_filter", &Loader::min_filter);
282         add("wrap",       &Loader::wrap);
283         add("wrap_r",     &Loader::wrap_r);
284         add("wrap_s",     &Loader::wrap_s);
285         add("wrap_t",     &Loader::wrap_t);
286 }
287
288 void Texture::Loader::filter(TextureFilter f)
289 {
290         obj.set_filter(f);
291 }
292
293 void Texture::Loader::generate_mipmap(bool gm)
294 {
295         obj.set_generate_mipmap(gm);
296 }
297
298 void Texture::Loader::mag_filter(TextureFilter f)
299 {
300         obj.set_mag_filter(f);
301 }
302
303 void Texture::Loader::max_anisotropy(float a)
304 {
305         obj.set_max_anisotropy(a);
306 }
307
308 void Texture::Loader::min_filter(TextureFilter f)
309 {
310         obj.set_min_filter(f);
311 }
312
313 void Texture::Loader::wrap(TextureWrap w)
314 {
315         obj.set_wrap(w);
316 }
317
318 void Texture::Loader::wrap_r(TextureWrap w)
319 {
320         obj.set_wrap_r(w);
321 }
322
323 void Texture::Loader::wrap_s(TextureWrap w)
324 {
325         obj.set_wrap_s(w);
326 }
327
328 void Texture::Loader::wrap_t(TextureWrap w)
329 {
330         obj.set_wrap_t(w);
331 }
332
333 } // namespace GL
334 } // namespace Msp