1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_texture_storage.h>
4 #include <msp/gl/extensions/msp_texture1d.h>
13 Texture1D::Texture1D():
14 Texture(GL_TEXTURE_1D),
18 static Require _req(MSP_texture1D);
21 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
25 if(fmt!=format || wd!=width || (lv && lv!=levels))
26 throw incompatible_data("Texture1D::storage");
30 throw invalid_argument("Texture1D::storage");
34 levels = get_n_levels();
36 levels = min(levels, lv);
39 void Texture1D::allocate(unsigned level)
42 throw invalid_operation("Texture1D::allocate");
44 throw invalid_argument("Texture1D::allocate");
46 bool direct = ARB_texture_storage && ARB_direct_state_access;
49 glActiveTexture(GL_TEXTURE0);
50 glBindTexture(target, id);
56 glBindTexture(target, 0);
59 void Texture1D::allocate_(unsigned level)
61 if(allocated&(1<<level))
64 if(ARB_texture_storage)
66 if(ARB_direct_state_access)
67 glTextureStorage1D(id, levels, storage_fmt, width);
69 glTexStorage1D(target, levels, storage_fmt, width);
71 allocated |= (1<<levels)-1;
77 void Texture1D::image(unsigned level, const void *data)
80 throw invalid_operation("Texture1D::image");
82 throw out_of_range("Texture1D::image");
84 if(ARB_texture_storage)
85 return sub_image(level, 0, get_level_size(level), data);
87 glActiveTexture(GL_TEXTURE0);
88 glBindTexture(target, id);
92 if(auto_gen_mipmap && level==0)
95 allocated |= (1<<levels)-1;
98 glBindTexture(target, 0);
101 void Texture1D::image_(unsigned level, const void *data)
105 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
109 PixelComponents comp = get_components(storage_fmt);
110 GLenum type = get_gl_type(get_component_type(storage_fmt));
111 glTexImage1D(target, level, storage_fmt, get_level_size(level), 0, comp, type, data);
113 allocated |= 1<<level;
116 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
118 if(comp!=get_components(format) || type!=get_component_type(format))
119 throw incompatible_data("Texture1D::image");
123 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
126 throw invalid_operation("Texture1D::sub_image");
128 throw out_of_range("Texture1D::sub_image");
130 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
133 glActiveTexture(GL_TEXTURE0);
134 glBindTexture(target, id);
139 PixelComponents comp = get_components(storage_fmt);
140 GLenum type = get_gl_type(get_component_type(storage_fmt));
141 if(ARB_direct_state_access)
142 glTextureSubImage1D(id, level, x, wd, comp, type, data);
144 glTexSubImage1D(target, level, x, wd, comp, type, data);
146 if(auto_gen_mipmap && level==0)
150 glBindTexture(target, 0);
153 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
155 if(comp!=get_components(format) || type!=get_component_type(format))
156 throw incompatible_data("Texture1D::sub_image");
157 sub_image(level, x, wd, data);
160 void Texture1D::image(const Graphics::Image &img, unsigned lv)
162 if(img.get_height()!=1)
163 throw incompatible_data("Texture1D::image");
165 unsigned w = img.get_width();
166 PixelFormat fmt = pixelformat_from_image(img);
167 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
169 image(0, img.get_pixels());
172 unsigned Texture1D::get_n_levels() const
175 for(unsigned s=width; s; s>>=1, ++n) ;
179 unsigned Texture1D::get_level_size(unsigned level) const
184 UInt64 Texture1D::get_data_size() const
186 return id ? width*get_pixel_size(storage_fmt) : 0;
190 Texture1D::Loader::Loader(Texture1D &t):
191 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
196 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
197 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
202 void Texture1D::Loader::init()
204 add("raw_data", &Loader::raw_data);
205 add("storage", &Loader::storage);
206 add("storage", &Loader::storage_levels);
209 void Texture1D::Loader::raw_data(const string &data)
211 obj.image(0, data.data());
214 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
219 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
221 obj.storage(fmt, w, l);