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>
15 class Texture2D::AsyncLoader: public Resource::AsyncLoader
22 Graphics::Image image;
23 Graphics::ImageLoader *img_loader;
28 AsyncLoader(Texture2D &, IO::Seekable &);
31 virtual bool needs_sync() const;
32 virtual bool process();
36 Texture2D::Texture2D(ResourceManager *m):
37 Texture(GL_TEXTURE_2D, m),
43 Texture2D::~Texture2D()
48 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
52 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
53 throw incompatible_data("Texture2D::storage");
57 throw invalid_argument("Texture2D::storage");
62 levels = get_n_levels();
64 levels = min(levels, lv);
67 void Texture2D::allocate(unsigned level)
69 if(width==0 || height==0)
70 throw invalid_operation("Texture2D::allocate");
72 throw invalid_argument("Texture2D::allocate");
73 if(allocated&(1<<level))
76 if(ARB_texture_storage)
78 GLenum fmt = get_gl_pixelformat(storage_fmt);
79 if(ARB_direct_state_access)
80 glTextureStorage2D(id, levels, fmt, width, height);
84 glTexStorage2D(target, levels, fmt, width, height);
87 allocated |= (1<<levels)-1;
93 void Texture2D::image(unsigned level, const void *data)
95 if(width==0 || height==0)
96 throw invalid_operation("Texture2D::image");
98 throw out_of_range("Texture2D::image");
100 LinAl::Vector<unsigned, 2> size = get_level_size(level);
102 if(ARB_texture_storage)
103 return sub_image(level, 0, 0, size.x, size.y, data);
109 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
113 GLenum fmt = get_gl_pixelformat(storage_fmt);
114 GLenum comp = get_gl_components(get_components(storage_fmt));
115 GLenum type = get_gl_type(get_component_type(storage_fmt));
116 glTexImage2D(target, level, fmt, size.x, size.y, 0, comp, type, data);
118 allocated |= 1<<level;
119 if(auto_gen_mipmap && level==0)
122 allocated |= (1<<levels)-1;
126 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
128 if(comp!=get_components(format) || type!=get_component_type(format))
129 throw incompatible_data("Texture2D::image");
133 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
135 if(width==0 || height==0)
136 throw invalid_operation("Texture2D::sub_image");
138 throw out_of_range("Texture2D::sub_image");
142 GLenum comp = get_gl_components(get_components(storage_fmt));
143 GLenum type = get_gl_type(get_component_type(storage_fmt));
144 if(ARB_direct_state_access)
145 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
149 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
152 if(auto_gen_mipmap && level==0)
156 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
158 if(comp!=get_components(format) || type!=get_component_type(format))
159 throw incompatible_data("Texture2D::sub_image");
160 sub_image(level, x, y, wd, ht, data);
163 void Texture2D::image(const Graphics::Image &img, unsigned lv)
165 image(img, lv, false);
168 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
170 unsigned w = img.get_width();
171 unsigned h = img.get_height();
172 PixelFormat fmt = pixelformat_from_image(img);
173 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
175 image(0, from_buffer ? 0 : img.get_pixels());
178 unsigned Texture2D::get_n_levels() const
181 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
185 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
187 unsigned w = width>>level;
188 unsigned h = height>>level;
195 return LinAl::Vector<unsigned, 2>(w, h);
198 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
200 AsyncLoader *ldr = new AsyncLoader(*this, io);
204 UInt64 Texture2D::get_data_size() const
206 return id ? width*height*get_pixel_size(format) : 0;
209 void Texture2D::unload()
211 glDeleteTextures(1, &id);
217 Texture2D::Loader::Loader(Texture2D &t):
218 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
223 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
224 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
229 void Texture2D::Loader::init()
231 add("raw_data", &Loader::raw_data);
232 add("storage", &Loader::storage);
233 add("storage", &Loader::storage_levels);
236 void Texture2D::Loader::raw_data(const string &data)
244 obj.image(0, data.data());
247 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
249 obj.storage(fmt, w, h);
252 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
254 obj.storage(fmt, w, h, l);
258 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
262 img_loader(Graphics::ImageLoader::open_io(io)),
266 Texture2D::AsyncLoader::~AsyncLoader()
269 pixel_buffer.unmap();
273 bool Texture2D::AsyncLoader::needs_sync() const
278 bool Texture2D::AsyncLoader::process()
282 image.load_headers(*img_loader);
283 n_bytes = image.get_stride()*image.get_height();
287 pixel_buffer.storage(n_bytes);
288 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
291 image.load_into(*img_loader, mapped_address);
295 if(!pixel_buffer.unmap())
302 texture.generate_id();
303 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
304 texture.image(image, 0, true);
305 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);