1 #include <msp/gl/extensions/arb_texture_storage.h>
5 #include "pixelstore.h"
14 class Texture2D::AsyncLoader: public Resource::AsyncLoader
22 Graphics::Image image;
27 AsyncLoader(Texture2D &, IO::Seekable &);
29 void set_srgb_conversion(bool);
30 virtual bool needs_sync() const;
31 virtual bool process();
35 Texture2D::Texture2D(ResourceManager *m):
36 Texture(GL_TEXTURE_2D, m),
43 Texture2D::~Texture2D()
48 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
51 throw invalid_operation("Texture2D::storage");
53 throw invalid_argument("Texture2D::storage");
55 if(MSP_sized_internal_formats)
56 fmt = get_sized_pixelformat(fmt);
57 require_pixelformat(fmt);
64 void Texture2D::allocate(unsigned level)
66 if(allocated&(1<<level))
69 if(ARB_texture_storage)
71 BindRestore _bind(this);
72 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
73 glTexStorage2D(target, n_levels, ifmt, width, height);
74 allocated |= (1<<n_levels)-1;
78 PixelFormat base_fmt = get_base_pixelformat(ifmt);
79 image(level, base_fmt, get_alloc_type(base_fmt), 0);
83 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
85 if(width==0 || height==0)
86 throw invalid_operation("Texture2D::image");
90 get_level_size(level, w, h);
92 BindRestore _bind(this);
93 if(ARB_texture_storage)
94 sub_image(level, 0, 0, w, h, fmt, type, data);
96 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
98 allocated |= 1<<level;
99 if(gen_mipmap && level==0)
101 auto_generate_mipmap();
102 allocated |= (1<<get_n_levels())-1;
106 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
108 if(width==0 || height==0)
109 throw invalid_operation("Texture2D::sub_image");
113 BindRestore _bind(this);
114 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
117 void Texture2D::image(const Graphics::Image &img, bool srgb)
119 image(img, srgb, false);
122 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
124 unsigned w = img.get_width();
125 unsigned h = img.get_height();
126 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
128 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
129 else if(w!=width || h!=height)
130 throw incompatible_data("Texture2D::image");
132 PixelStore pstore = PixelStore::from_image(img);
133 BindRestore _bind_ps(pstore);
135 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
138 unsigned Texture2D::get_n_levels() const
141 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
145 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
156 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
158 AsyncLoader *ldr = new AsyncLoader(*this, io);
160 ldr->set_srgb_conversion(res->get_srgb_conversion());
164 UInt64 Texture2D::get_data_size() const
166 return id ? width*height*get_component_count(ifmt) : 0;
169 void Texture2D::unload()
171 glDeleteTextures(1, &id);
173 // TODO check which params actually need refreshing
178 Texture2D::Loader::Loader(Texture2D &t):
179 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
184 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
185 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
190 void Texture2D::Loader::init()
192 add("raw_data", &Loader::raw_data);
193 add("storage", &Loader::storage);
196 void Texture2D::Loader::raw_data(const string &data)
198 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
201 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
204 fmt = get_srgb_pixelformat(fmt);
205 obj.storage(fmt, w, h);
209 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
212 srgb_conversion(false),
213 pixel_buffer(PIXEL_UNPACK_BUFFER),
218 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
223 bool Texture2D::AsyncLoader::needs_sync() const
228 bool Texture2D::AsyncLoader::process()
232 /* TODO Enhance the ImageLoader system so that the image can be loaded
233 directly to the buffer */
235 n_bytes = image.get_stride()*image.get_height();
239 pixel_buffer.data(n_bytes, 0);
240 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
244 const char *data = reinterpret_cast<const char *>(image.get_data());
245 copy(data, data+n_bytes, mapped_address);
249 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
250 if(!pixel_buffer.unmap())
257 glGenTextures(1, &texture.id);
258 texture.image(image, srgb_conversion, true);