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 PixelFormat base_fmt = get_base_pixelformat(ifmt);
67 image(level, base_fmt, get_alloc_type(base_fmt), 0);
70 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
72 if(width==0 || height==0)
73 throw invalid_operation("Texture2D::image");
77 get_level_size(level, w, h);
79 BindRestore _bind(this);
80 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
82 allocated |= 1<<level;
83 if(gen_mipmap && level==0)
85 auto_generate_mipmap();
86 for(; (w || h); w>>=1, h>>=1, ++level) ;
87 allocated |= (1<<level)-1;
91 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
93 if(width==0 || height==0)
94 throw invalid_operation("Texture2D::sub_image");
98 BindRestore _bind(this);
99 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
102 void Texture2D::load_image(const string &fn, bool srgb)
110 void Texture2D::image(const Graphics::Image &img, bool srgb)
112 image(img, srgb, false);
115 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
117 unsigned w = img.get_width();
118 unsigned h = img.get_height();
119 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
121 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
122 else if(w!=width || h!=height)
123 throw incompatible_data("Texture2D::image");
125 PixelStore pstore = PixelStore::from_image(img);
126 BindRestore _bind_ps(pstore);
128 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
131 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
142 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
144 AsyncLoader *ldr = new AsyncLoader(*this, io);
146 ldr->set_srgb_conversion(res->get_srgb_conversion());
150 UInt64 Texture2D::get_data_size() const
152 return id ? width*height*get_component_count(ifmt) : 0;
155 void Texture2D::unload()
157 glDeleteTextures(1, &id);
159 // TODO check which params actually need refreshing
164 Texture2D::Loader::Loader(Texture2D &t):
165 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
170 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
171 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
176 void Texture2D::Loader::init()
178 add("image_data", &Loader::image_data);
179 add("raw_data", &Loader::raw_data);
180 add("storage", &Loader::storage);
181 add("storage", &Loader::storage_b);
184 void Texture2D::Loader::image_data(const string &data)
187 IO::Memory mem(data.data(), data.size());
190 obj.image(img, srgb);
193 void Texture2D::Loader::raw_data(const string &data)
195 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
198 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
201 fmt = get_srgb_pixelformat(fmt);
202 obj.storage(fmt, w, h);
205 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
211 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
214 srgb_conversion(false),
215 pixel_buffer(PIXEL_UNPACK_BUFFER),
220 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
225 bool Texture2D::AsyncLoader::needs_sync() const
230 bool Texture2D::AsyncLoader::process()
234 /* TODO Enhance the ImageLoader system so that the image can be loaded
235 directly to the buffer */
237 n_bytes = image.get_stride()*image.get_height();
241 pixel_buffer.data(n_bytes, 0);
242 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
246 const char *data = reinterpret_cast<const char *>(image.get_data());
247 copy(data, data+n_bytes, mapped_address);
251 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
252 if(!pixel_buffer.unmap())
259 glGenTextures(1, &texture.id);
260 texture.image(image, srgb_conversion, true);