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