X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Ftexture.cpp;h=85f5fb5836ebdcdb56af17191343a9c2f21be9ea;hb=f19366d32cc29287a2730cfba90893e407754081;hp=ea503ed1f9d4414f28f200c89be286fdc49f6190;hpb=dbc91b65728ab9c0e574bb1127cfe4d2da55de7f;p=libs%2Fgl.git diff --git a/source/core/texture.cpp b/source/core/texture.cpp index ea503ed1..85f5fb58 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -1,65 +1,23 @@ -#include -#include -#include -#include #include -#include "bindable.h" #include "error.h" #include "resourcemanager.h" -#include "resources.h" #include "texture.h" -#include "texunit.h" using namespace std; namespace Msp { namespace GL { -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_BLUE, GL_GREEN, GL_RED, GL_ALPHA -}; - -Texture::Texture(GLenum t, ResourceManager *m): - id(0), - target(t), - format(RGB8), - storage_fmt(RGB8), +Texture::Texture(unsigned t, ResourceManager *m): + TextureBackend(t, !m), + format(NO_PIXELFORMAT), + storage_fmt(format), swizzle(NO_SWIZZLE), use_srgb_format(false), auto_gen_mipmap(false) { if(m) set_manager(m); - else - generate_id(); -} - -Texture::~Texture() -{ - while(TexUnit *unit = TexUnit::find_unit(this)) - unbind_from(unit->get_index()); - - if(id) - glDeleteTextures(1, &id); -} - -void Texture::generate_id() -{ - if(id) - throw invalid_operation("Texture::generate_id"); - if(ARB_direct_state_access) - glCreateTextures(target, 1, &id); - else - glGenTextures(1, &id); - -#ifdef DEBUG - if(!debug_name.empty() && KHR_debug) - glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str()); -#endif } void Texture::set_format(PixelFormat fmt) @@ -91,74 +49,13 @@ void Texture::set_format(PixelFormat fmt) PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt)); require_pixelformat(st_fmt); if(swiz!=NO_SWIZZLE) - static Require _req(ARB_texture_swizzle); + require_swizzle(); format = fmt; storage_fmt = st_fmt; swizzle = swiz; } -void Texture::apply_swizzle() -{ - if(swizzle==NO_SWIZZLE) - return; - - if(get_gl_api()==OPENGL_ES2) - { - set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]); - set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]); - set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]); - set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]); - } - else - { - if(ARB_direct_state_access) - glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4); - else - glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4); - } -} - -void Texture::set_parameter_i(GLenum param, int value) const -{ - if(ARB_direct_state_access) - glTextureParameteri(id, param, value); - else - glTexParameteri(target, param, value); -} - -bool Texture::can_generate_mipmap() -{ - return EXT_framebuffer_object; -} - -void Texture::generate_mipmap() -{ - // glGenerateMipmap is defined here - static Require _req(EXT_framebuffer_object); - - if(ARB_direct_state_access) - glGenerateTextureMipmap(id); - else - { - BindRestore _bind(this); - glGenerateMipmap(target); - } -} - -void Texture::set_auto_generate_mipmap(bool gm) -{ - if(gm) - static Require _req(EXT_framebuffer_object); - - auto_gen_mipmap = gm; -} - -void Texture::load_image(const string &fn, bool) -{ - load_image(fn, 0U); -} - void Texture::load_image(const string &fn, unsigned lv) { Graphics::Image img; @@ -167,89 +64,11 @@ void Texture::load_image(const string &fn, unsigned lv) image(img, lv); } -void Texture::image(const Graphics::Image &img, bool) -{ - image(img, 0U); -} -void Texture::bind_to(unsigned i) const +Texture::Loader::Loader(Texture &t, Collection *c): + CollectionObjectLoader(t, c), + levels(0) { - if(!id) - { - if(manager) - manager->resource_used(*this); - if(!id) - { - unbind_from(i); - return; - } - } - - TexUnit &unit = TexUnit::get_unit(i); - if(unit.set_texture(this)) - { - if(manager) - manager->resource_used(*this); - - if(ARB_direct_state_access) - glBindTextureUnit(i, id); - else - { - unit.bind(); - glBindTexture(target, id); - } - } -} - -const Texture *Texture::current(unsigned i) -{ - return TexUnit::get_unit(i).get_texture(); -} - -void Texture::unbind_from(unsigned i) -{ - TexUnit &unit = TexUnit::get_unit(i); - const Texture *cur = unit.get_texture(); - if(unit.set_texture(0)) - { - if(ARB_direct_state_access) - glBindTextureUnit(i, 0); - else - { - unit.bind(); - glBindTexture(cur->target, 0); - } - } -} - -void Texture::set_debug_name(const string &name) -{ -#ifdef DEBUG - debug_name = name; - if(id && KHR_debug) - glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str()); -#else - (void)name; -#endif -} - - -Texture::Loader::Loader(Texture &t): - DataFile::CollectionObjectLoader(t, 0) -{ - init(); -} - -Texture::Loader::Loader(Texture &t, Collection &c): - DataFile::CollectionObjectLoader(t, &c) -{ - init(); -} - -void Texture::Loader::init() -{ - levels = 0; - add("external_image", &Loader::external_image); add("external_image_srgb", &Loader::external_image_srgb); add("generate_mipmap", &Loader::generate_mipmap); @@ -257,6 +76,12 @@ void Texture::Loader::init() add("mipmap_levels", &Loader::mipmap_levels); } +void Texture::Loader::finish() +{ + if(obj.auto_gen_mipmap) + obj.generate_mipmap(); +} + void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn) { RefPtr io = get_collection().open_raw(fn); @@ -291,7 +116,7 @@ void Texture::Loader::external_image_common(const string &fn) void Texture::Loader::generate_mipmap(bool gm) { - obj.set_auto_generate_mipmap(gm); + obj.auto_gen_mipmap = gm; } void Texture::Loader::image_data(const string &data)