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 if(ARB_texture_storage)
82 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
84 BindRestore _bind(this);
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");
100 BindRestore _bind(this);
103 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
105 if(gen_mipmap && level==0)
106 auto_generate_mipmap();
109 void Texture3D::load_image(const string &fn, int dp)
114 unsigned w = img.get_width();
115 unsigned h = img.get_height();
121 throw incompatible_data("Texture3D::load_image");
127 for(d=h; d*d>h; d>>=2) ;
130 throw incompatible_data("Texture3D::load_image");
137 throw incompatible_data("Texture3D::load_image");
141 throw invalid_argument("Texture3D::load_image");
143 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
145 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
146 else if(w!=width || h!=height || d!=depth)
147 throw incompatible_data("Texture3D::load_image");
149 PixelStore pstore = PixelStore::from_image(img);
150 BindRestore _bind_ps(pstore);
152 image(0, fmt, UNSIGNED_BYTE, img.get_data());
155 void Texture3D::image(const Graphics::Image &img, bool srgb)
157 unsigned w = img.get_width();
158 unsigned h = img.get_height();
164 throw incompatible_data("Texture3D::load_image");
171 throw incompatible_data("Texture3D::load_image");
176 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
178 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
179 else if(w!=width || h!=height || d!=depth)
180 throw incompatible_data("Texture3D::load_image");
182 PixelStore pstore = PixelStore::from_image(img);
183 BindRestore _bind_ps(pstore);
185 image(0, fmt, UNSIGNED_BYTE, img.get_data());
188 unsigned Texture3D::get_n_levels() const
190 unsigned s = max(width, height);
191 if(target!=GL_TEXTURE_2D_ARRAY)
194 for(; s; s>>=1, ++n) ;
198 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
202 if(target!=GL_TEXTURE_2D_ARRAY)
213 UInt64 Texture3D::get_data_size() const
215 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
219 Texture3D::Loader::Loader(Texture3D &t):
220 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
225 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
226 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
231 void Texture3D::Loader::init()
233 add("raw_data", &Loader::raw_data);
234 add("storage", &Loader::storage);
237 void Texture3D::Loader::raw_data(const string &data)
239 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
242 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
245 fmt = get_srgb_pixelformat(fmt);
246 obj.storage(fmt, w, h, d);