]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.cpp
Fix extension declarations in GLSL output
[libs/gl.git] / source / core / 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         const Texture *cur = unit.get_texture();
230         if(unit.set_texture(this))
231         {
232                 if(manager)
233                         manager->resource_used(*this);
234
235                 if(ARB_direct_state_access)
236                         glBindTextureUnit(i, id);
237                 else
238                 {
239                         unit.bind();
240                         glBindTexture(target, id);
241                 }
242
243                 if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler)
244                         default_sampler.bind_to(i);
245         }
246 }
247
248 const Texture *Texture::current(unsigned i)
249 {
250         return TexUnit::get_unit(i).get_texture();
251 }
252
253 void Texture::unbind_from(unsigned i)
254 {
255         TexUnit &unit = TexUnit::get_unit(i);
256         const Texture *cur = unit.get_texture();
257         if(unit.set_texture(0))
258         {
259                 if(ARB_direct_state_access)
260                         glBindTextureUnit(i, 0);
261                 else
262                 {
263                         unit.bind();
264                         glBindTexture(cur->target, 0);
265                 }
266
267                 if(unit.get_sampler()==&cur->default_sampler)
268                         Sampler::unbind_from(i);
269         }
270 }
271
272
273 Texture::Loader::Loader(Texture &t):
274         DataFile::CollectionObjectLoader<Texture>(t, 0)
275 {
276         init();
277 }
278
279 Texture::Loader::Loader(Texture &t, Collection &c):
280         DataFile::CollectionObjectLoader<Texture>(t, &c)
281 {
282         init();
283 }
284
285 void Texture::Loader::init()
286 {
287         levels = 0;
288
289         add("external_image", &Loader::external_image);
290         add("external_image_srgb", &Loader::external_image);
291         add("filter", &Loader::filter);
292         add("generate_mipmap", &Loader::generate_mipmap);
293         add("image_data", &Loader::image_data);
294         add("mag_filter", &Loader::mag_filter);
295         add("max_anisotropy", &Loader::max_anisotropy);
296         add("min_filter", &Loader::min_filter);
297         add("mipmap_levels", &Loader::mipmap_levels);
298         add("sampler",    &Loader::sampler);
299         add("wrap",       &Loader::wrap);
300         add("wrap_r",     &Loader::wrap_r);
301         add("wrap_s",     &Loader::wrap_s);
302         add("wrap_t",     &Loader::wrap_t);
303 }
304
305 unsigned Texture::Loader::get_levels() const
306 {
307         return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
308 }
309
310 #pragma GCC diagnostic push
311 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
312 void Texture::Loader::load_external_image(Graphics::Image &img, const std::string &fn)
313 {
314         RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
315         if(!io)
316                 throw IO::file_not_found(fn);
317         img.load_io(*io);
318 }
319
320 void Texture::Loader::external_image(const string &fn)
321 {
322         obj.use_srgb_format = false;
323         external_image_common(fn);
324 }
325
326 void Texture::Loader::external_image_srgb(const string &fn)
327 {
328         obj.use_srgb_format = true;
329         external_image_common(fn);
330 }
331
332 void Texture::Loader::external_image_common(const string &fn)
333 {
334         if(obj.manager)
335                 obj.manager->set_resource_location(obj, get_collection(), fn);
336         else
337         {
338                 Graphics::Image img;
339                 load_external_image(img, fn);
340                 obj.image(img, get_levels());
341         }
342 }
343
344 void Texture::Loader::filter(TextureFilter f)
345 {
346         obj.set_filter(f);
347 }
348
349 void Texture::Loader::generate_mipmap(bool gm)
350 {
351         obj.set_auto_generate_mipmap(gm);
352 }
353
354 void Texture::Loader::image_data(const string &data)
355 {
356         Graphics::Image img;
357         IO::Memory mem(data.data(), data.size());
358         img.load_io(mem);
359
360         obj.image(img, get_levels());
361 }
362
363 void Texture::Loader::mag_filter(TextureFilter f)
364 {
365         obj.set_mag_filter(f);
366 }
367
368 void Texture::Loader::max_anisotropy(float a)
369 {
370         obj.set_max_anisotropy(a);
371 }
372
373 void Texture::Loader::min_filter(TextureFilter f)
374 {
375         obj.set_min_filter(f);
376 }
377
378 void Texture::Loader::mipmap_levels(unsigned l)
379 {
380         levels = l;
381 }
382
383 void Texture::Loader::sampler()
384 {
385         load_sub(obj.default_sampler);
386 }
387
388 void Texture::Loader::wrap(TextureWrap w)
389 {
390         obj.set_wrap(w);
391 }
392
393 void Texture::Loader::wrap_r(TextureWrap w)
394 {
395         obj.set_wrap_r(w);
396 }
397
398 void Texture::Loader::wrap_s(TextureWrap w)
399 {
400         obj.set_wrap_s(w);
401 }
402
403 void Texture::Loader::wrap_t(TextureWrap w)
404 {
405         obj.set_wrap_t(w);
406 }
407 #pragma GCC diagnostic pop
408
409 } // namespace GL
410 } // namespace Msp