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