9 Texture2D::~Texture2D()
14 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
18 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
19 throw incompatible_data("Texture2D::storage");
23 throw invalid_argument("Texture2D::storage");
28 levels = get_n_levels();
30 levels = min(levels, lv);
35 void Texture2D::image(unsigned level, const void *data)
37 LinAl::Vector<unsigned, 2> size = get_level_size(level);
38 return sub_image(level, 0, 0, size.x, size.y, data);
41 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
43 if(width==0 || height==0)
44 throw invalid_operation("Texture2D::sub_image");
46 throw out_of_range("Texture2D::sub_image");
48 Texture2DBackend::sub_image(level, x, y, wd, ht, data);
51 void Texture2D::image(const Graphics::Image &img, unsigned lv)
53 storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
54 image(0, img.get_pixels());
57 unsigned Texture2D::get_n_levels() const
60 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
64 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
66 unsigned w = width>>level;
67 unsigned h = height>>level;
74 return LinAl::Vector<unsigned, 2>(w, h);
77 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
79 return create_async_loader(io);
82 uint64_t Texture2D::get_data_size() const
84 return id ? width*height*get_pixel_size(format) : 0;
88 Texture2D::Loader::Loader(Texture2D &t):
89 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
94 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
95 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
100 void Texture2D::Loader::init()
102 add("raw_data", &Loader::raw_data);
103 add("storage", &Loader::storage);
104 add("storage", &Loader::storage_levels);
107 void Texture2D::Loader::raw_data(const string &data)
111 obj.image(0, data.data());
114 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
116 obj.storage(fmt, w, h);
119 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
121 obj.storage(fmt, w, h, l);