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>
16 Texture3D::Texture3D(GLenum t):
24 Texture3D::Texture3D():
25 Texture(GL_TEXTURE_3D),
31 static Require _req(EXT_texture3D);
34 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
38 if(fmt!=format || wd!=width || ht!=height || dp!=depth || (lv && lv!=levels))
39 throw incompatible_data("Texture3D::storage");
42 if(wd==0 || ht==0 || dp==0)
43 throw invalid_argument("Texture3D::storage");
49 levels = get_n_levels();
51 levels = min(levels, lv);
54 void Texture3D::allocate(unsigned level)
56 if(width==0 || height==0 || depth==0)
57 throw invalid_operation("Texture3D::allocate");
59 throw invalid_argument("Texture3D::allocate");
60 if(allocated&(1<<level))
63 if(ARB_texture_storage)
65 GLenum fmt = get_gl_pixelformat(storage_fmt);
66 if(ARB_direct_state_access)
67 glTextureStorage3D(id, levels, fmt, width, height, depth);
71 glTexStorage3D(target, levels, fmt, width, height, depth);
74 allocated |= (1<<levels)-1;
80 void Texture3D::image(unsigned level, const void *data)
82 if(width==0 || height==0 || depth==0)
83 throw invalid_operation("Texture3D::image");
85 throw out_of_range("Texture3D::image");
87 LinAl::Vector<unsigned, 3> size = get_level_size(level);
89 if(ARB_texture_storage)
90 return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
96 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
100 GLenum fmt = get_gl_pixelformat(storage_fmt);
101 GLenum comp = get_gl_components(get_components(storage_fmt));
102 GLenum type = get_gl_type(get_component_type(storage_fmt));
103 glTexImage3D(target, level, fmt, size.x, size.y, size.z, 0, comp, type, data);
105 allocated |= 1<<level;
108 void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
110 if(comp!=get_components(format) || type!=get_component_type(format))
111 throw incompatible_data("Texture3D::image");
115 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
117 if(width==0 || height==0 || depth==0)
118 throw invalid_operation("Texture3D::sub_image");
120 throw out_of_range("Texture3D::sub_image");
124 GLenum comp = get_gl_components(get_components(storage_fmt));
125 GLenum type = get_gl_type(get_component_type(storage_fmt));
126 if(ARB_direct_state_access)
127 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
131 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
135 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)
137 if(comp!=get_components(format) || type!=get_component_type(format))
138 throw incompatible_data("Texture3D::sub_image");
139 sub_image(level, x, y, z, wd, ht, dp, data);
142 void Texture3D::image(const Graphics::Image &img, unsigned lv)
144 unsigned w = img.get_width();
145 unsigned h = img.get_height();
148 throw incompatible_data("Texture3D::load_image");
152 PixelFormat fmt = pixelformat_from_image(img);
153 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
155 image(0, img.get_pixels());
158 unsigned Texture3D::get_n_levels() const
160 unsigned s = max(width, height);
161 if(target!=GL_TEXTURE_2D_ARRAY)
164 for(; s; s>>=1, ++n) ;
168 LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
170 unsigned w = width>>level;
171 unsigned h = height>>level;
173 if(target!=GL_TEXTURE_2D_ARRAY)
183 return LinAl::Vector<unsigned, 3>(w, h, d);
186 UInt64 Texture3D::get_data_size() const
188 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
192 Texture3D::Loader::Loader(Texture3D &t):
193 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
198 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
199 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
204 void Texture3D::Loader::init()
206 add("raw_data", &Loader::raw_data);
207 add("storage", &Loader::storage);
208 add("storage", &Loader::storage_levels);
211 void Texture3D::Loader::raw_data(const string &data)
213 obj.image(0, data.data());
216 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
218 obj.storage(fmt, w, h, d);
221 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
223 obj.storage(fmt, w, h, d, l);