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