2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/gl/extensions/ext_texture3d.h>
5 #include <msp/gl/extensions/ext_texture_array.h>
14 Texture3D::Texture3D(GLenum t):
21 Texture3D::Texture3D():
22 Texture(GL_TEXTURE_3D),
27 static Require _req(EXT_texture3D);
30 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
34 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
35 throw incompatible_data("Texture3D::storage");
38 if(wd==0 || ht==0 || dp==0)
39 throw invalid_argument("Texture3D::storage");
45 levels = get_n_levels();
47 levels = min(levels, lv);
49 GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
50 if(ARB_texture_storage)
52 if(ARB_direct_state_access)
53 glTextureStorage3D(id, levels, gl_fmt, width, height, depth);
57 glTexStorage3D(target, levels, gl_fmt, width, height, depth);
63 GLenum comp = get_gl_components(get_components(storage_fmt));
64 GLenum type = get_gl_type(get_component_type(storage_fmt));
65 for(unsigned i=0; i<levels; ++i)
67 LinAl::Vector<unsigned, 3> lv_size = get_level_size(i);
68 glTexImage3D(target, i, gl_fmt, lv_size.x, lv_size.y, lv_size.z, 0, comp, type, 0);
70 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
76 void Texture3D::image(unsigned level, const void *data)
78 LinAl::Vector<unsigned, 3> size = get_level_size(level);
79 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
82 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
84 if(width==0 || height==0 || depth==0)
85 throw invalid_operation("Texture3D::sub_image");
87 throw out_of_range("Texture3D::sub_image");
89 GLenum comp = get_gl_components(get_components(storage_fmt));
90 GLenum type = get_gl_type(get_component_type(storage_fmt));
91 if(ARB_direct_state_access)
92 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
96 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
100 void Texture3D::image(const Graphics::Image &img, unsigned lv)
102 unsigned w = img.get_width();
103 unsigned h = img.get_height();
106 throw incompatible_data("Texture3D::load_image");
110 storage(pixelformat_from_image(img, use_srgb_format), w, h, d, lv);
111 image(0, img.get_pixels());
114 unsigned Texture3D::get_n_levels() const
116 unsigned s = max(width, height);
117 if(target!=GL_TEXTURE_2D_ARRAY)
120 for(; s; s>>=1, ++n) ;
124 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
126 unsigned w = width>>level;
127 unsigned h = height>>level;
129 if(target!=GL_TEXTURE_2D_ARRAY)
139 return LinAl::Vector<unsigned, 3>(w, h, d);
142 uint64_t Texture3D::get_data_size() const
144 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
148 Texture3D::Loader::Loader(Texture3D &t):
149 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
154 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
155 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
160 void Texture3D::Loader::init()
162 add("raw_data", &Loader::raw_data);
163 add("storage", &Loader::storage);
164 add("storage", &Loader::storage_levels);
167 void Texture3D::Loader::raw_data(const string &data)
169 obj.image(0, data.data());
172 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
174 obj.storage(fmt, w, h, d);
177 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
179 obj.storage(fmt, w, h, d, l);