]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
f6268d697d6d60c00735c6230e6ff6c701eb8b7c
[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 "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         {
40                 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
41                         throw incompatible_data("Texture3D::storage");
42                 return;
43         }
44         if(wd==0 || ht==0 || dp==0)
45                 throw invalid_argument("Texture3D::storage");
46
47         set_format(fmt);
48         width = wd;
49         height = ht;
50         depth = dp;
51         levels = get_n_levels();
52         if(lv>0)
53                 levels = min(levels, lv);
54 }
55
56 void Texture3D::allocate(unsigned level)
57 {
58         if(width==0 || height==0 || depth==0)
59                 throw invalid_operation("Texture3D::allocate");
60         if(level>=levels)
61                 throw invalid_argument("Texture3D::allocate");
62         if(allocated&(1<<level))
63                 return;
64
65         if(ARB_texture_storage)
66         {
67                 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
68                 if(ARB_direct_state_access)
69                         glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
70                 else
71                         glTexStorage3D(target, levels, storage_fmt, width, height, depth);
72                 apply_swizzle();
73                 allocated |= (1<<levels)-1;
74         }
75         else
76                 image(level, 0);
77 }
78
79 void Texture3D::image(unsigned level, const void *data)
80 {
81         if(width==0 || height==0 || depth==0)
82                 throw invalid_operation("Texture3D::image");
83         if(level>=levels)
84                 throw out_of_range("Texture3D::image");
85
86         LinAl::Vector<unsigned, 3> size = get_level_size(level);
87
88         if(ARB_texture_storage)
89                 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
90
91         BindRestore _bind(this);
92
93         if(!allocated)
94         {
95                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
96                 apply_swizzle();
97         }
98
99         PixelComponents comp = get_components(storage_fmt);
100         GLenum type = get_gl_type(get_component_type(storage_fmt));
101         glTexImage3D(target, level, storage_fmt, size.x, size.y, size.z, 0, comp, type, data);
102
103         allocated |= 1<<level;
104         if(auto_gen_mipmap && level==0)
105         {
106                 generate_mipmap();
107                 allocated |= (1<<levels)-1;
108         }
109 }
110
111 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
112 {
113         if(comp!=get_components(format) || type!=get_component_type(format))
114                 throw incompatible_data("Texture3D::image");
115         image(level, data);
116 }
117
118 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
119 {
120         if(width==0 || height==0 || depth==0)
121                 throw invalid_operation("Texture3D::sub_image");
122         if(level>=levels)
123                 throw out_of_range("Texture3D::sub_image");
124
125         Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
126         allocate(level);
127
128         PixelComponents comp = get_components(storage_fmt);
129         GLenum type = get_gl_type(get_component_type(storage_fmt));
130         if(ARB_direct_state_access)
131                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
132         else
133                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
134
135         if(auto_gen_mipmap && level==0)
136                 generate_mipmap();
137 }
138
139 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)
140 {
141         if(comp!=get_components(format) || type!=get_component_type(format))
142                 throw incompatible_data("Texture3D::sub_image");
143         sub_image(level, x, y, z, wd, ht, dp, data);
144 }
145
146 void Texture3D::image(const Graphics::Image &img, unsigned lv)
147 {
148         unsigned w = img.get_width();
149         unsigned h = img.get_height();
150
151         if(h%w)
152                 throw incompatible_data("Texture3D::load_image");
153         unsigned d = h/w;
154         h = w;
155
156         PixelFormat fmt = pixelformat_from_image(img);
157         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
158
159         PixelStore pstore = PixelStore::from_image(img);
160         BindRestore _bind_ps(pstore);
161
162         image(0, img.get_pixels());
163 }
164
165 unsigned Texture3D::get_n_levels() const
166 {
167         unsigned s = max(width, height);
168         if(target!=GL_TEXTURE_2D_ARRAY)
169                 s = max(s, depth);
170         unsigned n = 0;
171         for(; s; s>>=1, ++n) ;
172         return n;
173 }
174
175 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
176 {
177         unsigned w = width>>level;
178         unsigned h = height>>level;
179         unsigned d = depth;
180         if(target!=GL_TEXTURE_2D_ARRAY)
181                 d >>= level;
182
183         if(!w && (h || d))
184                 w = 1;
185         if(!h && (w || d))
186                 h = 1;
187         if(!d && (w || h))
188                 d = 1;
189
190         return LinAl::Vector<unsigned, 3>(w, h, d);
191 }
192
193 UInt64 Texture3D::get_data_size() const
194 {
195         return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
196 }
197
198
199 Texture3D::Loader::Loader(Texture3D &t):
200         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
201 {
202         init();
203 }
204
205 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
206         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
207 {
208         init();
209 }
210
211 void Texture3D::Loader::init()
212 {
213         add("raw_data", &Loader::raw_data);
214         add("storage", &Loader::storage);
215         add("storage", &Loader::storage_levels);
216 }
217
218 void Texture3D::Loader::raw_data(const string &data)
219 {
220         obj.image(0, data.data());
221 }
222
223 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
224 {
225         obj.storage(fmt, w, h, d);
226 }
227
228 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
229 {
230         obj.storage(fmt, w, h, d, l);
231 }
232
233 } // namespace GL
234 } // namespace Msp