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");
75 if(allocated&(1<<level))
78 if(ARB_texture_storage)
80 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
81 if(ARB_direct_state_access)
82 glTextureStorage2D(id, levels, storage_fmt, width, height);
84 glTexStorage2D(target, levels, storage_fmt, width, height);
86 allocated |= (1<<levels)-1;
92 void Texture2D::image(unsigned level, const void *data)
94 if(width==0 || height==0)
95 throw invalid_operation("Texture2D::image");
99 get_level_size(level, w, h);
101 if(ARB_texture_storage)
102 return sub_image(level, 0, 0, w, h, data);
104 BindRestore _bind(this);
108 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
112 PixelComponents comp = get_components(storage_fmt);
113 GLenum type = get_gl_type(get_component_type(storage_fmt));
114 glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
116 allocated |= 1<<level;
117 if(auto_gen_mipmap && level==0)
120 allocated |= (1<<levels)-1;
124 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
126 if(comp!=get_components(format) || type!=get_component_type(format))
127 throw incompatible_data("Texture2D::image");
131 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
133 if(width==0 || height==0)
134 throw invalid_operation("Texture2D::sub_image");
136 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
139 PixelComponents comp = get_components(storage_fmt);
140 GLenum type = get_gl_type(get_component_type(storage_fmt));
141 if(ARB_direct_state_access)
142 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
144 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
146 if(auto_gen_mipmap && level==0)
150 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
152 if(comp!=get_components(format) || type!=get_component_type(format))
153 throw incompatible_data("Texture2D::sub_image");
154 sub_image(level, x, y, wd, ht, data);
157 void Texture2D::image(const Graphics::Image &img, unsigned lv)
159 image(img, lv, false);
162 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
164 unsigned w = img.get_width();
165 unsigned h = img.get_height();
166 PixelFormat fmt = pixelformat_from_image(img);
167 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
169 PixelStore pstore = PixelStore::from_image(img);
170 BindRestore _bind_ps(pstore);
172 image(0, from_buffer ? 0 : img.get_pixels());
175 unsigned Texture2D::get_n_levels() const
178 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
182 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
193 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
195 AsyncLoader *ldr = new AsyncLoader(*this, io);
199 UInt64 Texture2D::get_data_size() const
201 return id ? width*height*get_pixel_size(format) : 0;
204 void Texture2D::unload()
206 glDeleteTextures(1, &id);
209 default_sampler.unload();
213 Texture2D::Loader::Loader(Texture2D &t):
214 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
219 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
220 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
225 void Texture2D::Loader::init()
227 add("raw_data", &Loader::raw_data);
228 add("storage", &Loader::storage);
229 add("storage", &Loader::storage_levels);
232 void Texture2D::Loader::raw_data(const string &data)
240 obj.image(0, data.data());
243 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
245 obj.storage(fmt, w, h);
248 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
250 obj.storage(fmt, w, h, l);
254 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
257 pixel_buffer(PIXEL_UNPACK_BUFFER),
259 img_loader(Graphics::ImageLoader::open_io(io)),
263 Texture2D::AsyncLoader::~AsyncLoader()
266 pixel_buffer.unmap();
270 bool Texture2D::AsyncLoader::needs_sync() const
275 bool Texture2D::AsyncLoader::process()
279 image.load_headers(*img_loader);
280 n_bytes = image.get_stride()*image.get_height();
284 pixel_buffer.storage(n_bytes);
285 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
288 image.load_into(*img_loader, mapped_address);
291 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
293 if(!pixel_buffer.unmap())
300 texture.generate_id();
301 texture.image(image, 0, true);