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