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>
14 Texture1D::Texture1D():
15 Texture(GL_TEXTURE_1D),
19 static Require _req(MSP_texture1D);
22 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
26 if(fmt!=format || wd!=width || (lv && lv!=levels))
27 throw incompatible_data("Texture1D::storage");
31 throw invalid_argument("Texture1D::storage");
35 levels = get_n_levels();
37 levels = min(levels, lv);
40 void Texture1D::allocate(unsigned level)
43 throw invalid_operation("Texture1D::allocate");
45 throw invalid_argument("Texture1D::allocate");
47 bool direct = ARB_texture_storage && ARB_direct_state_access;
50 glActiveTexture(GL_TEXTURE0);
51 glBindTexture(target, id);
57 glBindTexture(target, 0);
60 void Texture1D::allocate_(unsigned level)
62 if(allocated&(1<<level))
65 if(ARB_texture_storage)
67 if(ARB_direct_state_access)
68 glTextureStorage1D(id, levels, storage_fmt, width);
70 glTexStorage1D(target, levels, storage_fmt, width);
72 allocated |= (1<<levels)-1;
78 void Texture1D::image(unsigned level, const void *data)
81 throw invalid_operation("Texture1D::image");
83 throw out_of_range("Texture1D::image");
85 if(ARB_texture_storage)
86 return sub_image(level, 0, get_level_size(level), data);
88 glActiveTexture(GL_TEXTURE0);
89 glBindTexture(target, id);
93 if(auto_gen_mipmap && level==0)
96 allocated |= (1<<levels)-1;
99 glBindTexture(target, 0);
102 void Texture1D::image_(unsigned level, const void *data)
106 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
110 PixelComponents comp = get_components(storage_fmt);
111 GLenum type = get_gl_type(get_component_type(storage_fmt));
112 glTexImage1D(target, level, storage_fmt, get_level_size(level), 0, comp, type, data);
114 allocated |= 1<<level;
117 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
119 if(comp!=get_components(format) || type!=get_component_type(format))
120 throw incompatible_data("Texture1D::image");
124 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
127 throw invalid_operation("Texture1D::sub_image");
129 throw out_of_range("Texture1D::sub_image");
131 bool direct = (ARB_direct_state_access && (ARB_texture_storage || (allocated&(1<<level))));
134 glActiveTexture(GL_TEXTURE0);
135 glBindTexture(target, id);
140 PixelComponents comp = get_components(storage_fmt);
141 GLenum type = get_gl_type(get_component_type(storage_fmt));
142 if(ARB_direct_state_access)
143 glTextureSubImage1D(id, level, x, wd, comp, type, data);
145 glTexSubImage1D(target, level, x, wd, comp, type, data);
147 if(auto_gen_mipmap && level==0)
151 glBindTexture(target, 0);
154 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
156 if(comp!=get_components(format) || type!=get_component_type(format))
157 throw incompatible_data("Texture1D::sub_image");
158 sub_image(level, x, wd, data);
161 void Texture1D::image(const Graphics::Image &img, unsigned lv)
163 if(img.get_height()!=1)
164 throw incompatible_data("Texture1D::image");
166 unsigned w = img.get_width();
167 PixelFormat fmt = pixelformat_from_image(img);
168 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
170 image(0, img.get_pixels());
173 unsigned Texture1D::get_n_levels() const
176 for(unsigned s=width; s; s>>=1, ++n) ;
180 unsigned Texture1D::get_level_size(unsigned level) const
185 UInt64 Texture1D::get_data_size() const
187 return id ? width*get_pixel_size(storage_fmt) : 0;
191 Texture1D::Loader::Loader(Texture1D &t):
192 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
197 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
198 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
203 void Texture1D::Loader::init()
205 add("raw_data", &Loader::raw_data);
206 add("storage", &Loader::storage);
207 add("storage", &Loader::storage_levels);
210 void Texture1D::Loader::raw_data(const string &data)
212 obj.image(0, data.data());
215 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
220 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
222 obj.storage(fmt, w, l);