]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Add some utility functions for textures
[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
54         if(MSP_sized_internal_formats)
55                 fmt = get_sized_pixelformat(fmt);
56         require_pixelformat(fmt);
57
58         ifmt = fmt;
59         width = wd;
60         height = ht;
61 }
62
63 void Texture2D::allocate(unsigned level)
64 {
65         if(allocated&(1<<level))
66                 return;
67
68         PixelFormat base_fmt = get_base_pixelformat(ifmt);
69         image(level, base_fmt, get_alloc_type(base_fmt), 0);
70 }
71
72 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
73 {
74         if(width==0 || height==0)
75                 throw invalid_operation("Texture2D::image");
76
77         unsigned w = width;
78         unsigned h = height;
79         get_level_size(level, w, h);
80
81         BindRestore _bind(this);
82         glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
83
84         allocated |= 1<<level;
85         if(gen_mipmap && level==0)
86         {
87                 auto_generate_mipmap();
88                 allocated |= (1<<get_n_levels())-1;
89         }
90 }
91
92 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
93 {
94         if(width==0 || height==0)
95                 throw invalid_operation("Texture2D::sub_image");
96
97         allocate(level);
98
99         BindRestore _bind(this);
100         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
101 }
102
103 void Texture2D::image(const Graphics::Image &img, bool srgb)
104 {
105         image(img, srgb, false);
106 }
107
108 void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
109 {
110         unsigned w = img.get_width();
111         unsigned h = img.get_height();
112         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
113         if(width==0)
114                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
115         else if(w!=width || h!=height)
116                 throw incompatible_data("Texture2D::image");
117
118         PixelStore pstore = PixelStore::from_image(img);
119         BindRestore _bind_ps(pstore);
120
121         image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
122 }
123
124 unsigned Texture2D::get_n_levels() const
125 {
126         unsigned n = 0;
127         for(unsigned s=max(width, height); s; s>>=1, ++n) ;
128         return n;
129 }
130
131 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
132 {
133         w >>= level;
134         h >>= level;
135
136         if(!w && h)
137                 w = 1;
138         else if(!h && w)
139                 h = 1;
140 }
141
142 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
143 {
144         AsyncLoader *ldr = new AsyncLoader(*this, io);
145         if(res)
146                 ldr->set_srgb_conversion(res->get_srgb_conversion());
147         return ldr;
148 }
149
150 UInt64 Texture2D::get_data_size() const
151 {
152         return id ? width*height*get_component_count(ifmt) : 0;
153 }
154
155 void Texture2D::unload()
156 {
157         glDeleteTextures(1, &id);
158         id = 0;
159         // TODO check which params actually need refreshing
160         dirty_params = -1;
161 }
162
163
164 Texture2D::Loader::Loader(Texture2D &t):
165         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
166 {
167         init();
168 }
169
170 Texture2D::Loader::Loader(Texture2D &t, Collection &c):
171         DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t, c)
172 {
173         init();
174 }
175
176 void Texture2D::Loader::init()
177 {
178         add("raw_data", &Loader::raw_data);
179         add("storage", &Loader::storage);
180 }
181
182 void Texture2D::Loader::raw_data(const string &data)
183 {
184         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
185 }
186
187 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
188 {
189         if(srgb)
190                 fmt = get_srgb_pixelformat(fmt);
191         obj.storage(fmt, w, h);
192 }
193
194
195 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
196         texture(t),
197         io(i),
198         srgb_conversion(false),
199         pixel_buffer(PIXEL_UNPACK_BUFFER),
200         mapped_address(0),
201         phase(0)
202 { }
203
204 void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
205 {
206         srgb_conversion = c;
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                 if(!texture.id)
243                         glGenTextures(1, &texture.id);
244                 texture.image(image, srgb_conversion, true);
245         }
246
247         ++phase;
248         return phase>3;
249 }
250
251 } // namespace GL
252 } // namespace Msp