]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.cpp
Add an asynchronous version of Texture2D::sub_image
[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 Texture2D::AsyncTransfer Texture2D::sub_image_async(unsigned level, unsigned x, unsigned y, unsigned wd, unsigned ht)
52 {
53         return AsyncTransfer(*this, level, x, y, wd, ht);
54 }
55
56 void Texture2D::image(const Graphics::Image &img, unsigned lv)
57 {
58         storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), img.get_height(), lv);
59         image(0, img.get_pixels());
60 }
61
62 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
63 {
64         unsigned w = width>>level;
65         unsigned h = height>>level;
66
67         if(!w && h)
68                 w = 1;
69         else if(!h && w)
70                 h = 1;
71
72         return LinAl::Vector<unsigned, 2>(w, h);
73 }
74
75
76 Texture2D::Loader::Loader(Texture2D &t):
77         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
78 {
79         init();
80 }
81
82 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
83         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
84 {
85         init();
86 }
87
88 void Texture2D::Loader::init()
89 {
90         add("storage", &Loader::storage);
91         add("storage", &Loader::storage_levels);
92 }
93
94 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
95 {
96         obj.storage(fmt, w, h);
97 }
98
99 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
100 {
101         obj.storage(fmt, w, h, l);
102 }
103
104
105 Texture2D::AsyncTransfer::AsyncTransfer(Texture2D &t, unsigned l, unsigned x_, unsigned y_, unsigned wd, unsigned ht):
106         texture(&t),
107         level(l),
108         x(x_),
109         y(y_),
110         width(wd),
111         height(ht),
112         data_size(width*height*get_pixel_size(texture->storage_fmt)),
113         dest_addr(0)
114 {
115         dest_addr = allocate();
116 }
117
118 Texture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
119         Texture2DBackend::AsyncTransfer(move(other)),
120         texture(other.texture),
121         level(other.level),
122         x(other.x),
123         y(other.y),
124         width(other.width),
125         height(other.height),
126         data_size(other.data_size),
127         dest_addr(other.dest_addr)
128 {
129         other.dest_addr = 0;
130 }
131
132 Texture2D::AsyncTransfer &Texture2D::AsyncTransfer::operator=(AsyncTransfer &&other)
133 {
134         if(dest_addr)
135                 finalize();
136
137         Texture2DBackend::AsyncTransfer::operator=(move(other));
138
139         texture = other.texture;
140         level = other.level;
141         x = other.x;
142         y = other.y;
143         width = other.width;
144         height = other.height;
145         data_size = other.data_size;
146         dest_addr = other.dest_addr;
147
148         other.dest_addr = 0;
149
150         return *this;
151 }
152
153 Texture2D::AsyncTransfer::~AsyncTransfer()
154 {
155         if(dest_addr)
156                 finalize();
157 }
158
159 } // namespace GL
160 } // namespace Msp