4 #include "pixelstore.h"
13 class Texture2D::AsyncLoader: public Resource::AsyncLoader
21 Graphics::Image image;
26 AsyncLoader(Texture2D &, IO::Seekable &);
28 void set_srgb_conversion(bool);
29 virtual bool needs_sync() const;
30 virtual bool process();
34 Texture2D::Texture2D(ResourceManager *m):
35 Texture(GL_TEXTURE_2D, m),
42 Texture2D::~Texture2D()
47 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
50 throw invalid_operation("Texture2D::storage");
52 throw invalid_argument("Texture2D::storage");
54 if(MSP_sized_internal_formats)
55 fmt = get_sized_pixelformat(fmt);
56 require_pixelformat(fmt);
63 void Texture2D::allocate(unsigned level)
65 if(allocated&(1<<level))
68 PixelFormat base_fmt = get_base_pixelformat(ifmt);
69 image(level, base_fmt, get_alloc_type(base_fmt), 0);
72 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
74 if(width==0 || height==0)
75 throw invalid_operation("Texture2D::image");
79 get_level_size(level, w, h);
81 BindRestore _bind(this);
82 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
84 allocated |= 1<<level;
85 if(gen_mipmap && level==0)
87 auto_generate_mipmap();
88 allocated |= (1<<get_n_levels())-1;
92 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
94 if(width==0 || height==0)
95 throw invalid_operation("Texture2D::sub_image");
99 BindRestore _bind(this);
100 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
103 void Texture2D::image(const Graphics::Image &img, bool srgb)
105 image(img, srgb, false);
108 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
110 unsigned w = img.get_width();
111 unsigned h = img.get_height();
112 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
114 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
115 else if(w!=width || h!=height)
116 throw incompatible_data("Texture2D::image");
118 PixelStore pstore = PixelStore::from_image(img);
119 BindRestore _bind_ps(pstore);
121 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
124 unsigned Texture2D::get_n_levels() const
127 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
131 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
142 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
144 AsyncLoader *ldr = new AsyncLoader(*this, io);
146 ldr->set_srgb_conversion(res->get_srgb_conversion());
150 UInt64 Texture2D::get_data_size() const
152 return id ? width*height*get_component_count(ifmt) : 0;
155 void Texture2D::unload()
157 glDeleteTextures(1, &id);
159 // TODO check which params actually need refreshing
164 Texture2D::Loader::Loader(Texture2D &t):
165 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
170 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
171 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
176 void Texture2D::Loader::init()
178 add("raw_data", &Loader::raw_data);
179 add("storage", &Loader::storage);
182 void Texture2D::Loader::raw_data(const string &data)
184 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
187 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
190 fmt = get_srgb_pixelformat(fmt);
191 obj.storage(fmt, w, h);
195 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
198 srgb_conversion(false),
199 pixel_buffer(PIXEL_UNPACK_BUFFER),
204 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
209 bool Texture2D::AsyncLoader::needs_sync() const
214 bool Texture2D::AsyncLoader::process()
218 /* TODO Enhance the ImageLoader system so that the image can be loaded
219 directly to the buffer */
221 n_bytes = image.get_stride()*image.get_height();
225 pixel_buffer.data(n_bytes, 0);
226 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
230 const char *data = reinterpret_cast<const char *>(image.get_data());
231 copy(data, data+n_bytes, mapped_address);
235 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
236 if(!pixel_buffer.unmap())
243 glGenTextures(1, &texture.id);
244 texture.image(image, srgb_conversion, true);