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)
39 throw invalid_operation("Texture3D::storage");
40 if(wd==0 || ht==0 || dp==0)
41 throw invalid_argument("Texture3D::storage");
43 set_internal_format(fmt);
49 void Texture3D::allocate(unsigned level)
51 if(width==0 || height==0 || depth==0)
52 throw invalid_operation("Texture3D::allocate");
53 if(allocated&(1<<level))
56 if(ARB_texture_storage)
58 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
59 if(ARB_direct_state_access)
60 glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
63 BindRestore _bind(this);
64 glTexStorage3D(target, n_levels, ifmt, width, height, depth);
66 allocated |= (1<<n_levels)-1;
70 PixelFormat base_fmt = get_base_pixelformat(ifmt);
71 image(level, base_fmt, get_alloc_type(base_fmt), 0);
75 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, 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, fmt, type, data);
88 BindRestore _bind(this);
89 glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_format(fmt), type, data);
91 allocated |= 1<<level;
92 if(gen_mipmap && level==0)
94 auto_generate_mipmap();
95 allocated |= (1<<get_n_levels())-1;
99 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)
101 if(width==0 || height==0 || depth==0)
102 throw invalid_operation("Texture3D::image");
104 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
107 fmt = get_upload_format(fmt);
108 if(ARB_direct_state_access)
109 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
111 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
113 if(gen_mipmap && level==0)
114 auto_generate_mipmap();
117 void Texture3D::load_image(const string &fn, int dp)
122 unsigned w = img.get_width();
123 unsigned h = img.get_height();
129 throw incompatible_data("Texture3D::load_image");
135 for(d=h; d*d>h; d>>=2) ;
138 throw incompatible_data("Texture3D::load_image");
145 throw incompatible_data("Texture3D::load_image");
149 throw invalid_argument("Texture3D::load_image");
151 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
153 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
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, fmt, UNSIGNED_BYTE, img.get_data());
163 void Texture3D::image(const Graphics::Image &img, bool srgb)
165 unsigned w = img.get_width();
166 unsigned h = img.get_height();
172 throw incompatible_data("Texture3D::load_image");
179 throw incompatible_data("Texture3D::load_image");
184 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
186 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
187 else if(w!=width || h!=height || d!=depth)
188 throw incompatible_data("Texture3D::load_image");
190 PixelStore pstore = PixelStore::from_image(img);
191 BindRestore _bind_ps(pstore);
193 image(0, fmt, UNSIGNED_BYTE, img.get_data());
196 unsigned Texture3D::get_n_levels() const
198 unsigned s = max(width, height);
199 if(target!=GL_TEXTURE_2D_ARRAY)
202 for(; s; s>>=1, ++n) ;
206 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
210 if(target!=GL_TEXTURE_2D_ARRAY)
221 UInt64 Texture3D::get_data_size() const
223 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
227 Texture3D::Loader::Loader(Texture3D &t):
228 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
233 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
234 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
239 void Texture3D::Loader::init()
241 add("raw_data", &Loader::raw_data);
242 add("storage", &Loader::storage);
245 void Texture3D::Loader::raw_data(const string &data)
247 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
250 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
253 fmt = get_srgb_pixelformat(fmt);
254 obj.storage(fmt, w, h, d);