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)
52 throw invalid_operation("Texture2D::storage");
54 throw invalid_argument("Texture2D::storage");
56 set_internal_format(fmt);
61 void Texture2D::allocate(unsigned level)
63 if(allocated&(1<<level))
66 if(ARB_texture_storage)
68 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
69 if(ARB_direct_state_access)
70 glTextureStorage2D(id, n_levels, ifmt, width, height);
73 BindRestore _bind(this);
74 glTexStorage2D(target, n_levels, ifmt, width, height);
76 allocated |= (1<<n_levels)-1;
80 PixelFormat base_fmt = get_base_pixelformat(ifmt);
81 image(level, base_fmt, get_alloc_type(base_fmt), 0);
85 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, 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, fmt, type, data);
97 BindRestore _bind(this);
98 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
100 allocated |= 1<<level;
101 if(gen_mipmap && level==0)
103 auto_generate_mipmap();
104 allocated |= (1<<get_n_levels())-1;
108 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
110 if(width==0 || height==0)
111 throw invalid_operation("Texture2D::sub_image");
113 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
116 if(ARB_direct_state_access)
117 glTextureSubImage2D(id, level, x, y, wd, ht, fmt, type, data);
119 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
121 if(gen_mipmap && level==0)
122 auto_generate_mipmap();
125 void Texture2D::image(const Graphics::Image &img, bool srgb)
127 image(img, srgb, false);
130 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
132 unsigned w = img.get_width();
133 unsigned h = img.get_height();
134 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
136 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
137 else if(w!=width || h!=height)
138 throw incompatible_data("Texture2D::image");
140 PixelStore pstore = PixelStore::from_image(img);
141 BindRestore _bind_ps(pstore);
143 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
146 unsigned Texture2D::get_n_levels() const
149 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
153 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
164 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
166 AsyncLoader *ldr = new AsyncLoader(*this, io);
168 ldr->set_srgb_conversion(res->get_srgb_conversion());
172 UInt64 Texture2D::get_data_size() const
174 return id ? width*height*get_component_count(ifmt) : 0;
177 void Texture2D::unload()
179 glDeleteTextures(1, &id);
181 // TODO check which params actually need refreshing
186 Texture2D::Loader::Loader(Texture2D &t):
187 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
192 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
193 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
198 void Texture2D::Loader::init()
200 add("raw_data", &Loader::raw_data);
201 add("storage", &Loader::storage);
204 void Texture2D::Loader::raw_data(const string &data)
206 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
209 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
212 fmt = get_srgb_pixelformat(fmt);
213 obj.storage(fmt, w, h);
217 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
220 srgb_conversion(false),
221 pixel_buffer(PIXEL_UNPACK_BUFFER),
226 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
231 bool Texture2D::AsyncLoader::needs_sync() const
236 bool Texture2D::AsyncLoader::process()
240 /* TODO Enhance the ImageLoader system so that the image can be loaded
241 directly to the buffer */
243 n_bytes = image.get_stride()*image.get_height();
247 pixel_buffer.data(n_bytes, 0);
248 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
252 const char *data = reinterpret_cast<const char *>(image.get_data());
253 copy(data, data+n_bytes, mapped_address);
257 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
258 if(!pixel_buffer.unmap())
265 glGenTextures(1, &texture.id);
266 texture.image(image, srgb_conversion, true);