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 auto_gen_mipmap(false),
32 default_sampler(*this)
36 else if(ARB_direct_state_access)
37 glCreateTextures(target, 1, &id);
39 glGenTextures(1, &id);
44 while(TexUnit *unit = TexUnit::find_unit(this))
45 unbind_from(unit->get_index());
48 glDeleteTextures(1, &id);
51 void Texture::set_format(PixelFormat fmt)
53 PixelComponents comp = get_components(fmt);
54 PixelComponents st_comp = comp;
55 FormatSwizzle swiz = NO_SWIZZLE;
60 swiz = R_TO_LUMINANCE;
64 swiz = RG_TO_LUMINANCE_ALPHA;
77 PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt));
78 require_pixelformat(st_fmt);
80 static Require _req(ARB_texture_swizzle);
87 void Texture::apply_swizzle()
89 if(swizzle==NO_SWIZZLE)
92 if(get_gl_api()==OPENGL_ES2)
94 set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
95 set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
96 set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
97 set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
101 if(ARB_direct_state_access)
102 glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
104 glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
108 void Texture::set_parameter_i(GLenum param, int value) const
110 if(ARB_direct_state_access)
111 glTextureParameteri(id, param, value);
113 glTexParameteri(target, param, value);
116 void Texture::set_min_filter(TextureFilter f)
118 default_sampler.set_min_filter(f);
121 void Texture::set_mag_filter(TextureFilter f)
123 default_sampler.set_mag_filter(f);
126 void Texture::set_filter(TextureFilter f)
128 default_sampler.set_filter(f);
131 void Texture::set_max_anisotropy(float a)
133 default_sampler.set_max_anisotropy(a);
136 void Texture::set_wrap(TextureWrap w)
138 default_sampler.set_wrap(w);
141 void Texture::set_wrap_s(TextureWrap w)
143 default_sampler.set_wrap_s(w);
146 void Texture::set_wrap_t(TextureWrap w)
148 default_sampler.set_wrap_t(w);
151 void Texture::set_wrap_r(TextureWrap w)
153 default_sampler.set_wrap_r(w);
156 bool Texture::can_generate_mipmap()
158 return EXT_framebuffer_object;
161 void Texture::generate_mipmap()
163 // glGenerateMipmap is defined here
164 static Require _req(EXT_framebuffer_object);
166 if(ARB_direct_state_access)
167 glGenerateTextureMipmap(id);
170 BindRestore _bind(this);
171 glGenerateMipmap(target);
175 void Texture::set_auto_generate_mipmap(bool gm)
178 static Require _req(EXT_framebuffer_object);
180 auto_gen_mipmap = gm;
183 void Texture::set_compare_enabled(bool c)
186 default_sampler.set_compare(default_sampler.get_compare_function());
188 default_sampler.disable_compare();
191 void Texture::set_compare_func(Predicate f)
193 default_sampler.set_compare(f);
196 void Texture::load_image(const string &fn, bool srgb)
198 load_image(fn, 0, srgb);
201 void Texture::load_image(const string &fn, unsigned lv, bool srgb)
206 image(img, lv, srgb);
209 void Texture::image(const Graphics::Image &img, bool srgb)
214 void Texture::bind_to(unsigned i) const
219 manager->resource_used(*this);
227 TexUnit &unit = TexUnit::get_unit(i);
228 if(unit.set_texture(this))
231 manager->resource_used(*this);
233 if(ARB_direct_state_access)
234 glBindTextureUnit(i, id);
238 glBindTexture(target, id);
241 default_sampler.bind_to(i);
245 const Texture *Texture::current(unsigned i)
247 return TexUnit::get_unit(i).get_texture();
250 void Texture::unbind_from(unsigned i)
252 TexUnit &unit = TexUnit::get_unit(i);
253 const Texture *cur = unit.get_texture();
254 if(unit.set_texture(0))
256 if(ARB_direct_state_access)
257 glBindTextureUnit(i, 0);
261 glBindTexture(cur->target, 0);
267 Texture::Loader::Loader(Texture &t):
268 DataFile::CollectionObjectLoader<Texture>(t, 0)
273 Texture::Loader::Loader(Texture &t, Collection &c):
274 DataFile::CollectionObjectLoader<Texture>(t, &c)
279 void Texture::Loader::init()
282 if(Resources *res = dynamic_cast<Resources *>(coll))
283 srgb = res->get_srgb_conversion();
287 add("external_image", &Loader::external_image);
288 add("filter", &Loader::filter);
289 add("generate_mipmap", &Loader::generate_mipmap);
290 add("image_data", &Loader::image_data);
291 add("mag_filter", &Loader::mag_filter);
292 add("max_anisotropy", &Loader::max_anisotropy);
293 add("min_filter", &Loader::min_filter);
294 add("mipmap_levels", &Loader::mipmap_levels);
295 add("sampler", &Loader::sampler);
296 add("wrap", &Loader::wrap);
297 add("wrap_r", &Loader::wrap_r);
298 add("wrap_s", &Loader::wrap_s);
299 add("wrap_t", &Loader::wrap_t);
302 unsigned Texture::Loader::get_levels() const
304 return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
307 #pragma GCC diagnostic push
308 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
309 void Texture::Loader::load_external_image(Graphics::Image &img, const std::string &fn)
311 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
313 throw IO::file_not_found(fn);
317 void Texture::Loader::external_image(const string &fn)
320 obj.manager->set_resource_location(obj, get_collection(), fn);
324 load_external_image(img, fn);
325 obj.image(img, get_levels(), srgb);
329 void Texture::Loader::filter(TextureFilter f)
334 void Texture::Loader::generate_mipmap(bool gm)
336 obj.set_auto_generate_mipmap(gm);
339 void Texture::Loader::image_data(const string &data)
342 IO::Memory mem(data.data(), data.size());
345 obj.image(img, get_levels(), srgb);
348 void Texture::Loader::mag_filter(TextureFilter f)
350 obj.set_mag_filter(f);
353 void Texture::Loader::max_anisotropy(float a)
355 obj.set_max_anisotropy(a);
358 void Texture::Loader::min_filter(TextureFilter f)
360 obj.set_min_filter(f);
363 void Texture::Loader::mipmap_levels(unsigned l)
368 void Texture::Loader::sampler()
370 load_sub(obj.default_sampler);
373 void Texture::Loader::wrap(TextureWrap w)
378 void Texture::Loader::wrap_r(TextureWrap w)
383 void Texture::Loader::wrap_s(TextureWrap w)
388 void Texture::Loader::wrap_t(TextureWrap w)
392 #pragma GCC diagnostic pop