]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.cpp
Redesign asynchronous buffer uploads
[libs/gl.git] / source / core / texture2d.cpp
1 #include "error.h"
2 #include "texture2d.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 Texture2D::~Texture2D()
10 {
11         set_manager(0);
12 }
13
14 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
15 {
16         if(width>0)
17         {
18                 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=n_levels))
19                         throw incompatible_data("Texture2D::storage");
20                 return;
21         }
22         if(wd==0 || ht==0)
23                 throw invalid_argument("Texture2D::storage");
24
25         set_format(fmt);
26         width = wd;
27         height = ht;
28         n_levels = count_levels(max(width, height));
29         if(lv>0)
30                 n_levels = min(n_levels, lv);
31
32         allocate();
33 }
34
35 void Texture2D::image(unsigned level, const void *data)
36 {
37         LinAl::Vector<unsigned, 2> size = get_level_size(level);
38         return sub_image(level, 0, 0, size.x, size.y, data);
39 }
40
41 void Texture2D::sub_image(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht, const void *data)
42 {
43         if(width==0 || height==0)
44                 throw invalid_operation("Texture2D::sub_image");
45         if(level>=n_levels || x>width || x+wd>width || y>height || y+ht>height)
46                 throw out_of_range("Texture2D::sub_image");
47
48         Texture2DBackend::sub_image(level, x, y, wd, ht, data);
49 }
50
51 void Texture2D::image(const Graphics::Image &img, unsigned lv)
52 {
53         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
54         image(0, img.get_pixels());
55 }
56
57 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
58 {
59         unsigned w = width>>level;
60         unsigned h = height>>level;
61
62         if(!w && h)
63                 w = 1;
64         else if(!h && w)
65                 h = 1;
66
67         return LinAl::Vector<unsigned, 2>(w, h);
68 }
69
70
71 Texture2D::Loader::Loader(Texture2D &t):
72         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
73 {
74         init();
75 }
76
77 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
78         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
79 {
80         init();
81 }
82
83 void Texture2D::Loader::init()
84 {
85         add("storage", &Loader::storage);
86         add("storage", &Loader::storage_levels);
87 }
88
89 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
90 {
91         obj.storage(fmt, w, h);
92 }
93
94 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
95 {
96         obj.storage(fmt, w, h, l);
97 }
98
99 } // namespace GL
100 } // namespace Msp