]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Require texture data to be uploaded in a format matching the storage
[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         bool srgb_conversion;
22         Buffer pixel_buffer;
23         char *mapped_address;
24         Graphics::Image image;
25         unsigned n_bytes;
26         int phase;
27
28 public:
29         AsyncLoader(Texture2D &, IO::Seekable &);
30
31         void set_srgb_conversion(bool);
32         virtual bool needs_sync() const;
33         virtual bool process();
34 };
35
36
37 Texture2D::Texture2D(ResourceManager *m):
38         Texture(GL_TEXTURE_2D, m),
39         width(0),
40         height(0),
41         allocated(0)
42 { }
43
44 Texture2D::~Texture2D()
45 {
46         set_manager(0);
47 }
48
49 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
50 {
51         if(width>0)
52                 throw invalid_operation("Texture2D::storage");
53         if(wd==0 || ht==0)
54                 throw invalid_argument("Texture2D::storage");
55
56         set_format(fmt);
57         width = wd;
58         height = ht;
59         levels = get_n_levels();
60         if(lv>0)
61                 levels = min(levels, lv);
62 }
63
64 void Texture2D::allocate(unsigned level)
65 {
66         if(width==0 || height==0)
67                 throw invalid_operation("Texture2D::allocate");
68         if(level>=levels)
69                 throw invalid_argument("Texture2D::allocate");
70         if(allocated&(1<<level))
71                 return;
72
73         if(ARB_texture_storage)
74         {
75                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
76                 if(ARB_direct_state_access)
77                         glTextureStorage2D(id, levels, storage_fmt, width, height);
78                 else
79                         glTexStorage2D(target, levels, storage_fmt, width, height);
80                 apply_swizzle();
81                 allocated |= (1<<levels)-1;
82         }
83         else
84                 image(level, 0);
85 }
86
87 void Texture2D::image(unsigned level, const void *data)
88 {
89         if(width==0 || height==0)
90                 throw invalid_operation("Texture2D::image");
91
92         unsigned w = width;
93         unsigned h = height;
94         get_level_size(level, w, h);
95
96         if(ARB_texture_storage)
97                 return sub_image(level, 0, 0, w, h, data);
98
99         BindRestore _bind(this);
100
101         if(!allocated)
102         {
103                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
104                 apply_swizzle();
105         }
106
107         PixelComponents comp = get_components(storage_fmt);
108         DataType type = get_component_type(storage_fmt);
109         glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
110
111         allocated |= 1<<level;
112         if(auto_gen_mipmap && level==0)
113         {
114                 generate_mipmap();
115                 allocated |= (1<<levels)-1;
116         }
117 }
118
119 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
120 {
121         if(comp!=get_components(format) || type!=get_component_type(format))
122                 throw incompatible_data("Texture2D::image");
123         image(level, data);
124 }
125
126 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
127 {
128         if(width==0 || height==0)
129                 throw invalid_operation("Texture2D::sub_image");
130
131         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
132         allocate(level);
133
134         PixelComponents comp = get_components(storage_fmt);
135         DataType type = get_component_type(storage_fmt);
136         if(ARB_direct_state_access)
137                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
138         else
139                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
140
141         if(auto_gen_mipmap && level==0)
142                 generate_mipmap();
143 }
144
145 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
146 {
147         if(comp!=get_components(format) || type!=get_component_type(format))
148                 throw incompatible_data("Texture2D::sub_image");
149         sub_image(level, x, y, wd, ht, data);
150 }
151
152 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
153 {
154         image(img, lv, srgb, false);
155 }
156
157 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
158 {
159         unsigned w = img.get_width();
160         unsigned h = img.get_height();
161         PixelFormat fmt = pixelformat_from_image(img);
162         if(width==0)
163                 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w, h, lv);
164         else if(w!=width || h!=height || (lv && lv!=levels))
165                 throw incompatible_data("Texture2D::image");
166
167         PixelStore pstore = PixelStore::from_image(img);
168         BindRestore _bind_ps(pstore);
169
170         image(0, from_buffer ? 0 : img.get_data());
171 }
172
173 unsigned Texture2D::get_n_levels() const
174 {
175         unsigned n = 0;
176         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
177         return n;
178 }
179
180 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
181 {
182         w >>= level;
183         h >>= level;
184
185         if(!w && h)
186                 w = 1;
187         else if(!h && w)
188                 h = 1;
189 }
190
191 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
192 {
193         AsyncLoader *ldr = new AsyncLoader(*this, io);
194         if(res)
195                 ldr->set_srgb_conversion(res->get_srgb_conversion());
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         obj.image(0, data.data());
235 }
236
237 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
238 {
239         obj.storage(fmt, w, h);
240 }
241
242 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
243 {
244         obj.storage(fmt, w, h, l);
245 }
246
247
248 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
249         texture(t),
250         io(i),
251         srgb_conversion(false),
252         pixel_buffer(PIXEL_UNPACK_BUFFER),
253         mapped_address(0),
254         phase(0)
255 { }
256
257 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
258 {
259         srgb_conversion = c;
260 }
261
262 bool Texture2D::AsyncLoader::needs_sync() const
263 {
264         return phase%2;
265 }
266
267 bool Texture2D::AsyncLoader::process()
268 {
269         if(phase==0)
270         {
271                 /* TODO Enhance the ImageLoader system so that the image can be loaded
272                 directly to the buffer */
273                 image.load_io(io);
274                 n_bytes = image.get_stride()*image.get_height();
275         }
276         else if(phase==1)
277         {
278                 pixel_buffer.storage(n_bytes);
279                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
280         }
281         else if(phase==2)
282         {
283                 const char *data = reinterpret_cast<const char *>(image.get_data());
284                 copy(data, data+n_bytes, mapped_address);
285         }
286         else if(phase==3)
287         {
288                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
289                 if(!pixel_buffer.unmap())
290                 {
291                         phase = 1;
292                         return false;
293                 }
294
295                 if(!texture.id)
296                 {
297                         if(ARB_direct_state_access)
298                                 glCreateTextures(texture.target, 1, &texture.id);
299                         else
300                                 glGenTextures(1, &texture.id);
301                 }
302                 texture.image(image, 0, srgb_conversion, true);
303         }
304
305         ++phase;
306         return phase>3;
307 }
308
309 } // namespace GL
310 } // namespace Msp