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)
53 throw invalid_operation("Texture2D::storage");
55 throw invalid_argument("Texture2D::storage");
60 levels = get_n_levels();
62 levels = min(levels, lv);
65 void Texture2D::allocate(unsigned level)
67 if(width==0 || height==0)
68 throw invalid_operation("Texture2D::allocate");
70 throw invalid_argument("Texture2D::allocate");
71 if(allocated&(1<<level))
74 if(ARB_texture_storage)
76 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
77 if(ARB_direct_state_access)
78 glTextureStorage2D(id, levels, storage_fmt, width, height);
80 glTexStorage2D(target, levels, storage_fmt, width, height);
82 allocated |= (1<<levels)-1;
88 void Texture2D::image(unsigned level, const void *data)
90 if(width==0 || height==0)
91 throw invalid_operation("Texture2D::image");
95 get_level_size(level, w, h);
97 if(ARB_texture_storage)
98 return sub_image(level, 0, 0, w, h, data);
100 BindRestore _bind(this);
104 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
108 PixelComponents comp = get_components(storage_fmt);
109 GLenum type = get_gl_type(get_component_type(storage_fmt));
110 glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
112 allocated |= 1<<level;
113 if(auto_gen_mipmap && level==0)
116 allocated |= (1<<levels)-1;
120 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
122 if(comp!=get_components(format) || type!=get_component_type(format))
123 throw incompatible_data("Texture2D::image");
127 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
129 if(width==0 || height==0)
130 throw invalid_operation("Texture2D::sub_image");
132 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
135 PixelComponents comp = get_components(storage_fmt);
136 GLenum type = get_gl_type(get_component_type(storage_fmt));
137 if(ARB_direct_state_access)
138 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
140 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
142 if(auto_gen_mipmap && level==0)
146 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
148 if(comp!=get_components(format) || type!=get_component_type(format))
149 throw incompatible_data("Texture2D::sub_image");
150 sub_image(level, x, y, wd, ht, data);
153 void Texture2D::image(const Graphics::Image &img, unsigned lv)
155 image(img, lv, false);
158 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
160 unsigned w = img.get_width();
161 unsigned h = img.get_height();
162 PixelFormat fmt = pixelformat_from_image(img);
164 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
165 else if(w!=width || h!=height || (lv && lv!=levels))
166 throw incompatible_data("Texture2D::image");
168 PixelStore pstore = PixelStore::from_image(img);
169 BindRestore _bind_ps(pstore);
171 image(0, from_buffer ? 0 : img.get_pixels());
174 unsigned Texture2D::get_n_levels() const
177 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
181 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
192 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
194 AsyncLoader *ldr = new AsyncLoader(*this, io);
198 UInt64 Texture2D::get_data_size() const
200 return id ? width*height*get_pixel_size(format) : 0;
203 void Texture2D::unload()
205 glDeleteTextures(1, &id);
208 default_sampler.unload();
212 Texture2D::Loader::Loader(Texture2D &t):
213 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
218 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
219 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
224 void Texture2D::Loader::init()
226 add("raw_data", &Loader::raw_data);
227 add("storage", &Loader::storage);
228 add("storage", &Loader::storage_levels);
231 void Texture2D::Loader::raw_data(const string &data)
239 obj.image(0, data.data());
242 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
244 obj.storage(fmt, w, h);
247 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
249 obj.storage(fmt, w, h, l);
253 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
256 pixel_buffer(PIXEL_UNPACK_BUFFER),
258 img_loader(Graphics::ImageLoader::open_io(io)),
262 Texture2D::AsyncLoader::~AsyncLoader()
265 pixel_buffer.unmap();
269 bool Texture2D::AsyncLoader::needs_sync() const
274 bool Texture2D::AsyncLoader::process()
278 image.load_headers(*img_loader);
279 n_bytes = image.get_stride()*image.get_height();
283 pixel_buffer.storage(n_bytes);
284 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
287 image.load_into(*img_loader, mapped_address);
290 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
292 if(!pixel_buffer.unmap())
299 texture.generate_id();
300 texture.image(image, 0, true);