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");
43 set_internal_format(fmt);
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, ifmt, width, height, depth);
67 glTexStorage3D(target, levels, ifmt, width, height, depth);
69 allocated |= (1<<levels)-1;
73 PixelFormat base_fmt = get_base_pixelformat(ifmt);
74 image(level, base_fmt, get_alloc_type(base_fmt), 0);
78 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
80 if(width==0 || height==0 || depth==0)
81 throw invalid_operation("Texture3D::image");
86 get_level_size(level, w, h, d);
88 if(ARB_texture_storage)
89 return sub_image(level, 0, 0, 0, w, h, d, fmt, type, data);
91 BindRestore _bind(this);
95 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
98 glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_format(fmt), type, data);
100 allocated |= 1<<level;
101 if(auto_gen_mipmap && level==0)
104 allocated |= (1<<levels)-1;
108 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)
110 if(width==0 || height==0 || depth==0)
111 throw invalid_operation("Texture3D::image");
113 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
116 fmt = get_upload_format(fmt);
117 if(ARB_direct_state_access)
118 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, fmt, type, data);
120 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
122 if(auto_gen_mipmap && level==0)
126 void Texture3D::image(const Graphics::Image &img, unsigned lv, bool srgb)
128 unsigned w = img.get_width();
129 unsigned h = img.get_height();
135 throw incompatible_data("Texture3D::load_image");
142 throw incompatible_data("Texture3D::load_image");
147 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
149 storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d, lv);
150 else if(w!=width || h!=height || d!=depth)
151 throw incompatible_data("Texture3D::load_image");
153 PixelStore pstore = PixelStore::from_image(img);
154 BindRestore _bind_ps(pstore);
156 image(0, fmt, UNSIGNED_BYTE, img.get_data());
159 unsigned Texture3D::get_n_levels() const
161 unsigned s = max(width, height);
162 if(target!=GL_TEXTURE_2D_ARRAY)
165 for(; s; s>>=1, ++n) ;
169 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
173 if(target!=GL_TEXTURE_2D_ARRAY)
184 UInt64 Texture3D::get_data_size() const
186 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
190 Texture3D::Loader::Loader(Texture3D &t):
191 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
196 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
197 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
202 void Texture3D::Loader::init()
204 add("raw_data", &Loader::raw_data);
205 add("storage", &Loader::storage);
206 add("storage", &Loader::storage_levels);
209 void Texture3D::Loader::raw_data(const string &data)
211 obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
214 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
216 obj.storage(fmt, w, h, d);
219 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
221 obj.storage(fmt, w, h, d, l);