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 if(ARB_direct_state_access)
76 glTextureStorage2D(id, levels, ifmt, width, height);
79 BindRestore _bind(this);
80 glTexStorage2D(target, levels, ifmt, width, height);
82 allocated |= (1<<levels)-1;
86 PixelFormat base_fmt = get_base_pixelformat(ifmt);
87 image(level, base_fmt, get_alloc_type(base_fmt), 0);
91 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
93 if(width==0 || height==0)
94 throw invalid_operation("Texture2D::image");
98 get_level_size(level, w, h);
100 if(ARB_texture_storage)
101 return sub_image(level, 0, 0, w, h, fmt, type, data);
103 BindRestore _bind(this);
104 glTexImage2D(target, level, ifmt, w, h, 0, get_upload_format(fmt), type, data);
106 allocated |= 1<<level;
107 if(auto_gen_mipmap && level==0)
110 allocated |= (1<<get_n_levels())-1;
114 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
116 if(width==0 || height==0)
117 throw invalid_operation("Texture2D::sub_image");
119 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
122 fmt = get_upload_format(fmt);
123 if(ARB_direct_state_access)
124 glTextureSubImage2D(id, level, x, y, wd, ht, fmt, type, data);
126 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
128 if(auto_gen_mipmap && level==0)
132 void Texture2D::image(const Graphics::Image &img, bool srgb)
134 image(img, srgb, false);
137 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
139 unsigned w = img.get_width();
140 unsigned h = img.get_height();
141 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
144 unsigned l = (is_mipmapped(min_filter) ? mipmap_levels ? mipmap_levels : 0 : 1);
145 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, l);
147 else if(w!=width || h!=height)
148 throw incompatible_data("Texture2D::image");
150 PixelStore pstore = PixelStore::from_image(img);
151 BindRestore _bind_ps(pstore);
153 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
156 unsigned Texture2D::get_n_levels() const
159 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
163 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
174 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
176 AsyncLoader *ldr = new AsyncLoader(*this, io);
178 ldr->set_srgb_conversion(res->get_srgb_conversion());
182 UInt64 Texture2D::get_data_size() const
184 return id ? width*height*get_pixel_size(ifmt) : 0;
187 void Texture2D::unload()
189 glDeleteTextures(1, &id);
191 // TODO check which params actually need refreshing
196 Texture2D::Loader::Loader(Texture2D &t):
197 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
202 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
203 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
208 void Texture2D::Loader::init()
210 add("raw_data", &Loader::raw_data);
211 add("storage", &Loader::storage);
214 void Texture2D::Loader::raw_data(const string &data)
216 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
219 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
221 obj.storage(fmt, w, h);
225 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
228 srgb_conversion(false),
229 pixel_buffer(PIXEL_UNPACK_BUFFER),
234 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
239 bool Texture2D::AsyncLoader::needs_sync() const
244 bool Texture2D::AsyncLoader::process()
248 /* TODO Enhance the ImageLoader system so that the image can be loaded
249 directly to the buffer */
251 n_bytes = image.get_stride()*image.get_height();
255 pixel_buffer.storage(n_bytes);
256 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
260 const char *data = reinterpret_cast<const char *>(image.get_data());
261 copy(data, data+n_bytes, mapped_address);
265 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
266 if(!pixel_buffer.unmap())
273 glGenTextures(1, &texture.id);
274 texture.image(image, srgb_conversion, true);