1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/graphics/imageloader.h>
8 #include "pixelstore.h"
10 #include "texture2d.h"
17 class Texture2D::AsyncLoader: public Resource::AsyncLoader
24 Graphics::Image image;
25 Graphics::ImageLoader *img_loader;
30 AsyncLoader(Texture2D &, IO::Seekable &);
33 virtual bool needs_sync() const;
34 virtual bool process();
38 Texture2D::Texture2D(ResourceManager *m):
39 Texture(GL_TEXTURE_2D, m),
45 Texture2D::~Texture2D()
50 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
54 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
55 throw incompatible_data("Texture2D::storage");
59 throw invalid_argument("Texture2D::storage");
64 levels = get_n_levels();
66 levels = min(levels, lv);
69 void Texture2D::allocate(unsigned level)
71 if(width==0 || height==0)
72 throw invalid_operation("Texture2D::allocate");
74 throw invalid_argument("Texture2D::allocate");
76 bool direct = ARB_texture_storage && ARB_direct_state_access;
79 glActiveTexture(GL_TEXTURE0);
80 glBindTexture(target, id);
86 glBindTexture(target, 0);
89 void Texture2D::allocate_(unsigned level)
91 if(allocated&(1<<level))
94 if(ARB_texture_storage)
96 if(ARB_direct_state_access)
97 glTextureStorage2D(id, levels, storage_fmt, width, height);
99 glTexStorage2D(target, levels, storage_fmt, width, height);
101 allocated |= (1<<levels)-1;
107 void Texture2D::image(unsigned level, const void *data)
109 if(width==0 || height==0)
110 throw invalid_operation("Texture2D::image");
112 throw out_of_range("Texture2D::image");
114 if(ARB_texture_storage)
116 LinAl::Vector<unsigned, 2> size = get_level_size(level);
117 return sub_image(level, 0, 0, size.x, size.y, data);
120 glActiveTexture(GL_TEXTURE0);
121 glBindTexture(target, id);
125 if(auto_gen_mipmap && level==0)
128 allocated |= (1<<levels)-1;
131 glBindTexture(target, 0);
134 void Texture2D::image_(unsigned level, const void *data)
138 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
142 LinAl::Vector<unsigned, 2> size = get_level_size(level);
143 PixelComponents comp = get_components(storage_fmt);
144 GLenum type = get_gl_type(get_component_type(storage_fmt));
145 glTexImage2D(target, level, storage_fmt, size.x, size.y, 0, comp, type, data);
147 allocated |= 1<<level;
150 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
152 if(comp!=get_components(format) || type!=get_component_type(format))
153 throw incompatible_data("Texture2D::image");
157 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
159 if(width==0 || height==0)
160 throw invalid_operation("Texture2D::sub_image");
162 throw out_of_range("Texture2D::sub_image");
164 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
167 glActiveTexture(GL_TEXTURE0);
168 glBindTexture(target, id);
173 PixelComponents comp = get_components(storage_fmt);
174 GLenum type = get_gl_type(get_component_type(storage_fmt));
175 if(ARB_direct_state_access)
176 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
178 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
180 if(auto_gen_mipmap && level==0)
184 glBindTexture(target, 0);
187 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
189 if(comp!=get_components(format) || type!=get_component_type(format))
190 throw incompatible_data("Texture2D::sub_image");
191 sub_image(level, x, y, wd, ht, data);
194 void Texture2D::image(const Graphics::Image &img, unsigned lv)
196 image(img, lv, false);
199 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
201 unsigned w = img.get_width();
202 unsigned h = img.get_height();
203 PixelFormat fmt = pixelformat_from_image(img);
204 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
206 PixelStore pstore = PixelStore::from_image(img);
207 BindRestore _bind_ps(pstore);
209 image(0, from_buffer ? 0 : img.get_pixels());
212 unsigned Texture2D::get_n_levels() const
215 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
219 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
221 unsigned w = width>>level;
222 unsigned h = height>>level;
229 return LinAl::Vector<unsigned, 2>(w, h);
232 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
234 AsyncLoader *ldr = new AsyncLoader(*this, io);
238 UInt64 Texture2D::get_data_size() const
240 return id ? width*height*get_pixel_size(format) : 0;
243 void Texture2D::unload()
245 glDeleteTextures(1, &id);
251 Texture2D::Loader::Loader(Texture2D &t):
252 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
257 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
258 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
263 void Texture2D::Loader::init()
265 add("raw_data", &Loader::raw_data);
266 add("storage", &Loader::storage);
267 add("storage", &Loader::storage_levels);
270 void Texture2D::Loader::raw_data(const string &data)
278 obj.image(0, data.data());
281 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
283 obj.storage(fmt, w, h);
286 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
288 obj.storage(fmt, w, h, l);
292 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
296 img_loader(Graphics::ImageLoader::open_io(io)),
300 Texture2D::AsyncLoader::~AsyncLoader()
303 pixel_buffer.unmap();
307 bool Texture2D::AsyncLoader::needs_sync() const
312 bool Texture2D::AsyncLoader::process()
316 image.load_headers(*img_loader);
317 n_bytes = image.get_stride()*image.get_height();
321 pixel_buffer.storage(n_bytes);
322 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
325 image.load_into(*img_loader, mapped_address);
329 if(!pixel_buffer.unmap())
336 texture.generate_id();
337 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
338 texture.image(image, 0, true);
339 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);