-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <png.h>
-#include "except.h"
+#include <msp/io/memory.h>
+#include "bindable.h"
+#include "buffer.h"
+#include "error.h"
+#include "pixelstore.h"
+#include "resources.h"
#include "texture2d.h"
using namespace std;
namespace Msp {
namespace GL {
-Texture2D::Texture2D()
+class Texture2D::AsyncLoader: public Resource::AsyncLoader
+{
+private:
+ Texture2D &texture;
+ IO::Seekable &io;
+ bool srgb_conversion;
+ Buffer pixel_buffer;
+ char *mapped_address;
+ Graphics::Image image;
+ unsigned n_bytes;
+ int phase;
+
+public:
+ AsyncLoader(Texture2D &, IO::Seekable &);
+
+ void set_srgb_conversion(bool);
+ virtual bool needs_sync() const;
+ virtual bool process();
+};
+
+
+Texture2D::Texture2D(ResourceManager *m):
+ Texture(GL_TEXTURE_2D, m),
+ ifmt(RGB),
+ width(0),
+ height(0),
+ allocated(0)
+{ }
+
+Texture2D::~Texture2D()
{
- target=GL_TEXTURE_2D;
+ set_manager(0);
}
-/**
-Uploads an image into the texture. Direct wrapper for glTexImage2D.
-*/
-void Texture2D::image(int level, int ifmt, sizei wd, sizei ht, int border, GLenum fmt, GLenum type, void *data)
+void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
{
- maybe_bind();
+ if(width>0)
+ throw invalid_operation("Texture2D::storage");
+ if(wd==0 || ht==0)
+ throw invalid_argument("Texture2D::storage");
+ require_pixelformat(fmt);
- glTexImage2D(target, level, ifmt, wd, ht, border, fmt, type, data);
- width_=wd;
- height_=ht;
+ ifmt = fmt;
+ width = wd;
+ height = ht;
}
-/**
-Uploads an image into the texture, with a simpler interface.
-*/
-void Texture2D::image(int level, sizei wd, sizei ht, TextureFormat tfmt, void *data)
+void Texture2D::allocate(unsigned level)
{
- int ifmt;
- int fmt;
- int type;
+ if(allocated&(1<<level))
+ return;
+
+ PixelFormat base_fmt = get_base_pixelformat(ifmt);
+ image(level, base_fmt, get_alloc_type(base_fmt), 0);
+}
- switch(tfmt)
+void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
+{
+ if(width==0 || height==0)
+ throw invalid_operation("Texture2D::image");
+
+ unsigned w = width;
+ unsigned h = height;
+ get_level_size(level, w, h);
+
+ BindRestore _bind(this);
+ glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
+
+ allocated |= 1<<level;
+ if(gen_mipmap && level==0)
{
- case LUMINANCE8: ifmt=GL_LUMINANCE; fmt=GL_LUMINANCE; type=GL_UNSIGNED_BYTE; break;
- case LUMINANCE8_ALPHA8: ifmt=GL_LUMINANCE_ALPHA; fmt=GL_LUMINANCE_ALPHA; type=GL_UNSIGNED_BYTE; break;
- case RGB8: ifmt=GL_RGB; fmt=GL_RGB; type=GL_UNSIGNED_BYTE; break;
- case RGBA8: ifmt=GL_RGBA; fmt=GL_RGBA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
- case BGR8: ifmt=GL_RGB; fmt=GL_BGR; type=GL_UNSIGNED_BYTE; break;
- case BGRA8: ifmt=GL_RGBA; fmt=GL_BGRA; type=GL_UNSIGNED_INT_8_8_8_8_REV; break;
- default: throw InvalidParameterValue("Invalid texture format");
+ auto_generate_mipmap();
+ for(; (w || h); w>>=1, h>>=1, ++level) ;
+ allocated |= (1<<level)-1;
}
+}
- image(level, ifmt, wd, ht, 0, fmt, type, data);
+void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
+{
+ if(width==0 || height==0)
+ throw invalid_operation("Texture2D::sub_image");
+
+ allocate(level);
+
+ BindRestore _bind(this);
+ glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
}
-/**
-Loads an image from a file and uploads it into the texture. Currently assumes
-the file to be a PNG image.
-*/
-void Texture2D::image(const string &fn)
+void Texture2D::load_image(const string &fn, bool srgb)
{
- FILE *file=fopen(fn.c_str(), "r");
- if(!file) throw Exception("Couldn't open "+fn);
+ Graphics::Image img;
+ img.load_file(fn);
- char sig[8];
- fread(sig, 8, 1, file);
- if(png_sig_cmp((png_byte *)sig, 0, 8))
- throw Exception("Not a PNG image");
+ image(img, srgb);
+}
- png_struct *pngs=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
- png_info *pngi=png_create_info_struct(pngs);
+void Texture2D::image(const Graphics::Image &img, bool srgb)
+{
+ image(img, srgb, false);
+}
- if(setjmp(png_jmpbuf(pngs)))
+void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
+{
+ unsigned w = img.get_width();
+ unsigned h = img.get_height();
+ PixelFormat fmt = pixelformat_from_graphics(img.get_format());
+ if(width==0)
{
- png_destroy_read_struct(&pngs, &pngi, 0);
- fclose(file);
- throw Exception("PNG error");
+ PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
+ if(srgb)
+ f = get_srgb_pixelformat(f);
+ storage(f, w, h);
}
- png_init_io(pngs, file);
- png_set_sig_bytes(pngs, 8);
+ else if(w!=width || h!=height)
+ throw incompatible_data("Texture2D::image");
- png_read_info(pngs, pngi);
+ PixelStore pstore = PixelStore::from_image(img);
+ BindRestore _bind_ps(pstore);
- unsigned wd=png_get_image_width(pngs, pngi);
- unsigned ht=png_get_image_height(pngs, pngi);
- unsigned depth=png_get_bit_depth(pngs, pngi);
- unsigned ctype=png_get_color_type(pngs, pngi);
+ image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
+}
- if(ctype==PNG_COLOR_TYPE_PALETTE || depth<8)
- {
- png_destroy_read_struct(&pngs, &pngi, 0);
- fclose(file);
- throw Exception("Invalid color type or bit depth");
- }
+void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
+{
+ w >>= level;
+ h >>= level;
- if(depth==16)
- png_set_strip_16(pngs);
+ if(!w && h)
+ w = 1;
+ else if(!h && w)
+ h = 1;
+}
- TextureFormat fmt;
- unsigned planes;
+Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
+{
+ AsyncLoader *ldr = new AsyncLoader(*this, io);
+ if(res)
+ ldr->set_srgb_conversion(res->get_srgb_conversion());
+ return ldr;
+}
- switch(ctype)
- {
- case PNG_COLOR_TYPE_GRAY: fmt=LUMINANCE8; planes=1; break;
- case PNG_COLOR_TYPE_GRAY_ALPHA: fmt=LUMINANCE8_ALPHA8; planes=2; break;
- case PNG_COLOR_TYPE_RGB: fmt=RGB8; planes=3; break;
- case PNG_COLOR_TYPE_RGB_ALPHA: fmt=RGBA8; planes=4; break;
- default: throw Exception("Invalid color type");
- }
+UInt64 Texture2D::get_data_size() const
+{
+ return id ? width*height*get_component_count(ifmt) : 0;
+}
+
+void Texture2D::unload()
+{
+ glDeleteTextures(1, &id);
+ id = 0;
+ // TODO check which params actually need refreshing
+ dirty_params = -1;
+}
- png_byte *data=new png_byte[wd*ht*planes];
- png_byte *row_ptrs[ht];
- for(unsigned i=0; i<ht; ++i)
- row_ptrs[i]=data+(ht-1-i)*wd*planes;
- png_read_image(pngs, row_ptrs);
+Texture2D::Loader::Loader(Texture2D &t):
+ DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
+{
+ init();
+}
- image(0, wd, ht, fmt, data);
- delete[] data;
+Texture2D::Loader::Loader(Texture2D &t, Collection &c):
+ DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
+{
+ init();
+}
+
+void Texture2D::Loader::init()
+{
+ add("image_data", &Loader::image_data);
+ add("raw_data", &Loader::raw_data);
+ add("storage", &Loader::storage);
+ add("storage", &Loader::storage_b);
+}
+
+void Texture2D::Loader::image_data(const string &data)
+{
+ Graphics::Image img;
+ IO::Memory mem(data.data(), data.size());
+ img.load_io(mem);
+
+ obj.image(img, srgb);
+}
+
+void Texture2D::Loader::raw_data(const string &data)
+{
+ obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
+}
+
+void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
+{
+ if(srgb)
+ fmt = get_srgb_pixelformat(fmt);
+ obj.storage(fmt, w, h);
+}
+
+void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
+{
+ storage(fmt, w, h);
+}
+
+
+Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
+ texture(t),
+ io(i),
+ srgb_conversion(false),
+ pixel_buffer(PIXEL_UNPACK_BUFFER),
+ mapped_address(0),
+ phase(0)
+{ }
+
+void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
+{
+ srgb_conversion = c;
+}
+
+bool Texture2D::AsyncLoader::needs_sync() const
+{
+ return phase%2;
+}
+
+bool Texture2D::AsyncLoader::process()
+{
+ if(phase==0)
+ {
+ /* TODO Enhance the ImageLoader system so that the image can be loaded
+ directly to the buffer */
+ image.load_io(io);
+ n_bytes = image.get_stride()*image.get_height();
+ }
+ else if(phase==1)
+ {
+ pixel_buffer.data(n_bytes, 0);
+ mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
+ }
+ else if(phase==2)
+ {
+ const char *data = reinterpret_cast<const char *>(image.get_data());
+ copy(data, data+n_bytes, mapped_address);
+ }
+ else if(phase==3)
+ {
+ Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
+ if(!pixel_buffer.unmap())
+ {
+ phase = 1;
+ return false;
+ }
+
+ if(!texture.id)
+ glGenTextures(1, &texture.id);
+ texture.image(image, srgb_conversion, true);
+ }
- png_destroy_read_struct(&pngs, &pngi, 0);
- fclose(file);
+ ++phase;
+ return phase>3;
}
} // namespace GL