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/gl/extensions/khr_debug.h>
5 #include <msp/io/memory.h>
8 #include "resourcemanager.h"
18 int Texture::swizzle_orders[] =
20 GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
21 GL_RED, GL_RED, GL_RED, GL_ONE,
22 GL_RED, GL_RED, GL_RED, GL_GREEN,
23 GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
26 Texture::Texture(GLenum t, ResourceManager *m):
32 use_srgb_format(false),
33 auto_gen_mipmap(false),
34 default_sampler(*this)
44 while(TexUnit *unit = TexUnit::find_unit(this))
45 unbind_from(unit->get_index());
48 glDeleteTextures(1, &id);
51 void Texture::generate_id()
54 throw invalid_operation("Texture::generate_id");
55 if(ARB_direct_state_access)
56 glCreateTextures(target, 1, &id);
58 glGenTextures(1, &id);
61 if(!debug_name.empty() && KHR_debug)
62 glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
66 void Texture::set_format(PixelFormat fmt)
68 PixelComponents comp = get_components(fmt);
69 PixelComponents st_comp = comp;
70 FormatSwizzle swiz = NO_SWIZZLE;
75 swiz = R_TO_LUMINANCE;
79 swiz = RG_TO_LUMINANCE_ALPHA;
92 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
93 require_pixelformat(st_fmt);
95 static Require _req(ARB_texture_swizzle);
102 void Texture::apply_swizzle()
104 if(swizzle==NO_SWIZZLE)
107 if(get_gl_api()==OPENGL_ES2)
109 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
110 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
111 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
112 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
116 if(ARB_direct_state_access)
117 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
119 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
123 void Texture::set_parameter_i(GLenum param, int value) const
125 if(ARB_direct_state_access)
126 glTextureParameteri(id, param, value);
128 glTexParameteri(target, param, value);
131 void Texture::set_min_filter(TextureFilter f)
133 default_sampler.set_min_filter(f);
136 void Texture::set_mag_filter(TextureFilter f)
138 default_sampler.set_mag_filter(f);
141 void Texture::set_filter(TextureFilter f)
143 default_sampler.set_filter(f);
146 void Texture::set_max_anisotropy(float a)
148 default_sampler.set_max_anisotropy(a);
151 void Texture::set_wrap(TextureWrap w)
153 default_sampler.set_wrap(w);
156 void Texture::set_wrap_s(TextureWrap w)
158 default_sampler.set_wrap_s(w);
161 void Texture::set_wrap_t(TextureWrap w)
163 default_sampler.set_wrap_t(w);
166 void Texture::set_wrap_r(TextureWrap w)
168 default_sampler.set_wrap_r(w);
171 bool Texture::can_generate_mipmap()
173 return EXT_framebuffer_object;
176 void Texture::generate_mipmap()
178 // glGenerateMipmap is defined here
179 static Require _req(EXT_framebuffer_object);
181 if(ARB_direct_state_access)
182 glGenerateTextureMipmap(id);
185 BindRestore _bind(this);
186 glGenerateMipmap(target);
190 void Texture::set_auto_generate_mipmap(bool gm)
193 static Require _req(EXT_framebuffer_object);
195 auto_gen_mipmap = gm;
198 void Texture::set_compare_enabled(bool c)
201 default_sampler.set_compare(default_sampler.get_compare_function());
203 default_sampler.disable_compare();
206 void Texture::set_compare_func(Predicate f)
208 default_sampler.set_compare(f);
211 void Texture::load_image(const string &fn, bool)
216 void Texture::load_image(const string &fn, unsigned lv)
224 void Texture::image(const Graphics::Image &img, bool)
229 void Texture::bind_to(unsigned i) const
234 manager->resource_used(*this);
242 TexUnit &unit = TexUnit::get_unit(i);
243 const Texture *cur = unit.get_texture();
244 if(unit.set_texture(this))
247 manager->resource_used(*this);
249 if(ARB_direct_state_access)
250 glBindTextureUnit(i, id);
254 glBindTexture(target, id);
257 if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler)
258 default_sampler.bind_to(i);
262 const Texture *Texture::current(unsigned i)
264 return TexUnit::get_unit(i).get_texture();
267 void Texture::unbind_from(unsigned i)
269 TexUnit &unit = TexUnit::get_unit(i);
270 const Texture *cur = unit.get_texture();
271 if(unit.set_texture(0))
273 if(ARB_direct_state_access)
274 glBindTextureUnit(i, 0);
278 glBindTexture(cur->target, 0);
281 if(unit.get_sampler()==&cur->default_sampler)
282 Sampler::unbind_from(i);
286 void Texture::set_debug_name(const string &name)
291 glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
298 Texture::Loader::Loader(Texture &t):
299 DataFile::CollectionObjectLoader<Texture>(t, 0)
304 Texture::Loader::Loader(Texture &t, Collection &c):
305 DataFile::CollectionObjectLoader<Texture>(t, &c)
310 void Texture::Loader::init()
314 add("external_image", &Loader::external_image);
315 add("external_image_srgb", &Loader::external_image_srgb);
316 add("filter", &Loader::filter);
317 add("generate_mipmap", &Loader::generate_mipmap);
318 add("image_data", &Loader::image_data);
319 add("mag_filter", &Loader::mag_filter);
320 add("max_anisotropy", &Loader::max_anisotropy);
321 add("min_filter", &Loader::min_filter);
322 add("mipmap_levels", &Loader::mipmap_levels);
323 add("sampler", &Loader::sampler);
324 add("wrap", &Loader::wrap);
325 add("wrap_r", &Loader::wrap_r);
326 add("wrap_s", &Loader::wrap_s);
327 add("wrap_t", &Loader::wrap_t);
330 unsigned Texture::Loader::get_levels() const
332 return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
335 #pragma GCC diagnostic push
336 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
337 void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn)
339 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
341 throw IO::file_not_found(fn);
345 void Texture::Loader::external_image(const string &fn)
347 obj.use_srgb_format = false;
348 external_image_common(fn);
351 void Texture::Loader::external_image_srgb(const string &fn)
353 obj.use_srgb_format = true;
354 external_image_common(fn);
357 void Texture::Loader::external_image_common(const string &fn)
360 obj.manager->set_resource_location(obj, get_collection(), fn);
364 load_external_image(img, fn);
365 obj.image(img, get_levels());
369 void Texture::Loader::filter(TextureFilter f)
374 void Texture::Loader::generate_mipmap(bool gm)
376 obj.set_auto_generate_mipmap(gm);
379 void Texture::Loader::image_data(const string &data)
389 IO::Memory mem(data.data(), data.size());
392 obj.image(img, get_levels());
395 void Texture::Loader::mag_filter(TextureFilter f)
397 obj.set_mag_filter(f);
400 void Texture::Loader::max_anisotropy(float a)
402 obj.set_max_anisotropy(a);
405 void Texture::Loader::min_filter(TextureFilter f)
407 obj.set_min_filter(f);
410 void Texture::Loader::mipmap_levels(unsigned l)
415 void Texture::Loader::sampler()
417 load_sub(obj.default_sampler);
420 void Texture::Loader::wrap(TextureWrap w)
425 void Texture::Loader::wrap_r(TextureWrap w)
430 void Texture::Loader::wrap_s(TextureWrap w)
435 void Texture::Loader::wrap_t(TextureWrap w)
439 #pragma GCC diagnostic pop