]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Use ARB_direct_state_access to avoid some bind calls
[libs/gl.git] / source / texture3d.cpp
1 #include <cmath>
2 #include <msp/core/raii.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_texture_storage.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/graphics/image.h>
7 #include "bindable.h"
8 #include "error.h"
9 #include "pixelstore.h"
10 #include "texture3d.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Texture3D::Texture3D(GLenum t):
18         Texture(t),
19         ifmt(RGB),
20         width(0),
21         height(0),
22         depth(0),
23         allocated(0)
24 { }
25
26 Texture3D::Texture3D():
27         Texture(GL_TEXTURE_3D),
28         ifmt(RGB),
29         width(0),
30         height(0),
31         depth(0),
32         allocated(0)
33 {
34         static Require _req(EXT_texture3D);
35 }
36
37 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
38 {
39         if(width>0)
40                 throw invalid_operation("Texture3D::storage");
41         if(wd==0 || ht==0 || dp==0)
42                 throw invalid_argument("Texture3D::storage");
43
44         if(MSP_sized_internal_formats)
45                 fmt = get_sized_pixelformat(fmt);
46         require_pixelformat(fmt);
47
48         ifmt = fmt;
49         width = wd;
50         height = ht;
51         depth = dp;
52 }
53
54 void Texture3D::allocate(unsigned level)
55 {
56         if(allocated&(1<<level))
57                 return;
58
59         if(ARB_texture_storage)
60         {
61                 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
62                 if(ARB_direct_state_access)
63                         glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
64                 else
65                 {
66                         BindRestore _bind(this);
67                         glTexStorage3D(target, n_levels, ifmt, width, height, depth);
68                 }
69                 allocated |= (1<<n_levels)-1;
70         }
71         else
72         {
73                 PixelFormat base_fmt = get_base_pixelformat(ifmt);
74                 image(level, base_fmt, get_alloc_type(base_fmt), 0);
75         }
76 }
77
78 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
79 {
80         if(width==0 || height==0 || depth==0)
81                 throw invalid_operation("Texture3D::image");
82
83         unsigned w = width;
84         unsigned h = height;
85         unsigned d = depth;
86         get_level_size(level, w, h, d);
87
88         if(ARB_texture_storage)
89                 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
90
91         BindRestore _bind(this);
92         glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
93
94         allocated |= 1<<level;
95         if(gen_mipmap && level==0)
96         {
97                 auto_generate_mipmap();
98                 allocated |= (1<<get_n_levels())-1;
99         }
100 }
101
102 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
103 {
104         if(width==0 || height==0 || depth==0)
105                 throw invalid_operation("Texture3D::image");
106
107         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
108         allocate(level);
109
110         if(ARB_direct_state_access)
111                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
112         else
113                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
114
115         if(gen_mipmap && level==0)
116                 auto_generate_mipmap();
117 }
118
119 void Texture3D::load_image(const string &fn, int dp)
120 {
121         Graphics::Image img;
122         img.load_file(fn);
123
124         unsigned w = img.get_width();
125         unsigned h = img.get_height();
126         unsigned d = 1;
127
128         if(dp==-1)
129         {
130                 if(h%w)
131                         throw incompatible_data("Texture3D::load_image");
132                 d = h/w;
133                 h = w;
134         }
135         else if(dp==-2)
136         {
137                 for(d=h; d*d>h; d>>=2) ;
138                 for(; d*d<h; ++d) ;
139                 if(d*d!=h)
140                         throw incompatible_data("Texture3D::load_image");
141                 h = d;
142         }
143         else if(dp>0)
144         {
145                 d = dp;
146                 if(h%d)
147                         throw incompatible_data("Texture3D::load_image");
148                 h /= d;
149         }
150         else
151                 throw invalid_argument("Texture3D::load_image");
152
153         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
154         if(width==0)
155                 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
156         else if(w!=width || h!=height || d!=depth)
157                 throw incompatible_data("Texture3D::load_image");
158
159         PixelStore pstore = PixelStore::from_image(img);
160         BindRestore _bind_ps(pstore);
161
162         image(0, fmt, UNSIGNED_BYTE, img.get_data());
163 }
164
165 void Texture3D::image(const Graphics::Image &img, bool srgb)
166 {
167         unsigned w = img.get_width();
168         unsigned h = img.get_height();
169         unsigned d = 1;
170
171         if(depth)
172         {
173                 if(h%depth)
174                         throw incompatible_data("Texture3D::load_image");
175                 h /= depth;
176                 d = depth;
177         }
178         else
179         {
180                 if(h%w)
181                         throw incompatible_data("Texture3D::load_image");
182                 d = h/w;
183                 h = w;
184         }
185
186         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
187         if(width==0)
188                 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
189         else if(w!=width || h!=height || d!=depth)
190                 throw incompatible_data("Texture3D::load_image");
191
192         PixelStore pstore = PixelStore::from_image(img);
193         BindRestore _bind_ps(pstore);
194
195         image(0, fmt, UNSIGNED_BYTE, img.get_data());
196 }
197
198 unsigned Texture3D::get_n_levels() const
199 {
200         unsigned s = max(width, height);
201         if(target!=GL_TEXTURE_2D_ARRAY)
202                 s = max(s, depth);
203         unsigned n = 0;
204         for(; s; s>>=1, ++n) ;
205         return n;
206 }
207
208 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
209 {
210         w >>= level;
211         h >>= level;
212         if(target!=GL_TEXTURE_2D_ARRAY)
213                 d >>= level;
214
215         if(!w && (h || d))
216                 w = 1;
217         if(!h && (w || d))
218                 h = 1;
219         if(!d && (w || h))
220                 d = 1;
221 }
222
223 UInt64 Texture3D::get_data_size() const
224 {
225         return id ? width*height*depth*get_pixel_size(ifmt) : 0;
226 }
227
228
229 Texture3D::Loader::Loader(Texture3D &t):
230         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
231 {
232         init();
233 }
234
235 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
236         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
237 {
238         init();
239 }
240
241 void Texture3D::Loader::init()
242 {
243         add("raw_data", &Loader::raw_data);
244         add("storage", &Loader::storage);
245 }
246
247 void Texture3D::Loader::raw_data(const string &data)
248 {
249         obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
250 }
251
252 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
253 {
254         if(srgb)
255                 fmt = get_srgb_pixelformat(fmt);
256         obj.storage(fmt, w, h, d);
257 }
258
259 } // namespace GL
260 } // namespace Msp