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>
10 #include "pixelstore.h"
11 #include "texture3d.h"
18 Texture3D::Texture3D(GLenum t):
26 Texture3D::Texture3D():
27 Texture(GL_TEXTURE_3D),
33 static Require _req(EXT_texture3D);
36 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
40 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
41 throw incompatible_data("Texture3D::storage");
44 if(wd==0 || ht==0 || dp==0)
45 throw invalid_argument("Texture3D::storage");
51 levels = get_n_levels();
53 levels = min(levels, lv);
56 void Texture3D::allocate(unsigned level)
58 if(width==0 || height==0 || depth==0)
59 throw invalid_operation("Texture3D::allocate");
61 throw invalid_argument("Texture3D::allocate");
62 if(allocated&(1<<level))
65 bool direct = ARB_texture_storage && ARB_direct_state_access;
68 glActiveTexture(GL_TEXTURE0);
69 glBindTexture(target, id);
75 glBindTexture(target, 0);
78 void Texture3D::allocate_(unsigned level)
80 if(ARB_texture_storage)
82 if(ARB_direct_state_access)
83 glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
85 glTexStorage3D(target, levels, storage_fmt, width, height, depth);
87 allocated |= (1<<levels)-1;
93 void Texture3D::image(unsigned level, const void *data)
95 if(width==0 || height==0 || depth==0)
96 throw invalid_operation("Texture3D::image");
98 throw out_of_range("Texture3D::image");
100 if(ARB_texture_storage)
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);
106 glActiveTexture(GL_TEXTURE0);
107 glBindTexture(target, id);
111 if(auto_gen_mipmap && level==0)
114 allocated |= (1<<levels)-1;
117 glBindTexture(target, 0);
120 void Texture3D::image_(unsigned level, const void *data)
124 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
128 LinAl::Vector<unsigned, 3> size = get_level_size(level);
129 PixelComponents comp = get_components(storage_fmt);
130 GLenum type = get_gl_type(get_component_type(storage_fmt));
131 glTexImage3D(target, level, storage_fmt, size.x, size.y, size.z, 0, comp, type, data);
133 allocated |= 1<<level;
136 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
138 if(comp!=get_components(format) || type!=get_component_type(format))
139 throw incompatible_data("Texture3D::image");
143 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
145 if(width==0 || height==0 || depth==0)
146 throw invalid_operation("Texture3D::sub_image");
148 throw out_of_range("Texture3D::sub_image");
150 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
153 glActiveTexture(GL_TEXTURE0);
154 glBindTexture(target, id);
159 PixelComponents comp = 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);
164 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
166 if(auto_gen_mipmap && level==0)
170 glBindTexture(target, 0);
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)
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);
180 void Texture3D::image(const Graphics::Image &img, unsigned lv)
182 unsigned w = img.get_width();
183 unsigned h = img.get_height();
186 throw incompatible_data("Texture3D::load_image");
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);
193 PixelStore pstore = PixelStore::from_image(img);
194 BindRestore _bind_ps(pstore);
196 image(0, img.get_pixels());
199 unsigned Texture3D::get_n_levels() const
201 unsigned s = max(width, height);
202 if(target!=GL_TEXTURE_2D_ARRAY)
205 for(; s; s>>=1, ++n) ;
209 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
211 unsigned w = width>>level;
212 unsigned h = height>>level;
214 if(target!=GL_TEXTURE_2D_ARRAY)
224 return LinAl::Vector<unsigned, 3>(w, h, d);
227 UInt64 Texture3D::get_data_size() const
229 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
233 Texture3D::Loader::Loader(Texture3D &t):
234 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
239 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
240 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
245 void Texture3D::Loader::init()
247 add("raw_data", &Loader::raw_data);
248 add("storage", &Loader::storage);
249 add("storage", &Loader::storage_levels);
252 void Texture3D::Loader::raw_data(const string &data)
254 obj.image(0, data.data());
257 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
259 obj.storage(fmt, w, h, d);
262 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
264 obj.storage(fmt, w, h, d, l);