]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.cpp
0fc300e0a09e7b4659aefe3bb842228797f6f3b1
[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         if(allocated&(1<<level))
74                 return;
75
76         if(ARB_texture_storage)
77         {
78                 GLenum fmt = get_gl_pixelformat(storage_fmt);
79                 if(ARB_direct_state_access)
80                         glTextureStorage2D(id, levels, fmt, width, height);
81                 else
82                 {
83                         bind_scratch();
84                         glTexStorage2D(target, levels, fmt, width, height);
85                 }
86                 apply_swizzle();
87                 allocated |= (1<<levels)-1;
88         }
89         else
90                 image(level, 0);
91 }
92
93 void Texture2D::image(unsigned level, const void *data)
94 {
95         if(width==0 || height==0)
96                 throw invalid_operation("Texture2D::image");
97         if(level>=levels)
98                 throw out_of_range("Texture2D::image");
99
100         LinAl::Vector<unsigned, 2> size = get_level_size(level);
101
102         if(ARB_texture_storage)
103                 return sub_image(level, 0, 0, size.x, size.y, data);
104
105         bind_scratch();
106
107         if(!allocated)
108         {
109                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
110                 apply_swizzle();
111         }
112
113         GLenum fmt = get_gl_pixelformat(storage_fmt);
114         GLenum comp = get_gl_components(get_components(storage_fmt));
115         GLenum type = get_gl_type(get_component_type(storage_fmt));
116         glTexImage2D(target, level, fmt, size.x, size.y, 0, comp, type, data);
117
118         allocated |= 1<<level;
119 }
120
121 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
122 {
123         if(comp!=get_components(format) || type!=get_component_type(format))
124                 throw incompatible_data("Texture2D::image");
125         image(level, data);
126 }
127
128 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
129 {
130         if(width==0 || height==0)
131                 throw invalid_operation("Texture2D::sub_image");
132         if(level>=levels)
133                 throw out_of_range("Texture2D::sub_image");
134
135         allocate(level);
136
137         GLenum comp = get_gl_components(get_components(storage_fmt));
138         GLenum type = get_gl_type(get_component_type(storage_fmt));
139         if(ARB_direct_state_access)
140                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
141         else
142         {
143                 bind_scratch();
144                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
145         }
146 }
147
148 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
149 {
150         if(comp!=get_components(format) || type!=get_component_type(format))
151                 throw incompatible_data("Texture2D::sub_image");
152         sub_image(level, x, y, wd, ht, data);
153 }
154
155 void Texture2D::image(const Graphics::Image &img, unsigned lv)
156 {
157         image(img, lv, false);
158 }
159
160 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
161 {
162         unsigned w = img.get_width();
163         unsigned h = img.get_height();
164         PixelFormat fmt = pixelformat_from_image(img);
165         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
166
167         image(0, from_buffer ? 0 : img.get_pixels());
168 }
169
170 unsigned Texture2D::get_n_levels() const
171 {
172         unsigned n = 0;
173         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
174         return n;
175 }
176
177 LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
178 {
179         unsigned w = width>>level;
180         unsigned h = height>>level;
181
182         if(!w && h)
183                 w = 1;
184         else if(!h && w)
185                 h = 1;
186
187         return LinAl::Vector<unsigned, 2>(w, h);
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 }
207
208
209 Texture2D::Loader::Loader(Texture2D &t):
210         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
211 {
212         init();
213 }
214
215 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
216         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
217 {
218         init();
219 }
220
221 void Texture2D::Loader::init()
222 {
223         add("raw_data", &Loader::raw_data);
224         add("storage", &Loader::storage);
225         add("storage", &Loader::storage_levels);
226 }
227
228 void Texture2D::Loader::raw_data(const string &data)
229 {
230         if(obj.manager)
231         {
232                 obj.set_manager(0);
233                 if(!obj.id)
234                         obj.generate_id();
235         }
236         obj.image(0, data.data());
237 }
238
239 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
240 {
241         obj.storage(fmt, w, h);
242 }
243
244 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
245 {
246         obj.storage(fmt, w, h, l);
247 }
248
249
250 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
251         texture(t),
252         io(i),
253         mapped_address(0),
254         img_loader(Graphics::ImageLoader::open_io(io)),
255         phase(0)
256 { }
257
258 Texture2D::AsyncLoader::~AsyncLoader()
259 {
260         if(mapped_address)
261                 pixel_buffer.unmap();
262         delete img_loader;
263 }
264
265 bool Texture2D::AsyncLoader::needs_sync() const
266 {
267         return phase%2;
268 }
269
270 bool Texture2D::AsyncLoader::process()
271 {
272         if(phase==0)
273         {
274                 image.load_headers(*img_loader);
275                 n_bytes = image.get_stride()*image.get_height();
276         }
277         else if(phase==1)
278         {
279                 pixel_buffer.storage(n_bytes);
280                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
281         }
282         else if(phase==2)
283                 image.load_into(*img_loader, mapped_address);
284         else if(phase==3)
285         {
286                 mapped_address = 0;
287                 if(!pixel_buffer.unmap())
288                 {
289                         phase = 1;
290                         return false;
291                 }
292
293                 if(!texture.id)
294                         texture.generate_id();
295                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer.get_id());
296                 texture.image(image, 0, true);
297                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
298                 if(texture.auto_gen_mipmap)
299                         texture.generate_mipmap();
300         }
301
302         ++phase;
303         return phase>3;
304 }
305
306 } // namespace GL
307 } // namespace Msp