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>
16 Texture3D::Texture3D(GLenum t):
24 Texture3D::Texture3D():
25 Texture(GL_TEXTURE_3D),
31 static Require _req(EXT_texture3D);
34 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
38 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
39 throw incompatible_data("Texture3D::storage");
42 if(wd==0 || ht==0 || dp==0)
43 throw invalid_argument("Texture3D::storage");
49 levels = get_n_levels();
51 levels = min(levels, lv);
54 void Texture3D::allocate(unsigned level)
56 if(width==0 || height==0 || depth==0)
57 throw invalid_operation("Texture3D::allocate");
59 throw invalid_argument("Texture3D::allocate");
60 if(allocated&(1<<level))
63 bool direct = ARB_texture_storage && ARB_direct_state_access;
66 glActiveTexture(GL_TEXTURE0);
67 glBindTexture(target, id);
73 glBindTexture(target, 0);
76 void Texture3D::allocate_(unsigned level)
78 if(ARB_texture_storage)
80 if(ARB_direct_state_access)
81 glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
83 glTexStorage3D(target, levels, storage_fmt, width, height, depth);
85 allocated |= (1<<levels)-1;
91 void Texture3D::image(unsigned level, const void *data)
93 if(width==0 || height==0 || depth==0)
94 throw invalid_operation("Texture3D::image");
96 throw out_of_range("Texture3D::image");
98 if(ARB_texture_storage)
100 LinAl::Vector<unsigned, 3> size = get_level_size(level);
101 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
104 glActiveTexture(GL_TEXTURE0);
105 glBindTexture(target, id);
109 if(auto_gen_mipmap && level==0)
112 allocated |= (1<<levels)-1;
115 glBindTexture(target, 0);
118 void Texture3D::image_(unsigned level, const void *data)
122 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
126 LinAl::Vector<unsigned, 3> size = get_level_size(level);
127 PixelComponents comp = get_components(storage_fmt);
128 GLenum type = get_gl_type(get_component_type(storage_fmt));
129 glTexImage3D(target, level, storage_fmt, size.x, size.y, size.z, 0, comp, type, data);
131 allocated |= 1<<level;
134 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
136 if(comp!=get_components(format) || type!=get_component_type(format))
137 throw incompatible_data("Texture3D::image");
141 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
143 if(width==0 || height==0 || depth==0)
144 throw invalid_operation("Texture3D::sub_image");
146 throw out_of_range("Texture3D::sub_image");
148 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
151 glActiveTexture(GL_TEXTURE0);
152 glBindTexture(target, id);
157 PixelComponents comp = get_components(storage_fmt);
158 GLenum type = get_gl_type(get_component_type(storage_fmt));
159 if(ARB_direct_state_access)
160 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
162 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
164 if(auto_gen_mipmap && level==0)
168 glBindTexture(target, 0);
171 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)
173 if(comp!=get_components(format) || type!=get_component_type(format))
174 throw incompatible_data("Texture3D::sub_image");
175 sub_image(level, x, y, z, wd, ht, dp, data);
178 void Texture3D::image(const Graphics::Image &img, unsigned lv)
180 unsigned w = img.get_width();
181 unsigned h = img.get_height();
184 throw incompatible_data("Texture3D::load_image");
188 PixelFormat fmt = pixelformat_from_image(img);
189 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
191 image(0, img.get_pixels());
194 unsigned Texture3D::get_n_levels() const
196 unsigned s = max(width, height);
197 if(target!=GL_TEXTURE_2D_ARRAY)
200 for(; s; s>>=1, ++n) ;
204 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
206 unsigned w = width>>level;
207 unsigned h = height>>level;
209 if(target!=GL_TEXTURE_2D_ARRAY)
219 return LinAl::Vector<unsigned, 3>(w, h, d);
222 UInt64 Texture3D::get_data_size() const
224 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
228 Texture3D::Loader::Loader(Texture3D &t):
229 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
234 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
235 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
240 void Texture3D::Loader::init()
242 add("raw_data", &Loader::raw_data);
243 add("storage", &Loader::storage);
244 add("storage", &Loader::storage_levels);
247 void Texture3D::Loader::raw_data(const string &data)
249 obj.image(0, data.data());
252 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
254 obj.storage(fmt, w, h, d);
257 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
259 obj.storage(fmt, w, h, d, l);