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>
7 #include "pixelstore.h"
16 class Texture2D::AsyncLoader: public Resource::AsyncLoader
23 Graphics::Image image;
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)
51 throw invalid_operation("Texture2D::storage");
53 throw invalid_argument("Texture2D::storage");
58 levels = get_n_levels();
60 levels = min(levels, lv);
63 void Texture2D::allocate(unsigned level)
65 if(width==0 || height==0)
66 throw invalid_operation("Texture2D::allocate");
68 throw invalid_argument("Texture2D::allocate");
69 if(allocated&(1<<level))
72 if(ARB_texture_storage)
74 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
75 if(ARB_direct_state_access)
76 glTextureStorage2D(id, levels, storage_fmt, width, height);
78 glTexStorage2D(target, levels, storage_fmt, width, height);
80 allocated |= (1<<levels)-1;
86 void Texture2D::image(unsigned level, const void *data)
88 if(width==0 || height==0)
89 throw invalid_operation("Texture2D::image");
93 get_level_size(level, w, h);
95 if(ARB_texture_storage)
96 return sub_image(level, 0, 0, w, h, data);
98 BindRestore _bind(this);
102 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
106 PixelComponents comp = get_components(storage_fmt);
107 DataType type = get_component_type(storage_fmt);
108 glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
110 allocated |= 1<<level;
111 if(auto_gen_mipmap && level==0)
114 allocated |= (1<<levels)-1;
118 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
120 if(comp!=get_components(format) || type!=get_component_type(format))
121 throw incompatible_data("Texture2D::image");
125 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
127 if(width==0 || height==0)
128 throw invalid_operation("Texture2D::sub_image");
130 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
133 PixelComponents comp = get_components(storage_fmt);
134 DataType type = get_component_type(storage_fmt);
135 if(ARB_direct_state_access)
136 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
138 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
140 if(auto_gen_mipmap && level==0)
144 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
146 if(comp!=get_components(format) || type!=get_component_type(format))
147 throw incompatible_data("Texture2D::sub_image");
148 sub_image(level, x, y, wd, ht, data);
151 void Texture2D::image(const Graphics::Image &img, unsigned lv)
153 image(img, lv, false);
156 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
158 unsigned w = img.get_width();
159 unsigned h = img.get_height();
160 PixelFormat fmt = pixelformat_from_image(img);
162 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
163 else if(w!=width || h!=height || (lv && lv!=levels))
164 throw incompatible_data("Texture2D::image");
166 PixelStore pstore = PixelStore::from_image(img);
167 BindRestore _bind_ps(pstore);
169 image(0, from_buffer ? 0 : img.get_pixels());
172 unsigned Texture2D::get_n_levels() const
175 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
179 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
190 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
192 AsyncLoader *ldr = new AsyncLoader(*this, io);
196 UInt64 Texture2D::get_data_size() const
198 return id ? width*height*get_pixel_size(format) : 0;
201 void Texture2D::unload()
203 glDeleteTextures(1, &id);
206 default_sampler.unload();
210 Texture2D::Loader::Loader(Texture2D &t):
211 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
216 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
217 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
222 void Texture2D::Loader::init()
224 add("raw_data", &Loader::raw_data);
225 add("storage", &Loader::storage);
226 add("storage", &Loader::storage_levels);
229 void Texture2D::Loader::raw_data(const string &data)
231 obj.image(0, data.data());
234 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
236 obj.storage(fmt, w, h);
239 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
241 obj.storage(fmt, w, h, l);
245 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
248 pixel_buffer(PIXEL_UNPACK_BUFFER),
253 Texture2D::AsyncLoader::~AsyncLoader()
256 pixel_buffer.unmap();
259 bool Texture2D::AsyncLoader::needs_sync() const
264 bool Texture2D::AsyncLoader::process()
268 /* TODO Enhance the ImageLoader system so that the image can be loaded
269 directly to the buffer */
271 n_bytes = image.get_stride()*image.get_height();
275 pixel_buffer.storage(n_bytes);
276 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
280 const char *data = reinterpret_cast<const char *>(image.get_pixels());
281 copy(data, data+n_bytes, mapped_address);
285 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
287 if(!pixel_buffer.unmap())
295 if(ARB_direct_state_access)
296 glCreateTextures(texture.target, 1, &texture.id);
298 glGenTextures(1, &texture.id);
300 texture.image(image, 0, true);