]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture3d.cpp
Add support for integer vertex attributes
[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
61         bool direct = ARB_texture_storage && ARB_direct_state_access;
62         if(!direct)
63         {
64                 glActiveTexture(GL_TEXTURE0);
65                 glBindTexture(target, id);
66         }
67
68         allocate_(level);
69
70         if(!direct)
71                 glBindTexture(target, 0);
72 }
73
74 void Texture3D::allocate_(unsigned level)
75 {
76         if(allocated&(1<<level))
77                 return;
78
79         if(ARB_texture_storage)
80         {
81                 GLenum fmt = get_gl_pixelformat(storage_fmt);
82                 if(ARB_direct_state_access)
83                         glTextureStorage3D(id, levels, fmt, width, height, depth);
84                 else
85                         glTexStorage3D(target, levels, fmt, width, height, depth);
86                 apply_swizzle();
87                 allocated |= (1<<levels)-1;
88         }
89         else
90                 image_(level, 0);
91 }
92
93 void Texture3D::image(unsigned level, const void *data)
94 {
95         if(width==0 || height==0 || depth==0)
96                 throw invalid_operation("Texture3D::image");
97         if(level>=levels)
98                 throw out_of_range("Texture3D::image");
99
100         if(ARB_texture_storage)
101         {
102                 LinAl::Vector<unsigned, 3> size = get_level_size(level);
103                 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
104         }
105
106         glActiveTexture(GL_TEXTURE0);
107         glBindTexture(target, id);
108
109         image_(level, data);
110
111         if(auto_gen_mipmap && level==0)
112         {
113                 generate_mipmap_();
114                 allocated |= (1<<levels)-1;
115         }
116
117         glBindTexture(target, 0);
118 }
119
120 void Texture3D::image_(unsigned level, const void *data)
121 {
122         if(!allocated)
123         {
124                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
125                 apply_swizzle();
126         }
127
128         LinAl::Vector<unsigned, 3> size = get_level_size(level);
129         GLenum fmt = get_gl_pixelformat(storage_fmt);
130         GLenum comp = get_gl_components(get_components(storage_fmt));
131         GLenum type = get_gl_type(get_component_type(storage_fmt));
132         glTexImage3D(target, level, fmt, size.x, size.y, size.z, 0, comp, type, data);
133
134         allocated |= 1<<level;
135 }
136
137 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
138 {
139         if(comp!=get_components(format) || type!=get_component_type(format))
140                 throw incompatible_data("Texture3D::image");
141         image(level, data);
142 }
143
144 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
145 {
146         if(width==0 || height==0 || depth==0)
147                 throw invalid_operation("Texture3D::sub_image");
148         if(level>=levels)
149                 throw out_of_range("Texture3D::sub_image");
150
151         bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
152         if(!direct)
153         {
154                 glActiveTexture(GL_TEXTURE0);
155                 glBindTexture(target, id);
156         }
157
158         allocate_(level);
159
160         GLenum comp = get_gl_components(get_components(storage_fmt));
161         GLenum type = get_gl_type(get_component_type(storage_fmt));
162         if(ARB_direct_state_access)
163                 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
164         else
165                 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
166
167         if(auto_gen_mipmap && level==0)
168                 generate_mipmap_();
169
170         if(!direct)
171                 glBindTexture(target, 0);
172 }
173
174 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)
175 {
176         if(comp!=get_components(format) || type!=get_component_type(format))
177                 throw incompatible_data("Texture3D::sub_image");
178         sub_image(level, x, y, z, wd, ht, dp, data);
179 }
180
181 void Texture3D::image(const Graphics::Image &img, unsigned lv)
182 {
183         unsigned w = img.get_width();
184         unsigned h = img.get_height();
185
186         if(h%w)
187                 throw incompatible_data("Texture3D::load_image");
188         unsigned d = h/w;
189         h = w;
190
191         PixelFormat fmt = pixelformat_from_image(img);
192         storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
193
194         image(0, img.get_pixels());
195 }
196
197 unsigned Texture3D::get_n_levels() const
198 {
199         unsigned s = max(width, height);
200         if(target!=GL_TEXTURE_2D_ARRAY)
201                 s = max(s, depth);
202         unsigned n = 0;
203         for(; s; s>>=1, ++n) ;
204         return n;
205 }
206
207 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
208 {
209         unsigned w = width>>level;
210         unsigned h = height>>level;
211         unsigned d = depth;
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         return LinAl::Vector<unsigned, 3>(w, h, d);
223 }
224
225 UInt64 Texture3D::get_data_size() const
226 {
227         return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
228 }
229
230
231 Texture3D::Loader::Loader(Texture3D &t):
232         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
233 {
234         init();
235 }
236
237 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
238         DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
239 {
240         init();
241 }
242
243 void Texture3D::Loader::init()
244 {
245         add("raw_data", &Loader::raw_data);
246         add("storage", &Loader::storage);
247         add("storage", &Loader::storage_levels);
248 }
249
250 void Texture3D::Loader::raw_data(const string &data)
251 {
252         obj.image(0, data.data());
253 }
254
255 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
256 {
257         obj.storage(fmt, w, h, d);
258 }
259
260 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
261 {
262         obj.storage(fmt, w, h, d, l);
263 }
264
265 } // namespace GL
266 } // namespace Msp