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