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");
45 if(allocated&(1<<level))
48 if(ARB_texture_storage)
50 GLenum fmt = get_gl_pixelformat(storage_fmt);
51 if(ARB_direct_state_access)
52 glTextureStorage1D(id, levels, fmt, width);
56 glTexStorage1D(target, levels, fmt, width);
59 allocated |= (1<<levels)-1;
65 void Texture1D::image(unsigned level, const void *data)
68 throw invalid_operation("Texture1D::image");
70 throw out_of_range("Texture1D::image");
72 if(ARB_texture_storage)
73 return sub_image(level, 0, get_level_size(level), data);
79 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
83 GLenum fmt = get_gl_pixelformat(storage_fmt);
84 GLenum comp = get_gl_components(get_components(storage_fmt));
85 GLenum type = get_gl_type(get_component_type(storage_fmt));
86 glTexImage1D(target, level, fmt, get_level_size(level), 0, comp, type, data);
88 allocated |= 1<<level;
91 void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
93 if(comp!=get_components(format) || type!=get_component_type(format))
94 throw incompatible_data("Texture1D::image");
98 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
101 throw invalid_operation("Texture1D::sub_image");
103 throw out_of_range("Texture1D::sub_image");
107 GLenum comp = get_gl_components(get_components(storage_fmt));
108 GLenum type = get_gl_type(get_component_type(storage_fmt));
109 if(ARB_direct_state_access)
110 glTextureSubImage1D(id, level, x, wd, comp, type, data);
114 glTexSubImage1D(target, level, x, wd, comp, type, data);
118 void Texture1D::sub_image(unsigned level, int x, unsigned wd, PixelComponents comp, DataType type, const void *data)
120 if(comp!=get_components(format) || type!=get_component_type(format))
121 throw incompatible_data("Texture1D::sub_image");
122 sub_image(level, x, wd, data);
125 void Texture1D::image(const Graphics::Image &img, unsigned lv)
127 if(img.get_height()!=1)
128 throw incompatible_data("Texture1D::image");
130 unsigned w = img.get_width();
131 PixelFormat fmt = pixelformat_from_image(img);
132 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
134 image(0, img.get_pixels());
137 unsigned Texture1D::get_n_levels() const
140 for(unsigned s=width; s; s>>=1, ++n) ;
144 unsigned Texture1D::get_level_size(unsigned level) const
149 uint64_t Texture1D::get_data_size() const
151 return id ? width*get_pixel_size(storage_fmt) : 0;
155 Texture1D::Loader::Loader(Texture1D &t):
156 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
161 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
162 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
167 void Texture1D::Loader::init()
169 add("raw_data", &Loader::raw_data);
170 add("storage", &Loader::storage);
171 add("storage", &Loader::storage_levels);
174 void Texture1D::Loader::raw_data(const string &data)
176 obj.image(0, data.data());
179 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
184 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
186 obj.storage(fmt, w, l);