]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.cpp
Remove the Bindable class
[libs/gl.git] / source / core / 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 <msp/graphics/imageloader.h>
5 #include "buffer.h"
6 #include "error.h"
7 #include "resources.h"
8 #include "texture2d.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 class Texture2D::AsyncLoader: public Resource::AsyncLoader
16 {
17 private:
18         Texture2D &texture;
19         IO::Seekable &io;
20         Buffer pixel_buffer;
21         char *mapped_address;
22         Graphics::Image image;
23         Graphics::ImageLoader *img_loader;
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         {
52                 if(fmt!=format || wd!=width || ht!=height || (lv && lv!=levels))
53                         throw incompatible_data("Texture2D::storage");
54                 return;
55         }
56         if(wd==0 || ht==0)
57                 throw invalid_argument("Texture2D::storage");
58
59         set_format(fmt);
60         width = wd;
61         height = ht;
62         levels = get_n_levels();
63         if(lv>0)
64                 levels = min(levels, lv);
65 }
66
67 void Texture2D::allocate(unsigned level)
68 {
69         if(width==0 || height==0)
70                 throw invalid_operation("Texture2D::allocate");
71         if(level>=levels)
72                 throw invalid_argument("Texture2D::allocate");
73
74         bool direct = ARB_texture_storage && ARB_direct_state_access;
75         if(!direct)
76         {
77                 glActiveTexture(GL_TEXTURE0);
78                 glBindTexture(target, id);
79         }
80
81         allocate_(level);
82
83         if(!direct)
84                 glBindTexture(target, 0);
85 }
86
87 void Texture2D::allocate_(unsigned level)
88 {
89         if(allocated&(1<<level))
90                 return;
91
92         if(ARB_texture_storage)
93         {
94                 if(ARB_direct_state_access)
95                         glTextureStorage2D(id, levels, storage_fmt, width, height);
96                 else
97                         glTexStorage2D(target, levels, storage_fmt, width, height);
98                 apply_swizzle();
99                 allocated |= (1<<levels)-1;
100         }
101         else
102                 image_(level, 0);
103 }
104
105 void Texture2D::image(unsigned level, const void *data)
106 {
107         if(width==0 || height==0)
108                 throw invalid_operation("Texture2D::image");
109         if(level>=levels)
110                 throw out_of_range("Texture2D::image");
111
112         if(ARB_texture_storage)
113         {
114                 LinAl::Vector<unsigned, 2> size = get_level_size(level);
115                 return sub_image(level, 0, 0, size.x, size.y, data);
116         }
117
118         glActiveTexture(GL_TEXTURE0);
119         glBindTexture(target, id);
120
121         image_(level, data);
122
123         if(auto_gen_mipmap && level==0)
124         {
125                 generate_mipmap_();
126                 allocated |= (1<<levels)-1;
127         }
128
129         glBindTexture(target, 0);
130 }
131
132 void Texture2D::image_(unsigned level, const void *data)
133 {
134         if(!allocated)
135         {
136                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
137                 apply_swizzle();
138         }
139
140         LinAl::Vector<unsigned, 2> size = get_level_size(level);
141         PixelComponents comp = get_components(storage_fmt);
142         GLenum type = get_gl_type(get_component_type(storage_fmt));
143         glTexImage2D(target, level, storage_fmt, size.x, size.y, 0, comp, type, data);
144
145         allocated |= 1<<level;
146 }
147
148 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
149 {
150         if(comp!=get_components(format) || type!=get_component_type(format))
151                 throw incompatible_data("Texture2D::image");
152         image(level, data);
153 }
154
155 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
156 {
157         if(width==0 || height==0)
158                 throw invalid_operation("Texture2D::sub_image");
159         if(level>=levels)
160                 throw out_of_range("Texture2D::sub_image");
161
162         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
163         if(!direct)
164         {
165                 glActiveTexture(GL_TEXTURE0);
166                 glBindTexture(target, id);
167         }
168
169         allocate_(level);
170
171         PixelComponents comp = get_components(storage_fmt);
172         GLenum type = get_gl_type(get_component_type(storage_fmt));
173         if(ARB_direct_state_access)
174                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
175         else
176                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
177
178         if(auto_gen_mipmap && level==0)
179                 generate_mipmap_();
180
181         if(!direct)
182                 glBindTexture(target, 0);
183 }
184
185 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
186 {
187         if(comp!=get_components(format) || type!=get_component_type(format))
188                 throw incompatible_data("Texture2D::sub_image");
189         sub_image(level, x, y, wd, ht, data);
190 }
191
192 void Texture2D::image(const Graphics::Image &img, unsigned lv)
193 {
194         image(img, lv, false);
195 }
196
197 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
198 {
199         unsigned w = img.get_width();
200         unsigned h = img.get_height();
201         PixelFormat fmt = pixelformat_from_image(img);
202         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
203
204         image(0, from_buffer ? 0 : img.get_pixels());
205 }
206
207 unsigned Texture2D::get_n_levels() const
208 {
209         unsigned n = 0;
210         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
211         return n;
212 }
213
214 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
215 {
216         unsigned w = width>>level;
217         unsigned h = height>>level;
218
219         if(!w && h)
220                 w = 1;
221         else if(!h && w)
222                 h = 1;
223
224         return LinAl::Vector<unsigned, 2>(w, h);
225 }
226
227 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
228 {
229         AsyncLoader *ldr = new AsyncLoader(*this, io);
230         return ldr;
231 }
232
233 UInt64 Texture2D::get_data_size() const
234 {
235         return id ? width*height*get_pixel_size(format) : 0;
236 }
237
238 void Texture2D::unload()
239 {
240         glDeleteTextures(1, &id);
241         id = 0;
242         allocated = 0;
243 }
244
245
246 Texture2D::Loader::Loader(Texture2D &t):
247         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
248 {
249         init();
250 }
251
252 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
253         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
254 {
255         init();
256 }
257
258 void Texture2D::Loader::init()
259 {
260         add("raw_data", &Loader::raw_data);
261         add("storage", &Loader::storage);
262         add("storage", &Loader::storage_levels);
263 }
264
265 void Texture2D::Loader::raw_data(const string &data)
266 {
267         if(obj.manager)
268         {
269                 obj.set_manager(0);
270                 if(!obj.id)
271                         obj.generate_id();
272         }
273         obj.image(0, data.data());
274 }
275
276 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
277 {
278         obj.storage(fmt, w, h);
279 }
280
281 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
282 {
283         obj.storage(fmt, w, h, l);
284 }
285
286
287 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
288         texture(t),
289         io(i),
290         mapped_address(0),
291         img_loader(Graphics::ImageLoader::open_io(io)),
292         phase(0)
293 { }
294
295 Texture2D::AsyncLoader::~AsyncLoader()
296 {
297         if(mapped_address)
298                 pixel_buffer.unmap();
299         delete img_loader;
300 }
301
302 bool Texture2D::AsyncLoader::needs_sync() const
303 {
304         return phase%2;
305 }
306
307 bool Texture2D::AsyncLoader::process()
308 {
309         if(phase==0)
310         {
311                 image.load_headers(*img_loader);
312                 n_bytes = image.get_stride()*image.get_height();
313         }
314         else if(phase==1)
315         {
316                 pixel_buffer.storage(n_bytes);
317                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
318         }
319         else if(phase==2)
320                 image.load_into(*img_loader, mapped_address);
321         else if(phase==3)
322         {
323                 mapped_address = 0;
324                 if(!pixel_buffer.unmap())
325                 {
326                         phase = 1;
327                         return false;
328                 }
329
330                 if(!texture.id)
331                         texture.generate_id();
332                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
333                 texture.image(image, 0, true);
334                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
335         }
336
337         ++phase;
338         return phase>3;
339 }
340
341 } // namespace GL
342 } // namespace Msp