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)
39 throw invalid_operation("Texture3D::storage");
40 if(wd==0 || ht==0 || dp==0)
41 throw invalid_argument("Texture3D::storage");
47 levels = get_n_levels();
49 levels = min(levels, lv);
52 void Texture3D::allocate(unsigned level)
54 if(width==0 || height==0 || depth==0)
55 throw invalid_operation("Texture3D::allocate");
57 throw invalid_argument("Texture3D::allocate");
58 if(allocated&(1<<level))
61 if(ARB_texture_storage)
63 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
64 if(ARB_direct_state_access)
65 glTextureStorage3D(id, levels, storage_fmt, width, height, depth);
67 glTexStorage3D(target, levels, storage_fmt, width, height, depth);
69 allocated |= (1<<levels)-1;
75 void Texture3D::image(unsigned level, const void *data)
77 if(width==0 || height==0 || depth==0)
78 throw invalid_operation("Texture3D::image");
83 get_level_size(level, w, h, d);
85 if(ARB_texture_storage)
86 return sub_image(level, 0, 0, 0, w, h, d, data);
88 BindRestore _bind(this);
92 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
96 PixelComponents comp = get_components(storage_fmt);
97 DataType type = get_component_type(storage_fmt);
98 glTexImage3D(target, level, storage_fmt, width, height, depth, 0, comp, type, data);
100 allocated |= 1<<level;
101 if(auto_gen_mipmap && level==0)
104 allocated |= (1<<levels)-1;
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::image");
120 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
123 PixelComponents comp = get_components(storage_fmt);
124 DataType type = get_component_type(storage_fmt);
125 if(ARB_direct_state_access)
126 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
128 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
130 if(auto_gen_mipmap && level==0)
134 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)
136 if(comp!=get_components(format) || type!=get_component_type(format))
137 throw incompatible_data("Texture3D::sub_image");
138 sub_image(level, x, y, z, wd, ht, dp, data);
141 void Texture3D::image(const Graphics::Image &img, unsigned lv)
143 unsigned w = img.get_width();
144 unsigned h = img.get_height();
147 throw incompatible_data("Texture3D::load_image");
151 PixelFormat fmt = pixelformat_from_image(img);
153 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, d, lv);
154 else if(w!=width || h!=height || d!=depth)
155 throw incompatible_data("Texture3D::load_image");
157 PixelStore pstore = PixelStore::from_image(img);
158 BindRestore _bind_ps(pstore);
160 image(0, img.get_pixels());
163 unsigned Texture3D::get_n_levels() const
165 unsigned s = max(width, height);
166 if(target!=GL_TEXTURE_2D_ARRAY)
169 for(; s; s>>=1, ++n) ;
173 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
177 if(target!=GL_TEXTURE_2D_ARRAY)
188 UInt64 Texture3D::get_data_size() const
190 return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
194 Texture3D::Loader::Loader(Texture3D &t):
195 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
200 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
201 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
206 void Texture3D::Loader::init()
208 add("raw_data", &Loader::raw_data);
209 add("storage", &Loader::storage);
210 add("storage", &Loader::storage_levels);
213 void Texture3D::Loader::raw_data(const string &data)
215 obj.image(0, data.data());
218 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
220 obj.storage(fmt, w, h, d);
223 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
225 obj.storage(fmt, w, h, d, l);