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