]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Separate abstract pixel compositions from concrete pixel formats
[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                 image(level, get_components(ifmt), get_component_type(ifmt), 0);
85 }
86
87 void Texture2D::image(unsigned level, PixelComponents comp, DataType type, 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, comp, type, 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         glTexImage2D(target, level, ifmt, w, h, 0, get_upload_components(comp), type, data);
107
108         allocated |= 1<<level;
109         if(auto_gen_mipmap && level==0)
110         {
111                 generate_mipmap();
112                 allocated |= (1<<levels)-1;
113         }
114 }
115
116 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
117 {
118         if(width==0 || height==0)
119                 throw invalid_operation("Texture2D::sub_image");
120
121         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
122         allocate(level);
123
124         comp = get_upload_components(comp);
125         if(ARB_direct_state_access)
126                 glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
127         else
128                 glTexSubImage2D(target, level, x, y, wd, ht, comp, type, data);
129
130         if(auto_gen_mipmap && level==0)
131                 generate_mipmap();
132 }
133
134 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
135 {
136         image(img, lv, srgb, false);
137 }
138
139 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
140 {
141         unsigned w = img.get_width();
142         unsigned h = img.get_height();
143         PixelFormat fmt = pixelformat_from_image(img);
144         PixelComponents comp = get_components(fmt);
145         DataType type = get_component_type(fmt);
146         if(width==0)
147                 storage(make_pixelformat(comp, type, srgb), w, h, lv);
148         else if(w!=width || h!=height || (lv && lv!=levels))
149                 throw incompatible_data("Texture2D::image");
150
151         PixelStore pstore = PixelStore::from_image(img);
152         BindRestore _bind_ps(pstore);
153
154         image(0, comp, type, from_buffer ? 0 : img.get_data());
155 }
156
157 unsigned Texture2D::get_n_levels() const
158 {
159         unsigned n = 0;
160         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
161         return n;
162 }
163
164 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
165 {
166         w >>= level;
167         h >>= level;
168
169         if(!w && h)
170                 w = 1;
171         else if(!h && w)
172                 h = 1;
173 }
174
175 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
176 {
177         AsyncLoader *ldr = new AsyncLoader(*this, io);
178         if(res)
179                 ldr->set_srgb_conversion(res->get_srgb_conversion());
180         return ldr;
181 }
182
183 UInt64 Texture2D::get_data_size() const
184 {
185         return id ? width*height*get_pixel_size(ifmt) : 0;
186 }
187
188 void Texture2D::unload()
189 {
190         glDeleteTextures(1, &id);
191         id = 0;
192         allocated = 0;
193         default_sampler.unload();
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_components(obj.ifmt), get_component_type(obj.ifmt), 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