]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Use ARB_texture_storage for defining texture storage
[libs/gl.git] / source / texture2d.cpp
1 #include <msp/gl/extensions/arb_texture_storage.h>
2 #include "bindable.h"
3 #include "buffer.h"
4 #include "error.h"
5 #include "pixelstore.h"
6 #include "resources.h"
7 #include "texture2d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 class Texture2D::AsyncLoader: public Resource::AsyncLoader
15 {
16 private:
17         Texture2D &texture;
18         IO::Seekable &io;
19         bool srgb_conversion;
20         Buffer pixel_buffer;
21         char *mapped_address;
22         Graphics::Image image;
23         unsigned n_bytes;
24         int phase;
25
26 public:
27         AsyncLoader(Texture2D &, IO::Seekable &);
28
29         void set_srgb_conversion(bool);
30         virtual bool needs_sync() const;
31         virtual bool process();
32 };
33
34
35 Texture2D::Texture2D(ResourceManager *m):
36         Texture(GL_TEXTURE_2D, m),
37         ifmt(RGB),
38         width(0),
39         height(0),
40         allocated(0)
41 { }
42
43 Texture2D::~Texture2D()
44 {
45         set_manager(0);
46 }
47
48 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
49 {
50         if(width>0)
51                 throw invalid_operation("Texture2D::storage");
52         if(wd==0 || ht==0)
53                 throw invalid_argument("Texture2D::storage");
54
55         if(MSP_sized_internal_formats)
56                 fmt = get_sized_pixelformat(fmt);
57         require_pixelformat(fmt);
58
59         ifmt = fmt;
60         width = wd;
61         height = ht;
62 }
63
64 void Texture2D::allocate(unsigned level)
65 {
66         if(allocated&(1<<level))
67                 return;
68
69         if(ARB_texture_storage)
70         {
71                 BindRestore _bind(this);
72                 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
73                 glTexStorage2D(target, n_levels, ifmt, width, height);
74                 allocated |= (1<<n_levels)-1;
75         }
76         else
77         {
78                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
79                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
80         }
81 }
82
83 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
84 {
85         if(width==0 || height==0)
86                 throw invalid_operation("Texture2D::image");
87
88         unsigned w = width;
89         unsigned h = height;
90         get_level_size(level, w, h);
91
92         BindRestore _bind(this);
93         if(ARB_texture_storage)
94                 sub_image(level, 0, 0, w, h, fmt, type, data);
95         else
96                 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
97
98         allocated |= 1<<level;
99         if(gen_mipmap && level==0)
100         {
101                 auto_generate_mipmap();
102                 allocated |= (1<<get_n_levels())-1;
103         }
104 }
105
106 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
107 {
108         if(width==0 || height==0)
109                 throw invalid_operation("Texture2D::sub_image");
110
111         allocate(level);
112
113         BindRestore _bind(this);
114         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
115 }
116
117 void Texture2D::image(const Graphics::Image &img, bool srgb)
118 {
119         image(img, srgb, false);
120 }
121
122 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
123 {
124         unsigned w = img.get_width();
125         unsigned h = img.get_height();
126         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
127         if(width==0)
128                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
129         else if(w!=width || h!=height)
130                 throw incompatible_data("Texture2D::image");
131
132         PixelStore pstore = PixelStore::from_image(img);
133         BindRestore _bind_ps(pstore);
134
135         image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
136 }
137
138 unsigned Texture2D::get_n_levels() const
139 {
140         unsigned n = 0;
141         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
142         return n;
143 }
144
145 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
146 {
147         w >>= level;
148         h >>= level;
149
150         if(!w && h)
151                 w = 1;
152         else if(!h && w)
153                 h = 1;
154 }
155
156 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
157 {
158         AsyncLoader *ldr = new AsyncLoader(*this, io);
159         if(res)
160                 ldr->set_srgb_conversion(res->get_srgb_conversion());
161         return ldr;
162 }
163
164 UInt64 Texture2D::get_data_size() const
165 {
166         return id ? width*height*get_component_count(ifmt) : 0;
167 }
168
169 void Texture2D::unload()
170 {
171         glDeleteTextures(1, &id);
172         id = 0;
173         // TODO check which params actually need refreshing
174         dirty_params = -1;
175 }
176
177
178 Texture2D::Loader::Loader(Texture2D &t):
179         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
180 {
181         init();
182 }
183
184 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
185         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
186 {
187         init();
188 }
189
190 void Texture2D::Loader::init()
191 {
192         add("raw_data", &Loader::raw_data);
193         add("storage", &Loader::storage);
194 }
195
196 void Texture2D::Loader::raw_data(const string &data)
197 {
198         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
199 }
200
201 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
202 {
203         if(srgb)
204                 fmt = get_srgb_pixelformat(fmt);
205         obj.storage(fmt, w, h);
206 }
207
208
209 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
210         texture(t),
211         io(i),
212         srgb_conversion(false),
213         pixel_buffer(PIXEL_UNPACK_BUFFER),
214         mapped_address(0),
215         phase(0)
216 { }
217
218 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
219 {
220         srgb_conversion = c;
221 }
222
223 bool Texture2D::AsyncLoader::needs_sync() const
224 {
225         return phase%2;
226 }
227
228 bool Texture2D::AsyncLoader::process()
229 {
230         if(phase==0)
231         {
232                 /* TODO Enhance the ImageLoader system so that the image can be loaded
233                 directly to the buffer */
234                 image.load_io(io);
235                 n_bytes = image.get_stride()*image.get_height();
236         }
237         else if(phase==1)
238         {
239                 pixel_buffer.data(n_bytes, 0);
240                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
241         }
242         else if(phase==2)
243         {
244                 const char *data = reinterpret_cast<const char *>(image.get_data());
245                 copy(data, data+n_bytes, mapped_address);
246         }
247         else if(phase==3)
248         {
249                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
250                 if(!pixel_buffer.unmap())
251                 {
252                         phase = 1;
253                         return false;
254                 }
255
256                 if(!texture.id)
257                         glGenTextures(1, &texture.id);
258                 texture.image(image, srgb_conversion, true);
259         }
260
261         ++phase;
262         return phase>3;
263 }
264
265 } // namespace GL
266 } // namespace Msp