2 #include <msp/core/raii.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_texture_storage.h>
5 #include <msp/gl/extensions/ext_texture3d.h>
6 #include <msp/gl/extensions/ext_texture_array.h>
7 #include <msp/graphics/image.h>
10 #include "pixelstore.h"
11 #include "texture3d.h"
18 Texture3D::Texture3D(GLenum t):
26 Texture3D::Texture3D():
27 Texture(GL_TEXTURE_3D),
33 static Require _req(EXT_texture3D);
36 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
40 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
41 throw incompatible_data("Texture3D::storage");
44 if(wd==0 || ht==0 || dp==0)
45 throw invalid_argument("Texture3D::storage");
51 levels = get_n_levels();
53 levels = min(levels, lv);
56 void Texture3D::allocate(unsigned level)
58 if(width==0 || height==0 || depth==0)
59 throw invalid_operation("Texture3D::allocate");
61 throw invalid_argument("Texture3D::allocate");
62 if(allocated&(1<<level))
65 if(ARB_texture_storage)
67 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
68 if(ARB_direct_state_access)
69 glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
71 glTexStorage3D(target, levels, storage_fmt, width, height, depth);
73 allocated |= (1<<levels)-1;
79 void Texture3D::image(unsigned level, const void *data)
81 if(width==0 || height==0 || depth==0)
82 throw invalid_operation("Texture3D::image");
87 get_level_size(level, w, h, d);
89 if(ARB_texture_storage)
90 return sub_image(level, 0, 0, 0, w, h, d, data);
92 BindRestore _bind(this);
96 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
100 PixelComponents comp = get_components(storage_fmt);
101 GLenum type = get_gl_type(get_component_type(storage_fmt));
102 glTexImage3D(target, level, storage_fmt, width, height, depth, 0, comp, type, data);
104 allocated |= 1<<level;
105 if(auto_gen_mipmap && level==0)
108 allocated |= (1<<levels)-1;
112 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
114 if(comp!=get_components(format) || type!=get_component_type(format))
115 throw incompatible_data("Texture3D::image");
119 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
121 if(width==0 || height==0 || depth==0)
122 throw invalid_operation("Texture3D::image");
124 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
127 PixelComponents comp = get_components(storage_fmt);
128 GLenum type = get_gl_type(get_component_type(storage_fmt));
129 if(ARB_direct_state_access)
130 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
132 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
134 if(auto_gen_mipmap && level==0)
138 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelComponents comp, DataType type, const void *data)
140 if(comp!=get_components(format) || type!=get_component_type(format))
141 throw incompatible_data("Texture3D::sub_image");
142 sub_image(level, x, y, z, wd, ht, dp, data);
145 void Texture3D::image(const Graphics::Image &img, unsigned lv)
147 unsigned w = img.get_width();
148 unsigned h = img.get_height();
151 throw incompatible_data("Texture3D::load_image");
155 PixelFormat fmt = pixelformat_from_image(img);
156 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
158 PixelStore pstore = PixelStore::from_image(img);
159 BindRestore _bind_ps(pstore);
161 image(0, img.get_pixels());
164 unsigned Texture3D::get_n_levels() const
166 unsigned s = max(width, height);
167 if(target!=GL_TEXTURE_2D_ARRAY)
170 for(; s; s>>=1, ++n) ;
174 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
178 if(target!=GL_TEXTURE_2D_ARRAY)
189 UInt64 Texture3D::get_data_size() const
191 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
195 Texture3D::Loader::Loader(Texture3D &t):
196 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
201 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
202 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
207 void Texture3D::Loader::init()
209 add("raw_data", &Loader::raw_data);
210 add("storage", &Loader::storage);
211 add("storage", &Loader::storage_levels);
214 void Texture3D::Loader::raw_data(const string &data)
216 obj.image(0, data.data());
219 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
221 obj.storage(fmt, w, h, d);
224 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
226 obj.storage(fmt, w, h, d, l);