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 if(ARB_texture_storage)
67 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
68 if(ARB_direct_state_access)
69 glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
71 glTexStorage3D(target, levels, storage_fmt, width, height, depth);
73 allocated |= (1<<levels)-1;
79 void Texture3D::image(unsigned level, const void *data)
81 if(width==0 || height==0 || depth==0)
82 throw invalid_operation("Texture3D::image");
84 throw out_of_range("Texture3D::image");
86 LinAl::Vector<unsigned, 3> size = get_level_size(level);
88 if(ARB_texture_storage)
89 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
91 BindRestore _bind(this);
95 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
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);
103 allocated |= 1<<level;
104 if(auto_gen_mipmap && level==0)
107 allocated |= (1<<levels)-1;
111 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
113 if(comp!=get_components(format) || type!=get_component_type(format))
114 throw incompatible_data("Texture3D::image");
118 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
120 if(width==0 || height==0 || depth==0)
121 throw invalid_operation("Texture3D::sub_image");
123 throw out_of_range("Texture3D::sub_image");
125 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
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);
133 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
135 if(auto_gen_mipmap && level==0)
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)
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);
146 void Texture3D::image(const Graphics::Image &img, unsigned lv)
148 unsigned w = img.get_width();
149 unsigned h = img.get_height();
152 throw incompatible_data("Texture3D::load_image");
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);
159 PixelStore pstore = PixelStore::from_image(img);
160 BindRestore _bind_ps(pstore);
162 image(0, img.get_pixels());
165 unsigned Texture3D::get_n_levels() const
167 unsigned s = max(width, height);
168 if(target!=GL_TEXTURE_2D_ARRAY)
171 for(; s; s>>=1, ++n) ;
175 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
177 unsigned w = width>>level;
178 unsigned h = height>>level;
180 if(target!=GL_TEXTURE_2D_ARRAY)
190 return LinAl::Vector<unsigned, 3>(w, h, d);
193 UInt64 Texture3D::get_data_size() const
195 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
199 Texture3D::Loader::Loader(Texture3D &t):
200 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
205 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
206 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
211 void Texture3D::Loader::init()
213 add("raw_data", &Loader::raw_data);
214 add("storage", &Loader::storage);
215 add("storage", &Loader::storage_levels);
218 void Texture3D::Loader::raw_data(const string &data)
220 obj.image(0, data.data());
223 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
225 obj.storage(fmt, w, h, d);
228 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
230 obj.storage(fmt, w, h, d, l);