1 #include <msp/datafile/rawdata.h>
2 #include <msp/graphics/imageloader.h>
11 class Texture2D::AsyncLoader: public Resource::AsyncLoader
16 Texture2D::AsyncTransfer transfer;
17 Graphics::Image image;
18 Graphics::ImageLoader *img_loader = 0;
19 DataFile::RawData *raw_data = 0;
24 AsyncLoader(Texture2D &, IO::Seekable &);
27 virtual bool needs_sync() const;
28 virtual bool process();
32 Texture2D::~Texture2D()
37 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
41 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=n_levels))
42 throw incompatible_data("Texture2D::storage");
46 throw invalid_argument("Texture2D::storage");
51 n_levels = count_levels(max(width, height));
53 n_levels = min(n_levels, lv);
58 void Texture2D::image(unsigned level, const void *data)
60 LinAl::Vector<unsigned, 2> size = get_level_size(level);
61 return sub_image(level, 0, 0, size.x, size.y, data);
64 void Texture2D::sub_image(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *data)
66 if(width==0 || height==0)
67 throw invalid_operation("Texture2D::sub_image");
68 if(level>=n_levels || x>width || x+wd>width || y>height || y+ht>height)
69 throw out_of_range("Texture2D::sub_image");
71 Texture2DBackend::sub_image(level, x, y, wd, ht, data);
74 Texture2D::AsyncTransfer Texture2D::sub_image_async(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht)
76 return AsyncTransfer(*this, level, x, y, wd, ht);
79 void Texture2D::image(const Graphics::Image &img, unsigned lv)
81 storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
82 image(0, img.get_pixels());
85 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
87 unsigned w = width>>level;
88 unsigned h = height>>level;
95 return LinAl::Vector<unsigned, 2>(w, h);
98 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
100 return new AsyncLoader(static_cast<Texture2D &>(*this), io);
104 Texture2D::Loader::Loader(Texture2D &t):
105 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
110 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
111 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
116 void Texture2D::Loader::init()
118 add("storage", &Loader::storage);
119 add("storage", &Loader::storage_levels);
122 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
124 obj.storage(fmt, w, h);
127 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
129 obj.storage(fmt, w, h, l);
133 Texture2D::AsyncTransfer::AsyncTransfer(Texture2D &t, unsigned l, unsigned x_, unsigned y_, unsigned wd, unsigned ht):
140 data_size(width*height*get_pixel_size(texture->storage_fmt)),
143 dest_addr = allocate();
146 Texture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
147 Texture2DBackend::AsyncTransfer(move(other)),
148 texture(other.texture),
153 height(other.height),
154 data_size(other.data_size),
155 dest_addr(other.dest_addr)
160 Texture2D::AsyncTransfer &Texture2D::AsyncTransfer::operator=(AsyncTransfer &&other)
165 Texture2DBackend::AsyncTransfer::operator=(move(other));
167 texture = other.texture;
172 height = other.height;
173 data_size = other.data_size;
174 dest_addr = other.dest_addr;
181 Texture2D::AsyncTransfer::~AsyncTransfer()
188 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
194 io.seek(0, IO::S_BEG);
196 if(DataFile::RawData::detect_signature(string(magic, 4)))
198 raw_data = new DataFile::RawData;
199 raw_data->open_io(io, "async");
202 img_loader = Graphics::ImageLoader::open_io(io);
205 Texture2D::AsyncLoader::~AsyncLoader()
211 bool Texture2D::AsyncLoader::needs_sync() const
216 bool Texture2D::AsyncLoader::process()
221 n_bytes = raw_data->get_size();
224 image.load_headers(*img_loader);
225 n_bytes = image.get_stride()*image.get_height();
232 unsigned w = image.get_width();
233 unsigned h = image.get_height();
234 texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h);
237 transfer = texture.sub_image_async(0, 0, 0, texture.width, texture.height);
241 if(texture.swizzle==RGBA_TO_RGB)
247 data = raw_data->get_data();
251 image.load(*img_loader);
252 data = image.get_pixels();
254 texture.stage_pixels(transfer.get_address(), data, texture.width*texture.height);
257 raw_data->load_into(transfer.get_address());
259 image.load_into(*img_loader, transfer.get_address());
263 transfer = Texture2D::AsyncTransfer();
265 if(texture.auto_gen_mipmap)
266 texture.generate_mipmap();