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),
41 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
44 throw invalid_operation("Texture2D::storage");
46 throw invalid_argument("Texture2D::storage");
47 require_pixelformat(fmt);
54 void Texture2D::allocate(unsigned level)
56 if(allocated&(1<<level))
59 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
62 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
64 if(width==0 || height==0)
65 throw invalid_operation("Texture2D::image");
69 get_level_size(level, w, h);
71 BindRestore _bind(this);
72 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
74 allocated |= 1<<level;
75 if(gen_mipmap && level==0)
77 for(; (w || h); w>>=1, h>>=1, ++level) ;
78 allocated |= (1<<level)-1;
82 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
84 if(width==0 || height==0)
85 throw invalid_operation("Texture2D::sub_image");
89 BindRestore _bind(this);
90 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
93 void Texture2D::load_image(const string &fn, bool srgb)
101 void Texture2D::image(const Graphics::Image &img, bool srgb)
103 image(img, srgb, false);
106 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
108 unsigned w = img.get_width();
109 unsigned h = img.get_height();
110 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
113 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
115 f = get_srgb_pixelformat(f);
118 else if(w!=width || h!=height)
119 throw incompatible_data("Texture2D::image");
121 PixelStore pstore = PixelStore::from_image(img);
122 BindRestore _bind_ps(pstore);
124 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
127 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
138 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io)
140 return new AsyncLoader(*this, io);
143 UInt64 Texture2D::get_data_size() const
145 return id ? width*height*get_component_count(ifmt) : 0;
148 void Texture2D::unload()
150 glDeleteTextures(1, &id);
152 // TODO check which params actually need refreshing
157 Texture2D::Loader::Loader(Texture2D &t):
158 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
163 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
164 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
169 void Texture2D::Loader::init()
171 add("image_data", &Loader::image_data);
172 add("raw_data", &Loader::raw_data);
173 add("storage", &Loader::storage);
174 add("storage", &Loader::storage_b);
177 void Texture2D::Loader::image_data(const string &data)
180 IO::Memory mem(data.data(), data.size());
183 obj.image(img, srgb);
186 void Texture2D::Loader::raw_data(const string &data)
188 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
191 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
194 fmt = get_srgb_pixelformat(fmt);
195 obj.storage(fmt, w, h);
198 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
204 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
207 pixel_buffer(PIXEL_UNPACK_BUFFER),
212 bool Texture2D::AsyncLoader::needs_sync() const
217 bool Texture2D::AsyncLoader::process()
221 /* TODO Enhance the ImageLoader system so that the image can be loaded
222 directly to the buffer */
224 n_bytes = image.get_stride()*image.get_height();
228 pixel_buffer.data(n_bytes, 0);
229 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
233 const char *data = reinterpret_cast<const char *>(image.get_data());
234 copy(data, data+n_bytes, mapped_address);
238 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
239 if(!pixel_buffer.unmap())
246 glGenTextures(1, &texture.id);
247 texture.image(image, false, true);