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>
7 #include "resourcemanager.h"
17 int Texture::swizzle_orders[] =
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
25 Texture::Texture(GLenum t, ResourceManager *m):
31 use_srgb_format(false),
32 auto_gen_mipmap(false),
33 default_sampler(*this)
37 else if(ARB_direct_state_access)
38 glCreateTextures(target, 1, &id);
40 glGenTextures(1, &id);
45 while(TexUnit *unit = TexUnit::find_unit(this))
46 unbind_from(unit->get_index());
49 glDeleteTextures(1, &id);
52 void Texture::set_format(PixelFormat fmt)
54 PixelComponents comp = get_components(fmt);
55 PixelComponents st_comp = comp;
56 FormatSwizzle swiz = NO_SWIZZLE;
61 swiz = R_TO_LUMINANCE;
65 swiz = RG_TO_LUMINANCE_ALPHA;
78 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt));
79 require_pixelformat(st_fmt);
81 static Require _req(ARB_texture_swizzle);
88 void Texture::apply_swizzle()
90 if(swizzle==NO_SWIZZLE)
93 if(get_gl_api()==OPENGL_ES2)
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]);
102 if(ARB_direct_state_access)
103 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
105 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
109 void Texture::set_parameter_i(GLenum param, int value) const
111 if(ARB_direct_state_access)
112 glTextureParameteri(id, param, value);
114 glTexParameteri(target, param, value);
117 void Texture::set_min_filter(TextureFilter f)
119 default_sampler.set_min_filter(f);
122 void Texture::set_mag_filter(TextureFilter f)
124 default_sampler.set_mag_filter(f);
127 void Texture::set_filter(TextureFilter f)
129 default_sampler.set_filter(f);
132 void Texture::set_max_anisotropy(float a)
134 default_sampler.set_max_anisotropy(a);
137 void Texture::set_wrap(TextureWrap w)
139 default_sampler.set_wrap(w);
142 void Texture::set_wrap_s(TextureWrap w)
144 default_sampler.set_wrap_s(w);
147 void Texture::set_wrap_t(TextureWrap w)
149 default_sampler.set_wrap_t(w);
152 void Texture::set_wrap_r(TextureWrap w)
154 default_sampler.set_wrap_r(w);
157 bool Texture::can_generate_mipmap()
159 return EXT_framebuffer_object;
162 void Texture::generate_mipmap()
164 // glGenerateMipmap is defined here
165 static Require _req(EXT_framebuffer_object);
167 if(ARB_direct_state_access)
168 glGenerateTextureMipmap(id);
171 BindRestore _bind(this);
172 glGenerateMipmap(target);
176 void Texture::set_auto_generate_mipmap(bool gm)
179 static Require _req(EXT_framebuffer_object);
181 auto_gen_mipmap = gm;
184 void Texture::set_compare_enabled(bool c)
187 default_sampler.set_compare(default_sampler.get_compare_function());
189 default_sampler.disable_compare();
192 void Texture::set_compare_func(Predicate f)
194 default_sampler.set_compare(f);
197 void Texture::load_image(const string &fn, bool)
202 void Texture::load_image(const string &fn, unsigned lv)
210 void Texture::image(const Graphics::Image &img, bool)
215 void Texture::bind_to(unsigned i) const
220 manager->resource_used(*this);
228 TexUnit &unit = TexUnit::get_unit(i);
229 const Texture *cur = unit.get_texture();
230 if(unit.set_texture(this))
233 manager->resource_used(*this);
235 if(ARB_direct_state_access)
236 glBindTextureUnit(i, id);
240 glBindTexture(target, id);
243 if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler)
244 default_sampler.bind_to(i);
248 const Texture *Texture::current(unsigned i)
250 return TexUnit::get_unit(i).get_texture();
253 void Texture::unbind_from(unsigned i)
255 TexUnit &unit = TexUnit::get_unit(i);
256 const Texture *cur = unit.get_texture();
257 if(unit.set_texture(0))
259 if(ARB_direct_state_access)
260 glBindTextureUnit(i, 0);
264 glBindTexture(cur->target, 0);
267 if(unit.get_sampler()==&cur->default_sampler)
268 Sampler::unbind_from(i);
273 Texture::Loader::Loader(Texture &t):
274 DataFile::CollectionObjectLoader<Texture>(t, 0)
279 Texture::Loader::Loader(Texture &t, Collection &c):
280 DataFile::CollectionObjectLoader<Texture>(t, &c)
285 void Texture::Loader::init()
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);
305 unsigned Texture::Loader::get_levels() const
307 return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
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)
314 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
316 throw IO::file_not_found(fn);
320 void Texture::Loader::external_image(const string &fn)
322 obj.use_srgb_format = false;
323 external_image_common(fn);
326 void Texture::Loader::external_image_srgb(const string &fn)
328 obj.use_srgb_format = true;
329 external_image_common(fn);
332 void Texture::Loader::external_image_common(const string &fn)
335 obj.manager->set_resource_location(obj, get_collection(), fn);
339 load_external_image(img, fn);
340 obj.image(img, get_levels());
344 void Texture::Loader::filter(TextureFilter f)
349 void Texture::Loader::generate_mipmap(bool gm)
351 obj.set_auto_generate_mipmap(gm);
354 void Texture::Loader::image_data(const string &data)
357 IO::Memory mem(data.data(), data.size());
360 obj.image(img, get_levels());
363 void Texture::Loader::mag_filter(TextureFilter f)
365 obj.set_mag_filter(f);
368 void Texture::Loader::max_anisotropy(float a)
370 obj.set_max_anisotropy(a);
373 void Texture::Loader::min_filter(TextureFilter f)
375 obj.set_min_filter(f);
378 void Texture::Loader::mipmap_levels(unsigned l)
383 void Texture::Loader::sampler()
385 load_sub(obj.default_sampler);
388 void Texture::Loader::wrap(TextureWrap w)
393 void Texture::Loader::wrap_r(TextureWrap w)
398 void Texture::Loader::wrap_s(TextureWrap w)
403 void Texture::Loader::wrap_t(TextureWrap w)
407 #pragma GCC diagnostic pop