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