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 storage(pixelformat_from_image(img, use_srgb_format), img.get_width(), lv);
89 image(0, img.get_pixels());
92 unsigned Texture1D::get_n_levels() const
95 for(unsigned s=width; s; s>>=1, ++n) ;
99 unsigned Texture1D::get_level_size(unsigned level) const
104 uint64_t Texture1D::get_data_size() const
106 return id ? width*get_pixel_size(storage_fmt) : 0;
110 Texture1D::Loader::Loader(Texture1D &t):
111 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t)
116 Texture1D::Loader::Loader(Texture1D &t, Collection &c):
117 DataFile::DerivedObjectLoader<Texture1D, Texture::Loader>(t, c)
122 void Texture1D::Loader::init()
124 add("raw_data", &Loader::raw_data);
125 add("storage", &Loader::storage);
126 add("storage", &Loader::storage_levels);
129 void Texture1D::Loader::raw_data(const string &data)
131 obj.image(0, data.data());
134 void Texture1D::Loader::storage(PixelFormat fmt, unsigned w)
139 void Texture1D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned l)
141 obj.storage(fmt, w, l);