]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Allow texture mipmap levels to be specified in datafiles
[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                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, lv);
147         else if(w!=width || h!=height || (lv && lv!=levels))
148                 throw incompatible_data("Texture2D::image");
149
150         PixelStore pstore = PixelStore::from_image(img);
151         BindRestore _bind_ps(pstore);
152
153         image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
154 }
155
156 unsigned Texture2D::get_n_levels() const
157 {
158         unsigned n = 0;
159         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
160         return n;
161 }
162
163 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
164 {
165         w >>= level;
166         h >>= level;
167
168         if(!w && h)
169                 w = 1;
170         else if(!h && w)
171                 h = 1;
172 }
173
174 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
175 {
176         AsyncLoader *ldr = new AsyncLoader(*this, io);
177         if(res)
178                 ldr->set_srgb_conversion(res->get_srgb_conversion());
179         return ldr;
180 }
181
182 UInt64 Texture2D::get_data_size() const
183 {
184         return id ? width*height*get_pixel_size(ifmt) : 0;
185 }
186
187 void Texture2D::unload()
188 {
189         glDeleteTextures(1, &id);
190         id = 0;
191         allocated = 0;
192         // TODO check which params actually need refreshing
193         dirty_params = -1;
194 }
195
196
197 Texture2D::Loader::Loader(Texture2D &t):
198         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
199 {
200         init();
201 }
202
203 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
204         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
205 {
206         init();
207 }
208
209 void Texture2D::Loader::init()
210 {
211         add("raw_data", &Loader::raw_data);
212         add("storage", &Loader::storage);
213         add("storage", &Loader::storage_levels);
214 }
215
216 void Texture2D::Loader::raw_data(const string &data)
217 {
218         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
219 }
220
221 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
222 {
223         obj.storage(fmt, w, h);
224 }
225
226 void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned l)
227 {
228         obj.storage(fmt, w, h, l);
229 }
230
231
232 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
233         texture(t),
234         io(i),
235         srgb_conversion(false),
236         pixel_buffer(PIXEL_UNPACK_BUFFER),
237         mapped_address(0),
238         phase(0)
239 { }
240
241 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
242 {
243         srgb_conversion = c;
244 }
245
246 bool Texture2D::AsyncLoader::needs_sync() const
247 {
248         return phase%2;
249 }
250
251 bool Texture2D::AsyncLoader::process()
252 {
253         if(phase==0)
254         {
255                 /* TODO Enhance the ImageLoader system so that the image can be loaded
256                 directly to the buffer */
257                 image.load_io(io);
258                 n_bytes = image.get_stride()*image.get_height();
259         }
260         else if(phase==1)
261         {
262                 pixel_buffer.storage(n_bytes);
263                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
264         }
265         else if(phase==2)
266         {
267                 const char *data = reinterpret_cast<const char *>(image.get_data());
268                 copy(data, data+n_bytes, mapped_address);
269         }
270         else if(phase==3)
271         {
272                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
273                 if(!pixel_buffer.unmap())
274                 {
275                         phase = 1;
276                         return false;
277                 }
278
279                 if(!texture.id)
280                 {
281                         if(ARB_direct_state_access)
282                                 glCreateTextures(texture.target, 1, &texture.id);
283                         else
284                                 glGenTextures(1, &texture.id);
285                 }
286                 texture.image(image, 0, srgb_conversion, true);
287         }
288
289         ++phase;
290         return phase>3;
291 }
292
293 } // namespace GL
294 } // namespace Msp