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