]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Simplify applying texture swizzling
[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_internal_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, ifmt, width, height);
78                 else
79                         glTexStorage2D(target, levels, ifmt, width, height);
80                 apply_swizzle();
81                 allocated |= (1<<levels)-1;
82         }
83         else
84         {
85                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
86                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
87         }
88 }
89
90 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
91 {
92         if(width==0 || height==0)
93                 throw invalid_operation("Texture2D::image");
94
95         unsigned w = width;
96         unsigned h = height;
97         get_level_size(level, w, h);
98
99         if(ARB_texture_storage)
100                 return sub_image(level, 0, 0, w, h, fmt, type, data);
101
102         BindRestore _bind(this);
103
104         if(!allocated)
105         {
106                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
107                 apply_swizzle();
108         }
109         glTexImage2D(target, level, ifmt, w, h, 0, get_upload_format(fmt), 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::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
120 {
121         if(width==0 || height==0)
122                 throw invalid_operation("Texture2D::sub_image");
123
124         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
125         allocate(level);
126
127         fmt = get_upload_format(fmt);
128         if(ARB_direct_state_access)
129                 glTextureSubImage2D(id, level, x, y, wd, ht, fmt, type, data);
130         else
131                 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
132
133         if(auto_gen_mipmap && level==0)
134                 generate_mipmap();
135 }
136
137 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
138 {
139         image(img, lv, srgb, false);
140 }
141
142 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
143 {
144         unsigned w = img.get_width();
145         unsigned h = img.get_height();
146         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
147         if(width==0)
148                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, lv);
149         else if(w!=width || h!=height || (lv && lv!=levels))
150                 throw incompatible_data("Texture2D::image");
151
152         PixelStore pstore = PixelStore::from_image(img);
153         BindRestore _bind_ps(pstore);
154
155         image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
156 }
157
158 unsigned Texture2D::get_n_levels() const
159 {
160         unsigned n = 0;
161         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
162         return n;
163 }
164
165 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
166 {
167         w >>= level;
168         h >>= level;
169
170         if(!w && h)
171                 w = 1;
172         else if(!h && w)
173                 h = 1;
174 }
175
176 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
177 {
178         AsyncLoader *ldr = new AsyncLoader(*this, io);
179         if(res)
180                 ldr->set_srgb_conversion(res->get_srgb_conversion());
181         return ldr;
182 }
183
184 UInt64 Texture2D::get_data_size() const
185 {
186         return id ? width*height*get_pixel_size(ifmt) : 0;
187 }
188
189 void Texture2D::unload()
190 {
191         glDeleteTextures(1, &id);
192         id = 0;
193         allocated = 0;
194         default_sampler.unload();
195 }
196
197
198 Texture2D::Loader::Loader(Texture2D &t):
199         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
200 {
201         init();
202 }
203
204 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
205         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
206 {
207         init();
208 }
209
210 void Texture2D::Loader::init()
211 {
212         add("raw_data", &Loader::raw_data);
213         add("storage", &Loader::storage);
214         add("storage", &Loader::storage_levels);
215 }
216
217 void Texture2D::Loader::raw_data(const string &data)
218 {
219         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
220 }
221
222 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
223 {
224         obj.storage(fmt, w, h);
225 }
226
227 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
228 {
229         obj.storage(fmt, w, h, l);
230 }
231
232
233 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
234         texture(t),
235         io(i),
236         srgb_conversion(false),
237         pixel_buffer(PIXEL_UNPACK_BUFFER),
238         mapped_address(0),
239         phase(0)
240 { }
241
242 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
243 {
244         srgb_conversion = c;
245 }
246
247 bool Texture2D::AsyncLoader::needs_sync() const
248 {
249         return phase%2;
250 }
251
252 bool Texture2D::AsyncLoader::process()
253 {
254         if(phase==0)
255         {
256                 /* TODO Enhance the ImageLoader system so that the image can be loaded
257                 directly to the buffer */
258                 image.load_io(io);
259                 n_bytes = image.get_stride()*image.get_height();
260         }
261         else if(phase==1)
262         {
263                 pixel_buffer.storage(n_bytes);
264                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
265         }
266         else if(phase==2)
267         {
268                 const char *data = reinterpret_cast<const char *>(image.get_data());
269                 copy(data, data+n_bytes, mapped_address);
270         }
271         else if(phase==3)
272         {
273                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
274                 if(!pixel_buffer.unmap())
275                 {
276                         phase = 1;
277                         return false;
278                 }
279
280                 if(!texture.id)
281                 {
282                         if(ARB_direct_state_access)
283                                 glCreateTextures(texture.target, 1, &texture.id);
284                         else
285                                 glGenTextures(1, &texture.id);
286                 }
287                 texture.image(image, 0, srgb_conversion, true);
288         }
289
290         ++phase;
291         return phase>3;
292 }
293
294 } // namespace GL
295 } // namespace Msp