]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Rework PixelComponents and PixelFormat to use custom values
[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 "error.h"
9 #include "texture3d.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Texture3D::Texture3D(GLenum t):
17         Texture(t),
18         width(0),
19         height(0),
20         depth(0),
21         allocated(0)
22 { }
23
24 Texture3D::Texture3D():
25         Texture(GL_TEXTURE_3D),
26         width(0),
27         height(0),
28         depth(0),
29         allocated(0)
30 {
31         static Require _req(EXT_texture3D);
32 }
33
34 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
35 {
36         if(width>0)
37         {
38                 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
39                         throw incompatible_data("Texture3D::storage");
40                 return;
41         }
42         if(wd==0 || ht==0 || dp==0)
43                 throw invalid_argument("Texture3D::storage");
44
45         set_format(fmt);
46         width = wd;
47         height = ht;
48         depth = dp;
49         levels = get_n_levels();
50         if(lv>0)
51                 levels = min(levels, lv);
52 }
53
54 void Texture3D::allocate(unsigned level)
55 {
56         if(width==0 || height==0 || depth==0)
57                 throw invalid_operation("Texture3D::allocate");
58         if(level>=levels)
59                 throw invalid_argument("Texture3D::allocate");
60         if(allocated&(1<<level))
61                 return;
62
63         bool direct = ARB_texture_storage && ARB_direct_state_access;
64         if(!direct)
65         {
66                 glActiveTexture(GL_TEXTURE0);
67                 glBindTexture(target, id);
68         }
69
70         allocate_(level);
71
72         if(!direct)
73                 glBindTexture(target, 0);
74 }
75
76 void Texture3D::allocate_(unsigned level)
77 {
78         if(ARB_texture_storage)
79         {
80                 GLenum fmt = get_gl_pixelformat(storage_fmt);
81                 if(ARB_direct_state_access)
82                         glTextureStorage3D(id, levels, fmt, width, height, depth);
83                 else
84                         glTexStorage3D(target, levels, 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         GLenum fmt = get_gl_pixelformat(storage_fmt);
129         GLenum comp = get_gl_components(get_components(storage_fmt));
130         GLenum type = get_gl_type(get_component_type(storage_fmt));
131         glTexImage3D(target, level, fmt, size.x, size.y, size.z, 0, comp, type, data);
132
133         allocated |= 1<<level;
134 }
135
136 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
137 {
138         if(comp!=get_components(format) || type!=get_component_type(format))
139                 throw incompatible_data("Texture3D::image");
140         image(level, data);
141 }
142
143 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
144 {
145         if(width==0 || height==0 || depth==0)
146                 throw invalid_operation("Texture3D::sub_image");
147         if(level>=levels)
148                 throw out_of_range("Texture3D::sub_image");
149
150         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
151         if(!direct)
152         {
153                 glActiveTexture(GL_TEXTURE0);
154                 glBindTexture(target, id);
155         }
156
157         allocate_(level);
158
159         GLenum comp = get_gl_components(get_components(storage_fmt));
160         GLenum type = get_gl_type(get_component_type(storage_fmt));
161         if(ARB_direct_state_access)
162                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
163         else
164                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
165
166         if(auto_gen_mipmap && level==0)
167                 generate_mipmap_();
168
169         if(!direct)
170                 glBindTexture(target, 0);
171 }
172
173 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)
174 {
175         if(comp!=get_components(format) || type!=get_component_type(format))
176                 throw incompatible_data("Texture3D::sub_image");
177         sub_image(level, x, y, z, wd, ht, dp, data);
178 }
179
180 void Texture3D::image(const Graphics::Image &img, unsigned lv)
181 {
182         unsigned w = img.get_width();
183         unsigned h = img.get_height();
184
185         if(h%w)
186                 throw incompatible_data("Texture3D::load_image");
187         unsigned d = h/w;
188         h = w;
189
190         PixelFormat fmt = pixelformat_from_image(img);
191         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
192
193         image(0, img.get_pixels());
194 }
195
196 unsigned Texture3D::get_n_levels() const
197 {
198         unsigned s = max(width, height);
199         if(target!=GL_TEXTURE_2D_ARRAY)
200                 s = max(s, depth);
201         unsigned n = 0;
202         for(; s; s>>=1, ++n) ;
203         return n;
204 }
205
206 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
207 {
208         unsigned w = width>>level;
209         unsigned h = height>>level;
210         unsigned d = depth;
211         if(target!=GL_TEXTURE_2D_ARRAY)
212                 d >>= level;
213
214         if(!w && (h || d))
215                 w = 1;
216         if(!h && (w || d))
217                 h = 1;
218         if(!d && (w || h))
219                 d = 1;
220
221         return LinAl::Vector<unsigned, 3>(w, h, d);
222 }
223
224 UInt64 Texture3D::get_data_size() const
225 {
226         return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
227 }
228
229
230 Texture3D::Loader::Loader(Texture3D &t):
231         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
232 {
233         init();
234 }
235
236 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
237         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
238 {
239         init();
240 }
241
242 void Texture3D::Loader::init()
243 {
244         add("raw_data", &Loader::raw_data);
245         add("storage", &Loader::storage);
246         add("storage", &Loader::storage_levels);
247 }
248
249 void Texture3D::Loader::raw_data(const string &data)
250 {
251         obj.image(0, data.data());
252 }
253
254 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
255 {
256         obj.storage(fmt, w, h, d);
257 }
258
259 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
260 {
261         obj.storage(fmt, w, h, d, l);
262 }
263
264 } // namespace GL
265 } // namespace Msp