2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
6 #include "pixelstore.h"
14 Texture3D::Texture3D():
15 Texture(GL_TEXTURE_3D),
22 static Require _req(EXT_texture3D);
25 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
28 throw invalid_operation("Texture3D::storage");
29 if(wd==0 || ht==0 || dp==0)
30 throw invalid_argument("Texture3D::storage");
31 require_pixelformat(fmt);
39 void Texture3D::allocate(unsigned level)
41 if(allocated&(1<<level))
44 PixelFormat base_fmt = get_base_pixelformat(ifmt);
45 image(level, base_fmt, get_alloc_type(base_fmt), 0);
48 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
50 if(width==0 || height==0 || depth==0)
51 throw invalid_operation("Texture3D::image");
56 get_level_size(level, w, h, d);
58 BindRestore _bind(this);
59 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
61 allocated |= 1<<level;
62 if(gen_mipmap && level==0)
64 auto_generate_mipmap();
65 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
66 allocated |= (1<<level)-1;
70 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)
72 if(width==0 || height==0 || depth==0)
73 throw invalid_operation("Texture3D::image");
77 BindRestore _bind(this);
78 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
81 void Texture3D::load_image(const string &fn, int dp)
86 unsigned w = img.get_width();
87 unsigned h = img.get_height();
93 throw incompatible_data("Texture3D::load_image");
99 for(d=h; d*d>h; d>>=2) ;
102 throw incompatible_data("Texture3D::load_image");
109 throw incompatible_data("Texture3D::load_image");
113 throw invalid_argument("Texture3D::load_image");
115 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
117 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
118 else if(w!=width || h!=height || d!=depth)
119 throw incompatible_data("Texture3D::load_image");
121 PixelStore pstore = PixelStore::from_image(img);
122 BindRestore _bind_ps(pstore);
124 image(0, fmt, UNSIGNED_BYTE, img.get_data());
127 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
141 UInt64 Texture3D::get_data_size() const
143 return id ? width*height*depth*get_pixel_size(ifmt) : 0;