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