]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Add another constructor to RenderTarget to resolve an ambiguity
[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         glTexImage2D(target, level, ifmt, w, h, 0, get_upload_format(fmt), type, data);
105
106         allocated |= 1<<level;
107         if(auto_gen_mipmap && level==0)
108         {
109                 generate_mipmap();
110                 allocated |= (1<<get_n_levels())-1;
111         }
112 }
113
114 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
115 {
116         if(width==0 || height==0)
117                 throw invalid_operation("Texture2D::sub_image");
118
119         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
120         allocate(level);
121
122         fmt = get_upload_format(fmt);
123         if(ARB_direct_state_access)
124                 glTextureSubImage2D(id, level, x, y, wd, ht, fmt, type, data);
125         else
126                 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
127
128         if(auto_gen_mipmap && level==0)
129                 generate_mipmap();
130 }
131
132 void Texture2D::image(const Graphics::Image &img, bool srgb)
133 {
134         image(img, srgb, false);
135 }
136
137 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
138 {
139         unsigned w = img.get_width();
140         unsigned h = img.get_height();
141         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
142         if(width==0)
143         {
144                 unsigned l = (is_mipmapped(min_filter) ? mipmap_levels ? mipmap_levels : 0 : 1);
145                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, l);
146         }
147         else if(w!=width || h!=height)
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         // TODO check which params actually need refreshing
192         dirty_params = -1;
193 }
194
195
196 Texture2D::Loader::Loader(Texture2D &t):
197         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
198 {
199         init();
200 }
201
202 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
203         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
204 {
205         init();
206 }
207
208 void Texture2D::Loader::init()
209 {
210         add("raw_data", &Loader::raw_data);
211         add("storage", &Loader::storage);
212 }
213
214 void Texture2D::Loader::raw_data(const string &data)
215 {
216         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
217 }
218
219 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
220 {
221         obj.storage(fmt, w, h);
222 }
223
224
225 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
226         texture(t),
227         io(i),
228         srgb_conversion(false),
229         pixel_buffer(PIXEL_UNPACK_BUFFER),
230         mapped_address(0),
231         phase(0)
232 { }
233
234 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
235 {
236         srgb_conversion = c;
237 }
238
239 bool Texture2D::AsyncLoader::needs_sync() const
240 {
241         return phase%2;
242 }
243
244 bool Texture2D::AsyncLoader::process()
245 {
246         if(phase==0)
247         {
248                 /* TODO Enhance the ImageLoader system so that the image can be loaded
249                 directly to the buffer */
250                 image.load_io(io);
251                 n_bytes = image.get_stride()*image.get_height();
252         }
253         else if(phase==1)
254         {
255                 pixel_buffer.storage(n_bytes);
256                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
257         }
258         else if(phase==2)
259         {
260                 const char *data = reinterpret_cast<const char *>(image.get_data());
261                 copy(data, data+n_bytes, mapped_address);
262         }
263         else if(phase==3)
264         {
265                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
266                 if(!pixel_buffer.unmap())
267                 {
268                         phase = 1;
269                         return false;
270                 }
271
272                 if(!texture.id)
273                         glGenTextures(1, &texture.id);
274                 texture.image(image, srgb_conversion, true);
275         }
276
277         ++phase;
278         return phase>3;
279 }
280
281 } // namespace GL
282 } // namespace Msp