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
24 Texture::Texture(GLenum t, ResourceManager *m):
29 auto_gen_mipmap(false),
30 default_sampler(*this)
34 else if(ARB_direct_state_access)
35 glCreateTextures(target, 1, &id);
37 glGenTextures(1, &id);
42 while(TexUnit *unit = TexUnit::find_unit(this))
43 unbind_from(unit->get_index());
46 glDeleteTextures(1, &id);
49 DataType Texture::get_alloc_type(PixelFormat fmt)
51 return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE);
54 void Texture::set_internal_format(PixelFormat fmt)
56 FormatSwizzle swiz = NO_SWIZZLE;
57 switch(get_base_pixelformat(fmt))
61 swiz = R_TO_LUMINANCE;
65 swiz = RG_TO_LUMINANCE_ALPHA;
70 if(!get_component_size(fmt) && OES_required_internalformat)
71 fmt = get_default_sized_pixelformat(fmt);
73 require_pixelformat(fmt);
75 static Require _req(ARB_texture_swizzle);
80 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
82 if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA)
83 return get_base_pixelformat(ifmt);
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 srgb)
199 load_image(fn, 0, srgb);
202 void Texture::load_image(const string &fn, unsigned lv, bool srgb)
207 image(img, lv, srgb);
210 void Texture::image(const Graphics::Image &img, bool srgb)
215 void Texture::bind_to(unsigned i) const
220 manager->resource_used(*this);
228 TexUnit &unit = TexUnit::get_unit(i);
229 if(unit.set_texture(this))
232 manager->resource_used(*this);
234 if(ARB_direct_state_access)
235 glBindTextureUnit(i, id);
239 glBindTexture(target, id);
242 default_sampler.bind_to(i);
246 const Texture *Texture::current(unsigned i)
248 return TexUnit::get_unit(i).get_texture();
251 void Texture::unbind_from(unsigned i)
253 TexUnit &unit = TexUnit::get_unit(i);
254 const Texture *cur = unit.get_texture();
255 if(unit.set_texture(0))
257 if(ARB_direct_state_access)
258 glBindTextureUnit(i, 0);
262 glBindTexture(cur->target, 0);
268 Texture::Loader::Loader(Texture &t):
269 DataFile::CollectionObjectLoader<Texture>(t, 0)
274 Texture::Loader::Loader(Texture &t, Collection &c):
275 DataFile::CollectionObjectLoader<Texture>(t, &c)
280 void Texture::Loader::init()
283 if(Resources *res = dynamic_cast<Resources *>(coll))
284 srgb = res->get_srgb_conversion();
288 add("external_image", &Loader::external_image);
289 add("filter", &Loader::filter);
290 add("generate_mipmap", &Loader::generate_mipmap);
291 add("image_data", &Loader::image_data);
292 add("mag_filter", &Loader::mag_filter);
293 add("max_anisotropy", &Loader::max_anisotropy);
294 add("min_filter", &Loader::min_filter);
295 add("mipmap_levels", &Loader::mipmap_levels);
296 add("sampler", &Loader::sampler);
297 add("wrap", &Loader::wrap);
298 add("wrap_r", &Loader::wrap_r);
299 add("wrap_s", &Loader::wrap_s);
300 add("wrap_t", &Loader::wrap_t);
303 unsigned Texture::Loader::get_levels() const
305 return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1);
308 #pragma GCC diagnostic push
309 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
310 void Texture::Loader::external_image(const string &fn)
313 RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
315 throw IO::file_not_found(fn);
318 obj.image(img, get_levels(), srgb);
321 void Texture::Loader::filter(TextureFilter f)
326 void Texture::Loader::generate_mipmap(bool gm)
328 obj.set_auto_generate_mipmap(gm);
331 void Texture::Loader::image_data(const string &data)
334 IO::Memory mem(data.data(), data.size());
337 obj.image(img, get_levels(), srgb);
340 void Texture::Loader::mag_filter(TextureFilter f)
342 obj.set_mag_filter(f);
345 void Texture::Loader::max_anisotropy(float a)
347 obj.set_max_anisotropy(a);
350 void Texture::Loader::min_filter(TextureFilter f)
352 obj.set_min_filter(f);
355 void Texture::Loader::mipmap_levels(unsigned l)
360 void Texture::Loader::sampler()
362 load_sub(obj.default_sampler);
365 void Texture::Loader::wrap(TextureWrap w)
370 void Texture::Loader::wrap_r(TextureWrap w)
375 void Texture::Loader::wrap_s(TextureWrap w)
380 void Texture::Loader::wrap_t(TextureWrap w)
384 #pragma GCC diagnostic pop