2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
6 #include "pixelstore.h"
14 Texture3D::Texture3D(GLenum t):
23 Texture3D::Texture3D():
24 Texture(GL_TEXTURE_3D),
31 static Require _req(EXT_texture3D);
34 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
37 throw invalid_operation("Texture3D::storage");
38 if(wd==0 || ht==0 || dp==0)
39 throw invalid_argument("Texture3D::storage");
41 if(MSP_sized_internal_formats)
42 fmt = get_sized_pixelformat(fmt);
43 require_pixelformat(fmt);
51 void Texture3D::allocate(unsigned level)
53 if(allocated&(1<<level))
56 PixelFormat base_fmt = get_base_pixelformat(ifmt);
57 image(level, base_fmt, get_alloc_type(base_fmt), 0);
60 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
62 if(width==0 || height==0 || depth==0)
63 throw invalid_operation("Texture3D::image");
68 get_level_size(level, w, h, d);
70 BindRestore _bind(this);
71 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
73 allocated |= 1<<level;
74 if(gen_mipmap && level==0)
76 auto_generate_mipmap();
77 allocated |= (1<<get_n_levels())-1;
81 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)
83 if(width==0 || height==0 || depth==0)
84 throw invalid_operation("Texture3D::image");
88 BindRestore _bind(this);
89 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
92 void Texture3D::load_image(const string &fn, int dp)
97 unsigned w = img.get_width();
98 unsigned h = img.get_height();
104 throw incompatible_data("Texture3D::load_image");
110 for(d=h; d*d>h; d>>=2) ;
113 throw incompatible_data("Texture3D::load_image");
120 throw incompatible_data("Texture3D::load_image");
124 throw invalid_argument("Texture3D::load_image");
126 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
128 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
129 else if(w!=width || h!=height || d!=depth)
130 throw incompatible_data("Texture3D::load_image");
132 PixelStore pstore = PixelStore::from_image(img);
133 BindRestore _bind_ps(pstore);
135 image(0, fmt, UNSIGNED_BYTE, img.get_data());
138 void Texture3D::image(const Graphics::Image &img, bool srgb)
140 unsigned w = img.get_width();
141 unsigned h = img.get_height();
147 throw incompatible_data("Texture3D::load_image");
154 throw incompatible_data("Texture3D::load_image");
159 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
161 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
162 else if(w!=width || h!=height || d!=depth)
163 throw incompatible_data("Texture3D::load_image");
165 PixelStore pstore = PixelStore::from_image(img);
166 BindRestore _bind_ps(pstore);
168 image(0, fmt, UNSIGNED_BYTE, img.get_data());
171 unsigned Texture3D::get_n_levels() const
173 unsigned s = max(width, height);
174 if(target!=GL_TEXTURE_2D_ARRAY)
177 for(; s; s>>=1, ++n) ;
181 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
185 if(target!=GL_TEXTURE_2D_ARRAY)
196 UInt64 Texture3D::get_data_size() const
198 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
202 Texture3D::Loader::Loader(Texture3D &t):
203 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
208 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
209 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
214 void Texture3D::Loader::init()
216 add("raw_data", &Loader::raw_data);
217 add("storage", &Loader::storage);
220 void Texture3D::Loader::raw_data(const string &data)
222 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
225 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
228 fmt = get_srgb_pixelformat(fmt);
229 obj.storage(fmt, w, h, d);