1 #include <msp/io/memory.h>
5 #include "pixelstore.h"
14 class Texture2D::AsyncLoader: public Resource::AsyncLoader
21 Graphics::Image image;
26 AsyncLoader(Texture2D &, IO::Seekable &);
28 virtual bool needs_sync() const;
29 virtual bool process();
33 Texture2D::Texture2D(ResourceManager *m):
34 Texture(GL_TEXTURE_2D, m),
40 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
43 throw invalid_operation("Texture2D::storage");
45 throw invalid_argument("Texture2D::storage");
46 require_pixelformat(fmt);
53 void Texture2D::allocate(unsigned level)
55 if(allocated&(1<<level))
58 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
61 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
63 if(width==0 || height==0)
64 throw invalid_operation("Texture2D::image");
68 get_level_size(level, w, h);
70 BindRestore _bind(this);
71 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
73 allocated |= 1<<level;
74 if(gen_mipmap && level==0)
76 for(; (w || h); w>>=1, h>>=1, ++level) ;
77 allocated |= (1<<level)-1;
81 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
83 if(width==0 || height==0)
84 throw invalid_operation("Texture2D::sub_image");
88 BindRestore _bind(this);
89 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
92 void Texture2D::load_image(const string &fn, bool srgb)
100 void Texture2D::image(const Graphics::Image &img, bool srgb)
102 image(img, srgb, false);
105 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
107 unsigned w = img.get_width();
108 unsigned h = img.get_height();
109 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
114 f = get_srgb_pixelformat(f);
117 else if(w!=width || h!=height)
118 throw incompatible_data("Texture2D::image");
120 PixelStore pstore = PixelStore::from_image(img);
121 BindRestore _bind_ps(pstore);
123 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
126 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
137 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io)
139 return new AsyncLoader(*this, io);
142 void Texture2D::unload()
144 glDeleteTextures(1, &id);
149 Texture2D::Loader::Loader(Texture2D &t):
150 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
155 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
156 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
161 void Texture2D::Loader::init()
163 add("image_data", &Loader::image_data);
164 add("raw_data", &Loader::raw_data);
165 add("storage", &Loader::storage);
166 add("storage", &Loader::storage_b);
169 void Texture2D::Loader::image_data(const string &data)
172 IO::Memory mem(data.data(), data.size());
175 obj.image(img, srgb);
178 void Texture2D::Loader::raw_data(const string &data)
180 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
183 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
186 fmt = get_srgb_pixelformat(fmt);
187 obj.storage(fmt, w, h);
190 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
196 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
199 pixel_buffer(PIXEL_UNPACK_BUFFER),
204 glGenTextures(1, &texture.id);
207 bool Texture2D::AsyncLoader::needs_sync() const
212 bool Texture2D::AsyncLoader::process()
216 /* TODO Enhance the ImageLoader system so that the image can be loaded
217 directly to the buffer */
219 n_bytes = image.get_stride()*image.get_height();
223 pixel_buffer.data(n_bytes, 0);
224 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
228 const char *data = reinterpret_cast<const char *>(image.get_data());
229 copy(data, data+n_bytes, mapped_address);
233 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
234 if(!pixel_buffer.unmap())
240 texture.image(image, false, true);