X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbackends%2Fopengl%2Ftexture2d_backend.cpp;h=a4e3cad64c737d78a840b15930542e7cf7e6838f;hb=HEAD;hp=9efc4351e65b47e8c6a08dda88d5cb3d344e4565;hpb=710363bade528b5015a7c472db3aa90eb08207ab;p=libs%2Fgl.git diff --git a/source/backends/opengl/texture2d_backend.cpp b/source/backends/opengl/texture2d_backend.cpp index 9efc4351..a4e3cad6 100644 --- a/source/backends/opengl/texture2d_backend.cpp +++ b/source/backends/opengl/texture2d_backend.cpp @@ -1,45 +1,23 @@ #include #include #include -#include #include "buffer.h" #include "gl.h" #include "texture2d.h" #include "texture2d_backend.h" +using namespace std; + namespace Msp { namespace GL { -class OpenGLTexture2D::AsyncLoader: public Resource::AsyncLoader -{ -private: - Texture2D &texture; - IO::Seekable &io; - Buffer pixel_buffer; - char *mapped_address = 0; - Graphics::Image image; - Graphics::ImageLoader *img_loader = 0; - unsigned n_bytes = 0; - int phase = 0; - -public: - AsyncLoader(Texture2D &, IO::Seekable &); - ~AsyncLoader(); - - virtual bool needs_sync() const; - virtual bool process(); -}; - - OpenGLTexture2D::OpenGLTexture2D(): Texture(GL_TEXTURE_2D) { } void OpenGLTexture2D::allocate() { - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned levels = static_cast(this)->levels; + const Texture2D &self = *static_cast(this); if(!id) create(); @@ -48,22 +26,22 @@ void OpenGLTexture2D::allocate() if(ARB_texture_storage) { if(ARB_direct_state_access) - glTextureStorage2D(id, levels, gl_fmt, width, height); + glTextureStorage2D(id, n_levels, gl_fmt, self.width, self.height); else { bind_scratch(); - glTexStorage2D(target, levels, gl_fmt, width, height); + glTexStorage2D(target, n_levels, gl_fmt, self.width, self.height); } } else { bind_scratch(); - glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1); + glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, n_levels-1); GLenum comp = get_gl_components(get_components(storage_fmt)); GLenum type = get_gl_type(get_component_type(storage_fmt)); - for(unsigned i=0; i(this)->get_level_size(i); + auto lv_size = self.get_level_size(i); glTexImage2D(target, i, gl_fmt, lv_size.x, lv_size.y, 0, comp, type, 0); } } @@ -84,16 +62,18 @@ void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig } } -void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const Buffer &buffer, unsigned offset) +uint64_t OpenGLTexture2D::get_data_size() const { - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer.id); - sub_image(level, x, y, wd, ht, reinterpret_cast(offset)); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); -} + if(!id) + return 0; -Resource::AsyncLoader *OpenGLTexture2D::create_async_loader(IO::Seekable &io) -{ - return new AsyncLoader(static_cast(*this), io); + const Texture2D &self = *static_cast(this); + + size_t level_size = self.width*self.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() @@ -103,61 +83,44 @@ void OpenGLTexture2D::unload() } -OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i): - texture(t), - io(i), - img_loader(Graphics::ImageLoader::open_io(io)) -{ } +OpenGLTexture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other): + pixel_buffer(other.pixel_buffer) +{ + other.pixel_buffer = 0; +} -OpenGLTexture2D::AsyncLoader::~AsyncLoader() +OpenGLTexture2D::AsyncTransfer &OpenGLTexture2D::AsyncTransfer::operator=(AsyncTransfer &&other) { - if(mapped_address) - pixel_buffer.unmap(); - delete img_loader; + delete pixel_buffer; + pixel_buffer = other.pixel_buffer; + other.pixel_buffer = 0; + + return *this; } -bool OpenGLTexture2D::AsyncLoader::needs_sync() const +OpenGLTexture2D::AsyncTransfer::~AsyncTransfer() { - return phase%2; + delete pixel_buffer; } -bool OpenGLTexture2D::AsyncLoader::process() +void *OpenGLTexture2D::AsyncTransfer::allocate() { - if(phase==0) - { - image.load_headers(*img_loader); - n_bytes = image.get_stride()*image.get_height(); - } - else if(phase==1) - { - pixel_buffer.storage(n_bytes); - mapped_address = reinterpret_cast(pixel_buffer.map()); - } - else if(phase==2) - image.load_into(*img_loader, mapped_address); - else if(phase==3) - { - mapped_address = 0; - if(!pixel_buffer.unmap()) - { - phase = 1; - return false; - } + const Texture2D::AsyncTransfer &self = *static_cast(this); - if(!texture.id) - texture.create(); + pixel_buffer = new Buffer; + pixel_buffer->storage(self.data_size, STREAMING); + return pixel_buffer->map(); +} - 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); +void OpenGLTexture2D::AsyncTransfer::finalize() +{ + const Texture2D::AsyncTransfer &self = *static_cast(this); - if(texture.auto_gen_mipmap) - texture.generate_mipmap(); - } + pixel_buffer->unmap(); - ++phase; - return phase>3; + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer->id); + self.texture->OpenGLTexture2D::sub_image(self.level, self.x, self.y, self.width, self.height, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } } // namespace GL