]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Implement loading functionality for texture classes that were missing it
[libs/gl.git] / source / texture2d.cpp
1 #include "bindable.h"
2 #include "buffer.h"
3 #include "error.h"
4 #include "pixelstore.h"
5 #include "resources.h"
6 #include "texture2d.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 class Texture2D::AsyncLoader: public Resource::AsyncLoader
14 {
15 private:
16         Texture2D &texture;
17         IO::Seekable &io;
18         bool srgb_conversion;
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         void set_srgb_conversion(bool);
29         virtual bool needs_sync() const;
30         virtual bool process();
31 };
32
33
34 Texture2D::Texture2D(ResourceManager *m):
35         Texture(GL_TEXTURE_2D, m),
36         ifmt(RGB),
37         width(0),
38         height(0),
39         allocated(0)
40 { }
41
42 Texture2D::~Texture2D()
43 {
44         set_manager(0);
45 }
46
47 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
48 {
49         if(width>0)
50                 throw invalid_operation("Texture2D::storage");
51         if(wd==0 || ht==0)
52                 throw invalid_argument("Texture2D::storage");
53         require_pixelformat(fmt);
54
55         ifmt = fmt;
56         width = wd;
57         height = ht;
58 }
59
60 void Texture2D::allocate(unsigned level)
61 {
62         if(allocated&(1<<level))
63                 return;
64
65         PixelFormat base_fmt = get_base_pixelformat(ifmt);
66         image(level, base_fmt, get_alloc_type(base_fmt), 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::image(const Graphics::Image &img, bool srgb)
102 {
103         image(img, srgb, false);
104 }
105
106 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
107 {
108         unsigned w = img.get_width();
109         unsigned h = img.get_height();
110         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
111         if(width==0)
112                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
113         else if(w!=width || h!=height)
114                 throw incompatible_data("Texture2D::image");
115
116         PixelStore pstore = PixelStore::from_image(img);
117         BindRestore _bind_ps(pstore);
118
119         image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
120 }
121
122 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
123 {
124         w >>= level;
125         h >>= level;
126
127         if(!w && h)
128                 w = 1;
129         else if(!h && w)
130                 h = 1;
131 }
132
133 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
134 {
135         AsyncLoader *ldr = new AsyncLoader(*this, io);
136         if(res)
137                 ldr->set_srgb_conversion(res->get_srgb_conversion());
138         return ldr;
139 }
140
141 UInt64 Texture2D::get_data_size() const
142 {
143         return id ? width*height*get_component_count(ifmt) : 0;
144 }
145
146 void Texture2D::unload()
147 {
148         glDeleteTextures(1, &id);
149         id = 0;
150         // TODO check which params actually need refreshing
151         dirty_params = -1;
152 }
153
154
155 Texture2D::Loader::Loader(Texture2D &t):
156         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
157 {
158         init();
159 }
160
161 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
162         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
163 {
164         init();
165 }
166
167 void Texture2D::Loader::init()
168 {
169         add("raw_data", &Loader::raw_data);
170         add("storage", &Loader::storage);
171         add("storage", &Loader::storage_b);
172 }
173
174 void Texture2D::Loader::raw_data(const string &data)
175 {
176         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
177 }
178
179 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
180 {
181         if(srgb)
182                 fmt = get_srgb_pixelformat(fmt);
183         obj.storage(fmt, w, h);
184 }
185
186 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
187 {
188         storage(fmt, w, h);
189 }
190
191
192 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
193         texture(t),
194         io(i),
195         srgb_conversion(false),
196         pixel_buffer(PIXEL_UNPACK_BUFFER),
197         mapped_address(0),
198         phase(0)
199 { }
200
201 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
202 {
203         srgb_conversion = c;
204 }
205
206 bool Texture2D::AsyncLoader::needs_sync() const
207 {
208         return phase%2;
209 }
210
211 bool Texture2D::AsyncLoader::process()
212 {
213         if(phase==0)
214         {
215                 /* TODO Enhance the ImageLoader system so that the image can be loaded
216                 directly to the buffer */
217                 image.load_io(io);
218                 n_bytes = image.get_stride()*image.get_height();
219         }
220         else if(phase==1)
221         {
222                 pixel_buffer.data(n_bytes, 0);
223                 mapped_address = reinterpret_cast<char *>(pixel_buffer.map(WRITE_ONLY));
224         }
225         else if(phase==2)
226         {
227                 const char *data = reinterpret_cast<const char *>(image.get_data());
228                 copy(data, data+n_bytes, mapped_address);
229         }
230         else if(phase==3)
231         {
232                 Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
233                 if(!pixel_buffer.unmap())
234                 {
235                         phase = 1;
236                         return false;
237                 }
238
239                 if(!texture.id)
240                         glGenTextures(1, &texture.id);
241                 texture.image(image, srgb_conversion, true);
242         }
243
244         ++phase;
245         return phase>3;
246 }
247
248 } // namespace GL
249 } // namespace Msp