X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftexture.cpp;h=f6594ab14c7e5143434ac0823c862427c70f2c9d;hp=90e3bb17f11de1c318f6670a28ce61a17a13a88b;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=73eec11d44a24bac121f1b0d85f20d58005f3545 diff --git a/source/texture.cpp b/source/texture.cpp index 90e3bb17..f6594ab1 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -18,14 +18,17 @@ int Texture::swizzle_orders[] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RED, GL_RED, GL_RED, GL_ONE, - GL_RED, GL_RED, GL_RED, GL_GREEN + GL_RED, GL_RED, GL_RED, GL_GREEN, + GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA }; Texture::Texture(GLenum t, ResourceManager *m): id(0), target(t), - ifmt(RGB), + format(RGB8), + storage_fmt(RGB8), swizzle(NO_SWIZZLE), + use_srgb_format(false), auto_gen_mipmap(false), default_sampler(*this) { @@ -46,41 +49,40 @@ Texture::~Texture() glDeleteTextures(1, &id); } -DataType Texture::get_alloc_type(PixelFormat fmt) +void Texture::set_format(PixelFormat fmt) { - return (get_base_pixelformat(fmt)==DEPTH_COMPONENT ? UNSIGNED_SHORT : UNSIGNED_BYTE); -} - -void Texture::set_internal_format(PixelFormat fmt) -{ - swizzle = NO_SWIZZLE; - if(ARB_texture_rg && ARB_texture_swizzle) + PixelComponents comp = get_components(fmt); + PixelComponents st_comp = comp; + FormatSwizzle swiz = NO_SWIZZLE; + switch(comp) { - if(fmt==LUMINANCE) - { - fmt = RED; - swizzle = R_TO_LUMINANCE; - } - else if(fmt==LUMINANCE_ALPHA) - { - fmt = RG; - swizzle = RG_TO_LUMINANCE_ALPHA; - } + case LUMINANCE: + st_comp = RED; + swiz = R_TO_LUMINANCE; + break; + case LUMINANCE_ALPHA: + st_comp = RG; + swiz = RG_TO_LUMINANCE_ALPHA; + break; + case BGR: + st_comp = RGB; + swiz = RGB_TO_BGR; + break; + case BGRA: + st_comp = RGBA; + swiz = RGB_TO_BGR; + break; + default:; } - if(!get_component_size(fmt) && OES_required_internalformat) - fmt = get_default_sized_pixelformat(fmt); + PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt)); + require_pixelformat(st_fmt); + if(swiz!=NO_SWIZZLE) + static Require _req(ARB_texture_swizzle); - require_pixelformat(fmt); - ifmt = fmt; -} - -PixelFormat Texture::get_upload_format(PixelFormat fmt) const -{ - if(fmt==LUMINANCE || fmt==LUMINANCE_ALPHA) - return get_base_pixelformat(ifmt); - else - return fmt; + format = fmt; + storage_fmt = st_fmt; + swizzle = swiz; } void Texture::apply_swizzle() @@ -192,22 +194,22 @@ void Texture::set_compare_func(Predicate f) default_sampler.set_compare(f); } -void Texture::load_image(const string &fn, bool srgb) +void Texture::load_image(const string &fn, bool) { - load_image(fn, 0, srgb); + load_image(fn, 0U); } -void Texture::load_image(const string &fn, unsigned lv, bool srgb) +void Texture::load_image(const string &fn, unsigned lv) { Graphics::Image img; img.load_file(fn); - image(img, lv, srgb); + image(img, lv); } -void Texture::image(const Graphics::Image &img, bool srgb) +void Texture::image(const Graphics::Image &img, bool) { - image(img, 0, srgb); + image(img, 0U); } void Texture::bind_to(unsigned i) const @@ -224,6 +226,7 @@ void Texture::bind_to(unsigned i) const } TexUnit &unit = TexUnit::get_unit(i); + const Texture *cur = unit.get_texture(); if(unit.set_texture(this)) { if(manager) @@ -237,7 +240,8 @@ void Texture::bind_to(unsigned i) const glBindTexture(target, id); } - default_sampler.bind_to(i); + if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler) + default_sampler.bind_to(i); } } @@ -259,6 +263,9 @@ void Texture::unbind_from(unsigned i) unit.bind(); glBindTexture(cur->target, 0); } + + if(unit.get_sampler()==&cur->default_sampler) + Sampler::unbind_from(i); } } @@ -278,12 +285,9 @@ Texture::Loader::Loader(Texture &t, Collection &c): void Texture::Loader::init() { levels = 0; - if(Resources *res = dynamic_cast(coll)) - srgb = res->get_srgb_conversion(); - else - srgb = false; add("external_image", &Loader::external_image); + add("external_image_srgb", &Loader::external_image); add("filter", &Loader::filter); add("generate_mipmap", &Loader::generate_mipmap); add("image_data", &Loader::image_data); @@ -305,15 +309,36 @@ unsigned Texture::Loader::get_levels() const #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -void Texture::Loader::external_image(const string &fn) +void Texture::Loader::load_external_image(Graphics::Image &img, const std::string &fn) { - Graphics::Image img; RefPtr io = get_collection().open_raw(fn); if(!io) throw IO::file_not_found(fn); img.load_io(*io); +} + +void Texture::Loader::external_image(const string &fn) +{ + obj.use_srgb_format = false; + external_image_common(fn); +} - obj.image(img, get_levels(), srgb); +void Texture::Loader::external_image_srgb(const string &fn) +{ + obj.use_srgb_format = true; + external_image_common(fn); +} + +void Texture::Loader::external_image_common(const string &fn) +{ + if(obj.manager) + obj.manager->set_resource_location(obj, get_collection(), fn); + else + { + Graphics::Image img; + load_external_image(img, fn); + obj.image(img, get_levels()); + } } void Texture::Loader::filter(TextureFilter f) @@ -332,7 +357,7 @@ void Texture::Loader::image_data(const string &data) IO::Memory mem(data.data(), data.size()); img.load_io(mem); - obj.image(img, get_levels(), srgb); + obj.image(img, get_levels()); } void Texture::Loader::mag_filter(TextureFilter f)