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 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
46 throw invalid_operation("Texture2D::storage");
48 throw invalid_argument("Texture2D::storage");
49 require_pixelformat(fmt);
56 void Texture2D::allocate(unsigned level)
58 if(allocated&(1<<level))
61 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
64 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
66 if(width==0 || height==0)
67 throw invalid_operation("Texture2D::image");
71 get_level_size(level, w, h);
73 BindRestore _bind(this);
74 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
76 allocated |= 1<<level;
77 if(gen_mipmap && level==0)
79 auto_generate_mipmap();
80 for(; (w || h); w>>=1, h>>=1, ++level) ;
81 allocated |= (1<<level)-1;
85 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
87 if(width==0 || height==0)
88 throw invalid_operation("Texture2D::sub_image");
92 BindRestore _bind(this);
93 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
96 void Texture2D::load_image(const string &fn, bool srgb)
104 void Texture2D::image(const Graphics::Image &img, bool srgb)
106 image(img, srgb, false);
109 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
111 unsigned w = img.get_width();
112 unsigned h = img.get_height();
113 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
116 PixelFormat f = storage_pixelformat_from_graphics(img.get_format());
118 f = get_srgb_pixelformat(f);
121 else if(w!=width || h!=height)
122 throw incompatible_data("Texture2D::image");
124 PixelStore pstore = PixelStore::from_image(img);
125 BindRestore _bind_ps(pstore);
127 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
130 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
141 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
143 AsyncLoader *ldr = new AsyncLoader(*this, io);
145 ldr->set_srgb_conversion(res->get_srgb_conversion());
149 UInt64 Texture2D::get_data_size() const
151 return id ? width*height*get_component_count(ifmt) : 0;
154 void Texture2D::unload()
156 glDeleteTextures(1, &id);
158 // TODO check which params actually need refreshing
163 Texture2D::Loader::Loader(Texture2D &t):
164 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
169 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
170 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
175 void Texture2D::Loader::init()
177 add("image_data", &Loader::image_data);
178 add("raw_data", &Loader::raw_data);
179 add("storage", &Loader::storage);
180 add("storage", &Loader::storage_b);
183 void Texture2D::Loader::image_data(const string &data)
186 IO::Memory mem(data.data(), data.size());
189 obj.image(img, srgb);
192 void Texture2D::Loader::raw_data(const string &data)
194 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
197 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
200 fmt = get_srgb_pixelformat(fmt);
201 obj.storage(fmt, w, h);
204 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
210 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
213 srgb_conversion(false),
214 pixel_buffer(PIXEL_UNPACK_BUFFER),
219 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
224 bool Texture2D::AsyncLoader::needs_sync() const
229 bool Texture2D::AsyncLoader::process()
233 /* TODO Enhance the ImageLoader system so that the image can be loaded
234 directly to the buffer */
236 n_bytes = image.get_stride()*image.get_height();
240 pixel_buffer.data(n_bytes, 0);
241 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
245 const char *data = reinterpret_cast<const char *>(image.get_data());
246 copy(data, data+n_bytes, mapped_address);
250 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
251 if(!pixel_buffer.unmap())
258 glGenTextures(1, &texture.id);
259 texture.image(image, srgb_conversion, true);