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/graphics/image.h>
9 #include "pixelstore.h"
10 #include "texture3d.h"
17 Texture3D::Texture3D(GLenum t):
26 Texture3D::Texture3D():
27 Texture(GL_TEXTURE_3D),
34 static Require _req(EXT_texture3D);
37 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
40 throw invalid_operation("Texture3D::storage");
41 if(wd==0 || ht==0 || dp==0)
42 throw invalid_argument("Texture3D::storage");
44 if(MSP_sized_internal_formats)
45 fmt = get_sized_pixelformat(fmt);
46 require_pixelformat(fmt);
54 void Texture3D::allocate(unsigned level)
56 if(allocated&(1<<level))
59 if(ARB_texture_storage)
61 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
62 if(ARB_direct_state_access)
63 glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
66 BindRestore _bind(this);
67 glTexStorage3D(target, n_levels, ifmt, width, height, depth);
69 allocated |= (1<<n_levels)-1;
73 PixelFormat base_fmt = get_base_pixelformat(ifmt);
74 image(level, base_fmt, get_alloc_type(base_fmt), 0);
78 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
80 if(width==0 || height==0 || depth==0)
81 throw invalid_operation("Texture3D::image");
86 get_level_size(level, w, h, d);
88 if(ARB_texture_storage)
89 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
91 BindRestore _bind(this);
92 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
94 allocated |= 1<<level;
95 if(gen_mipmap && level==0)
97 auto_generate_mipmap();
98 allocated |= (1<<get_n_levels())-1;
102 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
104 if(width==0 || height==0 || depth==0)
105 throw invalid_operation("Texture3D::image");
107 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
110 if(ARB_direct_state_access)
111 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
113 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
115 if(gen_mipmap && level==0)
116 auto_generate_mipmap();
119 void Texture3D::load_image(const string &fn, int dp)
124 unsigned w = img.get_width();
125 unsigned h = img.get_height();
131 throw incompatible_data("Texture3D::load_image");
137 for(d=h; d*d>h; d>>=2) ;
140 throw incompatible_data("Texture3D::load_image");
147 throw incompatible_data("Texture3D::load_image");
151 throw invalid_argument("Texture3D::load_image");
153 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
155 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
156 else if(w!=width || h!=height || d!=depth)
157 throw incompatible_data("Texture3D::load_image");
159 PixelStore pstore = PixelStore::from_image(img);
160 BindRestore _bind_ps(pstore);
162 image(0, fmt, UNSIGNED_BYTE, img.get_data());
165 void Texture3D::image(const Graphics::Image &img, bool srgb)
167 unsigned w = img.get_width();
168 unsigned h = img.get_height();
174 throw incompatible_data("Texture3D::load_image");
181 throw incompatible_data("Texture3D::load_image");
186 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
188 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
189 else if(w!=width || h!=height || d!=depth)
190 throw incompatible_data("Texture3D::load_image");
192 PixelStore pstore = PixelStore::from_image(img);
193 BindRestore _bind_ps(pstore);
195 image(0, fmt, UNSIGNED_BYTE, img.get_data());
198 unsigned Texture3D::get_n_levels() const
200 unsigned s = max(width, height);
201 if(target!=GL_TEXTURE_2D_ARRAY)
204 for(; s; s>>=1, ++n) ;
208 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
212 if(target!=GL_TEXTURE_2D_ARRAY)
223 UInt64 Texture3D::get_data_size() const
225 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
229 Texture3D::Loader::Loader(Texture3D &t):
230 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
235 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
236 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
241 void Texture3D::Loader::init()
243 add("raw_data", &Loader::raw_data);
244 add("storage", &Loader::storage);
247 void Texture3D::Loader::raw_data(const string &data)
249 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
252 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
255 fmt = get_srgb_pixelformat(fmt);
256 obj.storage(fmt, w, h, d);