9 Texture2D::Texture2D(ResourceManager *m):
15 Texture2D::~Texture2D()
20 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
24 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
25 throw incompatible_data("Texture2D::storage");
29 throw invalid_argument("Texture2D::storage");
34 levels = get_n_levels();
36 levels = min(levels, lv);
41 void Texture2D::image(unsigned level, const void *data)
43 LinAl::Vector<unsigned, 2> size = get_level_size(level);
44 return sub_image(level, 0, 0, size.x, size.y, data);
47 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
49 if(width==0 || height==0)
50 throw invalid_operation("Texture2D::sub_image");
52 throw out_of_range("Texture2D::sub_image");
54 Texture2DBackend::sub_image(level, x, y, wd, ht, data);
57 void Texture2D::image(const Graphics::Image &img, unsigned lv)
59 storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
60 image(0, img.get_pixels());
63 unsigned Texture2D::get_n_levels() const
66 for(unsigned s=max(width, height); s; s>>=1, ++n) ;
70 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
72 unsigned w = width>>level;
73 unsigned h = height>>level;
80 return LinAl::Vector<unsigned, 2>(w, h);
83 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
85 return create_async_loader(io);
88 uint64_t Texture2D::get_data_size() const
90 return id ? width*height*get_pixel_size(format) : 0;
94 Texture2D::Loader::Loader(Texture2D &t):
95 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
100 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
101 DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
106 void Texture2D::Loader::init()
108 add("raw_data", &Loader::raw_data);
109 add("storage", &Loader::storage);
110 add("storage", &Loader::storage_levels);
113 void Texture2D::Loader::raw_data(const string &data)
121 obj.image(0, data.data());
124 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
126 obj.storage(fmt, w, h);
129 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
131 obj.storage(fmt, w, h, l);