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
24 Graphics::Image image;
29 AsyncLoader(Texture2D &, IO::Seekable &);
31 void set_srgb_conversion(bool);
32 virtual bool needs_sync() const;
33 virtual bool process();
37 Texture2D::Texture2D(ResourceManager *m):
38 Texture(GL_TEXTURE_2D, m),
44 Texture2D::~Texture2D()
49 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
52 throw invalid_operation("Texture2D::storage");
54 throw invalid_argument("Texture2D::storage");
56 set_internal_format(fmt);
59 levels = get_n_levels();
61 levels = min(levels, lv);
64 void Texture2D::allocate(unsigned level)
66 if(width==0 || height==0)
67 throw invalid_operation("Texture2D::allocate");
69 throw invalid_argument("Texture2D::allocate");
70 if(allocated&(1<<level))
73 if(ARB_texture_storage)
75 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
76 if(ARB_direct_state_access)
77 glTextureStorage2D(id, levels, ifmt, width, height);
79 glTexStorage2D(target, levels, ifmt, width, height);
81 allocated |= (1<<levels)-1;
84 image(level, get_components(ifmt), get_component_type(ifmt), 0);
87 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
89 if(width==0 || height==0)
90 throw invalid_operation("Texture2D::image");
94 get_level_size(level, w, h);
96 if(ARB_texture_storage)
97 return sub_image(level, 0, 0, w, h, comp, type, data);
99 BindRestore _bind(this);
103 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
106 glTexImage2D(target, level, ifmt, w, h, 0, get_upload_components(comp), type, data);
108 allocated |= 1<<level;
109 if(auto_gen_mipmap && level==0)
112 allocated |= (1<<levels)-1;
116 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
118 if(width==0 || height==0)
119 throw invalid_operation("Texture2D::sub_image");
121 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
124 comp = get_upload_components(comp);
125 if(ARB_direct_state_access)
126 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
128 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
130 if(auto_gen_mipmap && level==0)
134 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
136 image(img, lv, srgb, false);
139 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
141 unsigned w = img.get_width();
142 unsigned h = img.get_height();
143 PixelFormat fmt = pixelformat_from_image(img);
144 PixelComponents comp = get_components(fmt);
145 DataType type = get_component_type(fmt);
147 storage(make_pixelformat(comp, type, srgb), w, h, lv);
148 else if(w!=width || h!=height || (lv && lv!=levels))
149 throw incompatible_data("Texture2D::image");
151 PixelStore pstore = PixelStore::from_image(img);
152 BindRestore _bind_ps(pstore);
154 image(0, comp, type, from_buffer ? 0 : img.get_data());
157 unsigned Texture2D::get_n_levels() const
160 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
164 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
175 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
177 AsyncLoader *ldr = new AsyncLoader(*this, io);
179 ldr->set_srgb_conversion(res->get_srgb_conversion());
183 UInt64 Texture2D::get_data_size() const
185 return id ? width*height*get_pixel_size(ifmt) : 0;
188 void Texture2D::unload()
190 glDeleteTextures(1, &id);
193 default_sampler.unload();
197 Texture2D::Loader::Loader(Texture2D &t):
198 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
203 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
204 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
209 void Texture2D::Loader::init()
211 add("raw_data", &Loader::raw_data);
212 add("storage", &Loader::storage);
213 add("storage", &Loader::storage_levels);
216 void Texture2D::Loader::raw_data(const string &data)
218 obj.image(0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
221 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
223 obj.storage(fmt, w, h);
226 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
228 obj.storage(fmt, w, h, l);
232 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
235 srgb_conversion(false),
236 pixel_buffer(PIXEL_UNPACK_BUFFER),
241 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
246 bool Texture2D::AsyncLoader::needs_sync() const
251 bool Texture2D::AsyncLoader::process()
255 /* TODO Enhance the ImageLoader system so that the image can be loaded
256 directly to the buffer */
258 n_bytes = image.get_stride()*image.get_height();
262 pixel_buffer.storage(n_bytes);
263 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
267 const char *data = reinterpret_cast<const char *>(image.get_data());
268 copy(data, data+n_bytes, mapped_address);
272 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
273 if(!pixel_buffer.unmap())
281 if(ARB_direct_state_access)
282 glCreateTextures(texture.target, 1, &texture.id);
284 glGenTextures(1, &texture.id);
286 texture.image(image, 0, srgb_conversion, true);