2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/ext_texture3d.h>
4 #include <msp/graphics/image.h>
7 #include "pixelstore.h"
15 Texture3D::Texture3D(GLenum t):
24 Texture3D::Texture3D():
25 Texture(GL_TEXTURE_3D),
32 static Require _req(EXT_texture3D);
35 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
38 throw invalid_operation("Texture3D::storage");
39 if(wd==0 || ht==0 || dp==0)
40 throw invalid_argument("Texture3D::storage");
42 if(MSP_sized_internal_formats)
43 fmt = get_sized_pixelformat(fmt);
44 require_pixelformat(fmt);
52 void Texture3D::allocate(unsigned level)
54 if(allocated&(1<<level))
57 if(ARB_texture_storage)
59 BindRestore _bind(this);
60 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
61 glTexStorage3D(target, n_levels, ifmt, width, height, depth);
62 allocated |= (1<<n_levels)-1;
66 PixelFormat base_fmt = get_base_pixelformat(ifmt);
67 image(level, base_fmt, get_alloc_type(base_fmt), 0);
71 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
73 if(width==0 || height==0 || depth==0)
74 throw invalid_operation("Texture3D::image");
79 get_level_size(level, w, h, d);
81 BindRestore _bind(this);
82 if(ARB_texture_storage)
83 sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
85 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
87 allocated |= 1<<level;
88 if(gen_mipmap && level==0)
90 auto_generate_mipmap();
91 allocated |= (1<<get_n_levels())-1;
95 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)
97 if(width==0 || height==0 || depth==0)
98 throw invalid_operation("Texture3D::image");
102 BindRestore _bind(this);
103 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
106 void Texture3D::load_image(const string &fn, int dp)
111 unsigned w = img.get_width();
112 unsigned h = img.get_height();
118 throw incompatible_data("Texture3D::load_image");
124 for(d=h; d*d>h; d>>=2) ;
127 throw incompatible_data("Texture3D::load_image");
134 throw incompatible_data("Texture3D::load_image");
138 throw invalid_argument("Texture3D::load_image");
140 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
142 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
143 else if(w!=width || h!=height || d!=depth)
144 throw incompatible_data("Texture3D::load_image");
146 PixelStore pstore = PixelStore::from_image(img);
147 BindRestore _bind_ps(pstore);
149 image(0, fmt, UNSIGNED_BYTE, img.get_data());
152 void Texture3D::image(const Graphics::Image &img, bool srgb)
154 unsigned w = img.get_width();
155 unsigned h = img.get_height();
161 throw incompatible_data("Texture3D::load_image");
168 throw incompatible_data("Texture3D::load_image");
173 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
175 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
176 else if(w!=width || h!=height || d!=depth)
177 throw incompatible_data("Texture3D::load_image");
179 PixelStore pstore = PixelStore::from_image(img);
180 BindRestore _bind_ps(pstore);
182 image(0, fmt, UNSIGNED_BYTE, img.get_data());
185 unsigned Texture3D::get_n_levels() const
187 unsigned s = max(width, height);
188 if(target!=GL_TEXTURE_2D_ARRAY)
191 for(; s; s>>=1, ++n) ;
195 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
199 if(target!=GL_TEXTURE_2D_ARRAY)
210 UInt64 Texture3D::get_data_size() const
212 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
216 Texture3D::Loader::Loader(Texture3D &t):
217 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
222 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
223 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
228 void Texture3D::Loader::init()
230 add("raw_data", &Loader::raw_data);
231 add("storage", &Loader::storage);
234 void Texture3D::Loader::raw_data(const string &data)
236 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
239 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
242 fmt = get_srgb_pixelformat(fmt);
243 obj.storage(fmt, w, h, d);