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