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