]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Remove the PixelStore class
[libs/gl.git] / source / core / 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/gl/extensions/ext_texture_array.h>
7 #include <msp/graphics/image.h>
8 #include "bindable.h"
9 #include "error.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         width(0),
20         height(0),
21         depth(0),
22         allocated(0)
23 { }
24
25 Texture3D::Texture3D():
26         Texture(GL_TEXTURE_3D),
27         width(0),
28         height(0),
29         depth(0),
30         allocated(0)
31 {
32         static Require _req(EXT_texture3D);
33 }
34
35 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
36 {
37         if(width>0)
38         {
39                 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
40                         throw incompatible_data("Texture3D::storage");
41                 return;
42         }
43         if(wd==0 || ht==0 || dp==0)
44                 throw invalid_argument("Texture3D::storage");
45
46         set_format(fmt);
47         width = wd;
48         height = ht;
49         depth = dp;
50         levels = get_n_levels();
51         if(lv>0)
52                 levels = min(levels, lv);
53 }
54
55 void Texture3D::allocate(unsigned level)
56 {
57         if(width==0 || height==0 || depth==0)
58                 throw invalid_operation("Texture3D::allocate");
59         if(level>=levels)
60                 throw invalid_argument("Texture3D::allocate");
61         if(allocated&(1<<level))
62                 return;
63
64         bool direct = ARB_texture_storage && ARB_direct_state_access;
65         if(!direct)
66         {
67                 glActiveTexture(GL_TEXTURE0);
68                 glBindTexture(target, id);
69         }
70
71         allocate_(level);
72
73         if(!direct)
74                 glBindTexture(target, 0);
75 }
76
77 void Texture3D::allocate_(unsigned level)
78 {
79         if(ARB_texture_storage)
80         {
81                 if(ARB_direct_state_access)
82                         glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
83                 else
84                         glTexStorage3D(target, levels, storage_fmt, width, height, depth);
85                 apply_swizzle();
86                 allocated |= (1<<levels)-1;
87         }
88         else
89                 image_(level, 0);
90 }
91
92 void Texture3D::image(unsigned level, const void *data)
93 {
94         if(width==0 || height==0 || depth==0)
95                 throw invalid_operation("Texture3D::image");
96         if(level>=levels)
97                 throw out_of_range("Texture3D::image");
98
99         if(ARB_texture_storage)
100         {
101                 LinAl::Vector<unsigned, 3> size = get_level_size(level);
102                 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
103         }
104
105         glActiveTexture(GL_TEXTURE0);
106         glBindTexture(target, id);
107
108         image_(level, data);
109
110         if(auto_gen_mipmap && level==0)
111         {
112                 generate_mipmap_();
113                 allocated |= (1<<levels)-1;
114         }
115
116         glBindTexture(target, 0);
117 }
118
119 void Texture3D::image_(unsigned level, const void *data)
120 {
121         if(!allocated)
122         {
123                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
124                 apply_swizzle();
125         }
126
127         LinAl::Vector<unsigned, 3> size = get_level_size(level);
128         PixelComponents comp = get_components(storage_fmt);
129         GLenum type = get_gl_type(get_component_type(storage_fmt));
130         glTexImage3D(target, level, storage_fmt, size.x, size.y, size.z, 0, comp, type, data);
131
132         allocated |= 1<<level;
133 }
134
135 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
136 {
137         if(comp!=get_components(format) || type!=get_component_type(format))
138                 throw incompatible_data("Texture3D::image");
139         image(level, data);
140 }
141
142 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
143 {
144         if(width==0 || height==0 || depth==0)
145                 throw invalid_operation("Texture3D::sub_image");
146         if(level>=levels)
147                 throw out_of_range("Texture3D::sub_image");
148
149         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
150         if(!direct)
151         {
152                 glActiveTexture(GL_TEXTURE0);
153                 glBindTexture(target, id);
154         }
155
156         allocate_(level);
157
158         PixelComponents comp = get_components(storage_fmt);
159         GLenum type = get_gl_type(get_component_type(storage_fmt));
160         if(ARB_direct_state_access)
161                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
162         else
163                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
164
165         if(auto_gen_mipmap && level==0)
166                 generate_mipmap_();
167
168         if(!direct)
169                 glBindTexture(target, 0);
170 }
171
172 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelComponents comp, DataType type, const void *data)
173 {
174         if(comp!=get_components(format) || type!=get_component_type(format))
175                 throw incompatible_data("Texture3D::sub_image");
176         sub_image(level, x, y, z, wd, ht, dp, data);
177 }
178
179 void Texture3D::image(const Graphics::Image &img, unsigned lv)
180 {
181         unsigned w = img.get_width();
182         unsigned h = img.get_height();
183
184         if(h%w)
185                 throw incompatible_data("Texture3D::load_image");
186         unsigned d = h/w;
187         h = w;
188
189         PixelFormat fmt = pixelformat_from_image(img);
190         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
191
192         image(0, img.get_pixels());
193 }
194
195 unsigned Texture3D::get_n_levels() const
196 {
197         unsigned s = max(width, height);
198         if(target!=GL_TEXTURE_2D_ARRAY)
199                 s = max(s, depth);
200         unsigned n = 0;
201         for(; s; s>>=1, ++n) ;
202         return n;
203 }
204
205 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
206 {
207         unsigned w = width>>level;
208         unsigned h = height>>level;
209         unsigned d = depth;
210         if(target!=GL_TEXTURE_2D_ARRAY)
211                 d >>= level;
212
213         if(!w && (h || d))
214                 w = 1;
215         if(!h && (w || d))
216                 h = 1;
217         if(!d && (w || h))
218                 d = 1;
219
220         return LinAl::Vector<unsigned, 3>(w, h, d);
221 }
222
223 UInt64 Texture3D::get_data_size() const
224 {
225         return id ? width*height*depth*get_pixel_size(storage_fmt) : 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         add("storage", &Loader::storage_levels);
246 }
247
248 void Texture3D::Loader::raw_data(const string &data)
249 {
250         obj.image(0, data.data());
251 }
252
253 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
254 {
255         obj.storage(fmt, w, h, d);
256 }
257
258 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
259 {
260         obj.storage(fmt, w, h, d, l);
261 }
262
263 } // namespace GL
264 } // namespace Msp