9 Texture2D::Texture2D():
14 Texture2D::~Texture2D()
19 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
23 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
24 throw incompatible_data("Texture2D::storage");
28 throw invalid_argument("Texture2D::storage");
33 levels = get_n_levels();
35 levels = min(levels, lv);
40 void Texture2D::image(unsigned level, const void *data)
42 LinAl::Vector<unsigned, 2> size = get_level_size(level);
43 return sub_image(level, 0, 0, size.x, size.y, data);
46 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
48 if(width==0 || height==0)
49 throw invalid_operation("Texture2D::sub_image");
51 throw out_of_range("Texture2D::sub_image");
53 Texture2DBackend::sub_image(level, x, y, wd, ht, data);
56 void Texture2D::image(const Graphics::Image &img, unsigned lv)
58 storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
59 image(0, img.get_pixels());
62 unsigned Texture2D::get_n_levels() const
65 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
69 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
71 unsigned w = width>>level;
72 unsigned h = height>>level;
79 return LinAl::Vector<unsigned, 2>(w, h);
82 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
84 return create_async_loader(io);
87 uint64_t Texture2D::get_data_size() const
89 return id ? width*height*get_pixel_size(format) : 0;
93 Texture2D::Loader::Loader(Texture2D &t):
94 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
99 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
100 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
105 void Texture2D::Loader::init()
107 add("raw_data", &Loader::raw_data);
108 add("storage", &Loader::storage);
109 add("storage", &Loader::storage_levels);
112 void Texture2D::Loader::raw_data(const string &data)
116 obj.image(0, data.data());
119 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
121 obj.storage(fmt, w, h);
124 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
126 obj.storage(fmt, w, h, l);