]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Ensure that mapped memory is unmapped in Texture2D::AsyncLoader
[libs/gl.git] / source / texture2d.cpp
1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include "bindable.h"
5 #include "buffer.h"
6 #include "error.h"
7 #include "pixelstore.h"
8 #include "resources.h"
9 #include "texture2d.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 class Texture2D::AsyncLoader: public Resource::AsyncLoader
17 {
18 private:
19         Texture2D &texture;
20         IO::Seekable &io;
21         Buffer pixel_buffer;
22         char *mapped_address;
23         Graphics::Image image;
24         unsigned n_bytes;
25         int phase;
26
27 public:
28         AsyncLoader(Texture2D &, IO::Seekable &);
29         ~AsyncLoader();
30
31         virtual bool needs_sync() const;
32         virtual bool process();
33 };
34
35
36 Texture2D::Texture2D(ResourceManager *m):
37         Texture(GL_TEXTURE_2D, m),
38         width(0),
39         height(0),
40         allocated(0)
41 { }
42
43 Texture2D::~Texture2D()
44 {
45         set_manager(0);
46 }
47
48 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
49 {
50         if(width>0)
51                 throw invalid_operation("Texture2D::storage");
52         if(wd==0 || ht==0)
53                 throw invalid_argument("Texture2D::storage");
54
55         set_format(fmt);
56         width = wd;
57         height = ht;
58         levels = get_n_levels();
59         if(lv>0)
60                 levels = min(levels, lv);
61 }
62
63 void Texture2D::allocate(unsigned level)
64 {
65         if(width==0 || height==0)
66                 throw invalid_operation("Texture2D::allocate");
67         if(level>=levels)
68                 throw invalid_argument("Texture2D::allocate");
69         if(allocated&(1<<level))
70                 return;
71
72         if(ARB_texture_storage)
73         {
74                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
75                 if(ARB_direct_state_access)
76                         glTextureStorage2D(id, levels, storage_fmt, width, height);
77                 else
78                         glTexStorage2D(target, levels, storage_fmt, width, height);
79                 apply_swizzle();
80                 allocated |= (1<<levels)-1;
81         }
82         else
83                 image(level, 0);
84 }
85
86 void Texture2D::image(unsigned level, const void *data)
87 {
88         if(width==0 || height==0)
89                 throw invalid_operation("Texture2D::image");
90
91         unsigned w = width;
92         unsigned h = height;
93         get_level_size(level, w, h);
94
95         if(ARB_texture_storage)
96                 return sub_image(level, 0, 0, w, h, data);
97
98         BindRestore _bind(this);
99
100         if(!allocated)
101         {
102                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
103                 apply_swizzle();
104         }
105
106         PixelComponents comp = get_components(storage_fmt);
107         DataType type = get_component_type(storage_fmt);
108         glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
109
110         allocated |= 1<<level;
111         if(auto_gen_mipmap && level==0)
112         {
113                 generate_mipmap();
114                 allocated |= (1<<levels)-1;
115         }
116 }
117
118 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
119 {
120         if(comp!=get_components(format) || type!=get_component_type(format))
121                 throw incompatible_data("Texture2D::image");
122         image(level, data);
123 }
124
125 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
126 {
127         if(width==0 || height==0)
128                 throw invalid_operation("Texture2D::sub_image");
129
130         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
131         allocate(level);
132
133         PixelComponents comp = get_components(storage_fmt);
134         DataType type = get_component_type(storage_fmt);
135         if(ARB_direct_state_access)
136                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
137         else
138                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
139
140         if(auto_gen_mipmap && level==0)
141                 generate_mipmap();
142 }
143
144 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
145 {
146         if(comp!=get_components(format) || type!=get_component_type(format))
147                 throw incompatible_data("Texture2D::sub_image");
148         sub_image(level, x, y, wd, ht, data);
149 }
150
151 void Texture2D::image(const Graphics::Image &img, unsigned lv)
152 {
153         image(img, lv, false);
154 }
155
156 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
157 {
158         unsigned w = img.get_width();
159         unsigned h = img.get_height();
160         PixelFormat fmt = pixelformat_from_image(img);
161         if(width==0)
162                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
163         else if(w!=width || h!=height || (lv && lv!=levels))
164                 throw incompatible_data("Texture2D::image");
165
166         PixelStore pstore = PixelStore::from_image(img);
167         BindRestore _bind_ps(pstore);
168
169         image(0, from_buffer ? 0 : img.get_pixels());
170 }
171
172 unsigned Texture2D::get_n_levels() const
173 {
174         unsigned n = 0;
175         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
176         return n;
177 }
178
179 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
180 {
181         w >>= level;
182         h >>= level;
183
184         if(!w && h)
185                 w = 1;
186         else if(!h && w)
187                 h = 1;
188 }
189
190 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
191 {
192         AsyncLoader *ldr = new AsyncLoader(*this, io);
193         return ldr;
194 }
195
196 UInt64 Texture2D::get_data_size() const
197 {
198         return id ? width*height*get_pixel_size(format) : 0;
199 }
200
201 void Texture2D::unload()
202 {
203         glDeleteTextures(1, &id);
204         id = 0;
205         allocated = 0;
206         default_sampler.unload();
207 }
208
209
210 Texture2D::Loader::Loader(Texture2D &t):
211         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
212 {
213         init();
214 }
215
216 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
217         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
218 {
219         init();
220 }
221
222 void Texture2D::Loader::init()
223 {
224         add("raw_data", &Loader::raw_data);
225         add("storage", &Loader::storage);
226         add("storage", &Loader::storage_levels);
227 }
228
229 void Texture2D::Loader::raw_data(const string &data)
230 {
231         obj.image(0, data.data());
232 }
233
234 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
235 {
236         obj.storage(fmt, w, h);
237 }
238
239 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
240 {
241         obj.storage(fmt, w, h, l);
242 }
243
244
245 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
246         texture(t),
247         io(i),
248         pixel_buffer(PIXEL_UNPACK_BUFFER),
249         mapped_address(0),
250         phase(0)
251 { }
252
253 Texture2D::AsyncLoader::~AsyncLoader()
254 {
255         if(mapped_address)
256                 pixel_buffer.unmap();
257 }
258
259 bool Texture2D::AsyncLoader::needs_sync() const
260 {
261         return phase%2;
262 }
263
264 bool Texture2D::AsyncLoader::process()
265 {
266         if(phase==0)
267         {
268                 /* TODO Enhance the ImageLoader system so that the image can be loaded
269                 directly to the buffer */
270                 image.load_io(io);
271                 n_bytes = image.get_stride()*image.get_height();
272         }
273         else if(phase==1)
274         {
275                 pixel_buffer.storage(n_bytes);
276                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
277         }
278         else if(phase==2)
279         {
280                 const char *data = reinterpret_cast<const char *>(image.get_pixels());
281                 copy(data, data+n_bytes, mapped_address);
282         }
283         else if(phase==3)
284         {
285                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
286                 mapped_address = 0;
287                 if(!pixel_buffer.unmap())
288                 {
289                         phase = 1;
290                         return false;
291                 }
292
293                 if(!texture.id)
294                 {
295                         if(ARB_direct_state_access)
296                                 glCreateTextures(texture.target, 1, &texture.id);
297                         else
298                                 glGenTextures(1, &texture.id);
299                 }
300                 texture.image(image, 0, true);
301         }
302
303         ++phase;
304         return phase>3;
305 }
306
307 } // namespace GL
308 } // namespace Msp