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 &);
30 virtual bool needs_sync() const;
31 virtual bool process();
35 Texture2D::Texture2D(ResourceManager *m):
36 Texture(GL_TEXTURE_2D, m),
42 Texture2D::~Texture2D()
47 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
50 throw invalid_operation("Texture2D::storage");
52 throw invalid_argument("Texture2D::storage");
57 levels = get_n_levels();
59 levels = min(levels, lv);
62 void Texture2D::allocate(unsigned level)
64 if(width==0 || height==0)
65 throw invalid_operation("Texture2D::allocate");
67 throw invalid_argument("Texture2D::allocate");
68 if(allocated&(1<<level))
71 if(ARB_texture_storage)
73 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
74 if(ARB_direct_state_access)
75 glTextureStorage2D(id, levels, storage_fmt, width, height);
77 glTexStorage2D(target, levels, storage_fmt, width, height);
79 allocated |= (1<<levels)-1;
85 void Texture2D::image(unsigned level, const void *data)
87 if(width==0 || height==0)
88 throw invalid_operation("Texture2D::image");
92 get_level_size(level, w, h);
94 if(ARB_texture_storage)
95 return sub_image(level, 0, 0, w, h, data);
97 BindRestore _bind(this);
101 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
105 PixelComponents comp = get_components(storage_fmt);
106 DataType type = get_component_type(storage_fmt);
107 glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
109 allocated |= 1<<level;
110 if(auto_gen_mipmap && level==0)
113 allocated |= (1<<levels)-1;
117 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
119 if(comp!=get_components(format) || type!=get_component_type(format))
120 throw incompatible_data("Texture2D::image");
124 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
126 if(width==0 || height==0)
127 throw invalid_operation("Texture2D::sub_image");
129 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
132 PixelComponents comp = get_components(storage_fmt);
133 DataType type = get_component_type(storage_fmt);
134 if(ARB_direct_state_access)
135 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
137 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
139 if(auto_gen_mipmap && level==0)
143 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
145 if(comp!=get_components(format) || type!=get_component_type(format))
146 throw incompatible_data("Texture2D::sub_image");
147 sub_image(level, x, y, wd, ht, data);
150 void Texture2D::image(const Graphics::Image &img, unsigned lv)
152 image(img, lv, false);
155 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
157 unsigned w = img.get_width();
158 unsigned h = img.get_height();
159 PixelFormat fmt = pixelformat_from_image(img);
161 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
162 else if(w!=width || h!=height || (lv && lv!=levels))
163 throw incompatible_data("Texture2D::image");
165 PixelStore pstore = PixelStore::from_image(img);
166 BindRestore _bind_ps(pstore);
168 image(0, from_buffer ? 0 : img.get_data());
171 unsigned Texture2D::get_n_levels() const
174 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
178 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
189 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
191 AsyncLoader *ldr = new AsyncLoader(*this, io);
195 UInt64 Texture2D::get_data_size() const
197 return id ? width*height*get_pixel_size(format) : 0;
200 void Texture2D::unload()
202 glDeleteTextures(1, &id);
205 default_sampler.unload();
209 Texture2D::Loader::Loader(Texture2D &t):
210 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
215 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
216 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
221 void Texture2D::Loader::init()
223 add("raw_data", &Loader::raw_data);
224 add("storage", &Loader::storage);
225 add("storage", &Loader::storage_levels);
228 void Texture2D::Loader::raw_data(const string &data)
230 obj.image(0, data.data());
233 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
235 obj.storage(fmt, w, h);
238 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
240 obj.storage(fmt, w, h, l);
244 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
247 pixel_buffer(PIXEL_UNPACK_BUFFER),
252 bool Texture2D::AsyncLoader::needs_sync() const
257 bool Texture2D::AsyncLoader::process()
261 /* TODO Enhance the ImageLoader system so that the image can be loaded
262 directly to the buffer */
264 n_bytes = image.get_stride()*image.get_height();
268 pixel_buffer.storage(n_bytes);
269 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
273 const char *data = reinterpret_cast<const char *>(image.get_data());
274 copy(data, data+n_bytes, mapped_address);
278 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
279 if(!pixel_buffer.unmap())
287 if(ARB_direct_state_access)
288 glCreateTextures(texture.target, 1, &texture.id);
290 glGenTextures(1, &texture.id);
292 texture.image(image, 0, true);