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");
53 require_pixelformat(fmt);
60 void Texture2D::allocate(unsigned level)
62 if(allocated&(1<<level))
65 PixelFormat base_fmt = get_base_pixelformat(ifmt);
66 image(level, base_fmt, get_alloc_type(base_fmt), 0);
69 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
71 if(width==0 || height==0)
72 throw invalid_operation("Texture2D::image");
76 get_level_size(level, w, h);
78 BindRestore _bind(this);
79 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
81 allocated |= 1<<level;
82 if(gen_mipmap && level==0)
84 auto_generate_mipmap();
85 for(; (w || h); w>>=1, h>>=1, ++level) ;
86 allocated |= (1<<level)-1;
90 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
92 if(width==0 || height==0)
93 throw invalid_operation("Texture2D::sub_image");
97 BindRestore _bind(this);
98 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
101 void Texture2D::image(const Graphics::Image &img, bool srgb)
103 image(img, srgb, false);
106 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
108 unsigned w = img.get_width();
109 unsigned h = img.get_height();
110 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
113 else if(w!=width || h!=height)
114 throw incompatible_data("Texture2D::image");
116 PixelStore pstore = PixelStore::from_image(img);
117 BindRestore _bind_ps(pstore);
119 image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
122 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
133 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
135 AsyncLoader *ldr = new AsyncLoader(*this, io);
137 ldr->set_srgb_conversion(res->get_srgb_conversion());
141 UInt64 Texture2D::get_data_size() const
143 return id ? width*height*get_component_count(ifmt) : 0;
146 void Texture2D::unload()
148 glDeleteTextures(1, &id);
150 // TODO check which params actually need refreshing
155 Texture2D::Loader::Loader(Texture2D &t):
156 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
161 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
162 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
167 void Texture2D::Loader::init()
169 add("raw_data", &Loader::raw_data);
170 add("storage", &Loader::storage);
173 void Texture2D::Loader::raw_data(const string &data)
175 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
178 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
181 fmt = get_srgb_pixelformat(fmt);
182 obj.storage(fmt, w, h);
186 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
189 srgb_conversion(false),
190 pixel_buffer(PIXEL_UNPACK_BUFFER),
195 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
200 bool Texture2D::AsyncLoader::needs_sync() const
205 bool Texture2D::AsyncLoader::process()
209 /* TODO Enhance the ImageLoader system so that the image can be loaded
210 directly to the buffer */
212 n_bytes = image.get_stride()*image.get_height();
216 pixel_buffer.data(n_bytes, 0);
217 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
221 const char *data = reinterpret_cast<const char *>(image.get_data());
222 copy(data, data+n_bytes, mapped_address);
226 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
227 if(!pixel_buffer.unmap())
234 glGenTextures(1, &texture.id);
235 texture.image(image, srgb_conversion, true);