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