1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_texture_storage.h>
3 #include <msp/gl/extensions/msp_texture1d.h>
12 Texture1D::Texture1D():
13 Texture(GL_TEXTURE_1D),
16 static Require _req(MSP_texture1D);
19 void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
23 if(fmt!=format || wd!=width || (lv && lv!=levels))
24 throw incompatible_data("Texture1D::storage");
28 throw invalid_argument("Texture1D::storage");
32 levels = get_n_levels();
34 levels = min(levels, lv);
36 GLenum gl_fmt = get_gl_pixelformat(storage_fmt);
37 if(ARB_texture_storage)
39 if(ARB_direct_state_access)
40 glTextureStorage1D(id, levels, gl_fmt, width);
44 glTexStorage1D(target, levels, gl_fmt, width);
50 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
51 GLenum comp = get_gl_components(get_components(storage_fmt));
52 GLenum type = get_gl_type(get_component_type(storage_fmt));
53 for(unsigned i=0; i<levels; ++i)
54 glTexImage1D(target, i, gl_fmt, get_level_size(i), 0, comp, type, 0);
60 void Texture1D::image(unsigned level, const void *data)
62 return sub_image(level, 0, get_level_size(level), data);
65 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
68 throw invalid_operation("Texture1D::sub_image");
70 throw out_of_range("Texture1D::sub_image");
72 GLenum comp = get_gl_components(get_components(storage_fmt));
73 GLenum type = get_gl_type(get_component_type(storage_fmt));
74 if(ARB_direct_state_access)
75 glTextureSubImage1D(id, level, x, wd, comp, type, data);
79 glTexSubImage1D(target, level, x, wd, comp, type, data);
83 void Texture1D::image(const Graphics::Image &img, unsigned lv)
85 if(img.get_height()!=1)
86 throw incompatible_data("Texture1D::image");
88 unsigned w = img.get_width();
89 PixelFormat fmt = pixelformat_from_image(img);
90 storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, lv);
92 image(0, img.get_pixels());
95 unsigned Texture1D::get_n_levels() const
98 for(unsigned s=width; s; s>>=1, ++n) ;
102 unsigned Texture1D::get_level_size(unsigned level) const
107 uint64_t Texture1D::get_data_size() const
109 return id ? width*get_pixel_size(storage_fmt) : 0;
113 Texture1D::Loader::Loader(Texture1D &t):
114 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
119 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
120 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
125 void Texture1D::Loader::init()
127 add("raw_data", &Loader::raw_data);
128 add("storage", &Loader::storage);
129 add("storage", &Loader::storage_levels);
132 void Texture1D::Loader::raw_data(const string &data)
134 obj.image(0, data.data());
137 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
142 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
144 obj.storage(fmt, w, l);