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(allocated&(1<<level))
54 if(ARB_texture_storage)
56 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
57 if(ARB_direct_state_access)
58 glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
61 BindRestore _bind(this);
62 glTexStorage3D(target, n_levels, ifmt, width, height, depth);
64 allocated |= (1<<n_levels)-1;
68 PixelFormat base_fmt = get_base_pixelformat(ifmt);
69 image(level, base_fmt, get_alloc_type(base_fmt), 0);
73 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
75 if(width==0 || height==0 || depth==0)
76 throw invalid_operation("Texture3D::image");
81 get_level_size(level, w, h, d);
83 if(ARB_texture_storage)
84 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
86 BindRestore _bind(this);
87 glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_format(fmt), type, data);
89 allocated |= 1<<level;
90 if(gen_mipmap && level==0)
92 auto_generate_mipmap();
93 allocated |= (1<<get_n_levels())-1;
97 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)
99 if(width==0 || height==0 || depth==0)
100 throw invalid_operation("Texture3D::image");
102 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
105 fmt = get_upload_format(fmt);
106 if(ARB_direct_state_access)
107 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
109 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
111 if(gen_mipmap && level==0)
112 auto_generate_mipmap();
115 void Texture3D::load_image(const string &fn, int dp)
120 unsigned w = img.get_width();
121 unsigned h = img.get_height();
127 throw incompatible_data("Texture3D::load_image");
133 for(d=h; d*d>h; d>>=2) ;
136 throw incompatible_data("Texture3D::load_image");
143 throw incompatible_data("Texture3D::load_image");
147 throw invalid_argument("Texture3D::load_image");
149 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
151 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
152 else if(w!=width || h!=height || d!=depth)
153 throw incompatible_data("Texture3D::load_image");
155 PixelStore pstore = PixelStore::from_image(img);
156 BindRestore _bind_ps(pstore);
158 image(0, fmt, UNSIGNED_BYTE, img.get_data());
161 void Texture3D::image(const Graphics::Image &img, bool srgb)
163 unsigned w = img.get_width();
164 unsigned h = img.get_height();
170 throw incompatible_data("Texture3D::load_image");
177 throw incompatible_data("Texture3D::load_image");
182 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
184 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
185 else if(w!=width || h!=height || d!=depth)
186 throw incompatible_data("Texture3D::load_image");
188 PixelStore pstore = PixelStore::from_image(img);
189 BindRestore _bind_ps(pstore);
191 image(0, fmt, UNSIGNED_BYTE, img.get_data());
194 unsigned Texture3D::get_n_levels() const
196 unsigned s = max(width, height);
197 if(target!=GL_TEXTURE_2D_ARRAY)
200 for(; s; s>>=1, ++n) ;
204 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
208 if(target!=GL_TEXTURE_2D_ARRAY)
219 UInt64 Texture3D::get_data_size() const
221 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
225 Texture3D::Loader::Loader(Texture3D &t):
226 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
231 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
232 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
237 void Texture3D::Loader::init()
239 add("raw_data", &Loader::raw_data);
240 add("storage", &Loader::storage);
243 void Texture3D::Loader::raw_data(const string &data)
245 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
248 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
251 fmt = get_srgb_pixelformat(fmt);
252 obj.storage(fmt, w, h, d);