]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Always use swizzle to implement luminance formats
[libs/gl.git] / source / texture.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_swizzle.h>
3 #include <msp/gl/extensions/ext_framebuffer_object.h>
4 #include <msp/io/memory.h>
5 #include "bindable.h"
6 #include "error.h"
7 #include "resourcemanager.h"
8 #include "resources.h"
9 #include "texture.h"
10 #include "texunit.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 int Texture::swizzle_orders[] =
18 {
19         GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
20         GL_RED, GL_RED, GL_RED, GL_ONE,
21         GL_RED, GL_RED, GL_RED, GL_GREEN
22 };
23
24 Texture::Texture(GLenum t, ResourceManager *m):
25         id(0),
26         target(t),
27         ifmt(RGB),
28         swizzle(NO_SWIZZLE),
29         auto_gen_mipmap(false),
30         default_sampler(*this)
31 {
32         if(m)
33                 set_manager(m);
34         else if(ARB_direct_state_access)
35                 glCreateTextures(target, 1, &id);
36         else
37                 glGenTextures(1, &id);
38 }
39
40 Texture::~Texture()
41 {
42         while(TexUnit *unit = TexUnit::find_unit(this))
43                 unbind_from(unit->get_index());
44
45         if(id)
46                 glDeleteTextures(1, &id);
47 }
48
49 DataType Texture::get_alloc_type(PixelFormat fmt)
50 {
51         return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
52 }
53
54 void Texture::set_internal_format(PixelFormat fmt)
55 {
56         FormatSwizzle swiz = NO_SWIZZLE;
57         switch(get_base_pixelformat(fmt))
58         {
59         case LUMINANCE:
60                 fmt = RED;
61                 swiz = R_TO_LUMINANCE;
62                 break;
63         case LUMINANCE_ALPHA:
64                 fmt = RG;
65                 swiz = RG_TO_LUMINANCE_ALPHA;
66                 break;
67         default:;
68         }
69
70         if(!get_component_size(fmt) && OES_required_internalformat)
71                 fmt = get_default_sized_pixelformat(fmt);
72
73         require_pixelformat(fmt);
74         if(swiz!=NO_SWIZZLE)
75                 static Require _req(ARB_texture_swizzle);
76         ifmt = fmt;
77         swizzle = swiz;
78 }
79
80 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
81 {
82         if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
83                 return get_base_pixelformat(ifmt);
84         else
85                 return fmt;
86 }
87
88 void Texture::apply_swizzle()
89 {
90         if(swizzle==NO_SWIZZLE)
91                 return;
92
93         if(get_gl_api()==OPENGL_ES2)
94         {
95                 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
96                 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
97                 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
98                 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
99         }
100         else
101         {
102                 if(ARB_direct_state_access)
103                         glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
104                 else
105                         glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
106         }
107 }
108
109 void Texture::set_parameter_i(GLenum param, int value) const
110 {
111         if(ARB_direct_state_access)
112                 glTextureParameteri(id, param, value);
113         else
114                 glTexParameteri(target, param, value);
115 }
116
117 void Texture::set_min_filter(TextureFilter f)
118 {
119         default_sampler.set_min_filter(f);
120 }
121
122 void Texture::set_mag_filter(TextureFilter f)
123 {
124         default_sampler.set_mag_filter(f);
125 }
126
127 void Texture::set_filter(TextureFilter f)
128 {
129         default_sampler.set_filter(f);
130 }
131
132 void Texture::set_max_anisotropy(float a)
133 {
134         default_sampler.set_max_anisotropy(a);
135 }
136
137 void Texture::set_wrap(TextureWrap w)
138 {
139         default_sampler.set_wrap(w);
140 }
141
142 void Texture::set_wrap_s(TextureWrap w)
143 {
144         default_sampler.set_wrap_s(w);
145 }
146
147 void Texture::set_wrap_t(TextureWrap w)
148 {
149         default_sampler.set_wrap_t(w);
150 }
151
152 void Texture::set_wrap_r(TextureWrap w)
153 {
154         default_sampler.set_wrap_r(w);
155 }
156
157 bool Texture::can_generate_mipmap()
158 {
159         return EXT_framebuffer_object;
160 }
161
162 void Texture::generate_mipmap()
163 {
164         // glGenerateMipmap is defined here
165         static Require _req(EXT_framebuffer_object);
166
167         if(ARB_direct_state_access)
168                 glGenerateTextureMipmap(id);
169         else
170         {
171                 BindRestore _bind(this);
172                 glGenerateMipmap(target);
173         }
174 }
175
176 void Texture::set_auto_generate_mipmap(bool gm)
177 {
178         if(gm)
179                 static Require _req(EXT_framebuffer_object);
180
181         auto_gen_mipmap = gm;
182 }
183
184 void Texture::set_compare_enabled(bool c)
185 {
186         if(c)
187                 default_sampler.set_compare(default_sampler.get_compare_function());
188         else
189                 default_sampler.disable_compare();
190 }
191
192 void Texture::set_compare_func(Predicate f)
193 {
194         default_sampler.set_compare(f);
195 }
196
197 void Texture::load_image(const string &fn, bool srgb)
198 {
199         load_image(fn, 0, srgb);
200 }
201
202 void Texture::load_image(const string &fn, unsigned lv, bool srgb)
203 {
204         Graphics::Image img;
205         img.load_file(fn);
206
207         image(img, lv, srgb);
208 }
209
210 void Texture::image(const Graphics::Image &img, bool srgb)
211 {
212         image(img, 0, srgb);
213 }
214
215 void Texture::bind_to(unsigned i) const
216 {
217         if(!id)
218         {
219                 if(manager)
220                         manager->resource_used(*this);
221                 if(!id)
222                 {
223                         unbind_from(i);
224                         return;
225                 }
226         }
227
228         TexUnit &unit = TexUnit::get_unit(i);
229         if(unit.set_texture(this))
230         {
231                 if(manager)
232                         manager->resource_used(*this);
233
234                 if(ARB_direct_state_access)
235                         glBindTextureUnit(i, id);
236                 else
237                 {
238                         unit.bind();
239                         glBindTexture(target, id);
240                 }
241
242                 default_sampler.bind_to(i);
243         }
244 }
245
246 const Texture *Texture::current(unsigned i)
247 {
248         return TexUnit::get_unit(i).get_texture();
249 }
250
251 void Texture::unbind_from(unsigned i)
252 {
253         TexUnit &unit = TexUnit::get_unit(i);
254         const Texture *cur = unit.get_texture();
255         if(unit.set_texture(0))
256         {
257                 if(ARB_direct_state_access)
258                         glBindTextureUnit(i, 0);
259                 else
260                 {
261                         unit.bind();
262                         glBindTexture(cur->target, 0);
263                 }
264         }
265 }
266
267
268 Texture::Loader::Loader(Texture &t):
269         DataFile::CollectionObjectLoader<Texture>(t, 0)
270 {
271         init();
272 }
273
274 Texture::Loader::Loader(Texture &t, Collection &c):
275         DataFile::CollectionObjectLoader<Texture>(t, &c)
276 {
277         init();
278 }
279
280 void Texture::Loader::init()
281 {
282         levels = 0;
283         if(Resources *res = dynamic_cast<Resources *>(coll))
284                 srgb = res->get_srgb_conversion();
285         else
286                 srgb = false;
287
288         add("external_image", &Loader::external_image);
289         add("filter", &Loader::filter);
290         add("generate_mipmap", &Loader::generate_mipmap);
291         add("image_data", &Loader::image_data);
292         add("mag_filter", &Loader::mag_filter);
293         add("max_anisotropy", &Loader::max_anisotropy);
294         add("min_filter", &Loader::min_filter);
295         add("mipmap_levels", &Loader::mipmap_levels);
296         add("sampler",    &Loader::sampler);
297         add("wrap",       &Loader::wrap);
298         add("wrap_r",     &Loader::wrap_r);
299         add("wrap_s",     &Loader::wrap_s);
300         add("wrap_t",     &Loader::wrap_t);
301 }
302
303 unsigned Texture::Loader::get_levels() const
304 {
305         return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
306 }
307
308 #pragma GCC diagnostic push
309 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
310 void Texture::Loader::external_image(const string &fn)
311 {
312         Graphics::Image img;
313         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
314         if(!io)
315                 throw IO::file_not_found(fn);
316         img.load_io(*io);
317
318         obj.image(img, get_levels(), srgb);
319 }
320
321 void Texture::Loader::filter(TextureFilter f)
322 {
323         obj.set_filter(f);
324 }
325
326 void Texture::Loader::generate_mipmap(bool gm)
327 {
328         obj.set_auto_generate_mipmap(gm);
329 }
330
331 void Texture::Loader::image_data(const string &data)
332 {
333         Graphics::Image img;
334         IO::Memory mem(data.data(), data.size());
335         img.load_io(mem);
336
337         obj.image(img, get_levels(), srgb);
338 }
339
340 void Texture::Loader::mag_filter(TextureFilter f)
341 {
342         obj.set_mag_filter(f);
343 }
344
345 void Texture::Loader::max_anisotropy(float a)
346 {
347         obj.set_max_anisotropy(a);
348 }
349
350 void Texture::Loader::min_filter(TextureFilter f)
351 {
352         obj.set_min_filter(f);
353 }
354
355 void Texture::Loader::mipmap_levels(unsigned l)
356 {
357         levels = l;
358 }
359
360 void Texture::Loader::sampler()
361 {
362         load_sub(obj.default_sampler);
363 }
364
365 void Texture::Loader::wrap(TextureWrap w)
366 {
367         obj.set_wrap(w);
368 }
369
370 void Texture::Loader::wrap_r(TextureWrap w)
371 {
372         obj.set_wrap_r(w);
373 }
374
375 void Texture::Loader::wrap_s(TextureWrap w)
376 {
377         obj.set_wrap_s(w);
378 }
379
380 void Texture::Loader::wrap_t(TextureWrap w)
381 {
382         obj.set_wrap_t(w);
383 }
384 #pragma GCC diagnostic pop
385
386 } // namespace GL
387 } // namespace Msp