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;
72 image(level, get_components(ifmt), get_component_type(ifmt), 0);
75 void Texture3D::image(unsigned level, PixelComponents comp, 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, comp, type, data);
88 BindRestore _bind(this);
92 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
95 glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_components(comp), type, data);
97 allocated |= 1<<level;
98 if(auto_gen_mipmap && level==0)
101 allocated |= (1<<levels)-1;
105 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)
107 if(width==0 || height==0 || depth==0)
108 throw invalid_operation("Texture3D::image");
110 Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
113 comp = get_upload_components(comp);
114 if(ARB_direct_state_access)
115 glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
117 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, comp, type, data);
119 if(auto_gen_mipmap && level==0)
123 void Texture3D::image(const Graphics::Image &img, unsigned lv, bool srgb)
125 unsigned w = img.get_width();
126 unsigned h = img.get_height();
132 throw incompatible_data("Texture3D::load_image");
139 throw incompatible_data("Texture3D::load_image");
144 PixelFormat fmt = pixelformat_from_image(img);
145 PixelComponents comp = get_components(fmt);
146 DataType type = get_component_type(fmt);
148 storage(make_pixelformat(comp, type, srgb), w, h, d, lv);
149 else if(w!=width || h!=height || d!=depth)
150 throw incompatible_data("Texture3D::load_image");
152 PixelStore pstore = PixelStore::from_image(img);
153 BindRestore _bind_ps(pstore);
155 image(0, comp, type, img.get_data());
158 unsigned Texture3D::get_n_levels() const
160 unsigned s = max(width, height);
161 if(target!=GL_TEXTURE_2D_ARRAY)
164 for(; s; s>>=1, ++n) ;
168 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
172 if(target!=GL_TEXTURE_2D_ARRAY)
183 UInt64 Texture3D::get_data_size() const
185 return id ? width*height*depth*get_pixel_size(ifmt) : 0;
189 Texture3D::Loader::Loader(Texture3D &t):
190 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
195 Texture3D::Loader::Loader(Texture3D &t, Collection &c):
196 DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
201 void Texture3D::Loader::init()
203 add("raw_data", &Loader::raw_data);
204 add("storage", &Loader::storage);
205 add("storage", &Loader::storage_levels);
208 void Texture3D::Loader::raw_data(const string &data)
210 obj.image(0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
213 void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
215 obj.storage(fmt, w, h, d);
218 void Texture3D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h, unsigned d, unsigned l)
220 obj.storage(fmt, w, h, d, l);