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");
40 require_pixelformat(fmt);
48 void Texture3D::allocate(unsigned level)
50 if(allocated&(1<<level))
53 PixelFormat base_fmt = get_base_pixelformat(ifmt);
54 image(level, base_fmt, get_alloc_type(base_fmt), 0);
57 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
59 if(width==0 || height==0 || depth==0)
60 throw invalid_operation("Texture3D::image");
65 get_level_size(level, w, h, d);
67 BindRestore _bind(this);
68 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
70 allocated |= 1<<level;
71 if(gen_mipmap && level==0)
73 auto_generate_mipmap();
74 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
75 allocated |= (1<<level)-1;
79 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)
81 if(width==0 || height==0 || depth==0)
82 throw invalid_operation("Texture3D::image");
86 BindRestore _bind(this);
87 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
90 void Texture3D::load_image(const string &fn, int dp)
95 unsigned w = img.get_width();
96 unsigned h = img.get_height();
102 throw incompatible_data("Texture3D::load_image");
108 for(d=h; d*d>h; d>>=2) ;
111 throw incompatible_data("Texture3D::load_image");
118 throw incompatible_data("Texture3D::load_image");
122 throw invalid_argument("Texture3D::load_image");
124 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
126 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
127 else if(w!=width || h!=height || d!=depth)
128 throw incompatible_data("Texture3D::load_image");
130 PixelStore pstore = PixelStore::from_image(img);
131 BindRestore _bind_ps(pstore);
133 image(0, fmt, UNSIGNED_BYTE, img.get_data());
136 void Texture3D::image(const Graphics::Image &img, bool srgb)
138 unsigned w = img.get_width();
139 unsigned h = img.get_height();
145 throw incompatible_data("Texture3D::load_image");
152 throw incompatible_data("Texture3D::load_image");
157 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
159 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
160 else if(w!=width || h!=height || d!=depth)
161 throw incompatible_data("Texture3D::load_image");
163 PixelStore pstore = PixelStore::from_image(img);
164 BindRestore _bind_ps(pstore);
166 image(0, fmt, UNSIGNED_BYTE, img.get_data());
169 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
173 if(target!=GL_TEXTURE_2D_ARRAY)
184 UInt64 Texture3D::get_data_size() const
186 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
190 Texture3D::Loader::Loader(Texture3D &t):
191 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
196 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
197 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
202 void Texture3D::Loader::init()
204 add("raw_data", &Loader::raw_data);
205 add("storage", &Loader::storage);
208 void Texture3D::Loader::raw_data(const string &data)
210 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
213 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
216 fmt = get_srgb_pixelformat(fmt);
217 obj.storage(fmt, w, h, d);