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;
85 PixelFormat base_fmt = get_base_pixelformat(ifmt);
86 image(level, base_fmt, get_alloc_type(base_fmt), 0);
90 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
92 if(width==0 || height==0)
93 throw invalid_operation("Texture2D::image");
97 get_level_size(level, w, h);
99 if(ARB_texture_storage)
100 return sub_image(level, 0, 0, w, h, fmt, type, data);
102 BindRestore _bind(this);
106 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
109 glTexImage2D(target, level, ifmt, w, h, 0, get_upload_format(fmt), type, data);
111 allocated |= 1<<level;
112 if(auto_gen_mipmap && level==0)
115 allocated |= (1<<levels)-1;
119 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
121 if(width==0 || height==0)
122 throw invalid_operation("Texture2D::sub_image");
124 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
127 fmt = get_upload_format(fmt);
128 if(ARB_direct_state_access)
129 glTextureSubImage2D(id, level, x, y, wd, ht, fmt, type, data);
131 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
133 if(auto_gen_mipmap && level==0)
137 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
139 image(img, lv, srgb, false);
142 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
144 unsigned w = img.get_width();
145 unsigned h = img.get_height();
146 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
148 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, lv);
149 else if(w!=width || h!=height || (lv && lv!=levels))
150 throw incompatible_data("Texture2D::image");
152 PixelStore pstore = PixelStore::from_image(img);
153 BindRestore _bind_ps(pstore);
155 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
158 unsigned Texture2D::get_n_levels() const
161 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
165 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
176 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
178 AsyncLoader *ldr = new AsyncLoader(*this, io);
180 ldr->set_srgb_conversion(res->get_srgb_conversion());
184 UInt64 Texture2D::get_data_size() const
186 return id ? width*height*get_pixel_size(ifmt) : 0;
189 void Texture2D::unload()
191 glDeleteTextures(1, &id);
194 default_sampler.unload();
198 Texture2D::Loader::Loader(Texture2D &t):
199 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
204 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
205 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
210 void Texture2D::Loader::init()
212 add("raw_data", &Loader::raw_data);
213 add("storage", &Loader::storage);
214 add("storage", &Loader::storage_levels);
217 void Texture2D::Loader::raw_data(const string &data)
219 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
222 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
224 obj.storage(fmt, w, h);
227 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
229 obj.storage(fmt, w, h, l);
233 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
236 srgb_conversion(false),
237 pixel_buffer(PIXEL_UNPACK_BUFFER),
242 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
247 bool Texture2D::AsyncLoader::needs_sync() const
252 bool Texture2D::AsyncLoader::process()
256 /* TODO Enhance the ImageLoader system so that the image can be loaded
257 directly to the buffer */
259 n_bytes = image.get_stride()*image.get_height();
263 pixel_buffer.storage(n_bytes);
264 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
268 const char *data = reinterpret_cast<const char *>(image.get_data());
269 copy(data, data+n_bytes, mapped_address);
273 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
274 if(!pixel_buffer.unmap())
282 if(ARB_direct_state_access)
283 glCreateTextures(texture.target, 1, &texture.id);
285 glGenTextures(1, &texture.id);
287 texture.image(image, 0, srgb_conversion, true);