1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
4 #include <msp/graphics/imageloader.h>
15 class Texture2D::AsyncLoader: public Resource::AsyncLoader
22 Graphics::Image image;
23 Graphics::ImageLoader *img_loader;
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),
42 Texture2D::~Texture2D()
47 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
51 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
52 throw incompatible_data("Texture2D::storage");
56 throw invalid_argument("Texture2D::storage");
61 levels = get_n_levels();
63 levels = min(levels, lv);
65 GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
66 if(ARB_texture_storage)
68 if(ARB_direct_state_access)
69 glTextureStorage2D(id, levels, gl_fmt, width, height);
73 glTexStorage2D(target, levels, gl_fmt, width, height);
79 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
80 GLenum comp = get_gl_components(get_components(storage_fmt));
81 GLenum type = get_gl_type(get_component_type(storage_fmt));
82 for(unsigned i=0; i<levels; ++i)
84 LinAl::Vector<unsigned, 2> lv_size = get_level_size(i);
85 glTexImage2D(target, i, gl_fmt, lv_size.x, lv_size.y, 0, comp, type, 0);
92 void Texture2D::image(unsigned level, const void *data)
94 LinAl::Vector<unsigned, 2> size = get_level_size(level);
95 return sub_image(level, 0, 0, size.x, size.y, data);
98 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
100 if(width==0 || height==0)
101 throw invalid_operation("Texture2D::sub_image");
103 throw out_of_range("Texture2D::sub_image");
105 GLenum comp = get_gl_components(get_components(storage_fmt));
106 GLenum type = get_gl_type(get_component_type(storage_fmt));
107 if(ARB_direct_state_access)
108 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
112 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
116 void Texture2D::image(const Graphics::Image &img, unsigned lv)
118 image(img, lv, false);
121 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
123 unsigned w = img.get_width();
124 unsigned h = img.get_height();
125 PixelFormat fmt = pixelformat_from_image(img);
126 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
128 image(0, from_buffer ? 0 : img.get_pixels());
131 unsigned Texture2D::get_n_levels() const
134 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
138 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
140 unsigned w = width>>level;
141 unsigned h = height>>level;
148 return LinAl::Vector<unsigned, 2>(w, h);
151 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
153 AsyncLoader *ldr = new AsyncLoader(*this, io);
157 uint64_t Texture2D::get_data_size() const
159 return id ? width*height*get_pixel_size(format) : 0;
162 void Texture2D::unload()
164 glDeleteTextures(1, &id);
169 Texture2D::Loader::Loader(Texture2D &t):
170 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
175 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
176 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
181 void Texture2D::Loader::init()
183 add("raw_data", &Loader::raw_data);
184 add("storage", &Loader::storage);
185 add("storage", &Loader::storage_levels);
188 void Texture2D::Loader::raw_data(const string &data)
196 obj.image(0, data.data());
199 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
201 obj.storage(fmt, w, h);
204 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
206 obj.storage(fmt, w, h, l);
210 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
214 img_loader(Graphics::ImageLoader::open_io(io)),
218 Texture2D::AsyncLoader::~AsyncLoader()
221 pixel_buffer.unmap();
225 bool Texture2D::AsyncLoader::needs_sync() const
230 bool Texture2D::AsyncLoader::process()
234 image.load_headers(*img_loader);
235 n_bytes = image.get_stride()*image.get_height();
239 pixel_buffer.storage(n_bytes);
240 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
243 image.load_into(*img_loader, mapped_address);
247 if(!pixel_buffer.unmap())
254 texture.generate_id();
255 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
256 texture.image(image, 0, true);
257 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
258 if(texture.auto_gen_mipmap)
259 texture.generate_mipmap();