X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbackends%2Fopengl%2Ftexture2d_backend.cpp;h=45ba78cc8f6f10c1e4e8d3b7d372b82b148843f0;hp=71a998dcd31187585fd266c1c5e4b19d1369917d;hb=6080120777fdf7b6e1184288eff9d205ca7ebf5f;hpb=160e9eea29bd10034733d59507fa1bcca36be401 diff --git a/source/backends/opengl/texture2d_backend.cpp b/source/backends/opengl/texture2d_backend.cpp index 71a998dc..45ba78cc 100644 --- a/source/backends/opengl/texture2d_backend.cpp +++ b/source/backends/opengl/texture2d_backend.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,8 @@ #include "texture2d.h" #include "texture2d_backend.h" +using namespace std; + namespace Msp { namespace GL { @@ -16,11 +19,12 @@ private: Texture2D &texture; IO::Seekable &io; Buffer pixel_buffer; - char *mapped_address; + char *mapped_address = 0; Graphics::Image image; - Graphics::ImageLoader *img_loader; - unsigned n_bytes; - int phase; + Graphics::ImageLoader *img_loader = 0; + DataFile::RawData *raw_data = 0; + unsigned n_bytes = 0; + int phase = 0; public: AsyncLoader(Texture2D &, IO::Seekable &); @@ -31,8 +35,8 @@ public: }; -OpenGLTexture2D::OpenGLTexture2D(ResourceManager *m): - Texture(GL_TEXTURE_2D, m) +OpenGLTexture2D::OpenGLTexture2D(): + Texture(GL_TEXTURE_2D) { } void OpenGLTexture2D::allocate() @@ -41,6 +45,9 @@ void OpenGLTexture2D::allocate() unsigned height = static_cast(this)->height; unsigned levels = static_cast(this)->levels; + if(!id) + create(); + GLenum gl_fmt = get_gl_pixelformat(storage_fmt); if(ARB_texture_storage) { @@ -88,11 +95,27 @@ void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } -Resource::AsyncLoader *OpenGLTexture2D::create_async_loader(IO::Seekable &io) +Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *) { return new AsyncLoader(static_cast(*this), io); } +uint64_t OpenGLTexture2D::get_data_size() const +{ + if(!id) + return 0; + + unsigned width = static_cast(this)->width; + unsigned height = static_cast(this)->height; + unsigned levels = static_cast(this)->levels; + + size_t level_size = width*height*get_pixel_size(format); + size_t total_size = level_size; + for(unsigned i=0; i>=2) + total_size += level_size; + return total_size; +} + void OpenGLTexture2D::unload() { glDeleteTextures(1, &id); @@ -102,17 +125,27 @@ void OpenGLTexture2D::unload() OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i): texture(t), - io(i), - mapped_address(0), - img_loader(Graphics::ImageLoader::open_io(io)), - phase(0) -{ } + io(i) +{ + char magic[4] = { }; + io.read(magic, 4); + io.seek(0, IO::S_BEG); + + if(DataFile::RawData::detect_signature(string(magic, 4))) + { + raw_data = new DataFile::RawData; + raw_data->open_io(io, "async"); + } + else + img_loader = Graphics::ImageLoader::open_io(io); +} OpenGLTexture2D::AsyncLoader::~AsyncLoader() { if(mapped_address) pixel_buffer.unmap(); delete img_loader; + delete raw_data; } bool OpenGLTexture2D::AsyncLoader::needs_sync() const @@ -124,16 +157,26 @@ bool OpenGLTexture2D::AsyncLoader::process() { if(phase==0) { - image.load_headers(*img_loader); - n_bytes = image.get_stride()*image.get_height(); + if(raw_data) + n_bytes = raw_data->get_size(); + else + { + image.load_headers(*img_loader); + n_bytes = image.get_stride()*image.get_height(); + } } else if(phase==1) { - pixel_buffer.storage(n_bytes); + pixel_buffer.storage(n_bytes, STREAMING); mapped_address = reinterpret_cast(pixel_buffer.map()); } else if(phase==2) - image.load_into(*img_loader, mapped_address); + { + if(raw_data) + raw_data->load_into(mapped_address); + else + image.load_into(*img_loader, mapped_address); + } else if(phase==3) { mapped_address = 0; @@ -144,12 +187,15 @@ bool OpenGLTexture2D::AsyncLoader::process() } if(!texture.id) - texture.generate_id(); + texture.create(); - unsigned w = image.get_width(); - unsigned h = image.get_height(); - texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h); - texture.OpenGLTexture2D::sub_image(0, 0, 0, w, h, pixel_buffer, 0); + if(img_loader) + { + unsigned w = image.get_width(); + unsigned h = image.get_height(); + texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h); + } + texture.OpenGLTexture2D::sub_image(0, 0, 0, texture.width, texture.height, pixel_buffer, 0); if(texture.auto_gen_mipmap) texture.generate_mipmap();