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/graphics/image.h>
9 #include "pixelstore.h"
10 #include "texture3d.h"
17 Texture3D::Texture3D(GLenum t):
25 Texture3D::Texture3D():
26 Texture(GL_TEXTURE_3D),
32 static Require _req(EXT_texture3D);
35 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
38 throw invalid_operation("Texture3D::storage");
39 if(wd==0 || ht==0 || dp==0)
40 throw invalid_argument("Texture3D::storage");
42 set_internal_format(fmt);
48 void Texture3D::allocate(unsigned level)
50 if(allocated&(1<<level))
53 if(ARB_texture_storage)
55 unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
56 if(ARB_direct_state_access)
57 glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
60 BindRestore _bind(this);
61 glTexStorage3D(target, n_levels, ifmt, width, height, depth);
63 allocated |= (1<<n_levels)-1;
67 PixelFormat base_fmt = get_base_pixelformat(ifmt);
68 image(level, base_fmt, get_alloc_type(base_fmt), 0);
72 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
74 if(width==0 || height==0 || depth==0)
75 throw invalid_operation("Texture3D::image");
80 get_level_size(level, w, h, d);
82 if(ARB_texture_storage)
83 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
85 BindRestore _bind(this);
86 glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_format(fmt), type, data);
88 allocated |= 1<<level;
89 if(gen_mipmap && level==0)
91 auto_generate_mipmap();
92 allocated |= (1<<get_n_levels())-1;
96 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)
98 if(width==0 || height==0 || depth==0)
99 throw invalid_operation("Texture3D::image");
101 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
104 fmt = get_upload_format(fmt);
105 if(ARB_direct_state_access)
106 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
108 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
110 if(gen_mipmap && level==0)
111 auto_generate_mipmap();
114 void Texture3D::load_image(const string &fn, int dp)
119 unsigned w = img.get_width();
120 unsigned h = img.get_height();
126 throw incompatible_data("Texture3D::load_image");
132 for(d=h; d*d>h; d>>=2) ;
135 throw incompatible_data("Texture3D::load_image");
142 throw incompatible_data("Texture3D::load_image");
146 throw invalid_argument("Texture3D::load_image");
148 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
150 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
151 else if(w!=width || h!=height || d!=depth)
152 throw incompatible_data("Texture3D::load_image");
154 PixelStore pstore = PixelStore::from_image(img);
155 BindRestore _bind_ps(pstore);
157 image(0, fmt, UNSIGNED_BYTE, img.get_data());
160 void Texture3D::image(const Graphics::Image &img, bool srgb)
162 unsigned w = img.get_width();
163 unsigned h = img.get_height();
169 throw incompatible_data("Texture3D::load_image");
176 throw incompatible_data("Texture3D::load_image");
181 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
183 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
184 else if(w!=width || h!=height || d!=depth)
185 throw incompatible_data("Texture3D::load_image");
187 PixelStore pstore = PixelStore::from_image(img);
188 BindRestore _bind_ps(pstore);
190 image(0, fmt, UNSIGNED_BYTE, img.get_data());
193 unsigned Texture3D::get_n_levels() const
195 unsigned s = max(width, height);
196 if(target!=GL_TEXTURE_2D_ARRAY)
199 for(; s; s>>=1, ++n) ;
203 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
207 if(target!=GL_TEXTURE_2D_ARRAY)
218 UInt64 Texture3D::get_data_size() const
220 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
224 Texture3D::Loader::Loader(Texture3D &t):
225 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
230 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
231 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
236 void Texture3D::Loader::init()
238 add("raw_data", &Loader::raw_data);
239 add("storage", &Loader::storage);
242 void Texture3D::Loader::raw_data(const string &data)
244 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
247 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
250 fmt = get_srgb_pixelformat(fmt);
251 obj.storage(fmt, w, h, d);