1 #include <msp/io/memory.h>
5 #include "pixelstore.h"
14 class Texture2D::AsyncLoader: public Resource::AsyncLoader
22 Graphics::Image image;
27 AsyncLoader(Texture2D &, IO::Seekable &);
29 void set_srgb_conversion(bool);
30 virtual bool needs_sync() const;
31 virtual bool process();
35 Texture2D::Texture2D(ResourceManager *m):
36 Texture(GL_TEXTURE_2D, m),
43 Texture2D::~Texture2D()
48 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
51 throw invalid_operation("Texture2D::storage");
53 throw invalid_argument("Texture2D::storage");
54 require_pixelformat(fmt);
61 void Texture2D::allocate(unsigned level)
63 if(allocated&(1<<level))
66 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
69 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
71 if(width==0 || height==0)
72 throw invalid_operation("Texture2D::image");
76 get_level_size(level, w, h);
78 BindRestore _bind(this);
79 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
81 allocated |= 1<<level;
82 if(gen_mipmap && level==0)
84 auto_generate_mipmap();
85 for(; (w || h); w>>=1, h>>=1, ++level) ;
86 allocated |= (1<<level)-1;
90 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
92 if(width==0 || height==0)
93 throw invalid_operation("Texture2D::sub_image");
97 BindRestore _bind(this);
98 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
101 void Texture2D::load_image(const string &fn, bool srgb)
109 void Texture2D::image(const Graphics::Image &img, bool srgb)
111 image(img, srgb, false);
114 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
116 unsigned w = img.get_width();
117 unsigned h = img.get_height();
118 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
121 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
123 f = get_srgb_pixelformat(f);
126 else if(w!=width || h!=height)
127 throw incompatible_data("Texture2D::image");
129 PixelStore pstore = PixelStore::from_image(img);
130 BindRestore _bind_ps(pstore);
132 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
135 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
146 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
148 AsyncLoader *ldr = new AsyncLoader(*this, io);
150 ldr->set_srgb_conversion(res->get_srgb_conversion());
154 UInt64 Texture2D::get_data_size() const
156 return id ? width*height*get_component_count(ifmt) : 0;
159 void Texture2D::unload()
161 glDeleteTextures(1, &id);
163 // TODO check which params actually need refreshing
168 Texture2D::Loader::Loader(Texture2D &t):
169 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
174 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
175 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
180 void Texture2D::Loader::init()
182 add("image_data", &Loader::image_data);
183 add("raw_data", &Loader::raw_data);
184 add("storage", &Loader::storage);
185 add("storage", &Loader::storage_b);
188 void Texture2D::Loader::image_data(const string &data)
191 IO::Memory mem(data.data(), data.size());
194 obj.image(img, srgb);
197 void Texture2D::Loader::raw_data(const string &data)
199 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
202 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
205 fmt = get_srgb_pixelformat(fmt);
206 obj.storage(fmt, w, h);
209 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
215 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
218 srgb_conversion(false),
219 pixel_buffer(PIXEL_UNPACK_BUFFER),
224 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
229 bool Texture2D::AsyncLoader::needs_sync() const
234 bool Texture2D::AsyncLoader::process()
238 /* TODO Enhance the ImageLoader system so that the image can be loaded
239 directly to the buffer */
241 n_bytes = image.get_stride()*image.get_height();
245 pixel_buffer.data(n_bytes, 0);
246 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
250 const char *data = reinterpret_cast<const char *>(image.get_data());
251 copy(data, data+n_bytes, mapped_address);
255 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
256 if(!pixel_buffer.unmap())
263 glGenTextures(1, &texture.id);
264 texture.image(image, srgb_conversion, true);