]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Initialize Texture::use_srgb_format
[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         GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
23 };
24
25 Texture::Texture(GLenum t, ResourceManager *m):
26         id(0),
27         target(t),
28         format(RGB8),
29         storage_fmt(RGB8),
30         swizzle(NO_SWIZZLE),
31         use_srgb_format(false),
32         auto_gen_mipmap(false),
33         default_sampler(*this)
34 {
35         if(m)
36                 set_manager(m);
37         else if(ARB_direct_state_access)
38                 glCreateTextures(target, 1, &id);
39         else
40                 glGenTextures(1, &id);
41 }
42
43 Texture::~Texture()
44 {
45         while(TexUnit *unit = TexUnit::find_unit(this))
46                 unbind_from(unit->get_index());
47
48         if(id)
49                 glDeleteTextures(1, &id);
50 }
51
52 void Texture::set_format(PixelFormat fmt)
53 {
54         PixelComponents comp = get_components(fmt);
55         PixelComponents st_comp = comp;
56         FormatSwizzle swiz = NO_SWIZZLE;
57         switch(comp)
58         {
59         case LUMINANCE:
60                 st_comp = RED;
61                 swiz = R_TO_LUMINANCE;
62                 break;
63         case LUMINANCE_ALPHA:
64                 st_comp = RG;
65                 swiz = RG_TO_LUMINANCE_ALPHA;
66                 break;
67         case BGR:
68                 st_comp = RGB;
69                 swiz = RGB_TO_BGR;
70                 break;
71         case BGRA:
72                 st_comp = RGBA;
73                 swiz = RGB_TO_BGR;
74                 break;
75         default:;
76         }
77
78         PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt));
79         require_pixelformat(st_fmt);
80         if(swiz!=NO_SWIZZLE)
81                 static Require _req(ARB_texture_swizzle);
82
83         format = fmt;
84         storage_fmt = st_fmt;
85         swizzle = swiz;
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)
198 {
199         load_image(fn, 0U);
200 }
201
202 void Texture::load_image(const string &fn, unsigned lv)
203 {
204         Graphics::Image img;
205         img.load_file(fn);
206
207         image(img, lv);
208 }
209
210 void Texture::image(const Graphics::Image &img, bool)
211 {
212         image(img, 0U);
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                 if(!unit.get_sampler())
243                         default_sampler.bind_to(i);
244         }
245 }
246
247 const Texture *Texture::current(unsigned i)
248 {
249         return TexUnit::get_unit(i).get_texture();
250 }
251
252 void Texture::unbind_from(unsigned i)
253 {
254         TexUnit &unit = TexUnit::get_unit(i);
255         const Texture *cur = unit.get_texture();
256         if(unit.set_texture(0))
257         {
258                 if(ARB_direct_state_access)
259                         glBindTextureUnit(i, 0);
260                 else
261                 {
262                         unit.bind();
263                         glBindTexture(cur->target, 0);
264                 }
265
266                 if(unit.get_sampler()==&cur->default_sampler)
267                         Sampler::unbind_from(i);
268         }
269 }
270
271
272 Texture::Loader::Loader(Texture &t):
273         DataFile::CollectionObjectLoader<Texture>(t, 0)
274 {
275         init();
276 }
277
278 Texture::Loader::Loader(Texture &t, Collection &c):
279         DataFile::CollectionObjectLoader<Texture>(t, &c)
280 {
281         init();
282 }
283
284 void Texture::Loader::init()
285 {
286         levels = 0;
287
288         add("external_image", &Loader::external_image);
289         add("external_image_srgb", &Loader::external_image);
290         add("filter", &Loader::filter);
291         add("generate_mipmap", &Loader::generate_mipmap);
292         add("image_data", &Loader::image_data);
293         add("mag_filter", &Loader::mag_filter);
294         add("max_anisotropy", &Loader::max_anisotropy);
295         add("min_filter", &Loader::min_filter);
296         add("mipmap_levels", &Loader::mipmap_levels);
297         add("sampler",    &Loader::sampler);
298         add("wrap",       &Loader::wrap);
299         add("wrap_r",     &Loader::wrap_r);
300         add("wrap_s",     &Loader::wrap_s);
301         add("wrap_t",     &Loader::wrap_t);
302 }
303
304 unsigned Texture::Loader::get_levels() const
305 {
306         return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
307 }
308
309 #pragma GCC diagnostic push
310 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
311 void Texture::Loader::load_external_image(Graphics::Image &img, const std::string &fn)
312 {
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
319 void Texture::Loader::external_image(const string &fn)
320 {
321         obj.use_srgb_format = false;
322         external_image_common(fn);
323 }
324
325 void Texture::Loader::external_image_srgb(const string &fn)
326 {
327         obj.use_srgb_format = true;
328         external_image_common(fn);
329 }
330
331 void Texture::Loader::external_image_common(const string &fn)
332 {
333         if(obj.manager)
334                 obj.manager->set_resource_location(obj, get_collection(), fn);
335         else
336         {
337                 Graphics::Image img;
338                 load_external_image(img, fn);
339                 obj.image(img, get_levels());
340         }
341 }
342
343 void Texture::Loader::filter(TextureFilter f)
344 {
345         obj.set_filter(f);
346 }
347
348 void Texture::Loader::generate_mipmap(bool gm)
349 {
350         obj.set_auto_generate_mipmap(gm);
351 }
352
353 void Texture::Loader::image_data(const string &data)
354 {
355         Graphics::Image img;
356         IO::Memory mem(data.data(), data.size());
357         img.load_io(mem);
358
359         obj.image(img, get_levels());
360 }
361
362 void Texture::Loader::mag_filter(TextureFilter f)
363 {
364         obj.set_mag_filter(f);
365 }
366
367 void Texture::Loader::max_anisotropy(float a)
368 {
369         obj.set_max_anisotropy(a);
370 }
371
372 void Texture::Loader::min_filter(TextureFilter f)
373 {
374         obj.set_min_filter(f);
375 }
376
377 void Texture::Loader::mipmap_levels(unsigned l)
378 {
379         levels = l;
380 }
381
382 void Texture::Loader::sampler()
383 {
384         load_sub(obj.default_sampler);
385 }
386
387 void Texture::Loader::wrap(TextureWrap w)
388 {
389         obj.set_wrap(w);
390 }
391
392 void Texture::Loader::wrap_r(TextureWrap w)
393 {
394         obj.set_wrap_r(w);
395 }
396
397 void Texture::Loader::wrap_s(TextureWrap w)
398 {
399         obj.set_wrap_s(w);
400 }
401
402 void Texture::Loader::wrap_t(TextureWrap w)
403 {
404         obj.set_wrap_t(w);
405 }
406 #pragma GCC diagnostic pop
407
408 } // namespace GL
409 } // namespace Msp